blob: 722b0b41546fad58afeaa96564741f150da7fec9 [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
sakal55d932b2016-09-30 06:19:08 -070011#include "webrtc/base/checks.h"
pbos854e84c2015-11-16 16:39:06 -080012#include "webrtc/base/logging.h"
nissef93752a2017-05-10 05:25:59 -070013#include "webrtc/base/timeutils.h"
pbosd9eec762015-11-17 06:03:43 -080014#include "webrtc/base/trace_event.h"
Peter Boströma443ec12015-11-30 19:14:50 +010015#include "webrtc/modules/video_coding/include/video_coding.h"
guidouc3372582017-04-04 07:16:21 -070016#include "webrtc/modules/video_coding/generic_decoder.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010017#include "webrtc/modules/video_coding/internal_defines.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010018#include "webrtc/system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20namespace webrtc {
21
philipel9d3ab612015-12-21 04:12:39 -080022VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000023 Clock* clock)
tommid0a71ba2017-03-14 04:16:20 -070024 : _clock(clock),
Peter Boströmb7d9a972015-12-18 16:01:11 +010025 _timing(timing),
26 _timestampMap(kDecoderFrameMemoryLength),
ilnik04f4d122017-06-19 07:18:55 -070027 _lastReceivedPictureID(0) {
28 ntp_offset_ =
29 _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds();
30}
niklase@google.com470e71d2011-07-07 08:21:25 +000031
philipel9d3ab612015-12-21 04:12:39 -080032VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000035void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080036 VCMReceiveCallback* receiveCallback) {
tommid0a71ba2017-03-14 04:16:20 -070037 RTC_DCHECK(construction_thread_.CalledOnValidThread());
38 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
39 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080040 _receiveCallback = receiveCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
philipel9d3ab612015-12-21 04:12:39 -080043VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
tommid0a71ba2017-03-14 04:16:20 -070044 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
45 // The callback must always have been set before this happens.
46 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080047 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000048}
49
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070050int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Per327d8ba2015-11-10 14:00:27 +010051 return Decoded(decodedImage, -1);
52}
53
54int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
55 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080056 Decoded(decodedImage,
57 decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms)
58 : rtc::Optional<int32_t>(),
59 rtc::Optional<uint8_t>());
60 return WEBRTC_VIDEO_CODEC_OK;
61}
62
63void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
64 rtc::Optional<int32_t> decode_time_ms,
65 rtc::Optional<uint8_t> qp) {
tommid0a71ba2017-03-14 04:16:20 -070066 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
philipel9d3ab612015-12-21 04:12:39 -080067 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
68 "timestamp", decodedImage.timestamp());
69 // TODO(holmer): We should improve this so that we can handle multiple
70 // callbacks from one call to Decode().
guidouc3372582017-04-04 07:16:21 -070071 VCMFrameInformation* frameInfo;
72 {
73 rtc::CritScope cs(&lock_);
74 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
75 }
philipel9d3ab612015-12-21 04:12:39 -080076
77 if (frameInfo == NULL) {
78 LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
79 "this one.";
sakalcc452e12017-02-09 04:53:45 -080080 return;
philipel9d3ab612015-12-21 04:12:39 -080081 }
82
83 const int64_t now_ms = _clock->TimeInMilliseconds();
sakalcc452e12017-02-09 04:53:45 -080084 if (!decode_time_ms) {
philipel9d3ab612015-12-21 04:12:39 -080085 decode_time_ms =
sakalcc452e12017-02-09 04:53:45 -080086 rtc::Optional<int32_t>(now_ms - frameInfo->decodeStartTimeMs);
philipel9d3ab612015-12-21 04:12:39 -080087 }
sakalcc452e12017-02-09 04:53:45 -080088 _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms,
philipel9d3ab612015-12-21 04:12:39 -080089 frameInfo->renderTimeMs);
90
ilnik04f4d122017-06-19 07:18:55 -070091 // Report timing information.
92 if (frameInfo->timing.is_timing_frame) {
93 // Convert remote timestamps to local time from ntp timestamps.
94 frameInfo->timing.encode_start_ms -= ntp_offset_;
95 frameInfo->timing.encode_finish_ms -= ntp_offset_;
96 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
97 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
98 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
99 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
100 // TODO(ilnik): Report timing information here.
101 // Capture time: decodedImage.ntp_time_ms() - ntp_offset
102 // Encode start: frameInfo->timing.encode_start_ms
103 // Encode finish: frameInfo->timing.encode_finish_ms
104 // Packetization done: frameInfo->timing.packetization_finish_ms
105 // Pacer exit: frameInfo->timing.pacer_exit_ms
106 // Network timestamp: frameInfo->timing.network_timestamp_ms
107 // Network2 timestamp: frameInfo->timing.network2_timestamp_ms
108 // Receive start: frameInfo->timing.receive_start_ms
109 // Receive finish: frameInfo->timing.receive_finish_ms
110 // Decode start: frameInfo->decodeStartTimeMs
111 // Decode finish: now_ms
112 // Render time: frameInfo->renderTimeMs
113 }
114
nisse1c0dea82017-01-30 02:43:18 -0800115 decodedImage.set_timestamp_us(
116 frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec);
sakal55d932b2016-09-30 06:19:08 -0700117 decodedImage.set_rotation(frameInfo->rotation);
ilnik00d802b2017-04-11 10:34:31 -0700118 _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119}
120
philipel9d3ab612015-12-21 04:12:39 -0800121int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
122 const uint64_t pictureId) {
tommid0a71ba2017-03-14 04:16:20 -0700123 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000124}
125
philipel9d3ab612015-12-21 04:12:39 -0800126int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame(
127 const uint64_t pictureId) {
128 _lastReceivedPictureID = pictureId;
129 return 0;
130}
131
132uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const {
133 return _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134}
135
Peter Boströmb7d9a972015-12-18 16:01:11 +0100136void VCMDecodedFrameCallback::OnDecoderImplementationName(
137 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700138 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100139}
140
pbos1968d3f2015-09-28 08:52:18 -0700141void VCMDecodedFrameCallback::Map(uint32_t timestamp,
142 VCMFrameInformation* frameInfo) {
guidouc3372582017-04-04 07:16:21 -0700143 rtc::CritScope cs(&lock_);
pbos1968d3f2015-09-28 08:52:18 -0700144 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145}
146
philipel9d3ab612015-12-21 04:12:39 -0800147int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
guidouc3372582017-04-04 07:16:21 -0700148 rtc::CritScope cs(&lock_);
149 if (_timestampMap.Pop(timestamp) == NULL) {
150 return VCM_GENERAL_ERROR;
151 }
152 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000153}
154
Peter Boström187db632015-12-01 17:20:01 +0100155VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
156 : _callback(NULL),
157 _frameInfos(),
158 _nextFrameInfoIdx(0),
guidouc3372582017-04-04 07:16:21 -0700159 _decoder(decoder),
Peter Boström187db632015-12-01 17:20:01 +0100160 _codecType(kVideoCodecUnknown),
guidouc3372582017-04-04 07:16:21 -0700161 _isExternal(isExternal),
ilnik00d802b2017-04-11 10:34:31 -0700162 _keyFrameDecoded(false),
163 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
guidouc3372582017-04-04 07:16:21 -0700165VCMGenericDecoder::~VCMGenericDecoder() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000167int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800168 int32_t numberOfCores) {
169 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
170 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
guidouc3372582017-04-04 07:16:21 -0700172 return _decoder->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000173}
174
pbosd9eec762015-11-17 06:03:43 -0800175int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
guidouc3372582017-04-04 07:16:21 -0700176 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
177 frame.EncodedImage()._timeStamp);
178 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
179 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
180 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
ilnik04f4d122017-06-19 07:18:55 -0700181 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
ilnik00d802b2017-04-11 10:34:31 -0700182 // Set correctly only for key frames. Thus, use latest key frame
183 // content type. If the corresponding key frame was lost, decode will fail
184 // and content type will be ignored.
185 if (frame.FrameType() == kVideoFrameKey) {
186 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
187 _last_keyframe_content_type = frame.contentType();
188 } else {
189 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
190 }
guidouc3372582017-04-04 07:16:21 -0700191 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
guidouc3372582017-04-04 07:16:21 -0700193 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
194 const RTPFragmentationHeader dummy_header;
195 int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(),
196 &dummy_header,
197 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
guidouc3372582017-04-04 07:16:21 -0700199 _callback->OnDecoderImplementationName(_decoder->ImplementationName());
philipel9d3ab612015-12-21 04:12:39 -0800200 if (ret < WEBRTC_VIDEO_CODEC_OK) {
guidouc3372582017-04-04 07:16:21 -0700201 LOG(LS_WARNING) << "Failed to decode frame with timestamp "
202 << frame.TimeStamp() << ", error code: " << ret;
203 _callback->Pop(frame.TimeStamp());
204 return ret;
205 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT ||
206 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) {
207 // No output
208 _callback->Pop(frame.TimeStamp());
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000209 }
guidouc3372582017-04-04 07:16:21 -0700210 return ret;
211}
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
guidouc3372582017-04-04 07:16:21 -0700213int32_t VCMGenericDecoder::Release() {
214 return _decoder->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
Peter Boström187db632015-12-01 17:20:01 +0100217int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
218 VCMDecodedFrameCallback* callback) {
219 _callback = callback;
guidouc3372582017-04-04 07:16:21 -0700220 return _decoder->RegisterDecodeCompleteCallback(callback);
221}
222
223bool VCMGenericDecoder::External() const {
224 return _isExternal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225}
226
perkj796cfaf2015-12-10 09:27:38 -0800227bool VCMGenericDecoder::PrefersLateDecoding() const {
guidouc3372582017-04-04 07:16:21 -0700228 return _decoder->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800229}
230
philipel9d3ab612015-12-21 04:12:39 -0800231} // namespace webrtc