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 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/generic_decoder.h" |
| 12 | |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
pbos | 854e84c | 2015-11-16 16:39:06 -0800 | [diff] [blame] | 14 | #include "webrtc/base/logging.h" |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 15 | #include "webrtc/base/trace_event.h" |
Peter Boström | a443ec1 | 2015-11-30 19:14:50 +0100 | [diff] [blame] | 16 | #include "webrtc/modules/video_coding/include/video_coding.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 17 | #include "webrtc/modules/video_coding/internal_defines.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 22 | VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 23 | Clock* clock) |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 24 | : _clock(clock), |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 25 | _timing(timing), |
| 26 | _timestampMap(kDecoderFrameMemoryLength), |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 27 | _lastReceivedPictureID(0) { |
| 28 | decoder_thread_.DetachFromThread(); |
| 29 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 31 | VCMDecodedFrameCallback::~VCMDecodedFrameCallback() { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 32 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 35 | void VCMDecodedFrameCallback::SetUserReceiveCallback( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 36 | VCMReceiveCallback* receiveCallback) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 37 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
| 38 | RTC_DCHECK((!_receiveCallback && receiveCallback) || |
| 39 | (_receiveCallback && !receiveCallback)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 40 | _receiveCallback = receiveCallback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 43 | VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 44 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 45 | // Called on the decode thread via VCMCodecDataBase::GetDecoder. |
| 46 | // The callback must always have been set before this happens. |
| 47 | RTC_DCHECK(_receiveCallback); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 48 | return _receiveCallback; |
wuchengli@chromium.org | 0d94c2f | 2013-08-12 14:20:49 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 51 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) { |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 52 | return Decoded(decodedImage, -1); |
| 53 | } |
| 54 | |
| 55 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
| 56 | int64_t decode_time_ms) { |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 57 | Decoded(decodedImage, |
| 58 | decode_time_ms >= 0 ? rtc::Optional<int32_t>(decode_time_ms) |
| 59 | : rtc::Optional<int32_t>(), |
| 60 | rtc::Optional<uint8_t>()); |
| 61 | return WEBRTC_VIDEO_CODEC_OK; |
| 62 | } |
| 63 | |
| 64 | void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
| 65 | rtc::Optional<int32_t> decode_time_ms, |
| 66 | rtc::Optional<uint8_t> qp) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 67 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 68 | RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point"; |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 69 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 70 | TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded", |
| 71 | "timestamp", decodedImage.timestamp()); |
| 72 | // TODO(holmer): We should improve this so that we can handle multiple |
| 73 | // callbacks from one call to Decode(). |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 74 | VCMFrameInformation* frameInfo = _timestampMap.Pop(decodedImage.timestamp()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 75 | |
| 76 | if (frameInfo == NULL) { |
| 77 | LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " |
| 78 | "this one."; |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 79 | return; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | const int64_t now_ms = _clock->TimeInMilliseconds(); |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 83 | if (!decode_time_ms) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 84 | decode_time_ms = |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 85 | rtc::Optional<int32_t>(now_ms - frameInfo->decodeStartTimeMs); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 86 | } |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 87 | _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 88 | frameInfo->renderTimeMs); |
| 89 | |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 90 | decodedImage.set_timestamp_us( |
| 91 | frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec); |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 92 | decodedImage.set_rotation(frameInfo->rotation); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 93 | _receiveCallback->FrameToRender(decodedImage, qp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | } |
| 95 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 96 | int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame( |
| 97 | const uint64_t pictureId) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 98 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 99 | return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | } |
| 101 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 102 | int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame( |
| 103 | const uint64_t pictureId) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 104 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 105 | _lastReceivedPictureID = pictureId; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 110 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 111 | return _lastReceivedPictureID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 114 | void VCMDecodedFrameCallback::OnDecoderImplementationName( |
| 115 | const char* implementation_name) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 116 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 117 | _receiveCallback->OnDecoderImplementationName(implementation_name); |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 118 | } |
| 119 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 120 | void VCMDecodedFrameCallback::Map(uint32_t timestamp, |
| 121 | VCMFrameInformation* frameInfo) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 122 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 123 | _timestampMap.Add(timestamp, frameInfo); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | } |
| 125 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 126 | int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 127 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
| 128 | return _timestampMap.Pop(timestamp) == nullptr ? VCM_GENERAL_ERROR : VCM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 131 | VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal) |
| 132 | : _callback(NULL), |
| 133 | _frameInfos(), |
| 134 | _nextFrameInfoIdx(0), |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 135 | decoder_(decoder), |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 136 | _codecType(kVideoCodecUnknown), |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 137 | _isExternal(isExternal) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 139 | VCMGenericDecoder::~VCMGenericDecoder() { |
| 140 | decoder_->Release(); |
| 141 | if (_isExternal) |
| 142 | decoder_.release(); |
| 143 | |
| 144 | RTC_DCHECK(_isExternal || !decoder_); |
| 145 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 147 | int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 148 | int32_t numberOfCores) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 149 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 150 | TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); |
| 151 | _codecType = settings->codecType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 153 | return decoder_->InitDecode(settings, numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | } |
| 155 | |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 156 | int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 157 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
| 158 | TRACE_EVENT2("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
| 159 | frame.EncodedImage()._timeStamp, "decoder", |
| 160 | decoder_->ImplementationName()); |
| 161 | _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; |
| 162 | _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); |
| 163 | _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); |
| 164 | _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 166 | _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; |
| 167 | const RTPFragmentationHeader dummy_header; |
| 168 | int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(), |
| 169 | &dummy_header, frame.CodecSpecific(), |
| 170 | frame.RenderTimeMs()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 172 | // TODO(tommi): Necessary every time? |
| 173 | // Maybe this should be the first thing the function does, and only the first |
| 174 | // time around? |
| 175 | _callback->OnDecoderImplementationName(decoder_->ImplementationName()); |
| 176 | |
| 177 | if (ret != WEBRTC_VIDEO_CODEC_OK) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 178 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 179 | LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
| 180 | << frame.TimeStamp() << ", error code: " << ret; |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 181 | } |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 182 | // We pop the frame for all non-'OK', failure or success codes such as |
| 183 | // WEBRTC_VIDEO_CODEC_NO_OUTPUT and WEBRTC_VIDEO_CODEC_REQUEST_SLI. |
| 184 | _callback->Pop(frame.TimeStamp()); |
| 185 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 187 | return ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 190 | int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
| 191 | VCMDecodedFrameCallback* callback) { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 192 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 193 | _callback = callback; |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 194 | return decoder_->RegisterDecodeCompleteCallback(callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | } |
| 196 | |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 197 | bool VCMGenericDecoder::PrefersLateDecoding() const { |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 198 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
| 199 | return decoder_->PrefersLateDecoding(); |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 200 | } |
| 201 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame] | 202 | #if defined(WEBRTC_ANDROID) |
| 203 | void VCMGenericDecoder::PollDecodedFrames() { |
| 204 | RTC_DCHECK_RUN_ON(&decoder_thread_); |
| 205 | decoder_->PollDecodedFrames(); |
| 206 | } |
| 207 | #endif |
| 208 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 209 | } // namespace webrtc |