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