Chromium Code Reviews| Index: webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc |
| diff --git a/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc b/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..da8e9daf978cb8b86c366625a4804c4c3d8e5130 |
| --- /dev/null |
| +++ b/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc |
| @@ -0,0 +1,737 @@ |
| +/* |
| + * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/rtp_rtcp/source/rtp_format_vp9.h" |
| + |
| +#include <assert.h> // assert |
| +#include <string.h> // memcpy |
| + |
| +#include <vector> |
| + |
| +#include "webrtc/base/bitbuffer.h" |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/system_wrappers/interface/logging.h" |
| + |
| +#define RETURN_FALSE_ON_ERROR(x) \ |
| + if (!(x)) { \ |
| + return false; \ |
| + } |
| + |
| +namespace webrtc { |
| +namespace { |
| +// Length of VP9 payload descriptors' fixed part. |
| +const size_t kFixedPayloadDescriptorBytes = 1; |
| + |
| +const uint32_t kReservedBitValue0 = 0; |
| + |
| +uint8_t TemporalIdxField(const RTPVideoHeaderVP9& hdr, uint8_t def) { |
| + return (hdr.temporalIdx == kNoTemporalIdx) ? def : hdr.temporalIdx; |
| +} |
| + |
| +uint8_t SpatialIdxField(const RTPVideoHeaderVP9& hdr, uint8_t def) { |
| + return (hdr.spatialIdx == kNoSpatialIdx) ? def : hdr.spatialIdx; |
| +} |
| + |
| +int16_t Tl0PicIdxField(const RTPVideoHeaderVP9& hdr, uint8_t def) { |
| + return (hdr.tl0PicIdx == kNoTl0PicIdx) ? def : hdr.tl0PicIdx; |
| +} |
| + |
| +uint8_t GofIdxField(const RTPVideoHeaderVP9& hdr, uint8_t def) { |
| + return (hdr.gofIdx == kNoGofIdx) ? def : hdr.gofIdx; |
| +} |
| + |
| +size_t PictureIdLength(const RTPVideoHeaderVP9& hdr) { |
| + if (hdr.pictureId == kNoPictureId) |
| + return 0; |
| + return (hdr.maxPictureId == kMaxOneBytePictureId) ? 1 : 2; |
| +} |
| + |
| +bool PictureIdPresent(const RTPVideoHeaderVP9& hdr) { |
| + return PictureIdLength(hdr) > 0; |
| +} |
| + |
| +size_t LayerInfoLength(const RTPVideoHeaderVP9& hdr) { |
| + if (hdr.flexibleMode) |
| + return (hdr.temporalIdx == kNoTemporalIdx && |
| + hdr.spatialIdx == kNoSpatialIdx) ? 0 : 1; |
| + else |
| + return (hdr.gofIdx == kNoGofIdx && hdr.spatialIdx == kNoSpatialIdx) ? 0 : 2; |
| +} |
| + |
| +bool LayerInfoPresent(const RTPVideoHeaderVP9& hdr) { |
| + return LayerInfoLength(hdr) > 0; |
| +} |
| + |
| +size_t RefIndicesLength(const RTPVideoHeaderVP9& hdr) { |
| + if (!hdr.interPicPredicted || !hdr.flexibleMode) |
| + return 0; |
| + |
| + DCHECK_GT(hdr.numRefPics, 0); |
| + DCHECK_LE(hdr.numRefPics, kMaxVp9RefPics); |
| + size_t length = 0; |
| + for (uint8_t i = 0; i < hdr.numRefPics; ++i) |
| + length += hdr.pidDiff[i] > 0x3F ? 2 : 1; |
| + |
| + return length; |
| +} |
| + |
| +size_t SsDataLength(const RTPVideoHeaderVP9& hdr) { |
| + if (!hdr.ssDataAvailable) |
| + return 0; |
| + |
| + DCHECK_GT(hdr.numSpatialLayers, 0); |
| + DCHECK_LE(hdr.numSpatialLayers, kMaxVp9NumberOfSpatialLayers); |
| + DCHECK_GT(hdr.gof.numFramesInGof, 0); |
| + DCHECK_LE(hdr.gof.numFramesInGof, kMaxVp9FramesInGof); |
| + size_t length = 1; |
| + if (hdr.spatialLayerResolutionPresent) { |
| + length += 4 * hdr.numSpatialLayers; |
| + } |
| + length += hdr.gof.numFramesInGof; |
| + for (uint8_t i = 0; i < hdr.gof.numFramesInGof; i++) { |
| + length += hdr.gof.numRefPics[i]; |
| + } |
| + return length; |
| +} |
| + |
| +size_t PayloadDescriptorLengthMinusSsData(const RTPVideoHeaderVP9& hdr) { |
| + return kFixedPayloadDescriptorBytes + PictureIdLength(hdr) + |
| + LayerInfoLength(hdr) + RefIndicesLength(hdr); |
| +} |
| + |
| +size_t PayloadDescriptorLength(const RTPVideoHeaderVP9& hdr) { |
| + return PayloadDescriptorLengthMinusSsData(hdr) + SsDataLength(hdr); |
| +} |
| + |
| +void QueuePacket(size_t start_pos, |
| + size_t packet_size, |
| + bool layer_begin, |
| + bool layer_end, |
| + RtpPacketizerVp9::InfoQueue* packets) { |
| + RtpPacketizerVp9::InfoStruct packet_info; |
| + packet_info.payload_start_pos = start_pos; |
| + packet_info.size = packet_size; |
| + packet_info.layer_begin = layer_begin; |
| + packet_info.layer_end = layer_end; |
| + packets->push(packet_info); |
| +} |
| + |
| +// Picture ID: |
| +// |
| +// +-+-+-+-+-+-+-+-+ |
| +// I: |M| PICTURE ID | |
| +// +-+-+-+-+-+-+-+-+ |
| +// M: | EXTENDED PID | |
| +// +-+-+-+-+-+-+-+-+ |
| +// |
| +bool WritePictureId(const RTPVideoHeaderVP9& vp9, |
| + rtc::BitBufferWriter* writer) { |
| + if (PictureIdLength(vp9) == 1) { |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(0, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.pictureId, 7)); |
| + } else { |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(1, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.pictureId, 15)); |
| + } |
| + return true; |
| +} |
| + |
| +// Layer indices: |
| +// |
| +// Flexible mode (F=1): |
| +// |
| +// +-+-+-+-+-+-+-+-+ |
| +// L: | T |U| S |D| |
| +// +-+-+-+-+-+-+-+-+ |
| +// |
| +bool WriteLayerInfoFlexibleMode(const RTPVideoHeaderVP9& vp9, |
| + rtc::BitBufferWriter* writer) { |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(TemporalIdxField(vp9, 0), 3)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.temporalUpSwitch ? 1 : 0, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(SpatialIdxField(vp9, 0), 3)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.interLayerPredicted ? 1 : 0, 1)); |
| + return true; |
| +} |
| + |
| +// Non-flexible mode (F=0): |
| +// |
| +// +-+-+-+-+-+-+-+-+ |
| +// L: |GOF_IDX| S |D| |
| +// +-+-+-+-+-+-+-+-+ |
| +// | TL0PICIDX | |
| +// +-+-+-+-+-+-+-+-+ |
| +// |
| +bool WriteLayerInfoNonFlexibleMode(const RTPVideoHeaderVP9& vp9, |
| + rtc::BitBufferWriter* writer) { |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(GofIdxField(vp9, 0), 4)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(SpatialIdxField(vp9, 0), 3)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.interLayerPredicted ? 1 : 0, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteUInt8(Tl0PicIdxField(vp9, 0))); |
| + return true; |
| +} |
| + |
| +bool WriteLayerInfo(const RTPVideoHeaderVP9& vp9, |
| + rtc::BitBufferWriter* writer) { |
| + if (vp9.flexibleMode) { |
| + return WriteLayerInfoFlexibleMode(vp9, writer); |
| + } else { |
| + return WriteLayerInfoNonFlexibleMode(vp9, writer); |
| + } |
| +} |
| + |
| +// Reference indices: |
| +// |
| +// +-+-+-+-+-+-+-+-+ -\ |
| +// P,F: | P_DIFF |X|N| . |
| +// +-+-+-+-+-+-+-+-+ . - up to 3 times |
| +// X: |EXTENDED P_DIFF| . |
| +// +-+-+-+-+-+-+-+-+ -/ |
| +// |
| +bool WriteRefIndices(const RTPVideoHeaderVP9& vp9, |
| + rtc::BitBufferWriter* writer) { |
| + if (!PictureIdPresent(vp9) || |
| + vp9.numRefPics == 0 || vp9.numRefPics > kMaxVp9RefPics) { |
| + return false; |
| + } |
| + for (uint8_t i = 0; i < vp9.numRefPics; ++i) { |
| + uint8_t n = (i == vp9.numRefPics - 1) ? 0 : 1; |
| + if (vp9.pidDiff[i] <= 0x3F) { |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.pidDiff[i], 6)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(0, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(n, 1)); |
| + } else { |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.pidDiff[i] >> 8, 6)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(1, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(n, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteUInt8(vp9.pidDiff[i])); |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +// Scalability structure (SS). |
| +// |
| +// +-+-+-+-+-+-+-+-+ |
| +// V: | N_S |Y| N_G | |
| +// +-+-+-+-+-+-+-+-+ -\ |
| +// Y: | WIDTH | (OPTIONAL) . |
| +// + + . |
| +// | | (OPTIONAL) . |
| +// +-+-+-+-+-+-+-+-+ . - N_S + 1 times |
| +// | HEIGHT | (OPTIONAL) . |
| +// + + . |
| +// | | (OPTIONAL) . |
| +// +-+-+-+-+-+-+-+-+ -/ -\ |
| +// N_G: | T |U| R |-|-| (OPTIONAL) . |
| +// +-+-+-+-+-+-+-+-+ -\ . - N_G + 1 times |
| +// | P_DIFF | (OPTIONAL) . - R times . |
| +// +-+-+-+-+-+-+-+-+ -/ -/ |
| +// |
| +bool WriteSsData(const RTPVideoHeaderVP9& vp9, rtc::BitBufferWriter* writer) { |
| + if (vp9.numSpatialLayers == 0 || |
| + vp9.numSpatialLayers > kMaxVp9NumberOfSpatialLayers || |
| + vp9.gof.numFramesInGof == 0 || |
| + vp9.gof.numFramesInGof > kMaxVp9FramesInGof) { |
| + return false; |
| + } |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.numSpatialLayers - 1, 3)); |
| + RETURN_FALSE_ON_ERROR( |
| + writer->WriteBits(vp9.spatialLayerResolutionPresent ? 1 : 0, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.gof.numFramesInGof - 1, 4)); |
| + |
| + if (vp9.spatialLayerResolutionPresent) { |
| + for (uint8_t s = 0; s < vp9.numSpatialLayers; s++) { |
| + RETURN_FALSE_ON_ERROR(writer->WriteUInt16(vp9.width[s])); |
| + RETURN_FALSE_ON_ERROR(writer->WriteUInt16(vp9.height[s])); |
| + } |
| + } |
| + for (uint8_t i = 0; i < vp9.gof.numFramesInGof; i++) { |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.gof.temporalIdx[i], 3)); |
| + RETURN_FALSE_ON_ERROR( |
| + writer->WriteBits(vp9.gof.temporalUpSwitch[i] ? 1 : 0, 1)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(vp9.gof.numRefPics[i], 2)); |
| + RETURN_FALSE_ON_ERROR(writer->WriteBits(kReservedBitValue0, 2)); |
| + for (uint8_t r = 0; r < vp9.gof.numRefPics[i]; r++) { |
| + RETURN_FALSE_ON_ERROR(writer->WriteUInt8(vp9.gof.pidDiff[i][r])); |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +// Picture ID: |
| +// |
| +// +-+-+-+-+-+-+-+-+ |
| +// I: |M| PICTURE ID | |
| +// +-+-+-+-+-+-+-+-+ |
| +// M: | EXTENDED PID | |
| +// +-+-+-+-+-+-+-+-+ |
| +// |
| +bool ParsePictureId(rtc::BitBuffer* parser, RTPVideoHeaderVP9* vp9) { |
| + uint32_t picture_id = 0; |
| + uint32_t m_bit = 0; |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&m_bit, 1)); |
| + if (m_bit) { |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&picture_id, 15)); |
| + vp9->maxPictureId = kMaxTwoBytePictureId; |
| + } else { |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&picture_id, 7)); |
| + vp9->maxPictureId = kMaxOneBytePictureId; |
| + } |
| + vp9->pictureId = picture_id; |
| + return true; |
| +} |
| + |
| +// Layer indices (flexible mode): |
| +// |
| +// +-+-+-+-+-+-+-+-+ |
| +// L: | T |U| S |D| |
| +// +-+-+-+-+-+-+-+-+ |
| +// |
| +bool ParseLayerInfoFlexibleMode(rtc::BitBuffer* parser, |
| + RTPVideoHeaderVP9* vp9) { |
| + uint32_t t = 0; |
| + uint32_t u_bit = 0; |
| + uint32_t s = 0; |
| + uint32_t d_bit = 0; |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&t, 3)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&u_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&s, 3)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&d_bit, 1)); |
| + vp9->temporalIdx = t; |
| + vp9->temporalUpSwitch = u_bit; |
| + vp9->spatialIdx = s; |
| + vp9->interLayerPredicted = d_bit; |
| + return true; |
| +} |
| + |
| +// Layer indices (non-flexible mode): |
| +// |
| +// +-+-+-+-+-+-+-+-+ |
| +// L: |GOF_IDX| S |D| |
| +// +-+-+-+-+-+-+-+-+ |
| +// | TL0PICIDX | |
| +// +-+-+-+-+-+-+-+-+ |
| +// |
| +bool ParseLayerInfoNonFlexibleMode(rtc::BitBuffer* parser, |
| + RTPVideoHeaderVP9* vp9) { |
| + uint32_t gof_idx = 0; |
| + uint32_t s = 0; |
| + uint32_t d_bit = 0; |
| + uint8_t tl0picidx = 0; |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&gof_idx, 4)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&s, 3)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&d_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadUInt8(&tl0picidx)); |
| + vp9->gofIdx = gof_idx; |
| + vp9->spatialIdx = s; |
| + vp9->interLayerPredicted = d_bit; |
| + vp9->tl0PicIdx = tl0picidx; |
| + return true; |
| +} |
| + |
| +bool ParseLayerInfo(rtc::BitBuffer* parser, RTPVideoHeaderVP9* vp9) { |
| + if (vp9->flexibleMode) |
| + return ParseLayerInfoFlexibleMode(parser, vp9); |
| + else |
| + return ParseLayerInfoNonFlexibleMode(parser, vp9); |
| +} |
| + |
| +// Reference indices: |
| +// |
| +// +-+-+-+-+-+-+-+-+ -\ |
| +// P,F: | P_DIFF |X|N| . |
| +// +-+-+-+-+-+-+-+-+ . - up to 3 times |
| +// X: |EXTENDED P_DIFF| . |
| +// +-+-+-+-+-+-+-+-+ -/ |
| +// |
| +bool ParseRefIndices(rtc::BitBuffer* parser, RTPVideoHeaderVP9* vp9) { |
| + if (vp9->pictureId == kNoPictureId) |
| + return false; |
| + |
| + vp9->numRefPics = 0; |
| + uint32_t x_bit = 0; |
| + uint32_t n_bit = 0; |
| + do { |
| + if (vp9->numRefPics == kMaxVp9RefPics) |
| + return false; |
| + |
| + uint32_t p_diff = 0; |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&p_diff, 6)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&x_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&n_bit, 1)); |
| + |
| + if (x_bit) { |
| + // P_DIFF is 14 bits. |
| + uint8_t ext_p_diff = 0; |
| + RETURN_FALSE_ON_ERROR(parser->ReadUInt8(&ext_p_diff)); |
| + p_diff = (p_diff << 8) + ext_p_diff; |
| + } |
| + |
| + vp9->pidDiff[vp9->numRefPics] = p_diff; |
| + uint32_t scaled_pid = vp9->pictureId; |
| + while (p_diff > scaled_pid) { |
| + scaled_pid += vp9->maxPictureId + 1; |
| + } |
| + vp9->refPictureId[vp9->numRefPics++] = scaled_pid - p_diff; |
| + } while (n_bit); |
| + |
| + return true; |
| +} |
| + |
| +// Scalability structure (SS). |
| +// |
| +// +-+-+-+-+-+-+-+-+ |
| +// V: | N_S |Y| N_G | |
| +// +-+-+-+-+-+-+-+-+ -\ |
| +// Y: | WIDTH | (OPTIONAL) . |
| +// + + . |
| +// | | (OPTIONAL) . |
| +// +-+-+-+-+-+-+-+-+ . - N_S + 1 times |
| +// | HEIGHT | (OPTIONAL) . |
| +// + + . |
| +// | | (OPTIONAL) . |
| +// +-+-+-+-+-+-+-+-+ -/ -\ |
| +// N_G: | T |U| R |-|-| (OPTIONAL) . |
| +// +-+-+-+-+-+-+-+-+ -\ . - N_G + 1 times |
| +// | P_DIFF | (OPTIONAL) . - R times . |
| +// +-+-+-+-+-+-+-+-+ -/ -/ |
| +// |
| +bool ParseSsData(rtc::BitBuffer* parser, RTPVideoHeaderVP9* vp9) { |
| + uint32_t n_s = 0; |
| + uint32_t y_bit = 0; |
| + uint32_t n_g = 0; |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&n_s, 3)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&y_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&n_g, 4)); |
| + vp9->numSpatialLayers = n_s + 1; |
| + vp9->spatialLayerResolutionPresent = y_bit ? true : false; |
| + vp9->gof.numFramesInGof = n_g + 1; |
| + |
| + if (y_bit) { |
| + for (uint8_t s = 0; s < vp9->numSpatialLayers; s++) { |
| + RETURN_FALSE_ON_ERROR(parser->ReadUInt16(&vp9->width[s])); |
| + RETURN_FALSE_ON_ERROR(parser->ReadUInt16(&vp9->height[s])); |
| + } |
| + } |
| + for (uint8_t i = 0; i < vp9->gof.numFramesInGof; i++) { |
| + uint32_t t = 0; |
| + uint32_t u = 0; |
| + uint32_t r = 0; |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&t, 3)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&u, 1)); |
| + RETURN_FALSE_ON_ERROR(parser->ReadBits(&r, 2)); |
| + RETURN_FALSE_ON_ERROR(parser->ConsumeBits(2)); |
| + vp9->gof.temporalIdx[i] = t; |
| + vp9->gof.temporalUpSwitch[i] = u ? true : false; |
| + vp9->gof.numRefPics[i] = r; |
| + |
| + for (uint8_t p = 0; p < vp9->gof.numRefPics[i]; p++) { |
| + uint8_t p_diff = 0; |
| + RETURN_FALSE_ON_ERROR(parser->ReadUInt8(&p_diff)); |
| + vp9->gof.pidDiff[i][p] = p_diff; |
| + } |
| + } |
| + return true; |
| +} |
| +} // namespace |
| + |
| +RtpPacketizerVp9::RtpPacketizerVp9(const RTPVideoHeaderVP9& hdr, |
| + size_t max_payload_len) |
| + : payload_data_(NULL), |
| + payload_size_(0), |
| + balance_(true), |
| + hdr_(hdr), |
| + max_payload_len_(max_payload_len) { |
| +} |
| + |
| +RtpPacketizerVp9::~RtpPacketizerVp9() { |
| +} |
| + |
| +void RtpPacketizerVp9::SetPayloadData( |
| + const uint8_t* payload_data, |
| + size_t payload_size, |
| + const RTPFragmentationHeader* fragmentation) { |
| + payload_data_ = payload_data; |
| + payload_size_ = payload_size; |
| + GeneratePackets(); |
| +} |
| + |
| +bool RtpPacketizerVp9::NextPacket(uint8_t* buffer, |
| + size_t* bytes_to_send, |
| + bool* last_packet) { |
| + if (packets_.empty()) { |
| + return false; |
| + } |
| + InfoStruct packet_info = packets_.front(); |
| + packets_.pop(); |
| + |
| + if (!WriteHeaderAndPayload(packet_info, buffer, bytes_to_send)) { |
| + return false; |
| + } |
| + *last_packet = packets_.empty(); |
| + return true; |
| +} |
| + |
| +ProtectionType RtpPacketizerVp9::GetProtectionType() { |
| + bool protect = |
| + hdr_.temporalIdx == 0 || hdr_.temporalIdx == kNoTemporalIdx; |
| + return protect ? kProtectedPacket : kUnprotectedPacket; |
| +} |
| + |
| +StorageType RtpPacketizerVp9::GetStorageType(uint32_t retransmission_settings) { |
| + StorageType storage = kAllowRetransmission; |
| + if (hdr_.temporalIdx == 0 && |
| + !(retransmission_settings & kRetransmitBaseLayer)) { |
| + storage = kDontRetransmit; |
| + } else if (hdr_.temporalIdx != kNoTemporalIdx && hdr_.temporalIdx > 0 && |
| + !(retransmission_settings & kRetransmitHigherLayers)) { |
| + storage = kDontRetransmit; |
| + } |
| + return storage; |
| +} |
| + |
| +std::string RtpPacketizerVp9::ToString() { |
| + return "RtpPacketizerVp9"; |
| +} |
| + |
| +size_t RtpPacketizerVp9::CalcNextSize(size_t max_payload_len, |
| + size_t rem_bytes) const { |
| + if (max_payload_len == 0 || rem_bytes == 0) { |
| + return 0; |
| + } |
| + if (balance_) { |
| + // Produce (almost) equal size fragments. |
| + // Number of fragments for remaining bytes. |
| + size_t num_frags = ceil(static_cast<double>(rem_bytes) / max_payload_len); |
| + // Number of bytes in this fragment. |
| + return static_cast<size_t>( |
| + static_cast<double>(rem_bytes) / num_frags + 0.5); |
| + } |
| + return max_payload_len >= rem_bytes ? rem_bytes : max_payload_len; |
| +} |
| + |
| +void RtpPacketizerVp9::GeneratePackets() { |
| + if (max_payload_len_ < PayloadDescriptorLength(hdr_) + 1) { |
| + LOG(LS_ERROR) << "Payload header and one payload byte won't fit."; |
| + return; |
| + } |
| + size_t bytes_processed = 0; |
| + while (bytes_processed < payload_size_) { |
| + size_t rem_bytes = payload_size_ - bytes_processed; |
| + size_t rem_payload_len = max_payload_len_ - |
| + (bytes_processed ? PayloadDescriptorLengthMinusSsData(hdr_) |
| + : PayloadDescriptorLength(hdr_)); |
| + |
| + size_t packet_bytes = CalcNextSize(rem_payload_len, rem_bytes); |
| + if (packet_bytes == 0) { |
| + LOG(LS_ERROR) << "Failed to generate VP9 packets."; |
| + while (!packets_.empty()) |
| + packets_.pop(); |
| + return; |
| + } |
| + QueuePacket(bytes_processed, packet_bytes, bytes_processed == 0, |
| + rem_bytes == packet_bytes, &packets_); |
| + bytes_processed += packet_bytes; |
| + } |
| + assert(bytes_processed == payload_size_); |
| +} |
| + |
| + |
| +// VP9 format: |
| +// |
| +// Payload descriptor for F = 1 (flexible mode) |
| +// 0 1 2 3 4 5 6 7 |
| +// +-+-+-+-+-+-+-+-+ |
| +// |I|P|L|F|B|E|V|-| (REQUIRED) |
| +// +-+-+-+-+-+-+-+-+ |
| +// I: |M| PICTURE ID | (RECOMMENDED) |
| +// +-+-+-+-+-+-+-+-+ |
| +// M: | EXTENDED PID | (RECOMMENDED) |
| +// +-+-+-+-+-+-+-+-+ |
| +// L: | T |U| S |D| (CONDITIONALLY RECOMMENDED) |
| +// +-+-+-+-+-+-+-+-+ -\ |
| +// P,F: | P_DIFF |X|N| (CONDITIONALLY RECOMMENDED) . |
| +// +-+-+-+-+-+-+-+-+ . - up to 3 times |
| +// X: |EXTENDED P_DIFF| . |
| +// +-+-+-+-+-+-+-+-+ -/ |
| +// V: | SS | |
| +// | .. | |
| +// +-+-+-+-+-+-+-+-+ |
| +// |
| +// Payload descriptor for F = 0 (non-flexible mode) |
| +// 0 1 2 3 4 5 6 7 |
| +// +-+-+-+-+-+-+-+-+ |
| +// |I|P|L|F|B|E|V|-| (REQUIRED) |
| +// +-+-+-+-+-+-+-+-+ |
| +// I: |M| PICTURE ID | (RECOMMENDED) |
| +// +-+-+-+-+-+-+-+-+ |
| +// M: | EXTENDED PID | (RECOMMENDED) |
| +// +-+-+-+-+-+-+-+-+ |
| +// L: |GOF_IDX| S |D| (CONDITIONALLY RECOMMENDED) |
| +// +-+-+-+-+-+-+-+-+ |
| +// | TL0PICIDX | (CONDITIONALLY REQUIRED) |
| +// +-+-+-+-+-+-+-+-+ |
| +// V: | SS | |
| +// | .. | |
| +// +-+-+-+-+-+-+-+-+ |
| + |
| +bool RtpPacketizerVp9::WriteHeaderAndPayload(const InfoStruct& packet_info, |
| + uint8_t* buffer, |
| + size_t* bytes_to_send) const { |
| + size_t header_length; |
| + if (!WriteHeader(packet_info, buffer, &header_length)) |
| + return false; |
| + |
| + // Copy payload data. |
| + memcpy(&buffer[header_length], |
| + &payload_data_[packet_info.payload_start_pos], packet_info.size); |
| + |
| + *bytes_to_send = header_length + packet_info.size; |
| + return true; |
| +} |
| + |
| +bool RtpPacketizerVp9::WriteHeader(const InfoStruct& packet_info, |
| + uint8_t* buffer, |
| + size_t* header_length) const { |
| + // Required payload descriptor byte. |
| + uint32_t i_bit = PictureIdPresent(hdr_) ? 1 : 0; |
| + uint32_t p_bit = hdr_.interPicPredicted ? 1 : 0; |
| + uint32_t l_bit = LayerInfoPresent(hdr_) ? 1 : 0; |
| + uint32_t f_bit = hdr_.flexibleMode ? 1 : 0; |
| + uint32_t b_bit = (hdr_.beginningOfFrame && |
| + packet_info.layer_begin) ? 1 : 0; |
| + uint32_t e_bit = (hdr_.endOfFrame && packet_info.layer_end) ? 1 : 0; |
| + uint32_t v_bit = (hdr_.ssDataAvailable && hdr_.beginningOfFrame && |
| + packet_info.layer_begin) ? 1 : 0; |
| + |
| + rtc::BitBufferWriter writer(buffer, max_payload_len_); |
| + RETURN_FALSE_ON_ERROR(writer.WriteBits(i_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(writer.WriteBits(p_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(writer.WriteBits(l_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(writer.WriteBits(f_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(writer.WriteBits(b_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(writer.WriteBits(e_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(writer.WriteBits(v_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(writer.WriteBits(kReservedBitValue0, 1)); |
| + |
| + // Add fields that are present. |
| + if (i_bit && !WritePictureId(hdr_, &writer)) { |
| + LOG(LS_ERROR) << "Failed writing VP9 picture id."; |
| + return false; |
| + } |
| + if (l_bit && !WriteLayerInfo(hdr_, &writer)) { |
| + LOG(LS_ERROR) << "Failed writing VP9 layer info."; |
| + return false; |
| + } |
| + if (p_bit && f_bit && !WriteRefIndices(hdr_, &writer)) { |
| + LOG(LS_ERROR) << "Failed writing VP9 ref indices."; |
| + return false; |
| + } |
| + if (v_bit && !WriteSsData(hdr_, &writer)) { |
| + LOG(LS_ERROR) << "Failed writing VP9 SS data."; |
| + return false; |
| + } |
| + |
| + size_t offset_bytes = 0; |
| + size_t offset_bits = 0; |
| + writer.GetCurrentOffset(&offset_bytes, &offset_bits); |
| + assert(offset_bits == 0); |
| + |
| + *header_length = offset_bytes; |
| + return true; |
| +} |
| + |
| +bool RtpDepacketizerVp9::Parse(ParsedPayload* parsed_payload, |
| + const uint8_t* payload_data, |
| + size_t payload_data_length) { |
| + assert(parsed_payload != NULL); |
| + if (payload_data_length == 0) { |
| + LOG(LS_ERROR) << "Payload length is zero."; |
| + return false; |
| + } |
| + |
| + // Parse mandatory first byte of payload descriptor. |
| + rtc::BitBuffer parser(payload_data, payload_data_length); |
| + uint32_t i_bit = 0; |
| + uint32_t p_bit = 0; |
| + uint32_t l_bit = 0; |
| + uint32_t f_bit = 0; |
| + uint32_t b_bit = 0; |
| + uint32_t e_bit = 0; |
| + uint32_t v_bit = 0; |
| + RETURN_FALSE_ON_ERROR(parser.ReadBits(&i_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser.ReadBits(&p_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser.ReadBits(&l_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser.ReadBits(&f_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser.ReadBits(&b_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser.ReadBits(&e_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser.ReadBits(&v_bit, 1)); |
| + RETURN_FALSE_ON_ERROR(parser.ConsumeBits(1)); |
| + |
| + // Parsed payload. |
| + parsed_payload->type.Video.width = 0; |
| + parsed_payload->type.Video.height = 0; |
| + parsed_payload->type.Video.simulcastIdx = 0; |
| + parsed_payload->type.Video.codec = kRtpVideoVp9; |
| + |
| + parsed_payload->frame_type = p_bit ? kVideoFrameDelta : kVideoFrameKey; |
| + |
| + RTPVideoHeaderVP9* vp9 = &parsed_payload->type.Video.codecHeader.VP9; |
| + vp9->InitRTPVideoHeaderVP9(); |
| + vp9->interPicPredicted = p_bit; |
| + vp9->flexibleMode = f_bit; |
| + vp9->beginningOfFrame = b_bit; |
| + vp9->endOfFrame = e_bit; |
| + vp9->ssDataAvailable = v_bit; |
| + vp9->temporalIdx = 0; |
| + vp9->spatialIdx = 0; |
| + |
| + // Parse fields that are present. |
| + if (i_bit && !ParsePictureId(&parser, vp9)) { |
| + LOG(LS_ERROR) << "Failed parsing VP9 picture id."; |
| + return false; |
| + } |
| + if (l_bit && !ParseLayerInfo(&parser, vp9)) { |
| + LOG(LS_ERROR) << "Failed parsing VP9 layer info."; |
| + return false; |
| + } |
| + if (p_bit && f_bit && !ParseRefIndices(&parser, vp9)) { |
| + LOG(LS_ERROR) << "Failed parsing VP9 ref indices."; |
| + return false; |
| + } |
| + if (v_bit) { |
| + if (!ParseSsData(&parser, vp9)) { |
| + LOG(LS_ERROR) << "Failed parsing VP9 SS data."; |
| + return false; |
| + } |
| + if (vp9->spatialLayerResolutionPresent) { |
| + // TODO(asapersson): Add support for spatial layers. |
| + parsed_payload->type.Video.width = vp9->width[0]; |
| + parsed_payload->type.Video.height = vp9->height[0]; |
| + } |
| + } |
| + |
| + // TODO(asapersson): check how this "isFirstPacket" variable is used. |
| + // 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.
|
| + parsed_payload->type.Video.isFirstPacket = b_bit && (vp9->spatialIdx == 0); |
| + |
| + uint64_t rem_bits = parser.RemainingBitCount(); |
| + assert(rem_bits % 8 == 0); |
| + parsed_payload->payload_length = rem_bits / 8; |
| + if (parsed_payload->payload_length == 0) { |
| + LOG(LS_ERROR) << "Failed parsing VP9 payload data."; |
| + return false; |
| + } |
| + parsed_payload->payload = |
| + payload_data + payload_data_length - parsed_payload->payload_length; |
| + |
| + return true; |
| +} |
| +} // namespace webrtc |