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 | |
pbos | 854e84c | 2015-11-16 16:39:06 -0800 | [diff] [blame] | 11 | #include "webrtc/base/logging.h" |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 12 | #include "webrtc/base/trace_event.h" |
Peter Boström | a443ec1 | 2015-11-30 19:14:50 +0100 | [diff] [blame] | 13 | #include "webrtc/modules/video_coding/include/video_coding.h" |
kjellander | ec192bd | 2015-11-30 23:14:33 -0800 | [diff] [blame] | 14 | #include "webrtc/modules/video_coding/generic_decoder.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 15 | #include "webrtc/modules/video_coding/internal_defines.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 20 | VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 21 | Clock* clock) |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 22 | : _critSect(CriticalSectionWrapper::CreateCriticalSection()), |
| 23 | _clock(clock), |
| 24 | _receiveCallback(NULL), |
| 25 | _timing(timing), |
| 26 | _timestampMap(kDecoderFrameMemoryLength), |
| 27 | _lastReceivedPictureID(0) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 29 | VCMDecodedFrameCallback::~VCMDecodedFrameCallback() { |
| 30 | delete _critSect; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | } |
| 32 | |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 33 | void VCMDecodedFrameCallback::SetUserReceiveCallback( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 34 | VCMReceiveCallback* receiveCallback) { |
| 35 | CriticalSectionScoped cs(_critSect); |
| 36 | _receiveCallback = receiveCallback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 39 | VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() { |
| 40 | CriticalSectionScoped cs(_critSect); |
| 41 | return _receiveCallback; |
wuchengli@chromium.org | 0d94c2f | 2013-08-12 14:20:49 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 44 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) { |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 45 | return Decoded(decodedImage, -1); |
| 46 | } |
| 47 | |
| 48 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
| 49 | int64_t decode_time_ms) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 50 | TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded", |
| 51 | "timestamp", decodedImage.timestamp()); |
| 52 | // TODO(holmer): We should improve this so that we can handle multiple |
| 53 | // callbacks from one call to Decode(). |
| 54 | VCMFrameInformation* frameInfo; |
| 55 | VCMReceiveCallback* callback; |
| 56 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | CriticalSectionScoped cs(_critSect); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 58 | frameInfo = _timestampMap.Pop(decodedImage.timestamp()); |
| 59 | callback = _receiveCallback; |
| 60 | } |
| 61 | |
| 62 | if (frameInfo == NULL) { |
| 63 | LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " |
| 64 | "this one."; |
| 65 | return WEBRTC_VIDEO_CODEC_OK; |
| 66 | } |
| 67 | |
| 68 | const int64_t now_ms = _clock->TimeInMilliseconds(); |
| 69 | if (decode_time_ms < 0) { |
| 70 | decode_time_ms = |
| 71 | static_cast<int32_t>(now_ms - frameInfo->decodeStartTimeMs); |
| 72 | } |
| 73 | _timing->StopDecodeTimer(decodedImage.timestamp(), decode_time_ms, now_ms, |
| 74 | frameInfo->renderTimeMs); |
| 75 | |
| 76 | if (callback != NULL) { |
| 77 | decodedImage.set_render_time_ms(frameInfo->renderTimeMs); |
| 78 | decodedImage.set_rotation(frameInfo->rotation); |
| 79 | callback->FrameToRender(decodedImage); |
| 80 | } |
| 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; |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 152 | int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(), |
| 153 | frame.FragmentationHeader(), |
| 154 | frame.CodecSpecific(), frame.RenderTimeMs()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 156 | _callback->OnDecoderImplementationName(_decoder->ImplementationName()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 157 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 158 | LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
| 159 | << frame.TimeStamp() << ", error code: " << ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | _callback->Pop(frame.TimeStamp()); |
| 161 | return ret; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 162 | } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || |
| 163 | ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) { |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 164 | // No output |
| 165 | _callback->Pop(frame.TimeStamp()); |
| 166 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 170 | int32_t VCMGenericDecoder::Release() { |
| 171 | return _decoder->Release(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 174 | int32_t VCMGenericDecoder::Reset() { |
| 175 | return _decoder->Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 178 | int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
| 179 | VCMDecodedFrameCallback* callback) { |
| 180 | _callback = callback; |
| 181 | return _decoder->RegisterDecodeCompleteCallback(callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 184 | bool VCMGenericDecoder::External() const { |
| 185 | return _isExternal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | } |
| 187 | |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 188 | bool VCMGenericDecoder::PrefersLateDecoding() const { |
| 189 | return _decoder->PrefersLateDecoding(); |
| 190 | } |
| 191 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame^] | 192 | } // namespace webrtc |