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 | } |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame^] | 62 | RTC_DCHECK(callback != nullptr); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 63 | |
| 64 | if (frameInfo == NULL) { |
| 65 | LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " |
| 66 | "this one."; |
| 67 | return WEBRTC_VIDEO_CODEC_OK; |
| 68 | } |
| 69 | |
| 70 | const int64_t now_ms = _clock->TimeInMilliseconds(); |
| 71 | if (decode_time_ms < 0) { |
| 72 | decode_time_ms = |
| 73 | static_cast<int32_t>(now_ms - frameInfo->decodeStartTimeMs); |
| 74 | } |
| 75 | _timing->StopDecodeTimer(decodedImage.timestamp(), decode_time_ms, now_ms, |
| 76 | frameInfo->renderTimeMs); |
| 77 | |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame^] | 78 | decodedImage.set_render_time_ms(frameInfo->renderTimeMs); |
| 79 | decodedImage.set_rotation(frameInfo->rotation); |
| 80 | callback->FrameToRender(decodedImage); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 81 | return WEBRTC_VIDEO_CODEC_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 84 | int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame( |
| 85 | const uint64_t pictureId) { |
| 86 | CriticalSectionScoped cs(_critSect); |
| 87 | if (_receiveCallback != NULL) { |
| 88 | return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId); |
| 89 | } |
| 90 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | } |
| 92 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 93 | int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame( |
| 94 | const uint64_t pictureId) { |
| 95 | _lastReceivedPictureID = pictureId; |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const { |
| 100 | return _lastReceivedPictureID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 103 | void VCMDecodedFrameCallback::OnDecoderImplementationName( |
| 104 | const char* implementation_name) { |
| 105 | CriticalSectionScoped cs(_critSect); |
| 106 | if (_receiveCallback) |
| 107 | _receiveCallback->OnDecoderImplementationName(implementation_name); |
| 108 | } |
| 109 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 110 | void VCMDecodedFrameCallback::Map(uint32_t timestamp, |
| 111 | VCMFrameInformation* frameInfo) { |
| 112 | CriticalSectionScoped cs(_critSect); |
| 113 | _timestampMap.Add(timestamp, frameInfo); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 116 | int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) { |
| 117 | CriticalSectionScoped cs(_critSect); |
| 118 | if (_timestampMap.Pop(timestamp) == NULL) { |
| 119 | return VCM_GENERAL_ERROR; |
| 120 | } |
| 121 | return VCM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 124 | VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal) |
| 125 | : _callback(NULL), |
| 126 | _frameInfos(), |
| 127 | _nextFrameInfoIdx(0), |
| 128 | _decoder(decoder), |
| 129 | _codecType(kVideoCodecUnknown), |
| 130 | _isExternal(isExternal), |
| 131 | _keyFrameDecoded(false) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 133 | VCMGenericDecoder::~VCMGenericDecoder() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 135 | int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 136 | int32_t numberOfCores) { |
| 137 | TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); |
| 138 | _codecType = settings->codecType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 140 | return _decoder->InitDecode(settings, numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | } |
| 142 | |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 143 | int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { |
| 144 | TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
| 145 | frame.EncodedImage()._timeStamp); |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 146 | _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 148 | _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]); |
| 150 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; |
nisse | 6f112cc | 2016-09-30 03:43:00 -0700 | [diff] [blame] | 152 | const RTPFragmentationHeader dummy_header; |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 153 | int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(), |
nisse | 6f112cc | 2016-09-30 03:43:00 -0700 | [diff] [blame] | 154 | &dummy_header, |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 155 | frame.CodecSpecific(), frame.RenderTimeMs()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 157 | _callback->OnDecoderImplementationName(_decoder->ImplementationName()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 158 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 159 | LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
| 160 | << frame.TimeStamp() << ", error code: " << ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | _callback->Pop(frame.TimeStamp()); |
| 162 | return ret; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 163 | } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || |
| 164 | ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) { |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 165 | // No output |
| 166 | _callback->Pop(frame.TimeStamp()); |
| 167 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 171 | int32_t VCMGenericDecoder::Release() { |
| 172 | return _decoder->Release(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 175 | int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
| 176 | VCMDecodedFrameCallback* callback) { |
| 177 | _callback = callback; |
| 178 | return _decoder->RegisterDecodeCompleteCallback(callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 181 | bool VCMGenericDecoder::External() const { |
| 182 | return _isExternal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | } |
| 184 | |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 185 | bool VCMGenericDecoder::PrefersLateDecoding() const { |
| 186 | return _decoder->PrefersLateDecoding(); |
| 187 | } |
| 188 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 189 | } // namespace webrtc |