blob: fe6899858d52473f24f8c4c8265e8f0e18306d15 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/generic_decoder.h"
tommi5b7fc8c2017-07-05 16:45:57 -070012
ilnik2edc6842017-07-06 03:06:50 -070013#include <algorithm>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/video_coding/include/video_coding.h"
16#include "modules/video_coding/internal_defines.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
19#include "rtc_base/timeutils.h"
20#include "rtc_base/trace_event.h"
21#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
24
philipel9d3ab612015-12-21 04:12:39 -080025VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000026 Clock* clock)
tommid0a71ba2017-03-14 04:16:20 -070027 : _clock(clock),
Peter Boströmb7d9a972015-12-18 16:01:11 +010028 _timing(timing),
29 _timestampMap(kDecoderFrameMemoryLength),
ilnik04f4d122017-06-19 07:18:55 -070030 _lastReceivedPictureID(0) {
Tommia4e71b92018-02-21 09:37:11 +010031 decoder_thread_.DetachFromThread();
ilnik04f4d122017-06-19 07:18:55 -070032 ntp_offset_ =
33 _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds();
34}
niklase@google.com470e71d2011-07-07 08:21:25 +000035
philipel9d3ab612015-12-21 04:12:39 -080036VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {
Lu Liu54daa3a2018-02-21 19:37:53 +000037 RTC_DCHECK(construction_thread_.CalledOnValidThread());
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000040void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080041 VCMReceiveCallback* receiveCallback) {
tommid0a71ba2017-03-14 04:16:20 -070042 RTC_DCHECK(construction_thread_.CalledOnValidThread());
43 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
44 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080045 _receiveCallback = receiveCallback;
Tommia4e71b92018-02-21 09:37:11 +010046 // When the callback is cleared, it signals to us that the decoder thread
47 // is no longer running. Another decoder thread might be started, so it's
48 // important to reset the thread checker first.
49 if (!receiveCallback)
50 decoder_thread_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000051}
52
philipel9d3ab612015-12-21 04:12:39 -080053VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
Tommia4e71b92018-02-21 09:37:11 +010054 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommid0a71ba2017-03-14 04:16:20 -070055 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
56 // The callback must always have been set before this happens.
57 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080058 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000059}
60
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070061int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Per327d8ba2015-11-10 14:00:27 +010062 return Decoded(decodedImage, -1);
63}
64
65int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
66 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080067 Decoded(decodedImage,
68 decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms)
Oskar Sundbom6bd39022017-11-16 10:54:49 +010069 : rtc::nullopt,
70 rtc::nullopt);
sakalcc452e12017-02-09 04:53:45 -080071 return WEBRTC_VIDEO_CODEC_OK;
72}
73
74void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
75 rtc::Optional<int32_t> decode_time_ms,
76 rtc::Optional<uint8_t> qp) {
Tommia4e71b92018-02-21 09:37:11 +010077 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommid0a71ba2017-03-14 04:16:20 -070078 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
Tommia4e71b92018-02-21 09:37:11 +010079
philipel9d3ab612015-12-21 04:12:39 -080080 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
81 "timestamp", decodedImage.timestamp());
82 // TODO(holmer): We should improve this so that we can handle multiple
83 // callbacks from one call to Decode().
Tommia4e71b92018-02-21 09:37:11 +010084 VCMFrameInformation* frameInfo = _timestampMap.Pop(decodedImage.timestamp());
philipel9d3ab612015-12-21 04:12:39 -080085
86 if (frameInfo == NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010087 RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
88 "this one.";
sakalcc452e12017-02-09 04:53:45 -080089 return;
philipel9d3ab612015-12-21 04:12:39 -080090 }
91
92 const int64_t now_ms = _clock->TimeInMilliseconds();
sakalcc452e12017-02-09 04:53:45 -080093 if (!decode_time_ms) {
Oskar Sundbom6bd39022017-11-16 10:54:49 +010094 decode_time_ms = now_ms - frameInfo->decodeStartTimeMs;
philipel9d3ab612015-12-21 04:12:39 -080095 }
sakalcc452e12017-02-09 04:53:45 -080096 _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms,
philipel9d3ab612015-12-21 04:12:39 -080097 frameInfo->renderTimeMs);
98
ilnik04f4d122017-06-19 07:18:55 -070099 // Report timing information.
sprangba050a62017-08-18 02:51:12 -0700100 if (frameInfo->timing.flags != TimingFrameFlags::kInvalid) {
ilnik2edc6842017-07-06 03:06:50 -0700101 int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_;
ilnik04f4d122017-06-19 07:18:55 -0700102 // Convert remote timestamps to local time from ntp timestamps.
103 frameInfo->timing.encode_start_ms -= ntp_offset_;
104 frameInfo->timing.encode_finish_ms -= ntp_offset_;
105 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
106 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
107 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
108 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
ilnik2edc6842017-07-06 03:06:50 -0700109
110 int64_t sender_delta_ms = 0;
111 if (decodedImage.ntp_time_ms() < 0) {
112 // Sender clock is not estimated yet. Make sure that sender times are all
113 // negative to indicate that. Yet they still should be relatively correct.
114 sender_delta_ms =
115 std::max({capture_time_ms, frameInfo->timing.encode_start_ms,
116 frameInfo->timing.encode_finish_ms,
117 frameInfo->timing.packetization_finish_ms,
118 frameInfo->timing.pacer_exit_ms,
119 frameInfo->timing.network_timestamp_ms,
120 frameInfo->timing.network2_timestamp_ms}) +
121 1;
122 }
123
124 TimingFrameInfo timing_frame_info;
125
126 timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
127 timing_frame_info.encode_start_ms =
128 frameInfo->timing.encode_start_ms - sender_delta_ms;
129 timing_frame_info.encode_finish_ms =
130 frameInfo->timing.encode_finish_ms - sender_delta_ms;
131 timing_frame_info.packetization_finish_ms =
132 frameInfo->timing.packetization_finish_ms - sender_delta_ms;
133 timing_frame_info.pacer_exit_ms =
134 frameInfo->timing.pacer_exit_ms - sender_delta_ms;
135 timing_frame_info.network_timestamp_ms =
136 frameInfo->timing.network_timestamp_ms - sender_delta_ms;
137 timing_frame_info.network2_timestamp_ms =
138 frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
139 timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms;
140 timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms;
141 timing_frame_info.decode_start_ms = frameInfo->decodeStartTimeMs;
142 timing_frame_info.decode_finish_ms = now_ms;
143 timing_frame_info.render_time_ms = frameInfo->renderTimeMs;
144 timing_frame_info.rtp_timestamp = decodedImage.timestamp();
sprangba050a62017-08-18 02:51:12 -0700145 timing_frame_info.flags = frameInfo->timing.flags;
ilnik2edc6842017-07-06 03:06:50 -0700146
147 _timing->SetTimingFrameInfo(timing_frame_info);
ilnik04f4d122017-06-19 07:18:55 -0700148 }
149
nisse1c0dea82017-01-30 02:43:18 -0800150 decodedImage.set_timestamp_us(
151 frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec);
sakal55d932b2016-09-30 06:19:08 -0700152 decodedImage.set_rotation(frameInfo->rotation);
ilnik00d802b2017-04-11 10:34:31 -0700153 _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154}
155
philipel9d3ab612015-12-21 04:12:39 -0800156int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
157 const uint64_t pictureId) {
Tommia4e71b92018-02-21 09:37:11 +0100158 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommid0a71ba2017-03-14 04:16:20 -0700159 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000160}
161
philipel9d3ab612015-12-21 04:12:39 -0800162int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame(
163 const uint64_t pictureId) {
Tommia4e71b92018-02-21 09:37:11 +0100164 RTC_DCHECK_RUN_ON(&decoder_thread_);
philipel9d3ab612015-12-21 04:12:39 -0800165 _lastReceivedPictureID = pictureId;
166 return 0;
167}
168
169uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const {
Tommia4e71b92018-02-21 09:37:11 +0100170 RTC_DCHECK_RUN_ON(&decoder_thread_);
philipel9d3ab612015-12-21 04:12:39 -0800171 return _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172}
173
Peter Boströmb7d9a972015-12-18 16:01:11 +0100174void VCMDecodedFrameCallback::OnDecoderImplementationName(
175 const char* implementation_name) {
Tommia4e71b92018-02-21 09:37:11 +0100176 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommid0a71ba2017-03-14 04:16:20 -0700177 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100178}
179
pbos1968d3f2015-09-28 08:52:18 -0700180void VCMDecodedFrameCallback::Map(uint32_t timestamp,
181 VCMFrameInformation* frameInfo) {
Tommia4e71b92018-02-21 09:37:11 +0100182 RTC_DCHECK_RUN_ON(&decoder_thread_);
pbos1968d3f2015-09-28 08:52:18 -0700183 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
philipel9d3ab612015-12-21 04:12:39 -0800186int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
Tommia4e71b92018-02-21 09:37:11 +0100187 RTC_DCHECK_RUN_ON(&decoder_thread_);
188 return _timestampMap.Pop(timestamp) == nullptr ? VCM_GENERAL_ERROR : VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
Magnus Jedvert46a27652017-11-13 14:10:02 +0100191VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder)
192 : VCMGenericDecoder(decoder.release(), false /* isExternal */) {}
193
Peter Boström187db632015-12-01 17:20:01 +0100194VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
195 : _callback(NULL),
196 _frameInfos(),
197 _nextFrameInfoIdx(0),
tommi5b7fc8c2017-07-05 16:45:57 -0700198 decoder_(decoder),
Peter Boström187db632015-12-01 17:20:01 +0100199 _codecType(kVideoCodecUnknown),
guidouc3372582017-04-04 07:16:21 -0700200 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700201 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
202 RTC_DCHECK(decoder_);
203}
niklase@google.com470e71d2011-07-07 08:21:25 +0000204
tommi5b7fc8c2017-07-05 16:45:57 -0700205VCMGenericDecoder::~VCMGenericDecoder() {
206 decoder_->Release();
207 if (_isExternal)
208 decoder_.release();
tommi058aa712017-07-15 11:33:35 -0700209 RTC_DCHECK(_isExternal || decoder_);
tommi5b7fc8c2017-07-05 16:45:57 -0700210}
niklase@google.com470e71d2011-07-07 08:21:25 +0000211
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000212int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800213 int32_t numberOfCores) {
Tommia4e71b92018-02-21 09:37:11 +0100214 RTC_DCHECK_RUN_ON(&decoder_thread_);
philipel9d3ab612015-12-21 04:12:39 -0800215 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
216 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000217
tommi5b7fc8c2017-07-05 16:45:57 -0700218 return decoder_->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000219}
220
pbosd9eec762015-11-17 06:03:43 -0800221int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
Tommia4e71b92018-02-21 09:37:11 +0100222 RTC_DCHECK_RUN_ON(&decoder_thread_);
223 TRACE_EVENT2("webrtc", "VCMGenericDecoder::Decode", "timestamp",
224 frame.EncodedImage()._timeStamp, "decoder",
225 decoder_->ImplementationName());
226 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
227 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
228 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
229 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
230 // Set correctly only for key frames. Thus, use latest key frame
231 // content type. If the corresponding key frame was lost, decode will fail
232 // and content type will be ignored.
233 if (frame.FrameType() == kVideoFrameKey) {
234 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
235 _last_keyframe_content_type = frame.contentType();
236 } else {
237 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
238 }
239 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000240
Tommia4e71b92018-02-21 09:37:11 +0100241 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
242 const RTPFragmentationHeader dummy_header;
243 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
244 &dummy_header, frame.CodecSpecific(),
245 frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
Tommia4e71b92018-02-21 09:37:11 +0100247 // TODO(tommi): Necessary every time?
248 // Maybe this should be the first thing the function does, and only the first
249 // time around?
250 _callback->OnDecoderImplementationName(decoder_->ImplementationName());
251
252 if (ret != WEBRTC_VIDEO_CODEC_OK) {
philipel9d3ab612015-12-21 04:12:39 -0800253 if (ret < WEBRTC_VIDEO_CODEC_OK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100254 RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp "
255 << frame.TimeStamp() << ", error code: " << ret;
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000256 }
Tommia4e71b92018-02-21 09:37:11 +0100257 // We pop the frame for all non-'OK', failure or success codes such as
258 // WEBRTC_VIDEO_CODEC_NO_OUTPUT and WEBRTC_VIDEO_CODEC_REQUEST_SLI.
259 _callback->Pop(frame.TimeStamp());
260 }
261
262 return ret;
guidouc3372582017-04-04 07:16:21 -0700263}
niklase@google.com470e71d2011-07-07 08:21:25 +0000264
Peter Boström187db632015-12-01 17:20:01 +0100265int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
266 VCMDecodedFrameCallback* callback) {
Tommia4e71b92018-02-21 09:37:11 +0100267 RTC_DCHECK_RUN_ON(&decoder_thread_);
Peter Boström187db632015-12-01 17:20:01 +0100268 _callback = callback;
tommi5b7fc8c2017-07-05 16:45:57 -0700269 return decoder_->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000270}
271
perkj796cfaf2015-12-10 09:27:38 -0800272bool VCMGenericDecoder::PrefersLateDecoding() const {
Tommia4e71b92018-02-21 09:37:11 +0100273 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommi5b7fc8c2017-07-05 16:45:57 -0700274 return decoder_->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800275}
276
philipel9d3ab612015-12-21 04:12:39 -0800277} // namespace webrtc