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