|
| 1 | +/* |
| 2 | + * Copyright 2026 Alex Andres |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef JNI_WEBRTC_MEDIA_VIDEO_CODEC_ENCODER_OUTPUT_PROCESSOR_H_ |
| 18 | +#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_ENCODER_OUTPUT_PROCESSOR_H_ |
| 19 | + |
| 20 | +#include "api/video/encoded_image.h" |
| 21 | +#include "api/video/video_codec_type.h" |
| 22 | +#include "api/video_codecs/sdp_video_format.h" |
| 23 | +#include "api/video_codecs/video_encoder.h" |
| 24 | +#include "common_video/h264/h264_bitstream_parser.h" |
| 25 | +#include "modules/video_coding/codecs/h264/include/h264_globals.h" |
| 26 | +#include "modules/video_coding/include/video_codec_interface.h" |
| 27 | +#include "modules/video_coding/svc/scalable_video_controller_no_layering.h" |
| 28 | + |
| 29 | +#include <cstdint> |
| 30 | +#include <span> |
| 31 | +#include <vector> |
| 32 | + |
| 33 | +namespace jni |
| 34 | +{ |
| 35 | + // Turns what a hardware encoder produces for a frame into what WebRTC |
| 36 | + // sends, the same way for every hardware encoder: H.264 as Annex B NAL |
| 37 | + // units, AV1 as OBUs in the low overhead bitstream format. |
| 38 | + // |
| 39 | + // A receiver can start decoding only from a key frame that carries the |
| 40 | + // parameter sets, the SPS and PPS of H.264 or the sequence header of AV1. |
| 41 | + // Encoders put them in front of the first key frame, not necessarily in |
| 42 | + // front of every one, so they are kept and put back where missing. The |
| 43 | + // frame dependencies WebRTC needs for AV1 are those of a stream without |
| 44 | + // layers, which is all hardware encoders produce here. |
| 45 | + class EncoderOutputProcessor |
| 46 | + { |
| 47 | + public: |
| 48 | + EncoderOutputProcessor(webrtc::VideoCodecType codec, const webrtc::SdpVideoFormat & format); |
| 49 | + |
| 50 | + // Forgets the parameter sets, for a stream that starts over. |
| 51 | + void Reset(); |
| 52 | + |
| 53 | + // Fills the image with the bitstream of one frame, and the codec |
| 54 | + // specific information WebRTC needs to send it. The caller sets |
| 55 | + // the size of the image before, which the AV1 frame dependencies |
| 56 | + // refer to, and its timestamps. The frame is a key |
| 57 | + // frame if the encoder says so or, for H.264, if it holds an IDR |
| 58 | + // slice. Returns false if the bitstream cannot be parsed, or a key |
| 59 | + // frame lacks parameter sets and there are none to put back. |
| 60 | + bool Process(std::span<const uint8_t> bitstream, bool keyFrame, webrtc::EncodedImage & image, |
| 61 | + webrtc::CodecSpecificInfo & info); |
| 62 | + |
| 63 | + // The quality scaling settings that suit the codec: the thresholds |
| 64 | + // of WebRTC's H.264 encoder, and none for AV1, whose QP is not |
| 65 | + // parsed from the bitstream. |
| 66 | + webrtc::VideoEncoder::ScalingSettings GetScalingSettings() const; |
| 67 | + |
| 68 | + private: |
| 69 | + bool ProcessH264(std::span<const uint8_t> bitstream, bool & keyFrame, std::vector<uint8_t> & output); |
| 70 | + bool ProcessAv1(std::span<const uint8_t> bitstream, bool keyFrame, std::vector<uint8_t> & output); |
| 71 | + |
| 72 | + void FillCodecSpecificInfo(const webrtc::EncodedImage & image, bool keyFrame, |
| 73 | + webrtc::CodecSpecificInfo & info); |
| 74 | + |
| 75 | + private: |
| 76 | + const webrtc::VideoCodecType codec; |
| 77 | + webrtc::H264PacketizationMode packetizationMode; |
| 78 | + |
| 79 | + // The SPS and PPS, or the sequence header OBU, last seen. |
| 80 | + std::vector<uint8_t> parameterSets; |
| 81 | + |
| 82 | + webrtc::H264BitstreamParser h264Parser; |
| 83 | + webrtc::ScalableVideoControllerNoLayering svcController; |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +#endif |
0 commit comments