niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
mikhal@webrtc.org | a2031d5 | 2012-07-31 15:53:44 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/video_coding/generic_decoder.h" |
tommi | 5b7fc8c | 2017-07-05 23:45:57 | [diff] [blame] | 12 | |
ilnik | 2edc684 | 2017-07-06 10:06:50 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "modules/video_coding/include/video_coding.h" |
| 16 | #include "modules/video_coding/internal_defines.h" |
| 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
| 19 | #include "rtc_base/timeutils.h" |
| 20 | #include "rtc_base/trace_event.h" |
| 21 | #include "system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 25 | VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 | [diff] [blame] | 26 | Clock* clock) |
tommi | d0a71ba | 2017-03-14 11:16:20 | [diff] [blame] | 27 | : _clock(clock), |
Peter Boström | b7d9a97 | 2015-12-18 15:01:11 | [diff] [blame] | 28 | _timing(timing), |
| 29 | _timestampMap(kDecoderFrameMemoryLength), |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 30 | _lastReceivedPictureID(0) { |
| 31 | ntp_offset_ = |
| 32 | _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds(); |
| 33 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 34 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 35 | VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 36 | |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 | [diff] [blame] | 37 | void VCMDecodedFrameCallback::SetUserReceiveCallback( |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 38 | VCMReceiveCallback* receiveCallback) { |
tommi | d0a71ba | 2017-03-14 11:16:20 | [diff] [blame] | 39 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
| 40 | RTC_DCHECK((!_receiveCallback && receiveCallback) || |
| 41 | (_receiveCallback && !receiveCallback)); |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 42 | _receiveCallback = receiveCallback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 43 | } |
| 44 | |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 45 | VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() { |
tommi | d0a71ba | 2017-03-14 11:16:20 | [diff] [blame] | 46 | // Called on the decode thread via VCMCodecDataBase::GetDecoder. |
| 47 | // The callback must always have been set before this happens. |
| 48 | RTC_DCHECK(_receiveCallback); |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 49 | return _receiveCallback; |
wuchengli@chromium.org | 0d94c2f | 2013-08-12 14:20:49 | [diff] [blame] | 50 | } |
| 51 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-30 00:21:40 | [diff] [blame] | 52 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) { |
Per | 327d8ba | 2015-11-10 13:00:27 | [diff] [blame] | 53 | return Decoded(decodedImage, -1); |
| 54 | } |
| 55 | |
| 56 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
| 57 | int64_t decode_time_ms) { |
sakal | cc452e1 | 2017-02-09 12:53:45 | [diff] [blame] | 58 | Decoded(decodedImage, |
Danil Chapovalov | 0040b66 | 2018-06-18 08:48:16 | [diff] [blame] | 59 | decode_time_ms >= 0 ? absl::optional<int32_t>(decode_time_ms) |
| 60 | : absl::nullopt, |
| 61 | absl::nullopt); |
sakal | cc452e1 | 2017-02-09 12:53:45 | [diff] [blame] | 62 | return WEBRTC_VIDEO_CODEC_OK; |
| 63 | } |
| 64 | |
| 65 | void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
Danil Chapovalov | 0040b66 | 2018-06-18 08:48:16 | [diff] [blame] | 66 | absl::optional<int32_t> decode_time_ms, |
| 67 | absl::optional<uint8_t> qp) { |
tommi | d0a71ba | 2017-03-14 11:16:20 | [diff] [blame] | 68 | RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point"; |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 69 | TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded", |
| 70 | "timestamp", decodedImage.timestamp()); |
| 71 | // TODO(holmer): We should improve this so that we can handle multiple |
| 72 | // callbacks from one call to Decode(). |
Lu Liu | 352314a | 2018-02-21 19:38:59 | [diff] [blame] | 73 | VCMFrameInformation* frameInfo; |
| 74 | { |
| 75 | rtc::CritScope cs(&lock_); |
| 76 | frameInfo = _timestampMap.Pop(decodedImage.timestamp()); |
| 77 | } |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 78 | |
| 79 | if (frameInfo == NULL) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 80 | RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " |
| 81 | "this one."; |
sakal | cc452e1 | 2017-02-09 12:53:45 | [diff] [blame] | 82 | return; |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | const int64_t now_ms = _clock->TimeInMilliseconds(); |
sakal | cc452e1 | 2017-02-09 12:53:45 | [diff] [blame] | 86 | if (!decode_time_ms) { |
Oskar Sundbom | 6bd3902 | 2017-11-16 09:54:49 | [diff] [blame] | 87 | decode_time_ms = now_ms - frameInfo->decodeStartTimeMs; |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 88 | } |
sakal | cc452e1 | 2017-02-09 12:53:45 | [diff] [blame] | 89 | _timing->StopDecodeTimer(decodedImage.timestamp(), *decode_time_ms, now_ms, |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 90 | frameInfo->renderTimeMs); |
| 91 | |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 92 | // Report timing information. |
Ilya Nikolaevskiy | b6c462d | 2018-06-05 13:21:32 | [diff] [blame] | 93 | if (frameInfo->timing.flags != VideoSendTiming::kInvalid) { |
ilnik | 2edc684 | 2017-07-06 10:06:50 | [diff] [blame] | 94 | int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_; |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 95 | // Convert remote timestamps to local time from ntp timestamps. |
| 96 | frameInfo->timing.encode_start_ms -= ntp_offset_; |
| 97 | frameInfo->timing.encode_finish_ms -= ntp_offset_; |
| 98 | frameInfo->timing.packetization_finish_ms -= ntp_offset_; |
| 99 | frameInfo->timing.pacer_exit_ms -= ntp_offset_; |
| 100 | frameInfo->timing.network_timestamp_ms -= ntp_offset_; |
| 101 | frameInfo->timing.network2_timestamp_ms -= ntp_offset_; |
ilnik | 2edc684 | 2017-07-06 10:06:50 | [diff] [blame] | 102 | |
| 103 | int64_t sender_delta_ms = 0; |
| 104 | if (decodedImage.ntp_time_ms() < 0) { |
| 105 | // Sender clock is not estimated yet. Make sure that sender times are all |
| 106 | // negative to indicate that. Yet they still should be relatively correct. |
| 107 | sender_delta_ms = |
| 108 | std::max({capture_time_ms, frameInfo->timing.encode_start_ms, |
| 109 | frameInfo->timing.encode_finish_ms, |
| 110 | frameInfo->timing.packetization_finish_ms, |
| 111 | frameInfo->timing.pacer_exit_ms, |
| 112 | frameInfo->timing.network_timestamp_ms, |
| 113 | frameInfo->timing.network2_timestamp_ms}) + |
| 114 | 1; |
| 115 | } |
| 116 | |
| 117 | TimingFrameInfo timing_frame_info; |
| 118 | |
| 119 | timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms; |
| 120 | timing_frame_info.encode_start_ms = |
| 121 | frameInfo->timing.encode_start_ms - sender_delta_ms; |
| 122 | timing_frame_info.encode_finish_ms = |
| 123 | frameInfo->timing.encode_finish_ms - sender_delta_ms; |
| 124 | timing_frame_info.packetization_finish_ms = |
| 125 | frameInfo->timing.packetization_finish_ms - sender_delta_ms; |
| 126 | timing_frame_info.pacer_exit_ms = |
| 127 | frameInfo->timing.pacer_exit_ms - sender_delta_ms; |
| 128 | timing_frame_info.network_timestamp_ms = |
| 129 | frameInfo->timing.network_timestamp_ms - sender_delta_ms; |
| 130 | timing_frame_info.network2_timestamp_ms = |
| 131 | frameInfo->timing.network2_timestamp_ms - sender_delta_ms; |
| 132 | timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms; |
| 133 | timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms; |
| 134 | timing_frame_info.decode_start_ms = frameInfo->decodeStartTimeMs; |
| 135 | timing_frame_info.decode_finish_ms = now_ms; |
| 136 | timing_frame_info.render_time_ms = frameInfo->renderTimeMs; |
| 137 | timing_frame_info.rtp_timestamp = decodedImage.timestamp(); |
sprang | ba050a6 | 2017-08-18 09:51:12 | [diff] [blame] | 138 | timing_frame_info.flags = frameInfo->timing.flags; |
ilnik | 2edc684 | 2017-07-06 10:06:50 | [diff] [blame] | 139 | |
| 140 | _timing->SetTimingFrameInfo(timing_frame_info); |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 141 | } |
| 142 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 143 | decodedImage.set_timestamp_us(frameInfo->renderTimeMs * |
| 144 | rtc::kNumMicrosecsPerMillisec); |
sakal | 55d932b | 2016-09-30 13:19:08 | [diff] [blame] | 145 | decodedImage.set_rotation(frameInfo->rotation); |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 146 | _receiveCallback->FrameToRender(decodedImage, qp, frameInfo->content_type); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 147 | } |
| 148 | |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 149 | int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame( |
| 150 | const uint64_t pictureId) { |
tommi | d0a71ba | 2017-03-14 11:16:20 | [diff] [blame] | 151 | return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 152 | } |
| 153 | |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 154 | int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame( |
| 155 | const uint64_t pictureId) { |
| 156 | _lastReceivedPictureID = pictureId; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const { |
| 161 | return _lastReceivedPictureID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 162 | } |
| 163 | |
Peter Boström | b7d9a97 | 2015-12-18 15:01:11 | [diff] [blame] | 164 | void VCMDecodedFrameCallback::OnDecoderImplementationName( |
| 165 | const char* implementation_name) { |
tommi | d0a71ba | 2017-03-14 11:16:20 | [diff] [blame] | 166 | _receiveCallback->OnDecoderImplementationName(implementation_name); |
Peter Boström | b7d9a97 | 2015-12-18 15:01:11 | [diff] [blame] | 167 | } |
| 168 | |
pbos | 1968d3f | 2015-09-28 15:52:18 | [diff] [blame] | 169 | void VCMDecodedFrameCallback::Map(uint32_t timestamp, |
| 170 | VCMFrameInformation* frameInfo) { |
Lu Liu | 352314a | 2018-02-21 19:38:59 | [diff] [blame] | 171 | rtc::CritScope cs(&lock_); |
pbos | 1968d3f | 2015-09-28 15:52:18 | [diff] [blame] | 172 | _timestampMap.Add(timestamp, frameInfo); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 173 | } |
| 174 | |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 175 | int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) { |
Lu Liu | 352314a | 2018-02-21 19:38:59 | [diff] [blame] | 176 | rtc::CritScope cs(&lock_); |
| 177 | if (_timestampMap.Pop(timestamp) == NULL) { |
| 178 | return VCM_GENERAL_ERROR; |
| 179 | } |
| 180 | return VCM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 181 | } |
| 182 | |
Magnus Jedvert | 46a2765 | 2017-11-13 13:10:02 | [diff] [blame] | 183 | VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder) |
| 184 | : VCMGenericDecoder(decoder.release(), false /* isExternal */) {} |
| 185 | |
Peter Boström | 187db63 | 2015-12-01 16:20:01 | [diff] [blame] | 186 | VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal) |
| 187 | : _callback(NULL), |
| 188 | _frameInfos(), |
| 189 | _nextFrameInfoIdx(0), |
tommi | 5b7fc8c | 2017-07-05 23:45:57 | [diff] [blame] | 190 | decoder_(decoder), |
Kári Tristan Helgason | 84ccb2d | 2018-08-16 12:35:26 | [diff] [blame] | 191 | _codecType(kVideoCodecGeneric), |
guidou | c337258 | 2017-04-04 14:16:21 | [diff] [blame] | 192 | _isExternal(isExternal), |
tommi | 5b7fc8c | 2017-07-05 23:45:57 | [diff] [blame] | 193 | _last_keyframe_content_type(VideoContentType::UNSPECIFIED) { |
| 194 | RTC_DCHECK(decoder_); |
| 195 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 196 | |
tommi | 5b7fc8c | 2017-07-05 23:45:57 | [diff] [blame] | 197 | VCMGenericDecoder::~VCMGenericDecoder() { |
| 198 | decoder_->Release(); |
| 199 | if (_isExternal) |
| 200 | decoder_.release(); |
tommi | 058aa71 | 2017-07-15 18:33:35 | [diff] [blame] | 201 | RTC_DCHECK(_isExternal || decoder_); |
tommi | 5b7fc8c | 2017-07-05 23:45:57 | [diff] [blame] | 202 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 203 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 | [diff] [blame] | 204 | int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 205 | int32_t numberOfCores) { |
| 206 | TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); |
| 207 | _codecType = settings->codecType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 208 | |
tommi | 5b7fc8c | 2017-07-05 23:45:57 | [diff] [blame] | 209 | return decoder_->InitDecode(settings, numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 210 | } |
| 211 | |
pbos | d9eec76 | 2015-11-17 14:03:43 | [diff] [blame] | 212 | int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 213 | TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
Niels Möller | 2377588 | 2018-08-16 08:24:12 | [diff] [blame] | 214 | frame.Timestamp()); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 215 | _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; |
| 216 | _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); |
| 217 | _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); |
| 218 | _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing(); |
| 219 | // Set correctly only for key frames. Thus, use latest key frame |
| 220 | // content type. If the corresponding key frame was lost, decode will fail |
| 221 | // and content type will be ignored. |
| 222 | if (frame.FrameType() == kVideoFrameKey) { |
| 223 | _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType(); |
| 224 | _last_keyframe_content_type = frame.contentType(); |
| 225 | } else { |
| 226 | _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type; |
| 227 | } |
Niels Möller | 2377588 | 2018-08-16 08:24:12 | [diff] [blame] | 228 | _callback->Map(frame.Timestamp(), &_frameInfos[_nextFrameInfoIdx]); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 229 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 230 | _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; |
| 231 | int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(), |
| 232 | frame.CodecSpecific(), frame.RenderTimeMs()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 233 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 234 | _callback->OnDecoderImplementationName(decoder_->ImplementationName()); |
| 235 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
| 236 | RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
Niels Möller | 2377588 | 2018-08-16 08:24:12 | [diff] [blame] | 237 | << frame.Timestamp() << ", error code: " << ret; |
| 238 | _callback->Pop(frame.Timestamp()); |
Lu Liu | 352314a | 2018-02-21 19:38:59 | [diff] [blame] | 239 | return ret; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 240 | } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || |
| 241 | ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) { |
| 242 | // No output |
Niels Möller | 2377588 | 2018-08-16 08:24:12 | [diff] [blame] | 243 | _callback->Pop(frame.Timestamp()); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 244 | } |
| 245 | return ret; |
guidou | c337258 | 2017-04-04 14:16:21 | [diff] [blame] | 246 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 247 | |
Peter Boström | 187db63 | 2015-12-01 16:20:01 | [diff] [blame] | 248 | int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
| 249 | VCMDecodedFrameCallback* callback) { |
| 250 | _callback = callback; |
tommi | 5b7fc8c | 2017-07-05 23:45:57 | [diff] [blame] | 251 | return decoder_->RegisterDecodeCompleteCallback(callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 252 | } |
| 253 | |
perkj | 796cfaf | 2015-12-10 17:27:38 | [diff] [blame] | 254 | bool VCMGenericDecoder::PrefersLateDecoding() const { |
tommi | 5b7fc8c | 2017-07-05 23:45:57 | [diff] [blame] | 255 | return decoder_->PrefersLateDecoding(); |
perkj | 796cfaf | 2015-12-10 17:27:38 | [diff] [blame] | 256 | } |
| 257 | |
philipel | 9d3ab61 | 2015-12-21 12:12:39 | [diff] [blame] | 258 | } // namespace webrtc |