blob: 1df2060247a91f4ade18c1329479493997dbd20a [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
tommie3aa88b2017-04-04 03:53:02 -070014#include <memory>
15
tommid0a71ba2017-03-14 04:16:20 -070016#include "webrtc/base/thread_checker.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010017#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010018#include "webrtc/modules/video_coding/encoded_frame.h"
tommid0a71ba2017-03-14 04:16:20 -070019#include "webrtc/modules/video_coding/include/video_codec_interface.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010020#include "webrtc/modules/video_coding/timestamp_map.h"
21#include "webrtc/modules/video_coding/timing.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
philipel9d3ab612015-12-21 04:12:39 -080023namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
25class VCMReceiveCallback;
26
27enum { kDecoderFrameMemoryLength = 10 };
28
philipel9d3ab612015-12-21 04:12:39 -080029struct VCMFrameInformation {
30 int64_t renderTimeMs;
31 int64_t decodeStartTimeMs;
32 void* userData;
33 VideoRotation rotation;
niklase@google.com470e71d2011-07-07 08:21:25 +000034};
35
philipel9d3ab612015-12-21 04:12:39 -080036class VCMDecodedFrameCallback : public DecodedImageCallback {
37 public:
38 VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
tommid0a71ba2017-03-14 04:16:20 -070039 ~VCMDecodedFrameCallback() override;
tommie3aa88b2017-04-04 03:53:02 -070040
tommid0a71ba2017-03-14 04:16:20 -070041 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_;
tommie3aa88b2017-04-04 03:53:02 -070060 rtc::ThreadChecker decoder_thread_;
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;
tommie3aa88b2017-04-04 03:53:02 -070068 VCMTiming* _timing ACCESS_ON(decoder_thread_);
69 VCMTimestampMap _timestampMap ACCESS_ON(decoder_thread_);
70 uint64_t _lastReceivedPictureID ACCESS_ON(decoder_thread_);
niklase@google.com470e71d2011-07-07 08:21:25 +000071};
72
philipel9d3ab612015-12-21 04:12:39 -080073class VCMGenericDecoder {
philipel9d3ab612015-12-21 04:12:39 -080074 public:
75 explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
76 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000077
philipel9d3ab612015-12-21 04:12:39 -080078 /**
79 * Initialize the decoder with the information from the VideoCodec
80 */
81 int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +000082
philipel9d3ab612015-12-21 04:12:39 -080083 /**
84 * Decode to a raw I420 frame,
85 *
86 * inputVideoBuffer reference to encoded video frame
87 */
88 int32_t Decode(const VCMEncodedFrame& inputFrame, int64_t nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000089
philipel9d3ab612015-12-21 04:12:39 -080090 /**
philipel9d3ab612015-12-21 04:12:39 -080091 * Set decode callback. Deregistering while decoding is illegal.
92 */
93 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000094
philipel9d3ab612015-12-21 04:12:39 -080095 bool PrefersLateDecoding() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000096
tommie3aa88b2017-04-04 03:53:02 -070097#if defined(WEBRTC_ANDROID)
98 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7361
99 void PollDecodedFrames();
100#endif
101
102 bool IsSameDecoder(VideoDecoder* decoder) const {
103 return decoder_.get() == decoder;
104 }
105
philipel9d3ab612015-12-21 04:12:39 -0800106 private:
tommie3aa88b2017-04-04 03:53:02 -0700107 rtc::ThreadChecker decoder_thread_;
108 VCMDecodedFrameCallback* _callback ACCESS_ON(decoder_thread_);
109 VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength] ACCESS_ON(
110 decoder_thread_);
111 uint32_t _nextFrameInfoIdx ACCESS_ON(decoder_thread_);
112 std::unique_ptr<VideoDecoder> decoder_;
113 VideoCodecType _codecType ACCESS_ON(decoder_thread_);
114 const bool _isExternal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115};
116
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000117} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
philipel9d3ab612015-12-21 04:12:39 -0800119#endif // WEBRTC_MODULES_VIDEO_CODING_GENERIC_DECODER_H_