1 /*
2  * Copyright (c) 2014 Tim Walker <tdskywalker@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * VPS/SPS/PPS decoders
7  *
8  * Based on hevc_ps.c from ffmpeg (www.ffmpeg.org)
9  *
10  * Copyright (C) 2012 - 2103 Guillaume Martres
11  * Copyright (C) 2012 - 2103 Mickael Raulet
12  * Copyright (C) 2012 - 2013 Gildas Cocherel
13  * Copyright (C) 2013 Vittorio Giovara
14  *
15  * This file is part of FFmpeg.
16  *
17  * slice header decoder
18  *
19  * Copyright (C) 2012 - 2013 Guillaume Martres
20  *
21  * This file is part of FFmpeg.
22  *
23  *
24  * FFmpeg is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU Lesser General Public
26  * License as published by the Free Software Foundation; either
27  * version 2.1 of the License, or (at your option) any later version.
28  *
29  * FFmpeg is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32  * Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public
35  * License along with FFmpeg; if not, write to the Free Software
36  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37  */
38 
39 #include "tvheadend.h"
40 #include "parsers.h"
41 #include "parser_hevc.h"
42 #include "parser_avc.h"
43 #include "bitstream.h"
44 #include "service.h"
45 
46 #define IS_IRAP_NAL(nal_type) (nal_type >= 16 && nal_type <= 23)
47 
48 #define MAX_REFS 16
49 #define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field
50 #define MAX_SUB_LAYERS 7
51 #define MAX_VPS_COUNT 16
52 #define MAX_SPS_COUNT 32
53 #define MAX_PPS_COUNT 256
54 #define MAX_SHORT_TERM_RPS_COUNT 64
55 
56 #define B_SLICE 0
57 #define P_SLICE 1
58 #define I_SLICE 2
59 
60 typedef struct HVCCNALUnitArray {
61     uint8_t  NAL_unit_type;
62     uint16_t numNalus;
63     uint16_t *nalUnitLength;
64     uint8_t  **nalUnit;
65 } HVCCNALUnitArray;
66 
67 typedef struct HEVCDecoderConfigurationRecord {
68     uint8_t  configurationVersion;
69     uint8_t  general_profile_space;
70     uint8_t  general_tier_flag;
71     uint8_t  general_profile_idc;
72     uint32_t general_profile_compatibility_flags;
73     uint64_t general_constraint_indicator_flags;
74     uint8_t  general_level_idc;
75     uint16_t min_spatial_segmentation_idc;
76     uint8_t  parallelismType;
77     uint8_t  chromaFormat;
78     uint8_t  bitDepthLumaMinus8;
79     uint8_t  bitDepthChromaMinus8;
80     uint16_t avgFrameRate;
81     uint8_t  constantFrameRate;
82     uint8_t  numTemporalLayers;
83     uint8_t  temporalIdNested;
84     uint8_t  lengthSizeMinusOne;
85     uint8_t  numOfArrays;
86     HVCCNALUnitArray *array;
87 } HEVCDecoderConfigurationRecord;
88 
89 typedef struct HVCCProfileTierLevel {
90     uint8_t  profile_space;
91     uint8_t  tier_flag;
92     uint8_t  profile_idc;
93     uint32_t profile_compatibility_flags;
94     uint64_t constraint_indicator_flags;
95     uint8_t  level_idc;
96 } HVCCProfileTierLevel;
97 
hvcc_update_ptl(HEVCDecoderConfigurationRecord * hvcc,HVCCProfileTierLevel * ptl)98 static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc,
99                             HVCCProfileTierLevel *ptl)
100 {
101     /*
102      * The value of general_profile_space in all the parameter sets must be
103      * identical.
104      */
105     hvcc->general_profile_space = ptl->profile_space;
106 
107     /*
108      * The level indication general_level_idc must indicate a level of
109      * capability equal to or greater than the highest level indicated for the
110      * highest tier in all the parameter sets.
111      */
112     if (hvcc->general_tier_flag < ptl->tier_flag)
113         hvcc->general_level_idc = ptl->level_idc;
114     else
115         hvcc->general_level_idc = MAX(hvcc->general_level_idc, ptl->level_idc);
116 
117     /*
118      * The tier indication general_tier_flag must indicate a tier equal to or
119      * greater than the highest tier indicated in all the parameter sets.
120      */
121     hvcc->general_tier_flag = MAX(hvcc->general_tier_flag, ptl->tier_flag);
122 
123     /*
124      * The profile indication general_profile_idc must indicate a profile to
125      * which the stream associated with this configuration record conforms.
126      *
127      * If the sequence parameter sets are marked with different profiles, then
128      * the stream may need examination to determine which profile, if any, the
129      * entire stream conforms to. If the entire stream is not examined, or the
130      * examination reveals that there is no profile to which the entire stream
131      * conforms, then the entire stream must be split into two or more
132      * sub-streams with separate configuration records in which these rules can
133      * be met.
134      *
135      * Note: set the profile to the highest value for the sake of simplicity.
136      */
137     hvcc->general_profile_idc = MAX(hvcc->general_profile_idc, ptl->profile_idc);
138 
139     /*
140      * Each bit in general_profile_compatibility_flags may only be set if all
141      * the parameter sets set that bit.
142      */
143     hvcc->general_profile_compatibility_flags &= ptl->profile_compatibility_flags;
144 
145     /*
146      * Each bit in general_constraint_indicator_flags may only be set if all
147      * the parameter sets set that bit.
148      */
149     hvcc->general_constraint_indicator_flags &= ptl->constraint_indicator_flags;
150 }
151 
hvcc_parse_ptl(bitstream_t * gb,HEVCDecoderConfigurationRecord * hvcc,unsigned int max_sub_layers)152 static int hvcc_parse_ptl(bitstream_t *gb,
153                           HEVCDecoderConfigurationRecord *hvcc,
154                           unsigned int max_sub_layers)
155 {
156     unsigned int i;
157     HVCCProfileTierLevel general_ptl;
158     uint8_t sub_layer_profile_present_flag[MAX_SUB_LAYERS];
159     uint8_t sub_layer_level_present_flag[MAX_SUB_LAYERS];
160 
161     if (remaining_bits(gb) < 2+1+5 + 32 + 4 + 16 + 16 + 12)
162       return -1;
163 
164     general_ptl.profile_space               = read_bits(gb, 2);
165     general_ptl.tier_flag                   = read_bits1(gb);
166     general_ptl.profile_idc                 = read_bits(gb, 5);
167     general_ptl.profile_compatibility_flags = read_bits(gb, 32);
168     if (general_ptl.profile_idc == 0) {
169       for (i = 0; i < 32; i++)
170         if (general_ptl.profile_compatibility_flags & (1 << (31-i)))
171           general_ptl.profile_idc = i;
172     }
173     general_ptl.constraint_indicator_flags  = read_bits64(gb, 48);
174 
175     if (remaining_bits(gb) < 8 + (8*2 * (max_sub_layers - 1 > 0)))
176       return -1;
177 
178     general_ptl.level_idc                   = read_bits(gb, 8);
179     if (hvcc)
180         hvcc_update_ptl(hvcc, &general_ptl);
181 
182     for (i = 0; i < max_sub_layers - 1; i++) {
183         sub_layer_profile_present_flag[i] = read_bits1(gb);
184         sub_layer_level_present_flag[i]   = read_bits1(gb);
185     }
186 
187     if (max_sub_layers - 1 > 0)
188         for (i = max_sub_layers - 1; i < 8; i++)
189             skip_bits(gb, 2); // reserved_zero_2bits[i]
190 
191     for (i = 0; i < max_sub_layers - 1; i++) {
192         if (sub_layer_profile_present_flag[i]) {
193             /*
194              * sub_layer_profile_space[i]                     u(2)
195              * sub_layer_tier_flag[i]                         u(1)
196              * sub_layer_profile_idc[i]                       u(5)
197              * sub_layer_profile_compatibility_flag[i][0..31] u(32)
198              * sub_layer_progressive_source_flag[i]           u(1)
199              * sub_layer_interlaced_source_flag[i]            u(1)
200              * sub_layer_non_packed_constraint_flag[i]        u(1)
201              * sub_layer_frame_only_constraint_flag[i]        u(1)
202              * sub_layer_reserved_zero_44bits[i]              u(44)
203              */
204             skip_bits(gb, 32);
205             skip_bits(gb, 32);
206             skip_bits(gb, 24);
207         }
208 
209         if (sub_layer_level_present_flag[i])
210             skip_bits(gb, 8);
211     }
212     return 0;
213 }
214 
skip_sub_layer_hrd_parameters(bitstream_t * gb,unsigned int cpb_cnt,uint8_t sub_pic_hrd_params_present_flag)215 static void skip_sub_layer_hrd_parameters(bitstream_t *gb,
216                                           unsigned int cpb_cnt,
217                                           uint8_t sub_pic_hrd_params_present_flag)
218 {
219     unsigned int i;
220 
221     for (i = 0; i < cpb_cnt; i++) {
222         read_golomb_ue(gb); // bit_rate_value_minus1
223         read_golomb_ue(gb); // cpb_size_value_minus1
224 
225         if (sub_pic_hrd_params_present_flag) {
226             read_golomb_ue(gb); // cpb_size_du_value_minus1
227             read_golomb_ue(gb); // bit_rate_du_value_minus1
228         }
229 
230         skip_bits1(gb); // cbr_flag
231     }
232 }
233 
skip_hrd_parameters(bitstream_t * gb,uint8_t cprms_present_flag,unsigned int max_sub_layers)234 static int skip_hrd_parameters(bitstream_t *gb, uint8_t cprms_present_flag,
235                                 unsigned int max_sub_layers)
236 {
237     unsigned int i;
238     uint8_t sub_pic_hrd_params_present_flag = 0;
239     uint8_t nal_hrd_parameters_present_flag = 0;
240     uint8_t vcl_hrd_parameters_present_flag = 0;
241 
242     if (cprms_present_flag) {
243         nal_hrd_parameters_present_flag = read_bits1(gb);
244         vcl_hrd_parameters_present_flag = read_bits1(gb);
245 
246         if (nal_hrd_parameters_present_flag ||
247             vcl_hrd_parameters_present_flag) {
248             sub_pic_hrd_params_present_flag = read_bits1(gb);
249 
250             if (sub_pic_hrd_params_present_flag)
251                 /*
252                  * tick_divisor_minus2                          u(8)
253                  * du_cpb_removal_delay_increment_length_minus1 u(5)
254                  * sub_pic_cpb_params_in_pic_timing_sei_flag    u(1)
255                  * dpb_output_delay_du_length_minus1            u(5)
256                  */
257                 skip_bits(gb, 19);
258 
259             /*
260              * bit_rate_scale u(4)
261              * cpb_size_scale u(4)
262              */
263             skip_bits(gb, 8);
264 
265             if (sub_pic_hrd_params_present_flag)
266                 skip_bits(gb, 4); // cpb_size_du_scale
267 
268             /*
269              * initial_cpb_removal_delay_length_minus1 u(5)
270              * au_cpb_removal_delay_length_minus1      u(5)
271              * dpb_output_delay_length_minus1          u(5)
272              */
273             skip_bits(gb, 15);
274         }
275     }
276 
277     for (i = 0; i < max_sub_layers; i++) {
278         unsigned int cpb_cnt                   = 0;
279         uint8_t low_delay_hrd_flag             = 0;
280         uint8_t fixed_pic_rate_within_cvs_flag = 0;
281         uint8_t fixed_pic_rate_general_flag    = read_bits1(gb);
282 
283         if (!fixed_pic_rate_general_flag)
284             fixed_pic_rate_within_cvs_flag = read_bits1(gb);
285 
286         if (fixed_pic_rate_within_cvs_flag)
287             read_golomb_ue(gb); // elemental_duration_in_tc_minus1
288         else
289             low_delay_hrd_flag = read_bits1(gb);
290 
291         if (!low_delay_hrd_flag) {
292             cpb_cnt = read_golomb_ue(gb) + 1;
293             if (cpb_cnt > 32)
294                 return -1;
295         }
296 
297         if (nal_hrd_parameters_present_flag)
298             skip_sub_layer_hrd_parameters(gb, cpb_cnt, sub_pic_hrd_params_present_flag);
299 
300         if (vcl_hrd_parameters_present_flag)
301             skip_sub_layer_hrd_parameters(gb, cpb_cnt, sub_pic_hrd_params_present_flag);
302     }
303 
304     return 0;
305 }
306 
skip_timing_info(bitstream_t * gb)307 static void skip_timing_info(bitstream_t *gb)
308 {
309     skip_bits(gb, 32); // num_units_in_tick
310     skip_bits(gb, 32); // time_scale
311 
312     if (read_bits1(gb))          // poc_proportional_to_timing_flag
313         read_golomb_ue(gb); // num_ticks_poc_diff_one_minus1
314 }
315 
hvcc_parse_vui(bitstream_t * gb,HEVCDecoderConfigurationRecord * hvcc,unsigned int max_sub_layers)316 static void hvcc_parse_vui(bitstream_t *gb,
317                            HEVCDecoderConfigurationRecord *hvcc,
318                            unsigned int max_sub_layers)
319 {
320     unsigned int min_spatial_segmentation_idc;
321 
322     if (read_bits1(gb))              // aspect_ratio_info_present_flag
323         if (read_bits(gb, 8) == 255) // aspect_ratio_idc
324             skip_bits(gb, 32); // sar_width u(16), sar_height u(16)
325 
326     if (read_bits1(gb))  // overscan_info_present_flag
327         skip_bits1(gb); // overscan_appropriate_flag
328 
329     if (read_bits1(gb)) {  // video_signal_type_present_flag
330         skip_bits(gb, 4); // video_format u(3), video_full_range_flag u(1)
331 
332         if (read_bits1(gb)) // colour_description_present_flag
333             /*
334              * colour_primaries         u(8)
335              * transfer_characteristics u(8)
336              * matrix_coeffs            u(8)
337              */
338             skip_bits(gb, 24);
339     }
340 
341     if (read_bits1(gb)) {        // chroma_loc_info_present_flag
342         read_golomb_ue(gb); // chroma_sample_loc_type_top_field
343         read_golomb_ue(gb); // chroma_sample_loc_type_bottom_field
344     }
345 
346     /*
347      * neutral_chroma_indication_flag u(1)
348      * field_seq_flag                 u(1)
349      * frame_field_info_present_flag  u(1)
350      */
351     skip_bits(gb, 3);
352 
353     if (read_bits1(gb)) {        // default_display_window_flag
354         read_golomb_ue(gb); // def_disp_win_left_offset
355         read_golomb_ue(gb); // def_disp_win_right_offset
356         read_golomb_ue(gb); // def_disp_win_top_offset
357         read_golomb_ue(gb); // def_disp_win_bottom_offset
358     }
359 
360     if (read_bits1(gb)) { // vui_timing_info_present_flag
361         skip_timing_info(gb);
362 
363         if (read_bits1(gb)) // vui_hrd_parameters_present_flag
364             skip_hrd_parameters(gb, 1, max_sub_layers);
365     }
366 
367     if (read_bits1(gb)) { // bitstream_restriction_flag
368         /*
369          * tiles_fixed_structure_flag              u(1)
370          * motion_vectors_over_pic_boundaries_flag u(1)
371          * restricted_ref_pic_lists_flag           u(1)
372          */
373         skip_bits(gb, 3);
374 
375         min_spatial_segmentation_idc = read_golomb_ue(gb);
376 
377         /*
378          * unsigned int(12) min_spatial_segmentation_idc;
379          *
380          * The min_spatial_segmentation_idc indication must indicate a level of
381          * spatial segmentation equal to or less than the lowest level of
382          * spatial segmentation indicated in all the parameter sets.
383          */
384         hvcc->min_spatial_segmentation_idc = MIN(hvcc->min_spatial_segmentation_idc,
385                                                  min_spatial_segmentation_idc);
386 
387         read_golomb_ue(gb); // max_bytes_per_pic_denom
388         read_golomb_ue(gb); // max_bits_per_min_cu_denom
389         read_golomb_ue(gb); // log2_max_mv_length_horizontal
390         read_golomb_ue(gb); // log2_max_mv_length_vertical
391     }
392 }
393 
skip_sub_layer_ordering_info(bitstream_t * gb)394 static void skip_sub_layer_ordering_info(bitstream_t *gb)
395 {
396     read_golomb_ue(gb); // max_dec_pic_buffering_minus1
397     read_golomb_ue(gb); // max_num_reorder_pics
398     read_golomb_ue(gb); // max_latency_increase_plus1
399 }
400 
hvcc_parse_vps(bitstream_t * gb,HEVCDecoderConfigurationRecord * hvcc)401 static int hvcc_parse_vps(bitstream_t *gb,
402                           HEVCDecoderConfigurationRecord *hvcc)
403 {
404     unsigned int vps_max_sub_layers;
405 
406     /*
407      * vps_video_parameter_set_id u(4)
408      * vps_reserved_three_2bits   u(2)
409      * vps_max_layers_minus1      u(6)
410      */
411     skip_bits(gb, 12);
412 
413     vps_max_sub_layers = read_bits(gb, 3) + 1;
414 
415     /*
416      * numTemporalLayers greater than 1 indicates that the stream to which this
417      * configuration record applies is temporally scalable and the contained
418      * number of temporal layers (also referred to as temporal sub-layer or
419      * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
420      * indicates that the stream is not temporally scalable. Value 0 indicates
421      * that it is unknown whether the stream is temporally scalable.
422      */
423     hvcc->numTemporalLayers = MAX(hvcc->numTemporalLayers, vps_max_sub_layers);
424 
425     /*
426      * vps_temporal_id_nesting_flag u(1)
427      * vps_reserved_0xffff_16bits   u(16)
428      */
429     skip_bits(gb, 17);
430 
431     if (hvcc_parse_ptl(gb, hvcc, vps_max_sub_layers))
432       return -1;
433 
434     /* nothing useful for hvcC past this point */
435     return 0;
436 }
437 
skip_scaling_list_data(bitstream_t * gb)438 static void skip_scaling_list_data(bitstream_t *gb)
439 {
440     int i, j, k, num_coeffs;
441 
442     for (i = 0; i < 4; i++)
443         for (j = 0; j < (i == 3 ? 2 : 6); j++)
444             if (!read_bits1(gb))         // scaling_list_pred_mode_flag[i][j]
445                 read_golomb_ue(gb); // scaling_list_pred_matrix_id_delta[i][j]
446             else {
447                 num_coeffs = MIN(64, 1 << (4 + (i << 1)));
448 
449                 if (i > 1)
450                     read_golomb_se(gb); // scaling_list_dc_coef_minus8[i-2][j]
451 
452                 for (k = 0; k < num_coeffs; k++)
453                     read_golomb_se(gb); // scaling_list_delta_coef
454             }
455 }
456 
parse_rps(bitstream_t * gb,unsigned int rps_idx,unsigned int num_rps,unsigned int num_delta_pocs[MAX_SHORT_TERM_RPS_COUNT])457 static int parse_rps(bitstream_t *gb, unsigned int rps_idx,
458                      unsigned int num_rps,
459                      unsigned int num_delta_pocs[MAX_SHORT_TERM_RPS_COUNT])
460 {
461     unsigned int i;
462 
463     if (rps_idx && read_bits1(gb)) { // inter_ref_pic_set_prediction_flag
464         /* this should only happen for slice headers, and this isn't one */
465         if (rps_idx >= num_rps)
466             return -1;
467 
468         skip_bits1        (gb); // delta_rps_sign
469         read_golomb_ue(gb); // abs_delta_rps_minus1
470 
471         num_delta_pocs[rps_idx] = 0;
472 
473         /*
474          * From libavcodec/hevc_ps.c:
475          *
476          * if (is_slice_header) {
477          *    //foo
478          * } else
479          *     rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
480          *
481          * where:
482          * rps:             &sps->st_rps[rps_idx]
483          * sps->st_rps:     &sps->st_rps[0]
484          * is_slice_header: rps_idx == num_rps
485          *
486          * thus:
487          * if (num_rps != rps_idx)
488          *     rps_ridx = &sps->st_rps[rps_idx - 1];
489          *
490          * NumDeltaPocs[RefRpsIdx]: num_delta_pocs[rps_idx - 1]
491          */
492         for (i = 0; i <= num_delta_pocs[rps_idx - 1]; i++) {
493             uint8_t use_delta_flag = 0;
494             uint8_t used_by_curr_pic_flag = read_bits1(gb);
495             if (!used_by_curr_pic_flag)
496                 use_delta_flag = read_bits1(gb);
497 
498             if (used_by_curr_pic_flag || use_delta_flag)
499                 num_delta_pocs[rps_idx]++;
500         }
501     } else {
502         unsigned int num_negative_pics = read_golomb_ue(gb);
503         unsigned int num_positive_pics = read_golomb_ue(gb);
504 
505         if ((num_positive_pics + (uint64_t)num_negative_pics) * 2 > remaining_bits(gb))
506             return -1;
507 
508         num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics;
509 
510         for (i = 0; i < num_negative_pics; i++) {
511             read_golomb_ue(gb); // delta_poc_s0_minus1[rps_idx]
512             skip_bits1        (gb); // used_by_curr_pic_s0_flag[rps_idx]
513         }
514 
515         for (i = 0; i < num_positive_pics; i++) {
516             read_golomb_ue(gb); // delta_poc_s1_minus1[rps_idx]
517             skip_bits1        (gb); // used_by_curr_pic_s1_flag[rps_idx]
518         }
519     }
520 
521     return 0;
522 }
523 
hvcc_parse_sps(bitstream_t * gb,HEVCDecoderConfigurationRecord * hvcc)524 static int hvcc_parse_sps(bitstream_t *gb,
525                           HEVCDecoderConfigurationRecord *hvcc)
526 {
527     unsigned int i, sps_max_sub_layers, log2_max_pic_order_cnt_lsb_minus4;
528     unsigned int num_short_term_ref_pic_sets, num_delta_pocs[MAX_SHORT_TERM_RPS_COUNT];
529 
530     skip_bits(gb, 4); // sps_video_parameter_set_id
531 
532     sps_max_sub_layers = read_bits (gb, 3) + 1;
533 
534     /*
535      * numTemporalLayers greater than 1 indicates that the stream to which this
536      * configuration record applies is temporally scalable and the contained
537      * number of temporal layers (also referred to as temporal sub-layer or
538      * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
539      * indicates that the stream is not temporally scalable. Value 0 indicates
540      * that it is unknown whether the stream is temporally scalable.
541      */
542     hvcc->numTemporalLayers = MAX(hvcc->numTemporalLayers, sps_max_sub_layers);
543 
544     hvcc->temporalIdNested = read_bits1(gb);
545 
546     hvcc_parse_ptl(gb, hvcc, sps_max_sub_layers);
547 
548     read_golomb_ue(gb); // sps_seq_parameter_set_id
549 
550     hvcc->chromaFormat = read_golomb_ue(gb);
551 
552     if (hvcc->chromaFormat == 3)
553         skip_bits1(gb); // separate_colour_plane_flag
554 
555     read_golomb_ue(gb); // pic_width_in_luma_samples
556     read_golomb_ue(gb); // pic_height_in_luma_samples
557 
558     if (read_bits1(gb)) {   // conformance_window_flag
559         read_golomb_ue(gb); // conf_win_left_offset
560         read_golomb_ue(gb); // conf_win_right_offset
561         read_golomb_ue(gb); // conf_win_top_offset
562         read_golomb_ue(gb); // conf_win_bottom_offset
563     }
564 
565     hvcc->bitDepthLumaMinus8          = read_golomb_ue(gb);
566     hvcc->bitDepthChromaMinus8        = read_golomb_ue(gb);
567     log2_max_pic_order_cnt_lsb_minus4 = read_golomb_ue(gb);
568 
569     /* sps_sub_layer_ordering_info_present_flag */
570     i = read_bits1(gb) ? 1 : sps_max_sub_layers;
571     for (; i < sps_max_sub_layers; i++)
572         skip_sub_layer_ordering_info(gb);
573 
574     read_golomb_ue(gb); // log2_min_luma_coding_block_size_minus3
575     read_golomb_ue(gb); // log2_diff_max_min_luma_coding_block_size
576     read_golomb_ue(gb); // log2_min_transform_block_size_minus2
577     read_golomb_ue(gb); // log2_diff_max_min_transform_block_size
578     read_golomb_ue(gb); // max_transform_hierarchy_depth_inter
579     read_golomb_ue(gb); // max_transform_hierarchy_depth_intra
580 
581     if (read_bits1(gb) && // scaling_list_enabled_flag
582         read_bits1(gb))   // sps_scaling_list_data_present_flag
583         skip_scaling_list_data(gb);
584 
585     skip_bits1(gb); // amp_enabled_flag
586     skip_bits1(gb); // sample_adaptive_offset_enabled_flag
587 
588     if (read_bits1(gb)) {           // pcm_enabled_flag
589         skip_bits         (gb, 4); // pcm_sample_bit_depth_luma_minus1
590         skip_bits         (gb, 4); // pcm_sample_bit_depth_chroma_minus1
591         read_golomb_ue(gb);    // log2_min_pcm_luma_coding_block_size_minus3
592         read_golomb_ue(gb);    // log2_diff_max_min_pcm_luma_coding_block_size
593         skip_bits1        (gb);    // pcm_loop_filter_disabled_flag
594     }
595 
596     num_short_term_ref_pic_sets = read_golomb_ue(gb);
597     if (num_short_term_ref_pic_sets > MAX_SHORT_TERM_RPS_COUNT)
598         return -1;
599 
600     for (i = 0; i < num_short_term_ref_pic_sets; i++) {
601         int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, num_delta_pocs);
602         if (ret < 0)
603             return ret;
604     }
605 
606     if (read_bits1(gb)) {                               // long_term_ref_pics_present_flag
607         unsigned num_long_term_ref_pics_sps = read_golomb_ue(gb);
608         if (num_long_term_ref_pics_sps > 31U)
609             return -1;
610         for (i = 0; i < num_long_term_ref_pics_sps; i++) { // num_long_term_ref_pics_sps
611             int len = MIN(log2_max_pic_order_cnt_lsb_minus4 + 4, 16);
612             skip_bits (gb, len); // lt_ref_pic_poc_lsb_sps[i]
613             skip_bits1(gb);      // used_by_curr_pic_lt_sps_flag[i]
614         }
615     }
616 
617     skip_bits1(gb); // sps_temporal_mvp_enabled_flag
618     skip_bits1(gb); // strong_intra_smoothing_enabled_flag
619 
620     if (read_bits1(gb)) // vui_parameters_present_flag
621         hvcc_parse_vui(gb, hvcc, sps_max_sub_layers);
622 
623     /* nothing useful for hvcC past this point */
624     return 0;
625 }
626 
hvcc_parse_pps(bitstream_t * gb,HEVCDecoderConfigurationRecord * hvcc)627 static int hvcc_parse_pps(bitstream_t *gb,
628                           HEVCDecoderConfigurationRecord *hvcc)
629 {
630     uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag;
631 
632     read_golomb_ue(gb); // pps_pic_parameter_set_id
633     read_golomb_ue(gb); // pps_seq_parameter_set_id
634 
635     /*
636      * dependent_slice_segments_enabled_flag u(1)
637      * output_flag_present_flag              u(1)
638      * num_extra_slice_header_bits           u(3)
639      * sign_data_hiding_enabled_flag         u(1)
640      * cabac_init_present_flag               u(1)
641      */
642     skip_bits(gb, 7);
643 
644     read_golomb_ue(gb); // num_ref_idx_l0_default_active_minus1
645     read_golomb_ue(gb); // num_ref_idx_l1_default_active_minus1
646     read_golomb_se(gb); // init_qp_minus26
647 
648     /*
649      * constrained_intra_pred_flag u(1)
650      * transform_skip_enabled_flag u(1)
651      */
652     skip_bits(gb, 2);
653 
654     if (read_bits1(gb))          // cu_qp_delta_enabled_flag
655         read_golomb_ue(gb); // diff_cu_qp_delta_depth
656 
657     read_golomb_se(gb); // pps_cb_qp_offset
658     read_golomb_se(gb); // pps_cr_qp_offset
659 
660     /*
661      * pps_slice_chroma_qp_offsets_present_flag u(1)
662      * weighted_pred_flag               u(1)
663      * weighted_bipred_flag             u(1)
664      * transquant_bypass_enabled_flag   u(1)
665      */
666     skip_bits(gb, 4);
667 
668     tiles_enabled_flag               = read_bits1(gb);
669     entropy_coding_sync_enabled_flag = read_bits1(gb);
670 
671     if (entropy_coding_sync_enabled_flag && tiles_enabled_flag)
672         hvcc->parallelismType = 0; // mixed-type parallel decoding
673     else if (entropy_coding_sync_enabled_flag)
674         hvcc->parallelismType = 3; // wavefront-based parallel decoding
675     else if (tiles_enabled_flag)
676         hvcc->parallelismType = 2; // tile-based parallel decoding
677     else
678         hvcc->parallelismType = 1; // slice-based parallel decoding
679 
680     /* nothing useful for hvcC past this point */
681     return 0;
682 }
683 
nal_unit_extract_rbsp(const uint8_t * src,uint32_t src_len,uint32_t * dst_len)684 static uint8_t *nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
685                                       uint32_t *dst_len)
686 {
687     uint8_t *dst;
688     uint32_t i, len;
689 
690     dst = malloc(src_len);
691     if (!dst)
692         return NULL;
693 
694     /* NAL unit header (2 bytes) */
695     i = len = 0;
696     while (i < 2 && i < src_len)
697         dst[len++] = src[i++];
698 
699     while (i + 2 < src_len)
700         if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
701             dst[len++] = src[i++];
702             dst[len++] = src[i++];
703             i++; // remove emulation_prevention_three_byte
704         } else
705             dst[len++] = src[i++];
706 
707     while (i < src_len)
708         dst[len++] = src[i++];
709 
710     *dst_len = len;
711     return dst;
712 }
713 
714 
715 
nal_unit_parse_header(bitstream_t * gb,uint8_t * nal_type)716 static void nal_unit_parse_header(bitstream_t *gb, uint8_t *nal_type)
717 {
718     skip_bits1(gb); // forbidden_zero_bit
719 
720     *nal_type = read_bits(gb, 6);
721 
722     /*
723      * nuh_layer_id          u(6)
724      * nuh_temporal_id_plus1 u(3)
725      */
726     skip_bits(gb, 9);
727 }
728 
hvcc_array_add_nal_unit(uint8_t * nal_buf,uint32_t nal_size,uint8_t nal_type,HEVCDecoderConfigurationRecord * hvcc)729 static int hvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
730                                    uint8_t nal_type,
731                                    HEVCDecoderConfigurationRecord *hvcc)
732 {
733     uint8_t index;
734     uint16_t numNalus;
735     HVCCNALUnitArray *array;
736     void *n;
737 
738     for (index = 0; index < hvcc->numOfArrays; index++)
739         if (hvcc->array[index].NAL_unit_type == nal_type)
740             break;
741 
742     if (index >= hvcc->numOfArrays) {
743         uint8_t i;
744 
745         n = realloc(hvcc->array, (index + 1) * sizeof(HVCCNALUnitArray));
746         if (n == NULL)
747             return -1;
748         hvcc->array = n;
749 
750         for (i = hvcc->numOfArrays; i <= index; i++)
751             memset(&hvcc->array[i], 0, sizeof(HVCCNALUnitArray));
752         hvcc->numOfArrays = index + 1;
753     }
754 
755     array    = &hvcc->array[index];
756     numNalus = array->numNalus;
757 
758     n = realloc(array->nalUnit, (numNalus + 1) * sizeof(uint8_t*));
759     if (n == NULL)
760         return -1;
761     array->nalUnit = n;
762 
763     n = realloc(array->nalUnitLength, (numNalus + 1) * sizeof(uint16_t));
764     if (n == NULL)
765         return -1;
766     array->nalUnitLength = n;
767 
768     array->nalUnit      [numNalus] = nal_buf;
769     array->nalUnitLength[numNalus] = nal_size;
770     array->NAL_unit_type           = nal_type;
771     array->numNalus++;
772 
773     return 0;
774 }
775 
hvcc_add_nal_unit(uint8_t * nal_buf,uint32_t nal_size,HEVCDecoderConfigurationRecord * hvcc)776 static int hvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
777                              HEVCDecoderConfigurationRecord *hvcc)
778 {
779     int ret = 0;
780     bitstream_t bs;
781     uint8_t nal_type;
782     uint8_t *rbsp_buf;
783     uint32_t rbsp_size;
784 
785     rbsp_buf = nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size);
786     if (!rbsp_buf) {
787         ret = -1;
788         goto end;
789     }
790 
791     ret = init_rbits(&bs, rbsp_buf, rbsp_size);
792     if (ret < 0)
793         goto end;
794 
795     nal_unit_parse_header(&bs, &nal_type);
796 
797     /*
798      * Note: only 'declarative' SEI messages are allowed in
799      * hvcC. Perhaps the SEI playload type should be checked
800      * and non-declarative SEI messages discarded?
801      */
802     switch (nal_type) {
803     case HEVC_NAL_VPS:
804     case HEVC_NAL_SPS:
805     case HEVC_NAL_PPS:
806     case HEVC_NAL_SEI_PREFIX:
807     case HEVC_NAL_SEI_SUFFIX:
808         ret = hvcc_array_add_nal_unit(nal_buf, nal_size, nal_type, hvcc);
809         if (ret < 0)
810             goto end;
811         else if (nal_type == HEVC_NAL_VPS)
812             ret = hvcc_parse_vps(&bs, hvcc);
813         else if (nal_type == HEVC_NAL_SPS)
814             ret = hvcc_parse_sps(&bs, hvcc);
815         else if (nal_type == HEVC_NAL_PPS)
816             ret = hvcc_parse_pps(&bs, hvcc);
817         if (ret < 0)
818             goto end;
819         break;
820     default:
821         ret = -1;
822         goto end;
823     }
824 
825 end:
826     free(rbsp_buf);
827     return ret;
828 }
829 
hvcc_init(HEVCDecoderConfigurationRecord * hvcc)830 static void hvcc_init(HEVCDecoderConfigurationRecord *hvcc)
831 {
832     memset(hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
833     hvcc->configurationVersion = 1;
834     hvcc->lengthSizeMinusOne   = 3; // 4 bytes
835 
836     /*
837      * The following fields have all their valid bits set by default,
838      * the ProfileTierLevel parsing code will unset them when needed.
839      */
840     hvcc->general_profile_compatibility_flags = 0xffffffff;
841     hvcc->general_constraint_indicator_flags  = 0xffffffffffffLL;
842 
843     /*
844      * Initialize this field with an invalid value which can be used to detect
845      * whether we didn't see any VUI (in which case it should be reset to zero).
846      */
847     hvcc->min_spatial_segmentation_idc = MAX_SPATIAL_SEGMENTATION + 1;
848 }
849 
hvcc_close(HEVCDecoderConfigurationRecord * hvcc)850 static void hvcc_close(HEVCDecoderConfigurationRecord *hvcc)
851 {
852     uint8_t i;
853 
854     for (i = 0; i < hvcc->numOfArrays; i++) {
855         hvcc->array[i].numNalus = 0;
856         free(hvcc->array[i].nalUnit);
857         hvcc->array[i].nalUnit = NULL;
858         free(hvcc->array[i].nalUnitLength);
859         hvcc->array[i].nalUnitLength = NULL;
860     }
861 
862     hvcc->numOfArrays = 0;
863     free(hvcc->array);
864     hvcc->array = NULL;
865 }
866 
hvcc_write(sbuf_t * pb,HEVCDecoderConfigurationRecord * hvcc)867 static int hvcc_write(sbuf_t *pb, HEVCDecoderConfigurationRecord *hvcc)
868 {
869     uint8_t i;
870     uint16_t j, vps_count = 0, sps_count = 0, pps_count = 0;
871 
872     /*
873      * We only support writing HEVCDecoderConfigurationRecord version 1.
874      */
875     hvcc->configurationVersion = 1;
876 
877     /*
878      * If min_spatial_segmentation_idc is invalid, reset to 0 (unspecified).
879      */
880     if (hvcc->min_spatial_segmentation_idc > MAX_SPATIAL_SEGMENTATION)
881         hvcc->min_spatial_segmentation_idc = 0;
882 
883     /*
884      * parallelismType indicates the type of parallelism that is used to meet
885      * the restrictions imposed by min_spatial_segmentation_idc when the value
886      * of min_spatial_segmentation_idc is greater than 0.
887      */
888     if (!hvcc->min_spatial_segmentation_idc)
889         hvcc->parallelismType = 0;
890 
891     /*
892      * It's unclear how to properly compute these fields, so
893      * let's always set them to values meaning 'unspecified'.
894      */
895     hvcc->avgFrameRate      = 0;
896     hvcc->constantFrameRate = 0;
897 
898     tvhtrace(LS_HEVC, "configurationVersion:                %"PRIu8,
899             hvcc->configurationVersion);
900     tvhtrace(LS_HEVC, "general_profile_space:               %"PRIu8,
901             hvcc->general_profile_space);
902     tvhtrace(LS_HEVC,  "general_tier_flag:                   %"PRIu8,
903             hvcc->general_tier_flag);
904     tvhtrace(LS_HEVC,  "general_profile_idc:                 %"PRIu8,
905             hvcc->general_profile_idc);
906     tvhtrace(LS_HEVC, "general_profile_compatibility_flags: 0x%08"PRIx32,
907             hvcc->general_profile_compatibility_flags);
908     tvhtrace(LS_HEVC, "general_constraint_indicator_flags:  0x%012"PRIx64,
909             hvcc->general_constraint_indicator_flags);
910     tvhtrace(LS_HEVC,  "general_level_idc:                   %"PRIu8,
911             hvcc->general_level_idc);
912     tvhtrace(LS_HEVC,  "min_spatial_segmentation_idc:        %"PRIu16,
913             hvcc->min_spatial_segmentation_idc);
914     tvhtrace(LS_HEVC,  "parallelismType:                     %"PRIu8,
915             hvcc->parallelismType);
916     tvhtrace(LS_HEVC,  "chromaFormat:                        %"PRIu8,
917             hvcc->chromaFormat);
918     tvhtrace(LS_HEVC,  "bitDepthLumaMinus8:                  %"PRIu8,
919             hvcc->bitDepthLumaMinus8);
920     tvhtrace(LS_HEVC,  "bitDepthChromaMinus8:                %"PRIu8,
921             hvcc->bitDepthChromaMinus8);
922     tvhtrace(LS_HEVC,  "avgFrameRate:                        %"PRIu16,
923             hvcc->avgFrameRate);
924     tvhtrace(LS_HEVC,  "constantFrameRate:                   %"PRIu8,
925             hvcc->constantFrameRate);
926     tvhtrace(LS_HEVC,  "numTemporalLayers:                   %"PRIu8,
927             hvcc->numTemporalLayers);
928     tvhtrace(LS_HEVC,  "temporalIdNested:                    %"PRIu8,
929             hvcc->temporalIdNested);
930     tvhtrace(LS_HEVC,  "lengthSizeMinusOne:                  %"PRIu8,
931             hvcc->lengthSizeMinusOne);
932     tvhtrace(LS_HEVC,  "numOfArrays:                         %"PRIu8,
933             hvcc->numOfArrays);
934     for (i = 0; i < hvcc->numOfArrays; i++) {
935         tvhtrace(LS_HEVC, "NAL_unit_type[%"PRIu8"]:                    %"PRIu8,
936                 i, hvcc->array[i].NAL_unit_type);
937         tvhtrace(LS_HEVC, "numNalus[%"PRIu8"]:                         %"PRIu16,
938                 i, hvcc->array[i].numNalus);
939         for (j = 0; j < hvcc->array[i].numNalus; j++)
940             tvhtrace(LS_HEVC,
941                     "nalUnitLength[%"PRIu8"][%"PRIu16"]:                 %"PRIu16,
942                     i, j, hvcc->array[i].nalUnitLength[j]);
943     }
944 
945     /*
946      * We need at least one of each: VPS, SPS and PPS.
947      */
948     for (i = 0; i < hvcc->numOfArrays; i++)
949         switch (hvcc->array[i].NAL_unit_type) {
950         case HEVC_NAL_VPS:
951             vps_count += hvcc->array[i].numNalus;
952             break;
953         case HEVC_NAL_SPS:
954             sps_count += hvcc->array[i].numNalus;
955             break;
956         case HEVC_NAL_PPS:
957             pps_count += hvcc->array[i].numNalus;
958             break;
959         default:
960             break;
961         }
962     if (!vps_count || vps_count > MAX_VPS_COUNT ||
963         !sps_count || sps_count > MAX_SPS_COUNT ||
964         !pps_count || pps_count > MAX_PPS_COUNT)
965         return -1;
966 
967     /* unsigned int(8) configurationVersion = 1; */
968     sbuf_put_byte(pb, hvcc->configurationVersion);
969 
970     /*
971      * unsigned int(2) general_profile_space;
972      * unsigned int(1) general_tier_flag;
973      * unsigned int(5) general_profile_idc;
974      */
975     sbuf_put_byte(pb, hvcc->general_profile_space << 6 |
976                 hvcc->general_tier_flag     << 5 |
977                 hvcc->general_profile_idc);
978 
979     /* unsigned int(32) general_profile_compatibility_flags; */
980     sbuf_put_be32(pb, hvcc->general_profile_compatibility_flags);
981 
982     /* unsigned int(48) general_constraint_indicator_flags; */
983     sbuf_put_be32(pb, hvcc->general_constraint_indicator_flags >> 16);
984     sbuf_put_be16(pb, hvcc->general_constraint_indicator_flags);
985 
986     /* unsigned int(8) general_level_idc; */
987     sbuf_put_byte(pb, hvcc->general_level_idc);
988 
989     /*
990      * bit(4) reserved = ‘1111’b;
991      * unsigned int(12) min_spatial_segmentation_idc;
992      */
993     sbuf_put_be16(pb, hvcc->min_spatial_segmentation_idc | 0xf000);
994 
995     /*
996      * bit(6) reserved = ‘111111’b;
997      * unsigned int(2) parallelismType;
998      */
999     sbuf_put_byte(pb, hvcc->parallelismType | 0xfc);
1000 
1001     /*
1002      * bit(6) reserved = ‘111111’b;
1003      * unsigned int(2) chromaFormat;
1004      */
1005     sbuf_put_byte(pb, hvcc->chromaFormat | 0xfc);
1006 
1007     /*
1008      * bit(5) reserved = ‘11111’b;
1009      * unsigned int(3) bitDepthLumaMinus8;
1010      */
1011     sbuf_put_byte(pb, hvcc->bitDepthLumaMinus8 | 0xf8);
1012 
1013     /*
1014      * bit(5) reserved = ‘11111’b;
1015      * unsigned int(3) bitDepthChromaMinus8;
1016      */
1017     sbuf_put_byte(pb, hvcc->bitDepthChromaMinus8 | 0xf8);
1018 
1019     /* bit(16) avgFrameRate; */
1020     sbuf_put_be16(pb, hvcc->avgFrameRate);
1021 
1022     /*
1023      * bit(2) constantFrameRate;
1024      * bit(3) numTemporalLayers;
1025      * bit(1) temporalIdNested;
1026      * unsigned int(2) lengthSizeMinusOne;
1027      */
1028     sbuf_put_byte(pb, hvcc->constantFrameRate << 6 |
1029                   hvcc->numTemporalLayers << 3 |
1030                   hvcc->temporalIdNested  << 2 |
1031                   hvcc->lengthSizeMinusOne);
1032 
1033     /* unsigned int(8) numOfArrays; */
1034     sbuf_put_byte(pb, hvcc->numOfArrays);
1035 
1036     for (i = 0; i < hvcc->numOfArrays; i++) {
1037         /*
1038          * bit(1) array_completeness;
1039          * unsigned int(1) reserved = 0;
1040          * unsigned int(6) NAL_unit_type;
1041          */
1042         sbuf_put_byte(pb, (0 << 7) |
1043                       (hvcc->array[i].NAL_unit_type & 0x3f));
1044 
1045         /* unsigned int(16) numNalus; */
1046         sbuf_put_be16(pb, hvcc->array[i].numNalus);
1047 
1048         for (j = 0; j < hvcc->array[i].numNalus; j++) {
1049             /* unsigned int(16) nalUnitLength; */
1050             sbuf_put_be16(pb, hvcc->array[i].nalUnitLength[j]);
1051 
1052             /* bit(8*nalUnitLength) nalUnit; */
1053             sbuf_append(pb, hvcc->array[i].nalUnit[j],
1054                         hvcc->array[i].nalUnitLength[j]);
1055         }
1056     }
1057 
1058     return 0;
1059 }
1060 
1061 #if 0
1062 int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
1063                        int size, int filter_ps, int *ps_count)
1064 {
1065     int num_ps = 0, ret = 0;
1066     uint8_t *buf, *end, *start = NULL;
1067 
1068     if (!filter_ps) {
1069         ret = ff_avc_parse_nal_units(pb, buf_in, size);
1070         goto end;
1071     }
1072 
1073     ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
1074     if (ret < 0)
1075         goto end;
1076 
1077     ret = 0;
1078     buf = start;
1079     end = start + size;
1080 
1081     while (end - buf > 4) {
1082         uint32_t len = MIN(RB32(buf), end - buf - 4);
1083         uint8_t type = (buf[4] >> 1) & 0x3f;
1084 
1085         buf += 4;
1086 
1087         switch (type) {
1088         case HEVC_NAL_VPS:
1089         case HEVC_NAL_SPS:
1090         case HEVC_NAL_PPS:
1091             num_ps++;
1092             break;
1093         default:
1094             ret += 4 + len;
1095             sbuf_put_be32(pb, len);
1096             sbuf_append(pb, buf, len);
1097             break;
1098         }
1099 
1100         buf += len;
1101     }
1102 
1103 end:
1104     av_free(start);
1105     if (ps_count)
1106         *ps_count = num_ps;
1107     return ret;
1108 }
1109 
1110 int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
1111                            int *size, int filter_ps, int *ps_count)
1112 {
1113     AVIOContext *pb;
1114     int ret;
1115 
1116     ret = avio_open_dyn_buf(&pb);
1117     if (ret < 0)
1118         return ret;
1119 
1120     ret   = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
1121     *size = avio_close_dyn_buf(pb, buf_out);
1122 
1123     return ret;
1124 }
1125 #endif
1126 
isom_write_hvcc(sbuf_t * pb,const uint8_t * data,int size)1127 int isom_write_hvcc(sbuf_t *pb, const uint8_t *data, int size)
1128 {
1129     int ret = 0;
1130     sbuf_t sb;
1131     uint8_t *buf, *end;
1132     HEVCDecoderConfigurationRecord hvcc;
1133 
1134     sbuf_init(&sb);
1135     hvcc_init(&hvcc);
1136 
1137     if (size < 6) {
1138         /* We can't write a valid hvcC from the provided data */
1139         ret = -1;
1140         goto end;
1141     } else if (*data == 1) {
1142         /* Data is already hvcC-formatted */
1143         sbuf_append(pb, data, size);
1144         goto end;
1145     } else if (!(RB24(data) == 1 || RB32(data) == 1)) {
1146         /* Not a valid Annex B start code prefix */
1147         ret = -1;
1148         goto end;
1149     }
1150 
1151     size = avc_parse_nal_units(&sb, data, size);
1152 
1153     buf = sb.sb_data;
1154     end = buf + size;
1155 
1156     while (end - buf > 4) {
1157         uint32_t len = MIN(RB32(buf), end - buf - 4);
1158         uint8_t type = (buf[4] >> 1) & 0x3f;
1159 
1160         buf += 4;
1161 
1162         switch (type) {
1163         case HEVC_NAL_VPS:
1164         case HEVC_NAL_SPS:
1165         case HEVC_NAL_PPS:
1166         case HEVC_NAL_SEI_PREFIX:
1167         case HEVC_NAL_SEI_SUFFIX:
1168             ret = hvcc_add_nal_unit(buf, len, &hvcc);
1169             if (ret < 0)
1170                 goto end;
1171             break;
1172         default:
1173             break;
1174         }
1175 
1176         buf += len;
1177     }
1178 
1179     ret = hvcc_write(pb, &hvcc);
1180 
1181 end:
1182     hvcc_close(&hvcc);
1183     sbuf_free(&sb);
1184     return ret;
1185 }
1186 
1187 th_pkt_t *
hevc_convert_pkt(th_pkt_t * src)1188 hevc_convert_pkt(th_pkt_t *src)
1189 {
1190   sbuf_t payload;
1191   th_pkt_t *pkt = pkt_copy_nodata(src);
1192 
1193   sbuf_init(&payload);
1194 
1195   avc_parse_nal_units(&payload, pktbuf_ptr(src->pkt_payload),
1196                       pktbuf_len(src->pkt_payload));
1197 
1198   pkt->pkt_payload = pktbuf_make(payload.sb_data, payload.sb_ptr);
1199   return pkt;
1200 }
1201 
1202 /*
1203  *
1204  */
1205 
1206 typedef struct hevc_vps {
1207   uint8_t valid: 1;
1208   uint32_t num_units_in_tick;
1209   uint32_t time_scale;
1210 } hevc_vps_t;
1211 
1212 typedef struct hevc_vui {
1213   struct {
1214     uint32_t num;
1215     uint32_t den;
1216   } sar;
1217   uint32_t num_units_in_tick;
1218   uint32_t time_scale;
1219 } hevc_vui_t;
1220 
1221 typedef struct hevc_sps {
1222   uint8_t valid: 1;
1223   uint8_t vps_id;
1224   uint32_t width;
1225   uint32_t height;
1226   uint32_t ctb_width;
1227   uint32_t ctb_height;
1228   hevc_vui_t vui;
1229 } hevc_sps_t;
1230 
1231 typedef struct hevc_pps {
1232   uint8_t valid: 1;
1233   uint8_t dependent_slice_segments: 1;
1234   uint8_t sps_id;
1235   uint8_t num_extra_slice_header_bits;
1236 } hevc_pps_t;
1237 
1238 typedef struct hevc_private {
1239 
1240   hevc_vps_t vps[MAX_VPS_COUNT];
1241   hevc_sps_t sps[MAX_SPS_COUNT];
1242   hevc_pps_t pps[MAX_PPS_COUNT];
1243 
1244 } hevc_private_t;
1245 
1246 static const uint8_t vui_sar[][2] = {
1247   {  0,   1 },
1248   {  1,   1 },
1249   { 12,  11 },
1250   { 10,  11 },
1251   { 16,  11 },
1252   { 40,  33 },
1253   { 24,  11 },
1254   { 20,  11 },
1255   { 32,  11 },
1256   { 80,  33 },
1257   { 18,  11 },
1258   { 15,  11 },
1259   { 64,  33 },
1260   { 160, 99 },
1261   {  4,   3 },
1262   {  3,   2 },
1263   {  2,   1 },
1264 };
1265 
ilog2(uint32_t v)1266 static uint_fast32_t ilog2(uint32_t v)
1267 {
1268   uint_fast32_t r = 0;
1269   while (v) {
1270     v >>= 1;
1271     r++;
1272   }
1273   return r;
1274 }
1275 
check_width(uint32_t w)1276 static inline int check_width(uint32_t w)
1277 {
1278   return w < 100 || w > 16384;
1279 }
1280 
check_height(uint32_t h)1281 static inline int check_height(uint32_t h)
1282 {
1283   return h < 100 || h > 16384;
1284 }
1285 
1286 void
hevc_decode_vps(elementary_stream_t * st,bitstream_t * bs)1287 hevc_decode_vps(elementary_stream_t *st, bitstream_t *bs)
1288 {
1289   hevc_private_t *p;
1290   hevc_vps_t *vps;
1291   uint32_t vps_id, max_sub_layers;
1292   uint32_t sub_layer_ordering_info_present;
1293   uint32_t max_layer_id, num_layer_sets;
1294   uint32_t u, v;
1295 
1296   if (read_bits1(bs)) /* zero bit */
1297     return ;
1298 
1299   if((p = st->es_priv) == NULL)
1300     p = st->es_priv = calloc(1, sizeof(hevc_private_t));
1301 
1302   skip_bits(bs, 15);  /* NAL type, Layer ID, Temporal ID */
1303 
1304   vps_id = read_bits(bs, 4);
1305   if (vps_id >= MAX_VPS_COUNT)
1306     return;
1307   vps = &p->vps[vps_id];
1308 
1309   if (read_bits(bs, 2) != 3) /* vps_reserved_three_2bits */
1310     return;
1311 
1312   skip_bits(bs, 6); /* vps_max_sublayers */
1313   max_sub_layers = read_bits(bs, 3) + 1;
1314   skip_bits1(bs);   /* vps_temporal_id_nesting */
1315 
1316   if (read_bits(bs, 16) != 0xffff) /* reserved */
1317     return;
1318 
1319   if (max_sub_layers > MAX_SUB_LAYERS)
1320     return;
1321 
1322   hvcc_parse_ptl(bs, NULL, max_sub_layers);
1323 
1324   sub_layer_ordering_info_present = read_bits1(bs);
1325   u = sub_layer_ordering_info_present ? 0 : max_sub_layers - 1;
1326   for ( ; u < max_sub_layers; u++) {
1327     read_golomb_ue(bs); /* vps_max_dec_pic_buffering */
1328     read_golomb_ue(bs); /* vps_num_reorder_pics */
1329     read_golomb_ue(bs); /* vps_max_latency_increase */
1330   }
1331 
1332   max_layer_id   = read_bits(bs, 6); /* vps_max_layer_id */
1333   num_layer_sets = read_golomb_ue(bs) + 1;
1334   if (num_layer_sets < 1 || num_layer_sets > 1024 ||
1335       (num_layer_sets - 1LL) * (max_layer_id + 1LL) > remaining_bits(bs))
1336     return;
1337 
1338   for (u = 1; u < num_layer_sets; u++)
1339     for (v = 0; v < max_layer_id; v++)
1340       skip_bits1(bs);  /* layer_id_included */
1341 
1342   vps->valid = 1;
1343 
1344   if (!read_bits1(bs)) { /* vps_timing_info_present */
1345     vps->num_units_in_tick = read_bits(bs, 32);
1346     vps->time_scale        = read_bits(bs, 32);
1347   }
1348 }
1349 
1350 static int
hevc_decode_vui(hevc_vui_t * vui,bitstream_t * bs)1351 hevc_decode_vui(hevc_vui_t *vui, bitstream_t *bs)
1352 {
1353   uint32_t u, sar_num, sar_den;
1354   bitstream_t backup;
1355 
1356   sar_num = 1;
1357   sar_den = 1;
1358   if (read_bits1(bs)) { /* sar_present */
1359     u = read_bits(bs, 8);
1360     if (u < ARRAY_SIZE(vui_sar)) {
1361       sar_num = vui_sar[u][0];
1362       sar_den = vui_sar[u][1];
1363     } else if (u == 255) {
1364       sar_num = read_bits(bs, 16);
1365       sar_den = read_bits(bs, 16);
1366     } else
1367       return -1;
1368   }
1369   vui->sar.num = sar_num;
1370   vui->sar.den = sar_den;
1371 
1372   if (read_bits1(bs)) /* overscan_info_present */
1373     skip_bits1(bs);   /* overscan_appropriate */
1374 
1375   if (read_bits1(bs)) { /* video_signal_type_present */
1376     skip_bits(bs, 3);   /* video_format */
1377     skip_bits1(bs);     /* video_full_range */
1378     if (read_bits1(bs)) { /* colour_description_present */
1379       skip_bits(bs, 8); /* colour_primaries */
1380       skip_bits(bs, 8); /* transfer_characteristic */
1381       skip_bits(bs, 8); /* matrix_coeffs */
1382     }
1383   }
1384 
1385   if (read_bits1(bs)) { /* chroma_loc_info_present */
1386     read_golomb_ue(bs); /* chroma_sample_loc_type_top_field */
1387     read_golomb_ue(bs); /* chroma_sample_loc_type_bottom_field */
1388   }
1389 
1390   skip_bits1(bs); /* neutra_chroma_indication_flag */
1391   skip_bits1(bs); /* field_seq_flag */
1392   skip_bits1(bs); /* frame_field_info_present_flag */
1393 
1394   if (remaining_bits(bs) >= 68 && show_bits(bs, 21) == 0x100000)
1395     u = 0; /* Invalid default display window */
1396   else
1397     u = read_bits1(bs); /* default_display_window */
1398   /* Backup context in case an alternate header is detected */
1399   memcpy(&backup, bs, sizeof(backup));
1400   if (u) { /* default_display_window */
1401     read_golomb_ue(bs); /* left_offset */
1402     read_golomb_ue(bs); /* right_offset */
1403     read_golomb_ue(bs); /* top_offset */
1404     read_golomb_ue(bs); /* bottom_offset */
1405   }
1406 
1407   if (read_bits1(bs)) { /* vui_timing_info_present */
1408     if (remaining_bits(bs) < 66) {
1409       /* Strange VUI timing information */
1410       memcpy(bs, &backup, sizeof(backup));
1411     }
1412     vui->num_units_in_tick = read_bits(bs, 32);
1413     vui->time_scale        = read_bits(bs, 32);
1414   }
1415 
1416   return 0;
1417 }
1418 
1419 void
hevc_decode_sps(elementary_stream_t * st,bitstream_t * bs)1420 hevc_decode_sps(elementary_stream_t *st, bitstream_t *bs)
1421 {
1422   hevc_private_t *p;
1423   hevc_vps_t *vps;
1424   hevc_sps_t *sps;
1425   uint32_t vps_id, sps_id, max_sub_layers, u, v, i;
1426   uint32_t width, height;
1427   uint32_t chroma_format_idc, bit_depth, bit_depth_chroma;
1428   uint32_t log2_max_poc_lsb;
1429   uint32_t log2_min_cb_size, log2_min_tb_size;
1430   uint32_t log2_diff_max_min_coding_block_size;
1431   uint32_t log2_diff_max_min_transform_block_size;
1432   uint32_t log2_ctb_size, nb_st_rps;
1433 
1434   if (read_bits1(bs)) /* zero bit */
1435     return;
1436 
1437   if((p = st->es_priv) == NULL)
1438     p = st->es_priv = calloc(1, sizeof(hevc_private_t));
1439 
1440   skip_bits(bs, 15);  /* NAL type, Layer ID, Temporal ID */
1441 
1442   vps_id = read_bits(bs, 4);
1443   if (vps_id >= MAX_VPS_COUNT)
1444     return;
1445   vps = &p->vps[vps_id];
1446   if (!vps->valid)
1447     return;
1448 
1449   max_sub_layers = read_bits(bs, 3) + 1;
1450   if (max_sub_layers > MAX_SUB_LAYERS)
1451     return;
1452   skip_bits1(bs);     /* temporal_id_nesting */
1453 
1454   hvcc_parse_ptl(bs, NULL, max_sub_layers);
1455 
1456   sps_id = read_golomb_ue(bs);
1457   if (sps_id >= MAX_SPS_COUNT)
1458     return;
1459   sps = &p->sps[sps_id];
1460 
1461   chroma_format_idc = read_golomb_ue(bs);
1462   if (chroma_format_idc == 3)
1463     if (read_bits1(bs))   /* separate_colour_plane */
1464       chroma_format_idc = 0;
1465   width  = read_golomb_ue(bs);
1466   height = read_golomb_ue(bs);
1467   if (check_width(width) || check_height(height))
1468     return;
1469 
1470   if (read_bits1(bs)) { /* pic_conformance */
1471     read_golomb_ue(bs); /* left_offset */
1472     read_golomb_ue(bs); /* right_offset */
1473     read_golomb_ue(bs); /* top_offset */
1474     read_golomb_ue(bs); /* bottom_offset */
1475   }
1476 
1477   bit_depth        = read_golomb_ue(bs);
1478   bit_depth_chroma = read_golomb_ue(bs);
1479   if (chroma_format_idc && bit_depth_chroma != bit_depth)
1480     return;
1481 
1482   log2_max_poc_lsb = read_golomb_ue(bs) + 4;
1483   if (log2_max_poc_lsb > 16)
1484     return;
1485 
1486   u = read_bits1(bs);   /* sublayer_ordering_info */
1487   for (u = u ? 0 : max_sub_layers - 1; u < max_sub_layers; u++) {
1488     read_golomb_ue(bs); /* max_dec_pic_buffering */
1489     read_golomb_ue(bs); /* num_reorder_pics */
1490     read_golomb_ue(bs); /* max_latency_increase */
1491   }
1492 
1493   log2_min_cb_size = read_golomb_ue(bs) + 3;
1494   if (log2_min_cb_size < 3 || log2_min_cb_size > 30)
1495     return;
1496   log2_diff_max_min_coding_block_size = read_golomb_ue(bs);
1497   if (log2_diff_max_min_coding_block_size > 30)
1498     return;
1499   log2_min_tb_size = read_golomb_ue(bs) + 2;
1500   if (log2_min_tb_size >= log2_min_cb_size || log2_min_tb_size < 2)
1501     return;
1502   log2_diff_max_min_transform_block_size = read_golomb_ue(bs);
1503   if (log2_diff_max_min_transform_block_size > 30)
1504     return;
1505 
1506   read_golomb_ue(bs); /* max_transform_hierarchy_depth_inter */
1507   read_golomb_ue(bs); /* max_transform_hierarchy_depth_intra */
1508 
1509   if (read_bits1(bs)) { /* scaling_list_enable_flag */
1510     if (read_bits1(bs)) { /* scaling_list_data */
1511       for (u = 0; u < 4; u++) {
1512         for (v = 0; v < 6; v += ((u == 3) ? 3 : 1)) {
1513           if (!read_bits1(bs)) { /* scaling_list_pred_mode */
1514             read_golomb_ue(bs); /* delta */
1515           } else {
1516             if (u > 1)
1517               read_golomb_se(bs); /* scaling_list_dc_coef */
1518             i = MIN(64, 1 << (4 + (u << 1)));
1519             while (i--)
1520               read_golomb_se(bs); /* scaling_list_delta_coef */
1521           }
1522         }
1523       }
1524     }
1525   }
1526 
1527   skip_bits1(bs); /* amp_enabled */
1528   skip_bits1(bs); /* sao_enabled */
1529 
1530   if (read_bits1(bs)) { /* pcm_enabled */
1531     skip_bits(bs, 4);   /* pcm.bit_depth */
1532     skip_bits(bs, 4);   /* pcm.bit_depth_chroma */
1533     read_golomb_ue(bs); /* pcm.log2_min_pcm_cb_size */
1534     read_golomb_ue(bs); /* pcm.log2_max_pcm_cb_size */
1535     skip_bits1(bs);     /* pcm.loop_filter_disable_flag */
1536   }
1537 
1538   nb_st_rps = read_golomb_ue(bs);
1539   if (nb_st_rps > MAX_SHORT_TERM_RPS_COUNT)
1540     return;
1541 
1542   for (u = 0; u < nb_st_rps; u++) {
1543     if (u > 0 && read_bits1(bs)) { /* rps_predict */
1544       skip_bits1(bs);         /* delta_rps_sign */
1545       read_golomb_ue(bs);     /* abs_delta_rps */
1546     } else {
1547       u = read_golomb_ue(bs); /* rps->num_negative_pics */
1548       v = read_golomb_ue(bs); /* nb_positive_pics */
1549       if (u > MAX_REFS || v > MAX_REFS)
1550         return;
1551       for (i = 0; i < u; i++) {
1552         read_golomb_ue(bs);   /* delta_poc */
1553         skip_bits1(bs);       /* used */
1554       }
1555       for (i = 0; i < v; i++) {
1556         read_golomb_ue(bs);   /* delta_poc */
1557         skip_bits1(bs);       /* used */
1558       }
1559     }
1560   }
1561 
1562   if (read_bits1(bs)) { /* long_term_ref_pics_present */
1563     u = read_golomb_ue(bs); /* num_long_term_ref_pics_sps */
1564     if (u > 31)
1565       return;
1566     for (i = 0; i < u; i++) {
1567       skip_bits(bs, log2_max_poc_lsb);
1568       skip_bits1(bs);
1569     }
1570   }
1571 
1572   skip_bits1(bs); /* sps_temporal_mvp_enabled_flag */
1573   skip_bits1(bs); /* sps_strong_intra_smoothing_enable_flag */
1574 
1575   if (read_bits1(bs))  /* vui_present */
1576     if (hevc_decode_vui(&sps->vui, bs))
1577       return;
1578 
1579   if (!vps->num_units_in_tick && !vps->time_scale &&
1580       !sps->vui.num_units_in_tick && !sps->vui.time_scale)
1581     return;
1582 
1583   sps->width = width;
1584   sps->height = height;
1585 
1586   log2_ctb_size = log2_min_cb_size +
1587                   log2_diff_max_min_coding_block_size;
1588   if (log2_ctb_size > 18)
1589     return;
1590 
1591   sps->ctb_width  = (width  + (1 << log2_ctb_size) - 1) >> log2_ctb_size;
1592   sps->ctb_height = (height + (1 << log2_ctb_size) - 1) >> log2_ctb_size;
1593 
1594   sps->vps_id = vps_id;
1595   sps->valid = 1;
1596 }
1597 
1598 void
hevc_decode_pps(elementary_stream_t * st,bitstream_t * bs)1599 hevc_decode_pps(elementary_stream_t *st, bitstream_t *bs)
1600 {
1601   hevc_private_t *p;
1602   hevc_pps_t *pps;
1603   uint32_t pps_id, sps_id;
1604 
1605   if (read_bits1(bs)) /* zero bit */
1606     return ;
1607 
1608   if((p = st->es_priv) == NULL)
1609     p = st->es_priv = calloc(1, sizeof(hevc_private_t));
1610 
1611   skip_bits(bs, 15);  /* NAL type, Layer ID, Temporal ID */
1612 
1613   pps_id = read_golomb_ue(bs);
1614   if (pps_id >= MAX_PPS_COUNT)
1615     return;
1616   pps = &p->pps[pps_id];
1617 
1618   sps_id = read_golomb_ue(bs);
1619   if (sps_id >= MAX_SPS_COUNT || !p->sps[sps_id].valid)
1620     return;
1621 
1622   pps->sps_id = sps_id;
1623   pps->dependent_slice_segments = read_bits1(bs);
1624   skip_bits1(bs); /* output_flag_present */
1625   pps->num_extra_slice_header_bits = read_bits(bs, 3);
1626 
1627   pps->valid = 1;
1628 }
1629 
1630 int
hevc_decode_slice_header(struct elementary_stream * st,bitstream_t * bs,int * pkttype)1631 hevc_decode_slice_header(struct elementary_stream *st, bitstream_t *bs,
1632                          int *pkttype)
1633 {
1634   hevc_private_t *p;
1635   hevc_vps_t *vps;
1636   hevc_sps_t *sps;
1637   hevc_pps_t *pps;
1638   int nal_type;
1639   int first_slice_in_pic, dependent_slice_segment;
1640   uint32_t pps_id, sps_id, vps_id, u, v, width, height;
1641   uint64_t d;
1642 
1643   if (read_bits1(bs)) /* zero bit */
1644     return -1;
1645 
1646   if((p = st->es_priv) == NULL)
1647       p = st->es_priv = calloc(1, sizeof(hevc_private_t));
1648 
1649   nal_type = read_bits(bs, 6);
1650   skip_bits(bs, 6);   /* Layer ID */
1651   skip_bits(bs, 3);   /* Temporal ID */
1652 
1653   first_slice_in_pic = read_bits1(bs);
1654 
1655   if (IS_IRAP_NAL(nal_type))
1656     skip_bits1(bs);  /* no_output_of_prior_pics_flag */
1657 
1658   pps_id = read_golomb_ue(bs);
1659   if (pps_id >= MAX_PPS_COUNT)
1660     return -1;
1661   pps = &p->pps[pps_id];
1662   if (!pps->valid)
1663     return -1;
1664 
1665   sps_id = pps->sps_id;
1666   if (sps_id >= MAX_SPS_COUNT)
1667     return -1;
1668   sps = &p->sps[sps_id];
1669   if (!sps->valid)
1670     return -1;
1671 
1672   dependent_slice_segment = 0;
1673   if (!first_slice_in_pic) {
1674     if (pps->dependent_slice_segments)
1675       dependent_slice_segment = read_bits1(bs);
1676     v = sps->ctb_width * sps->ctb_height;
1677     u = ilog2((v - 1) << 1);
1678     if (u >= v)
1679       return -1;
1680     skip_bits(bs, u);
1681   }
1682 
1683   if (dependent_slice_segment)
1684     return 1; /* append */
1685 
1686   for (u = 0, v = pps->num_extra_slice_header_bits; u < v; u++)
1687     skip_bits1(bs);
1688 
1689   switch (read_golomb_ue(bs)) {
1690   case B_SLICE: *pkttype = PKT_B_FRAME; break;
1691   case P_SLICE: *pkttype = PKT_P_FRAME; break;
1692   case I_SLICE: *pkttype = PKT_I_FRAME; break;
1693   default: return -1;
1694   }
1695 
1696   vps_id = sps->vps_id;
1697   width  = sps->width;
1698   height = sps->height;
1699 
1700   vps = &p->vps[vps_id];
1701 
1702   d = 0;
1703   if (sps->vui.time_scale) {
1704     d = 180000 * (uint64_t)sps->vui.num_units_in_tick / (uint64_t)sps->vui.time_scale;
1705   } else if (vps->time_scale) {
1706     d = 180000 * (uint64_t)vps->num_units_in_tick / (uint64_t)vps->time_scale;
1707   }
1708 
1709   if (width && height && d)
1710     parser_set_stream_vparam(st, width, height, d);
1711 
1712   if (sps->vui.sar.num && sps->vui.sar.den) {
1713     width  *= sps->vui.sar.num;
1714     height *= sps->vui.sar.den;
1715     if (width && height) {
1716       v = gcdU32(width, height);
1717       st->es_aspect_num = width / v;
1718       st->es_aspect_den = height / v;
1719     }
1720   } else {
1721     st->es_aspect_num = 0;
1722     st->es_aspect_den = 1;
1723   }
1724 
1725   return 0;
1726 }
1727