Coverage Report

Created: 2026-05-16 07:42

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
0
{
100
0
    return ( i_buf >= H264_MIN_AVCC_SIZE &&
101
0
             p_buf[0] != 0x00 &&
102
0
             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
0
            );
108
0
}
109
110
static bool get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf, size_t *pi_res )
111
0
{
112
0
    size_t i_total = 0;
113
114
0
    if( i_buf < H264_MIN_AVCC_SIZE )
115
0
        return false;
116
117
0
    p_buf += 5;
118
0
    i_buf -= 5;
119
120
0
    for ( unsigned int j = 0; j < 2; j++ )
121
0
    {
122
        /* First time is SPS, Second is PPS */
123
0
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
124
0
        p_buf++; i_buf--;
125
126
0
        for ( unsigned int i = 0; i < i_loop_end; i++ )
127
0
        {
128
0
            if( i_buf < 2 )
129
0
                return false;
130
131
0
            uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
132
0
            if(i_nal_size > i_buf - 2)
133
0
                return false;
134
0
            i_total += i_nal_size + 4;
135
0
            p_buf += i_nal_size + 2;
136
0
            i_buf -= i_nal_size + 2;
137
0
        }
138
139
0
        if( j == 0 && i_buf < 1 )
140
0
            return false;
141
0
    }
142
0
    *pi_res = i_total;
143
0
    return true;
144
0
}
145
146
bool h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
147
                              uint8_t **pp_result, size_t *pi_result,
148
                              uint8_t *pi_nal_length_size )
149
0
{
150
0
    size_t i_result;
151
0
    if( !get_avcC_to_AnnexB_NAL_size( p_buf, i_buf, &i_result ) ) /* Does check min size */
152
0
        return false;
153
154
    /* Read infos in first 6 bytes */
155
0
    const uint8_t i_nal_length_size = (p_buf[4] & 0x03) + 1;
156
157
0
    uint8_t *p_ret = NULL;
158
0
    uint8_t *p_out_buf = NULL;
159
0
    if( i_result > 0 )
160
0
    {
161
0
        p_ret = p_out_buf = malloc( i_result );
162
0
        if( !p_out_buf )
163
0
            return false;
164
165
0
        p_buf += 5;
166
167
0
        for ( unsigned int j = 0; j < 2; j++ )
168
0
        {
169
0
            const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
170
0
            p_buf++;
171
172
0
            for ( unsigned int i = 0; i < i_loop_end; i++)
173
0
            {
174
0
                uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
175
0
                p_buf += 2;
176
177
0
                memcpy( p_out_buf, annexb_startcode4, 4 );
178
0
                p_out_buf += 4;
179
180
0
                memcpy( p_out_buf, p_buf, i_nal_size );
181
0
                p_out_buf += i_nal_size;
182
0
                p_buf += i_nal_size;
183
0
            }
184
0
        }
185
0
    }
186
187
0
    *pp_result = p_ret;
188
0
    *pi_result = i_result;
189
190
0
    if ( pi_nal_length_size )
191
0
        *pi_nal_length_size = i_nal_length_size;
192
193
0
    return true;
194
0
}
195
196
void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
197
                             uint8_t i_nal_length_size )
198
0
{
199
0
    uint32_t nal_len = 0;
200
0
    uint8_t nal_pos = 0;
201
202
0
    if( i_nal_length_size != 4 )
203
0
        return;
204
205
    /* This only works for a NAL length size of 4 */
206
    /* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
207
0
    while( i_len > 0 )
208
0
    {
209
0
        if( nal_pos < i_nal_length_size ) {
210
0
            unsigned int i;
211
0
            for( i = 0; nal_pos < i_nal_length_size && i < i_len; i++, nal_pos++ ) {
212
0
                nal_len = (nal_len << 8) | p_buf[i];
213
0
                p_buf[i] = 0;
214
0
            }
215
0
            if( nal_pos < i_nal_length_size )
216
0
                return;
217
0
            p_buf[i - 1] = 1;
218
0
            p_buf += i;
219
0
            i_len -= i;
220
0
        }
221
0
        if( nal_len > INT_MAX )
222
0
            return;
223
0
        if( nal_len > i_len )
224
0
        {
225
0
            nal_len -= i_len;
226
0
            return;
227
0
        }
228
0
        else
229
0
        {
230
0
            p_buf += nal_len;
231
0
            i_len -= nal_len;
232
0
            nal_len = 0;
233
0
            nal_pos = 0;
234
0
        }
235
0
    }
236
0
}
237
238
bool h264_AnnexB_get_spspps( const uint8_t *p_buf, size_t i_buf,
239
                             const uint8_t **pp_sps, size_t *p_sps_size,
240
                             const uint8_t **pp_pps, size_t *p_pps_size,
241
                             const uint8_t **pp_ext, size_t *p_ext_size )
242
0
{
243
0
    if( pp_sps ) { *p_sps_size = 0; *pp_sps = NULL; }
244
0
    if( pp_pps ) { *p_pps_size = 0; *pp_pps = NULL; }
245
0
    if( pp_ext ) { *p_ext_size = 0; *pp_ext = NULL; }
246
247
0
    hxxx_iterator_ctx_t it;
248
0
    hxxx_iterator_init( &it, p_buf, i_buf, 0 );
249
250
0
    const uint8_t *p_nal; size_t i_nal;
251
0
    while( hxxx_annexb_iterate_next( &it, &p_nal, &i_nal ) )
252
0
    {
253
0
        if( i_nal < 2 )
254
0
            continue;
255
256
0
        const enum h264_nal_unit_type_e i_nal_type = p_nal[0] & 0x1F;
257
258
0
        if ( i_nal_type <= H264_NAL_SLICE_IDR && i_nal_type != H264_NAL_UNKNOWN )
259
0
            break;
260
261
0
#define IFSET_NAL(type, var) \
262
0
    if( i_nal_type == type && pp_##var && *pp_##var == NULL )\
263
0
        { *pp_##var = p_nal; *p_##var##_size = i_nal; }
264
265
0
        IFSET_NAL(H264_NAL_SPS, sps)
266
0
        else
267
0
        IFSET_NAL(H264_NAL_PPS, pps)
268
0
        else
269
0
        IFSET_NAL(H264_NAL_SPS_EXT, ext);
270
0
#undef IFSET_NAL
271
0
    }
272
273
0
    return (pp_sps && *p_sps_size) || (pp_pps && *p_pps_size);
274
0
}
275
276
void h264_release_sps( h264_sequence_parameter_set_t *p_sps )
277
158k
{
278
158k
    free( p_sps );
279
158k
}
280
281
0
#define H264_CONSTRAINT_SET_FLAG(N) (0x80 >> N)
282
283
static bool h264_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
284
                                                    h264_sequence_parameter_set_t *p_sps )
285
158k
{
286
158k
    int i_tmp;
287
288
158k
    int i_profile_idc = bs_read( p_bs, 8 );
289
158k
    p_sps->i_profile = i_profile_idc;
290
158k
    p_sps->i_constraint_set_flags = bs_read( p_bs, 8 );
291
158k
    p_sps->i_level = bs_read( p_bs, 8 );
292
    /* sps id */
293
158k
    uint32_t i_sps_id = bs_read_ue( p_bs );
294
158k
    if( i_sps_id > H264_SPS_ID_MAX )
295
1.10k
        return false;
296
157k
    p_sps->i_id = i_sps_id;
297
298
157k
    if( i_profile_idc == PROFILE_H264_HIGH ||
299
141k
        i_profile_idc == PROFILE_H264_HIGH_10 ||
300
131k
        i_profile_idc == PROFILE_H264_HIGH_422 ||
301
129k
        i_profile_idc == PROFILE_H264_HIGH_444 || /* Old one, no longer on spec */
302
126k
        i_profile_idc == PROFILE_H264_HIGH_444_PREDICTIVE ||
303
126k
        i_profile_idc == PROFILE_H264_CAVLC_INTRA ||
304
121k
        i_profile_idc == PROFILE_H264_SVC_BASELINE ||
305
120k
        i_profile_idc == PROFILE_H264_SVC_HIGH ||
306
119k
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_HIGH ||
307
117k
        i_profile_idc == PROFILE_H264_MVC_STEREO_HIGH ||
308
111k
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH ||
309
111k
        i_profile_idc == PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH ||
310
110k
        i_profile_idc == PROFILE_H264_MFC_HIGH )
311
48.3k
    {
312
        /* chroma_format_idc */
313
48.3k
        p_sps->i_chroma_idc = bs_read_ue( p_bs );
314
48.3k
        if( p_sps->i_chroma_idc > 3 )
315
4.09k
            return false;
316
44.2k
        if( p_sps->i_chroma_idc == 3 )
317
7.07k
            p_sps->b_separate_colour_planes_flag = bs_read1( p_bs );
318
37.1k
        else
319
37.1k
            p_sps->b_separate_colour_planes_flag = 0;
320
        /* bit_depth_luma_minus8 */
321
44.2k
        p_sps->i_bit_depth_luma = bs_read_ue( p_bs ) + 8;
322
44.2k
        if( p_sps->i_bit_depth_luma > 6 + 8 )
323
1.99k
            return false;
324
        /* bit_depth_chroma_minus8 */
325
42.2k
        p_sps->i_bit_depth_chroma = bs_read_ue( p_bs ) + 8;
326
42.2k
        if( p_sps->i_bit_depth_chroma > 6 + 8 )
327
3.36k
            return false;
328
        /* qpprime_y_zero_transform_bypass_flag */
329
38.9k
        bs_skip( p_bs, 1 );
330
        /* seq_scaling_matrix_present_flag */
331
38.9k
        i_tmp = bs_read( p_bs, 1 );
332
38.9k
        if( i_tmp )
333
20.5k
        {
334
167k
            for( int i = 0; i < ((3 != p_sps->i_chroma_idc) ? 8 : 12); i++ )
335
153k
            {
336
                /* seq_scaling_list_present_flag[i] */
337
153k
                i_tmp = bs_read( p_bs, 1 );
338
153k
                if( !i_tmp )
339
84.7k
                    continue;
340
68.8k
                const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
341
                /* scaling_list (...) */
342
68.8k
                int i_lastscale = 8;
343
68.8k
                int i_nextscale = 8;
344
1.50M
                for( int j = 0; j < i_size_of_scaling_list; j++ )
345
1.44M
                {
346
1.44M
                    if( i_nextscale != 0 )
347
1.36M
                    {
348
                        /* delta_scale */
349
1.36M
                        i_tmp = bs_read_se( p_bs );
350
1.36M
                        if(i_tmp < -128 || i_tmp > 127)
351
6.53k
                          return false;
352
1.36M
                        i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
353
                        /* useDefaultScalingMatrixFlag = ... */
354
1.36M
                    }
355
                    /* scalinglist[j] */
356
1.43M
                    i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
357
1.43M
                }
358
68.8k
            }
359
20.5k
        }
360
38.9k
    }
361
109k
    else
362
109k
    {
363
109k
        p_sps->i_chroma_idc = 1; /* Not present == inferred to 4:2:0 */
364
109k
        p_sps->i_bit_depth_luma = 8;
365
109k
        p_sps->i_bit_depth_chroma = 8;
366
109k
    }
367
368
    /* Skip i_log2_max_frame_num */
369
141k
    p_sps->i_log2_max_frame_num = bs_read_ue( p_bs );
370
141k
    if( (uint_fast32_t) p_sps->i_log2_max_frame_num > 12)
371
6.33k
        p_sps->i_log2_max_frame_num = 12;
372
373
    /* Read poc_type */
374
141k
    p_sps->i_pic_order_cnt_type = bs_read_ue( p_bs );
375
141k
    if( p_sps->i_pic_order_cnt_type > 2 )
376
11.5k
        return false;
377
130k
    if( p_sps->i_pic_order_cnt_type == 0 )
378
96.9k
    {
379
        /* skip i_log2_max_poc_lsb */
380
96.9k
        p_sps->i_log2_max_pic_order_cnt_lsb = bs_read_ue( p_bs );
381
96.9k
        if( p_sps->i_log2_max_pic_order_cnt_lsb > 12 )
382
2.50k
            p_sps->i_log2_max_pic_order_cnt_lsb = 12;
383
96.9k
    }
384
33.2k
    else if( p_sps->i_pic_order_cnt_type == 1 )
385
24.6k
    {
386
24.6k
        p_sps->i_delta_pic_order_always_zero_flag = bs_read( p_bs, 1 );
387
24.6k
        p_sps->offset_for_non_ref_pic = bs_read_se( p_bs );
388
24.6k
        p_sps->offset_for_top_to_bottom_field = bs_read_se( p_bs );
389
24.6k
        p_sps->i_num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue( p_bs );
390
24.6k
        if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 255 )
391
1.07k
            return false;
392
375k
        for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ )
393
352k
            p_sps->offset_for_ref_frame[i] = bs_read_se( p_bs );
394
23.6k
    }
395
    /* max_num_ref_frames */
396
129k
    if( bs_read_ue( p_bs ) > 16 )
397
3.47k
        return false;
398
    /* b_gaps_in_frame_num_value_allowed */
399
125k
    bs_skip( p_bs, 1 );
400
401
    /* Read size */
402
125k
    p_sps->pic_width_in_mbs_minus1 = bs_read_ue( p_bs );
403
125k
    p_sps->pic_height_in_map_units_minus1 = bs_read_ue( p_bs );
404
405
    /* b_frame_mbs_only */
406
125k
    p_sps->frame_mbs_only_flag = bs_read( p_bs, 1 );
407
125k
    if( !p_sps->frame_mbs_only_flag )
408
52.8k
        p_sps->mb_adaptive_frame_field_flag = bs_read( p_bs, 1 );
409
410
    /* b_direct8x8_inference */
411
125k
    bs_skip( p_bs, 1 );
412
413
    /* crop */
414
125k
    if( bs_read1( p_bs ) ) /* frame_cropping_flag */
415
68.5k
    {
416
68.5k
        p_sps->frame_crop.left_offset = bs_read_ue( p_bs );
417
68.5k
        p_sps->frame_crop.right_offset = bs_read_ue( p_bs );
418
68.5k
        p_sps->frame_crop.top_offset = bs_read_ue( p_bs );
419
68.5k
        p_sps->frame_crop.bottom_offset = bs_read_ue( p_bs );
420
68.5k
    }
421
422
    /* vui */
423
125k
    p_sps->vui_parameters_present_flag = bs_read( p_bs, 1 );
424
125k
    if( p_sps->vui_parameters_present_flag )
425
74.7k
    {
426
        /* read the aspect ratio part if any */
427
74.7k
        i_tmp = bs_read( p_bs, 1 );
428
74.7k
        if( i_tmp )
429
52.6k
        {
430
52.6k
            static const struct { int w, h; } sar[17] =
431
52.6k
            {
432
52.6k
                { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
433
52.6k
                { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
434
52.6k
                { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
435
52.6k
                { 64, 33 }, { 160,99 }, {  4,  3 }, {  3,  2 },
436
52.6k
                {  2,  1 },
437
52.6k
            };
438
52.6k
            int i_sar = bs_read( p_bs, 8 );
439
52.6k
            int w, h;
440
441
52.6k
            if( i_sar < 17 )
442
3.21k
            {
443
3.21k
                w = sar[i_sar].w;
444
3.21k
                h = sar[i_sar].h;
445
3.21k
            }
446
49.4k
            else if( i_sar == 255 )
447
31.5k
            {
448
31.5k
                w = bs_read( p_bs, 16 );
449
31.5k
                h = bs_read( p_bs, 16 );
450
31.5k
            }
451
17.8k
            else
452
17.8k
            {
453
17.8k
                w = 0;
454
17.8k
                h = 0;
455
17.8k
            }
456
457
52.6k
            if( w != 0 && h != 0 )
458
30.9k
            {
459
30.9k
                p_sps->vui.i_sar_num = w;
460
30.9k
                p_sps->vui.i_sar_den = h;
461
30.9k
            }
462
21.7k
            else
463
21.7k
            {
464
21.7k
                p_sps->vui.i_sar_num = 1;
465
21.7k
                p_sps->vui.i_sar_den = 1;
466
21.7k
            }
467
52.6k
        }
468
469
        /* overscan */
470
74.7k
        i_tmp = bs_read( p_bs, 1 );
471
74.7k
        if ( i_tmp )
472
31.3k
            bs_skip( p_bs, 1 );
473
474
        /* video signal type */
475
74.7k
        i_tmp = bs_read( p_bs, 1 );
476
74.7k
        if( i_tmp )
477
37.4k
        {
478
37.4k
            bs_skip( p_bs, 3 );
479
37.4k
            p_sps->vui.colour.b_full_range = bs_read( p_bs, 1 );
480
            /* colour desc */
481
37.4k
            i_tmp = bs_read( p_bs, 1 );
482
37.4k
            if ( i_tmp )
483
22.0k
            {
484
22.0k
                p_sps->vui.colour.i_colour_primaries = bs_read( p_bs, 8 );
485
22.0k
                p_sps->vui.colour.i_transfer_characteristics = bs_read( p_bs, 8 );
486
22.0k
                p_sps->vui.colour.i_matrix_coefficients = bs_read( p_bs, 8 );
487
22.0k
            }
488
15.3k
            else
489
15.3k
            {
490
15.3k
                p_sps->vui.colour.i_colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
491
15.3k
                p_sps->vui.colour.i_transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
492
15.3k
                p_sps->vui.colour.i_matrix_coefficients = ISO_23001_8_MC_UNSPECIFIED;
493
15.3k
            }
494
37.4k
        }
495
496
        /* chroma loc info */
497
74.7k
        i_tmp = bs_read( p_bs, 1 );
498
74.7k
        if( i_tmp )
499
27.5k
        {
500
27.5k
            bs_read_ue( p_bs );
501
27.5k
            bs_read_ue( p_bs );
502
27.5k
        }
503
504
        /* timing info */
505
74.7k
        p_sps->vui.b_timing_info_present_flag = bs_read( p_bs, 1 );
506
74.7k
        if( p_sps->vui.b_timing_info_present_flag )
507
27.0k
        {
508
27.0k
            p_sps->vui.i_num_units_in_tick = bs_read( p_bs, 32 );
509
27.0k
            p_sps->vui.i_time_scale = bs_read( p_bs, 32 );
510
27.0k
            p_sps->vui.b_fixed_frame_rate = bs_read( p_bs, 1 );
511
27.0k
        }
512
513
        /* Nal hrd & VC1 hrd parameters */
514
74.7k
        p_sps->vui.b_hrd_parameters_present_flag = false;
515
203k
        for ( int i=0; i<2; i++ )
516
143k
        {
517
143k
            i_tmp = bs_read( p_bs, 1 );
518
143k
            if( i_tmp )
519
67.4k
            {
520
67.4k
                p_sps->vui.b_hrd_parameters_present_flag = true;
521
67.4k
                uint32_t count = bs_read_ue( p_bs ) + 1;
522
67.4k
                if( count > 31 )
523
4.61k
                    return false;
524
62.8k
                bs_skip( p_bs, 4 );
525
62.8k
                bs_skip( p_bs, 4 );
526
175k
                for( uint32_t j = 0; j < count; j++ )
527
123k
                {
528
123k
                    bs_read_ue( p_bs );
529
123k
                    bs_read_ue( p_bs );
530
123k
                    bs_skip( p_bs, 1 );
531
123k
                    if( bs_error( p_bs ) )
532
10.6k
                        return false;
533
123k
                }
534
52.2k
                bs_skip( p_bs, 5 );
535
52.2k
                p_sps->vui.i_cpb_removal_delay_length_minus1 = bs_read( p_bs, 5 );
536
52.2k
                p_sps->vui.i_dpb_output_delay_length_minus1 = bs_read( p_bs, 5 );
537
52.2k
                bs_skip( p_bs, 5 );
538
52.2k
            }
539
143k
        }
540
541
59.5k
        if( p_sps->vui.b_hrd_parameters_present_flag )
542
29.0k
            bs_skip( p_bs, 1 ); /* low delay hrd */
543
544
        /* pic struct info */
545
59.5k
        p_sps->vui.b_pic_struct_present_flag = bs_read( p_bs, 1 );
546
547
59.5k
        p_sps->vui.b_bitstream_restriction_flag = bs_read( p_bs, 1 );
548
59.5k
        if( p_sps->vui.b_bitstream_restriction_flag )
549
20.9k
        {
550
20.9k
            bs_skip( p_bs, 1 ); /* motion vector pic boundaries */
551
20.9k
            bs_read_ue( p_bs ); /* max bytes per pic */
552
20.9k
            bs_read_ue( p_bs ); /* max bits per mb */
553
20.9k
            bs_read_ue( p_bs ); /* log2 max mv h */
554
20.9k
            bs_read_ue( p_bs ); /* log2 max mv v */
555
20.9k
            p_sps->vui.i_max_num_reorder_frames = bs_read_ue( p_bs );
556
20.9k
            p_sps->vui.i_max_dec_frame_buffering = bs_read_ue( p_bs );
557
20.9k
        }
558
59.5k
    }
559
560
110k
    return !bs_error( p_bs );
561
125k
}
562
563
void h264_release_pps( h264_picture_parameter_set_t *p_pps )
564
186k
{
565
186k
    free( p_pps );
566
186k
}
567
568
static bool h264_parse_picture_parameter_set_rbsp( bs_t *p_bs,
569
                                                   h264_picture_parameter_set_t *p_pps )
570
186k
{
571
186k
    uint32_t i_pps_id = bs_read_ue( p_bs ); // pps id
572
186k
    uint32_t i_sps_id = bs_read_ue( p_bs ); // sps id
573
186k
    if( i_pps_id > H264_PPS_ID_MAX || i_sps_id > H264_SPS_ID_MAX )
574
7.36k
        return false;
575
178k
    p_pps->i_id = i_pps_id;
576
178k
    p_pps->i_sps_id = i_sps_id;
577
578
178k
    bs_skip( p_bs, 1 ); // entropy coding mode flag
579
178k
    p_pps->i_pic_order_present_flag = bs_read( p_bs, 1 );
580
581
178k
    unsigned num_slice_groups_minus1 = bs_read_ue( p_bs );
582
178k
    if( num_slice_groups_minus1 > 7 ) /* never has value > 7. Annex A, G & J */
583
12.6k
        return false;
584
166k
    if( num_slice_groups_minus1 > 0 )
585
74.8k
    {
586
74.8k
        unsigned slice_group_map_type = bs_read_ue( p_bs );
587
74.8k
        if( slice_group_map_type == 0 )
588
18.7k
        {
589
90.4k
            for( unsigned i = 0; i <= num_slice_groups_minus1; i++ )
590
71.6k
                bs_read_ue( p_bs ); /* run_length_minus1[group] */
591
18.7k
        }
592
56.1k
        else if( slice_group_map_type == 2 )
593
7.15k
        {
594
27.3k
            for( unsigned i = 0; i < num_slice_groups_minus1; i++ )
595
20.2k
            {
596
20.2k
                bs_read_ue( p_bs ); /* top_left[group] */
597
20.2k
                bs_read_ue( p_bs ); /* bottom_right[group] */
598
20.2k
            }
599
7.15k
        }
600
49.0k
        else if( slice_group_map_type > 2 && slice_group_map_type < 6 )
601
13.2k
        {
602
13.2k
            bs_read1( p_bs );   /* slice_group_change_direction_flag */
603
13.2k
            bs_read_ue( p_bs ); /* slice_group_change_rate_minus1 */
604
13.2k
        }
605
35.7k
        else if( slice_group_map_type == 6 )
606
3.80k
        {
607
3.80k
            unsigned pic_size_in_maps_units = bs_read_ue( p_bs ) + 1;
608
            // Ceil( Log2( num_slice_groups_minus1 + 1 ) )
609
3.80k
            static const int ceil_log2_table[8] =
610
3.80k
            {
611
3.80k
                0,1,2,2,3,3,3,3
612
3.80k
            };
613
3.80k
            unsigned sliceGroupSize = ceil_log2_table[num_slice_groups_minus1];
614
            // slice_group_id[]
615
3.80k
            bs_skip( p_bs, sliceGroupSize * pic_size_in_maps_units );
616
3.80k
        }
617
74.8k
    }
618
619
166k
    p_pps->num_ref_idx_l01_default_active_minus1[0] = bs_read_ue( p_bs );
620
166k
    p_pps->num_ref_idx_l01_default_active_minus1[1] = bs_read_ue( p_bs );
621
166k
    if (p_pps->num_ref_idx_l01_default_active_minus1[0] > 31 ||
622
161k
        p_pps->num_ref_idx_l01_default_active_minus1[1] > 31)
623
9.50k
        return false;
624
156k
    p_pps->weighted_pred_flag = bs_read( p_bs, 1 );
625
156k
    p_pps->weighted_bipred_idc = bs_read( p_bs, 2 );
626
156k
    bs_read_se( p_bs ); /* pic_init_qp_minus26 */
627
156k
    bs_read_se( p_bs ); /* pic_init_qs_minus26 */
628
156k
    bs_read_se( p_bs ); /* chroma_qp_index_offset */
629
156k
    bs_skip( p_bs, 1 ); /* deblocking_filter_control_present_flag */
630
156k
    bs_skip( p_bs, 1 ); /* constrained_intra_pred_flag */
631
156k
    p_pps->i_redundant_pic_present_flag = bs_read( p_bs, 1 );
632
633
    /* TODO */
634
635
156k
    return true;
636
166k
}
637
638
static bool h264_parse_sequence_parameter_set_extension_rbsp( bs_t *p_bs,
639
                                 h264_sequence_parameter_set_extension_t *p_sps_ext )
640
0
{
641
0
    p_sps_ext->i_sps_id = bs_read_ue( p_bs );
642
0
    if( p_sps_ext->i_sps_id > H264_SPSEXT_ID_MAX )
643
0
        return false;
644
0
    return true;
645
0
}
646
647
void h264_release_sps_extension( h264_sequence_parameter_set_extension_t *p_sps_ext )
648
0
{
649
0
    free( p_sps_ext );
650
0
}
651
652
#define IMPL_h264_generic_decode( name, h264type, decode, release ) \
653
    h264type * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
654
345k
    { \
655
345k
        h264type *p_h264type = calloc(1, sizeof(h264type)); \
656
345k
        if(likely(p_h264type)) \
657
345k
        { \
658
345k
            bs_t bs; \
659
345k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
660
345k
            if( b_escaped ) \
661
345k
            { \
662
345k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
663
345k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
664
345k
            } \
665
345k
            else bs_init( &bs, p_buf, i_buf ); \
666
345k
            bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
667
345k
            if( !decode( &bs, p_h264type ) ) \
668
345k
            { \
669
123k
                release( p_h264type ); \
670
123k
                p_h264type = NULL; \
671
123k
            } \
672
345k
        } \
673
345k
        return p_h264type; \
674
345k
    }
675
676
158k
IMPL_h264_generic_decode( h264_decode_sps, h264_sequence_parameter_set_t,
677
                          h264_parse_sequence_parameter_set_rbsp, h264_release_sps )
678
679
186k
IMPL_h264_generic_decode( h264_decode_pps, h264_picture_parameter_set_t,
680
                          h264_parse_picture_parameter_set_rbsp, h264_release_pps )
681
682
0
IMPL_h264_generic_decode( h264_decode_sps_extension, h264_sequence_parameter_set_extension_t,
683
                          h264_parse_sequence_parameter_set_extension_rbsp, h264_release_sps_extension )
684
685
block_t *h264_NAL_to_avcC( uint8_t i_nal_length_size,
686
                           const uint8_t **pp_sps_buf,
687
                           const size_t *p_sps_size, uint8_t i_sps_count,
688
                           const uint8_t **pp_pps_buf,
689
                           const size_t *p_pps_size, uint8_t i_pps_count,
690
                           const uint8_t **pp_sps_ext_buf,
691
                           const size_t *p_sps_ext_size, uint8_t i_sps_ext_count )
692
0
{
693
    /* The length of the NAL size is encoded using 1, 2 or 4 bytes */
694
0
    if( i_nal_length_size != 1 && i_nal_length_size != 2
695
0
     && i_nal_length_size != 4 )
696
0
        return NULL;
697
0
    if( i_sps_count == 0 || i_sps_count > H264_SPS_ID_MAX || i_pps_count == 0 )
698
0
        return NULL;
699
700
    /* Calculate the total size of all SPS and PPS NALs */
701
0
    size_t i_spspps_size = 0;
702
0
    for( size_t i = 0; i < i_sps_count; ++i )
703
0
    {
704
0
        assert( pp_sps_buf[i] && p_sps_size[i] );
705
0
        if( p_sps_size[i] < 4 || p_sps_size[i] > UINT16_MAX )
706
0
            return NULL;
707
0
        i_spspps_size += p_sps_size[i] +  2 /* 16be size place holder */;
708
0
    }
709
0
    for( size_t i = 0; i < i_pps_count; ++i )
710
0
    {
711
0
        assert( pp_pps_buf[i] && p_pps_size[i] );
712
0
        if( p_pps_size[i] > UINT16_MAX)
713
0
            return NULL;
714
0
        i_spspps_size += p_pps_size[i] +  2 /* 16be size place holder */;
715
0
    }
716
717
0
    bo_t bo;
718
    /* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
719
0
    if( bo_init( &bo, 7 + i_spspps_size ) != true )
720
0
        return NULL;
721
722
0
    bo_add_8( &bo, 1 ); /* configuration version */
723
0
    bo_add_mem( &bo, 3, &pp_sps_buf[0][1] ); /* i_profile/profile_compatibility/level */
724
0
    bo_add_8( &bo, 0xfc | (i_nal_length_size - 1) ); /* 0b11111100 | lengthsize - 1*/
725
726
0
    bo_add_8( &bo, 0xe0 | i_sps_count ); /* 0b11100000 | sps_count */
727
0
    for( size_t i = 0; i < i_sps_count; ++i )
728
0
    {
729
0
        bo_add_16be( &bo, p_sps_size[i] );
730
0
        bo_add_mem( &bo, p_sps_size[i], pp_sps_buf[i] );
731
0
    }
732
733
0
    bo_add_8( &bo, i_pps_count ); /* pps_count */
734
0
    for( size_t i = 0; i < i_pps_count; ++i )
735
0
    {
736
0
        bo_add_16be( &bo, p_pps_size[i] );
737
0
        bo_add_mem( &bo, p_pps_size[i], pp_pps_buf[i] );
738
0
    }
739
740
0
    const uint8_t i_profile = pp_sps_buf[0][1];
741
0
    if( i_profile == PROFILE_H264_HIGH ||
742
0
        i_profile == PROFILE_H264_HIGH_10 ||
743
0
        i_profile == PROFILE_H264_HIGH_422 ||
744
0
        i_profile == PROFILE_H264_HIGH_444 )
745
0
    {
746
0
        h264_sequence_parameter_set_t *p_sps = h264_decode_sps( pp_sps_buf[0], p_sps_size[0], true );
747
0
        bo_add_8( &bo, 0xfc | (p_sps ? p_sps->i_chroma_idc : 0) );
748
0
        bo_add_8( &bo, 0xf8 | (p_sps ? (p_sps->i_bit_depth_luma - 8) : 0) );
749
0
        bo_add_8( &bo, 0xf8 | (p_sps ? (p_sps->i_bit_depth_chroma - 8) : 0) );
750
0
        if( p_sps )
751
0
            h264_release_sps( p_sps );
752
0
        bo_add_8( &bo, i_sps_ext_count );
753
0
        for( size_t i = 0; i < i_sps_ext_count; ++i )
754
0
        {
755
0
            bo_add_16be( &bo, p_sps_ext_size[i] );
756
0
            bo_add_mem( &bo, p_sps_ext_size[i], pp_sps_ext_buf[i] );
757
0
        }
758
0
    }
759
760
0
    return bo.b;
761
0
}
762
763
bool h264_get_xps_id( const uint8_t *p_buf, size_t i_buf, uint8_t *pi_id )
764
631k
{
765
631k
    if( i_buf < 2 )
766
18.2k
        return false;
767
768
    /* No need to lookup convert from emulation for that data */
769
613k
    uint8_t i_max, i_offset;
770
613k
    switch( h264_getNALType(p_buf) )
771
613k
    {
772
275k
        case H264_NAL_SPS:
773
275k
            i_offset = 1 + 3 /* profile constraint level */;
774
275k
            i_max = H264_SPS_ID_MAX;
775
275k
            break;
776
315k
        case H264_NAL_PPS:
777
315k
            i_offset = 1;
778
315k
            i_max = H264_PPS_ID_MAX;
779
315k
            break;
780
21.8k
        case H264_NAL_SPS_EXT:
781
21.8k
            i_offset = 1;
782
21.8k
            i_max = H264_SPSEXT_ID_MAX;
783
21.8k
            break;
784
0
        default:
785
0
            return false;
786
613k
    }
787
788
613k
    if( i_buf <= i_offset )
789
8.33k
        return false;
790
791
604k
    bs_t bs;
792
604k
    bs_init( &bs, &p_buf[i_offset], i_buf - i_offset );
793
604k
    *pi_id = bs_read_ue( &bs );
794
795
604k
    return !bs_error( &bs ) && *pi_id <= i_max;
796
613k
}
797
798
uint8_t h264_get_sps_id( const h264_sequence_parameter_set_t *p_sps )
799
0
{
800
0
    return p_sps->i_id;
801
0
}
802
803
uint8_t h264_get_pps_sps_id( const h264_picture_parameter_set_t *p_pps )
804
1.21M
{
805
1.21M
    return p_pps->i_sps_id;
806
1.21M
}
807
808
static const h264_level_limits_t * h264_get_level_limits( const h264_sequence_parameter_set_t *p_sps )
809
0
{
810
0
    uint16_t i_level_number = p_sps->i_level;
811
0
    if( i_level_number == H264_LEVEL_NUMBER_1_1 &&
812
0
       (p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3)) )
813
0
    {
814
0
        i_level_number = H264_LEVEL_NUMBER_1_B;
815
0
    }
816
817
0
    for( size_t i=0; i< ARRAY_SIZE(h264_levels_limits); i++ )
818
0
        if( h264_levels_limits[i].i_level == i_level_number )
819
0
            return & h264_levels_limits[i].limits;
820
821
0
    return NULL;
822
0
}
823
824
static uint8_t h264_get_max_dpb_frames( const h264_sequence_parameter_set_t *p_sps )
825
0
{
826
0
    const h264_level_limits_t *limits = h264_get_level_limits( p_sps );
827
0
    if( limits )
828
0
    {
829
0
        unsigned i_frame_height_in_mbs = ( p_sps->pic_height_in_map_units_minus1 + 1 ) *
830
0
                                         ( 2 - p_sps->frame_mbs_only_flag );
831
0
        unsigned i_den = ( p_sps->pic_width_in_mbs_minus1 + 1 ) * i_frame_height_in_mbs;
832
0
        uint8_t i_max_dpb_frames = limits->i_max_dpb_mbs / i_den;
833
0
        if( i_max_dpb_frames < 16 )
834
0
            return i_max_dpb_frames;
835
0
    }
836
0
    return 16;
837
0
}
838
839
bool h264_get_dpb_values( const h264_sequence_parameter_set_t *p_sps,
840
                          uint8_t *pi_max_num_reorder, uint8_t *pi_max_dec_buffering )
841
0
{
842
0
    uint8_t i_max_num_reorder_frames = p_sps->vui.i_max_num_reorder_frames;
843
0
    uint8_t i_max_dec_frame_buffering = p_sps->vui.i_max_dec_frame_buffering;
844
0
    if( !p_sps->vui.b_bitstream_restriction_flag )
845
0
    {
846
0
        switch( p_sps->i_profile ) /* E-2.1 */
847
0
        {
848
0
            case PROFILE_H264_BASELINE:
849
0
                i_max_num_reorder_frames = 0; /* only I & P */
850
0
                break;
851
0
            case PROFILE_H264_CAVLC_INTRA:
852
0
            case PROFILE_H264_SVC_HIGH:
853
0
            case PROFILE_H264_HIGH:
854
0
            case PROFILE_H264_HIGH_10:
855
0
            case PROFILE_H264_HIGH_422:
856
0
            case PROFILE_H264_HIGH_444_PREDICTIVE:
857
0
                if( p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3) )
858
0
                {
859
0
                    i_max_num_reorder_frames =
860
0
                    i_max_dec_frame_buffering = 0; /* all IDR */
861
0
                    break;
862
0
                }
863
                /* fallthrough */
864
0
            default:
865
0
                i_max_num_reorder_frames =
866
0
                i_max_dec_frame_buffering = h264_get_max_dpb_frames( p_sps );
867
0
                break;
868
0
        }
869
0
    }
870
871
0
    if( i_max_num_reorder_frames > i_max_dec_frame_buffering )
872
0
        i_max_num_reorder_frames = i_max_dec_frame_buffering;
873
874
0
    *pi_max_num_reorder = i_max_num_reorder_frames;
875
0
    *pi_max_dec_buffering = i_max_dec_frame_buffering;
876
877
0
    return true;
878
0
}
879
880
unsigned h264_get_max_frame_num( const h264_sequence_parameter_set_t *p_sps )
881
12.3k
{
882
12.3k
    return 1 << (p_sps->i_log2_max_frame_num + 4);
883
12.3k
}
884
885
bool h264_is_frames_only( const h264_sequence_parameter_set_t *p_sps )
886
434k
{
887
434k
    return p_sps->frame_mbs_only_flag;
888
434k
}
889
890
bool h264_using_adaptive_frames( const h264_sequence_parameter_set_t *p_sps )
891
0
{
892
0
    return p_sps->mb_adaptive_frame_field_flag;
893
0
}
894
895
896
bool h264_get_sps_profile_tier_level( const h264_sequence_parameter_set_t *p_sps,
897
                                      uint8_t *pi_profile, uint8_t *pi_level)
898
523k
{
899
523k
    *pi_profile = p_sps->i_profile;
900
523k
    *pi_level = p_sps->i_level;
901
523k
    return true;
902
523k
}
903
904
bool h264_get_picture_size( const h264_sequence_parameter_set_t *p_sps,
905
                            unsigned *p_ox, unsigned *p_oy,
906
                            unsigned *p_w, unsigned *p_h,
907
                            unsigned *p_vw, unsigned *p_vh )
908
523k
{
909
523k
    unsigned CropUnitX = 1;
910
523k
    unsigned CropUnitY = 2 - p_sps->frame_mbs_only_flag;
911
523k
    if( p_sps->b_separate_colour_planes_flag != 1 )
912
516k
    {
913
516k
        if( p_sps->i_chroma_idc > 0 )
914
500k
        {
915
500k
            unsigned SubWidthC = 2;
916
500k
            unsigned SubHeightC = 2;
917
500k
            if( p_sps->i_chroma_idc > 1 )
918
106k
            {
919
106k
                SubHeightC = 1;
920
106k
                if( p_sps->i_chroma_idc > 2 )
921
2.10k
                    SubWidthC = 1;
922
106k
            }
923
500k
            CropUnitX *= SubWidthC;
924
500k
            CropUnitY *= SubHeightC;
925
500k
        }
926
516k
    }
927
928
523k
    *p_w = 16 * p_sps->pic_width_in_mbs_minus1 + 16;
929
523k
    *p_h = 16 * p_sps->pic_height_in_map_units_minus1 + 16;
930
523k
    *p_h *= ( 2 - p_sps->frame_mbs_only_flag );
931
932
523k
    *p_ox = p_sps->frame_crop.left_offset * CropUnitX;
933
523k
    *p_oy = p_sps->frame_crop.top_offset * CropUnitY;
934
523k
    *p_vw = *p_w - ( p_sps->frame_crop.left_offset + p_sps->frame_crop.right_offset ) * CropUnitX;
935
523k
    *p_vh = *p_h - ( p_sps->frame_crop.bottom_offset + p_sps->frame_crop.top_offset ) * CropUnitY;
936
937
523k
    return true;
938
523k
}
939
940
bool h264_get_frame_rate( const h264_sequence_parameter_set_t *p_sps,
941
                         unsigned *pi_num, unsigned *pi_den )
942
8.16k
{
943
8.16k
    if(!p_sps->vui_parameters_present_flag || !p_sps->vui.i_num_units_in_tick ||
944
1.80k
        p_sps->vui.i_time_scale <= 1)
945
6.48k
        return false;
946
1.68k
    *pi_num = p_sps->vui.i_time_scale;
947
1.68k
    *pi_den = p_sps->vui.i_num_units_in_tick;
948
1.68k
    return true;
949
8.16k
}
950
951
bool h264_get_aspect_ratio( const h264_sequence_parameter_set_t *p_sps,
952
                           unsigned *pi_num, unsigned *pi_den )
953
523k
{
954
523k
    if(!p_sps->vui.i_sar_num || !p_sps->vui.i_sar_den)
955
349k
        return false;
956
173k
    *pi_num = p_sps->vui.i_sar_num;
957
173k
    *pi_den = p_sps->vui.i_sar_den;
958
173k
    return true;
959
523k
}
960
961
bool h264_get_chroma_luma( const h264_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format,
962
                           uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma )
963
0
{
964
0
    *pi_chroma_format = p_sps->i_chroma_idc;
965
0
    *pi_depth_luma = p_sps->i_bit_depth_luma;
966
0
    *pi_depth_chroma = p_sps->i_bit_depth_chroma;
967
0
    return true;
968
0
}
969
bool h264_get_colorimetry( const h264_sequence_parameter_set_t *p_sps,
970
                           video_color_primaries_t *p_primaries,
971
                           video_transfer_func_t *p_transfer,
972
                           video_color_space_t *p_colorspace,
973
                           video_color_range_t *p_full_range )
974
520k
{
975
520k
    if( !p_sps->vui_parameters_present_flag )
976
266k
        return false;
977
253k
    *p_primaries =
978
253k
        iso_23001_8_cp_to_vlc_primaries( p_sps->vui.colour.i_colour_primaries );
979
253k
    *p_transfer =
980
253k
        iso_23001_8_tc_to_vlc_xfer( p_sps->vui.colour.i_transfer_characteristics );
981
253k
    *p_colorspace =
982
253k
        iso_23001_8_mc_to_vlc_coeffs( p_sps->vui.colour.i_matrix_coefficients );
983
253k
    *p_full_range = p_sps->vui.colour.b_full_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
984
253k
    return true;
985
520k
}
986
987
988
bool h264_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
989
                            uint8_t *pi_level, uint8_t *pi_nal_length_size)
990
0
{
991
0
    uint8_t *p = (uint8_t*)p_fmt->p_extra;
992
0
    if(p_fmt->i_extra < 8)
993
0
        return false;
994
995
    /* Check the profile / level */
996
0
    if (p[0] == 1 && p_fmt->i_extra >= 12)
997
0
    {
998
0
        if (pi_nal_length_size)
999
0
            *pi_nal_length_size = 1 + (p[4]&0x03);
1000
0
        p += 8;
1001
0
    }
1002
0
    else if(!p[0] && !p[1]) /* FIXME: WTH is setting AnnexB data here ? */
1003
0
    {
1004
0
        if (!p[2] && p[3] == 1)
1005
0
            p += 4;
1006
0
        else if (p[2] == 1)
1007
0
            p += 3;
1008
0
        else
1009
0
            return false;
1010
0
    }
1011
0
    else return false;
1012
1013
0
    if ( ((*p++)&0x1f) != 7) return false;
1014
1015
0
    if (pi_profile)
1016
0
        *pi_profile = p[0];
1017
1018
0
    if (pi_level)
1019
0
        *pi_level = p[2];
1020
1021
0
    return true;
1022
0
}
1023
1024
bool h264_decode_sei_recovery_point( bs_t *p_bs, h264_sei_recovery_point_t *p_reco )
1025
35.5k
{
1026
35.5k
    p_reco->i_frames = bs_read_ue( p_bs );
1027
    //bool b_exact_match = bs_read( p_bs, 1 );
1028
    //bool b_broken_link = bs_read( p_bs, 1 );
1029
    //int i_changing_slice_group = bs_read( p_bs, 2 );
1030
35.5k
    return true;
1031
35.5k
}
1032
1033
bool h264_decode_sei_pic_timing(  bs_t *p_bs,
1034
                                  const h264_sequence_parameter_set_t *p_sps,
1035
                                  uint8_t *pic_struct, uint8_t *output_delay  )
1036
56.6k
{
1037
56.6k
    if( !p_sps->vui_parameters_present_flag )
1038
30.6k
        return false;
1039
1040
26.0k
    if( p_sps->vui.b_hrd_parameters_present_flag )
1041
18.7k
    {
1042
18.7k
        bs_skip( p_bs, p_sps->vui.i_cpb_removal_delay_length_minus1 + 1 );
1043
18.7k
        *output_delay =
1044
18.7k
            bs_read( p_bs, p_sps->vui.i_dpb_output_delay_length_minus1 + 1 );
1045
18.7k
    }
1046
1047
26.0k
    if( p_sps->vui.b_pic_struct_present_flag )
1048
17.6k
        *pic_struct = bs_read( p_bs, 4 );
1049
1050
    /* + unparsed remains */
1051
    return true;
1052
56.6k
}