Coverage Report

Created: 2026-02-14 08:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/h264_nal.c
Line
Count
Source
1
/*****************************************************************************
2
 * Copyright © 2010-2014 VideoLAN
3
 *
4
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program 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
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19
 *****************************************************************************/
20
#ifdef HAVE_CONFIG_H
21
# include "config.h"
22
#endif
23
24
#include <assert.h>
25
26
#include "h264_nal.h"
27
#include "hxxx_nal.h"
28
#include "hxxx_ep3b.h"
29
#include "iso_color_tables.h"
30
31
#include <vlc_bits.h>
32
#include <vlc_boxes.h>
33
#include <vlc_es.h>
34
#include <limits.h>
35
36
/* H264 Level limits from Table A-1 */
37
typedef struct
38
{
39
    unsigned i_max_dpb_mbs;
40
} h264_level_limits_t;
41
42
enum h264_level_numbers_e
43
{
44
    H264_LEVEL_NUMBER_1_B = 9, /* special level not following the 10x rule */
45
    H264_LEVEL_NUMBER_1   = 10,
46
    H264_LEVEL_NUMBER_1_1 = 11,
47
    H264_LEVEL_NUMBER_1_2 = 12,
48
    H264_LEVEL_NUMBER_1_3 = 13,
49
    H264_LEVEL_NUMBER_2   = 20,
50
    H264_LEVEL_NUMBER_2_1 = 21,
51
    H264_LEVEL_NUMBER_2_2 = 22,
52
    H264_LEVEL_NUMBER_3   = 30,
53
    H264_LEVEL_NUMBER_3_1 = 31,
54
    H264_LEVEL_NUMBER_3_2 = 32,
55
    H264_LEVEL_NUMBER_4   = 40,
56
    H264_LEVEL_NUMBER_4_1 = 41,
57
    H264_LEVEL_NUMBER_4_2 = 42,
58
    H264_LEVEL_NUMBER_5   = 50,
59
    H264_LEVEL_NUMBER_5_1 = 51,
60
    H264_LEVEL_NUMBER_5_2 = 52,
61
    H264_LEVEL_NUMBER_6   = 60,
62
    H264_LEVEL_NUMBER_6_1 = 61,
63
    H264_LEVEL_NUMBER_6_2 = 62,
64
};
65
66
const struct
67
{
68
    const uint16_t i_level;
69
    const h264_level_limits_t limits;
70
} h264_levels_limits[] = {
71
    { H264_LEVEL_NUMBER_1_B, { 396 } },
72
    { H264_LEVEL_NUMBER_1,   { 396 } },
73
    { H264_LEVEL_NUMBER_1_1, { 900 } },
74
    { H264_LEVEL_NUMBER_1_2, { 2376 } },
75
    { H264_LEVEL_NUMBER_1_3, { 2376 } },
76
    { H264_LEVEL_NUMBER_2,   { 2376 } },
77
    { H264_LEVEL_NUMBER_2_1, { 4752 } },
78
    { H264_LEVEL_NUMBER_2_2, { 8100 } },
79
    { H264_LEVEL_NUMBER_3,   { 8100 } },
80
    { H264_LEVEL_NUMBER_3_1, { 18000 } },
81
    { H264_LEVEL_NUMBER_3_2, { 20480 } },
82
    { H264_LEVEL_NUMBER_4,   { 32768 } },
83
    { H264_LEVEL_NUMBER_4_1, { 32768 } },
84
    { H264_LEVEL_NUMBER_4_2, { 34816 } },
85
    { H264_LEVEL_NUMBER_5,   { 110400 } },
86
    { H264_LEVEL_NUMBER_5_1, { 184320 } },
87
    { H264_LEVEL_NUMBER_5_2, { 184320 } },
88
    { H264_LEVEL_NUMBER_6,   { 696320 } },
89
    { H264_LEVEL_NUMBER_6_1, { 696320 } },
90
    { H264_LEVEL_NUMBER_6_2, { 696320 } },
91
};
92
93
/*
94
 * For avcC specification, see ISO/IEC 14496-15,
95
 * For Annex B specification, see ISO/IEC 14496-10
96
 */
97
98
bool h264_isavcC( const uint8_t *p_buf, size_t i_buf )
99
2.14k
{
100
2.14k
    return ( i_buf >= H264_MIN_AVCC_SIZE &&
101
2.14k
             p_buf[0] != 0x00 &&
102
2.08k
             p_buf[1] != 0x00
103
/*          /!\Broken quicktime streams does not respect reserved bits
104
            (p_buf[4] & 0xFC) == 0xFC &&
105
            (p_buf[4] & 0x03) != 0x02 &&
106
            (p_buf[5] & 0x1F) > 0x00 */
107
2.14k
            );
108
2.14k
}
109
110
static size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
111
2.06k
{
112
2.06k
    size_t i_total = 0;
113
114
2.06k
    if( i_buf < H264_MIN_AVCC_SIZE )
115
0
        return 0;
116
117
2.06k
    p_buf += 5;
118
2.06k
    i_buf -= 5;
119
120
6.07k
    for ( unsigned int j = 0; j < 2; j++ )
121
4.08k
    {
122
        /* First time is SPS, Second is PPS */
123
4.08k
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
124
4.08k
        p_buf++; i_buf--;
125
126
14.0k
        for ( unsigned int i = 0; i < i_loop_end; i++ )
127
10.0k
        {
128
10.0k
            if( i_buf < 2 )
129
1
                return 0;
130
131
10.0k
            uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
132
10.0k
            if(i_nal_size > i_buf - 2)
133
69
                return 0;
134
9.98k
            i_total += i_nal_size + 4;
135
9.98k
            p_buf += i_nal_size + 2;
136
9.98k
            i_buf -= i_nal_size + 2;
137
9.98k
        }
138
139
4.01k
        if( j == 0 && i_buf < 1 )
140
1
            return 0;
141
4.01k
    }
142
1.99k
    return i_total;
143
2.06k
}
144
145
uint8_t *h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
146
                                  size_t *pi_result, uint8_t *pi_nal_length_size )
147
2.06k
{
148
2.06k
    *pi_result = get_avcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does check min size */
149
2.06k
    if( *pi_result == 0 )
150
78
        return NULL;
151
152
    /* Read infos in first 6 bytes */
153
1.98k
    if ( pi_nal_length_size )
154
1.98k
        *pi_nal_length_size = (p_buf[4] & 0x03) + 1;
155
156
1.98k
    uint8_t *p_ret;
157
1.98k
    uint8_t *p_out_buf = p_ret = malloc( *pi_result );
158
1.98k
    if( !p_out_buf )
159
0
    {
160
0
        *pi_result = 0;
161
0
        return NULL;
162
0
    }
163
164
1.98k
    p_buf += 5;
165
166
5.95k
    for ( unsigned int j = 0; j < 2; j++ )
167
3.96k
    {
168
3.96k
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
169
3.96k
        p_buf++;
170
171
13.7k
        for ( unsigned int i = 0; i < i_loop_end; i++)
172
9.79k
        {
173
9.79k
            uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
174
9.79k
            p_buf += 2;
175
176
9.79k
            memcpy( p_out_buf, annexb_startcode4, 4 );
177
9.79k
            p_out_buf += 4;
178
179
9.79k
            memcpy( p_out_buf, p_buf, i_nal_size );
180
9.79k
            p_out_buf += i_nal_size;
181
9.79k
            p_buf += i_nal_size;
182
9.79k
        }
183
3.96k
    }
184
185
1.98k
    return p_ret;
186
1.98k
}
187
188
void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
189
                             uint8_t i_nal_length_size )
190
0
{
191
0
    uint32_t nal_len = 0;
192
0
    uint8_t nal_pos = 0;
193
194
0
    if( i_nal_length_size != 4 )
195
0
        return;
196
197
    /* This only works for a NAL length size of 4 */
198
    /* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
199
0
    while( i_len > 0 )
200
0
    {
201
0
        if( nal_pos < i_nal_length_size ) {
202
0
            unsigned int i;
203
0
            for( i = 0; nal_pos < i_nal_length_size && i < i_len; i++, nal_pos++ ) {
204
0
                nal_len = (nal_len << 8) | p_buf[i];
205
0
                p_buf[i] = 0;
206
0
            }
207
0
            if( nal_pos < i_nal_length_size )
208
0
                return;
209
0
            p_buf[i - 1] = 1;
210
0
            p_buf += i;
211
0
            i_len -= i;
212
0
        }
213
0
        if( nal_len > INT_MAX )
214
0
            return;
215
0
        if( nal_len > i_len )
216
0
        {
217
0
            nal_len -= i_len;
218
0
            return;
219
0
        }
220
0
        else
221
0
        {
222
0
            p_buf += nal_len;
223
0
            i_len -= nal_len;
224
0
            nal_len = 0;
225
0
            nal_pos = 0;
226
0
        }
227
0
    }
228
0
}
229
230
bool h264_AnnexB_get_spspps( const uint8_t *p_buf, size_t i_buf,
231
                             const uint8_t **pp_sps, size_t *p_sps_size,
232
                             const uint8_t **pp_pps, size_t *p_pps_size,
233
                             const uint8_t **pp_ext, size_t *p_ext_size )
234
0
{
235
0
    if( pp_sps ) { *p_sps_size = 0; *pp_sps = NULL; }
236
0
    if( pp_pps ) { *p_pps_size = 0; *pp_pps = NULL; }
237
0
    if( pp_ext ) { *p_ext_size = 0; *pp_ext = NULL; }
238
239
0
    hxxx_iterator_ctx_t it;
240
0
    hxxx_iterator_init( &it, p_buf, i_buf, 0 );
241
242
0
    const uint8_t *p_nal; size_t i_nal;
243
0
    while( hxxx_annexb_iterate_next( &it, &p_nal, &i_nal ) )
244
0
    {
245
0
        if( i_nal < 2 )
246
0
            continue;
247
248
0
        const enum h264_nal_unit_type_e i_nal_type = p_nal[0] & 0x1F;
249
250
0
        if ( i_nal_type <= H264_NAL_SLICE_IDR && i_nal_type != H264_NAL_UNKNOWN )
251
0
            break;
252
253
0
#define IFSET_NAL(type, var) \
254
0
    if( i_nal_type == type && pp_##var && *pp_##var == NULL )\
255
0
        { *pp_##var = p_nal; *p_##var##_size = i_nal; }
256
257
0
        IFSET_NAL(H264_NAL_SPS, sps)
258
0
        else
259
0
        IFSET_NAL(H264_NAL_PPS, pps)
260
0
        else
261
0
        IFSET_NAL(H264_NAL_SPS_EXT, ext);
262
0
#undef IFSET_NAL
263
0
    }
264
265
0
    return (pp_sps && *p_sps_size) || (pp_pps && *p_pps_size);
266
0
}
267
268
void h264_release_sps( h264_sequence_parameter_set_t *p_sps )
269
82.0k
{
270
82.0k
    free( p_sps );
271
82.0k
}
272
273
0
#define H264_CONSTRAINT_SET_FLAG(N) (0x80 >> N)
274
275
static bool h264_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
276
                                                    h264_sequence_parameter_set_t *p_sps )
277
82.0k
{
278
82.0k
    int i_tmp;
279
280
82.0k
    int i_profile_idc = bs_read( p_bs, 8 );
281
82.0k
    p_sps->i_profile = i_profile_idc;
282
82.0k
    p_sps->i_constraint_set_flags = bs_read( p_bs, 8 );
283
82.0k
    p_sps->i_level = bs_read( p_bs, 8 );
284
    /* sps id */
285
82.0k
    uint32_t i_sps_id = bs_read_ue( p_bs );
286
82.0k
    if( i_sps_id > H264_SPS_ID_MAX )
287
499
        return false;
288
81.5k
    p_sps->i_id = i_sps_id;
289
290
81.5k
    if( i_profile_idc == PROFILE_H264_HIGH ||
291
67.2k
        i_profile_idc == PROFILE_H264_HIGH_10 ||
292
66.9k
        i_profile_idc == PROFILE_H264_HIGH_422 ||
293
66.2k
        i_profile_idc == PROFILE_H264_HIGH_444 || /* Old one, no longer on spec */
294
65.6k
        i_profile_idc == PROFILE_H264_HIGH_444_PREDICTIVE ||
295
65.2k
        i_profile_idc == PROFILE_H264_CAVLC_INTRA ||
296
62.0k
        i_profile_idc == PROFILE_H264_SVC_BASELINE ||
297
61.7k
        i_profile_idc == PROFILE_H264_SVC_HIGH ||
298
61.2k
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_HIGH ||
299
59.0k
        i_profile_idc == PROFILE_H264_MVC_STEREO_HIGH ||
300
57.8k
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH ||
301
57.5k
        i_profile_idc == PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH ||
302
57.2k
        i_profile_idc == PROFILE_H264_MFC_HIGH )
303
24.5k
    {
304
        /* chroma_format_idc */
305
24.5k
        p_sps->i_chroma_idc = bs_read_ue( p_bs );
306
24.5k
        if( p_sps->i_chroma_idc > 3 )
307
3.03k
            return false;
308
21.5k
        if( p_sps->i_chroma_idc == 3 )
309
4.32k
            p_sps->b_separate_colour_planes_flag = bs_read1( p_bs );
310
17.1k
        else
311
17.1k
            p_sps->b_separate_colour_planes_flag = 0;
312
        /* bit_depth_luma_minus8 */
313
21.5k
        p_sps->i_bit_depth_luma = bs_read_ue( p_bs ) + 8;
314
        /* bit_depth_chroma_minus8 */
315
21.5k
        p_sps->i_bit_depth_chroma = bs_read_ue( p_bs ) + 8;
316
        /* qpprime_y_zero_transform_bypass_flag */
317
21.5k
        bs_skip( p_bs, 1 );
318
        /* seq_scaling_matrix_present_flag */
319
21.5k
        i_tmp = bs_read( p_bs, 1 );
320
21.5k
        if( i_tmp )
321
5.92k
        {
322
46.6k
            for( int i = 0; i < ((3 != p_sps->i_chroma_idc) ? 8 : 12); i++ )
323
42.3k
            {
324
                /* seq_scaling_list_present_flag[i] */
325
42.3k
                i_tmp = bs_read( p_bs, 1 );
326
42.3k
                if( !i_tmp )
327
34.8k
                    continue;
328
7.52k
                const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
329
                /* scaling_list (...) */
330
7.52k
                int i_lastscale = 8;
331
7.52k
                int i_nextscale = 8;
332
133k
                for( int j = 0; j < i_size_of_scaling_list; j++ )
333
127k
                {
334
127k
                    if( i_nextscale != 0 )
335
119k
                    {
336
                        /* delta_scale */
337
119k
                        i_tmp = bs_read_se( p_bs );
338
119k
                        if(i_tmp < -128 || i_tmp > 127)
339
1.57k
                          return false;
340
118k
                        i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
341
                        /* useDefaultScalingMatrixFlag = ... */
342
118k
                    }
343
                    /* scalinglist[j] */
344
125k
                    i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
345
125k
                }
346
7.52k
            }
347
5.92k
        }
348
21.5k
    }
349
57.0k
    else
350
57.0k
    {
351
57.0k
        p_sps->i_chroma_idc = 1; /* Not present == inferred to 4:2:0 */
352
57.0k
        p_sps->i_bit_depth_luma = 8;
353
57.0k
        p_sps->i_bit_depth_chroma = 8;
354
57.0k
    }
355
356
    /* Skip i_log2_max_frame_num */
357
76.9k
    p_sps->i_log2_max_frame_num = bs_read_ue( p_bs );
358
76.9k
    if( (uint_fast32_t) p_sps->i_log2_max_frame_num > 12)
359
4.60k
        p_sps->i_log2_max_frame_num = 12;
360
361
    /* Read poc_type */
362
76.9k
    p_sps->i_pic_order_cnt_type = bs_read_ue( p_bs );
363
76.9k
    if( p_sps->i_pic_order_cnt_type == 0 )
364
48.5k
    {
365
        /* skip i_log2_max_poc_lsb */
366
48.5k
        p_sps->i_log2_max_pic_order_cnt_lsb = bs_read_ue( p_bs );
367
48.5k
        if( p_sps->i_log2_max_pic_order_cnt_lsb > 12 )
368
2.45k
            p_sps->i_log2_max_pic_order_cnt_lsb = 12;
369
48.5k
    }
370
28.3k
    else if( p_sps->i_pic_order_cnt_type == 1 )
371
13.2k
    {
372
13.2k
        p_sps->i_delta_pic_order_always_zero_flag = bs_read( p_bs, 1 );
373
13.2k
        p_sps->offset_for_non_ref_pic = bs_read_se( p_bs );
374
13.2k
        p_sps->offset_for_top_to_bottom_field = bs_read_se( p_bs );
375
13.2k
        p_sps->i_num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue( p_bs );
376
13.2k
        if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 255 )
377
618
            return false;
378
99.9k
        for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ )
379
87.3k
            p_sps->offset_for_ref_frame[i] = bs_read_se( p_bs );
380
12.6k
    }
381
    /* i_num_ref_frames */
382
76.3k
    bs_read_ue( p_bs );
383
    /* b_gaps_in_frame_num_value_allowed */
384
76.3k
    bs_skip( p_bs, 1 );
385
386
    /* Read size */
387
76.3k
    p_sps->pic_width_in_mbs_minus1 = bs_read_ue( p_bs );
388
76.3k
    p_sps->pic_height_in_map_units_minus1 = bs_read_ue( p_bs );
389
390
    /* b_frame_mbs_only */
391
76.3k
    p_sps->frame_mbs_only_flag = bs_read( p_bs, 1 );
392
76.3k
    if( !p_sps->frame_mbs_only_flag )
393
40.0k
        p_sps->mb_adaptive_frame_field_flag = bs_read( p_bs, 1 );
394
395
    /* b_direct8x8_inference */
396
76.3k
    bs_skip( p_bs, 1 );
397
398
    /* crop */
399
76.3k
    if( bs_read1( p_bs ) ) /* frame_cropping_flag */
400
29.6k
    {
401
29.6k
        p_sps->frame_crop.left_offset = bs_read_ue( p_bs );
402
29.6k
        p_sps->frame_crop.right_offset = bs_read_ue( p_bs );
403
29.6k
        p_sps->frame_crop.top_offset = bs_read_ue( p_bs );
404
29.6k
        p_sps->frame_crop.bottom_offset = bs_read_ue( p_bs );
405
29.6k
    }
406
407
    /* vui */
408
76.3k
    p_sps->vui_parameters_present_flag = bs_read( p_bs, 1 );
409
76.3k
    if( p_sps->vui_parameters_present_flag )
410
41.5k
    {
411
        /* read the aspect ratio part if any */
412
41.5k
        i_tmp = bs_read( p_bs, 1 );
413
41.5k
        if( i_tmp )
414
18.0k
        {
415
18.0k
            static const struct { int w, h; } sar[17] =
416
18.0k
            {
417
18.0k
                { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
418
18.0k
                { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
419
18.0k
                { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
420
18.0k
                { 64, 33 }, { 160,99 }, {  4,  3 }, {  3,  2 },
421
18.0k
                {  2,  1 },
422
18.0k
            };
423
18.0k
            int i_sar = bs_read( p_bs, 8 );
424
18.0k
            int w, h;
425
426
18.0k
            if( i_sar < 17 )
427
1.41k
            {
428
1.41k
                w = sar[i_sar].w;
429
1.41k
                h = sar[i_sar].h;
430
1.41k
            }
431
16.6k
            else if( i_sar == 255 )
432
4.81k
            {
433
4.81k
                w = bs_read( p_bs, 16 );
434
4.81k
                h = bs_read( p_bs, 16 );
435
4.81k
            }
436
11.8k
            else
437
11.8k
            {
438
11.8k
                w = 0;
439
11.8k
                h = 0;
440
11.8k
            }
441
442
18.0k
            if( w != 0 && h != 0 )
443
4.59k
            {
444
4.59k
                p_sps->vui.i_sar_num = w;
445
4.59k
                p_sps->vui.i_sar_den = h;
446
4.59k
            }
447
13.4k
            else
448
13.4k
            {
449
13.4k
                p_sps->vui.i_sar_num = 1;
450
13.4k
                p_sps->vui.i_sar_den = 1;
451
13.4k
            }
452
18.0k
        }
453
454
        /* overscan */
455
41.5k
        i_tmp = bs_read( p_bs, 1 );
456
41.5k
        if ( i_tmp )
457
15.6k
            bs_skip( p_bs, 1 );
458
459
        /* video signal type */
460
41.5k
        i_tmp = bs_read( p_bs, 1 );
461
41.5k
        if( i_tmp )
462
15.6k
        {
463
15.6k
            bs_skip( p_bs, 3 );
464
15.6k
            p_sps->vui.colour.b_full_range = bs_read( p_bs, 1 );
465
            /* colour desc */
466
15.6k
            i_tmp = bs_read( p_bs, 1 );
467
15.6k
            if ( i_tmp )
468
6.31k
            {
469
6.31k
                p_sps->vui.colour.i_colour_primaries = bs_read( p_bs, 8 );
470
6.31k
                p_sps->vui.colour.i_transfer_characteristics = bs_read( p_bs, 8 );
471
6.31k
                p_sps->vui.colour.i_matrix_coefficients = bs_read( p_bs, 8 );
472
6.31k
            }
473
9.32k
            else
474
9.32k
            {
475
9.32k
                p_sps->vui.colour.i_colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
476
9.32k
                p_sps->vui.colour.i_transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
477
9.32k
                p_sps->vui.colour.i_matrix_coefficients = ISO_23001_8_MC_UNSPECIFIED;
478
9.32k
            }
479
15.6k
        }
480
481
        /* chroma loc info */
482
41.5k
        i_tmp = bs_read( p_bs, 1 );
483
41.5k
        if( i_tmp )
484
12.2k
        {
485
12.2k
            bs_read_ue( p_bs );
486
12.2k
            bs_read_ue( p_bs );
487
12.2k
        }
488
489
        /* timing info */
490
41.5k
        p_sps->vui.b_timing_info_present_flag = bs_read( p_bs, 1 );
491
41.5k
        if( p_sps->vui.b_timing_info_present_flag )
492
7.89k
        {
493
7.89k
            p_sps->vui.i_num_units_in_tick = bs_read( p_bs, 32 );
494
7.89k
            p_sps->vui.i_time_scale = bs_read( p_bs, 32 );
495
7.89k
            p_sps->vui.b_fixed_frame_rate = bs_read( p_bs, 1 );
496
7.89k
        }
497
498
        /* Nal hrd & VC1 hrd parameters */
499
41.5k
        p_sps->vui.b_hrd_parameters_present_flag = false;
500
115k
        for ( int i=0; i<2; i++ )
501
79.5k
        {
502
79.5k
            i_tmp = bs_read( p_bs, 1 );
503
79.5k
            if( i_tmp )
504
19.1k
            {
505
19.1k
                p_sps->vui.b_hrd_parameters_present_flag = true;
506
19.1k
                uint32_t count = bs_read_ue( p_bs ) + 1;
507
19.1k
                if( count > 31 )
508
812
                    return false;
509
18.3k
                bs_skip( p_bs, 4 );
510
18.3k
                bs_skip( p_bs, 4 );
511
49.9k
                for( uint32_t j = 0; j < count; j++ )
512
36.4k
                {
513
36.4k
                    bs_read_ue( p_bs );
514
36.4k
                    bs_read_ue( p_bs );
515
36.4k
                    bs_skip( p_bs, 1 );
516
36.4k
                    if( bs_error( p_bs ) )
517
4.79k
                        return false;
518
36.4k
                }
519
13.5k
                bs_skip( p_bs, 5 );
520
13.5k
                p_sps->vui.i_cpb_removal_delay_length_minus1 = bs_read( p_bs, 5 );
521
13.5k
                p_sps->vui.i_dpb_output_delay_length_minus1 = bs_read( p_bs, 5 );
522
13.5k
                bs_skip( p_bs, 5 );
523
13.5k
            }
524
79.5k
        }
525
526
35.9k
        if( p_sps->vui.b_hrd_parameters_present_flag )
527
10.0k
            bs_skip( p_bs, 1 ); /* low delay hrd */
528
529
        /* pic struct info */
530
35.9k
        p_sps->vui.b_pic_struct_present_flag = bs_read( p_bs, 1 );
531
532
35.9k
        p_sps->vui.b_bitstream_restriction_flag = bs_read( p_bs, 1 );
533
35.9k
        if( p_sps->vui.b_bitstream_restriction_flag )
534
7.83k
        {
535
7.83k
            bs_skip( p_bs, 1 ); /* motion vector pic boundaries */
536
7.83k
            bs_read_ue( p_bs ); /* max bytes per pic */
537
7.83k
            bs_read_ue( p_bs ); /* max bits per mb */
538
7.83k
            bs_read_ue( p_bs ); /* log2 max mv h */
539
7.83k
            bs_read_ue( p_bs ); /* log2 max mv v */
540
7.83k
            p_sps->vui.i_max_num_reorder_frames = bs_read_ue( p_bs );
541
7.83k
            p_sps->vui.i_max_dec_frame_buffering = bs_read_ue( p_bs );
542
7.83k
        }
543
35.9k
    }
544
545
70.7k
    return !bs_error( p_bs );
546
76.3k
}
547
548
void h264_release_pps( h264_picture_parameter_set_t *p_pps )
549
83.2k
{
550
83.2k
    free( p_pps );
551
83.2k
}
552
553
static bool h264_parse_picture_parameter_set_rbsp( bs_t *p_bs,
554
                                                   h264_picture_parameter_set_t *p_pps )
555
83.2k
{
556
83.2k
    uint32_t i_pps_id = bs_read_ue( p_bs ); // pps id
557
83.2k
    uint32_t i_sps_id = bs_read_ue( p_bs ); // sps id
558
83.2k
    if( i_pps_id > H264_PPS_ID_MAX || i_sps_id > H264_SPS_ID_MAX )
559
3.01k
        return false;
560
80.1k
    p_pps->i_id = i_pps_id;
561
80.1k
    p_pps->i_sps_id = i_sps_id;
562
563
80.1k
    bs_skip( p_bs, 1 ); // entropy coding mode flag
564
80.1k
    p_pps->i_pic_order_present_flag = bs_read( p_bs, 1 );
565
566
80.1k
    unsigned num_slice_groups_minus1 = bs_read_ue( p_bs );
567
80.1k
    if( num_slice_groups_minus1 > 7 ) /* never has value > 7. Annex A, G & J */
568
3.32k
        return false;
569
76.8k
    if( num_slice_groups_minus1 > 0 )
570
22.9k
    {
571
22.9k
        unsigned slice_group_map_type = bs_read_ue( p_bs );
572
22.9k
        if( slice_group_map_type == 0 )
573
6.34k
        {
574
37.0k
            for( unsigned i = 0; i <= num_slice_groups_minus1; i++ )
575
30.6k
                bs_read_ue( p_bs ); /* run_length_minus1[group] */
576
6.34k
        }
577
16.6k
        else if( slice_group_map_type == 2 )
578
2.73k
        {
579
12.3k
            for( unsigned i = 0; i < num_slice_groups_minus1; i++ )
580
9.59k
            {
581
9.59k
                bs_read_ue( p_bs ); /* top_left[group] */
582
9.59k
                bs_read_ue( p_bs ); /* bottom_right[group] */
583
9.59k
            }
584
2.73k
        }
585
13.8k
        else if( slice_group_map_type > 2 && slice_group_map_type < 6 )
586
2.87k
        {
587
2.87k
            bs_read1( p_bs );   /* slice_group_change_direction_flag */
588
2.87k
            bs_read_ue( p_bs ); /* slice_group_change_rate_minus1 */
589
2.87k
        }
590
11.0k
        else if( slice_group_map_type == 6 )
591
4.84k
        {
592
4.84k
            unsigned pic_size_in_maps_units = bs_read_ue( p_bs ) + 1;
593
            // Ceil( Log2( num_slice_groups_minus1 + 1 ) )
594
4.84k
            static const int ceil_log2_table[8] =
595
4.84k
            {
596
4.84k
                0,1,2,2,3,3,3,3
597
4.84k
            };
598
4.84k
            unsigned sliceGroupSize = ceil_log2_table[num_slice_groups_minus1];
599
            // slice_group_id[]
600
4.84k
            bs_skip( p_bs, sliceGroupSize * pic_size_in_maps_units );
601
4.84k
        }
602
22.9k
    }
603
604
76.8k
    p_pps->num_ref_idx_l01_default_active_minus1[0] = bs_read_ue( p_bs );
605
76.8k
    p_pps->num_ref_idx_l01_default_active_minus1[1] = bs_read_ue( p_bs );
606
76.8k
    if (p_pps->num_ref_idx_l01_default_active_minus1[0] > 31 ||
607
73.0k
        p_pps->num_ref_idx_l01_default_active_minus1[1] > 31)
608
5.49k
        return false;
609
71.3k
    p_pps->weighted_pred_flag = bs_read( p_bs, 1 );
610
71.3k
    p_pps->weighted_bipred_idc = bs_read( p_bs, 2 );
611
71.3k
    bs_read_se( p_bs ); /* pic_init_qp_minus26 */
612
71.3k
    bs_read_se( p_bs ); /* pic_init_qs_minus26 */
613
71.3k
    bs_read_se( p_bs ); /* chroma_qp_index_offset */
614
71.3k
    bs_skip( p_bs, 1 ); /* deblocking_filter_control_present_flag */
615
71.3k
    bs_skip( p_bs, 1 ); /* constrained_intra_pred_flag */
616
71.3k
    p_pps->i_redundant_pic_present_flag = bs_read( p_bs, 1 );
617
618
    /* TODO */
619
620
71.3k
    return true;
621
76.8k
}
622
623
static bool h264_parse_sequence_parameter_set_extension_rbsp( bs_t *p_bs,
624
                                 h264_sequence_parameter_set_extension_t *p_sps_ext )
625
0
{
626
0
    p_sps_ext->i_sps_id = bs_read_ue( p_bs );
627
0
    if( p_sps_ext->i_sps_id > H264_SPSEXT_ID_MAX )
628
0
        return false;
629
0
    return true;
630
0
}
631
632
void h264_release_sps_extension( h264_sequence_parameter_set_extension_t *p_sps_ext )
633
0
{
634
0
    free( p_sps_ext );
635
0
}
636
637
#define IMPL_h264_generic_decode( name, h264type, decode, release ) \
638
    h264type * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
639
165k
    { \
640
165k
        h264type *p_h264type = calloc(1, sizeof(h264type)); \
641
165k
        if(likely(p_h264type)) \
642
165k
        { \
643
165k
            bs_t bs; \
644
165k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
645
165k
            if( b_escaped ) \
646
165k
            { \
647
165k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
648
165k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
649
165k
            } \
650
165k
            else bs_init( &bs, p_buf, i_buf ); \
651
165k
            bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
652
165k
            if( !decode( &bs, p_h264type ) ) \
653
165k
            { \
654
71.2k
                release( p_h264type ); \
655
71.2k
                p_h264type = NULL; \
656
71.2k
            } \
657
165k
        } \
658
165k
        return p_h264type; \
659
165k
    }
660
661
82.0k
IMPL_h264_generic_decode( h264_decode_sps, h264_sequence_parameter_set_t,
662
                          h264_parse_sequence_parameter_set_rbsp, h264_release_sps )
663
664
83.2k
IMPL_h264_generic_decode( h264_decode_pps, h264_picture_parameter_set_t,
665
                          h264_parse_picture_parameter_set_rbsp, h264_release_pps )
666
667
0
IMPL_h264_generic_decode( h264_decode_sps_extension, h264_sequence_parameter_set_extension_t,
668
                          h264_parse_sequence_parameter_set_extension_rbsp, h264_release_sps_extension )
669
670
block_t *h264_NAL_to_avcC( uint8_t i_nal_length_size,
671
                           const uint8_t **pp_sps_buf,
672
                           const size_t *p_sps_size, uint8_t i_sps_count,
673
                           const uint8_t **pp_pps_buf,
674
                           const size_t *p_pps_size, uint8_t i_pps_count,
675
                           const uint8_t **pp_sps_ext_buf,
676
                           const size_t *p_sps_ext_size, uint8_t i_sps_ext_count )
677
0
{
678
    /* The length of the NAL size is encoded using 1, 2 or 4 bytes */
679
0
    if( i_nal_length_size != 1 && i_nal_length_size != 2
680
0
     && i_nal_length_size != 4 )
681
0
        return NULL;
682
0
    if( i_sps_count == 0 || i_sps_count > H264_SPS_ID_MAX || i_pps_count == 0 )
683
0
        return NULL;
684
685
    /* Calculate the total size of all SPS and PPS NALs */
686
0
    size_t i_spspps_size = 0;
687
0
    for( size_t i = 0; i < i_sps_count; ++i )
688
0
    {
689
0
        assert( pp_sps_buf[i] && p_sps_size[i] );
690
0
        if( p_sps_size[i] < 4 || p_sps_size[i] > UINT16_MAX )
691
0
            return NULL;
692
0
        i_spspps_size += p_sps_size[i] +  2 /* 16be size place holder */;
693
0
    }
694
0
    for( size_t i = 0; i < i_pps_count; ++i )
695
0
    {
696
0
        assert( pp_pps_buf[i] && p_pps_size[i] );
697
0
        if( p_pps_size[i] > UINT16_MAX)
698
0
            return NULL;
699
0
        i_spspps_size += p_pps_size[i] +  2 /* 16be size place holder */;
700
0
    }
701
702
0
    bo_t bo;
703
    /* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
704
0
    if( bo_init( &bo, 7 + i_spspps_size ) != true )
705
0
        return NULL;
706
707
0
    bo_add_8( &bo, 1 ); /* configuration version */
708
0
    bo_add_mem( &bo, 3, &pp_sps_buf[0][1] ); /* i_profile/profile_compatibility/level */
709
0
    bo_add_8( &bo, 0xfc | (i_nal_length_size - 1) ); /* 0b11111100 | lengthsize - 1*/
710
711
0
    bo_add_8( &bo, 0xe0 | i_sps_count ); /* 0b11100000 | sps_count */
712
0
    for( size_t i = 0; i < i_sps_count; ++i )
713
0
    {
714
0
        bo_add_16be( &bo, p_sps_size[i] );
715
0
        bo_add_mem( &bo, p_sps_size[i], pp_sps_buf[i] );
716
0
    }
717
718
0
    bo_add_8( &bo, i_pps_count ); /* pps_count */
719
0
    for( size_t i = 0; i < i_pps_count; ++i )
720
0
    {
721
0
        bo_add_16be( &bo, p_pps_size[i] );
722
0
        bo_add_mem( &bo, p_pps_size[i], pp_pps_buf[i] );
723
0
    }
724
725
0
    const uint8_t i_profile = pp_sps_buf[0][1];
726
0
    if( i_profile == PROFILE_H264_HIGH ||
727
0
        i_profile == PROFILE_H264_HIGH_10 ||
728
0
        i_profile == PROFILE_H264_HIGH_422 ||
729
0
        i_profile == PROFILE_H264_HIGH_444 )
730
0
    {
731
0
        h264_sequence_parameter_set_t *p_sps = h264_decode_sps( pp_sps_buf[0], p_sps_size[0], true );
732
0
        bo_add_8( &bo, 0xfc | (p_sps ? p_sps->i_chroma_idc : 0) );
733
0
        bo_add_8( &bo, 0xf8 | (p_sps ? (p_sps->i_bit_depth_luma - 8) : 0) );
734
0
        bo_add_8( &bo, 0xf8 | (p_sps ? (p_sps->i_bit_depth_chroma - 8) : 0) );
735
0
        if( p_sps )
736
0
            h264_release_sps( p_sps );
737
0
        bo_add_8( &bo, i_sps_ext_count );
738
0
        for( size_t i = 0; i < i_sps_ext_count; ++i )
739
0
        {
740
0
            bo_add_16be( &bo, p_sps_ext_size[i] );
741
0
            bo_add_mem( &bo, p_sps_ext_size[i], pp_sps_ext_buf[i] );
742
0
        }
743
0
    }
744
745
0
    return bo.b;
746
0
}
747
748
bool h264_get_xps_id( const uint8_t *p_buf, size_t i_buf, uint8_t *pi_id )
749
184k
{
750
184k
    if( i_buf < 2 )
751
3.65k
        return false;
752
753
    /* No need to lookup convert from emulation for that data */
754
180k
    uint8_t i_max, i_offset;
755
180k
    switch( h264_getNALType(p_buf) )
756
180k
    {
757
89.3k
        case H264_NAL_SPS:
758
89.3k
            i_offset = 1 + 3 /* profile constraint level */;
759
89.3k
            i_max = H264_SPS_ID_MAX;
760
89.3k
            break;
761
89.2k
        case H264_NAL_PPS:
762
89.2k
            i_offset = 1;
763
89.2k
            i_max = H264_PPS_ID_MAX;
764
89.2k
            break;
765
2.00k
        case H264_NAL_SPS_EXT:
766
2.00k
            i_offset = 1;
767
2.00k
            i_max = H264_SPSEXT_ID_MAX;
768
2.00k
            break;
769
0
        default:
770
0
            return false;
771
180k
    }
772
773
180k
    if( i_buf <= i_offset )
774
2.50k
        return false;
775
776
178k
    bs_t bs;
777
178k
    bs_init( &bs, &p_buf[i_offset], i_buf - i_offset );
778
178k
    *pi_id = bs_read_ue( &bs );
779
780
178k
    return !bs_error( &bs ) && *pi_id <= i_max;
781
180k
}
782
783
uint8_t h264_get_sps_id( const h264_sequence_parameter_set_t *p_sps )
784
0
{
785
0
    return p_sps->i_id;
786
0
}
787
788
uint8_t h264_get_pps_sps_id( const h264_picture_parameter_set_t *p_pps )
789
597k
{
790
597k
    return p_pps->i_sps_id;
791
597k
}
792
793
static const h264_level_limits_t * h264_get_level_limits( const h264_sequence_parameter_set_t *p_sps )
794
0
{
795
0
    uint16_t i_level_number = p_sps->i_level;
796
0
    if( i_level_number == H264_LEVEL_NUMBER_1_1 &&
797
0
       (p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3)) )
798
0
    {
799
0
        i_level_number = H264_LEVEL_NUMBER_1_B;
800
0
    }
801
802
0
    for( size_t i=0; i< ARRAY_SIZE(h264_levels_limits); i++ )
803
0
        if( h264_levels_limits[i].i_level == i_level_number )
804
0
            return & h264_levels_limits[i].limits;
805
806
0
    return NULL;
807
0
}
808
809
static uint8_t h264_get_max_dpb_frames( const h264_sequence_parameter_set_t *p_sps )
810
0
{
811
0
    const h264_level_limits_t *limits = h264_get_level_limits( p_sps );
812
0
    if( limits )
813
0
    {
814
0
        unsigned i_frame_height_in_mbs = ( p_sps->pic_height_in_map_units_minus1 + 1 ) *
815
0
                                         ( 2 - p_sps->frame_mbs_only_flag );
816
0
        unsigned i_den = ( p_sps->pic_width_in_mbs_minus1 + 1 ) * i_frame_height_in_mbs;
817
0
        uint8_t i_max_dpb_frames = limits->i_max_dpb_mbs / i_den;
818
0
        if( i_max_dpb_frames < 16 )
819
0
            return i_max_dpb_frames;
820
0
    }
821
0
    return 16;
822
0
}
823
824
bool h264_get_dpb_values( const h264_sequence_parameter_set_t *p_sps,
825
                          uint8_t *pi_max_num_reorder, uint8_t *pi_max_dec_buffering )
826
0
{
827
0
    uint8_t i_max_num_reorder_frames = p_sps->vui.i_max_num_reorder_frames;
828
0
    uint8_t i_max_dec_frame_buffering = p_sps->vui.i_max_dec_frame_buffering;
829
0
    if( !p_sps->vui.b_bitstream_restriction_flag )
830
0
    {
831
0
        switch( p_sps->i_profile ) /* E-2.1 */
832
0
        {
833
0
            case PROFILE_H264_BASELINE:
834
0
                i_max_num_reorder_frames = 0; /* only I & P */
835
0
                break;
836
0
            case PROFILE_H264_CAVLC_INTRA:
837
0
            case PROFILE_H264_SVC_HIGH:
838
0
            case PROFILE_H264_HIGH:
839
0
            case PROFILE_H264_HIGH_10:
840
0
            case PROFILE_H264_HIGH_422:
841
0
            case PROFILE_H264_HIGH_444_PREDICTIVE:
842
0
                if( p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3) )
843
0
                {
844
0
                    i_max_num_reorder_frames =
845
0
                    i_max_dec_frame_buffering = 0; /* all IDR */
846
0
                    break;
847
0
                }
848
                /* fallthrough */
849
0
            default:
850
0
                i_max_num_reorder_frames =
851
0
                i_max_dec_frame_buffering = h264_get_max_dpb_frames( p_sps );
852
0
                break;
853
0
        }
854
0
    }
855
856
0
    if( i_max_num_reorder_frames > i_max_dec_frame_buffering )
857
0
        i_max_num_reorder_frames = i_max_dec_frame_buffering;
858
859
0
    *pi_max_num_reorder = i_max_num_reorder_frames;
860
0
    *pi_max_dec_buffering = i_max_dec_frame_buffering;
861
862
0
    return true;
863
0
}
864
865
unsigned h264_get_max_frame_num( const h264_sequence_parameter_set_t *p_sps )
866
2.48k
{
867
2.48k
    return 1 << (p_sps->i_log2_max_frame_num + 4);
868
2.48k
}
869
870
bool h264_is_frames_only( const h264_sequence_parameter_set_t *p_sps )
871
235k
{
872
235k
    return p_sps->frame_mbs_only_flag;
873
235k
}
874
875
bool h264_using_adaptive_frames( const h264_sequence_parameter_set_t *p_sps )
876
0
{
877
0
    return p_sps->mb_adaptive_frame_field_flag;
878
0
}
879
880
881
bool h264_get_sps_profile_tier_level( const h264_sequence_parameter_set_t *p_sps,
882
                                      uint8_t *pi_profile, uint8_t *pi_level)
883
264k
{
884
264k
    *pi_profile = p_sps->i_profile;
885
264k
    *pi_level = p_sps->i_level;
886
264k
    return true;
887
264k
}
888
889
bool h264_get_picture_size( const h264_sequence_parameter_set_t *p_sps,
890
                            unsigned *p_ox, unsigned *p_oy,
891
                            unsigned *p_w, unsigned *p_h,
892
                            unsigned *p_vw, unsigned *p_vh )
893
264k
{
894
264k
    unsigned CropUnitX = 1;
895
264k
    unsigned CropUnitY = 2 - p_sps->frame_mbs_only_flag;
896
264k
    if( p_sps->b_separate_colour_planes_flag != 1 )
897
261k
    {
898
261k
        if( p_sps->i_chroma_idc > 0 )
899
255k
        {
900
255k
            unsigned SubWidthC = 2;
901
255k
            unsigned SubHeightC = 2;
902
255k
            if( p_sps->i_chroma_idc > 1 )
903
91.6k
            {
904
91.6k
                SubHeightC = 1;
905
91.6k
                if( p_sps->i_chroma_idc > 2 )
906
754
                    SubWidthC = 1;
907
91.6k
            }
908
255k
            CropUnitX *= SubWidthC;
909
255k
            CropUnitY *= SubHeightC;
910
255k
        }
911
261k
    }
912
913
264k
    *p_w = 16 * p_sps->pic_width_in_mbs_minus1 + 16;
914
264k
    *p_h = 16 * p_sps->pic_height_in_map_units_minus1 + 16;
915
264k
    *p_h *= ( 2 - p_sps->frame_mbs_only_flag );
916
917
264k
    *p_ox = p_sps->frame_crop.left_offset * CropUnitX;
918
264k
    *p_oy = p_sps->frame_crop.top_offset * CropUnitY;
919
264k
    *p_vw = *p_w - ( p_sps->frame_crop.left_offset + p_sps->frame_crop.right_offset ) * CropUnitX;
920
264k
    *p_vh = *p_h - ( p_sps->frame_crop.bottom_offset + p_sps->frame_crop.top_offset ) * CropUnitY;
921
922
264k
    return true;
923
264k
}
924
925
bool h264_get_frame_rate( const h264_sequence_parameter_set_t *p_sps,
926
                         unsigned *pi_num, unsigned *pi_den )
927
4.26k
{
928
4.26k
    if(!p_sps->vui_parameters_present_flag || !p_sps->vui.i_num_units_in_tick ||
929
574
        p_sps->vui.i_time_scale <= 1)
930
3.69k
        return false;
931
564
    *pi_num = p_sps->vui.i_time_scale;
932
564
    *pi_den = p_sps->vui.i_num_units_in_tick;
933
564
    return true;
934
4.26k
}
935
936
bool h264_get_aspect_ratio( const h264_sequence_parameter_set_t *p_sps,
937
                           unsigned *pi_num, unsigned *pi_den )
938
264k
{
939
264k
    if(!p_sps->vui.i_sar_num || !p_sps->vui.i_sar_den)
940
229k
        return false;
941
35.0k
    *pi_num = p_sps->vui.i_sar_num;
942
35.0k
    *pi_den = p_sps->vui.i_sar_den;
943
35.0k
    return true;
944
264k
}
945
946
bool h264_get_chroma_luma( const h264_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format,
947
                           uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma )
948
0
{
949
0
    *pi_chroma_format = p_sps->i_chroma_idc;
950
0
    *pi_depth_luma = p_sps->i_bit_depth_luma;
951
0
    *pi_depth_chroma = p_sps->i_bit_depth_chroma;
952
0
    return true;
953
0
}
954
bool h264_get_colorimetry( const h264_sequence_parameter_set_t *p_sps,
955
                           video_color_primaries_t *p_primaries,
956
                           video_transfer_func_t *p_transfer,
957
                           video_color_space_t *p_colorspace,
958
                           video_color_range_t *p_full_range )
959
264k
{
960
264k
    if( !p_sps->vui_parameters_present_flag )
961
172k
        return false;
962
92.2k
    *p_primaries =
963
92.2k
        iso_23001_8_cp_to_vlc_primaries( p_sps->vui.colour.i_colour_primaries );
964
92.2k
    *p_transfer =
965
92.2k
        iso_23001_8_tc_to_vlc_xfer( p_sps->vui.colour.i_transfer_characteristics );
966
92.2k
    *p_colorspace =
967
92.2k
        iso_23001_8_mc_to_vlc_coeffs( p_sps->vui.colour.i_matrix_coefficients );
968
92.2k
    *p_full_range = p_sps->vui.colour.b_full_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
969
92.2k
    return true;
970
264k
}
971
972
973
bool h264_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
974
                            uint8_t *pi_level, uint8_t *pi_nal_length_size)
975
0
{
976
0
    uint8_t *p = (uint8_t*)p_fmt->p_extra;
977
0
    if(p_fmt->i_extra < 8)
978
0
        return false;
979
980
    /* Check the profile / level */
981
0
    if (p[0] == 1 && p_fmt->i_extra >= 12)
982
0
    {
983
0
        if (pi_nal_length_size)
984
0
            *pi_nal_length_size = 1 + (p[4]&0x03);
985
0
        p += 8;
986
0
    }
987
0
    else if(!p[0] && !p[1]) /* FIXME: WTH is setting AnnexB data here ? */
988
0
    {
989
0
        if (!p[2] && p[3] == 1)
990
0
            p += 4;
991
0
        else if (p[2] == 1)
992
0
            p += 3;
993
0
        else
994
0
            return false;
995
0
    }
996
0
    else return false;
997
998
0
    if ( ((*p++)&0x1f) != 7) return false;
999
1000
0
    if (pi_profile)
1001
0
        *pi_profile = p[0];
1002
1003
0
    if (pi_level)
1004
0
        *pi_level = p[2];
1005
1006
0
    return true;
1007
0
}
1008
1009
bool h264_decode_sei_recovery_point( bs_t *p_bs, h264_sei_recovery_point_t *p_reco )
1010
5.23k
{
1011
5.23k
    p_reco->i_frames = bs_read_ue( p_bs );
1012
    //bool b_exact_match = bs_read( p_bs, 1 );
1013
    //bool b_broken_link = bs_read( p_bs, 1 );
1014
    //int i_changing_slice_group = bs_read( p_bs, 2 );
1015
5.23k
    return true;
1016
5.23k
}
1017
1018
bool h264_decode_sei_pic_timing(  bs_t *p_bs,
1019
                                  const h264_sequence_parameter_set_t *p_sps,
1020
                                  uint8_t *pic_struct, uint8_t *output_delay  )
1021
80.5k
{
1022
80.5k
    if( !p_sps->vui_parameters_present_flag )
1023
70.0k
        return false;
1024
1025
10.4k
    if( p_sps->vui.b_hrd_parameters_present_flag )
1026
6.27k
    {
1027
6.27k
        bs_skip( p_bs, p_sps->vui.i_cpb_removal_delay_length_minus1 + 1 );
1028
6.27k
        *output_delay =
1029
6.27k
            bs_read( p_bs, p_sps->vui.i_dpb_output_delay_length_minus1 + 1 );
1030
6.27k
    }
1031
1032
10.4k
    if( p_sps->vui.b_pic_struct_present_flag )
1033
5.68k
        *pic_struct = bs_read( p_bs, 4 );
1034
1035
    /* + unparsed remains */
1036
    return true;
1037
80.5k
}