niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | ddab60b | 2012-04-23 14:52:15 +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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/encoded_frame.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
| 15 | #include "absl/types/variant.h" |
| 16 | #include "api/video/video_timing.h" |
| 17 | #include "modules/video_coding/codecs/interface/common_constants.h" |
| 18 | #include "modules/video_coding/codecs/vp8/include/vp8_globals.h" |
| 19 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
| 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
| 23 | VCMEncodedFrame::VCMEncodedFrame() |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 24 | : webrtc::EncodedImage(), |
| 25 | _renderTimeMs(-1), |
| 26 | _payloadType(0), |
| 27 | _missingFrame(false), |
Kári Tristan Helgason | 84ccb2d | 2018-08-16 14:35:26 +0200 | [diff] [blame] | 28 | _codec(kVideoCodecGeneric), |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 29 | _rotation_set(false) { |
Kári Tristan Helgason | 84ccb2d | 2018-08-16 14:35:26 +0200 | [diff] [blame] | 30 | _codecSpecificInfo.codecType = kVideoCodecGeneric; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | } |
| 32 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 33 | VCMEncodedFrame::~VCMEncodedFrame() { |
| 34 | Free(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
| 36 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 37 | void VCMEncodedFrame::Free() { |
| 38 | Reset(); |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 39 | if (data() != nullptr) { |
| 40 | delete[] data(); |
| 41 | set_buffer(nullptr, 0); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 42 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 45 | void VCMEncodedFrame::Reset() { |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 46 | SetTimestamp(0); |
Sergey Silkin | 61832dd | 2018-12-20 14:32:14 +0100 | [diff] [blame] | 47 | SetSpatialIndex(absl::nullopt); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 48 | _renderTimeMs = -1; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 49 | _payloadType = 0; |
| 50 | _frameType = kVideoFrameDelta; |
| 51 | _encodedWidth = 0; |
| 52 | _encodedHeight = 0; |
| 53 | _completeFrame = false; |
| 54 | _missingFrame = false; |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 55 | set_size(0); |
Kári Tristan Helgason | 84ccb2d | 2018-08-16 14:35:26 +0200 | [diff] [blame] | 56 | _codecSpecificInfo.codecType = kVideoCodecGeneric; |
| 57 | _codec = kVideoCodecGeneric; |
perkj | 414dda1 | 2016-07-04 01:45:23 -0700 | [diff] [blame] | 58 | rotation_ = kVideoRotation_0; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 59 | content_type_ = VideoContentType::UNSPECIFIED; |
Ilya Nikolaevskiy | b6c462d | 2018-06-05 15:21:32 +0200 | [diff] [blame] | 60 | timing_.flags = VideoSendTiming::kInvalid; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 61 | _rotation_set = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 64 | void VCMEncodedFrame::CopyCodecSpecific(const RTPVideoHeader* header) { |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 65 | if (header) { |
| 66 | switch (header->codec) { |
Niels Möller | 520ca4e | 2018-06-04 11:14:38 +0200 | [diff] [blame] | 67 | case kVideoCodecVP8: { |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 68 | const auto& vp8_header = |
| 69 | absl::get<RTPVideoHeaderVP8>(header->video_type_header); |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 70 | if (_codecSpecificInfo.codecType != kVideoCodecVP8) { |
| 71 | // This is the first packet for this frame. |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 72 | _codecSpecificInfo.codecSpecific.VP8.temporalIdx = 0; |
| 73 | _codecSpecificInfo.codecSpecific.VP8.layerSync = false; |
| 74 | _codecSpecificInfo.codecSpecific.VP8.keyIdx = -1; |
| 75 | _codecSpecificInfo.codecType = kVideoCodecVP8; |
henrik.lundin@webrtc.org | 473bac8 | 2011-08-17 09:47:33 +0000 | [diff] [blame] | 76 | } |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 77 | _codecSpecificInfo.codecSpecific.VP8.nonReference = |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 78 | vp8_header.nonReference; |
| 79 | if (vp8_header.temporalIdx != kNoTemporalIdx) { |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 80 | _codecSpecificInfo.codecSpecific.VP8.temporalIdx = |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 81 | vp8_header.temporalIdx; |
| 82 | _codecSpecificInfo.codecSpecific.VP8.layerSync = vp8_header.layerSync; |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 83 | } |
Philip Eliasson | d52a1a6 | 2018-09-07 13:03:55 +0000 | [diff] [blame] | 84 | if (vp8_header.keyIdx != kNoKeyIdx) { |
| 85 | _codecSpecificInfo.codecSpecific.VP8.keyIdx = vp8_header.keyIdx; |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 86 | } |
| 87 | break; |
| 88 | } |
Niels Möller | 520ca4e | 2018-06-04 11:14:38 +0200 | [diff] [blame] | 89 | case kVideoCodecVP9: { |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 90 | const auto& vp9_header = |
| 91 | absl::get<RTPVideoHeaderVP9>(header->video_type_header); |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 92 | if (_codecSpecificInfo.codecType != kVideoCodecVP9) { |
| 93 | // This is the first packet for this frame. |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 94 | _codecSpecificInfo.codecSpecific.VP9.temporal_idx = 0; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 95 | _codecSpecificInfo.codecSpecific.VP9.gof_idx = 0; |
| 96 | _codecSpecificInfo.codecSpecific.VP9.inter_layer_predicted = false; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 97 | _codecSpecificInfo.codecType = kVideoCodecVP9; |
| 98 | } |
| 99 | _codecSpecificInfo.codecSpecific.VP9.inter_pic_predicted = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 100 | vp9_header.inter_pic_predicted; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 101 | _codecSpecificInfo.codecSpecific.VP9.flexible_mode = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 102 | vp9_header.flexible_mode; |
philipel | cfc319b | 2015-11-10 07:17:23 -0800 | [diff] [blame] | 103 | _codecSpecificInfo.codecSpecific.VP9.num_ref_pics = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 104 | vp9_header.num_ref_pics; |
| 105 | for (uint8_t r = 0; r < vp9_header.num_ref_pics; ++r) { |
philipel | cfc319b | 2015-11-10 07:17:23 -0800 | [diff] [blame] | 106 | _codecSpecificInfo.codecSpecific.VP9.p_diff[r] = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 107 | vp9_header.pid_diff[r]; |
philipel | cfc319b | 2015-11-10 07:17:23 -0800 | [diff] [blame] | 108 | } |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 109 | _codecSpecificInfo.codecSpecific.VP9.ss_data_available = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 110 | vp9_header.ss_data_available; |
| 111 | if (vp9_header.temporal_idx != kNoTemporalIdx) { |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 112 | _codecSpecificInfo.codecSpecific.VP9.temporal_idx = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 113 | vp9_header.temporal_idx; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 114 | _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 115 | vp9_header.temporal_up_switch; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 116 | } |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 117 | if (vp9_header.spatial_idx != kNoSpatialIdx) { |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 118 | _codecSpecificInfo.codecSpecific.VP9.inter_layer_predicted = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 119 | vp9_header.inter_layer_predicted; |
Sergey Silkin | 61832dd | 2018-12-20 14:32:14 +0100 | [diff] [blame] | 120 | SetSpatialIndex(vp9_header.spatial_idx); |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 121 | } |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 122 | if (vp9_header.gof_idx != kNoGofIdx) { |
| 123 | _codecSpecificInfo.codecSpecific.VP9.gof_idx = vp9_header.gof_idx; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 124 | } |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 125 | if (vp9_header.ss_data_available) { |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 126 | _codecSpecificInfo.codecSpecific.VP9.num_spatial_layers = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 127 | vp9_header.num_spatial_layers; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 128 | _codecSpecificInfo.codecSpecific.VP9 |
| 129 | .spatial_layer_resolution_present = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 130 | vp9_header.spatial_layer_resolution_present; |
| 131 | if (vp9_header.spatial_layer_resolution_present) { |
| 132 | for (size_t i = 0; i < vp9_header.num_spatial_layers; ++i) { |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 133 | _codecSpecificInfo.codecSpecific.VP9.width[i] = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 134 | vp9_header.width[i]; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 135 | _codecSpecificInfo.codecSpecific.VP9.height[i] = |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 136 | vp9_header.height[i]; |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | _codecSpecificInfo.codecSpecific.VP9.gof.CopyGofInfoVP9( |
philipel | 29d8846 | 2018-08-08 14:26:00 +0200 | [diff] [blame] | 140 | vp9_header.gof); |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 141 | } |
| 142 | break; |
| 143 | } |
Niels Möller | 520ca4e | 2018-06-04 11:14:38 +0200 | [diff] [blame] | 144 | case kVideoCodecH264: { |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 145 | _codecSpecificInfo.codecType = kVideoCodecH264; |
| 146 | break; |
| 147 | } |
Niels Möller | 520ca4e | 2018-06-04 11:14:38 +0200 | [diff] [blame] | 148 | default: { |
Kári Tristan Helgason | 84ccb2d | 2018-08-16 14:35:26 +0200 | [diff] [blame] | 149 | _codecSpecificInfo.codecType = kVideoCodecGeneric; |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
henrik.lundin@webrtc.org | 473bac8 | 2011-08-17 09:47:33 +0000 | [diff] [blame] | 152 | } |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 153 | } |
henrik.lundin@webrtc.org | 473bac8 | 2011-08-17 09:47:33 +0000 | [diff] [blame] | 154 | } |
| 155 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 156 | void VCMEncodedFrame::VerifyAndAllocate(size_t minimumSize) { |
Niels Möller | 48a7946 | 2018-12-07 16:21:18 +0100 | [diff] [blame] | 157 | size_t old_capacity = capacity(); |
| 158 | if (minimumSize > old_capacity) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 159 | // create buffer of sufficient size |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 160 | uint8_t* old_data = data(); |
Niels Möller | 48a7946 | 2018-12-07 16:21:18 +0100 | [diff] [blame] | 161 | |
| 162 | set_buffer(new uint8_t[minimumSize], minimumSize); |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 163 | if (old_data) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 164 | // copy old data |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 165 | memcpy(data(), old_data, old_capacity); |
| 166 | delete[] old_data; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 168 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | } |
| 170 | |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 171 | } // namespace webrtc |