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 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
| 12 | #include "webrtc/modules/video_coding/encoded_frame.h" |
| 13 | #include "webrtc/modules/video_coding/generic_encoder.h" |
| 14 | #include "webrtc/modules/video_coding/jitter_buffer_common.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | VCMEncodedFrame::VCMEncodedFrame() |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 19 | : webrtc::EncodedImage(), |
| 20 | _renderTimeMs(-1), |
| 21 | _payloadType(0), |
| 22 | _missingFrame(false), |
| 23 | _codec(kVideoCodecUnknown), |
| 24 | _fragmentation(), |
| 25 | _rotation(kVideoRotation_0), |
| 26 | _rotation_set(false) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 27 | _codecSpecificInfo.codecType = kVideoCodecUnknown; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | VCMEncodedFrame::VCMEncodedFrame(const webrtc::EncodedImage& rhs) |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 31 | : webrtc::EncodedImage(rhs), |
| 32 | _renderTimeMs(-1), |
| 33 | _payloadType(0), |
| 34 | _missingFrame(false), |
| 35 | _codec(kVideoCodecUnknown), |
| 36 | _fragmentation(), |
| 37 | _rotation(kVideoRotation_0), |
| 38 | _rotation_set(false) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 39 | _codecSpecificInfo.codecType = kVideoCodecUnknown; |
| 40 | _buffer = NULL; |
| 41 | _size = 0; |
| 42 | _length = 0; |
| 43 | if (rhs._buffer != NULL) { |
hbos | d664836 | 2016-01-21 05:43:11 -0800 | [diff] [blame] | 44 | VerifyAndAllocate(rhs._length + |
| 45 | EncodedImage::GetBufferPaddingBytes(_codec)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 46 | memcpy(_buffer, rhs._buffer, rhs._length); |
| 47 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | VCMEncodedFrame::VCMEncodedFrame(const VCMEncodedFrame& rhs) |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 51 | : webrtc::EncodedImage(rhs), |
| 52 | _renderTimeMs(rhs._renderTimeMs), |
| 53 | _payloadType(rhs._payloadType), |
| 54 | _missingFrame(rhs._missingFrame), |
| 55 | _codecSpecificInfo(rhs._codecSpecificInfo), |
| 56 | _codec(rhs._codec), |
| 57 | _fragmentation(), |
| 58 | _rotation(rhs._rotation), |
| 59 | _rotation_set(rhs._rotation_set) { |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 +0000 | [diff] [blame] | 60 | _buffer = NULL; |
| 61 | _size = 0; |
| 62 | _length = 0; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 63 | if (rhs._buffer != NULL) { |
hbos | d664836 | 2016-01-21 05:43:11 -0800 | [diff] [blame] | 64 | VerifyAndAllocate(rhs._length + |
| 65 | EncodedImage::GetBufferPaddingBytes(_codec)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 66 | memcpy(_buffer, rhs._buffer, rhs._length); |
| 67 | _length = rhs._length; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 +0000 | [diff] [blame] | 68 | } |
andrew@webrtc.org | 418443c | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 69 | _fragmentation.CopyFrom(rhs._fragmentation); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 72 | VCMEncodedFrame::~VCMEncodedFrame() { |
| 73 | Free(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | } |
| 75 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 76 | void VCMEncodedFrame::Free() { |
| 77 | Reset(); |
| 78 | if (_buffer != NULL) { |
| 79 | delete[] _buffer; |
| 80 | _buffer = NULL; |
| 81 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 84 | void VCMEncodedFrame::Reset() { |
| 85 | _renderTimeMs = -1; |
| 86 | _timeStamp = 0; |
| 87 | _payloadType = 0; |
| 88 | _frameType = kVideoFrameDelta; |
| 89 | _encodedWidth = 0; |
| 90 | _encodedHeight = 0; |
| 91 | _completeFrame = false; |
| 92 | _missingFrame = false; |
| 93 | _length = 0; |
| 94 | _codecSpecificInfo.codecType = kVideoCodecUnknown; |
| 95 | _codec = kVideoCodecUnknown; |
| 96 | _rotation = kVideoRotation_0; |
| 97 | _rotation_set = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 100 | void VCMEncodedFrame::CopyCodecSpecific(const RTPVideoHeader* header) { |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 101 | if (header) { |
| 102 | switch (header->codec) { |
| 103 | case kRtpVideoVp8: { |
| 104 | if (_codecSpecificInfo.codecType != kVideoCodecVP8) { |
| 105 | // This is the first packet for this frame. |
| 106 | _codecSpecificInfo.codecSpecific.VP8.pictureId = -1; |
| 107 | _codecSpecificInfo.codecSpecific.VP8.temporalIdx = 0; |
| 108 | _codecSpecificInfo.codecSpecific.VP8.layerSync = false; |
| 109 | _codecSpecificInfo.codecSpecific.VP8.keyIdx = -1; |
| 110 | _codecSpecificInfo.codecType = kVideoCodecVP8; |
henrik.lundin@webrtc.org | 473bac8 | 2011-08-17 09:47:33 +0000 | [diff] [blame] | 111 | } |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 112 | _codecSpecificInfo.codecSpecific.VP8.nonReference = |
| 113 | header->codecHeader.VP8.nonReference; |
| 114 | if (header->codecHeader.VP8.pictureId != kNoPictureId) { |
| 115 | _codecSpecificInfo.codecSpecific.VP8.pictureId = |
| 116 | header->codecHeader.VP8.pictureId; |
| 117 | } |
| 118 | if (header->codecHeader.VP8.temporalIdx != kNoTemporalIdx) { |
| 119 | _codecSpecificInfo.codecSpecific.VP8.temporalIdx = |
| 120 | header->codecHeader.VP8.temporalIdx; |
| 121 | _codecSpecificInfo.codecSpecific.VP8.layerSync = |
| 122 | header->codecHeader.VP8.layerSync; |
| 123 | } |
| 124 | if (header->codecHeader.VP8.keyIdx != kNoKeyIdx) { |
| 125 | _codecSpecificInfo.codecSpecific.VP8.keyIdx = |
| 126 | header->codecHeader.VP8.keyIdx; |
| 127 | } |
| 128 | break; |
| 129 | } |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 130 | case kRtpVideoVp9: { |
| 131 | if (_codecSpecificInfo.codecType != kVideoCodecVP9) { |
| 132 | // This is the first packet for this frame. |
| 133 | _codecSpecificInfo.codecSpecific.VP9.picture_id = -1; |
| 134 | _codecSpecificInfo.codecSpecific.VP9.temporal_idx = 0; |
| 135 | _codecSpecificInfo.codecSpecific.VP9.spatial_idx = 0; |
| 136 | _codecSpecificInfo.codecSpecific.VP9.gof_idx = 0; |
| 137 | _codecSpecificInfo.codecSpecific.VP9.inter_layer_predicted = false; |
| 138 | _codecSpecificInfo.codecSpecific.VP9.tl0_pic_idx = -1; |
| 139 | _codecSpecificInfo.codecType = kVideoCodecVP9; |
| 140 | } |
| 141 | _codecSpecificInfo.codecSpecific.VP9.inter_pic_predicted = |
| 142 | header->codecHeader.VP9.inter_pic_predicted; |
| 143 | _codecSpecificInfo.codecSpecific.VP9.flexible_mode = |
| 144 | header->codecHeader.VP9.flexible_mode; |
philipel | cfc319b | 2015-11-10 07:17:23 -0800 | [diff] [blame] | 145 | _codecSpecificInfo.codecSpecific.VP9.num_ref_pics = |
| 146 | header->codecHeader.VP9.num_ref_pics; |
| 147 | for (uint8_t r = 0; r < header->codecHeader.VP9.num_ref_pics; ++r) { |
| 148 | _codecSpecificInfo.codecSpecific.VP9.p_diff[r] = |
| 149 | header->codecHeader.VP9.pid_diff[r]; |
| 150 | } |
asapersson | a9455ab | 2015-07-31 06:10:09 -0700 | [diff] [blame] | 151 | _codecSpecificInfo.codecSpecific.VP9.ss_data_available = |
| 152 | header->codecHeader.VP9.ss_data_available; |
| 153 | if (header->codecHeader.VP9.picture_id != kNoPictureId) { |
| 154 | _codecSpecificInfo.codecSpecific.VP9.picture_id = |
| 155 | header->codecHeader.VP9.picture_id; |
| 156 | } |
| 157 | if (header->codecHeader.VP9.tl0_pic_idx != kNoTl0PicIdx) { |
| 158 | _codecSpecificInfo.codecSpecific.VP9.tl0_pic_idx = |
| 159 | header->codecHeader.VP9.tl0_pic_idx; |
| 160 | } |
| 161 | if (header->codecHeader.VP9.temporal_idx != kNoTemporalIdx) { |
| 162 | _codecSpecificInfo.codecSpecific.VP9.temporal_idx = |
| 163 | header->codecHeader.VP9.temporal_idx; |
| 164 | _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch = |
| 165 | header->codecHeader.VP9.temporal_up_switch; |
| 166 | } |
| 167 | if (header->codecHeader.VP9.spatial_idx != kNoSpatialIdx) { |
| 168 | _codecSpecificInfo.codecSpecific.VP9.spatial_idx = |
| 169 | header->codecHeader.VP9.spatial_idx; |
| 170 | _codecSpecificInfo.codecSpecific.VP9.inter_layer_predicted = |
| 171 | header->codecHeader.VP9.inter_layer_predicted; |
| 172 | } |
| 173 | if (header->codecHeader.VP9.gof_idx != kNoGofIdx) { |
| 174 | _codecSpecificInfo.codecSpecific.VP9.gof_idx = |
| 175 | header->codecHeader.VP9.gof_idx; |
| 176 | } |
| 177 | if (header->codecHeader.VP9.ss_data_available) { |
| 178 | _codecSpecificInfo.codecSpecific.VP9.num_spatial_layers = |
| 179 | header->codecHeader.VP9.num_spatial_layers; |
| 180 | _codecSpecificInfo.codecSpecific.VP9 |
| 181 | .spatial_layer_resolution_present = |
| 182 | header->codecHeader.VP9.spatial_layer_resolution_present; |
| 183 | if (header->codecHeader.VP9.spatial_layer_resolution_present) { |
| 184 | for (size_t i = 0; i < header->codecHeader.VP9.num_spatial_layers; |
| 185 | ++i) { |
| 186 | _codecSpecificInfo.codecSpecific.VP9.width[i] = |
| 187 | header->codecHeader.VP9.width[i]; |
| 188 | _codecSpecificInfo.codecSpecific.VP9.height[i] = |
| 189 | header->codecHeader.VP9.height[i]; |
| 190 | } |
| 191 | } |
| 192 | _codecSpecificInfo.codecSpecific.VP9.gof.CopyGofInfoVP9( |
| 193 | header->codecHeader.VP9.gof); |
| 194 | } |
| 195 | break; |
| 196 | } |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 197 | case kRtpVideoH264: { |
| 198 | _codecSpecificInfo.codecType = kVideoCodecH264; |
| 199 | break; |
| 200 | } |
| 201 | default: { |
| 202 | _codecSpecificInfo.codecType = kVideoCodecUnknown; |
| 203 | break; |
| 204 | } |
henrik.lundin@webrtc.org | 473bac8 | 2011-08-17 09:47:33 +0000 | [diff] [blame] | 205 | } |
stefan@webrtc.org | 2ec5606 | 2014-07-31 14:59:24 +0000 | [diff] [blame] | 206 | } |
henrik.lundin@webrtc.org | 473bac8 | 2011-08-17 09:47:33 +0000 | [diff] [blame] | 207 | } |
| 208 | |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 +0000 | [diff] [blame] | 209 | const RTPFragmentationHeader* VCMEncodedFrame::FragmentationHeader() const { |
| 210 | return &_fragmentation; |
| 211 | } |
| 212 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 213 | void VCMEncodedFrame::VerifyAndAllocate(size_t minimumSize) { |
| 214 | if (minimumSize > _size) { |
| 215 | // create buffer of sufficient size |
| 216 | uint8_t* newBuffer = new uint8_t[minimumSize]; |
| 217 | if (_buffer) { |
| 218 | // copy old data |
| 219 | memcpy(newBuffer, _buffer, _size); |
| 220 | delete[] _buffer; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 222 | _buffer = newBuffer; |
| 223 | _size = minimumSize; |
| 224 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | } |
| 226 | |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 227 | } // namespace webrtc |