|
| 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_VT_VP9_DECODER_H_ |
| 18 | +#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_VT_VP9_DECODER_H_ |
| 19 | + |
| 20 | +#include "api/video/encoded_image.h" |
| 21 | +#include "api/video_codecs/video_decoder.h" |
| 22 | +#include "modules/video_coding/utility/vp9_uncompressed_header_parser.h" |
| 23 | + |
| 24 | +#include <CoreMedia/CoreMedia.h> |
| 25 | +#include <CoreVideo/CoreVideo.h> |
| 26 | +#include <VideoToolbox/VideoToolbox.h> |
| 27 | + |
| 28 | +#include <cstdint> |
| 29 | + |
| 30 | +namespace jni |
| 31 | +{ |
| 32 | + // Decodes VP9 profile 0 on the media engine or GPU of a Mac, through the |
| 33 | + // VP9 decoder VideoToolbox offers once it is registered. |
| 34 | + // |
| 35 | + // The decompression session needs the properties of the stream, so it is |
| 36 | + // created on the first key frame, from the header the key frame carries, |
| 37 | + // and again whenever a key frame changes the size or the range. Decoding |
| 38 | + // is synchronous: each frame comes out of VideoToolbox before Decode |
| 39 | + // returns. A frame is handed on as the CVPixelBuffer VideoToolbox made, |
| 40 | + // without a copy. |
| 41 | + // |
| 42 | + // Whatever VideoToolbox cannot decode in place goes to the software |
| 43 | + // decoder, by returning WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE: a profile |
| 44 | + // other than 0, a size outside what the hardware decodes, frames with |
| 45 | + // spatial layers, and a decoder that keeps failing. |
| 46 | + class VTVp9Decoder : public webrtc::VideoDecoder |
| 47 | + { |
| 48 | + public: |
| 49 | + VTVp9Decoder(); |
| 50 | + ~VTVp9Decoder() override; |
| 51 | + |
| 52 | + using webrtc::VideoDecoder::Decode; |
| 53 | + |
| 54 | + bool Configure(const Settings & settings) override; |
| 55 | + int32_t Decode(const webrtc::EncodedImage & image, int64_t renderTimeMs) override; |
| 56 | + int32_t RegisterDecodeCompleteCallback(webrtc::DecodedImageCallback * callback) override; |
| 57 | + int32_t Release() override; |
| 58 | + DecoderInfo GetDecoderInfo() const override; |
| 59 | + const char * ImplementationName() const override; |
| 60 | + |
| 61 | + private: |
| 62 | + // What a session is created for. A key frame that differs from it |
| 63 | + // needs a new format description, and often a new session. |
| 64 | + struct StreamConfig |
| 65 | + { |
| 66 | + int width = 0; |
| 67 | + int height = 0; |
| 68 | + bool fullRange = false; |
| 69 | + webrtc::Vp9ColorSpace colorSpace = webrtc::Vp9ColorSpace::CS_UNKNOWN; |
| 70 | + |
| 71 | + bool operator==(const StreamConfig & other) const; |
| 72 | + }; |
| 73 | + |
| 74 | + // The result of decoding one frame, filled in by the callback of |
| 75 | + // the session. |
| 76 | + struct Output |
| 77 | + { |
| 78 | + OSStatus status = noErr; |
| 79 | + CVImageBufferRef image = nullptr; |
| 80 | + }; |
| 81 | + |
| 82 | + static void OnOutput(void * decoder, void * frame, OSStatus status, VTDecodeInfoFlags flags, |
| 83 | + CVImageBufferRef image, CMTime presentationTime, CMTime duration); |
| 84 | + |
| 85 | + // Reads the stream properties from a key frame header. Returns |
| 86 | + // false for a stream the hardware decoder does not take. |
| 87 | + static bool ReadConfig(const webrtc::Vp9UncompressedHeader & header, StreamConfig & config); |
| 88 | + |
| 89 | + // Makes sure there is a session for the stream, creating or |
| 90 | + // replacing it where the stream changed. |
| 91 | + bool EnsureSession(const StreamConfig & config); |
| 92 | + bool CreateSession(const StreamConfig & config, CMVideoFormatDescriptionRef format); |
| 93 | + void DestroySession(); |
| 94 | + |
| 95 | + // Decodes one encoded frame as a single sample. |
| 96 | + OSStatus DecodeSample(const webrtc::EncodedImage & image, Output & output); |
| 97 | + |
| 98 | + // Counts a failure and tells WebRTC what to do about it. |
| 99 | + int32_t Fail(OSStatus status); |
| 100 | + |
| 101 | + void Deliver(const webrtc::EncodedImage & image, int64_t renderTimeMs, CVImageBufferRef pixels); |
| 102 | + |
| 103 | + private: |
| 104 | + webrtc::DecodedImageCallback * callback; |
| 105 | + |
| 106 | + VTDecompressionSessionRef session; |
| 107 | + CMVideoFormatDescriptionRef format; |
| 108 | + StreamConfig active; |
| 109 | + |
| 110 | + // Set after a failure, until the next key frame. |
| 111 | + bool requireKeyFrame; |
| 112 | + int consecutiveErrors; |
| 113 | + int64_t sampleCount; |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +#endif |
0 commit comments