blob: 5d22ffacc9adf4e5b34b1b258c39f90b995e800a [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() {
Tommi9f016a02018-02-21 12:20:47 +010037 // TODO(tommi): Re-enable this DCHECK once downstream issues have been fixed.
38 // RTC_DCHECK(construction_thread_.CalledOnValidThread());
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000041void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080042 VCMReceiveCallback* receiveCallback) {
tommid0a71ba2017-03-14 04:16:20 -070043 RTC_DCHECK(construction_thread_.CalledOnValidThread());
44 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
45 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080046 _receiveCallback = receiveCallback;
Tommia4e71b92018-02-21 09:37:11 +010047 // When the callback is cleared, it signals to us that the decoder thread
48 // is no longer running. Another decoder thread might be started, so it's
49 // important to reset the thread checker first.
50 if (!receiveCallback)
51 decoder_thread_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000052}
53
philipel9d3ab612015-12-21 04:12:39 -080054VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
Tommia4e71b92018-02-21 09:37:11 +010055 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommid0a71ba2017-03-14 04:16:20 -070056 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
57 // The callback must always have been set before this happens.
58 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080059 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000060}
61
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070062int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Per327d8ba2015-11-10 14:00:27 +010063 return Decoded(decodedImage, -1);
64}
65
66int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
67 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080068 Decoded(decodedImage,
69 decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms)
Oskar Sundbom6bd39022017-11-16 10:54:49 +010070 : rtc::nullopt,
71 rtc::nullopt);
sakalcc452e12017-02-09 04:53:45 -080072 return WEBRTC_VIDEO_CODEC_OK;
73}
74
75void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
76 rtc::Optional<int32_t> decode_time_ms,
77 rtc::Optional<uint8_t> qp) {
Tommia4e71b92018-02-21 09:37:11 +010078 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommid0a71ba2017-03-14 04:16:20 -070079 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
Tommia4e71b92018-02-21 09:37:11 +010080
philipel9d3ab612015-12-21 04:12:39 -080081 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
82 "timestamp", decodedImage.timestamp());
83 // TODO(holmer): We should improve this so that we can handle multiple
84 // callbacks from one call to Decode().
Tommia4e71b92018-02-21 09:37:11 +010085 VCMFrameInformation* frameInfo = _timestampMap.Pop(decodedImage.timestamp());
philipel9d3ab612015-12-21 04:12:39 -080086
87 if (frameInfo == NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010088 RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
89 "this one.";
sakalcc452e12017-02-09 04:53:45 -080090 return;
philipel9d3ab612015-12-21 04:12:39 -080091 }
92
93 const int64_t now_ms = _clock->TimeInMilliseconds();
sakalcc452e12017-02-09 04:53:45 -080094 if (!decode_time_ms) {
Oskar Sundbom6bd39022017-11-16 10:54:49 +010095 decode_time_ms = now_ms - frameInfo->decodeStartTimeMs;
philipel9d3ab612015-12-21 04:12:39 -080096 }
sakalcc452e12017-02-09 04:53:45 -080097 _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms,
philipel9d3ab612015-12-21 04:12:39 -080098 frameInfo->renderTimeMs);
99
ilnik04f4d122017-06-19 07:18:55 -0700100 // Report timing information.
sprangba050a62017-08-18 02:51:12 -0700101 if (frameInfo->timing.flags != TimingFrameFlags::kInvalid) {
ilnik2edc6842017-07-06 03:06:50 -0700102 int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_;
ilnik04f4d122017-06-19 07:18:55 -0700103 // Convert remote timestamps to local time from ntp timestamps.
104 frameInfo->timing.encode_start_ms -= ntp_offset_;
105 frameInfo->timing.encode_finish_ms -= ntp_offset_;
106 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
107 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
108 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
109 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
ilnik2edc6842017-07-06 03:06:50 -0700110
111 int64_t sender_delta_ms = 0;
112 if (decodedImage.ntp_time_ms() < 0) {
113 // Sender clock is not estimated yet. Make sure that sender times are all
114 // negative to indicate that. Yet they still should be relatively correct.
115 sender_delta_ms =
116 std::max({capture_time_ms, frameInfo->timing.encode_start_ms,
117 frameInfo->timing.encode_finish_ms,
118 frameInfo->timing.packetization_finish_ms,
119 frameInfo->timing.pacer_exit_ms,
120 frameInfo->timing.network_timestamp_ms,
121 frameInfo->timing.network2_timestamp_ms}) +
122 1;
123 }
124
125 TimingFrameInfo timing_frame_info;
126
127 timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
128 timing_frame_info.encode_start_ms =
129 frameInfo->timing.encode_start_ms - sender_delta_ms;
130 timing_frame_info.encode_finish_ms =
131 frameInfo->timing.encode_finish_ms - sender_delta_ms;
132 timing_frame_info.packetization_finish_ms =
133 frameInfo->timing.packetization_finish_ms - sender_delta_ms;
134 timing_frame_info.pacer_exit_ms =
135 frameInfo->timing.pacer_exit_ms - sender_delta_ms;
136 timing_frame_info.network_timestamp_ms =
137 frameInfo->timing.network_timestamp_ms - sender_delta_ms;
138 timing_frame_info.network2_timestamp_ms =
139 frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
140 timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms;
141 timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms;
142 timing_frame_info.decode_start_ms = frameInfo->decodeStartTimeMs;
143 timing_frame_info.decode_finish_ms = now_ms;
144 timing_frame_info.render_time_ms = frameInfo->renderTimeMs;
145 timing_frame_info.rtp_timestamp = decodedImage.timestamp();
sprangba050a62017-08-18 02:51:12 -0700146 timing_frame_info.flags = frameInfo->timing.flags;
ilnik2edc6842017-07-06 03:06:50 -0700147
148 _timing->SetTimingFrameInfo(timing_frame_info);
ilnik04f4d122017-06-19 07:18:55 -0700149 }
150
nisse1c0dea82017-01-30 02:43:18 -0800151 decodedImage.set_timestamp_us(
152 frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec);
sakal55d932b2016-09-30 06:19:08 -0700153 decodedImage.set_rotation(frameInfo->rotation);
ilnik00d802b2017-04-11 10:34:31 -0700154 _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155}
156
philipel9d3ab612015-12-21 04:12:39 -0800157int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
158 const uint64_t pictureId) {
Tommia4e71b92018-02-21 09:37:11 +0100159 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommid0a71ba2017-03-14 04:16:20 -0700160 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
philipel9d3ab612015-12-21 04:12:39 -0800163int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame(
164 const uint64_t pictureId) {
Tommia4e71b92018-02-21 09:37:11 +0100165 RTC_DCHECK_RUN_ON(&decoder_thread_);
philipel9d3ab612015-12-21 04:12:39 -0800166 _lastReceivedPictureID = pictureId;
167 return 0;
168}
169
170uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const {
Tommia4e71b92018-02-21 09:37:11 +0100171 RTC_DCHECK_RUN_ON(&decoder_thread_);
philipel9d3ab612015-12-21 04:12:39 -0800172 return _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173}
174
Peter Boströmb7d9a972015-12-18 16:01:11 +0100175void VCMDecodedFrameCallback::OnDecoderImplementationName(
176 const char* implementation_name) {
Tommia4e71b92018-02-21 09:37:11 +0100177 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommid0a71ba2017-03-14 04:16:20 -0700178 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100179}
180
pbos1968d3f2015-09-28 08:52:18 -0700181void VCMDecodedFrameCallback::Map(uint32_t timestamp,
182 VCMFrameInformation* frameInfo) {
Tommia4e71b92018-02-21 09:37:11 +0100183 RTC_DCHECK_RUN_ON(&decoder_thread_);
pbos1968d3f2015-09-28 08:52:18 -0700184 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
philipel9d3ab612015-12-21 04:12:39 -0800187int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
Tommia4e71b92018-02-21 09:37:11 +0100188 RTC_DCHECK_RUN_ON(&decoder_thread_);
189 return _timestampMap.Pop(timestamp) == nullptr ? VCM_GENERAL_ERROR : VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
Magnus Jedvert46a27652017-11-13 14:10:02 +0100192VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder)
193 : VCMGenericDecoder(decoder.release(), false /* isExternal */) {}
194
Peter Boström187db632015-12-01 17:20:01 +0100195VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
196 : _callback(NULL),
197 _frameInfos(),
198 _nextFrameInfoIdx(0),
tommi5b7fc8c2017-07-05 16:45:57 -0700199 decoder_(decoder),
Peter Boström187db632015-12-01 17:20:01 +0100200 _codecType(kVideoCodecUnknown),
guidouc3372582017-04-04 07:16:21 -0700201 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700202 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
203 RTC_DCHECK(decoder_);
204}
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
tommi5b7fc8c2017-07-05 16:45:57 -0700206VCMGenericDecoder::~VCMGenericDecoder() {
207 decoder_->Release();
208 if (_isExternal)
209 decoder_.release();
tommi058aa712017-07-15 11:33:35 -0700210 RTC_DCHECK(_isExternal || decoder_);
tommi5b7fc8c2017-07-05 16:45:57 -0700211}
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000213int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800214 int32_t numberOfCores) {
Tommia4e71b92018-02-21 09:37:11 +0100215 RTC_DCHECK_RUN_ON(&decoder_thread_);
philipel9d3ab612015-12-21 04:12:39 -0800216 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
217 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218
tommi5b7fc8c2017-07-05 16:45:57 -0700219 return decoder_->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
221
pbosd9eec762015-11-17 06:03:43 -0800222int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
Tommia4e71b92018-02-21 09:37:11 +0100223 RTC_DCHECK_RUN_ON(&decoder_thread_);
224 TRACE_EVENT2("webrtc", "VCMGenericDecoder::Decode", "timestamp",
225 frame.EncodedImage()._timeStamp, "decoder",
226 decoder_->ImplementationName());
227 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
228 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
229 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
230 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
231 // Set correctly only for key frames. Thus, use latest key frame
232 // content type. If the corresponding key frame was lost, decode will fail
233 // and content type will be ignored.
234 if (frame.FrameType() == kVideoFrameKey) {
235 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
236 _last_keyframe_content_type = frame.contentType();
237 } else {
238 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
239 }
240 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
Tommia4e71b92018-02-21 09:37:11 +0100242 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
243 const RTPFragmentationHeader dummy_header;
244 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
245 &dummy_header, frame.CodecSpecific(),
246 frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
Tommia4e71b92018-02-21 09:37:11 +0100248 // TODO(tommi): Necessary every time?
249 // Maybe this should be the first thing the function does, and only the first
250 // time around?
251 _callback->OnDecoderImplementationName(decoder_->ImplementationName());
252
253 if (ret != WEBRTC_VIDEO_CODEC_OK) {
philipel9d3ab612015-12-21 04:12:39 -0800254 if (ret < WEBRTC_VIDEO_CODEC_OK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100255 RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp "
256 << frame.TimeStamp() << ", error code: " << ret;
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000257 }
Tommia4e71b92018-02-21 09:37:11 +0100258 // We pop the frame for all non-'OK', failure or success codes such as
259 // WEBRTC_VIDEO_CODEC_NO_OUTPUT and WEBRTC_VIDEO_CODEC_REQUEST_SLI.
260 _callback->Pop(frame.TimeStamp());
261 }
262
263 return ret;
guidouc3372582017-04-04 07:16:21 -0700264}
niklase@google.com470e71d2011-07-07 08:21:25 +0000265
Peter Boström187db632015-12-01 17:20:01 +0100266int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
267 VCMDecodedFrameCallback* callback) {
Tommia4e71b92018-02-21 09:37:11 +0100268 RTC_DCHECK_RUN_ON(&decoder_thread_);
Peter Boström187db632015-12-01 17:20:01 +0100269 _callback = callback;
tommi5b7fc8c2017-07-05 16:45:57 -0700270 return decoder_->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000271}
272
perkj796cfaf2015-12-10 09:27:38 -0800273bool VCMGenericDecoder::PrefersLateDecoding() const {
Tommia4e71b92018-02-21 09:37:11 +0100274 RTC_DCHECK_RUN_ON(&decoder_thread_);
tommi5b7fc8c2017-07-05 16:45:57 -0700275 return decoder_->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800276}
277
philipel9d3ab612015-12-21 04:12:39 -0800278} // namespace webrtc