1 /*
2  *  Copyright (c) 2012 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_MODULES_VIDEO_CODING_GENERIC_DECODER_H_
12 #define WEBRTC_MODULES_VIDEO_CODING_GENERIC_DECODER_H_
13 
14 #include "webrtc/modules/include/module_common_types.h"
15 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
16 #include "webrtc/modules/video_coding/encoded_frame.h"
17 #include "webrtc/modules/video_coding/timestamp_map.h"
18 #include "webrtc/modules/video_coding/timing.h"
19 
20 namespace webrtc {
21 
22 class VCMReceiveCallback;
23 
24 enum { kDecoderFrameMemoryLength = 10 };
25 
26 struct VCMFrameInformation {
27   int64_t renderTimeMs;
28   int64_t decodeStartTimeMs;
29   void* userData;
30   VideoRotation rotation;
31 };
32 
33 class VCMDecodedFrameCallback : public DecodedImageCallback {
34  public:
35   VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
36     virtual ~VCMDecodedFrameCallback();
37     void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
38     VCMReceiveCallback* UserReceiveCallback();
39 
40     virtual int32_t Decoded(VideoFrame& decodedImage);  // NOLINT
41     virtual int32_t Decoded(VideoFrame& decodedImage,   // NOLINT
42                             int64_t decode_time_ms);
43     virtual int32_t ReceivedDecodedReferenceFrame(const uint64_t pictureId);
44     virtual int32_t ReceivedDecodedFrame(const uint64_t pictureId);
45 
46     uint64_t LastReceivedPictureID() const;
47     void OnDecoderImplementationName(const char* implementation_name);
48 
49     void Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
50     int32_t Pop(uint32_t timestamp);
51 
52  private:
53     // Protect |_receiveCallback| and |_timestampMap|.
54     CriticalSectionWrapper* _critSect;
55     Clock* _clock;
56     VCMReceiveCallback* _receiveCallback GUARDED_BY(_critSect);
57     VCMTiming* _timing;
58     VCMTimestampMap _timestampMap GUARDED_BY(_critSect);
59     uint64_t _lastReceivedPictureID;
60 };
61 
62 class VCMGenericDecoder {
63   friend class VCMCodecDataBase;
64 
65  public:
66   explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
67   ~VCMGenericDecoder();
68 
69   /**
70   * Initialize the decoder with the information from the VideoCodec
71   */
72   int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
73 
74   /**
75   * Decode to a raw I420 frame,
76   *
77   * inputVideoBuffer reference to encoded video frame
78   */
79   int32_t Decode(const VCMEncodedFrame& inputFrame, int64_t nowMs);
80 
81   /**
82   * Free the decoder memory
83   */
84   int32_t Release();
85 
86   /**
87   * Reset the decoder state, prepare for a new call
88   */
89   int32_t Reset();
90 
91   /**
92   * Set decode callback. Deregistering while decoding is illegal.
93   */
94   int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
95 
96   bool External() const;
97   bool PrefersLateDecoding() const;
98 
99  private:
100   VCMDecodedFrameCallback* _callback;
101   VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength];
102   uint32_t _nextFrameInfoIdx;
103   VideoDecoder* const _decoder;
104   VideoCodecType _codecType;
105   bool _isExternal;
106   bool _keyFrameDecoded;
107 };
108 
109 }  // namespace webrtc
110 
111 #endif  // WEBRTC_MODULES_VIDEO_CODING_GENERIC_DECODER_H_
112