1 /* 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_VIDEO_CODECS_VIDEO_DECODER_H_ 12 #define API_VIDEO_CODECS_VIDEO_DECODER_H_ 13 14 #include <memory> 15 #include <string> 16 #include <vector> 17 18 #include "absl/types/optional.h" 19 #include "api/video/encoded_image.h" 20 #include "api/video/render_resolution.h" 21 #include "api/video/video_codec_type.h" 22 #include "api/video/video_frame.h" 23 #include "rtc_base/system/rtc_export.h" 24 25 namespace webrtc { 26 27 class RTC_EXPORT DecodedImageCallback { 28 public: ~DecodedImageCallback()29 virtual ~DecodedImageCallback() {} 30 31 virtual int32_t Decoded(VideoFrame& decodedImage) = 0; 32 // Provides an alternative interface that allows the decoder to specify the 33 // decode time excluding waiting time for any previous pending frame to 34 // return. This is necessary for breaking positive feedback in the delay 35 // estimation when the decoder has a single output buffer. 36 virtual int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms); 37 38 // TODO(sakal): Remove other implementations when upstream projects have been 39 // updated. 40 virtual void Decoded(VideoFrame& decodedImage, 41 absl::optional<int32_t> decode_time_ms, 42 absl::optional<uint8_t> qp); 43 }; 44 45 class RTC_EXPORT VideoDecoder { 46 public: 47 struct DecoderInfo { 48 // Descriptive name of the decoder implementation. 49 std::string implementation_name; 50 51 // True if the decoder is backed by hardware acceleration. 52 bool is_hardware_accelerated = false; 53 54 std::string ToString() const; 55 bool operator==(const DecoderInfo& rhs) const; 56 bool operator!=(const DecoderInfo& rhs) const { return !(*this == rhs); } 57 }; 58 59 class Settings { 60 public: 61 Settings() = default; 62 Settings(const Settings&) = default; 63 Settings& operator=(const Settings&) = default; 64 ~Settings() = default; 65 66 // The size of pool which is used to store video frame buffers inside 67 // decoder. If value isn't present some codec-default value will be used. If 68 // value is present and decoder doesn't have buffer pool the value will be 69 // ignored. 70 absl::optional<int> buffer_pool_size() const; 71 void set_buffer_pool_size(absl::optional<int> value); 72 73 // When valid, user of the VideoDecoder interface shouldn't `Decode` 74 // encoded images with render resolution larger than width and height 75 // specified here. 76 RenderResolution max_render_resolution() const; 77 void set_max_render_resolution(RenderResolution value); 78 79 // Maximum number of cpu cores the decoder is allowed to use in parallel. 80 // Must be positive. number_of_cores()81 int number_of_cores() const { return number_of_cores_; } 82 void set_number_of_cores(int value); 83 84 // Codec of encoded images user of the VideoDecoder interface will `Decode`. codec_type()85 VideoCodecType codec_type() const { return codec_type_; } set_codec_type(VideoCodecType value)86 void set_codec_type(VideoCodecType value) { codec_type_ = value; } 87 88 private: 89 absl::optional<int> buffer_pool_size_; 90 RenderResolution max_resolution_; 91 int number_of_cores_ = 1; 92 VideoCodecType codec_type_ = kVideoCodecGeneric; 93 }; 94 95 virtual ~VideoDecoder() = default; 96 97 // Prepares decoder to handle incoming encoded frames. Can be called multiple 98 // times, in such case only latest `settings` are in effect. 99 virtual bool Configure(const Settings& settings) = 0; 100 101 virtual int32_t Decode(const EncodedImage& input_image, 102 bool missing_frames, 103 int64_t render_time_ms) = 0; 104 105 virtual int32_t RegisterDecodeCompleteCallback( 106 DecodedImageCallback* callback) = 0; 107 108 virtual int32_t Release() = 0; 109 110 virtual DecoderInfo GetDecoderInfo() const; 111 112 // Deprecated, use GetDecoderInfo().implementation_name instead. 113 virtual const char* ImplementationName() const; 114 }; 115 buffer_pool_size()116inline absl::optional<int> VideoDecoder::Settings::buffer_pool_size() const { 117 return buffer_pool_size_; 118 } 119 set_buffer_pool_size(absl::optional<int> value)120inline void VideoDecoder::Settings::set_buffer_pool_size( 121 absl::optional<int> value) { 122 buffer_pool_size_ = value; 123 } 124 max_render_resolution()125inline RenderResolution VideoDecoder::Settings::max_render_resolution() const { 126 return max_resolution_; 127 } 128 set_max_render_resolution(RenderResolution value)129inline void VideoDecoder::Settings::set_max_render_resolution( 130 RenderResolution value) { 131 max_resolution_ = value; 132 } 133 134 } // namespace webrtc 135 136 #endif // API_VIDEO_CODECS_VIDEO_DECODER_H_ 137