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 | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 11 | #include "webrtc/modules/video_coding/generic_decoder.h" |
| 12 | |
Henrik Kjellander | dca1e09 | 2017-07-01 16:42:22 +0200 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
| 14 | #include "webrtc/base/logging.h" |
| 15 | #include "webrtc/base/timeutils.h" |
| 16 | #include "webrtc/base/trace_event.h" |
kjellander | c8fa692 | 2017-06-30 14:02:00 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/video_coding/include/video_coding.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/video_coding/internal_defines.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 23 | VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 24 | Clock* clock) |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 25 | : _clock(clock), |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 26 | _timing(timing), |
| 27 | _timestampMap(kDecoderFrameMemoryLength), |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 28 | _lastReceivedPictureID(0) { |
| 29 | ntp_offset_ = |
| 30 | _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds(); |
| 31 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 33 | VCMDecodedFrameCallback::~VCMDecodedFrameCallback() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 36 | void VCMDecodedFrameCallback::SetUserReceiveCallback( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 37 | VCMReceiveCallback* receiveCallback) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 38 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
| 39 | RTC_DCHECK((!_receiveCallback && receiveCallback) || |
| 40 | (_receiveCallback && !receiveCallback)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 41 | _receiveCallback = receiveCallback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 44 | VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() { |
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 | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 67 | RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point"; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 68 | TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded", |
| 69 | "timestamp", decodedImage.timestamp()); |
| 70 | // TODO(holmer): We should improve this so that we can handle multiple |
| 71 | // callbacks from one call to Decode(). |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 72 | VCMFrameInformation* frameInfo; |
| 73 | { |
| 74 | rtc::CritScope cs(&lock_); |
| 75 | frameInfo = _timestampMap.Pop(decodedImage.timestamp()); |
| 76 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 77 | |
| 78 | if (frameInfo == NULL) { |
| 79 | LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " |
| 80 | "this one."; |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 81 | return; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | const int64_t now_ms = _clock->TimeInMilliseconds(); |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 85 | if (!decode_time_ms) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 86 | decode_time_ms = |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 87 | rtc::Optional<int32_t>(now_ms - frameInfo->decodeStartTimeMs); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 88 | } |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 89 | _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 90 | frameInfo->renderTimeMs); |
| 91 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 92 | // Report timing information. |
| 93 | if (frameInfo->timing.is_timing_frame) { |
| 94 | // Convert remote timestamps to local time from ntp timestamps. |
| 95 | frameInfo->timing.encode_start_ms -= ntp_offset_; |
| 96 | frameInfo->timing.encode_finish_ms -= ntp_offset_; |
| 97 | frameInfo->timing.packetization_finish_ms -= ntp_offset_; |
| 98 | frameInfo->timing.pacer_exit_ms -= ntp_offset_; |
| 99 | frameInfo->timing.network_timestamp_ms -= ntp_offset_; |
| 100 | frameInfo->timing.network2_timestamp_ms -= ntp_offset_; |
| 101 | // TODO(ilnik): Report timing information here. |
| 102 | // Capture time: decodedImage.ntp_time_ms() - ntp_offset |
| 103 | // Encode start: frameInfo->timing.encode_start_ms |
| 104 | // Encode finish: frameInfo->timing.encode_finish_ms |
| 105 | // Packetization done: frameInfo->timing.packetization_finish_ms |
| 106 | // Pacer exit: frameInfo->timing.pacer_exit_ms |
| 107 | // Network timestamp: frameInfo->timing.network_timestamp_ms |
| 108 | // Network2 timestamp: frameInfo->timing.network2_timestamp_ms |
| 109 | // Receive start: frameInfo->timing.receive_start_ms |
| 110 | // Receive finish: frameInfo->timing.receive_finish_ms |
| 111 | // Decode start: frameInfo->decodeStartTimeMs |
| 112 | // Decode finish: now_ms |
| 113 | // Render time: frameInfo->renderTimeMs |
| 114 | } |
| 115 | |
nisse | 1c0dea8 | 2017-01-30 02:43:18 -0800 | [diff] [blame] | 116 | decodedImage.set_timestamp_us( |
| 117 | frameInfo->renderTimeMs * rtc::kNumMicrosecsPerMillisec); |
sakal | 55d932b | 2016-09-30 06:19:08 -0700 | [diff] [blame] | 118 | decodedImage.set_rotation(frameInfo->rotation); |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 119 | _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
| 121 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 122 | int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame( |
| 123 | const uint64_t pictureId) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 124 | return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 127 | int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame( |
| 128 | const uint64_t pictureId) { |
| 129 | _lastReceivedPictureID = pictureId; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const { |
| 134 | return _lastReceivedPictureID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 137 | void VCMDecodedFrameCallback::OnDecoderImplementationName( |
| 138 | const char* implementation_name) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 139 | _receiveCallback->OnDecoderImplementationName(implementation_name); |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 140 | } |
| 141 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 142 | void VCMDecodedFrameCallback::Map(uint32_t timestamp, |
| 143 | VCMFrameInformation* frameInfo) { |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 144 | rtc::CritScope cs(&lock_); |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 145 | _timestampMap.Add(timestamp, frameInfo); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | } |
| 147 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 148 | int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) { |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 149 | rtc::CritScope cs(&lock_); |
| 150 | if (_timestampMap.Pop(timestamp) == NULL) { |
| 151 | return VCM_GENERAL_ERROR; |
| 152 | } |
| 153 | return VCM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 156 | VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal) |
| 157 | : _callback(NULL), |
| 158 | _frameInfos(), |
| 159 | _nextFrameInfoIdx(0), |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 160 | decoder_(decoder), |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 161 | _codecType(kVideoCodecUnknown), |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 162 | _isExternal(isExternal), |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 163 | _last_keyframe_content_type(VideoContentType::UNSPECIFIED) { |
| 164 | RTC_DCHECK(decoder_); |
| 165 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 167 | VCMGenericDecoder::~VCMGenericDecoder() { |
| 168 | decoder_->Release(); |
| 169 | if (_isExternal) |
| 170 | decoder_.release(); |
| 171 | RTC_DCHECK(_isExternal || !decoder_); |
| 172 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 173 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 174 | int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 175 | int32_t numberOfCores) { |
| 176 | TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); |
| 177 | _codecType = settings->codecType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 179 | return decoder_->InitDecode(settings, numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | } |
| 181 | |
pbos | d9eec76 | 2015-11-17 06:03:43 -0800 | [diff] [blame] | 182 | int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 183 | TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
| 184 | frame.EncodedImage()._timeStamp); |
| 185 | _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; |
| 186 | _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); |
| 187 | _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 188 | _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing(); |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 189 | // Set correctly only for key frames. Thus, use latest key frame |
| 190 | // content type. If the corresponding key frame was lost, decode will fail |
| 191 | // and content type will be ignored. |
| 192 | if (frame.FrameType() == kVideoFrameKey) { |
| 193 | _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType(); |
| 194 | _last_keyframe_content_type = frame.contentType(); |
| 195 | } else { |
| 196 | _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type; |
| 197 | } |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 198 | _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 199 | |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 200 | _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; |
| 201 | const RTPFragmentationHeader dummy_header; |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 202 | int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(), |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 203 | &dummy_header, |
| 204 | frame.CodecSpecific(), frame.RenderTimeMs()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 206 | _callback->OnDecoderImplementationName(decoder_->ImplementationName()); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 207 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 208 | LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
| 209 | << frame.TimeStamp() << ", error code: " << ret; |
| 210 | _callback->Pop(frame.TimeStamp()); |
| 211 | return ret; |
| 212 | } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || |
| 213 | ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) { |
| 214 | // No output |
| 215 | _callback->Pop(frame.TimeStamp()); |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 216 | } |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 217 | return ret; |
| 218 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 220 | int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
| 221 | VCMDecodedFrameCallback* callback) { |
| 222 | _callback = callback; |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 223 | return decoder_->RegisterDecodeCompleteCallback(callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 226 | bool VCMGenericDecoder::PrefersLateDecoding() const { |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame^] | 227 | return decoder_->PrefersLateDecoding(); |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 228 | } |
| 229 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 230 | } // namespace webrtc |