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