blob: b1059e8a1b6997a378b85400d57860a5b7cdf878 [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
nisse1c0dea82017-01-30 02:43:18 -080077 decodedImage.set_timestamp_us(
78 frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec);
sakal55d932b2016-09-30 06:19:08 -070079 decodedImage.set_rotation(frameInfo->rotation);
sakald2275222016-10-03 08:54:39 -070080 // TODO(sakal): Investigate why callback is NULL sometimes and replace if
81 // statement with a DCHECK.
82 if (callback) {
83 callback->FrameToRender(decodedImage);
84 } else {
85 LOG(LS_WARNING) << "No callback, dropping frame.";
86 }
philipel9d3ab612015-12-21 04:12:39 -080087 return WEBRTC_VIDEO_CODEC_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +000088}
89
philipel9d3ab612015-12-21 04:12:39 -080090int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
91 const uint64_t pictureId) {
92 CriticalSectionScoped cs(_critSect);
93 if (_receiveCallback != NULL) {
94 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
95 }
96 return -1;
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) {
111 CriticalSectionScoped cs(_critSect);
112 if (_receiveCallback)
113 _receiveCallback->OnDecoderImplementationName(implementation_name);
114}
115
pbos1968d3f2015-09-28 08:52:18 -0700116void VCMDecodedFrameCallback::Map(uint32_t timestamp,
117 VCMFrameInformation* frameInfo) {
118 CriticalSectionScoped cs(_critSect);
119 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
121
philipel9d3ab612015-12-21 04:12:39 -0800122int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
123 CriticalSectionScoped cs(_critSect);
124 if (_timestampMap.Pop(timestamp) == NULL) {
125 return VCM_GENERAL_ERROR;
126 }
127 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128}
129
Peter Boström187db632015-12-01 17:20:01 +0100130VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
131 : _callback(NULL),
132 _frameInfos(),
133 _nextFrameInfoIdx(0),
134 _decoder(decoder),
135 _codecType(kVideoCodecUnknown),
136 _isExternal(isExternal),
137 _keyFrameDecoded(false) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
Peter Boström187db632015-12-01 17:20:01 +0100139VCMGenericDecoder::~VCMGenericDecoder() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000141int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800142 int32_t numberOfCores) {
143 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
144 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
philipel9d3ab612015-12-21 04:12:39 -0800146 return _decoder->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000147}
148
pbosd9eec762015-11-17 06:03:43 -0800149int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
150 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
151 frame.EncodedImage()._timeStamp);
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +0000152 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000153 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000154 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
156
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
nisse6f112cc2016-09-30 03:43:00 -0700158 const RTPFragmentationHeader dummy_header;
Peter Boström187db632015-12-01 17:20:01 +0100159 int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(),
nisse6f112cc2016-09-30 03:43:00 -0700160 &dummy_header,
Peter Boström187db632015-12-01 17:20:01 +0100161 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000162
Peter Boströmb7d9a972015-12-18 16:01:11 +0100163 _callback->OnDecoderImplementationName(_decoder->ImplementationName());
philipel9d3ab612015-12-21 04:12:39 -0800164 if (ret < WEBRTC_VIDEO_CODEC_OK) {
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000165 LOG(LS_WARNING) << "Failed to decode frame with timestamp "
166 << frame.TimeStamp() << ", error code: " << ret;
niklase@google.com470e71d2011-07-07 08:21:25 +0000167 _callback->Pop(frame.TimeStamp());
168 return ret;
philipel9d3ab612015-12-21 04:12:39 -0800169 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT ||
170 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) {
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000171 // No output
172 _callback->Pop(frame.TimeStamp());
173 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 return ret;
175}
176
Peter Boström187db632015-12-01 17:20:01 +0100177int32_t VCMGenericDecoder::Release() {
178 return _decoder->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}
180
Peter Boström187db632015-12-01 17:20:01 +0100181int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
182 VCMDecodedFrameCallback* callback) {
183 _callback = callback;
184 return _decoder->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
Peter Boström187db632015-12-01 17:20:01 +0100187bool VCMGenericDecoder::External() const {
188 return _isExternal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
perkj796cfaf2015-12-10 09:27:38 -0800191bool VCMGenericDecoder::PrefersLateDecoding() const {
192 return _decoder->PrefersLateDecoding();
193}
194
philipel9d3ab612015-12-21 04:12:39 -0800195} // namespace webrtc