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 WEBRTC_VIDEO_DECODER_H_
12#define WEBRTC_VIDEO_DECODER_H_
13
14#include <string>
15#include <vector>
16
17#include "webrtc/common_types.h"
18#include "webrtc/typedefs.h"
19#include "webrtc/video_frame.h"
20
21namespace webrtc {
22
23class RTPFragmentationHeader;
24// TODO(pbos): Expose these through a public (root) header or change these APIs.
25struct CodecSpecificInfo;
26struct VideoCodec;
27
28class DecodedImageCallback {
29 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.
37  // TODO(perkj): Remove default implementation when chromium has been updated.
38  virtual int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) {
39    // The default implementation ignores custom decode time value.
40    return Decoded(decodedImage);
41  }
42
43  virtual int32_t ReceivedDecodedReferenceFrame(const uint64_t pictureId) {
44    return -1;
45  }
46
47  virtual int32_t ReceivedDecodedFrame(const uint64_t pictureId) { return -1; }
48};
49
50class VideoDecoder {
51 public:
52  enum DecoderType {
53    kH264,
54    kVp8,
55    kVp9,
56    kUnsupportedCodec,
57  };
58
59  static VideoDecoder* Create(DecoderType codec_type);
60
61  virtual ~VideoDecoder() {}
62
63  virtual int32_t InitDecode(const VideoCodec* codec_settings,
64                             int32_t number_of_cores) = 0;
65
66  virtual int32_t Decode(const EncodedImage& input_image,
67                         bool missing_frames,
68                         const RTPFragmentationHeader* fragmentation,
69                         const CodecSpecificInfo* codec_specific_info = NULL,
70                         int64_t render_time_ms = -1) = 0;
71
72  virtual int32_t RegisterDecodeCompleteCallback(
73      DecodedImageCallback* callback) = 0;
74
75  virtual int32_t Release() = 0;
76  virtual int32_t Reset() = 0;
77
78  // Returns true if the decoder prefer to decode frames late.
79  // That is, it can not decode infinite number of frames before the decoded
80  // frame is consumed.
81  virtual bool PrefersLateDecoding() const { return true; }
82
83  virtual const char* ImplementationName() const { return "unknown"; }
84};
85
86// Class used to wrap external VideoDecoders to provide a fallback option on
87// software decoding when a hardware decoder fails to decode a stream due to
88// hardware restrictions, such as max resolution.
89class VideoDecoderSoftwareFallbackWrapper : public webrtc::VideoDecoder {
90 public:
91  VideoDecoderSoftwareFallbackWrapper(VideoCodecType codec_type,
92                                      VideoDecoder* decoder);
93
94  int32_t InitDecode(const VideoCodec* codec_settings,
95                     int32_t number_of_cores) override;
96
97  int32_t Decode(const EncodedImage& input_image,
98                 bool missing_frames,
99                 const RTPFragmentationHeader* fragmentation,
100                 const CodecSpecificInfo* codec_specific_info,
101                 int64_t render_time_ms) override;
102
103  int32_t RegisterDecodeCompleteCallback(
104      DecodedImageCallback* callback) override;
105
106  int32_t Release() override;
107  int32_t Reset() override;
108  bool PrefersLateDecoding() const override;
109
110  const char* ImplementationName() const override;
111
112 private:
113  bool InitFallbackDecoder();
114
115  const DecoderType decoder_type_;
116  VideoDecoder* const decoder_;
117
118  VideoCodec codec_settings_;
119  int32_t number_of_cores_;
120  std::string fallback_implementation_name_;
121  rtc::scoped_ptr<VideoDecoder> fallback_decoder_;
122  DecodedImageCallback* callback_;
123};
124
125}  // namespace webrtc
126
127#endif  // WEBRTC_VIDEO_DECODER_H_
128