blob: b95571b8f73793718d318eedc82fff249e9a9f17 [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"
kjellanderec192bd2015-11-30 23:14:33 -080015#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)
Peter Boströmb7d9a972015-12-18 16:01:11 +010023 : _critSect(CriticalSectionWrapper::CreateCriticalSection()),
24 _clock(clock),
25 _receiveCallback(NULL),
26 _timing(timing),
27 _timestampMap(kDecoderFrameMemoryLength),
28 _lastReceivedPictureID(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000029
philipel9d3ab612015-12-21 04:12:39 -080030VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {
31 delete _critSect;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
stefan@webrtc.org06887ae2011-10-10 14:17:46 +000034void VCMDecodedFrameCallback::SetUserReceiveCallback(
philipel9d3ab612015-12-21 04:12:39 -080035 VCMReceiveCallback* receiveCallback) {
36 CriticalSectionScoped cs(_critSect);
37 _receiveCallback = receiveCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
philipel9d3ab612015-12-21 04:12:39 -080040VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
41 CriticalSectionScoped cs(_critSect);
42 return _receiveCallback;
wuchengli@chromium.org0d94c2f2013-08-12 14:20:49 +000043}
44
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070045int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
Per327d8ba2015-11-10 14:00:27 +010046 return Decoded(decodedImage, -1);
47}
48
49int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
50 int64_t decode_time_ms) {
philipel9d3ab612015-12-21 04:12:39 -080051 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
52 "timestamp", decodedImage.timestamp());
53 // TODO(holmer): We should improve this so that we can handle multiple
54 // callbacks from one call to Decode().
55 VCMFrameInformation* frameInfo;
56 VCMReceiveCallback* callback;
57 {
niklase@google.com470e71d2011-07-07 08:21:25 +000058 CriticalSectionScoped cs(_critSect);
philipel9d3ab612015-12-21 04:12:39 -080059 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
60 callback = _receiveCallback;
61 }
62
63 if (frameInfo == NULL) {
64 LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
65 "this one.";
66 return WEBRTC_VIDEO_CODEC_OK;
67 }
68
69 const int64_t now_ms = _clock->TimeInMilliseconds();
70 if (decode_time_ms < 0) {
71 decode_time_ms =
72 static_cast<int32_t>(now_ms - frameInfo->decodeStartTimeMs);
73 }
74 _timing->StopDecodeTimer(decodedImage.timestamp(), decode_time_ms, now_ms,
75 frameInfo->renderTimeMs);
76
sakal55d932b2016-09-30 06:19:08 -070077 decodedImage.set_render_time_ms(frameInfo->renderTimeMs);
78 decodedImage.set_rotation(frameInfo->rotation);
sakald2275222016-10-03 08:54:39 -070079 // TODO(sakal): Investigate why callback is NULL sometimes and replace if
80 // statement with a DCHECK.
81 if (callback) {
82 callback->FrameToRender(decodedImage);
83 } else {
84 LOG(LS_WARNING) << "No callback, dropping frame.";
85 }
philipel9d3ab612015-12-21 04:12:39 -080086 return WEBRTC_VIDEO_CODEC_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
philipel9d3ab612015-12-21 04:12:39 -080089int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
90 const uint64_t pictureId) {
91 CriticalSectionScoped cs(_critSect);
92 if (_receiveCallback != NULL) {
93 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
94 }
95 return -1;
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) {
110 CriticalSectionScoped cs(_critSect);
111 if (_receiveCallback)
112 _receiveCallback->OnDecoderImplementationName(implementation_name);
113}
114
pbos1968d3f2015-09-28 08:52:18 -0700115void VCMDecodedFrameCallback::Map(uint32_t timestamp,
116 VCMFrameInformation* frameInfo) {
117 CriticalSectionScoped cs(_critSect);
118 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119}
120
philipel9d3ab612015-12-21 04:12:39 -0800121int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
122 CriticalSectionScoped cs(_critSect);
123 if (_timestampMap.Pop(timestamp) == NULL) {
124 return VCM_GENERAL_ERROR;
125 }
126 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127}
128
Peter Boström187db632015-12-01 17:20:01 +0100129VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
130 : _callback(NULL),
131 _frameInfos(),
132 _nextFrameInfoIdx(0),
133 _decoder(decoder),
134 _codecType(kVideoCodecUnknown),
135 _isExternal(isExternal),
136 _keyFrameDecoded(false) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000137
Peter Boström187db632015-12-01 17:20:01 +0100138VCMGenericDecoder::~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
philipel9d3ab612015-12-21 04:12:39 -0800145 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) {
149 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
150 frame.EncodedImage()._timeStamp);
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +0000151 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000153 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
niklase@google.com470e71d2011-07-07 08:21:25 +0000154 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
155
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
nisse6f112cc2016-09-30 03:43:00 -0700157 const RTPFragmentationHeader dummy_header;
Peter Boström187db632015-12-01 17:20:01 +0100158 int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(),
nisse6f112cc2016-09-30 03:43:00 -0700159 &dummy_header,
Peter Boström187db632015-12-01 17:20:01 +0100160 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
Peter Boströmb7d9a972015-12-18 16:01:11 +0100162 _callback->OnDecoderImplementationName(_decoder->ImplementationName());
philipel9d3ab612015-12-21 04:12:39 -0800163 if (ret < WEBRTC_VIDEO_CODEC_OK) {
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000164 LOG(LS_WARNING) << "Failed to decode frame with timestamp "
165 << frame.TimeStamp() << ", error code: " << ret;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166 _callback->Pop(frame.TimeStamp());
167 return ret;
philipel9d3ab612015-12-21 04:12:39 -0800168 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT ||
169 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) {
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000170 // No output
171 _callback->Pop(frame.TimeStamp());
172 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000173 return ret;
174}
175
Peter Boström187db632015-12-01 17:20:01 +0100176int32_t VCMGenericDecoder::Release() {
177 return _decoder->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000178}
179
Peter Boström187db632015-12-01 17:20:01 +0100180int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
181 VCMDecodedFrameCallback* callback) {
182 _callback = callback;
183 return _decoder->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
Peter Boström187db632015-12-01 17:20:01 +0100186bool VCMGenericDecoder::External() const {
187 return _isExternal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188}
189
perkj796cfaf2015-12-10 09:27:38 -0800190bool VCMGenericDecoder::PrefersLateDecoding() const {
191 return _decoder->PrefersLateDecoding();
192}
193
philipel9d3ab612015-12-21 04:12:39 -0800194} // namespace webrtc