Browse code

movenc: write hvcC tag for HEVC.

(cherry picked from commit 20b40a597cdd4969cf1147d7c7efee2b6232524b)

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