OSDN Git Service

Fixup armv8-a building, and make multiarch builds work
[android-x86/external-ffmpeg.git] / 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_RPS_COUNT])
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_RPS_COUNT];
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_RPS_COUNT)
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     *dst_len = len;
673     return dst;
674 }
675
676
677
678 static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
679 {
680     skip_bits1(gb); // forbidden_zero_bit
681
682     *nal_type = get_bits(gb, 6);
683
684     /*
685      * nuh_layer_id          u(6)
686      * nuh_temporal_id_plus1 u(3)
687      */
688     skip_bits(gb, 9);
689 }
690
691 static int hvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
692                                    uint8_t nal_type, int ps_array_completeness,
693                                    HEVCDecoderConfigurationRecord *hvcc)
694 {
695     int ret;
696     uint8_t index;
697     uint16_t numNalus;
698     HVCCNALUnitArray *array;
699
700     for (index = 0; index < hvcc->numOfArrays; index++)
701         if (hvcc->array[index].NAL_unit_type == nal_type)
702             break;
703
704     if (index >= hvcc->numOfArrays) {
705         uint8_t i;
706
707         ret = av_reallocp_array(&hvcc->array, index + 1, sizeof(HVCCNALUnitArray));
708         if (ret < 0)
709             return ret;
710
711         for (i = hvcc->numOfArrays; i <= index; i++)
712             memset(&hvcc->array[i], 0, sizeof(HVCCNALUnitArray));
713         hvcc->numOfArrays = index + 1;
714     }
715
716     array    = &hvcc->array[index];
717     numNalus = array->numNalus;
718
719     ret = av_reallocp_array(&array->nalUnit, numNalus + 1, sizeof(uint8_t*));
720     if (ret < 0)
721         return ret;
722
723     ret = av_reallocp_array(&array->nalUnitLength, numNalus + 1, sizeof(uint16_t));
724     if (ret < 0)
725         return ret;
726
727     array->nalUnit      [numNalus] = nal_buf;
728     array->nalUnitLength[numNalus] = nal_size;
729     array->NAL_unit_type           = nal_type;
730     array->numNalus++;
731
732     /*
733      * When the sample entry name is ‘hvc1’, the default and mandatory value of
734      * array_completeness is 1 for arrays of all types of parameter sets, and 0
735      * for all other arrays. When the sample entry name is ‘hev1’, the default
736      * value of array_completeness is 0 for all arrays.
737      */
738     if (nal_type == HEVC_NAL_VPS || nal_type == HEVC_NAL_SPS || nal_type == HEVC_NAL_PPS)
739         array->array_completeness = ps_array_completeness;
740
741     return 0;
742 }
743
744 static int hvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
745                              int ps_array_completeness,
746                              HEVCDecoderConfigurationRecord *hvcc)
747 {
748     int ret = 0;
749     GetBitContext gbc;
750     uint8_t nal_type;
751     uint8_t *rbsp_buf;
752     uint32_t rbsp_size;
753
754     rbsp_buf = nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size);
755     if (!rbsp_buf) {
756         ret = AVERROR(ENOMEM);
757         goto end;
758     }
759
760     ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
761     if (ret < 0)
762         goto end;
763
764     nal_unit_parse_header(&gbc, &nal_type);
765
766     /*
767      * Note: only 'declarative' SEI messages are allowed in
768      * hvcC. Perhaps the SEI playload type should be checked
769      * and non-declarative SEI messages discarded?
770      */
771     switch (nal_type) {
772     case HEVC_NAL_VPS:
773     case HEVC_NAL_SPS:
774     case HEVC_NAL_PPS:
775     case HEVC_NAL_SEI_PREFIX:
776     case HEVC_NAL_SEI_SUFFIX:
777         ret = hvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
778                                       ps_array_completeness, hvcc);
779         if (ret < 0)
780             goto end;
781         else if (nal_type == HEVC_NAL_VPS)
782             ret = hvcc_parse_vps(&gbc, hvcc);
783         else if (nal_type == HEVC_NAL_SPS)
784             ret = hvcc_parse_sps(&gbc, hvcc);
785         else if (nal_type == HEVC_NAL_PPS)
786             ret = hvcc_parse_pps(&gbc, hvcc);
787         if (ret < 0)
788             goto end;
789         break;
790     default:
791         ret = AVERROR_INVALIDDATA;
792         goto end;
793     }
794
795 end:
796     av_free(rbsp_buf);
797     return ret;
798 }
799
800 static void hvcc_init(HEVCDecoderConfigurationRecord *hvcc)
801 {
802     memset(hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
803     hvcc->configurationVersion = 1;
804     hvcc->lengthSizeMinusOne   = 3; // 4 bytes
805
806     /*
807      * The following fields have all their valid bits set by default,
808      * the ProfileTierLevel parsing code will unset them when needed.
809      */
810     hvcc->general_profile_compatibility_flags = 0xffffffff;
811     hvcc->general_constraint_indicator_flags  = 0xffffffffffff;
812
813     /*
814      * Initialize this field with an invalid value which can be used to detect
815      * whether we didn't see any VUI (in which case it should be reset to zero).
816      */
817     hvcc->min_spatial_segmentation_idc = MAX_SPATIAL_SEGMENTATION + 1;
818 }
819
820 static void hvcc_close(HEVCDecoderConfigurationRecord *hvcc)
821 {
822     uint8_t i;
823
824     for (i = 0; i < hvcc->numOfArrays; i++) {
825         hvcc->array[i].numNalus = 0;
826         av_freep(&hvcc->array[i].nalUnit);
827         av_freep(&hvcc->array[i].nalUnitLength);
828     }
829
830     hvcc->numOfArrays = 0;
831     av_freep(&hvcc->array);
832 }
833
834 static int hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc)
835 {
836     uint8_t i;
837     uint16_t j, vps_count = 0, sps_count = 0, pps_count = 0;
838
839     /*
840      * We only support writing HEVCDecoderConfigurationRecord version 1.
841      */
842     hvcc->configurationVersion = 1;
843
844     /*
845      * If min_spatial_segmentation_idc is invalid, reset to 0 (unspecified).
846      */
847     if (hvcc->min_spatial_segmentation_idc > MAX_SPATIAL_SEGMENTATION)
848         hvcc->min_spatial_segmentation_idc = 0;
849
850     /*
851      * parallelismType indicates the type of parallelism that is used to meet
852      * the restrictions imposed by min_spatial_segmentation_idc when the value
853      * of min_spatial_segmentation_idc is greater than 0.
854      */
855     if (!hvcc->min_spatial_segmentation_idc)
856         hvcc->parallelismType = 0;
857
858     /*
859      * It's unclear how to properly compute these fields, so
860      * let's always set them to values meaning 'unspecified'.
861      */
862     hvcc->avgFrameRate      = 0;
863     hvcc->constantFrameRate = 0;
864
865     av_log(NULL, AV_LOG_TRACE,  "configurationVersion:                %"PRIu8"\n",
866             hvcc->configurationVersion);
867     av_log(NULL, AV_LOG_TRACE,  "general_profile_space:               %"PRIu8"\n",
868             hvcc->general_profile_space);
869     av_log(NULL, AV_LOG_TRACE,  "general_tier_flag:                   %"PRIu8"\n",
870             hvcc->general_tier_flag);
871     av_log(NULL, AV_LOG_TRACE,  "general_profile_idc:                 %"PRIu8"\n",
872             hvcc->general_profile_idc);
873     av_log(NULL, AV_LOG_TRACE, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
874             hvcc->general_profile_compatibility_flags);
875     av_log(NULL, AV_LOG_TRACE, "general_constraint_indicator_flags:  0x%012"PRIx64"\n",
876             hvcc->general_constraint_indicator_flags);
877     av_log(NULL, AV_LOG_TRACE,  "general_level_idc:                   %"PRIu8"\n",
878             hvcc->general_level_idc);
879     av_log(NULL, AV_LOG_TRACE,  "min_spatial_segmentation_idc:        %"PRIu16"\n",
880             hvcc->min_spatial_segmentation_idc);
881     av_log(NULL, AV_LOG_TRACE,  "parallelismType:                     %"PRIu8"\n",
882             hvcc->parallelismType);
883     av_log(NULL, AV_LOG_TRACE,  "chromaFormat:                        %"PRIu8"\n",
884             hvcc->chromaFormat);
885     av_log(NULL, AV_LOG_TRACE,  "bitDepthLumaMinus8:                  %"PRIu8"\n",
886             hvcc->bitDepthLumaMinus8);
887     av_log(NULL, AV_LOG_TRACE,  "bitDepthChromaMinus8:                %"PRIu8"\n",
888             hvcc->bitDepthChromaMinus8);
889     av_log(NULL, AV_LOG_TRACE,  "avgFrameRate:                        %"PRIu16"\n",
890             hvcc->avgFrameRate);
891     av_log(NULL, AV_LOG_TRACE,  "constantFrameRate:                   %"PRIu8"\n",
892             hvcc->constantFrameRate);
893     av_log(NULL, AV_LOG_TRACE,  "numTemporalLayers:                   %"PRIu8"\n",
894             hvcc->numTemporalLayers);
895     av_log(NULL, AV_LOG_TRACE,  "temporalIdNested:                    %"PRIu8"\n",
896             hvcc->temporalIdNested);
897     av_log(NULL, AV_LOG_TRACE,  "lengthSizeMinusOne:                  %"PRIu8"\n",
898             hvcc->lengthSizeMinusOne);
899     av_log(NULL, AV_LOG_TRACE,  "numOfArrays:                         %"PRIu8"\n",
900             hvcc->numOfArrays);
901     for (i = 0; i < hvcc->numOfArrays; i++) {
902         av_log(NULL, AV_LOG_TRACE, "array_completeness[%"PRIu8"]:               %"PRIu8"\n",
903                 i, hvcc->array[i].array_completeness);
904         av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%"PRIu8"]:                    %"PRIu8"\n",
905                 i, hvcc->array[i].NAL_unit_type);
906         av_log(NULL, AV_LOG_TRACE, "numNalus[%"PRIu8"]:                         %"PRIu16"\n",
907                 i, hvcc->array[i].numNalus);
908         for (j = 0; j < hvcc->array[i].numNalus; j++)
909             av_log(NULL, AV_LOG_TRACE,
910                     "nalUnitLength[%"PRIu8"][%"PRIu16"]:                 %"PRIu16"\n",
911                     i, j, hvcc->array[i].nalUnitLength[j]);
912     }
913
914     /*
915      * We need at least one of each: VPS, SPS and PPS.
916      */
917     for (i = 0; i < hvcc->numOfArrays; i++)
918         switch (hvcc->array[i].NAL_unit_type) {
919         case HEVC_NAL_VPS:
920             vps_count += hvcc->array[i].numNalus;
921             break;
922         case HEVC_NAL_SPS:
923             sps_count += hvcc->array[i].numNalus;
924             break;
925         case HEVC_NAL_PPS:
926             pps_count += hvcc->array[i].numNalus;
927             break;
928         default:
929             break;
930         }
931     if (!vps_count || vps_count > HEVC_MAX_VPS_COUNT ||
932         !sps_count || sps_count > HEVC_MAX_SPS_COUNT ||
933         !pps_count || pps_count > HEVC_MAX_PPS_COUNT)
934         return AVERROR_INVALIDDATA;
935
936     /* unsigned int(8) configurationVersion = 1; */
937     avio_w8(pb, hvcc->configurationVersion);
938
939     /*
940      * unsigned int(2) general_profile_space;
941      * unsigned int(1) general_tier_flag;
942      * unsigned int(5) general_profile_idc;
943      */
944     avio_w8(pb, hvcc->general_profile_space << 6 |
945                 hvcc->general_tier_flag     << 5 |
946                 hvcc->general_profile_idc);
947
948     /* unsigned int(32) general_profile_compatibility_flags; */
949     avio_wb32(pb, hvcc->general_profile_compatibility_flags);
950
951     /* unsigned int(48) general_constraint_indicator_flags; */
952     avio_wb32(pb, hvcc->general_constraint_indicator_flags >> 16);
953     avio_wb16(pb, hvcc->general_constraint_indicator_flags);
954
955     /* unsigned int(8) general_level_idc; */
956     avio_w8(pb, hvcc->general_level_idc);
957
958     /*
959      * bit(4) reserved = ‘1111’b;
960      * unsigned int(12) min_spatial_segmentation_idc;
961      */
962     avio_wb16(pb, hvcc->min_spatial_segmentation_idc | 0xf000);
963
964     /*
965      * bit(6) reserved = ‘111111’b;
966      * unsigned int(2) parallelismType;
967      */
968     avio_w8(pb, hvcc->parallelismType | 0xfc);
969
970     /*
971      * bit(6) reserved = ‘111111’b;
972      * unsigned int(2) chromaFormat;
973      */
974     avio_w8(pb, hvcc->chromaFormat | 0xfc);
975
976     /*
977      * bit(5) reserved = ‘11111’b;
978      * unsigned int(3) bitDepthLumaMinus8;
979      */
980     avio_w8(pb, hvcc->bitDepthLumaMinus8 | 0xf8);
981
982     /*
983      * bit(5) reserved = ‘11111’b;
984      * unsigned int(3) bitDepthChromaMinus8;
985      */
986     avio_w8(pb, hvcc->bitDepthChromaMinus8 | 0xf8);
987
988     /* bit(16) avgFrameRate; */
989     avio_wb16(pb, hvcc->avgFrameRate);
990
991     /*
992      * bit(2) constantFrameRate;
993      * bit(3) numTemporalLayers;
994      * bit(1) temporalIdNested;
995      * unsigned int(2) lengthSizeMinusOne;
996      */
997     avio_w8(pb, hvcc->constantFrameRate << 6 |
998                 hvcc->numTemporalLayers << 3 |
999                 hvcc->temporalIdNested  << 2 |
1000                 hvcc->lengthSizeMinusOne);
1001
1002     /* unsigned int(8) numOfArrays; */
1003     avio_w8(pb, hvcc->numOfArrays);
1004
1005     for (i = 0; i < hvcc->numOfArrays; i++) {
1006         /*
1007          * bit(1) array_completeness;
1008          * unsigned int(1) reserved = 0;
1009          * unsigned int(6) NAL_unit_type;
1010          */
1011         avio_w8(pb, hvcc->array[i].array_completeness << 7 |
1012                     hvcc->array[i].NAL_unit_type & 0x3f);
1013
1014         /* unsigned int(16) numNalus; */
1015         avio_wb16(pb, hvcc->array[i].numNalus);
1016
1017         for (j = 0; j < hvcc->array[i].numNalus; j++) {
1018             /* unsigned int(16) nalUnitLength; */
1019             avio_wb16(pb, hvcc->array[i].nalUnitLength[j]);
1020
1021             /* bit(8*nalUnitLength) nalUnit; */
1022             avio_write(pb, hvcc->array[i].nalUnit[j],
1023                        hvcc->array[i].nalUnitLength[j]);
1024         }
1025     }
1026
1027     return 0;
1028 }
1029
1030 int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
1031                        int size, int filter_ps, int *ps_count)
1032 {
1033     int num_ps = 0, ret = 0;
1034     uint8_t *buf, *end, *start = NULL;
1035
1036     if (!filter_ps) {
1037         ret = ff_avc_parse_nal_units(pb, buf_in, size);
1038         goto end;
1039     }
1040
1041     ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
1042     if (ret < 0)
1043         goto end;
1044
1045     ret = 0;
1046     buf = start;
1047     end = start + size;
1048
1049     while (end - buf > 4) {
1050         uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1051         uint8_t type = (buf[4] >> 1) & 0x3f;
1052
1053         buf += 4;
1054
1055         switch (type) {
1056         case HEVC_NAL_VPS:
1057         case HEVC_NAL_SPS:
1058         case HEVC_NAL_PPS:
1059             num_ps++;
1060             break;
1061         default:
1062             ret += 4 + len;
1063             avio_wb32(pb, len);
1064             avio_write(pb, buf, len);
1065             break;
1066         }
1067
1068         buf += len;
1069     }
1070
1071 end:
1072     av_free(start);
1073     if (ps_count)
1074         *ps_count = num_ps;
1075     return ret;
1076 }
1077
1078 int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
1079                            int *size, int filter_ps, int *ps_count)
1080 {
1081     AVIOContext *pb;
1082     int ret;
1083
1084     ret = avio_open_dyn_buf(&pb);
1085     if (ret < 0)
1086         return ret;
1087
1088     ret   = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
1089     *size = avio_close_dyn_buf(pb, buf_out);
1090
1091     return ret;
1092 }
1093
1094 int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
1095                        int size, int ps_array_completeness)
1096 {
1097     int ret = 0;
1098     uint8_t *buf, *end, *start = NULL;
1099     HEVCDecoderConfigurationRecord hvcc;
1100
1101     hvcc_init(&hvcc);
1102
1103     if (size < 6) {
1104         /* We can't write a valid hvcC from the provided data */
1105         ret = AVERROR_INVALIDDATA;
1106         goto end;
1107     } else if (*data == 1) {
1108         /* Data is already hvcC-formatted */
1109         avio_write(pb, data, size);
1110         goto end;
1111     } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
1112         /* Not a valid Annex B start code prefix */
1113         ret = AVERROR_INVALIDDATA;
1114         goto end;
1115     }
1116
1117     ret = ff_avc_parse_nal_units_buf(data, &start, &size);
1118     if (ret < 0)
1119         goto end;
1120
1121     buf = start;
1122     end = start + size;
1123
1124     while (end - buf > 4) {
1125         uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1126         uint8_t type = (buf[4] >> 1) & 0x3f;
1127
1128         buf += 4;
1129
1130         switch (type) {
1131         case HEVC_NAL_VPS:
1132         case HEVC_NAL_SPS:
1133         case HEVC_NAL_PPS:
1134         case HEVC_NAL_SEI_PREFIX:
1135         case HEVC_NAL_SEI_SUFFIX:
1136             ret = hvcc_add_nal_unit(buf, len, ps_array_completeness, &hvcc);
1137             if (ret < 0)
1138                 goto end;
1139             break;
1140         default:
1141             break;
1142         }
1143
1144         buf += len;
1145     }
1146
1147     ret = hvcc_write(pb, &hvcc);
1148
1149 end:
1150     hvcc_close(&hvcc);
1151     av_free(start);
1152     return ret;
1153 }