OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 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 |
| 11 #include "webrtc/modules/rtp_rtcp/source/rtp_format_vp9.h" |
| 12 |
| 13 #include <assert.h> |
| 14 #include <string.h> |
| 15 |
| 16 #include <cmath> |
| 17 |
| 18 #include "webrtc/base/bitbuffer.h" |
| 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/system_wrappers/interface/logging.h" |
| 21 |
| 22 #define RETURN_FALSE_ON_ERROR(x) \ |
| 23 if (!(x)) { \ |
| 24 return false; \ |
| 25 } |
| 26 |
| 27 namespace webrtc { |
| 28 namespace { |
| 29 // Length of VP9 payload descriptors' fixed part. |
| 30 const size_t kFixedPayloadDescriptorBytes = 1; |
| 31 |
| 32 // Packet fragmentation mode. If true, packets are split into (almost) equal |
| 33 // sizes. Otherwise, as many bytes as possible are fit into one packet. |
| 34 const bool kBalancedMode = true; |
| 35 |
| 36 const uint32_t kReservedBitValue0 = 0; |
| 37 |
| 38 uint8_t TemporalIdxField(const RTPVideoHeaderVP9& hdr, uint8_t def) { |
| 39 return (hdr.temporal_idx == kNoTemporalIdx) ? def : hdr.temporal_idx; |
| 40 } |
| 41 |
| 42 uint8_t SpatialIdxField(const RTPVideoHeaderVP9& hdr, uint8_t def) { |
| 43 return (hdr.spatial_idx == kNoSpatialIdx) ? def : hdr.spatial_idx; |
| 44 } |
| 45 |
| 46 int16_t Tl0PicIdxField(const RTPVideoHeaderVP9& hdr, uint8_t def) { |
| 47 return (hdr.tl0_pic_idx == kNoTl0PicIdx) ? def : hdr.tl0_pic_idx; |
| 48 } |
| 49 |
| 50 uint8_t GofIdxField(const RTPVideoHeaderVP9& hdr, uint8_t def) { |
| 51 return (hdr.gof_idx == kNoGofIdx) ? def : hdr.gof_idx; |
| 52 } |
| 53 |
| 54 // Picture ID: |
| 55 // |
| 56 // +-+-+-+-+-+-+-+-+ |
| 57 // I: |M| PICTURE ID | M:0 => picture id is 7 bits. |
| 58 // +-+-+-+-+-+-+-+-+ M:1 => picture id is 15 bits. |
| 59 // M: | EXTENDED PID | |
| 60 // +-+-+-+-+-+-+-+-+ |
| 61 // |
| 62 size_t PictureIdLength(const RTPVideoHeaderVP9& hdr) { |
| 63 if (hdr.picture_id == kNoPictureId) |
| 64 return 0; |
| 65 return (hdr.max_picture_id == kMaxOneBytePictureId) ? 1 : 2; |
| 66 } |
| 67 |
| 68 bool PictureIdPresent(const RTPVideoHeaderVP9& hdr) { |
| 69 return PictureIdLength(hdr) > 0; |
| 70 } |
| 71 |
| 72 // Layer indices: |
| 73 // |
| 74 // Flexible mode (F=1): Non-flexible mode (F=0): |
| 75 // |
| 76 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ |
| 77 // L: | T |U| S |D| |GOF_IDX| S |D| |
| 78 // +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ |
| 79 // | TL0PICIDX | |
| 80 // +-+-+-+-+-+-+-+-+ |
| 81 // |
| 82 size_t LayerInfoLength(const RTPVideoHeaderVP9& hdr) { |
| 83 if (hdr.flexible_mode) { |
| 84 return (hdr.temporal_idx == kNoTemporalIdx && |
| 85 hdr.spatial_idx == kNoSpatialIdx) ? 0 : 1; |
| 86 } else { |
| 87 return (hdr.gof_idx == kNoGofIdx && |
| 88 hdr.spatial_idx == kNoSpatialIdx) ? 0 : 2; |
| 89 } |
| 90 } |
| 91 |
| 92 bool LayerInfoPresent(const RTPVideoHeaderVP9& hdr) { |
| 93 return LayerInfoLength(hdr) > 0; |
| 94 } |
| 95 |
| 96 // Reference indices: |
| 97 // |
| 98 // +-+-+-+-+-+-+-+-+ -| P=1,F=1: At least one reference index |
| 99 // P,F: | P_DIFF |X|N| . has to be specified. |
| 100 // +-+-+-+-+-+-+-+-+ . up to 3 times |
| 101 // X: |EXTENDED P_DIFF| . X=1: Extended P_DIFF is used (14 |
| 102 // +-+-+-+-+-+-+-+-+ -| bits). Else 6 bits are used. |
| 103 // N=1: An additional P_DIFF follows |
| 104 // current P_DIFF. |
| 105 size_t RefIndicesLength(const RTPVideoHeaderVP9& hdr) { |
| 106 if (!hdr.inter_pic_predicted || !hdr.flexible_mode) |
| 107 return 0; |
| 108 |
| 109 DCHECK_GT(hdr.num_ref_pics, 0U); |
| 110 DCHECK_LE(hdr.num_ref_pics, kMaxVp9RefPics); |
| 111 size_t length = 0; |
| 112 for (size_t i = 0; i < hdr.num_ref_pics; ++i) { |
| 113 length += hdr.pid_diff[i] > 0x3F ? 2 : 1; // P_DIFF > 6 bits => extended |
| 114 } |
| 115 return length; |
| 116 } |
| 117 |
| 118 // Scalability structure (SS). |
| 119 // |
| 120 // +-+-+-+-+-+-+-+-+ |
| 121 // V: | N_S |Y| N_G | |
| 122 // +-+-+-+-+-+-+-+-+ -| |
| 123 // Y: | WIDTH | (OPTIONAL) . |
| 124 // + + . |
| 125 // | | (OPTIONAL) . |
| 126 // +-+-+-+-+-+-+-+-+ . N_S + 1 times |
| 127 // | HEIGHT | (OPTIONAL) . |
| 128 // + + . |
| 129 // | | (OPTIONAL) . |
| 130 // +-+-+-+-+-+-+-+-+ -| -| |
| 131 // N_G: | T |U| R |-|-| (OPTIONAL) . |
| 132 // +-+-+-+-+-+-+-+-+ -| . N_G + 1 times |
| 133 // | P_DIFF | (OPTIONAL) . R times . |
| 134 // +-+-+-+-+-+-+-+-+ -| -| |
| 135 // |
| 136 size_t SsDataLength(const RTPVideoHeaderVP9& hdr) { |
| 137 if (!hdr.ss_data_available) |
| 138 return 0; |
| 139 |
| 140 DCHECK_GT(hdr.num_spatial_layers, 0U); |
| 141 DCHECK_LE(hdr.num_spatial_layers, kMaxVp9NumberOfSpatialLayers); |
| 142 DCHECK_GT(hdr.gof.num_frames_in_gof, 0U); |
| 143 DCHECK_LE(hdr.gof.num_frames_in_gof, kMaxVp9FramesInGof); |
| 144 size_t length = 1; // V |
| 145 if (hdr.spatial_layer_resolution_present) { |
| 146 length += 4 * hdr.num_spatial_layers; // Y |
| 147 } |
| 148 // N_G |
| 149 length += hdr.gof.num_frames_in_gof; // T, U, R |
| 150 for (size_t i = 0; i < hdr.gof.num_frames_in_gof; ++i) { |
| 151 DCHECK_LE(hdr.gof.num_ref_pics[i], kMaxVp9RefPics); |
| 152 length += hdr.gof.num_ref_pics[i]; // R times |
| 153 } |
| 154 return length; |
| 155 } |
| 156 |
| 157 size_t PayloadDescriptorLengthMinusSsData(const RTPVideoHeaderVP9& hdr) { |
| 158 return kFixedPayloadDescriptorBytes + PictureIdLength(hdr) + |
| 159 LayerInfoLength(hdr) + RefIndicesLength(hdr); |
| 160 } |
| 161 |
| 162 size_t PayloadDescriptorLength(const RTPVideoHeaderVP9& hdr) { |
| 163 return PayloadDescriptorLengthMinusSsData(hdr) + SsDataLength(hdr); |
| 164 } |
| 165 |
| 166 void QueuePacket(size_t start_pos, |
| 167 size_t size, |
| 168 bool layer_begin, |
| 169 bool layer_end, |
| 170 RtpPacketizerVp9::PacketInfoQueue* packets) { |
| 171 RtpPacketizerVp9::PacketInfo packet_info; |
| 172 packet_info.payload_start_pos = start_pos; |
| 173 packet_info.size = size; |
| 174 packet_info.layer_begin = layer_begin; |
| 175 packet_info.layer_end = layer_end; |
| 176 packets->push(packet_info); |
| 177 } |
| 178 |
| 179 // Picture ID: |
| 180 // |
| 181 // +-+-+-+-+-+-+-+-+ |
| 182 // I: |M| PICTURE ID | M:0 => picture id is 7 bits. |
| 183 // +-+-+-+-+-+-+-+-+ M:1 => picture id is 15 bits. |
| 184 // M: | EXTENDED PID | |
| 185 // +-+-+-+-+-+-+-+-+ |
| 186 // |
| 187 bool WritePictureId(const RTPVideoHeaderVP9& vp9, |
| 188 rtc::BitBufferWriter* writer) { |
| 189 bool m_bit = (PictureIdLength(vp9) == 2); |
| 190 RETURN_FALSE_ON_ERROR(writer->WriteBits(m_bit ? 1 : 0, 1)); |
| 191 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.picture_id, m_bit ? 15 : 7)); |
| 192 return true; |
| 193 } |
| 194 |
| 195 // Layer indices: |
| 196 // |
| 197 // Flexible mode (F=1): |
| 198 // |
| 199 // +-+-+-+-+-+-+-+-+ |
| 200 // L: | T |U| S |D| |
| 201 // +-+-+-+-+-+-+-+-+ |
| 202 // |
| 203 bool WriteLayerInfoFlexibleMode(const RTPVideoHeaderVP9& vp9, |
| 204 rtc::BitBufferWriter* writer) { |
| 205 RETURN_FALSE_ON_ERROR(writer->WriteBits(TemporalIdxField(vp9, 0), 3)); |
| 206 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.temporal_up_switch ? 1 : 0, 1)); |
| 207 RETURN_FALSE_ON_ERROR(writer->WriteBits(SpatialIdxField(vp9, 0), 3)); |
| 208 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.inter_layer_predicted ? 1: 0, 1)); |
| 209 return true; |
| 210 } |
| 211 |
| 212 // Non-flexible mode (F=0): |
| 213 // |
| 214 // +-+-+-+-+-+-+-+-+ |
| 215 // L: |GOF_IDX| S |D| |
| 216 // +-+-+-+-+-+-+-+-+ |
| 217 // | TL0PICIDX | |
| 218 // +-+-+-+-+-+-+-+-+ |
| 219 // |
| 220 bool WriteLayerInfoNonFlexibleMode(const RTPVideoHeaderVP9& vp9, |
| 221 rtc::BitBufferWriter* writer) { |
| 222 RETURN_FALSE_ON_ERROR(writer->WriteBits(GofIdxField(vp9, 0), 4)); |
| 223 RETURN_FALSE_ON_ERROR(writer->WriteBits(SpatialIdxField(vp9, 0), 3)); |
| 224 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.inter_layer_predicted ? 1: 0, 1)); |
| 225 RETURN_FALSE_ON_ERROR(writer->WriteUInt8(Tl0PicIdxField(vp9, 0))); |
| 226 return true; |
| 227 } |
| 228 |
| 229 bool WriteLayerInfo(const RTPVideoHeaderVP9& vp9, |
| 230 rtc::BitBufferWriter* writer) { |
| 231 if (vp9.flexible_mode) { |
| 232 return WriteLayerInfoFlexibleMode(vp9, writer); |
| 233 } else { |
| 234 return WriteLayerInfoNonFlexibleMode(vp9, writer); |
| 235 } |
| 236 } |
| 237 |
| 238 // Reference indices: |
| 239 // |
| 240 // +-+-+-+-+-+-+-+-+ -| P=1,F=1: At least one reference index |
| 241 // P,F: | P_DIFF |X|N| . has to be specified. |
| 242 // +-+-+-+-+-+-+-+-+ . up to 3 times |
| 243 // X: |EXTENDED P_DIFF| . X=1: Extended P_DIFF is used (14 |
| 244 // +-+-+-+-+-+-+-+-+ -| bits). Else 6 bits are used. |
| 245 // N=1: An additional P_DIFF follows |
| 246 // current P_DIFF. |
| 247 bool WriteRefIndices(const RTPVideoHeaderVP9& vp9, |
| 248 rtc::BitBufferWriter* writer) { |
| 249 if (!PictureIdPresent(vp9) || |
| 250 vp9.num_ref_pics == 0 || vp9.num_ref_pics > kMaxVp9RefPics) { |
| 251 return false; |
| 252 } |
| 253 for (size_t i = 0; i < vp9.num_ref_pics; ++i) { |
| 254 bool x_bit = (vp9.pid_diff[i] > 0x3F); |
| 255 bool n_bit = !(i == vp9.num_ref_pics - 1); |
| 256 if (x_bit) { |
| 257 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.pid_diff[i] >> 8, 6)); |
| 258 RETURN_FALSE_ON_ERROR(writer->WriteBits(x_bit ? 1 : 0, 1)); |
| 259 RETURN_FALSE_ON_ERROR(writer->WriteBits(n_bit ? 1 : 0, 1)); |
| 260 RETURN_FALSE_ON_ERROR(writer->WriteUInt8(vp9.pid_diff[i])); |
| 261 } else { |
| 262 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.pid_diff[i], 6)); |
| 263 RETURN_FALSE_ON_ERROR(writer->WriteBits(x_bit ? 1 : 0, 1)); |
| 264 RETURN_FALSE_ON_ERROR(writer->WriteBits(n_bit ? 1 : 0, 1)); |
| 265 } |
| 266 } |
| 267 return true; |
| 268 } |
| 269 |
| 270 // Scalability structure (SS). |
| 271 // |
| 272 // +-+-+-+-+-+-+-+-+ |
| 273 // V: | N_S |Y| N_G | |
| 274 // +-+-+-+-+-+-+-+-+ -| |
| 275 // Y: | WIDTH | (OPTIONAL) . |
| 276 // + + . |
| 277 // | | (OPTIONAL) . |
| 278 // +-+-+-+-+-+-+-+-+ . N_S + 1 times |
| 279 // | HEIGHT | (OPTIONAL) . |
| 280 // + + . |
| 281 // | | (OPTIONAL) . |
| 282 // +-+-+-+-+-+-+-+-+ -| -| |
| 283 // N_G: | T |U| R |-|-| (OPTIONAL) . |
| 284 // +-+-+-+-+-+-+-+-+ -| . N_G + 1 times |
| 285 // | P_DIFF | (OPTIONAL) . R times . |
| 286 // +-+-+-+-+-+-+-+-+ -| -| |
| 287 // |
| 288 bool WriteSsData(const RTPVideoHeaderVP9& vp9, rtc::BitBufferWriter* writer) { |
| 289 DCHECK_GT(vp9.num_spatial_layers, 0U); |
| 290 DCHECK_LE(vp9.num_spatial_layers, kMaxVp9NumberOfSpatialLayers); |
| 291 DCHECK_GT(vp9.gof.num_frames_in_gof, 0U); |
| 292 DCHECK_LE(vp9.gof.num_frames_in_gof, kMaxVp9FramesInGof); |
| 293 |
| 294 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.num_spatial_layers - 1, 3)); |
| 295 RETURN_FALSE_ON_ERROR( |
| 296 writer->WriteBits(vp9.spatial_layer_resolution_present ? 1 : 0, 1)); |
| 297 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.gof.num_frames_in_gof - 1, 4)); |
| 298 |
| 299 if (vp9.spatial_layer_resolution_present) { |
| 300 for (size_t i = 0; i < vp9.num_spatial_layers; ++i) { |
| 301 RETURN_FALSE_ON_ERROR(writer->WriteUInt16(vp9.width[i])); |
| 302 RETURN_FALSE_ON_ERROR(writer->WriteUInt16(vp9.height[i])); |
| 303 } |
| 304 } |
| 305 for (size_t i = 0; i < vp9.gof.num_frames_in_gof; ++i) { |
| 306 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.gof.temporal_idx[i], 3)); |
| 307 RETURN_FALSE_ON_ERROR( |
| 308 writer->WriteBits(vp9.gof.temporal_up_switch[i] ? 1 : 0, 1)); |
| 309 RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.gof.num_ref_pics[i], 2)); |
| 310 RETURN_FALSE_ON_ERROR(writer->WriteBits(kReservedBitValue0, 2)); |
| 311 for (size_t r = 0; r < vp9.gof.num_ref_pics[i]; ++r) { |
| 312 RETURN_FALSE_ON_ERROR(writer->WriteUInt8(vp9.gof.pid_diff[i][r])); |
| 313 } |
| 314 } |
| 315 return true; |
| 316 } |
| 317 |
| 318 // Picture ID: |
| 319 // |
| 320 // +-+-+-+-+-+-+-+-+ |
| 321 // I: |M| PICTURE ID | M:0 => picture id is 7 bits. |
| 322 // +-+-+-+-+-+-+-+-+ M:1 => picture id is 15 bits. |
| 323 // M: | EXTENDED PID | |
| 324 // +-+-+-+-+-+-+-+-+ |
| 325 // |
| 326 bool ParsePictureId(rtc::BitBuffer* parser, RTPVideoHeaderVP9* vp9) { |
| 327 uint32_t picture_id; |
| 328 uint32_t m_bit; |
| 329 RETURN_FALSE_ON_ERROR(parser->ReadBits(&m_bit, 1)); |
| 330 if (m_bit) { |
| 331 RETURN_FALSE_ON_ERROR(parser->ReadBits(&picture_id, 15)); |
| 332 vp9->max_picture_id = kMaxTwoBytePictureId; |
| 333 } else { |
| 334 RETURN_FALSE_ON_ERROR(parser->ReadBits(&picture_id, 7)); |
| 335 vp9->max_picture_id = kMaxOneBytePictureId; |
| 336 } |
| 337 vp9->picture_id = picture_id; |
| 338 return true; |
| 339 } |
| 340 |
| 341 // Layer indices (flexible mode): |
| 342 // |
| 343 // +-+-+-+-+-+-+-+-+ |
| 344 // L: | T |U| S |D| |
| 345 // +-+-+-+-+-+-+-+-+ |
| 346 // |
| 347 bool ParseLayerInfoFlexibleMode(rtc::BitBuffer* parser, |
| 348 RTPVideoHeaderVP9* vp9) { |
| 349 uint32_t t, u_bit, s, d_bit; |
| 350 RETURN_FALSE_ON_ERROR(parser->ReadBits(&t, 3)); |
| 351 RETURN_FALSE_ON_ERROR(parser->ReadBits(&u_bit, 1)); |
| 352 RETURN_FALSE_ON_ERROR(parser->ReadBits(&s, 3)); |
| 353 RETURN_FALSE_ON_ERROR(parser->ReadBits(&d_bit, 1)); |
| 354 vp9->temporal_idx = t; |
| 355 vp9->temporal_up_switch = u_bit ? true : false; |
| 356 vp9->spatial_idx = s; |
| 357 vp9->inter_layer_predicted = d_bit ? true : false; |
| 358 return true; |
| 359 } |
| 360 |
| 361 // Layer indices (non-flexible mode): |
| 362 // |
| 363 // +-+-+-+-+-+-+-+-+ |
| 364 // L: |GOF_IDX| S |D| |
| 365 // +-+-+-+-+-+-+-+-+ |
| 366 // | TL0PICIDX | |
| 367 // +-+-+-+-+-+-+-+-+ |
| 368 // |
| 369 bool ParseLayerInfoNonFlexibleMode(rtc::BitBuffer* parser, |
| 370 RTPVideoHeaderVP9* vp9) { |
| 371 uint32_t gof_idx, s, d_bit; |
| 372 uint8_t tl0picidx; |
| 373 RETURN_FALSE_ON_ERROR(parser->ReadBits(&gof_idx, 4)); |
| 374 RETURN_FALSE_ON_ERROR(parser->ReadBits(&s, 3)); |
| 375 RETURN_FALSE_ON_ERROR(parser->ReadBits(&d_bit, 1)); |
| 376 RETURN_FALSE_ON_ERROR(parser->ReadUInt8(&tl0picidx)); |
| 377 vp9->gof_idx = gof_idx; |
| 378 vp9->spatial_idx = s; |
| 379 vp9->inter_layer_predicted = d_bit ? true : false; |
| 380 vp9->tl0_pic_idx = tl0picidx; |
| 381 return true; |
| 382 } |
| 383 |
| 384 bool ParseLayerInfo(rtc::BitBuffer* parser, RTPVideoHeaderVP9* vp9) { |
| 385 if (vp9->flexible_mode) { |
| 386 return ParseLayerInfoFlexibleMode(parser, vp9); |
| 387 } else { |
| 388 return ParseLayerInfoNonFlexibleMode(parser, vp9); |
| 389 } |
| 390 } |
| 391 |
| 392 // Reference indices: |
| 393 // |
| 394 // +-+-+-+-+-+-+-+-+ -| P=1,F=1: At least one reference index |
| 395 // P,F: | P_DIFF |X|N| . has to be specified. |
| 396 // +-+-+-+-+-+-+-+-+ . up to 3 times |
| 397 // X: |EXTENDED P_DIFF| . X=1: Extended P_DIFF is used (14 |
| 398 // +-+-+-+-+-+-+-+-+ -| bits). Else 6 bits are used. |
| 399 // N=1: An additional P_DIFF follows |
| 400 // current P_DIFF. |
| 401 bool ParseRefIndices(rtc::BitBuffer* parser, RTPVideoHeaderVP9* vp9) { |
| 402 if (vp9->picture_id == kNoPictureId) |
| 403 return false; |
| 404 |
| 405 vp9->num_ref_pics = 0; |
| 406 uint32_t n_bit; |
| 407 do { |
| 408 if (vp9->num_ref_pics == kMaxVp9RefPics) |
| 409 return false; |
| 410 |
| 411 uint32_t p_diff, x_bit; |
| 412 RETURN_FALSE_ON_ERROR(parser->ReadBits(&p_diff, 6)); |
| 413 RETURN_FALSE_ON_ERROR(parser->ReadBits(&x_bit, 1)); |
| 414 RETURN_FALSE_ON_ERROR(parser->ReadBits(&n_bit, 1)); |
| 415 |
| 416 if (x_bit) { |
| 417 // P_DIFF is 14 bits. |
| 418 uint8_t ext_p_diff; |
| 419 RETURN_FALSE_ON_ERROR(parser->ReadUInt8(&ext_p_diff)); |
| 420 p_diff = (p_diff << 8) + ext_p_diff; |
| 421 } |
| 422 |
| 423 vp9->pid_diff[vp9->num_ref_pics] = p_diff; |
| 424 uint32_t scaled_pid = vp9->picture_id; |
| 425 while (p_diff > scaled_pid) { |
| 426 scaled_pid += vp9->max_picture_id + 1; |
| 427 } |
| 428 vp9->ref_picture_id[vp9->num_ref_pics++] = scaled_pid - p_diff; |
| 429 } while (n_bit); |
| 430 |
| 431 return true; |
| 432 } |
| 433 |
| 434 // Scalability structure (SS). |
| 435 // |
| 436 // +-+-+-+-+-+-+-+-+ |
| 437 // V: | N_S |Y| N_G | |
| 438 // +-+-+-+-+-+-+-+-+ -| |
| 439 // Y: | WIDTH | (OPTIONAL) . |
| 440 // + + . |
| 441 // | | (OPTIONAL) . |
| 442 // +-+-+-+-+-+-+-+-+ . N_S + 1 times |
| 443 // | HEIGHT | (OPTIONAL) . |
| 444 // + + . |
| 445 // | | (OPTIONAL) . |
| 446 // +-+-+-+-+-+-+-+-+ -| -| |
| 447 // N_G: | T |U| R |-|-| (OPTIONAL) . |
| 448 // +-+-+-+-+-+-+-+-+ -| . N_G + 1 times |
| 449 // | P_DIFF | (OPTIONAL) . R times . |
| 450 // +-+-+-+-+-+-+-+-+ -| -| |
| 451 // |
| 452 bool ParseSsData(rtc::BitBuffer* parser, RTPVideoHeaderVP9* vp9) { |
| 453 uint32_t n_s, y_bit, n_g; |
| 454 RETURN_FALSE_ON_ERROR(parser->ReadBits(&n_s, 3)); |
| 455 RETURN_FALSE_ON_ERROR(parser->ReadBits(&y_bit, 1)); |
| 456 RETURN_FALSE_ON_ERROR(parser->ReadBits(&n_g, 4)); |
| 457 vp9->num_spatial_layers = n_s + 1; |
| 458 vp9->spatial_layer_resolution_present = y_bit ? true : false; |
| 459 vp9->gof.num_frames_in_gof = n_g + 1; |
| 460 |
| 461 if (y_bit) { |
| 462 for (size_t i = 0; i < vp9->num_spatial_layers; ++i) { |
| 463 RETURN_FALSE_ON_ERROR(parser->ReadUInt16(&vp9->width[i])); |
| 464 RETURN_FALSE_ON_ERROR(parser->ReadUInt16(&vp9->height[i])); |
| 465 } |
| 466 } |
| 467 for (size_t i = 0; i < vp9->gof.num_frames_in_gof; ++i) { |
| 468 uint32_t t, u_bit, r; |
| 469 RETURN_FALSE_ON_ERROR(parser->ReadBits(&t, 3)); |
| 470 RETURN_FALSE_ON_ERROR(parser->ReadBits(&u_bit, 1)); |
| 471 RETURN_FALSE_ON_ERROR(parser->ReadBits(&r, 2)); |
| 472 RETURN_FALSE_ON_ERROR(parser->ConsumeBits(2)); |
| 473 vp9->gof.temporal_idx[i] = t; |
| 474 vp9->gof.temporal_up_switch[i] = u_bit ? true : false; |
| 475 vp9->gof.num_ref_pics[i] = r; |
| 476 |
| 477 for (size_t p = 0; p < vp9->gof.num_ref_pics[i]; ++p) { |
| 478 uint8_t p_diff; |
| 479 RETURN_FALSE_ON_ERROR(parser->ReadUInt8(&p_diff)); |
| 480 vp9->gof.pid_diff[i][p] = p_diff; |
| 481 } |
| 482 } |
| 483 return true; |
| 484 } |
| 485 |
| 486 // Gets the size of next payload chunk to send. Returns 0 on error. |
| 487 size_t CalcNextSize(size_t max_length, size_t rem_bytes) { |
| 488 if (max_length == 0 || rem_bytes == 0) { |
| 489 return 0; |
| 490 } |
| 491 if (kBalancedMode) { |
| 492 size_t num_frags = std::ceil(static_cast<double>(rem_bytes) / max_length); |
| 493 return static_cast<size_t>( |
| 494 static_cast<double>(rem_bytes) / num_frags + 0.5); |
| 495 } |
| 496 return max_length >= rem_bytes ? rem_bytes : max_length; |
| 497 } |
| 498 } // namespace |
| 499 |
| 500 |
| 501 RtpPacketizerVp9::RtpPacketizerVp9(const RTPVideoHeaderVP9& hdr, |
| 502 size_t max_payload_length) |
| 503 : hdr_(hdr), |
| 504 max_payload_length_(max_payload_length), |
| 505 payload_(nullptr), |
| 506 payload_size_(0) { |
| 507 } |
| 508 |
| 509 RtpPacketizerVp9::~RtpPacketizerVp9() { |
| 510 } |
| 511 |
| 512 ProtectionType RtpPacketizerVp9::GetProtectionType() { |
| 513 bool protect = |
| 514 hdr_.temporal_idx == 0 || hdr_.temporal_idx == kNoTemporalIdx; |
| 515 return protect ? kProtectedPacket : kUnprotectedPacket; |
| 516 } |
| 517 |
| 518 StorageType RtpPacketizerVp9::GetStorageType(uint32_t retransmission_settings) { |
| 519 StorageType storage = kAllowRetransmission; |
| 520 if (hdr_.temporal_idx == 0 && |
| 521 !(retransmission_settings & kRetransmitBaseLayer)) { |
| 522 storage = kDontRetransmit; |
| 523 } else if (hdr_.temporal_idx != kNoTemporalIdx && hdr_.temporal_idx > 0 && |
| 524 !(retransmission_settings & kRetransmitHigherLayers)) { |
| 525 storage = kDontRetransmit; |
| 526 } |
| 527 return storage; |
| 528 } |
| 529 |
| 530 std::string RtpPacketizerVp9::ToString() { |
| 531 return "RtpPacketizerVp9"; |
| 532 } |
| 533 |
| 534 void RtpPacketizerVp9::SetPayloadData( |
| 535 const uint8_t* payload, |
| 536 size_t payload_size, |
| 537 const RTPFragmentationHeader* fragmentation) { |
| 538 payload_ = payload; |
| 539 payload_size_ = payload_size; |
| 540 GeneratePackets(); |
| 541 } |
| 542 |
| 543 void RtpPacketizerVp9::GeneratePackets() { |
| 544 if (max_payload_length_ < PayloadDescriptorLength(hdr_) + 1) { |
| 545 LOG(LS_ERROR) << "Payload header and one payload byte won't fit."; |
| 546 return; |
| 547 } |
| 548 size_t bytes_processed = 0; |
| 549 while (bytes_processed < payload_size_) { |
| 550 size_t rem_bytes = payload_size_ - bytes_processed; |
| 551 size_t rem_payload_len = max_payload_length_ - |
| 552 (bytes_processed ? PayloadDescriptorLengthMinusSsData(hdr_) |
| 553 : PayloadDescriptorLength(hdr_)); |
| 554 |
| 555 size_t packet_bytes = CalcNextSize(rem_payload_len, rem_bytes); |
| 556 if (packet_bytes == 0) { |
| 557 LOG(LS_ERROR) << "Failed to generate VP9 packets."; |
| 558 while (!packets_.empty()) |
| 559 packets_.pop(); |
| 560 return; |
| 561 } |
| 562 QueuePacket(bytes_processed, packet_bytes, bytes_processed == 0, |
| 563 rem_bytes == packet_bytes, &packets_); |
| 564 bytes_processed += packet_bytes; |
| 565 } |
| 566 assert(bytes_processed == payload_size_); |
| 567 } |
| 568 |
| 569 bool RtpPacketizerVp9::NextPacket(uint8_t* buffer, |
| 570 size_t* bytes_to_send, |
| 571 bool* last_packet) { |
| 572 if (packets_.empty()) { |
| 573 return false; |
| 574 } |
| 575 PacketInfo packet_info = packets_.front(); |
| 576 packets_.pop(); |
| 577 |
| 578 if (!WriteHeaderAndPayload(packet_info, buffer, bytes_to_send)) { |
| 579 return false; |
| 580 } |
| 581 *last_packet = packets_.empty(); |
| 582 return true; |
| 583 } |
| 584 |
| 585 // VP9 format: |
| 586 // |
| 587 // Payload descriptor for F = 1 (flexible mode) |
| 588 // 0 1 2 3 4 5 6 7 |
| 589 // +-+-+-+-+-+-+-+-+ |
| 590 // |I|P|L|F|B|E|V|-| (REQUIRED) |
| 591 // +-+-+-+-+-+-+-+-+ |
| 592 // I: |M| PICTURE ID | (RECOMMENDED) |
| 593 // +-+-+-+-+-+-+-+-+ |
| 594 // M: | EXTENDED PID | (RECOMMENDED) |
| 595 // +-+-+-+-+-+-+-+-+ |
| 596 // L: | T |U| S |D| (CONDITIONALLY RECOMMENDED) |
| 597 // +-+-+-+-+-+-+-+-+ -| |
| 598 // P,F: | P_DIFF |X|N| (CONDITIONALLY RECOMMENDED) . |
| 599 // +-+-+-+-+-+-+-+-+ . up to 3 times |
| 600 // X: |EXTENDED P_DIFF| . |
| 601 // +-+-+-+-+-+-+-+-+ -| |
| 602 // V: | SS | |
| 603 // | .. | |
| 604 // +-+-+-+-+-+-+-+-+ |
| 605 // |
| 606 // Payload descriptor for F = 0 (non-flexible mode) |
| 607 // 0 1 2 3 4 5 6 7 |
| 608 // +-+-+-+-+-+-+-+-+ |
| 609 // |I|P|L|F|B|E|V|-| (REQUIRED) |
| 610 // +-+-+-+-+-+-+-+-+ |
| 611 // I: |M| PICTURE ID | (RECOMMENDED) |
| 612 // +-+-+-+-+-+-+-+-+ |
| 613 // M: | EXTENDED PID | (RECOMMENDED) |
| 614 // +-+-+-+-+-+-+-+-+ |
| 615 // L: |GOF_IDX| S |D| (CONDITIONALLY RECOMMENDED) |
| 616 // +-+-+-+-+-+-+-+-+ |
| 617 // | TL0PICIDX | (CONDITIONALLY REQUIRED) |
| 618 // +-+-+-+-+-+-+-+-+ |
| 619 // V: | SS | |
| 620 // | .. | |
| 621 // +-+-+-+-+-+-+-+-+ |
| 622 |
| 623 bool RtpPacketizerVp9::WriteHeaderAndPayload(const PacketInfo& packet_info, |
| 624 uint8_t* buffer, |
| 625 size_t* bytes_to_send) const { |
| 626 size_t header_length; |
| 627 if (!WriteHeader(packet_info, buffer, &header_length)) |
| 628 return false; |
| 629 |
| 630 // Copy payload data. |
| 631 memcpy(&buffer[header_length], |
| 632 &payload_[packet_info.payload_start_pos], packet_info.size); |
| 633 |
| 634 *bytes_to_send = header_length + packet_info.size; |
| 635 return true; |
| 636 } |
| 637 |
| 638 bool RtpPacketizerVp9::WriteHeader(const PacketInfo& packet_info, |
| 639 uint8_t* buffer, |
| 640 size_t* header_length) const { |
| 641 // Required payload descriptor byte. |
| 642 bool i_bit = PictureIdPresent(hdr_); |
| 643 bool p_bit = hdr_.inter_pic_predicted; |
| 644 bool l_bit = LayerInfoPresent(hdr_); |
| 645 bool f_bit = hdr_.flexible_mode; |
| 646 bool b_bit = hdr_.beginning_of_frame && packet_info.layer_begin; |
| 647 bool e_bit = hdr_.end_of_frame && packet_info.layer_end; |
| 648 bool v_bit = hdr_.ss_data_available && b_bit; |
| 649 |
| 650 rtc::BitBufferWriter writer(buffer, max_payload_length_); |
| 651 RETURN_FALSE_ON_ERROR(writer.WriteBits(i_bit ? 1 : 0, 1)); |
| 652 RETURN_FALSE_ON_ERROR(writer.WriteBits(p_bit ? 1 : 0, 1)); |
| 653 RETURN_FALSE_ON_ERROR(writer.WriteBits(l_bit ? 1 : 0, 1)); |
| 654 RETURN_FALSE_ON_ERROR(writer.WriteBits(f_bit ? 1 : 0, 1)); |
| 655 RETURN_FALSE_ON_ERROR(writer.WriteBits(b_bit ? 1 : 0, 1)); |
| 656 RETURN_FALSE_ON_ERROR(writer.WriteBits(e_bit ? 1 : 0, 1)); |
| 657 RETURN_FALSE_ON_ERROR(writer.WriteBits(v_bit ? 1 : 0, 1)); |
| 658 RETURN_FALSE_ON_ERROR(writer.WriteBits(kReservedBitValue0, 1)); |
| 659 |
| 660 // Add fields that are present. |
| 661 if (i_bit && !WritePictureId(hdr_, &writer)) { |
| 662 LOG(LS_ERROR) << "Failed writing VP9 picture id."; |
| 663 return false; |
| 664 } |
| 665 if (l_bit && !WriteLayerInfo(hdr_, &writer)) { |
| 666 LOG(LS_ERROR) << "Failed writing VP9 layer info."; |
| 667 return false; |
| 668 } |
| 669 if (p_bit && f_bit && !WriteRefIndices(hdr_, &writer)) { |
| 670 LOG(LS_ERROR) << "Failed writing VP9 ref indices."; |
| 671 return false; |
| 672 } |
| 673 if (v_bit && !WriteSsData(hdr_, &writer)) { |
| 674 LOG(LS_ERROR) << "Failed writing VP9 SS data."; |
| 675 return false; |
| 676 } |
| 677 |
| 678 size_t offset_bytes = 0; |
| 679 size_t offset_bits = 0; |
| 680 writer.GetCurrentOffset(&offset_bytes, &offset_bits); |
| 681 assert(offset_bits == 0); |
| 682 |
| 683 *header_length = offset_bytes; |
| 684 return true; |
| 685 } |
| 686 |
| 687 bool RtpDepacketizerVp9::Parse(ParsedPayload* parsed_payload, |
| 688 const uint8_t* payload, |
| 689 size_t payload_length) { |
| 690 assert(parsed_payload != nullptr); |
| 691 if (payload_length == 0) { |
| 692 LOG(LS_ERROR) << "Payload length is zero."; |
| 693 return false; |
| 694 } |
| 695 |
| 696 // Parse mandatory first byte of payload descriptor. |
| 697 rtc::BitBuffer parser(payload, payload_length); |
| 698 uint32_t i_bit, p_bit, l_bit, f_bit, b_bit, e_bit, v_bit; |
| 699 RETURN_FALSE_ON_ERROR(parser.ReadBits(&i_bit, 1)); |
| 700 RETURN_FALSE_ON_ERROR(parser.ReadBits(&p_bit, 1)); |
| 701 RETURN_FALSE_ON_ERROR(parser.ReadBits(&l_bit, 1)); |
| 702 RETURN_FALSE_ON_ERROR(parser.ReadBits(&f_bit, 1)); |
| 703 RETURN_FALSE_ON_ERROR(parser.ReadBits(&b_bit, 1)); |
| 704 RETURN_FALSE_ON_ERROR(parser.ReadBits(&e_bit, 1)); |
| 705 RETURN_FALSE_ON_ERROR(parser.ReadBits(&v_bit, 1)); |
| 706 RETURN_FALSE_ON_ERROR(parser.ConsumeBits(1)); |
| 707 |
| 708 // Parsed payload. |
| 709 parsed_payload->type.Video.width = 0; |
| 710 parsed_payload->type.Video.height = 0; |
| 711 parsed_payload->type.Video.simulcastIdx = 0; |
| 712 parsed_payload->type.Video.codec = kRtpVideoVp9; |
| 713 |
| 714 parsed_payload->frame_type = p_bit ? kVideoFrameDelta : kVideoFrameKey; |
| 715 |
| 716 RTPVideoHeaderVP9* vp9 = &parsed_payload->type.Video.codecHeader.VP9; |
| 717 vp9->InitRTPVideoHeaderVP9(); |
| 718 vp9->inter_pic_predicted = p_bit ? true : false; |
| 719 vp9->flexible_mode = f_bit ? true : false; |
| 720 vp9->beginning_of_frame = b_bit ? true : false; |
| 721 vp9->end_of_frame = e_bit ? true : false; |
| 722 vp9->ss_data_available = v_bit ? true : false; |
| 723 vp9->temporal_idx = 0; |
| 724 vp9->spatial_idx = 0; |
| 725 |
| 726 // Parse fields that are present. |
| 727 if (i_bit && !ParsePictureId(&parser, vp9)) { |
| 728 LOG(LS_ERROR) << "Failed parsing VP9 picture id."; |
| 729 return false; |
| 730 } |
| 731 if (l_bit && !ParseLayerInfo(&parser, vp9)) { |
| 732 LOG(LS_ERROR) << "Failed parsing VP9 layer info."; |
| 733 return false; |
| 734 } |
| 735 if (p_bit && f_bit && !ParseRefIndices(&parser, vp9)) { |
| 736 LOG(LS_ERROR) << "Failed parsing VP9 ref indices."; |
| 737 return false; |
| 738 } |
| 739 if (v_bit) { |
| 740 if (!ParseSsData(&parser, vp9)) { |
| 741 LOG(LS_ERROR) << "Failed parsing VP9 SS data."; |
| 742 return false; |
| 743 } |
| 744 if (vp9->spatial_layer_resolution_present) { |
| 745 // TODO(asapersson): Add support for spatial layers. |
| 746 parsed_payload->type.Video.width = vp9->width[0]; |
| 747 parsed_payload->type.Video.height = vp9->height[0]; |
| 748 } |
| 749 } |
| 750 parsed_payload->type.Video.isFirstPacket = b_bit && (vp9->spatial_idx == 0); |
| 751 |
| 752 uint64_t rem_bits = parser.RemainingBitCount(); |
| 753 assert(rem_bits % 8 == 0); |
| 754 parsed_payload->payload_length = rem_bits / 8; |
| 755 if (parsed_payload->payload_length == 0) { |
| 756 LOG(LS_ERROR) << "Failed parsing VP9 payload data."; |
| 757 return false; |
| 758 } |
| 759 parsed_payload->payload = |
| 760 payload + payload_length - parsed_payload->payload_length; |
| 761 |
| 762 return true; |
| 763 } |
| 764 } // namespace webrtc |
OLD | NEW |