blob: 34500c69e2a39f9b69b0e2488318ce75ee0e370f [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) {
sakalcc452e12017-02-09 04:53:45 -080051 Decoded(decodedImage,
52 decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms)
53 : rtc::Optional<int32_t>(),
54 rtc::Optional<uint8_t>());
55 return WEBRTC_VIDEO_CODEC_OK;
56}
57
58void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
59 rtc::Optional<int32_t> decode_time_ms,
60 rtc::Optional<uint8_t> qp) {
philipel9d3ab612015-12-21 04:12:39 -080061 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
62 "timestamp", decodedImage.timestamp());
63 // TODO(holmer): We should improve this so that we can handle multiple
64 // callbacks from one call to Decode().
65 VCMFrameInformation* frameInfo;
66 VCMReceiveCallback* callback;
67 {
niklase@google.com470e71d2011-07-07 08:21:25 +000068 CriticalSectionScoped cs(_critSect);
philipel9d3ab612015-12-21 04:12:39 -080069 frameInfo = _timestampMap.Pop(decodedImage.timestamp());
70 callback = _receiveCallback;
71 }
72
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);
sakald2275222016-10-03 08:54:39 -070090 // TODO(sakal): Investigate why callback is NULL sometimes and replace if
91 // statement with a DCHECK.
92 if (callback) {
sakalcc452e12017-02-09 04:53:45 -080093 callback->FrameToRender(decodedImage, qp);
sakald2275222016-10-03 08:54:39 -070094 } else {
95 LOG(LS_WARNING) << "No callback, dropping frame.";
96 }
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
philipel9d3ab612015-12-21 04:12:39 -080099int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame(
100 const uint64_t pictureId) {
101 CriticalSectionScoped cs(_critSect);
102 if (_receiveCallback != NULL) {
103 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId);
104 }
105 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106}
107
philipel9d3ab612015-12-21 04:12:39 -0800108int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame(
109 const uint64_t pictureId) {
110 _lastReceivedPictureID = pictureId;
111 return 0;
112}
113
114uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const {
115 return _lastReceivedPictureID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}
117
Peter Boströmb7d9a972015-12-18 16:01:11 +0100118void VCMDecodedFrameCallback::OnDecoderImplementationName(
119 const char* implementation_name) {
120 CriticalSectionScoped cs(_critSect);
121 if (_receiveCallback)
122 _receiveCallback->OnDecoderImplementationName(implementation_name);
123}
124
pbos1968d3f2015-09-28 08:52:18 -0700125void VCMDecodedFrameCallback::Map(uint32_t timestamp,
126 VCMFrameInformation* frameInfo) {
127 CriticalSectionScoped cs(_critSect);
128 _timestampMap.Add(timestamp, frameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129}
130
philipel9d3ab612015-12-21 04:12:39 -0800131int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
132 CriticalSectionScoped cs(_critSect);
133 if (_timestampMap.Pop(timestamp) == NULL) {
134 return VCM_GENERAL_ERROR;
135 }
136 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137}
138
Peter Boström187db632015-12-01 17:20:01 +0100139VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
140 : _callback(NULL),
141 _frameInfos(),
142 _nextFrameInfoIdx(0),
143 _decoder(decoder),
144 _codecType(kVideoCodecUnknown),
145 _isExternal(isExternal),
146 _keyFrameDecoded(false) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
Peter Boström187db632015-12-01 17:20:01 +0100148VCMGenericDecoder::~VCMGenericDecoder() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000150int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
philipel9d3ab612015-12-21 04:12:39 -0800151 int32_t numberOfCores) {
152 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
153 _codecType = settings->codecType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
philipel9d3ab612015-12-21 04:12:39 -0800155 return _decoder->InitDecode(settings, numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000156}
157
pbosd9eec762015-11-17 06:03:43 -0800158int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
159 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
160 frame.EncodedImage()._timeStamp);
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +0000161 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000162 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000163 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
niklase@google.com470e71d2011-07-07 08:21:25 +0000164 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]);
165
niklase@google.com470e71d2011-07-07 08:21:25 +0000166 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
nisse6f112cc2016-09-30 03:43:00 -0700167 const RTPFragmentationHeader dummy_header;
Peter Boström187db632015-12-01 17:20:01 +0100168 int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(),
nisse6f112cc2016-09-30 03:43:00 -0700169 &dummy_header,
Peter Boström187db632015-12-01 17:20:01 +0100170 frame.CodecSpecific(), frame.RenderTimeMs());
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
Peter Boströmb7d9a972015-12-18 16:01:11 +0100172 _callback->OnDecoderImplementationName(_decoder->ImplementationName());
philipel9d3ab612015-12-21 04:12:39 -0800173 if (ret < WEBRTC_VIDEO_CODEC_OK) {
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000174 LOG(LS_WARNING) << "Failed to decode frame with timestamp "
175 << frame.TimeStamp() << ", error code: " << ret;
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 _callback->Pop(frame.TimeStamp());
177 return ret;
philipel9d3ab612015-12-21 04:12:39 -0800178 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT ||
179 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) {
stefan@webrtc.org06887ae2011-10-10 14:17:46 +0000180 // No output
181 _callback->Pop(frame.TimeStamp());
182 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000183 return ret;
184}
185
Peter Boström187db632015-12-01 17:20:01 +0100186int32_t VCMGenericDecoder::Release() {
187 return _decoder->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000188}
189
Peter Boström187db632015-12-01 17:20:01 +0100190int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
191 VCMDecodedFrameCallback* callback) {
192 _callback = callback;
193 return _decoder->RegisterDecodeCompleteCallback(callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
195
Peter Boström187db632015-12-01 17:20:01 +0100196bool VCMGenericDecoder::External() const {
197 return _isExternal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
perkj796cfaf2015-12-10 09:27:38 -0800200bool VCMGenericDecoder::PrefersLateDecoding() const {
201 return _decoder->PrefersLateDecoding();
202}
203
philipel9d3ab612015-12-21 04:12:39 -0800204} // namespace webrtc