blob: 80b08b54784d256080672b620740c6e185b9fa56 [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
tommi5b7fc8c2017-07-05 16:45:57 -070011#include "webrtc/modules/video_coding/generic_decoder.h"
12
ilnik2edc6842017-07-06 03:06:50 -070013#include <algorithm>
14
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020015#include "webrtc/base/checks.h"
16#include "webrtc/base/logging.h"
17#include "webrtc/base/timeutils.h"
18#include "webrtc/base/trace_event.h"
kjellanderc8fa6922017-06-30 14:02:00 -070019#include "webrtc/modules/video_coding/include/video_coding.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010020#include "webrtc/modules/video_coding/internal_defines.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010021#include "webrtc/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) {
31 ntp_offset_ =
32 _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds();
33}
niklase@google.com470e71d2011-07-07 08:21:25 +000034
philipel9d3ab612015-12-21 04:12:39 -080035VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
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,
60 decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms)
61 : rtc::Optional<int32_t>(),
62 rtc::Optional<uint8_t>());
63 return WEBRTC_VIDEO_CODEC_OK;
64}
65
66void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
67 rtc::Optional<int32_t> decode_time_ms,
68 rtc::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().
guidouc3372582017-04-04 07:16:21 -070074 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) {
81 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) {
philipel9d3ab612015-12-21 04:12:39 -080088 decode_time_ms =
sakalcc452e12017-02-09 04:53:45 -080089 rtc::Optional<int32_t>(now_ms - frameInfo->decodeStartTimeMs);
philipel9d3ab612015-12-21 04:12:39 -080090 }
sakalcc452e12017-02-09 04:53:45 -080091 _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms,
philipel9d3ab612015-12-21 04:12:39 -080092 frameInfo->renderTimeMs);
93
ilnik04f4d122017-06-19 07:18:55 -070094 // Report timing information.
95 if (frameInfo->timing.is_timing_frame) {
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
119 TimingFrameInfo timing_frame_info;
120
121 timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
122 timing_frame_info.encode_start_ms =
123 frameInfo->timing.encode_start_ms - sender_delta_ms;
124 timing_frame_info.encode_finish_ms =
125 frameInfo->timing.encode_finish_ms - sender_delta_ms;
126 timing_frame_info.packetization_finish_ms =
127 frameInfo->timing.packetization_finish_ms - sender_delta_ms;
128 timing_frame_info.pacer_exit_ms =
129 frameInfo->timing.pacer_exit_ms - sender_delta_ms;
130 timing_frame_info.network_timestamp_ms =
131 frameInfo->timing.network_timestamp_ms - sender_delta_ms;
132 timing_frame_info.network2_timestamp_ms =
133 frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
134 timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms;
135 timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms;
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
141 _timing->SetTimingFrameInfo(timing_frame_info);
ilnik04f4d122017-06-19 07:18:55 -0700142 }
143
nisse1c0dea82017-01-30 02:43:18 -0800144 decodedImage.set_timestamp_us(
145 frameInfo->renderTimeMs * 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) {
guidouc3372582017-04-04 07:16:21 -0700172 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) {
guidouc3372582017-04-04 07:16:21 -0700177 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
Peter Boström187db632015-12-01 17:20:01 +0100184VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
185 : _callback(NULL),
186 _frameInfos(),
187 _nextFrameInfoIdx(0),
tommi5b7fc8c2017-07-05 16:45:57 -0700188 decoder_(decoder),
Peter Boström187db632015-12-01 17:20:01 +0100189 _codecType(kVideoCodecUnknown),
guidouc3372582017-04-04 07:16:21 -0700190 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700191 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
192 RTC_DCHECK(decoder_);
193}
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
tommi5b7fc8c2017-07-05 16:45:57 -0700195VCMGenericDecoder::~VCMGenericDecoder() {
196 decoder_->Release();
197 if (_isExternal)
198 decoder_.release();
199 RTC_DCHECK(_isExternal || !decoder_);
200}
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000202int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800203 int32_t numberOfCores) {
204 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
205 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
tommi5b7fc8c2017-07-05 16:45:57 -0700207 return decoder_->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
pbosd9eec762015-11-17 06:03:43 -0800210int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
guidouc3372582017-04-04 07:16:21 -0700211 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
212 frame.EncodedImage()._timeStamp);
213 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
214 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
215 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
ilnik04f4d122017-06-19 07:18:55 -0700216 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
ilnik00d802b2017-04-11 10:34:31 -0700217 // Set correctly only for key frames. Thus, use latest key frame
218 // content type. If the corresponding key frame was lost, decode will fail
219 // and content type will be ignored.
220 if (frame.FrameType() == kVideoFrameKey) {
221 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
222 _last_keyframe_content_type = frame.contentType();
223 } else {
224 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
225 }
guidouc3372582017-04-04 07:16:21 -0700226 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
guidouc3372582017-04-04 07:16:21 -0700228 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
229 const RTPFragmentationHeader dummy_header;
tommi5b7fc8c2017-07-05 16:45:57 -0700230 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
guidouc3372582017-04-04 07:16:21 -0700231 &dummy_header,
232 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
tommi5b7fc8c2017-07-05 16:45:57 -0700234 _callback->OnDecoderImplementationName(decoder_->ImplementationName());
philipel9d3ab612015-12-21 04:12:39 -0800235 if (ret < WEBRTC_VIDEO_CODEC_OK) {
guidouc3372582017-04-04 07:16:21 -0700236 LOG(LS_WARNING) << "Failed to decode frame with timestamp "
237 << frame.TimeStamp() << ", error code: " << ret;
238 _callback->Pop(frame.TimeStamp());
239 return ret;
240 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT ||
241 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) {
242 // No output
243 _callback->Pop(frame.TimeStamp());
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000244 }
guidouc3372582017-04-04 07:16:21 -0700245 return ret;
246}
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