blob: 033ef2d590fdae829e434d0602b8af840f8cdd84 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mikhal@webrtc.orga2031d52012-07-31 15:53:44 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010014#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010015#include "webrtc/modules/video_coding/encoded_frame.h"
tommid0a71ba2017-03-14 04:16:20 -070016#include "webrtc/modules/video_coding/include/video_codec_interface.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010017#include "webrtc/modules/video_coding/timestamp_map.h"
18#include "webrtc/modules/video_coding/timing.h"
kjellanderc8fa6922017-06-30 14:02:00 -070019#include "webrtc/rtc_base/criticalsection.h"
20#include "webrtc/rtc_base/thread_checker.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
philipel9d3ab612015-12-21 04:12:39 -080022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24class VCMReceiveCallback;
25
26enum { kDecoderFrameMemoryLength = 10 };
27
philipel9d3ab612015-12-21 04:12:39 -080028struct VCMFrameInformation {
29 int64_t renderTimeMs;
30 int64_t decodeStartTimeMs;
31 void* userData;
32 VideoRotation rotation;
ilnik00d802b2017-04-11 10:34:31 -070033 VideoContentType content_type;
ilnik04f4d122017-06-19 07:18:55 -070034 EncodedImage::Timing timing;
niklase@google.com470e71d2011-07-07 08:21:25 +000035};
36
philipel9d3ab612015-12-21 04:12:39 -080037class VCMDecodedFrameCallback : public DecodedImageCallback {
38 public:
39 VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
tommid0a71ba2017-03-14 04:16:20 -070040 ~VCMDecodedFrameCallback() override;
41 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
42 VCMReceiveCallback* UserReceiveCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000043
tommid0a71ba2017-03-14 04:16:20 -070044 int32_t Decoded(VideoFrame& decodedImage) override;
45 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
46 void Decoded(VideoFrame& decodedImage,
47 rtc::Optional<int32_t> decode_time_ms,
48 rtc::Optional<uint8_t> qp) override;
49 int32_t ReceivedDecodedReferenceFrame(const uint64_t pictureId) override;
50 int32_t ReceivedDecodedFrame(const uint64_t pictureId) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
tommid0a71ba2017-03-14 04:16:20 -070052 uint64_t LastReceivedPictureID() const;
53 void OnDecoderImplementationName(const char* implementation_name);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
tommid0a71ba2017-03-14 04:16:20 -070055 void Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
56 int32_t Pop(uint32_t timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
philipel9d3ab612015-12-21 04:12:39 -080058 private:
tommid0a71ba2017-03-14 04:16:20 -070059 rtc::ThreadChecker construction_thread_;
guidouc3372582017-04-04 07:16:21 -070060 // Protect |_timestampMap|.
tommid0a71ba2017-03-14 04:16:20 -070061 Clock* const _clock;
62 // This callback must be set before the decoder thread starts running
63 // and must only be unset when external threads (e.g decoder thread)
64 // have been stopped. Due to that, the variable should regarded as const
65 // while there are more than one threads involved, it must be set
66 // from the same thread, and therfore a lock is not required to access it.
67 VCMReceiveCallback* _receiveCallback = nullptr;
guidouc3372582017-04-04 07:16:21 -070068 VCMTiming* _timing;
69 rtc::CriticalSection lock_;
70 VCMTimestampMap _timestampMap GUARDED_BY(lock_);
71 uint64_t _lastReceivedPictureID;
ilnik04f4d122017-06-19 07:18:55 -070072 int64_t ntp_offset_;
niklase@google.com470e71d2011-07-07 08:21:25 +000073};
74
philipel9d3ab612015-12-21 04:12:39 -080075class VCMGenericDecoder {
guidouc3372582017-04-04 07:16:21 -070076 friend class VCMCodecDataBase;
77
philipel9d3ab612015-12-21 04:12:39 -080078 public:
79 explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
80 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000081
philipel9d3ab612015-12-21 04:12:39 -080082 /**
83 * Initialize the decoder with the information from the VideoCodec
84 */
85 int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
philipel9d3ab612015-12-21 04:12:39 -080087 /**
88 * Decode to a raw I420 frame,
89 *
90 * inputVideoBuffer reference to encoded video frame
91 */
92 int32_t Decode(const VCMEncodedFrame& inputFrame, int64_t nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000093
philipel9d3ab612015-12-21 04:12:39 -080094 /**
guidouc3372582017-04-04 07:16:21 -070095 * Free the decoder memory
96 */
97 int32_t Release();
98
99 /**
philipel9d3ab612015-12-21 04:12:39 -0800100 * Set decode callback. Deregistering while decoding is illegal.
101 */
102 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
guidouc3372582017-04-04 07:16:21 -0700104 bool External() const;
philipel9d3ab612015-12-21 04:12:39 -0800105 bool PrefersLateDecoding() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
philipel9d3ab612015-12-21 04:12:39 -0800107 private:
guidouc3372582017-04-04 07:16:21 -0700108 VCMDecodedFrameCallback* _callback;
109 VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength];
110 uint32_t _nextFrameInfoIdx;
111 VideoDecoder* const _decoder;
112 VideoCodecType _codecType;
113 bool _isExternal;
114 bool _keyFrameDecoded;
ilnik00d802b2017-04-11 10:34:31 -0700115 VideoContentType _last_keyframe_content_type;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116};
117
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000118} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
philipel9d3ab612015-12-21 04:12:39 -0800120#endif // WEBRTC_MODULES_VIDEO_CODING_GENERIC_DECODER_H_