blob: 42ee8b600c7f2564d6368a2fd4c24f95d0fa210c [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
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020013#include "webrtc/base/checks.h"
14#include "webrtc/base/logging.h"
15#include "webrtc/base/timeutils.h"
16#include "webrtc/base/trace_event.h"
kjellanderc8fa6922017-06-30 14:02:00 -070017#include "webrtc/modules/video_coding/include/video_coding.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010018#include "webrtc/modules/video_coding/internal_defines.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010019#include "webrtc/system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
22
philipel9d3ab612015-12-21 04:12:39 -080023VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000024 Clock* clock)
tommid0a71ba2017-03-14 04:16:20 -070025 : _clock(clock),
Peter Boströmb7d9a972015-12-18 16:01:11 +010026 _timing(timing),
27 _timestampMap(kDecoderFrameMemoryLength),
ilnik04f4d122017-06-19 07:18:55 -070028 _lastReceivedPictureID(0) {
29 ntp_offset_ =
30 _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds();
31}
niklase@google.com470e71d2011-07-07 08:21:25 +000032
philipel9d3ab612015-12-21 04:12:39 -080033VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000036void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080037 VCMReceiveCallback* receiveCallback) {
tommid0a71ba2017-03-14 04:16:20 -070038 RTC_DCHECK(construction_thread_.CalledOnValidThread());
39 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
40 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080041 _receiveCallback = receiveCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
philipel9d3ab612015-12-21 04:12:39 -080044VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
tommid0a71ba2017-03-14 04:16:20 -070045 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
46 // The callback must always have been set before this happens.
47 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080048 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000049}
50
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070051int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Per327d8ba2015-11-10 14:00:27 +010052 return Decoded(decodedImage, -1);
53}
54
55int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
56 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080057 Decoded(decodedImage,
58 decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms)
59 : rtc::Optional<int32_t>(),
60 rtc::Optional<uint8_t>());
61 return WEBRTC_VIDEO_CODEC_OK;
62}
63
64void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
65 rtc::Optional<int32_t> decode_time_ms,
66 rtc::Optional<uint8_t> qp) {
tommid0a71ba2017-03-14 04:16:20 -070067 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
philipel9d3ab612015-12-21 04:12:39 -080068 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
69 "timestamp", decodedImage.timestamp());
70 // TODO(holmer): We should improve this so that we can handle multiple
71 // callbacks from one call to Decode().
guidouc3372582017-04-04 07:16:21 -070072 VCMFrameInformation* frameInfo;
73 {
74 rtc::CritScope cs(&lock_);
75 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
76 }
philipel9d3ab612015-12-21 04:12:39 -080077
78 if (frameInfo == NULL) {
79 LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
80 "this one.";
sakalcc452e12017-02-09 04:53:45 -080081 return;
philipel9d3ab612015-12-21 04:12:39 -080082 }
83
84 const int64_t now_ms = _clock->TimeInMilliseconds();
sakalcc452e12017-02-09 04:53:45 -080085 if (!decode_time_ms) {
philipel9d3ab612015-12-21 04:12:39 -080086 decode_time_ms =
sakalcc452e12017-02-09 04:53:45 -080087 rtc::Optional<int32_t>(now_ms - frameInfo->decodeStartTimeMs);
philipel9d3ab612015-12-21 04:12:39 -080088 }
sakalcc452e12017-02-09 04:53:45 -080089 _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms,
philipel9d3ab612015-12-21 04:12:39 -080090 frameInfo->renderTimeMs);
91
ilnik04f4d122017-06-19 07:18:55 -070092 // Report timing information.
93 if (frameInfo->timing.is_timing_frame) {
94 // Convert remote timestamps to local time from ntp timestamps.
95 frameInfo->timing.encode_start_ms -= ntp_offset_;
96 frameInfo->timing.encode_finish_ms -= ntp_offset_;
97 frameInfo->timing.packetization_finish_ms -= ntp_offset_;
98 frameInfo->timing.pacer_exit_ms -= ntp_offset_;
99 frameInfo->timing.network_timestamp_ms -= ntp_offset_;
100 frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
101 // TODO(ilnik): Report timing information here.
102 // Capture time: decodedImage.ntp_time_ms() - ntp_offset
103 // Encode start: frameInfo->timing.encode_start_ms
104 // Encode finish: frameInfo->timing.encode_finish_ms
105 // Packetization done: frameInfo->timing.packetization_finish_ms
106 // Pacer exit: frameInfo->timing.pacer_exit_ms
107 // Network timestamp: frameInfo->timing.network_timestamp_ms
108 // Network2 timestamp: frameInfo->timing.network2_timestamp_ms
109 // Receive start: frameInfo->timing.receive_start_ms
110 // Receive finish: frameInfo->timing.receive_finish_ms
111 // Decode start: frameInfo->decodeStartTimeMs
112 // Decode finish: now_ms
113 // Render time: frameInfo->renderTimeMs
114 }
115
nisse1c0dea82017-01-30 02:43:18 -0800116 decodedImage.set_timestamp_us(
117 frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec);
sakal55d932b2016-09-30 06:19:08 -0700118 decodedImage.set_rotation(frameInfo->rotation);
ilnik00d802b2017-04-11 10:34:31 -0700119 _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
121
philipel9d3ab612015-12-21 04:12:39 -0800122int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
123 const uint64_t pictureId) {
tommid0a71ba2017-03-14 04:16:20 -0700124 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
philipel9d3ab612015-12-21 04:12:39 -0800127int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame(
128 const uint64_t pictureId) {
129 _lastReceivedPictureID = pictureId;
130 return 0;
131}
132
133uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const {
134 return _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135}
136
Peter Boströmb7d9a972015-12-18 16:01:11 +0100137void VCMDecodedFrameCallback::OnDecoderImplementationName(
138 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700139 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100140}
141
pbos1968d3f2015-09-28 08:52:18 -0700142void VCMDecodedFrameCallback::Map(uint32_t timestamp,
143 VCMFrameInformation* frameInfo) {
guidouc3372582017-04-04 07:16:21 -0700144 rtc::CritScope cs(&lock_);
pbos1968d3f2015-09-28 08:52:18 -0700145 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146}
147
philipel9d3ab612015-12-21 04:12:39 -0800148int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
guidouc3372582017-04-04 07:16:21 -0700149 rtc::CritScope cs(&lock_);
150 if (_timestampMap.Pop(timestamp) == NULL) {
151 return VCM_GENERAL_ERROR;
152 }
153 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154}
155
Peter Boström187db632015-12-01 17:20:01 +0100156VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
157 : _callback(NULL),
158 _frameInfos(),
159 _nextFrameInfoIdx(0),
tommi5b7fc8c2017-07-05 16:45:57 -0700160 decoder_(decoder),
Peter Boström187db632015-12-01 17:20:01 +0100161 _codecType(kVideoCodecUnknown),
guidouc3372582017-04-04 07:16:21 -0700162 _isExternal(isExternal),
tommi5b7fc8c2017-07-05 16:45:57 -0700163 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
164 RTC_DCHECK(decoder_);
165}
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
tommi5b7fc8c2017-07-05 16:45:57 -0700167VCMGenericDecoder::~VCMGenericDecoder() {
168 decoder_->Release();
169 if (_isExternal)
170 decoder_.release();
171 RTC_DCHECK(_isExternal || !decoder_);
172}
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000174int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800175 int32_t numberOfCores) {
176 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
177 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
tommi5b7fc8c2017-07-05 16:45:57 -0700179 return decoder_->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180}
181
pbosd9eec762015-11-17 06:03:43 -0800182int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
guidouc3372582017-04-04 07:16:21 -0700183 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
184 frame.EncodedImage()._timeStamp);
185 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
186 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
187 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
ilnik04f4d122017-06-19 07:18:55 -0700188 _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
ilnik00d802b2017-04-11 10:34:31 -0700189 // Set correctly only for key frames. Thus, use latest key frame
190 // content type. If the corresponding key frame was lost, decode will fail
191 // and content type will be ignored.
192 if (frame.FrameType() == kVideoFrameKey) {
193 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
194 _last_keyframe_content_type = frame.contentType();
195 } else {
196 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
197 }
guidouc3372582017-04-04 07:16:21 -0700198 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
guidouc3372582017-04-04 07:16:21 -0700200 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
201 const RTPFragmentationHeader dummy_header;
tommi5b7fc8c2017-07-05 16:45:57 -0700202 int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
guidouc3372582017-04-04 07:16:21 -0700203 &dummy_header,
204 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
tommi5b7fc8c2017-07-05 16:45:57 -0700206 _callback->OnDecoderImplementationName(decoder_->ImplementationName());
philipel9d3ab612015-12-21 04:12:39 -0800207 if (ret < WEBRTC_VIDEO_CODEC_OK) {
guidouc3372582017-04-04 07:16:21 -0700208 LOG(LS_WARNING) << "Failed to decode frame with timestamp "
209 << frame.TimeStamp() << ", error code: " << ret;
210 _callback->Pop(frame.TimeStamp());
211 return ret;
212 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT ||
213 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) {
214 // No output
215 _callback->Pop(frame.TimeStamp());
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000216 }
guidouc3372582017-04-04 07:16:21 -0700217 return ret;
218}
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
Peter Boström187db632015-12-01 17:20:01 +0100220int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
221 VCMDecodedFrameCallback* callback) {
222 _callback = callback;
tommi5b7fc8c2017-07-05 16:45:57 -0700223 return decoder_->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
perkj796cfaf2015-12-10 09:27:38 -0800226bool VCMGenericDecoder::PrefersLateDecoding() const {
tommi5b7fc8c2017-07-05 16:45:57 -0700227 return decoder_->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800228}
229
philipel9d3ab612015-12-21 04:12:39 -0800230} // namespace webrtc