blob: 6ef4c361befc181e332463698e4d11a752b1b860 [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
ilnik2edc6842017-07-06 03:06:50 -070014#include <algorithm>
15
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "api/video/video_timing.h"
17#include "modules/video_coding/include/video_error_codes.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/trace_event.h"
22#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24namespace webrtc {
25
philipel9d3ab612015-12-21 04:12:39 -080026VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000027 Clock* clock)
tommid0a71ba2017-03-14 04:16:20 -070028 : _clock(clock),
Peter Boströmb7d9a972015-12-18 16:01:11 +010029 _timing(timing),
30 _timestampMap(kDecoderFrameMemoryLength),
ilnik04f4d122017-06-19 07:18:55 -070031 _lastReceivedPictureID(0) {
32 ntp_offset_ =
33 _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds();
34}
niklase@google.com470e71d2011-07-07 08:21:25 +000035
Yves Gerey665174f2018-06-19 15:03:05 +020036VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000037
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000038void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080039 VCMReceiveCallback* receiveCallback) {
tommid0a71ba2017-03-14 04:16:20 -070040 RTC_DCHECK(construction_thread_.CalledOnValidThread());
41 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
42 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080043 _receiveCallback = receiveCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
philipel9d3ab612015-12-21 04:12:39 -080046VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
tommid0a71ba2017-03-14 04:16:20 -070047 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
48 // The callback must always have been set before this happens.
49 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080050 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000051}
52
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070053int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Per327d8ba2015-11-10 14:00:27 +010054 return Decoded(decodedImage, -1);
55}
56
57int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
58 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080059 Decoded(decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020060 decode_time_ms >= 0 ? absl::optional<int32_t>(decode_time_ms)
61 : absl::nullopt,
62 absl::nullopt);
sakalcc452e12017-02-09 04:53:45 -080063 return WEBRTC_VIDEO_CODEC_OK;
64}
65
66void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020067 absl::optional<int32_t> decode_time_ms,
68 absl::optional<uint8_t> qp) {
tommid0a71ba2017-03-14 04:16:20 -070069 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
philipel9d3ab612015-12-21 04:12:39 -080070 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
71 "timestamp", decodedImage.timestamp());
72 // TODO(holmer): We should improve this so that we can handle multiple
73 // callbacks from one call to Decode().
Lu Liu352314a2018-02-21 19:38:59 +000074 VCMFrameInformation* frameInfo;
75 {
76 rtc::CritScope cs(&lock_);
77 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
78 }
philipel9d3ab612015-12-21 04:12:39 -080079
80 if (frameInfo == NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010081 RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
82 "this one.";
sakalcc452e12017-02-09 04:53:45 -080083 return;
philipel9d3ab612015-12-21 04:12:39 -080084 }
85
86 const int64_t now_ms = _clock->TimeInMilliseconds();
sakalcc452e12017-02-09 04:53:45 -080087 if (!decode_time_ms) {
Oskar Sundbom6bd39022017-11-16 10:54:49 +010088 decode_time_ms = now_ms - frameInfo->decodeStartTimeMs;
philipel9d3ab612015-12-21 04:12:39 -080089 }
sakalcc452e12017-02-09 04:53:45 -080090 _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms,
philipel9d3ab612015-12-21 04:12:39 -080091 frameInfo->renderTimeMs);
92
ilnik04f4d122017-06-19 07:18:55 -070093 // Report timing information.
Benjamin Wright3f10ca82018-12-07 03:45:19 -080094 TimingFrameInfo timing_frame_info;
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +020095 if (frameInfo->timing.flags != VideoSendTiming::kInvalid) {
ilnik2edc6842017-07-06 03:06:50 -070096 int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_;
ilnik04f4d122017-06-19 07:18:55 -070097 // Convert remote timestamps to local time from ntp timestamps.
98 frameInfo->timing.encode_start_ms -= ntp_offset_;
99 frameInfo->timing.encode_finish_ms -= ntp_offset_;
100 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
101 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
102 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
103 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
ilnik2edc6842017-07-06 03:06:50 -0700104
105 int64_t sender_delta_ms = 0;
106 if (decodedImage.ntp_time_ms() < 0) {
107 // Sender clock is not estimated yet. Make sure that sender times are all
108 // negative to indicate that. Yet they still should be relatively correct.
109 sender_delta_ms =
110 std::max({capture_time_ms, frameInfo->timing.encode_start_ms,
111 frameInfo->timing.encode_finish_ms,
112 frameInfo->timing.packetization_finish_ms,
113 frameInfo->timing.pacer_exit_ms,
114 frameInfo->timing.network_timestamp_ms,
115 frameInfo->timing.network2_timestamp_ms}) +
116 1;
117 }
118
ilnik2edc6842017-07-06 03:06:50 -0700119
120 timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
121 timing_frame_info.encode_start_ms =
122 frameInfo->timing.encode_start_ms - sender_delta_ms;
123 timing_frame_info.encode_finish_ms =
124 frameInfo->timing.encode_finish_ms - sender_delta_ms;
125 timing_frame_info.packetization_finish_ms =
126 frameInfo->timing.packetization_finish_ms - sender_delta_ms;
127 timing_frame_info.pacer_exit_ms =
128 frameInfo->timing.pacer_exit_ms - sender_delta_ms;
129 timing_frame_info.network_timestamp_ms =
130 frameInfo->timing.network_timestamp_ms - sender_delta_ms;
131 timing_frame_info.network2_timestamp_ms =
132 frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700133 }
134
Benjamin Wright3f10ca82018-12-07 03:45:19 -0800135 timing_frame_info.flags = frameInfo->timing.flags;
136 timing_frame_info.decode_start_ms = frameInfo->decodeStartTimeMs;
137 timing_frame_info.decode_finish_ms = now_ms;
138 timing_frame_info.render_time_ms = frameInfo->renderTimeMs;
139 timing_frame_info.rtp_timestamp = decodedImage.timestamp();
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->SetTimingFrameInfo(timing_frame_info);
143
Yves Gerey665174f2018-06-19 15:03:05 +0200144 decodedImage.set_timestamp_us(frameInfo->renderTimeMs *
145 rtc::kNumMicrosecsPerMillisec);
sakal55d932b2016-09-30 06:19:08 -0700146 decodedImage.set_rotation(frameInfo->rotation);
ilnik00d802b2017-04-11 10:34:31 -0700147 _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148}
149
philipel9d3ab612015-12-21 04:12:39 -0800150int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
151 const uint64_t pictureId) {
tommid0a71ba2017-03-14 04:16:20 -0700152 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153}
154
philipel9d3ab612015-12-21 04:12:39 -0800155int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame(
156 const uint64_t pictureId) {
157 _lastReceivedPictureID = pictureId;
158 return 0;
159}
160
161uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const {
162 return _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163}
164
Peter Boströmb7d9a972015-12-18 16:01:11 +0100165void VCMDecodedFrameCallback::OnDecoderImplementationName(
166 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700167 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100168}
169
pbos1968d3f2015-09-28 08:52:18 -0700170void VCMDecodedFrameCallback::Map(uint32_t timestamp,
171 VCMFrameInformation* frameInfo) {
Lu Liu352314a2018-02-21 19:38:59 +0000172 rtc::CritScope cs(&lock_);
pbos1968d3f2015-09-28 08:52:18 -0700173 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000174}
175
philipel9d3ab612015-12-21 04:12:39 -0800176int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
Lu Liu352314a2018-02-21 19:38:59 +0000177 rtc::CritScope cs(&lock_);
178 if (_timestampMap.Pop(timestamp) == NULL) {
179 return VCM_GENERAL_ERROR;
180 }
181 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182}
183
Magnus Jedvert46a27652017-11-13 14:10:02 +0100184VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder)
185 : VCMGenericDecoder(decoder.release(), false /* isExternal */) {}
186
Peter Boström187db632015-12-01 17:20:01 +0100187VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
188 : _callback(NULL),
189 _frameInfos(),
190 _nextFrameInfoIdx(0),
tommi5b7fc8c2017-07-05 16:45:57 -0700191 decoder_(decoder),
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +0200192 _codecType(kVideoCodecGeneric),
guidouc3372582017-04-04 07:16:21 -0700193 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700194 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
195 RTC_DCHECK(decoder_);
196}
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
tommi5b7fc8c2017-07-05 16:45:57 -0700198VCMGenericDecoder::~VCMGenericDecoder() {
199 decoder_->Release();
200 if (_isExternal)
201 decoder_.release();
tommi058aa712017-07-15 11:33:35 -0700202 RTC_DCHECK(_isExternal || decoder_);
tommi5b7fc8c2017-07-05 16:45:57 -0700203}
niklase@google.com470e71d2011-07-07 08:21:25 +0000204
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000205int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800206 int32_t numberOfCores) {
207 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
208 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
tommi5b7fc8c2017-07-05 16:45:57 -0700210 return decoder_->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
pbosd9eec762015-11-17 06:03:43 -0800213int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
Yves Gerey665174f2018-06-19 15:03:05 +0200214 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
Niels Möller23775882018-08-16 10:24:12 +0200215 frame.Timestamp());
Yves Gerey665174f2018-06-19 15:03:05 +0200216 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
217 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
218 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
219 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
220 // Set correctly only for key frames. Thus, use latest key frame
221 // content type. If the corresponding key frame was lost, decode will fail
222 // and content type will be ignored.
223 if (frame.FrameType() == kVideoFrameKey) {
224 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
225 _last_keyframe_content_type = frame.contentType();
226 } else {
227 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
228 }
Niels Möller23775882018-08-16 10:24:12 +0200229 _callback->Map(frame.Timestamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
Yves Gerey665174f2018-06-19 15:03:05 +0200231 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
232 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
233 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
Yves Gerey665174f2018-06-19 15:03:05 +0200235 _callback->OnDecoderImplementationName(decoder_->ImplementationName());
236 if (ret < WEBRTC_VIDEO_CODEC_OK) {
237 RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp "
Niels Möller23775882018-08-16 10:24:12 +0200238 << frame.Timestamp() << ", error code: " << ret;
239 _callback->Pop(frame.Timestamp());
Lu Liu352314a2018-02-21 19:38:59 +0000240 return ret;
Niels Möller9cccf852018-12-04 11:01:26 +0100241 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT) {
Yves Gerey665174f2018-06-19 15:03:05 +0200242 // No output
Niels Möller23775882018-08-16 10:24:12 +0200243 _callback->Pop(frame.Timestamp());
Yves Gerey665174f2018-06-19 15:03:05 +0200244 }
245 return ret;
guidouc3372582017-04-04 07:16:21 -0700246}
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
Peter Boström187db632015-12-01 17:20:01 +0100248int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
249 VCMDecodedFrameCallback* callback) {
250 _callback = callback;
tommi5b7fc8c2017-07-05 16:45:57 -0700251 return decoder_->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000252}
253
perkj796cfaf2015-12-10 09:27:38 -0800254bool VCMGenericDecoder::PrefersLateDecoding() const {
tommi5b7fc8c2017-07-05 16:45:57 -0700255 return decoder_->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800256}
257
philipel9d3ab612015-12-21 04:12:39 -0800258} // namespace webrtc