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) |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 23 | : _clock(clock), |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 24 | _timing(timing), |
| 25 | _timestampMap(kDecoderFrameMemoryLength), |
| 26 | _lastReceivedPictureID(0) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 28 | VCMDecodedFrameCallback::~VCMDecodedFrameCallback() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 31 | void VCMDecodedFrameCallback::SetUserReceiveCallback( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 32 | VCMReceiveCallback* receiveCallback) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 33 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
| 34 | RTC_DCHECK((!_receiveCallback && receiveCallback) || |
| 35 | (_receiveCallback && !receiveCallback)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 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() { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 40 | // Called on the decode thread via VCMCodecDataBase::GetDecoder. |
| 41 | // The callback must always have been set before this happens. |
| 42 | RTC_DCHECK(_receiveCallback); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 43 | return _receiveCallback; |
wuchengli@chromium.org | 0d94c2f | 2013-08-12 14:20:49 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 46 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) { |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 47 | return Decoded(decodedImage, -1); |
| 48 | } |
| 49 | |
| 50 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
| 51 | int64_t decode_time_ms) { |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 52 | Decoded(decodedImage, |
| 53 | decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms) |
| 54 | : rtc::Optional<int32_t>(), |
| 55 | rtc::Optional<uint8_t>()); |
| 56 | return WEBRTC_VIDEO_CODEC_OK; |
| 57 | } |
| 58 | |
| 59 | void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
| 60 | rtc::Optional<int32_t> decode_time_ms, |
| 61 | rtc::Optional<uint8_t> qp) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 62 | RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point"; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 63 | TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded", |
| 64 | "timestamp", decodedImage.timestamp()); |
| 65 | // TODO(holmer): We should improve this so that we can handle multiple |
| 66 | // callbacks from one call to Decode(). |
| 67 | VCMFrameInformation* frameInfo; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 68 | { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 69 | rtc::CritScope cs(&lock_); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 70 | frameInfo = _timestampMap.Pop(decodedImage.timestamp()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (frameInfo == NULL) { |
| 74 | LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " |
| 75 | "this one."; |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 76 | return; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | const int64_t now_ms = _clock->TimeInMilliseconds(); |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 80 | if (!decode_time_ms) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 81 | decode_time_ms = |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 82 | rtc::Optional<int32_t>(now_ms - frameInfo->decodeStartTimeMs); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 83 | } |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 84 | _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 85 | frameInfo->renderTimeMs); |
| 86 | |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 87 | decodedImage.set_timestamp_us( |
| 88 | frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec); |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 89 | decodedImage.set_rotation(frameInfo->rotation); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 90 | _receiveCallback->FrameToRender(decodedImage, qp); |
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::ReceivedDecodedReferenceFrame( |
| 94 | const uint64_t pictureId) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 95 | return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId); |
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) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 110 | _receiveCallback->OnDecoderImplementationName(implementation_name); |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 111 | } |
| 112 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 113 | void VCMDecodedFrameCallback::Map(uint32_t timestamp, |
| 114 | VCMFrameInformation* frameInfo) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 115 | rtc::CritScope cs(&lock_); |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 116 | _timestampMap.Add(timestamp, frameInfo); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 119 | int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 120 | rtc::CritScope cs(&lock_); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 121 | if (_timestampMap.Pop(timestamp) == NULL) { |
| 122 | return VCM_GENERAL_ERROR; |
| 123 | } |
| 124 | return VCM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 127 | VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal) |
| 128 | : _callback(NULL), |
| 129 | _frameInfos(), |
| 130 | _nextFrameInfoIdx(0), |
| 131 | _decoder(decoder), |
| 132 | _codecType(kVideoCodecUnknown), |
| 133 | _isExternal(isExternal), |
| 134 | _keyFrameDecoded(false) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 136 | VCMGenericDecoder::~VCMGenericDecoder() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 138 | int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 139 | int32_t numberOfCores) { |
| 140 | TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); |
| 141 | _codecType = settings->codecType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 143 | return _decoder->InitDecode(settings, numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | } |
| 145 | |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 146 | int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { |
| 147 | TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
| 148 | frame.EncodedImage()._timeStamp); |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 149 | _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 151 | _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]); |
| 153 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; |
nisse | 6f112cc | 2016-09-30 03:43:00 -0700 | [diff] [blame] | 155 | const RTPFragmentationHeader dummy_header; |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 156 | int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(), |
nisse | 6f112cc | 2016-09-30 03:43:00 -0700 | [diff] [blame] | 157 | &dummy_header, |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 158 | frame.CodecSpecific(), frame.RenderTimeMs()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 160 | _callback->OnDecoderImplementationName(_decoder->ImplementationName()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 161 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 162 | LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
| 163 | << frame.TimeStamp() << ", error code: " << ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | _callback->Pop(frame.TimeStamp()); |
| 165 | return ret; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 166 | } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || |
| 167 | ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) { |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 168 | // No output |
| 169 | _callback->Pop(frame.TimeStamp()); |
| 170 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | return ret; |
| 172 | } |
| 173 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 174 | int32_t VCMGenericDecoder::Release() { |
| 175 | return _decoder->Release(); |
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 |