|
| 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_VAAPI_H265_ENCODER_H_ |
| 18 | +#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_VAAPI_H265_ENCODER_H_ |
| 19 | + |
| 20 | +#include "media/video/codec/EncoderOutputProcessor.h" |
| 21 | +#include "media/video/codec/linux/VaapiEncodeSession.h" |
| 22 | +#include "media/video/codec/linux/VaapiLibrary.h" |
| 23 | + |
| 24 | +#include "api/video/video_frame.h" |
| 25 | +#include "api/video_codecs/sdp_video_format.h" |
| 26 | +#include "api/video_codecs/video_codec.h" |
| 27 | +#include "api/video_codecs/video_encoder.h" |
| 28 | + |
| 29 | +#include <va/va.h> |
| 30 | +#include <va/va_enc_hevc.h> |
| 31 | + |
| 32 | +#include <cstdint> |
| 33 | +#include <string> |
| 34 | +#include <vector> |
| 35 | + |
| 36 | +namespace jni |
| 37 | +{ |
| 38 | + // Encodes H.265 Main with the GPU driver's VA-API encoder, as Intel and |
| 39 | + // AMD drivers provide it on Linux. Experimental: it has not run on such a |
| 40 | + // GPU yet, and is used only where it is turned on. |
| 41 | + // |
| 42 | + // It works as VaapiH264Encoder does: synchronous, one reference frame, |
| 43 | + // P-frames only, the driver writing the parameter sets and slice headers |
| 44 | + // from the parameters it is given. The block sizes and coding tools are |
| 45 | + // those the driver says it takes, as FFmpeg's VA-API encoder chooses them. |
| 46 | + // The frame size is kept to a multiple of the smallest coding block, so |
| 47 | + // the coded picture is the frame and needs no cropping. There is no |
| 48 | + // software H.265 to fall back to: anything that fails, including a driver |
| 49 | + // that writes no parameter sets into key frames, is an error, upon which |
| 50 | + // WebRTC switches to the next codec it negotiated. |
| 51 | + class VaapiH265Encoder : public webrtc::VideoEncoder |
| 52 | + { |
| 53 | + public: |
| 54 | + VaapiH265Encoder(VaapiLibrary & library, VAEntrypoint entrypoint, const webrtc::SdpVideoFormat & format); |
| 55 | + ~VaapiH265Encoder() override; |
| 56 | + |
| 57 | + int32_t InitEncode(const webrtc::VideoCodec * codecSettings, const Settings & settings) override; |
| 58 | + int32_t RegisterEncodeCompleteCallback(webrtc::EncodedImageCallback * callback) override; |
| 59 | + int32_t Release() override; |
| 60 | + int32_t Encode(const webrtc::VideoFrame & frame, const std::vector<webrtc::VideoFrameType> * frameTypes) override; |
| 61 | + void SetRates(const RateControlParameters & parameters) override; |
| 62 | + EncoderInfo GetEncoderInfo() const override; |
| 63 | + |
| 64 | + private: |
| 65 | + // What the driver says about the HEVC encoder it has. |
| 66 | + void QueryCapabilities(); |
| 67 | + bool Submit(bool idr); |
| 68 | + |
| 69 | + void FillSequence(VAEncSequenceParameterBufferHEVC & sequence) const; |
| 70 | + void FillPicture(VAEncPictureParameterBufferHEVC & picture, bool idr) const; |
| 71 | + void FillSlice(VAEncSliceParameterBufferHEVC & slice, bool idr) const; |
| 72 | + // The picture the current one refers to. |
| 73 | + VAPictureHEVC Reference() const; |
| 74 | + |
| 75 | + private: |
| 76 | + VaapiLibrary & library; |
| 77 | + const VAEntrypoint entrypoint; |
| 78 | + const std::string implementationName; |
| 79 | + VaapiEncodeSession session; |
| 80 | + |
| 81 | + // The sizes, in log2 of luma samples, and the tools the driver |
| 82 | + // takes. |
| 83 | + uint32_t log2CtbSize; |
| 84 | + uint32_t log2MinCbSize; |
| 85 | + uint32_t log2MinTbSize; |
| 86 | + uint32_t log2MaxTbSize; |
| 87 | + uint32_t maxTransformDepthInter; |
| 88 | + uint32_t maxTransformDepthIntra; |
| 89 | + bool amp; |
| 90 | + bool sao; |
| 91 | + bool signDataHiding; |
| 92 | + bool transformSkip; |
| 93 | + bool cuQpDelta; |
| 94 | + |
| 95 | + bool initialized; |
| 96 | + webrtc::VideoCodec codecSettings; |
| 97 | + uint32_t widthInCtbs; |
| 98 | + uint32_t heightInCtbs; |
| 99 | + uint32_t bitrateBps; |
| 100 | + uint32_t framerate; |
| 101 | + bool ratesChanged; |
| 102 | + |
| 103 | + // The picture order count since the last IDR picture, and which of |
| 104 | + // the reconstructed surfaces holds the current picture. |
| 105 | + int32_t pictureOrder; |
| 106 | + int current; |
| 107 | + bool referenceValid; |
| 108 | + |
| 109 | + webrtc::EncodedImageCallback * callback; |
| 110 | + |
| 111 | + EncoderOutputProcessor outputProcessor; |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +#endif |
0 commit comments