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" |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [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), |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 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(). |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 67 | VCMFrameInformation* frameInfo; |
| 68 | { |
| 69 | rtc::CritScope cs(&lock_); |
| 70 | frameInfo = _timestampMap.Pop(decodedImage.timestamp()); |
| 71 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 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); |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame^] | 90 | _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type); |
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) { |
guidou | c337258 | 2017-04-04 07:16:21 -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) { |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 120 | rtc::CritScope cs(&lock_); |
| 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), |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 131 | _decoder(decoder), |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 132 | _codecType(kVideoCodecUnknown), |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 133 | _isExternal(isExternal), |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame^] | 134 | _keyFrameDecoded(false), |
| 135 | _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 137 | VCMGenericDecoder::~VCMGenericDecoder() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 139 | int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 140 | int32_t numberOfCores) { |
| 141 | TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); |
| 142 | _codecType = settings->codecType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 144 | return _decoder->InitDecode(settings, numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | } |
| 146 | |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 147 | int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 148 | TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
| 149 | frame.EncodedImage()._timeStamp); |
| 150 | _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; |
| 151 | _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); |
| 152 | _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame^] | 153 | // Set correctly only for key frames. Thus, use latest key frame |
| 154 | // content type. If the corresponding key frame was lost, decode will fail |
| 155 | // and content type will be ignored. |
| 156 | if (frame.FrameType() == kVideoFrameKey) { |
| 157 | _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType(); |
| 158 | _last_keyframe_content_type = frame.contentType(); |
| 159 | } else { |
| 160 | _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type; |
| 161 | } |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 162 | _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 164 | _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; |
| 165 | const RTPFragmentationHeader dummy_header; |
| 166 | int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(), |
| 167 | &dummy_header, |
| 168 | frame.CodecSpecific(), frame.RenderTimeMs()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 170 | _callback->OnDecoderImplementationName(_decoder->ImplementationName()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 171 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 172 | LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
| 173 | << frame.TimeStamp() << ", error code: " << ret; |
| 174 | _callback->Pop(frame.TimeStamp()); |
| 175 | return ret; |
| 176 | } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || |
| 177 | ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) { |
| 178 | // No output |
| 179 | _callback->Pop(frame.TimeStamp()); |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 180 | } |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 181 | return ret; |
| 182 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 184 | int32_t VCMGenericDecoder::Release() { |
| 185 | return _decoder->Release(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 188 | int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
| 189 | VCMDecodedFrameCallback* callback) { |
| 190 | _callback = callback; |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 191 | return _decoder->RegisterDecodeCompleteCallback(callback); |
| 192 | } |
| 193 | |
| 194 | bool VCMGenericDecoder::External() const { |
| 195 | return _isExternal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | } |
| 197 | |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 198 | bool VCMGenericDecoder::PrefersLateDecoding() const { |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 199 | return _decoder->PrefersLateDecoding(); |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 200 | } |
| 201 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 202 | } // namespace webrtc |