niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mikhal@webrtc.org | a2031d5 | 2012-07-31 15:53:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 | |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 11 | #include "webrtc/base/checks.h" |
pbos | 854e84c | 2015-11-16 16:39:06 -0800 | [diff] [blame] | 12 | #include "webrtc/base/logging.h" |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 13 | #include "webrtc/base/trace_event.h" |
Peter Boström | a443ec1 | 2015-11-30 19:14:50 +0100 | [diff] [blame] | 14 | #include "webrtc/modules/video_coding/include/video_coding.h" |
kjellander | ec192bd | 2015-11-30 23:14:33 -0800 | [diff] [blame] | 15 | #include "webrtc/modules/video_coding/generic_decoder.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 16 | #include "webrtc/modules/video_coding/internal_defines.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 21 | VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 22 | Clock* clock) |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 23 | : _critSect(CriticalSectionWrapper::CreateCriticalSection()), |
| 24 | _clock(clock), |
| 25 | _receiveCallback(NULL), |
| 26 | _timing(timing), |
| 27 | _timestampMap(kDecoderFrameMemoryLength), |
| 28 | _lastReceivedPictureID(0) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 30 | VCMDecodedFrameCallback::~VCMDecodedFrameCallback() { |
| 31 | delete _critSect; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 34 | void VCMDecodedFrameCallback::SetUserReceiveCallback( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 35 | VCMReceiveCallback* receiveCallback) { |
| 36 | CriticalSectionScoped cs(_critSect); |
| 37 | _receiveCallback = receiveCallback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 40 | VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() { |
| 41 | CriticalSectionScoped cs(_critSect); |
| 42 | return _receiveCallback; |
wuchengli@chromium.org | 0d94c2f | 2013-08-12 14:20:49 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 45 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) { |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 46 | return Decoded(decodedImage, -1); |
| 47 | } |
| 48 | |
| 49 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
| 50 | int64_t decode_time_ms) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 51 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | CriticalSectionScoped cs(_critSect); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 59 | 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 | |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 77 | decodedImage.set_render_time_ms(frameInfo->renderTimeMs); |
| 78 | decodedImage.set_rotation(frameInfo->rotation); |
sakal | d227522 | 2016-10-03 08:54:39 -0700 | [diff] [blame] | 79 | // 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 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 86 | return WEBRTC_VIDEO_CODEC_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 89 | int32_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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | } |
| 97 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 98 | int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame( |
| 99 | const uint64_t pictureId) { |
| 100 | _lastReceivedPictureID = pictureId; |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const { |
| 105 | return _lastReceivedPictureID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 108 | void VCMDecodedFrameCallback::OnDecoderImplementationName( |
| 109 | const char* implementation_name) { |
| 110 | CriticalSectionScoped cs(_critSect); |
| 111 | if (_receiveCallback) |
| 112 | _receiveCallback->OnDecoderImplementationName(implementation_name); |
| 113 | } |
| 114 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 115 | void VCMDecodedFrameCallback::Map(uint32_t timestamp, |
| 116 | VCMFrameInformation* frameInfo) { |
| 117 | CriticalSectionScoped cs(_critSect); |
| 118 | _timestampMap.Add(timestamp, frameInfo); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | } |
| 120 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 121 | int32_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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 129 | VCMGenericDecoder::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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 138 | VCMGenericDecoder::~VCMGenericDecoder() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 140 | int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 141 | int32_t numberOfCores) { |
| 142 | TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); |
| 143 | _codecType = settings->codecType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 145 | return _decoder->InitDecode(settings, numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | } |
| 147 | |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 148 | int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { |
| 149 | TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
| 150 | frame.EncodedImage()._timeStamp); |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 151 | _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 153 | _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]); |
| 155 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; |
nisse | 6f112cc | 2016-09-30 03:43:00 -0700 | [diff] [blame] | 157 | const RTPFragmentationHeader dummy_header; |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 158 | int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(), |
nisse | 6f112cc | 2016-09-30 03:43:00 -0700 | [diff] [blame] | 159 | &dummy_header, |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 160 | frame.CodecSpecific(), frame.RenderTimeMs()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 162 | _callback->OnDecoderImplementationName(_decoder->ImplementationName()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 163 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 164 | LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
| 165 | << frame.TimeStamp() << ", error code: " << ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | _callback->Pop(frame.TimeStamp()); |
| 167 | return ret; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 168 | } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || |
| 169 | ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) { |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 170 | // No output |
| 171 | _callback->Pop(frame.TimeStamp()); |
| 172 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 176 | int32_t VCMGenericDecoder::Release() { |
| 177 | return _decoder->Release(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 180 | int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
| 181 | VCMDecodedFrameCallback* callback) { |
| 182 | _callback = callback; |
| 183 | return _decoder->RegisterDecodeCompleteCallback(callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 186 | bool VCMGenericDecoder::External() const { |
| 187 | return _isExternal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | } |
| 189 | |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 190 | bool VCMGenericDecoder::PrefersLateDecoding() const { |
| 191 | return _decoder->PrefersLateDecoding(); |
| 192 | } |
| 193 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 194 | } // namespace webrtc |