blob: f5d9cfe07396ab03d9767a6ec6ac0fa38ed6c1dc [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"
pbosd9eec762015-11-17 06:03:43 -080013#include "webrtc/base/trace_event.h"
Peter Boströma443ec12015-11-30 19:14:50 +010014#include "webrtc/modules/video_coding/include/video_coding.h"
guidouc3372582017-04-04 07:16:21 -070015#include "webrtc/modules/video_coding/generic_decoder.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010016#include "webrtc/modules/video_coding/internal_defines.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19namespace webrtc {
20
philipel9d3ab612015-12-21 04:12:39 -080021VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000022 Clock* clock)
tommid0a71ba2017-03-14 04:16:20 -070023 : _clock(clock),
Peter Boströmb7d9a972015-12-18 16:01:11 +010024 _timing(timing),
25 _timestampMap(kDecoderFrameMemoryLength),
guidouc3372582017-04-04 07:16:21 -070026 _lastReceivedPictureID(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000027
philipel9d3ab612015-12-21 04:12:39 -080028VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {
niklase@google.com470e71d2011-07-07 08:21:25 +000029}
30
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000031void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080032 VCMReceiveCallback* receiveCallback) {
tommid0a71ba2017-03-14 04:16:20 -070033 RTC_DCHECK(construction_thread_.CalledOnValidThread());
34 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
35 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080036 _receiveCallback = receiveCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
philipel9d3ab612015-12-21 04:12:39 -080039VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
tommid0a71ba2017-03-14 04:16:20 -070040 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
41 // The callback must always have been set before this happens.
42 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080043 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000044}
45
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070046int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Per327d8ba2015-11-10 14:00:27 +010047 return Decoded(decodedImage, -1);
48}
49
50int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
51 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080052 Decoded(decodedImage,
53 decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms)
54 : rtc::Optional<int32_t>(),
55 rtc::Optional<uint8_t>());
56 return WEBRTC_VIDEO_CODEC_OK;
57}
58
59void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
60 rtc::Optional<int32_t> decode_time_ms,
61 rtc::Optional<uint8_t> qp) {
tommid0a71ba2017-03-14 04:16:20 -070062 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
philipel9d3ab612015-12-21 04:12:39 -080063 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
64 "timestamp", decodedImage.timestamp());
65 // TODO(holmer): We should improve this so that we can handle multiple
66 // callbacks from one call to Decode().
guidouc3372582017-04-04 07:16:21 -070067 VCMFrameInformation* frameInfo;
68 {
69 rtc::CritScope cs(&lock_);
70 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
71 }
philipel9d3ab612015-12-21 04:12:39 -080072
73 if (frameInfo == NULL) {
74 LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
75 "this one.";
sakalcc452e12017-02-09 04:53:45 -080076 return;
philipel9d3ab612015-12-21 04:12:39 -080077 }
78
79 const int64_t now_ms = _clock->TimeInMilliseconds();
sakalcc452e12017-02-09 04:53:45 -080080 if (!decode_time_ms) {
philipel9d3ab612015-12-21 04:12:39 -080081 decode_time_ms =
sakalcc452e12017-02-09 04:53:45 -080082 rtc::Optional<int32_t>(now_ms - frameInfo->decodeStartTimeMs);
philipel9d3ab612015-12-21 04:12:39 -080083 }
sakalcc452e12017-02-09 04:53:45 -080084 _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms,
philipel9d3ab612015-12-21 04:12:39 -080085 frameInfo->renderTimeMs);
86
nisse1c0dea82017-01-30 02:43:18 -080087 decodedImage.set_timestamp_us(
88 frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec);
sakal55d932b2016-09-30 06:19:08 -070089 decodedImage.set_rotation(frameInfo->rotation);
ilnik00d802b2017-04-11 10:34:31 -070090 _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +000091}
92
philipel9d3ab612015-12-21 04:12:39 -080093int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
94 const uint64_t pictureId) {
tommid0a71ba2017-03-14 04:16:20 -070095 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
niklase@google.com470e71d2011-07-07 08:21:25 +000096}
97
philipel9d3ab612015-12-21 04:12:39 -080098int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame(
99 const uint64_t pictureId) {
100 _lastReceivedPictureID = pictureId;
101 return 0;
102}
103
104uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const {
105 return _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106}
107
Peter Boströmb7d9a972015-12-18 16:01:11 +0100108void VCMDecodedFrameCallback::OnDecoderImplementationName(
109 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700110 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100111}
112
pbos1968d3f2015-09-28 08:52:18 -0700113void VCMDecodedFrameCallback::Map(uint32_t timestamp,
114 VCMFrameInformation* frameInfo) {
guidouc3372582017-04-04 07:16:21 -0700115 rtc::CritScope cs(&lock_);
pbos1968d3f2015-09-28 08:52:18 -0700116 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117}
118
philipel9d3ab612015-12-21 04:12:39 -0800119int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
guidouc3372582017-04-04 07:16:21 -0700120 rtc::CritScope cs(&lock_);
121 if (_timestampMap.Pop(timestamp) == NULL) {
122 return VCM_GENERAL_ERROR;
123 }
124 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
Peter Boström187db632015-12-01 17:20:01 +0100127VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
128 : _callback(NULL),
129 _frameInfos(),
130 _nextFrameInfoIdx(0),
guidouc3372582017-04-04 07:16:21 -0700131 _decoder(decoder),
Peter Boström187db632015-12-01 17:20:01 +0100132 _codecType(kVideoCodecUnknown),
guidouc3372582017-04-04 07:16:21 -0700133 _isExternal(isExternal),
ilnik00d802b2017-04-11 10:34:31 -0700134 _keyFrameDecoded(false),
135 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
guidouc3372582017-04-04 07:16:21 -0700137VCMGenericDecoder::~VCMGenericDecoder() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000139int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800140 int32_t numberOfCores) {
141 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
142 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
guidouc3372582017-04-04 07:16:21 -0700144 return _decoder->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145}
146
pbosd9eec762015-11-17 06:03:43 -0800147int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
guidouc3372582017-04-04 07:16:21 -0700148 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
149 frame.EncodedImage()._timeStamp);
150 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
151 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
152 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
ilnik00d802b2017-04-11 10:34:31 -0700153 // Set correctly only for key frames. Thus, use latest key frame
154 // content type. If the corresponding key frame was lost, decode will fail
155 // and content type will be ignored.
156 if (frame.FrameType() == kVideoFrameKey) {
157 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
158 _last_keyframe_content_type = frame.contentType();
159 } else {
160 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
161 }
guidouc3372582017-04-04 07:16:21 -0700162 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
guidouc3372582017-04-04 07:16:21 -0700164 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
165 const RTPFragmentationHeader dummy_header;
166 int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(),
167 &dummy_header,
168 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000169
guidouc3372582017-04-04 07:16:21 -0700170 _callback->OnDecoderImplementationName(_decoder->ImplementationName());
philipel9d3ab612015-12-21 04:12:39 -0800171 if (ret < WEBRTC_VIDEO_CODEC_OK) {
guidouc3372582017-04-04 07:16:21 -0700172 LOG(LS_WARNING) << "Failed to decode frame with timestamp "
173 << frame.TimeStamp() << ", error code: " << ret;
174 _callback->Pop(frame.TimeStamp());
175 return ret;
176 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT ||
177 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) {
178 // No output
179 _callback->Pop(frame.TimeStamp());
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000180 }
guidouc3372582017-04-04 07:16:21 -0700181 return ret;
182}
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
guidouc3372582017-04-04 07:16:21 -0700184int32_t VCMGenericDecoder::Release() {
185 return _decoder->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
Peter Boström187db632015-12-01 17:20:01 +0100188int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
189 VCMDecodedFrameCallback* callback) {
190 _callback = callback;
guidouc3372582017-04-04 07:16:21 -0700191 return _decoder->RegisterDecodeCompleteCallback(callback);
192}
193
194bool VCMGenericDecoder::External() const {
195 return _isExternal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
perkj796cfaf2015-12-10 09:27:38 -0800198bool VCMGenericDecoder::PrefersLateDecoding() const {
guidouc3372582017-04-04 07:16:21 -0700199 return _decoder->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800200}
201
philipel9d3ab612015-12-21 04:12:39 -0800202} // namespace webrtc