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