blob: 1104b004dfd6245df7ecd0ceb7c7ae705b1a193b [file] [log] [blame]
ilnikd60d06a2017-04-05 03:02:20 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_VIDEO_CODECS_VIDEO_DECODER_H_
12#define API_VIDEO_CODECS_VIDEO_DECODER_H_
ilnikd60d06a2017-04-05 03:02:20 -070013
14#include <memory>
15#include <string>
16#include <vector>
17
Danil Chapovalovecc46ef2021-08-09 15:30:47 +020018#include "absl/types/optional.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020019#include "api/video/encoded_image.h"
Danil Chapovalovecc46ef2021-08-09 15:30:47 +020020#include "api/video/render_resolution.h"
21#include "api/video/video_codec_type.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/video/video_frame.h"
Niels Möller802506c2018-05-31 10:44:51 +020023#include "api/video_codecs/video_codec.h"
Mirko Bonadei276827c2018-10-16 14:13:50 +020024#include "rtc_base/system/rtc_export.h"
ilnikd60d06a2017-04-05 03:02:20 -070025
26namespace webrtc {
27
Mirko Bonadei977b46a2018-10-24 16:22:04 +020028class RTC_EXPORT DecodedImageCallback {
ilnikd60d06a2017-04-05 03:02:20 -070029 public:
30 virtual ~DecodedImageCallback() {}
31
32 virtual int32_t Decoded(VideoFrame& decodedImage) = 0;
33 // Provides an alternative interface that allows the decoder to specify the
34 // decode time excluding waiting time for any previous pending frame to
35 // return. This is necessary for breaking positive feedback in the delay
36 // estimation when the decoder has a single output buffer.
Niels Möllerbe682d42018-03-27 08:31:45 +020037 virtual int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms);
38
ilnikd60d06a2017-04-05 03:02:20 -070039 // TODO(sakal): Remove other implementations when upstream projects have been
40 // updated.
41 virtual void Decoded(VideoFrame& decodedImage,
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020042 absl::optional<int32_t> decode_time_ms,
43 absl::optional<uint8_t> qp);
ilnikd60d06a2017-04-05 03:02:20 -070044};
45
Mirko Bonadei276827c2018-10-16 14:13:50 +020046class RTC_EXPORT VideoDecoder {
ilnikd60d06a2017-04-05 03:02:20 -070047 public:
Erik Språngc12f6252021-01-13 21:49:59 +010048 struct DecoderInfo {
49 // Descriptive name of the decoder implementation.
50 std::string implementation_name;
51
52 // True if the decoder is backed by hardware acceleration.
53 bool is_hardware_accelerated = false;
54
55 std::string ToString() const;
56 bool operator==(const DecoderInfo& rhs) const;
57 bool operator!=(const DecoderInfo& rhs) const { return !(*this == rhs); }
58 };
59
Danil Chapovalovecc46ef2021-08-09 15:30:47 +020060 class Settings {
61 public:
62 Settings() = default;
63 Settings(const Settings&) = default;
64 Settings& operator=(const Settings&) = default;
65 ~Settings() = default;
ilnikd60d06a2017-04-05 03:02:20 -070066
Danil Chapovalovecc46ef2021-08-09 15:30:47 +020067 // The size of pool which is used to store video frame buffers inside
68 // decoder. If value isn't present some codec-default value will be used. If
69 // value is present and decoder doesn't have buffer pool the value will be
70 // ignored.
71 absl::optional<int> buffer_pool_size() const;
72 void set_buffer_pool_size(absl::optional<int> value);
73
74 // When valid, user of the VideoDecoder interface shouldn't `Decode`
75 // encoded images with render resolution larger than width and height
76 // specified here.
77 RenderResolution max_render_resolution() const;
78 void set_max_render_resolution(RenderResolution value);
79
80 // Maximum number of cpu cores the decoder is allowed to use in parallel.
Danil Chapovalovba0a3062021-08-13 18:15:55 +020081 // Must be positive.
Danil Chapovalovecc46ef2021-08-09 15:30:47 +020082 int number_of_cores() const { return number_of_cores_; }
Danil Chapovalovba0a3062021-08-13 18:15:55 +020083 void set_number_of_cores(int value);
Danil Chapovalovecc46ef2021-08-09 15:30:47 +020084
85 // Codec of encoded images user of the VideoDecoder interface will `Decode`.
86 VideoCodecType codec_type() const { return codec_type_; }
87 void set_codec_type(VideoCodecType value) { codec_type_ = value; }
88
89 private:
90 absl::optional<int> buffer_pool_size_;
91 RenderResolution max_resolution_;
92 int number_of_cores_ = 1;
93 VideoCodecType codec_type_ = kVideoCodecGeneric;
94 };
95
96 virtual ~VideoDecoder() = default;
97
98 // Prepares decoder to handle incoming encoded frames. Can be called multiple
99 // times, in such case only latest `settings` are in effect.
100 // TODO(bugs.webrtc.org/13045): Make pure virtual when implemented by all
101 // derived classes.
102 virtual bool Configure(const Settings& settings);
103
104 // TODO(bugs.webrtc.org/13045): Delete in favor of the Configure function
105 // above.
ilnikd60d06a2017-04-05 03:02:20 -0700106 virtual int32_t InitDecode(const VideoCodec* codec_settings,
Danil Chapovalovecc46ef2021-08-09 15:30:47 +0200107 int32_t number_of_cores);
ilnikd60d06a2017-04-05 03:02:20 -0700108
109 virtual int32_t Decode(const EncodedImage& input_image,
110 bool missing_frames,
Niels Möller5d34dcf2019-04-11 15:39:39 +0200111 int64_t render_time_ms) = 0;
ilnikd60d06a2017-04-05 03:02:20 -0700112
113 virtual int32_t RegisterDecodeCompleteCallback(
114 DecodedImageCallback* callback) = 0;
115
116 virtual int32_t Release() = 0;
117
Erik Språngc12f6252021-01-13 21:49:59 +0100118 virtual DecoderInfo GetDecoderInfo() const;
119
Erik Språngc12f6252021-01-13 21:49:59 +0100120 // Deprecated, use GetDecoderInfo().implementation_name instead.
Niels Möllerbe682d42018-03-27 08:31:45 +0200121 virtual const char* ImplementationName() const;
ilnikd60d06a2017-04-05 03:02:20 -0700122};
123
Danil Chapovalovecc46ef2021-08-09 15:30:47 +0200124inline absl::optional<int> VideoDecoder::Settings::buffer_pool_size() const {
125 return buffer_pool_size_;
126}
127
128inline void VideoDecoder::Settings::set_buffer_pool_size(
129 absl::optional<int> value) {
130 buffer_pool_size_ = value;
131}
132
133inline RenderResolution VideoDecoder::Settings::max_render_resolution() const {
134 return max_resolution_;
135}
136
137inline void VideoDecoder::Settings::set_max_render_resolution(
138 RenderResolution value) {
139 max_resolution_ = value;
140}
141
ilnikd60d06a2017-04-05 03:02:20 -0700142} // namespace webrtc
143
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200144#endif // API_VIDEO_CODECS_VIDEO_DECODER_H_