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