blob: 891ec893ff2cd98e8337152ecbc64d355a04c2ff [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
tommid0a71ba2017-03-14 04:16:20 -070014#include "webrtc/base/criticalsection.h"
15#include "webrtc/base/thread_checker.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010016#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010017#include "webrtc/modules/video_coding/encoded_frame.h"
tommid0a71ba2017-03-14 04:16:20 -070018#include "webrtc/modules/video_coding/include/video_codec_interface.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010019#include "webrtc/modules/video_coding/timestamp_map.h"
20#include "webrtc/modules/video_coding/timing.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;
niklase@google.com470e71d2011-07-07 08:21:25 +000033};
34
philipel9d3ab612015-12-21 04:12:39 -080035class VCMDecodedFrameCallback : public DecodedImageCallback {
36 public:
37 VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
tommid0a71ba2017-03-14 04:16:20 -070038 ~VCMDecodedFrameCallback() override;
39 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
40 VCMReceiveCallback* UserReceiveCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000041
tommid0a71ba2017-03-14 04:16:20 -070042 int32_t Decoded(VideoFrame& decodedImage) override;
43 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
44 void Decoded(VideoFrame& decodedImage,
45 rtc::Optional<int32_t> decode_time_ms,
46 rtc::Optional<uint8_t> qp) override;
47 int32_t ReceivedDecodedReferenceFrame(const uint64_t pictureId) override;
48 int32_t ReceivedDecodedFrame(const uint64_t pictureId) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000049
tommid0a71ba2017-03-14 04:16:20 -070050 uint64_t LastReceivedPictureID() const;
51 void OnDecoderImplementationName(const char* implementation_name);
niklase@google.com470e71d2011-07-07 08:21:25 +000052
tommid0a71ba2017-03-14 04:16:20 -070053 void Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
54 int32_t Pop(uint32_t timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000055
philipel9d3ab612015-12-21 04:12:39 -080056 private:
tommid0a71ba2017-03-14 04:16:20 -070057 rtc::ThreadChecker construction_thread_;
58 // Protect |_timestampMap|.
59 Clock* const _clock;
60 // This callback must be set before the decoder thread starts running
61 // and must only be unset when external threads (e.g decoder thread)
62 // have been stopped. Due to that, the variable should regarded as const
63 // while there are more than one threads involved, it must be set
64 // from the same thread, and therfore a lock is not required to access it.
65 VCMReceiveCallback* _receiveCallback = nullptr;
66 VCMTiming* _timing;
67 rtc::CriticalSection lock_;
68 VCMTimestampMap _timestampMap GUARDED_BY(lock_);
69 uint64_t _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +000070};
71
philipel9d3ab612015-12-21 04:12:39 -080072class VCMGenericDecoder {
73 friend class VCMCodecDataBase;
niklase@google.com470e71d2011-07-07 08:21:25 +000074
philipel9d3ab612015-12-21 04:12:39 -080075 public:
76 explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
77 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000078
philipel9d3ab612015-12-21 04:12:39 -080079 /**
80 * Initialize the decoder with the information from the VideoCodec
81 */
82 int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
philipel9d3ab612015-12-21 04:12:39 -080084 /**
85 * Decode to a raw I420 frame,
86 *
87 * inputVideoBuffer reference to encoded video frame
88 */
89 int32_t Decode(const VCMEncodedFrame& inputFrame, int64_t nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
philipel9d3ab612015-12-21 04:12:39 -080091 /**
92 * Free the decoder memory
93 */
94 int32_t Release();
niklase@google.com470e71d2011-07-07 08:21:25 +000095
philipel9d3ab612015-12-21 04:12:39 -080096 /**
philipel9d3ab612015-12-21 04:12:39 -080097 * Set decode callback. Deregistering while decoding is illegal.
98 */
99 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
philipel9d3ab612015-12-21 04:12:39 -0800101 bool External() const;
102 bool PrefersLateDecoding() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
philipel9d3ab612015-12-21 04:12:39 -0800104 private:
105 VCMDecodedFrameCallback* _callback;
106 VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength];
107 uint32_t _nextFrameInfoIdx;
108 VideoDecoder* const _decoder;
109 VideoCodecType _codecType;
110 bool _isExternal;
111 bool _keyFrameDecoded;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112};
113
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000114} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
philipel9d3ab612015-12-21 04:12:39 -0800116#endif // WEBRTC_MODULES_VIDEO_CODING_GENERIC_DECODER_H_