blob: 5969f2369684030cd30c1f8a281860dc1e4ce805 [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),
guidouc3372582017-04-04 07:16:21 -070027 _lastReceivedPictureID(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000028
philipel9d3ab612015-12-21 04:12:39 -080029VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {
niklase@google.com470e71d2011-07-07 08:21:25 +000030}
31
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000032void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080033 VCMReceiveCallback* receiveCallback) {
tommid0a71ba2017-03-14 04:16:20 -070034 RTC_DCHECK(construction_thread_.CalledOnValidThread());
35 RTC_DCHECK((!_receiveCallback && receiveCallback) ||
36 (_receiveCallback && !receiveCallback));
philipel9d3ab612015-12-21 04:12:39 -080037 _receiveCallback = receiveCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
philipel9d3ab612015-12-21 04:12:39 -080040VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
tommid0a71ba2017-03-14 04:16:20 -070041 // Called on the decode thread via VCMCodecDataBase::GetDecoder.
42 // The callback must always have been set before this happens.
43 RTC_DCHECK(_receiveCallback);
philipel9d3ab612015-12-21 04:12:39 -080044 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000045}
46
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070047int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Per327d8ba2015-11-10 14:00:27 +010048 return Decoded(decodedImage, -1);
49}
50
51int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
52 int64_t decode_time_ms) {
sakalcc452e12017-02-09 04:53:45 -080053 Decoded(decodedImage,
54 decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms)
55 : rtc::Optional<int32_t>(),
56 rtc::Optional<uint8_t>());
57 return WEBRTC_VIDEO_CODEC_OK;
58}
59
60void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
61 rtc::Optional<int32_t> decode_time_ms,
62 rtc::Optional<uint8_t> qp) {
tommid0a71ba2017-03-14 04:16:20 -070063 RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
philipel9d3ab612015-12-21 04:12:39 -080064 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
65 "timestamp", decodedImage.timestamp());
66 // TODO(holmer): We should improve this so that we can handle multiple
67 // callbacks from one call to Decode().
guidouc3372582017-04-04 07:16:21 -070068 VCMFrameInformation* frameInfo;
69 {
70 rtc::CritScope cs(&lock_);
71 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
72 }
philipel9d3ab612015-12-21 04:12:39 -080073
74 if (frameInfo == NULL) {
75 LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
76 "this one.";
sakalcc452e12017-02-09 04:53:45 -080077 return;
philipel9d3ab612015-12-21 04:12:39 -080078 }
79
80 const int64_t now_ms = _clock->TimeInMilliseconds();
sakalcc452e12017-02-09 04:53:45 -080081 if (!decode_time_ms) {
philipel9d3ab612015-12-21 04:12:39 -080082 decode_time_ms =
sakalcc452e12017-02-09 04:53:45 -080083 rtc::Optional<int32_t>(now_ms - frameInfo->decodeStartTimeMs);
philipel9d3ab612015-12-21 04:12:39 -080084 }
sakalcc452e12017-02-09 04:53:45 -080085 _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms,
philipel9d3ab612015-12-21 04:12:39 -080086 frameInfo->renderTimeMs);
87
nisse1c0dea82017-01-30 02:43:18 -080088 decodedImage.set_timestamp_us(
89 frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec);
sakal55d932b2016-09-30 06:19:08 -070090 decodedImage.set_rotation(frameInfo->rotation);
ilnik00d802b2017-04-11 10:34:31 -070091 _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type);
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
philipel9d3ab612015-12-21 04:12:39 -080094int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
95 const uint64_t pictureId) {
tommid0a71ba2017-03-14 04:16:20 -070096 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
philipel9d3ab612015-12-21 04:12:39 -080099int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame(
100 const uint64_t pictureId) {
101 _lastReceivedPictureID = pictureId;
102 return 0;
103}
104
105uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const {
106 return _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
108
Peter Boströmb7d9a972015-12-18 16:01:11 +0100109void VCMDecodedFrameCallback::OnDecoderImplementationName(
110 const char* implementation_name) {
tommid0a71ba2017-03-14 04:16:20 -0700111 _receiveCallback->OnDecoderImplementationName(implementation_name);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100112}
113
pbos1968d3f2015-09-28 08:52:18 -0700114void VCMDecodedFrameCallback::Map(uint32_t timestamp,
115 VCMFrameInformation* frameInfo) {
guidouc3372582017-04-04 07:16:21 -0700116 rtc::CritScope cs(&lock_);
pbos1968d3f2015-09-28 08:52:18 -0700117 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000118}
119
philipel9d3ab612015-12-21 04:12:39 -0800120int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
guidouc3372582017-04-04 07:16:21 -0700121 rtc::CritScope cs(&lock_);
122 if (_timestampMap.Pop(timestamp) == NULL) {
123 return VCM_GENERAL_ERROR;
124 }
125 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126}
127
Peter Boström187db632015-12-01 17:20:01 +0100128VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
129 : _callback(NULL),
130 _frameInfos(),
131 _nextFrameInfoIdx(0),
guidouc3372582017-04-04 07:16:21 -0700132 _decoder(decoder),
Peter Boström187db632015-12-01 17:20:01 +0100133 _codecType(kVideoCodecUnknown),
guidouc3372582017-04-04 07:16:21 -0700134 _isExternal(isExternal),
ilnik00d802b2017-04-11 10:34:31 -0700135 _keyFrameDecoded(false),
136 _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000137
guidouc3372582017-04-04 07:16:21 -0700138VCMGenericDecoder::~VCMGenericDecoder() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000139
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000140int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800141 int32_t numberOfCores) {
142 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
143 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
guidouc3372582017-04-04 07:16:21 -0700145 return _decoder->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146}
147
pbosd9eec762015-11-17 06:03:43 -0800148int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
guidouc3372582017-04-04 07:16:21 -0700149 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
150 frame.EncodedImage()._timeStamp);
151 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
152 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
153 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
ilnik00d802b2017-04-11 10:34:31 -0700154 // Set correctly only for key frames. Thus, use latest key frame
155 // content type. If the corresponding key frame was lost, decode will fail
156 // and content type will be ignored.
157 if (frame.FrameType() == kVideoFrameKey) {
158 _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
159 _last_keyframe_content_type = frame.contentType();
160 } else {
161 _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
162 }
guidouc3372582017-04-04 07:16:21 -0700163 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
guidouc3372582017-04-04 07:16:21 -0700165 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
166 const RTPFragmentationHeader dummy_header;
167 int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(),
168 &dummy_header,
169 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
guidouc3372582017-04-04 07:16:21 -0700171 _callback->OnDecoderImplementationName(_decoder->ImplementationName());
philipel9d3ab612015-12-21 04:12:39 -0800172 if (ret < WEBRTC_VIDEO_CODEC_OK) {
guidouc3372582017-04-04 07:16:21 -0700173 LOG(LS_WARNING) << "Failed to decode frame with timestamp "
174 << frame.TimeStamp() << ", error code: " << ret;
175 _callback->Pop(frame.TimeStamp());
176 return ret;
177 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT ||
178 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) {
179 // No output
180 _callback->Pop(frame.TimeStamp());
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000181 }
guidouc3372582017-04-04 07:16:21 -0700182 return ret;
183}
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
guidouc3372582017-04-04 07:16:21 -0700185int32_t VCMGenericDecoder::Release() {
186 return _decoder->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
Peter Boström187db632015-12-01 17:20:01 +0100189int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
190 VCMDecodedFrameCallback* callback) {
191 _callback = callback;
guidouc3372582017-04-04 07:16:21 -0700192 return _decoder->RegisterDecodeCompleteCallback(callback);
193}
194
195bool VCMGenericDecoder::External() const {
196 return _isExternal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197}
198
perkj796cfaf2015-12-10 09:27:38 -0800199bool VCMGenericDecoder::PrefersLateDecoding() const {
guidouc3372582017-04-04 07:16:21 -0700200 return _decoder->PrefersLateDecoding();
perkj796cfaf2015-12-10 09:27:38 -0800201}
202
philipel9d3ab612015-12-21 04:12:39 -0800203} // namespace webrtc