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