]> git.sesse.net Git - ffmpeg/blob - libavformat/hevc.c
avformat/avio: Add Metacube support
[ffmpeg] / libavformat / hevc.c
1 /*
2  * Copyright (c) 2014 Tim Walker <tdskywalker@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavcodec/avcodec.h"
22 #include "libavcodec/get_bits.h"
23 #include "libavcodec/golomb.h"
24 #include "libavcodec/hevc.h"
25 #include "libavutil/intreadwrite.h"
26 #include "avc.h"
27 #include "avio.h"
28 #include "avio_internal.h"
29 #include "hevc.h"
30
31 #define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field
32
33 typedef struct HVCCNALUnitArray {
34     uint8_t  array_completeness;
35     uint8_t  NAL_unit_type;
36     uint16_t numNalus;
37     uint16_t *nalUnitLength;
38     uint8_t  **nalUnit;
39 } HVCCNALUnitArray;
40
41 typedef struct HEVCDecoderConfigurationRecord {
42     uint8_t  configurationVersion;
43     uint8_t  general_profile_space;
44     uint8_t  general_tier_flag;
45     uint8_t  general_profile_idc;
46     uint32_t general_profile_compatibility_flags;
47     uint64_t general_constraint_indicator_flags;
48     uint8_t  general_level_idc;
49     uint16_t min_spatial_segmentation_idc;
50     uint8_t  parallelismType;
51     uint8_t  chromaFormat;
52     uint8_t  bitDepthLumaMinus8;
53     uint8_t  bitDepthChromaMinus8;
54     uint16_t avgFrameRate;
55     uint8_t  constantFrameRate;
56     uint8_t  numTemporalLayers;
57     uint8_t  temporalIdNested;
58     uint8_t  lengthSizeMinusOne;
59     uint8_t  numOfArrays;
60     HVCCNALUnitArray *array;
61 } HEVCDecoderConfigurationRecord;
62
63 typedef struct HVCCProfileTierLevel {
64     uint8_t  profile_space;
65     uint8_t  tier_flag;
66     uint8_t  profile_idc;
67     uint32_t profile_compatibility_flags;
68     uint64_t constraint_indicator_flags;
69     uint8_t  level_idc;
70 } HVCCProfileTierLevel;
71
72 static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc,
73                             HVCCProfileTierLevel *ptl)
74 {
75     /*
76      * The value of general_profile_space in all the parameter sets must be
77      * identical.
78      */
79     hvcc->general_profile_space = ptl->profile_space;
80
81     /*
82      * The level indication general_level_idc must indicate a level of
83      * capability equal to or greater than the highest level indicated for the
84      * highest tier in all the parameter sets.
85      */
86     if (hvcc->general_tier_flag < ptl->tier_flag)
87         hvcc->general_level_idc = ptl->level_idc;
88     else
89         hvcc->general_level_idc = FFMAX(hvcc->general_level_idc, ptl->level_idc);
90
91     /*
92      * The tier indication general_tier_flag must indicate a tier equal to or
93      * greater than the highest tier indicated in all the parameter sets.
94      */
95     hvcc->general_tier_flag = FFMAX(hvcc->general_tier_flag, ptl->tier_flag);
96
97     /*
98      * The profile indication general_profile_idc must indicate a profile to
99      * which the stream associated with this configuration record conforms.
100      *
101      * If the sequence parameter sets are marked with different profiles, then
102      * the stream may need examination to determine which profile, if any, the
103      * entire stream conforms to. If the entire stream is not examined, or the
104      * examination reveals that there is no profile to which the entire stream
105      * conforms, then the entire stream must be split into two or more
106      * sub-streams with separate configuration records in which these rules can
107      * be met.
108      *
109      * Note: set the profile to the highest value for the sake of simplicity.
110      */
111     hvcc->general_profile_idc = FFMAX(hvcc->general_profile_idc, ptl->profile_idc);
112
113     /*
114      * Each bit in general_profile_compatibility_flags may only be set if all
115      * the parameter sets set that bit.
116      */
117     hvcc->general_profile_compatibility_flags &= ptl->profile_compatibility_flags;
118
119     /*
120      * Each bit in general_constraint_indicator_flags may only be set if all
121      * the parameter sets set that bit.
122      */
123     hvcc->general_constraint_indicator_flags &= ptl->constraint_indicator_flags;
124 }
125
126 static void hvcc_parse_ptl(GetBitContext *gb,
127                            HEVCDecoderConfigurationRecord *hvcc,
128                            unsigned int max_sub_layers_minus1)
129 {
130     unsigned int i;
131     HVCCProfileTierLevel general_ptl;
132     uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS];
133     uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS];
134
135     general_ptl.profile_space               = get_bits(gb, 2);
136     general_ptl.tier_flag                   = get_bits1(gb);
137     general_ptl.profile_idc                 = get_bits(gb, 5);
138     general_ptl.profile_compatibility_flags = get_bits_long(gb, 32);
139     general_ptl.constraint_indicator_flags  = get_bits64(gb, 48);
140     general_ptl.level_idc                   = get_bits(gb, 8);
141     hvcc_update_ptl(hvcc, &general_ptl);
142
143     for (i = 0; i < max_sub_layers_minus1; i++) {
144         sub_layer_profile_present_flag[i] = get_bits1(gb);
145         sub_layer_level_present_flag[i]   = get_bits1(gb);
146     }
147
148     if (max_sub_layers_minus1 > 0)
149         for (i = max_sub_layers_minus1; i < 8; i++)
150             skip_bits(gb, 2); // reserved_zero_2bits[i]
151
152     for (i = 0; i < max_sub_layers_minus1; i++) {
153         if (sub_layer_profile_present_flag[i]) {
154             /*
155              * sub_layer_profile_space[i]                     u(2)
156              * sub_layer_tier_flag[i]                         u(1)
157              * sub_layer_profile_idc[i]                       u(5)
158              * sub_layer_profile_compatibility_flag[i][0..31] u(32)
159              * sub_layer_progressive_source_flag[i]           u(1)
160              * sub_layer_interlaced_source_flag[i]            u(1)
161              * sub_layer_non_packed_constraint_flag[i]        u(1)
162              * sub_layer_frame_only_constraint_flag[i]        u(1)
163              * sub_layer_reserved_zero_44bits[i]              u(44)
164              */
165             skip_bits_long(gb, 32);
166             skip_bits_long(gb, 32);
167             skip_bits     (gb, 24);
168         }
169
170         if (sub_layer_level_present_flag[i])
171             skip_bits(gb, 8);
172     }
173 }
174
175 static void skip_sub_layer_hrd_parameters(GetBitContext *gb,
176                                           unsigned int cpb_cnt_minus1,
177                                           uint8_t sub_pic_hrd_params_present_flag)
178 {
179     unsigned int i;
180
181     for (i = 0; i <= cpb_cnt_minus1; i++) {
182         get_ue_golomb_long(gb); // bit_rate_value_minus1
183         get_ue_golomb_long(gb); // cpb_size_value_minus1
184
185         if (sub_pic_hrd_params_present_flag) {
186             get_ue_golomb_long(gb); // cpb_size_du_value_minus1
187             get_ue_golomb_long(gb); // bit_rate_du_value_minus1
188         }
189
190         skip_bits1(gb); // cbr_flag
191     }
192 }
193
194 static int skip_hrd_parameters(GetBitContext *gb, uint8_t cprms_present_flag,
195                                 unsigned int max_sub_layers_minus1)
196 {
197     unsigned int i;
198     uint8_t sub_pic_hrd_params_present_flag = 0;
199     uint8_t nal_hrd_parameters_present_flag = 0;
200     uint8_t vcl_hrd_parameters_present_flag = 0;
201
202     if (cprms_present_flag) {
203         nal_hrd_parameters_present_flag = get_bits1(gb);
204         vcl_hrd_parameters_present_flag = get_bits1(gb);
205
206         if (nal_hrd_parameters_present_flag ||
207             vcl_hrd_parameters_present_flag) {
208             sub_pic_hrd_params_present_flag = get_bits1(gb);
209
210             if (sub_pic_hrd_params_present_flag)
211                 /*
212                  * tick_divisor_minus2                          u(8)
213                  * du_cpb_removal_delay_increment_length_minus1 u(5)
214                  * sub_pic_cpb_params_in_pic_timing_sei_flag    u(1)
215                  * dpb_output_delay_du_length_minus1            u(5)
216                  */
217                 skip_bits(gb, 19);
218
219             /*
220              * bit_rate_scale u(4)
221              * cpb_size_scale u(4)
222              */
223             skip_bits(gb, 8);
224
225             if (sub_pic_hrd_params_present_flag)
226                 skip_bits(gb, 4); // cpb_size_du_scale
227
228             /*
229              * initial_cpb_removal_delay_length_minus1 u(5)
230              * au_cpb_removal_delay_length_minus1      u(5)
231              * dpb_output_delay_length_minus1          u(5)
232              */
233             skip_bits(gb, 15);
234         }
235     }
236
237     for (i = 0; i <= max_sub_layers_minus1; i++) {
238         unsigned int cpb_cnt_minus1            = 0;
239         uint8_t low_delay_hrd_flag             = 0;
240         uint8_t fixed_pic_rate_within_cvs_flag = 0;
241         uint8_t fixed_pic_rate_general_flag    = get_bits1(gb);
242
243         if (!fixed_pic_rate_general_flag)
244             fixed_pic_rate_within_cvs_flag = get_bits1(gb);
245
246         if (fixed_pic_rate_within_cvs_flag)
247             get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
248         else
249             low_delay_hrd_flag = get_bits1(gb);
250
251         if (!low_delay_hrd_flag) {
252             cpb_cnt_minus1 = get_ue_golomb_long(gb);
253             if (cpb_cnt_minus1 > 31)
254                 return AVERROR_INVALIDDATA;
255         }
256
257         if (nal_hrd_parameters_present_flag)
258             skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
259                                           sub_pic_hrd_params_present_flag);
260
261         if (vcl_hrd_parameters_present_flag)
262             skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
263                                           sub_pic_hrd_params_present_flag);
264     }
265
266     return 0;
267 }
268
269 static void skip_timing_info(GetBitContext *gb)
270 {
271     skip_bits_long(gb, 32); // num_units_in_tick
272     skip_bits_long(gb, 32); // time_scale
273
274     if (get_bits1(gb))          // poc_proportional_to_timing_flag
275         get_ue_golomb_long(gb); // num_ticks_poc_diff_one_minus1
276 }
277
278 static void hvcc_parse_vui(GetBitContext *gb,
279                            HEVCDecoderConfigurationRecord *hvcc,
280                            unsigned int max_sub_layers_minus1)
281 {
282     unsigned int min_spatial_segmentation_idc;
283
284     if (get_bits1(gb))              // aspect_ratio_info_present_flag
285         if (get_bits(gb, 8) == 255) // aspect_ratio_idc
286             skip_bits_long(gb, 32); // sar_width u(16), sar_height u(16)
287
288     if (get_bits1(gb))  // overscan_info_present_flag
289         skip_bits1(gb); // overscan_appropriate_flag
290
291     if (get_bits1(gb)) {  // video_signal_type_present_flag
292         skip_bits(gb, 4); // video_format u(3), video_full_range_flag u(1)
293
294         if (get_bits1(gb)) // colour_description_present_flag
295             /*
296              * colour_primaries         u(8)
297              * transfer_characteristics u(8)
298              * matrix_coeffs            u(8)
299              */
300             skip_bits(gb, 24);
301     }
302
303     if (get_bits1(gb)) {        // chroma_loc_info_present_flag
304         get_ue_golomb_long(gb); // chroma_sample_loc_type_top_field
305         get_ue_golomb_long(gb); // chroma_sample_loc_type_bottom_field
306     }
307
308     /*
309      * neutral_chroma_indication_flag u(1)
310      * field_seq_flag                 u(1)
311      * frame_field_info_present_flag  u(1)
312      */
313     skip_bits(gb, 3);
314
315     if (get_bits1(gb)) {        // default_display_window_flag
316         get_ue_golomb_long(gb); // def_disp_win_left_offset
317         get_ue_golomb_long(gb); // def_disp_win_right_offset
318         get_ue_golomb_long(gb); // def_disp_win_top_offset
319         get_ue_golomb_long(gb); // def_disp_win_bottom_offset
320     }
321
322     if (get_bits1(gb)) { // vui_timing_info_present_flag
323         skip_timing_info(gb);
324
325         if (get_bits1(gb)) // vui_hrd_parameters_present_flag
326             skip_hrd_parameters(gb, 1, max_sub_layers_minus1);
327     }
328
329     if (get_bits1(gb)) { // bitstream_restriction_flag
330         /*
331          * tiles_fixed_structure_flag              u(1)
332          * motion_vectors_over_pic_boundaries_flag u(1)
333          * restricted_ref_pic_lists_flag           u(1)
334          */
335         skip_bits(gb, 3);
336
337         min_spatial_segmentation_idc = get_ue_golomb_long(gb);
338
339         /*
340          * unsigned int(12) min_spatial_segmentation_idc;
341          *
342          * The min_spatial_segmentation_idc indication must indicate a level of
343          * spatial segmentation equal to or less than the lowest level of
344          * spatial segmentation indicated in all the parameter sets.
345          */
346         hvcc->min_spatial_segmentation_idc = FFMIN(hvcc->min_spatial_segmentation_idc,
347                                                    min_spatial_segmentation_idc);
348
349         get_ue_golomb_long(gb); // max_bytes_per_pic_denom
350         get_ue_golomb_long(gb); // max_bits_per_min_cu_denom
351         get_ue_golomb_long(gb); // log2_max_mv_length_horizontal
352         get_ue_golomb_long(gb); // log2_max_mv_length_vertical
353     }
354 }
355
356 static void skip_sub_layer_ordering_info(GetBitContext *gb)
357 {
358     get_ue_golomb_long(gb); // max_dec_pic_buffering_minus1
359     get_ue_golomb_long(gb); // max_num_reorder_pics
360     get_ue_golomb_long(gb); // max_latency_increase_plus1
361 }
362
363 static int hvcc_parse_vps(GetBitContext *gb,
364                           HEVCDecoderConfigurationRecord *hvcc)
365 {
366     unsigned int vps_max_sub_layers_minus1;
367
368     /*
369      * vps_video_parameter_set_id u(4)
370      * vps_reserved_three_2bits   u(2)
371      * vps_max_layers_minus1      u(6)
372      */
373     skip_bits(gb, 12);
374
375     vps_max_sub_layers_minus1 = get_bits(gb, 3);
376
377     /*
378      * numTemporalLayers greater than 1 indicates that the stream to which this
379      * configuration record applies is temporally scalable and the contained
380      * number of temporal layers (also referred to as temporal sub-layer or
381      * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
382      * indicates that the stream is not temporally scalable. Value 0 indicates
383      * that it is unknown whether the stream is temporally scalable.
384      */
385     hvcc->numTemporalLayers = FFMAX(hvcc->numTemporalLayers,
386                                     vps_max_sub_layers_minus1 + 1);
387
388     /*
389      * vps_temporal_id_nesting_flag u(1)
390      * vps_reserved_0xffff_16bits   u(16)
391      */
392     skip_bits(gb, 17);
393
394     hvcc_parse_ptl(gb, hvcc, vps_max_sub_layers_minus1);
395
396     /* nothing useful for hvcC past this point */
397     return 0;
398 }
399
400 static void skip_scaling_list_data(GetBitContext *gb)
401 {
402     int i, j, k, num_coeffs;
403
404     for (i = 0; i < 4; i++)
405         for (j = 0; j < (i == 3 ? 2 : 6); j++)
406             if (!get_bits1(gb))         // scaling_list_pred_mode_flag[i][j]
407                 get_ue_golomb_long(gb); // scaling_list_pred_matrix_id_delta[i][j]
408             else {
409                 num_coeffs = FFMIN(64, 1 << (4 + (i << 1)));
410
411                 if (i > 1)
412                     get_se_golomb_long(gb); // scaling_list_dc_coef_minus8[i-2][j]
413
414                 for (k = 0; k < num_coeffs; k++)
415                     get_se_golomb_long(gb); // scaling_list_delta_coef
416             }
417 }
418
419 static int parse_rps(GetBitContext *gb, unsigned int rps_idx,
420                      unsigned int num_rps,
421                      unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
422 {
423     unsigned int i;
424
425     if (rps_idx && get_bits1(gb)) { // inter_ref_pic_set_prediction_flag
426         /* this should only happen for slice headers, and this isn't one */
427         if (rps_idx >= num_rps)
428             return AVERROR_INVALIDDATA;
429
430         skip_bits1        (gb); // delta_rps_sign
431         get_ue_golomb_long(gb); // abs_delta_rps_minus1
432
433         num_delta_pocs[rps_idx] = 0;
434
435         /*
436          * From libavcodec/hevc_ps.c:
437          *
438          * if (is_slice_header) {
439          *    //foo
440          * } else
441          *     rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
442          *
443          * where:
444          * rps:             &sps->st_rps[rps_idx]
445          * sps->st_rps:     &sps->st_rps[0]
446          * is_slice_header: rps_idx == num_rps
447          *
448          * thus:
449          * if (num_rps != rps_idx)
450          *     rps_ridx = &sps->st_rps[rps_idx - 1];
451          *
452          * NumDeltaPocs[RefRpsIdx]: num_delta_pocs[rps_idx - 1]
453          */
454         for (i = 0; i <= num_delta_pocs[rps_idx - 1]; i++) {
455             uint8_t use_delta_flag = 0;
456             uint8_t used_by_curr_pic_flag = get_bits1(gb);
457             if (!used_by_curr_pic_flag)
458                 use_delta_flag = get_bits1(gb);
459
460             if (used_by_curr_pic_flag || use_delta_flag)
461                 num_delta_pocs[rps_idx]++;
462         }
463     } else {
464         unsigned int num_negative_pics = get_ue_golomb_long(gb);
465         unsigned int num_positive_pics = get_ue_golomb_long(gb);
466
467         if ((num_positive_pics + (uint64_t)num_negative_pics) * 2 > get_bits_left(gb))
468             return AVERROR_INVALIDDATA;
469
470         num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics;
471
472         for (i = 0; i < num_negative_pics; i++) {
473             get_ue_golomb_long(gb); // delta_poc_s0_minus1[rps_idx]
474             skip_bits1        (gb); // used_by_curr_pic_s0_flag[rps_idx]
475         }
476
477         for (i = 0; i < num_positive_pics; i++) {
478             get_ue_golomb_long(gb); // delta_poc_s1_minus1[rps_idx]
479             skip_bits1        (gb); // used_by_curr_pic_s1_flag[rps_idx]
480         }
481     }
482
483     return 0;
484 }
485
486 static int hvcc_parse_sps(GetBitContext *gb,
487                           HEVCDecoderConfigurationRecord *hvcc)
488 {
489     unsigned int i, sps_max_sub_layers_minus1, log2_max_pic_order_cnt_lsb_minus4;
490     unsigned int num_short_term_ref_pic_sets, num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
491
492     skip_bits(gb, 4); // sps_video_parameter_set_id
493
494     sps_max_sub_layers_minus1 = get_bits (gb, 3);
495
496     /*
497      * numTemporalLayers greater than 1 indicates that the stream to which this
498      * configuration record applies is temporally scalable and the contained
499      * number of temporal layers (also referred to as temporal sub-layer or
500      * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
501      * indicates that the stream is not temporally scalable. Value 0 indicates
502      * that it is unknown whether the stream is temporally scalable.
503      */
504     hvcc->numTemporalLayers = FFMAX(hvcc->numTemporalLayers,
505                                     sps_max_sub_layers_minus1 + 1);
506
507     hvcc->temporalIdNested = get_bits1(gb);
508
509     hvcc_parse_ptl(gb, hvcc, sps_max_sub_layers_minus1);
510
511     get_ue_golomb_long(gb); // sps_seq_parameter_set_id
512
513     hvcc->chromaFormat = get_ue_golomb_long(gb);
514
515     if (hvcc->chromaFormat == 3)
516         skip_bits1(gb); // separate_colour_plane_flag
517
518     get_ue_golomb_long(gb); // pic_width_in_luma_samples
519     get_ue_golomb_long(gb); // pic_height_in_luma_samples
520
521     if (get_bits1(gb)) {        // conformance_window_flag
522         get_ue_golomb_long(gb); // conf_win_left_offset
523         get_ue_golomb_long(gb); // conf_win_right_offset
524         get_ue_golomb_long(gb); // conf_win_top_offset
525         get_ue_golomb_long(gb); // conf_win_bottom_offset
526     }
527
528     hvcc->bitDepthLumaMinus8          = get_ue_golomb_long(gb);
529     hvcc->bitDepthChromaMinus8        = get_ue_golomb_long(gb);
530     log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb_long(gb);
531
532     /* sps_sub_layer_ordering_info_present_flag */
533     i = get_bits1(gb) ? 0 : sps_max_sub_layers_minus1;
534     for (; i <= sps_max_sub_layers_minus1; i++)
535         skip_sub_layer_ordering_info(gb);
536
537     get_ue_golomb_long(gb); // log2_min_luma_coding_block_size_minus3
538     get_ue_golomb_long(gb); // log2_diff_max_min_luma_coding_block_size
539     get_ue_golomb_long(gb); // log2_min_transform_block_size_minus2
540     get_ue_golomb_long(gb); // log2_diff_max_min_transform_block_size
541     get_ue_golomb_long(gb); // max_transform_hierarchy_depth_inter
542     get_ue_golomb_long(gb); // max_transform_hierarchy_depth_intra
543
544     if (get_bits1(gb) && // scaling_list_enabled_flag
545         get_bits1(gb))   // sps_scaling_list_data_present_flag
546         skip_scaling_list_data(gb);
547
548     skip_bits1(gb); // amp_enabled_flag
549     skip_bits1(gb); // sample_adaptive_offset_enabled_flag
550
551     if (get_bits1(gb)) {           // pcm_enabled_flag
552         skip_bits         (gb, 4); // pcm_sample_bit_depth_luma_minus1
553         skip_bits         (gb, 4); // pcm_sample_bit_depth_chroma_minus1
554         get_ue_golomb_long(gb);    // log2_min_pcm_luma_coding_block_size_minus3
555         get_ue_golomb_long(gb);    // log2_diff_max_min_pcm_luma_coding_block_size
556         skip_bits1        (gb);    // pcm_loop_filter_disabled_flag
557     }
558
559     num_short_term_ref_pic_sets = get_ue_golomb_long(gb);
560     if (num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
561         return AVERROR_INVALIDDATA;
562
563     for (i = 0; i < num_short_term_ref_pic_sets; i++) {
564         int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, num_delta_pocs);
565         if (ret < 0)
566             return ret;
567     }
568
569     if (get_bits1(gb)) {                               // long_term_ref_pics_present_flag
570         unsigned num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
571         if (num_long_term_ref_pics_sps > 31U)
572             return AVERROR_INVALIDDATA;
573         for (i = 0; i < num_long_term_ref_pics_sps; i++) { // num_long_term_ref_pics_sps
574             int len = FFMIN(log2_max_pic_order_cnt_lsb_minus4 + 4, 16);
575             skip_bits (gb, len); // lt_ref_pic_poc_lsb_sps[i]
576             skip_bits1(gb);      // used_by_curr_pic_lt_sps_flag[i]
577         }
578     }
579
580     skip_bits1(gb); // sps_temporal_mvp_enabled_flag
581     skip_bits1(gb); // strong_intra_smoothing_enabled_flag
582
583     if (get_bits1(gb)) // vui_parameters_present_flag
584         hvcc_parse_vui(gb, hvcc, sps_max_sub_layers_minus1);
585
586     /* nothing useful for hvcC past this point */
587     return 0;
588 }
589
590 static int hvcc_parse_pps(GetBitContext *gb,
591                           HEVCDecoderConfigurationRecord *hvcc)
592 {
593     uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag;
594
595     get_ue_golomb_long(gb); // pps_pic_parameter_set_id
596     get_ue_golomb_long(gb); // pps_seq_parameter_set_id
597
598     /*
599      * dependent_slice_segments_enabled_flag u(1)
600      * output_flag_present_flag              u(1)
601      * num_extra_slice_header_bits           u(3)
602      * sign_data_hiding_enabled_flag         u(1)
603      * cabac_init_present_flag               u(1)
604      */
605     skip_bits(gb, 7);
606
607     get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1
608     get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1
609     get_se_golomb_long(gb); // init_qp_minus26
610
611     /*
612      * constrained_intra_pred_flag u(1)
613      * transform_skip_enabled_flag u(1)
614      */
615     skip_bits(gb, 2);
616
617     if (get_bits1(gb))          // cu_qp_delta_enabled_flag
618         get_ue_golomb_long(gb); // diff_cu_qp_delta_depth
619
620     get_se_golomb_long(gb); // pps_cb_qp_offset
621     get_se_golomb_long(gb); // pps_cr_qp_offset
622
623     /*
624      * pps_slice_chroma_qp_offsets_present_flag u(1)
625      * weighted_pred_flag               u(1)
626      * weighted_bipred_flag             u(1)
627      * transquant_bypass_enabled_flag   u(1)
628      */
629     skip_bits(gb, 4);
630
631     tiles_enabled_flag               = get_bits1(gb);
632     entropy_coding_sync_enabled_flag = get_bits1(gb);
633
634     if (entropy_coding_sync_enabled_flag && tiles_enabled_flag)
635         hvcc->parallelismType = 0; // mixed-type parallel decoding
636     else if (entropy_coding_sync_enabled_flag)
637         hvcc->parallelismType = 3; // wavefront-based parallel decoding
638     else if (tiles_enabled_flag)
639         hvcc->parallelismType = 2; // tile-based parallel decoding
640     else
641         hvcc->parallelismType = 1; // slice-based parallel decoding
642
643     /* nothing useful for hvcC past this point */
644     return 0;
645 }
646
647 static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
648 {
649     skip_bits1(gb); // forbidden_zero_bit
650
651     *nal_type = get_bits(gb, 6);
652
653     /*
654      * nuh_layer_id          u(6)
655      * nuh_temporal_id_plus1 u(3)
656      */
657     skip_bits(gb, 9);
658 }
659
660 static int hvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
661                                    uint8_t nal_type, int ps_array_completeness,
662                                    HEVCDecoderConfigurationRecord *hvcc)
663 {
664     int ret;
665     uint8_t index;
666     uint16_t numNalus;
667     HVCCNALUnitArray *array;
668
669     for (index = 0; index < hvcc->numOfArrays; index++)
670         if (hvcc->array[index].NAL_unit_type == nal_type)
671             break;
672
673     if (index >= hvcc->numOfArrays) {
674         uint8_t i;
675
676         ret = av_reallocp_array(&hvcc->array, index + 1, sizeof(HVCCNALUnitArray));
677         if (ret < 0)
678             return ret;
679
680         for (i = hvcc->numOfArrays; i <= index; i++)
681             memset(&hvcc->array[i], 0, sizeof(HVCCNALUnitArray));
682         hvcc->numOfArrays = index + 1;
683     }
684
685     array    = &hvcc->array[index];
686     numNalus = array->numNalus;
687
688     ret = av_reallocp_array(&array->nalUnit, numNalus + 1, sizeof(uint8_t*));
689     if (ret < 0)
690         return ret;
691
692     ret = av_reallocp_array(&array->nalUnitLength, numNalus + 1, sizeof(uint16_t));
693     if (ret < 0)
694         return ret;
695
696     array->nalUnit      [numNalus] = nal_buf;
697     array->nalUnitLength[numNalus] = nal_size;
698     array->NAL_unit_type           = nal_type;
699     array->numNalus++;
700
701     /*
702      * When the sample entry name is ‘hvc1’, the default and mandatory value of
703      * array_completeness is 1 for arrays of all types of parameter sets, and 0
704      * for all other arrays. When the sample entry name is ‘hev1’, the default
705      * value of array_completeness is 0 for all arrays.
706      */
707     if (nal_type == HEVC_NAL_VPS || nal_type == HEVC_NAL_SPS || nal_type == HEVC_NAL_PPS)
708         array->array_completeness = ps_array_completeness;
709
710     return 0;
711 }
712
713 static int hvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
714                              int ps_array_completeness,
715                              HEVCDecoderConfigurationRecord *hvcc)
716 {
717     int ret = 0;
718     GetBitContext gbc;
719     uint8_t nal_type;
720     uint8_t *rbsp_buf;
721     uint32_t rbsp_size;
722
723     rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
724     if (!rbsp_buf) {
725         ret = AVERROR(ENOMEM);
726         goto end;
727     }
728
729     ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
730     if (ret < 0)
731         goto end;
732
733     nal_unit_parse_header(&gbc, &nal_type);
734
735     /*
736      * Note: only 'declarative' SEI messages are allowed in
737      * hvcC. Perhaps the SEI playload type should be checked
738      * and non-declarative SEI messages discarded?
739      */
740     switch (nal_type) {
741     case HEVC_NAL_VPS:
742     case HEVC_NAL_SPS:
743     case HEVC_NAL_PPS:
744     case HEVC_NAL_SEI_PREFIX:
745     case HEVC_NAL_SEI_SUFFIX:
746         ret = hvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
747                                       ps_array_completeness, hvcc);
748         if (ret < 0)
749             goto end;
750         else if (nal_type == HEVC_NAL_VPS)
751             ret = hvcc_parse_vps(&gbc, hvcc);
752         else if (nal_type == HEVC_NAL_SPS)
753             ret = hvcc_parse_sps(&gbc, hvcc);
754         else if (nal_type == HEVC_NAL_PPS)
755             ret = hvcc_parse_pps(&gbc, hvcc);
756         if (ret < 0)
757             goto end;
758         break;
759     default:
760         ret = AVERROR_INVALIDDATA;
761         goto end;
762     }
763
764 end:
765     av_free(rbsp_buf);
766     return ret;
767 }
768
769 static void hvcc_init(HEVCDecoderConfigurationRecord *hvcc)
770 {
771     memset(hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
772     hvcc->configurationVersion = 1;
773     hvcc->lengthSizeMinusOne   = 3; // 4 bytes
774
775     /*
776      * The following fields have all their valid bits set by default,
777      * the ProfileTierLevel parsing code will unset them when needed.
778      */
779     hvcc->general_profile_compatibility_flags = 0xffffffff;
780     hvcc->general_constraint_indicator_flags  = 0xffffffffffff;
781
782     /*
783      * Initialize this field with an invalid value which can be used to detect
784      * whether we didn't see any VUI (in which case it should be reset to zero).
785      */
786     hvcc->min_spatial_segmentation_idc = MAX_SPATIAL_SEGMENTATION + 1;
787 }
788
789 static void hvcc_close(HEVCDecoderConfigurationRecord *hvcc)
790 {
791     uint8_t i;
792
793     for (i = 0; i < hvcc->numOfArrays; i++) {
794         hvcc->array[i].numNalus = 0;
795         av_freep(&hvcc->array[i].nalUnit);
796         av_freep(&hvcc->array[i].nalUnitLength);
797     }
798
799     hvcc->numOfArrays = 0;
800     av_freep(&hvcc->array);
801 }
802
803 static int hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc)
804 {
805     uint8_t i;
806     uint16_t j, vps_count = 0, sps_count = 0, pps_count = 0;
807
808     /*
809      * We only support writing HEVCDecoderConfigurationRecord version 1.
810      */
811     hvcc->configurationVersion = 1;
812
813     /*
814      * If min_spatial_segmentation_idc is invalid, reset to 0 (unspecified).
815      */
816     if (hvcc->min_spatial_segmentation_idc > MAX_SPATIAL_SEGMENTATION)
817         hvcc->min_spatial_segmentation_idc = 0;
818
819     /*
820      * parallelismType indicates the type of parallelism that is used to meet
821      * the restrictions imposed by min_spatial_segmentation_idc when the value
822      * of min_spatial_segmentation_idc is greater than 0.
823      */
824     if (!hvcc->min_spatial_segmentation_idc)
825         hvcc->parallelismType = 0;
826
827     /*
828      * It's unclear how to properly compute these fields, so
829      * let's always set them to values meaning 'unspecified'.
830      */
831     hvcc->avgFrameRate      = 0;
832     hvcc->constantFrameRate = 0;
833
834     av_log(NULL, AV_LOG_TRACE,  "configurationVersion:                %"PRIu8"\n",
835             hvcc->configurationVersion);
836     av_log(NULL, AV_LOG_TRACE,  "general_profile_space:               %"PRIu8"\n",
837             hvcc->general_profile_space);
838     av_log(NULL, AV_LOG_TRACE,  "general_tier_flag:                   %"PRIu8"\n",
839             hvcc->general_tier_flag);
840     av_log(NULL, AV_LOG_TRACE,  "general_profile_idc:                 %"PRIu8"\n",
841             hvcc->general_profile_idc);
842     av_log(NULL, AV_LOG_TRACE, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
843             hvcc->general_profile_compatibility_flags);
844     av_log(NULL, AV_LOG_TRACE, "general_constraint_indicator_flags:  0x%012"PRIx64"\n",
845             hvcc->general_constraint_indicator_flags);
846     av_log(NULL, AV_LOG_TRACE,  "general_level_idc:                   %"PRIu8"\n",
847             hvcc->general_level_idc);
848     av_log(NULL, AV_LOG_TRACE,  "min_spatial_segmentation_idc:        %"PRIu16"\n",
849             hvcc->min_spatial_segmentation_idc);
850     av_log(NULL, AV_LOG_TRACE,  "parallelismType:                     %"PRIu8"\n",
851             hvcc->parallelismType);
852     av_log(NULL, AV_LOG_TRACE,  "chromaFormat:                        %"PRIu8"\n",
853             hvcc->chromaFormat);
854     av_log(NULL, AV_LOG_TRACE,  "bitDepthLumaMinus8:                  %"PRIu8"\n",
855             hvcc->bitDepthLumaMinus8);
856     av_log(NULL, AV_LOG_TRACE,  "bitDepthChromaMinus8:                %"PRIu8"\n",
857             hvcc->bitDepthChromaMinus8);
858     av_log(NULL, AV_LOG_TRACE,  "avgFrameRate:                        %"PRIu16"\n",
859             hvcc->avgFrameRate);
860     av_log(NULL, AV_LOG_TRACE,  "constantFrameRate:                   %"PRIu8"\n",
861             hvcc->constantFrameRate);
862     av_log(NULL, AV_LOG_TRACE,  "numTemporalLayers:                   %"PRIu8"\n",
863             hvcc->numTemporalLayers);
864     av_log(NULL, AV_LOG_TRACE,  "temporalIdNested:                    %"PRIu8"\n",
865             hvcc->temporalIdNested);
866     av_log(NULL, AV_LOG_TRACE,  "lengthSizeMinusOne:                  %"PRIu8"\n",
867             hvcc->lengthSizeMinusOne);
868     av_log(NULL, AV_LOG_TRACE,  "numOfArrays:                         %"PRIu8"\n",
869             hvcc->numOfArrays);
870     for (i = 0; i < hvcc->numOfArrays; i++) {
871         av_log(NULL, AV_LOG_TRACE, "array_completeness[%"PRIu8"]:               %"PRIu8"\n",
872                 i, hvcc->array[i].array_completeness);
873         av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%"PRIu8"]:                    %"PRIu8"\n",
874                 i, hvcc->array[i].NAL_unit_type);
875         av_log(NULL, AV_LOG_TRACE, "numNalus[%"PRIu8"]:                         %"PRIu16"\n",
876                 i, hvcc->array[i].numNalus);
877         for (j = 0; j < hvcc->array[i].numNalus; j++)
878             av_log(NULL, AV_LOG_TRACE,
879                     "nalUnitLength[%"PRIu8"][%"PRIu16"]:                 %"PRIu16"\n",
880                     i, j, hvcc->array[i].nalUnitLength[j]);
881     }
882
883     /*
884      * We need at least one of each: VPS, SPS and PPS.
885      */
886     for (i = 0; i < hvcc->numOfArrays; i++)
887         switch (hvcc->array[i].NAL_unit_type) {
888         case HEVC_NAL_VPS:
889             vps_count += hvcc->array[i].numNalus;
890             break;
891         case HEVC_NAL_SPS:
892             sps_count += hvcc->array[i].numNalus;
893             break;
894         case HEVC_NAL_PPS:
895             pps_count += hvcc->array[i].numNalus;
896             break;
897         default:
898             break;
899         }
900     if (!vps_count || vps_count > HEVC_MAX_VPS_COUNT ||
901         !sps_count || sps_count > HEVC_MAX_SPS_COUNT ||
902         !pps_count || pps_count > HEVC_MAX_PPS_COUNT)
903         return AVERROR_INVALIDDATA;
904
905     /* unsigned int(8) configurationVersion = 1; */
906     avio_w8(pb, hvcc->configurationVersion);
907
908     /*
909      * unsigned int(2) general_profile_space;
910      * unsigned int(1) general_tier_flag;
911      * unsigned int(5) general_profile_idc;
912      */
913     avio_w8(pb, hvcc->general_profile_space << 6 |
914                 hvcc->general_tier_flag     << 5 |
915                 hvcc->general_profile_idc);
916
917     /* unsigned int(32) general_profile_compatibility_flags; */
918     avio_wb32(pb, hvcc->general_profile_compatibility_flags);
919
920     /* unsigned int(48) general_constraint_indicator_flags; */
921     avio_wb32(pb, hvcc->general_constraint_indicator_flags >> 16);
922     avio_wb16(pb, hvcc->general_constraint_indicator_flags);
923
924     /* unsigned int(8) general_level_idc; */
925     avio_w8(pb, hvcc->general_level_idc);
926
927     /*
928      * bit(4) reserved = ‘1111’b;
929      * unsigned int(12) min_spatial_segmentation_idc;
930      */
931     avio_wb16(pb, hvcc->min_spatial_segmentation_idc | 0xf000);
932
933     /*
934      * bit(6) reserved = ‘111111’b;
935      * unsigned int(2) parallelismType;
936      */
937     avio_w8(pb, hvcc->parallelismType | 0xfc);
938
939     /*
940      * bit(6) reserved = ‘111111’b;
941      * unsigned int(2) chromaFormat;
942      */
943     avio_w8(pb, hvcc->chromaFormat | 0xfc);
944
945     /*
946      * bit(5) reserved = ‘11111’b;
947      * unsigned int(3) bitDepthLumaMinus8;
948      */
949     avio_w8(pb, hvcc->bitDepthLumaMinus8 | 0xf8);
950
951     /*
952      * bit(5) reserved = ‘11111’b;
953      * unsigned int(3) bitDepthChromaMinus8;
954      */
955     avio_w8(pb, hvcc->bitDepthChromaMinus8 | 0xf8);
956
957     /* bit(16) avgFrameRate; */
958     avio_wb16(pb, hvcc->avgFrameRate);
959
960     /*
961      * bit(2) constantFrameRate;
962      * bit(3) numTemporalLayers;
963      * bit(1) temporalIdNested;
964      * unsigned int(2) lengthSizeMinusOne;
965      */
966     avio_w8(pb, hvcc->constantFrameRate << 6 |
967                 hvcc->numTemporalLayers << 3 |
968                 hvcc->temporalIdNested  << 2 |
969                 hvcc->lengthSizeMinusOne);
970
971     /* unsigned int(8) numOfArrays; */
972     avio_w8(pb, hvcc->numOfArrays);
973
974     for (i = 0; i < hvcc->numOfArrays; i++) {
975         /*
976          * bit(1) array_completeness;
977          * unsigned int(1) reserved = 0;
978          * unsigned int(6) NAL_unit_type;
979          */
980         avio_w8(pb, hvcc->array[i].array_completeness << 7 |
981                     hvcc->array[i].NAL_unit_type & 0x3f);
982
983         /* unsigned int(16) numNalus; */
984         avio_wb16(pb, hvcc->array[i].numNalus);
985
986         for (j = 0; j < hvcc->array[i].numNalus; j++) {
987             /* unsigned int(16) nalUnitLength; */
988             avio_wb16(pb, hvcc->array[i].nalUnitLength[j]);
989
990             /* bit(8*nalUnitLength) nalUnit; */
991             avio_write(pb, hvcc->array[i].nalUnit[j],
992                        hvcc->array[i].nalUnitLength[j]);
993         }
994     }
995
996     return 0;
997 }
998
999 int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
1000                        int size, int filter_ps, int *ps_count)
1001 {
1002     int num_ps = 0, ret = 0;
1003     uint8_t *buf, *end, *start = NULL;
1004
1005     if (!filter_ps) {
1006         ret = ff_avc_parse_nal_units(pb, buf_in, size);
1007         goto end;
1008     }
1009
1010     ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
1011     if (ret < 0)
1012         goto end;
1013
1014     ret = 0;
1015     buf = start;
1016     end = start + size;
1017
1018     while (end - buf > 4) {
1019         uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1020         uint8_t type = (buf[4] >> 1) & 0x3f;
1021
1022         buf += 4;
1023
1024         switch (type) {
1025         case HEVC_NAL_VPS:
1026         case HEVC_NAL_SPS:
1027         case HEVC_NAL_PPS:
1028             num_ps++;
1029             break;
1030         default:
1031             ret += 4 + len;
1032             avio_wb32(pb, len);
1033             avio_write(pb, buf, len);
1034             break;
1035         }
1036
1037         buf += len;
1038     }
1039
1040 end:
1041     av_free(start);
1042     if (ps_count)
1043         *ps_count = num_ps;
1044     return ret;
1045 }
1046
1047 int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
1048                            int *size, int filter_ps, int *ps_count)
1049 {
1050     AVIOContext *pb;
1051     int ret;
1052
1053     ret = avio_open_dyn_buf(&pb);
1054     if (ret < 0)
1055         return ret;
1056
1057     ret   = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
1058     if (ret < 0) {
1059         ffio_free_dyn_buf(&pb);
1060         return ret;
1061     }
1062
1063     *size = avio_close_dyn_buf(pb, buf_out);
1064
1065     return 0;
1066 }
1067
1068 int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
1069                        int size, int ps_array_completeness)
1070 {
1071     HEVCDecoderConfigurationRecord hvcc;
1072     uint8_t *buf, *end, *start;
1073     int ret;
1074
1075     if (size < 6) {
1076         /* We can't write a valid hvcC from the provided data */
1077         return AVERROR_INVALIDDATA;
1078     } else if (*data == 1) {
1079         /* Data is already hvcC-formatted */
1080         avio_write(pb, data, size);
1081         return 0;
1082     } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
1083         /* Not a valid Annex B start code prefix */
1084         return AVERROR_INVALIDDATA;
1085     }
1086
1087     ret = ff_avc_parse_nal_units_buf(data, &start, &size);
1088     if (ret < 0)
1089         return ret;
1090
1091     hvcc_init(&hvcc);
1092
1093     buf = start;
1094     end = start + size;
1095
1096     while (end - buf > 4) {
1097         uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1098         uint8_t type = (buf[4] >> 1) & 0x3f;
1099
1100         buf += 4;
1101
1102         switch (type) {
1103         case HEVC_NAL_VPS:
1104         case HEVC_NAL_SPS:
1105         case HEVC_NAL_PPS:
1106         case HEVC_NAL_SEI_PREFIX:
1107         case HEVC_NAL_SEI_SUFFIX:
1108             ret = hvcc_add_nal_unit(buf, len, ps_array_completeness, &hvcc);
1109             if (ret < 0)
1110                 goto end;
1111             break;
1112         default:
1113             break;
1114         }
1115
1116         buf += len;
1117     }
1118
1119     ret = hvcc_write(pb, &hvcc);
1120
1121 end:
1122     hvcc_close(&hvcc);
1123     av_free(start);
1124     return ret;
1125 }