Coverage Report

Created: 2025-10-12 06:51

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
1.97k
{
100
1.97k
    return ( i_buf >= H264_MIN_AVCC_SIZE &&
101
1.97k
             p_buf[0] != 0x00 &&
102
1.91k
             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
1.97k
            );
108
1.97k
}
109
110
static size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
111
1.90k
{
112
1.90k
    size_t i_total = 0;
113
114
1.90k
    if( i_buf < H264_MIN_AVCC_SIZE )
115
0
        return 0;
116
117
1.90k
    p_buf += 5;
118
1.90k
    i_buf -= 5;
119
120
5.49k
    for ( unsigned int j = 0; j < 2; j++ )
121
3.70k
    {
122
        /* First time is SPS, Second is PPS */
123
3.70k
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
124
3.70k
        p_buf++; i_buf--;
125
126
13.9k
        for ( unsigned int i = 0; i < i_loop_end; i++ )
127
10.3k
        {
128
10.3k
            if( i_buf < 2 )
129
0
                return 0;
130
131
10.3k
            uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
132
10.3k
            if(i_nal_size > i_buf - 2)
133
117
                return 0;
134
10.2k
            i_total += i_nal_size + 4;
135
10.2k
            p_buf += i_nal_size + 2;
136
10.2k
            i_buf -= i_nal_size + 2;
137
10.2k
        }
138
139
3.58k
        if( j == 0 && i_buf < 1 )
140
1
            return 0;
141
3.58k
    }
142
1.78k
    return i_total;
143
1.90k
}
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
1.90k
{
148
1.90k
    *pi_result = get_avcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does check min size */
149
1.90k
    if( *pi_result == 0 )
150
139
        return NULL;
151
152
    /* Read infos in first 6 bytes */
153
1.76k
    if ( pi_nal_length_size )
154
1.76k
        *pi_nal_length_size = (p_buf[4] & 0x03) + 1;
155
156
1.76k
    uint8_t *p_ret;
157
1.76k
    uint8_t *p_out_buf = p_ret = malloc( *pi_result );
158
1.76k
    if( !p_out_buf )
159
0
    {
160
0
        *pi_result = 0;
161
0
        return NULL;
162
0
    }
163
164
1.76k
    p_buf += 5;
165
166
5.28k
    for ( unsigned int j = 0; j < 2; j++ )
167
3.52k
    {
168
3.52k
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
169
3.52k
        p_buf++;
170
171
13.5k
        for ( unsigned int i = 0; i < i_loop_end; i++)
172
10.0k
        {
173
10.0k
            uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
174
10.0k
            p_buf += 2;
175
176
10.0k
            memcpy( p_out_buf, annexb_startcode4, 4 );
177
10.0k
            p_out_buf += 4;
178
179
10.0k
            memcpy( p_out_buf, p_buf, i_nal_size );
180
10.0k
            p_out_buf += i_nal_size;
181
10.0k
            p_buf += i_nal_size;
182
10.0k
        }
183
3.52k
    }
184
185
1.76k
    return p_ret;
186
1.76k
}
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
97.4k
{
270
97.4k
    free( p_sps );
271
97.4k
}
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
97.4k
{
278
97.4k
    int i_tmp;
279
280
97.4k
    int i_profile_idc = bs_read( p_bs, 8 );
281
97.4k
    p_sps->i_profile = i_profile_idc;
282
97.4k
    p_sps->i_constraint_set_flags = bs_read( p_bs, 8 );
283
97.4k
    p_sps->i_level = bs_read( p_bs, 8 );
284
    /* sps id */
285
97.4k
    uint32_t i_sps_id = bs_read_ue( p_bs );
286
97.4k
    if( i_sps_id > H264_SPS_ID_MAX )
287
641
        return false;
288
96.7k
    p_sps->i_id = i_sps_id;
289
290
96.7k
    if( i_profile_idc == PROFILE_H264_HIGH ||
291
74.0k
        i_profile_idc == PROFILE_H264_HIGH_10 ||
292
73.7k
        i_profile_idc == PROFILE_H264_HIGH_422 ||
293
72.2k
        i_profile_idc == PROFILE_H264_HIGH_444 || /* Old one, no longer on spec */
294
71.6k
        i_profile_idc == PROFILE_H264_HIGH_444_PREDICTIVE ||
295
71.2k
        i_profile_idc == PROFILE_H264_CAVLC_INTRA ||
296
68.4k
        i_profile_idc == PROFILE_H264_SVC_BASELINE ||
297
68.1k
        i_profile_idc == PROFILE_H264_SVC_HIGH ||
298
67.8k
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_HIGH ||
299
66.7k
        i_profile_idc == PROFILE_H264_MVC_STEREO_HIGH ||
300
66.4k
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH ||
301
66.1k
        i_profile_idc == PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH ||
302
65.8k
        i_profile_idc == PROFILE_H264_MFC_HIGH )
303
31.1k
    {
304
        /* chroma_format_idc */
305
31.1k
        p_sps->i_chroma_idc = bs_read_ue( p_bs );
306
31.1k
        if( p_sps->i_chroma_idc > 3 )
307
5.47k
            return false;
308
25.7k
        if( p_sps->i_chroma_idc == 3 )
309
3.30k
            p_sps->b_separate_colour_planes_flag = bs_read1( p_bs );
310
22.4k
        else
311
22.4k
            p_sps->b_separate_colour_planes_flag = 0;
312
        /* bit_depth_luma_minus8 */
313
25.7k
        p_sps->i_bit_depth_luma = bs_read_ue( p_bs ) + 8;
314
        /* bit_depth_chroma_minus8 */
315
25.7k
        p_sps->i_bit_depth_chroma = bs_read_ue( p_bs ) + 8;
316
        /* qpprime_y_zero_transform_bypass_flag */
317
25.7k
        bs_skip( p_bs, 1 );
318
        /* seq_scaling_matrix_present_flag */
319
25.7k
        i_tmp = bs_read( p_bs, 1 );
320
25.7k
        if( i_tmp )
321
5.22k
        {
322
39.3k
            for( int i = 0; i < ((3 != p_sps->i_chroma_idc) ? 8 : 12); i++ )
323
35.8k
            {
324
                /* seq_scaling_list_present_flag[i] */
325
35.8k
                i_tmp = bs_read( p_bs, 1 );
326
35.8k
                if( !i_tmp )
327
28.4k
                    continue;
328
7.44k
                const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
329
                /* scaling_list (...) */
330
7.44k
                int i_lastscale = 8;
331
7.44k
                int i_nextscale = 8;
332
128k
                for( int j = 0; j < i_size_of_scaling_list; j++ )
333
122k
                {
334
122k
                    if( i_nextscale != 0 )
335
115k
                    {
336
                        /* delta_scale */
337
115k
                        i_tmp = bs_read_se( p_bs );
338
115k
                        if(i_tmp < -128 || i_tmp > 127)
339
1.72k
                          return false;
340
113k
                        i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
341
                        /* useDefaultScalingMatrixFlag = ... */
342
113k
                    }
343
                    /* scalinglist[j] */
344
120k
                    i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
345
120k
                }
346
7.44k
            }
347
5.22k
        }
348
25.7k
    }
349
65.5k
    else
350
65.5k
    {
351
65.5k
        p_sps->i_chroma_idc = 1; /* Not present == inferred to 4:2:0 */
352
65.5k
        p_sps->i_bit_depth_luma = 8;
353
65.5k
        p_sps->i_bit_depth_chroma = 8;
354
65.5k
    }
355
356
    /* Skip i_log2_max_frame_num */
357
89.5k
    p_sps->i_log2_max_frame_num = bs_read_ue( p_bs );
358
89.5k
    if( (uint_fast32_t) p_sps->i_log2_max_frame_num > 12)
359
7.02k
        p_sps->i_log2_max_frame_num = 12;
360
361
    /* Read poc_type */
362
89.5k
    p_sps->i_pic_order_cnt_type = bs_read_ue( p_bs );
363
89.5k
    if( p_sps->i_pic_order_cnt_type == 0 )
364
47.7k
    {
365
        /* skip i_log2_max_poc_lsb */
366
47.7k
        p_sps->i_log2_max_pic_order_cnt_lsb = bs_read_ue( p_bs );
367
47.7k
        if( p_sps->i_log2_max_pic_order_cnt_lsb > 12 )
368
2.14k
            p_sps->i_log2_max_pic_order_cnt_lsb = 12;
369
47.7k
    }
370
41.8k
    else if( p_sps->i_pic_order_cnt_type == 1 )
371
14.5k
    {
372
14.5k
        p_sps->i_delta_pic_order_always_zero_flag = bs_read( p_bs, 1 );
373
14.5k
        p_sps->offset_for_non_ref_pic = bs_read_se( p_bs );
374
14.5k
        p_sps->offset_for_top_to_bottom_field = bs_read_se( p_bs );
375
14.5k
        p_sps->i_num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue( p_bs );
376
14.5k
        if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 255 )
377
446
            return false;
378
92.0k
        for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ )
379
77.8k
            p_sps->offset_for_ref_frame[i] = bs_read_se( p_bs );
380
14.1k
    }
381
    /* i_num_ref_frames */
382
89.1k
    bs_read_ue( p_bs );
383
    /* b_gaps_in_frame_num_value_allowed */
384
89.1k
    bs_skip( p_bs, 1 );
385
386
    /* Read size */
387
89.1k
    p_sps->pic_width_in_mbs_minus1 = bs_read_ue( p_bs );
388
89.1k
    p_sps->pic_height_in_map_units_minus1 = bs_read_ue( p_bs );
389
390
    /* b_frame_mbs_only */
391
89.1k
    p_sps->frame_mbs_only_flag = bs_read( p_bs, 1 );
392
89.1k
    if( !p_sps->frame_mbs_only_flag )
393
50.5k
        p_sps->mb_adaptive_frame_field_flag = bs_read( p_bs, 1 );
394
395
    /* b_direct8x8_inference */
396
89.1k
    bs_skip( p_bs, 1 );
397
398
    /* crop */
399
89.1k
    if( bs_read1( p_bs ) ) /* frame_cropping_flag */
400
36.4k
    {
401
36.4k
        p_sps->frame_crop.left_offset = bs_read_ue( p_bs );
402
36.4k
        p_sps->frame_crop.right_offset = bs_read_ue( p_bs );
403
36.4k
        p_sps->frame_crop.top_offset = bs_read_ue( p_bs );
404
36.4k
        p_sps->frame_crop.bottom_offset = bs_read_ue( p_bs );
405
36.4k
    }
406
407
    /* vui */
408
89.1k
    p_sps->vui_parameters_present_flag = bs_read( p_bs, 1 );
409
89.1k
    if( p_sps->vui_parameters_present_flag )
410
45.2k
    {
411
        /* read the aspect ratio part if any */
412
45.2k
        i_tmp = bs_read( p_bs, 1 );
413
45.2k
        if( i_tmp )
414
19.8k
        {
415
19.8k
            static const struct { int w, h; } sar[17] =
416
19.8k
            {
417
19.8k
                { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
418
19.8k
                { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
419
19.8k
                { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
420
19.8k
                { 64, 33 }, { 160,99 }, {  4,  3 }, {  3,  2 },
421
19.8k
                {  2,  1 },
422
19.8k
            };
423
19.8k
            int i_sar = bs_read( p_bs, 8 );
424
19.8k
            int w, h;
425
426
19.8k
            if( i_sar < 17 )
427
1.07k
            {
428
1.07k
                w = sar[i_sar].w;
429
1.07k
                h = sar[i_sar].h;
430
1.07k
            }
431
18.7k
            else if( i_sar == 255 )
432
4.60k
            {
433
4.60k
                w = bs_read( p_bs, 16 );
434
4.60k
                h = bs_read( p_bs, 16 );
435
4.60k
            }
436
14.1k
            else
437
14.1k
            {
438
14.1k
                w = 0;
439
14.1k
                h = 0;
440
14.1k
            }
441
442
19.8k
            if( w != 0 && h != 0 )
443
4.19k
            {
444
4.19k
                p_sps->vui.i_sar_num = w;
445
4.19k
                p_sps->vui.i_sar_den = h;
446
4.19k
            }
447
15.6k
            else
448
15.6k
            {
449
15.6k
                p_sps->vui.i_sar_num = 1;
450
15.6k
                p_sps->vui.i_sar_den = 1;
451
15.6k
            }
452
19.8k
        }
453
454
        /* overscan */
455
45.2k
        i_tmp = bs_read( p_bs, 1 );
456
45.2k
        if ( i_tmp )
457
20.0k
            bs_skip( p_bs, 1 );
458
459
        /* video signal type */
460
45.2k
        i_tmp = bs_read( p_bs, 1 );
461
45.2k
        if( i_tmp )
462
18.2k
        {
463
18.2k
            bs_skip( p_bs, 3 );
464
18.2k
            p_sps->vui.colour.b_full_range = bs_read( p_bs, 1 );
465
            /* colour desc */
466
18.2k
            i_tmp = bs_read( p_bs, 1 );
467
18.2k
            if ( i_tmp )
468
7.47k
            {
469
7.47k
                p_sps->vui.colour.i_colour_primaries = bs_read( p_bs, 8 );
470
7.47k
                p_sps->vui.colour.i_transfer_characteristics = bs_read( p_bs, 8 );
471
7.47k
                p_sps->vui.colour.i_matrix_coefficients = bs_read( p_bs, 8 );
472
7.47k
            }
473
10.7k
            else
474
10.7k
            {
475
10.7k
                p_sps->vui.colour.i_colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
476
10.7k
                p_sps->vui.colour.i_transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
477
10.7k
                p_sps->vui.colour.i_matrix_coefficients = ISO_23001_8_MC_UNSPECIFIED;
478
10.7k
            }
479
18.2k
        }
480
481
        /* chroma loc info */
482
45.2k
        i_tmp = bs_read( p_bs, 1 );
483
45.2k
        if( i_tmp )
484
14.8k
        {
485
14.8k
            bs_read_ue( p_bs );
486
14.8k
            bs_read_ue( p_bs );
487
14.8k
        }
488
489
        /* timing info */
490
45.2k
        p_sps->vui.b_timing_info_present_flag = bs_read( p_bs, 1 );
491
45.2k
        if( p_sps->vui.b_timing_info_present_flag )
492
11.4k
        {
493
11.4k
            p_sps->vui.i_num_units_in_tick = bs_read( p_bs, 32 );
494
11.4k
            p_sps->vui.i_time_scale = bs_read( p_bs, 32 );
495
11.4k
            p_sps->vui.b_fixed_frame_rate = bs_read( p_bs, 1 );
496
11.4k
        }
497
498
        /* Nal hrd & VC1 hrd parameters */
499
45.2k
        p_sps->vui.b_hrd_parameters_present_flag = false;
500
123k
        for ( int i=0; i<2; i++ )
501
85.9k
        {
502
85.9k
            i_tmp = bs_read( p_bs, 1 );
503
85.9k
            if( i_tmp )
504
24.6k
            {
505
24.6k
                p_sps->vui.b_hrd_parameters_present_flag = true;
506
24.6k
                uint32_t count = bs_read_ue( p_bs ) + 1;
507
24.6k
                if( count > 31 )
508
831
                    return false;
509
23.8k
                bs_skip( p_bs, 4 );
510
23.8k
                bs_skip( p_bs, 4 );
511
60.4k
                for( uint32_t j = 0; j < count; j++ )
512
43.1k
                {
513
43.1k
                    bs_read_ue( p_bs );
514
43.1k
                    bs_read_ue( p_bs );
515
43.1k
                    bs_skip( p_bs, 1 );
516
43.1k
                    if( bs_error( p_bs ) )
517
6.54k
                        return false;
518
43.1k
                }
519
17.3k
                bs_skip( p_bs, 5 );
520
17.3k
                p_sps->vui.i_cpb_removal_delay_length_minus1 = bs_read( p_bs, 5 );
521
17.3k
                p_sps->vui.i_dpb_output_delay_length_minus1 = bs_read( p_bs, 5 );
522
17.3k
                bs_skip( p_bs, 5 );
523
17.3k
            }
524
85.9k
        }
525
526
37.8k
        if( p_sps->vui.b_hrd_parameters_present_flag )
527
12.6k
            bs_skip( p_bs, 1 ); /* low delay hrd */
528
529
        /* pic struct info */
530
37.8k
        p_sps->vui.b_pic_struct_present_flag = bs_read( p_bs, 1 );
531
532
37.8k
        p_sps->vui.b_bitstream_restriction_flag = bs_read( p_bs, 1 );
533
37.8k
        if( p_sps->vui.b_bitstream_restriction_flag )
534
10.5k
        {
535
10.5k
            bs_skip( p_bs, 1 ); /* motion vector pic boundaries */
536
10.5k
            bs_read_ue( p_bs ); /* max bytes per pic */
537
10.5k
            bs_read_ue( p_bs ); /* max bits per mb */
538
10.5k
            bs_read_ue( p_bs ); /* log2 max mv h */
539
10.5k
            bs_read_ue( p_bs ); /* log2 max mv v */
540
10.5k
            p_sps->vui.i_max_num_reorder_frames = bs_read_ue( p_bs );
541
10.5k
            p_sps->vui.i_max_dec_frame_buffering = bs_read_ue( p_bs );
542
10.5k
        }
543
37.8k
    }
544
545
81.7k
    return !bs_error( p_bs );
546
89.1k
}
547
548
void h264_release_pps( h264_picture_parameter_set_t *p_pps )
549
114k
{
550
114k
    free( p_pps );
551
114k
}
552
553
static bool h264_parse_picture_parameter_set_rbsp( bs_t *p_bs,
554
                                                   h264_picture_parameter_set_t *p_pps )
555
114k
{
556
114k
    uint32_t i_pps_id = bs_read_ue( p_bs ); // pps id
557
114k
    uint32_t i_sps_id = bs_read_ue( p_bs ); // sps id
558
114k
    if( i_pps_id > H264_PPS_ID_MAX || i_sps_id > H264_SPS_ID_MAX )
559
3.79k
        return false;
560
110k
    p_pps->i_id = i_pps_id;
561
110k
    p_pps->i_sps_id = i_sps_id;
562
563
110k
    bs_skip( p_bs, 1 ); // entropy coding mode flag
564
110k
    p_pps->i_pic_order_present_flag = bs_read( p_bs, 1 );
565
566
110k
    unsigned num_slice_groups_minus1 = bs_read_ue( p_bs );
567
110k
    if( num_slice_groups_minus1 > 7 ) /* never has value > 7. Annex A, G & J */
568
6.73k
        return false;
569
103k
    if( num_slice_groups_minus1 > 0 )
570
31.9k
    {
571
31.9k
        unsigned slice_group_map_type = bs_read_ue( p_bs );
572
31.9k
        if( slice_group_map_type == 0 )
573
6.72k
        {
574
39.6k
            for( unsigned i = 0; i <= num_slice_groups_minus1; i++ )
575
32.9k
                bs_read_ue( p_bs ); /* run_length_minus1[group] */
576
6.72k
        }
577
25.2k
        else if( slice_group_map_type == 2 )
578
3.67k
        {
579
16.1k
            for( unsigned i = 0; i < num_slice_groups_minus1; i++ )
580
12.4k
            {
581
12.4k
                bs_read_ue( p_bs ); /* top_left[group] */
582
12.4k
                bs_read_ue( p_bs ); /* bottom_right[group] */
583
12.4k
            }
584
3.67k
        }
585
21.5k
        else if( slice_group_map_type > 2 && slice_group_map_type < 6 )
586
3.67k
        {
587
3.67k
            bs_read1( p_bs );   /* slice_group_change_direction_flag */
588
3.67k
            bs_read_ue( p_bs ); /* slice_group_change_rate_minus1 */
589
3.67k
        }
590
17.8k
        else if( slice_group_map_type == 6 )
591
9.52k
        {
592
9.52k
            unsigned pic_size_in_maps_units = bs_read_ue( p_bs ) + 1;
593
            // Ceil( Log2( num_slice_groups_minus1 + 1 ) )
594
9.52k
            static const int ceil_log2_table[8] =
595
9.52k
            {
596
9.52k
                0,1,2,2,3,3,3,3
597
9.52k
            };
598
9.52k
            unsigned sliceGroupSize = ceil_log2_table[num_slice_groups_minus1];
599
            // slice_group_id[]
600
9.52k
            bs_skip( p_bs, sliceGroupSize * pic_size_in_maps_units );
601
9.52k
        }
602
31.9k
    }
603
604
103k
    p_pps->num_ref_idx_l01_default_active_minus1[0] = bs_read_ue( p_bs );
605
103k
    p_pps->num_ref_idx_l01_default_active_minus1[1] = bs_read_ue( p_bs );
606
103k
    if (p_pps->num_ref_idx_l01_default_active_minus1[0] > 31 ||
607
98.2k
        p_pps->num_ref_idx_l01_default_active_minus1[1] > 31)
608
11.1k
        return false;
609
92.6k
    p_pps->weighted_pred_flag = bs_read( p_bs, 1 );
610
92.6k
    p_pps->weighted_bipred_idc = bs_read( p_bs, 2 );
611
92.6k
    bs_read_se( p_bs ); /* pic_init_qp_minus26 */
612
92.6k
    bs_read_se( p_bs ); /* pic_init_qs_minus26 */
613
92.6k
    bs_read_se( p_bs ); /* chroma_qp_index_offset */
614
92.6k
    bs_skip( p_bs, 1 ); /* deblocking_filter_control_present_flag */
615
92.6k
    bs_skip( p_bs, 1 ); /* constrained_intra_pred_flag */
616
92.6k
    p_pps->i_redundant_pic_present_flag = bs_read( p_bs, 1 );
617
618
    /* TODO */
619
620
92.6k
    return true;
621
103k
}
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
211k
    { \
640
211k
        h264type *p_h264type = calloc(1, sizeof(h264type)); \
641
211k
        if(likely(p_h264type)) \
642
211k
        { \
643
211k
            bs_t bs; \
644
211k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
645
211k
            if( b_escaped ) \
646
211k
            { \
647
211k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
648
211k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
649
211k
            } \
650
211k
            else bs_init( &bs, p_buf, i_buf ); \
651
211k
            bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
652
211k
            if( !decode( &bs, p_h264type ) ) \
653
211k
            { \
654
90.8k
                release( p_h264type ); \
655
90.8k
                p_h264type = NULL; \
656
90.8k
            } \
657
211k
        } \
658
211k
        return p_h264type; \
659
211k
    }
660
661
97.4k
IMPL_h264_generic_decode( h264_decode_sps, h264_sequence_parameter_set_t,
662
                          h264_parse_sequence_parameter_set_rbsp, h264_release_sps )
663
664
114k
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
239k
{
750
239k
    if( i_buf < 2 )
751
4.39k
        return false;
752
753
    /* No need to lookup convert from emulation for that data */
754
234k
    uint8_t i_max, i_offset;
755
234k
    switch( h264_getNALType(p_buf) )
756
234k
    {
757
108k
        case H264_NAL_SPS:
758
108k
            i_offset = 1 + 3 /* profile constraint level */;
759
108k
            i_max = H264_SPS_ID_MAX;
760
108k
            break;
761
123k
        case H264_NAL_PPS:
762
123k
            i_offset = 1;
763
123k
            i_max = H264_PPS_ID_MAX;
764
123k
            break;
765
2.80k
        case H264_NAL_SPS_EXT:
766
2.80k
            i_offset = 1;
767
2.80k
            i_max = H264_SPSEXT_ID_MAX;
768
2.80k
            break;
769
0
        default:
770
0
            return false;
771
234k
    }
772
773
234k
    if( i_buf <= i_offset )
774
4.91k
        return false;
775
776
230k
    bs_t bs;
777
230k
    bs_init( &bs, &p_buf[i_offset], i_buf - i_offset );
778
230k
    *pi_id = bs_read_ue( &bs );
779
780
230k
    return !bs_error( &bs ) && *pi_id <= i_max;
781
234k
}
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
543k
{
790
543k
    return p_pps->i_sps_id;
791
543k
}
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
1.52k
{
867
1.52k
    return 1 << (p_sps->i_log2_max_frame_num + 4);
868
1.52k
}
869
870
bool h264_is_frames_only( const h264_sequence_parameter_set_t *p_sps )
871
208k
{
872
208k
    return p_sps->frame_mbs_only_flag;
873
208k
}
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
243k
{
884
243k
    *pi_profile = p_sps->i_profile;
885
243k
    *pi_level = p_sps->i_level;
886
243k
    return true;
887
243k
}
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
243k
{
894
243k
    unsigned CropUnitX = 1;
895
243k
    unsigned CropUnitY = 2 - p_sps->frame_mbs_only_flag;
896
243k
    if( p_sps->b_separate_colour_planes_flag != 1 )
897
238k
    {
898
238k
        if( p_sps->i_chroma_idc > 0 )
899
233k
        {
900
233k
            unsigned SubWidthC = 2;
901
233k
            unsigned SubHeightC = 2;
902
233k
            if( p_sps->i_chroma_idc > 1 )
903
8.95k
            {
904
8.95k
                SubHeightC = 1;
905
8.95k
                if( p_sps->i_chroma_idc > 2 )
906
499
                    SubWidthC = 1;
907
8.95k
            }
908
233k
            CropUnitX *= SubWidthC;
909
233k
            CropUnitY *= SubHeightC;
910
233k
        }
911
238k
    }
912
913
243k
    *p_w = 16 * p_sps->pic_width_in_mbs_minus1 + 16;
914
243k
    *p_h = 16 * p_sps->pic_height_in_map_units_minus1 + 16;
915
243k
    *p_h *= ( 2 - p_sps->frame_mbs_only_flag );
916
917
243k
    *p_ox = p_sps->frame_crop.left_offset * CropUnitX;
918
243k
    *p_oy = p_sps->frame_crop.top_offset * CropUnitY;
919
243k
    *p_vw = *p_w - ( p_sps->frame_crop.left_offset + p_sps->frame_crop.right_offset ) * CropUnitX;
920
243k
    *p_vh = *p_h - ( p_sps->frame_crop.bottom_offset + p_sps->frame_crop.top_offset ) * CropUnitY;
921
922
243k
    return true;
923
243k
}
924
925
bool h264_get_frame_rate( const h264_sequence_parameter_set_t *p_sps,
926
                         unsigned *pi_num, unsigned *pi_den )
927
4.02k
{
928
4.02k
    if(!p_sps->vui_parameters_present_flag || !p_sps->vui.i_num_units_in_tick ||
929
725
        p_sps->vui.i_time_scale <= 1)
930
3.31k
        return false;
931
717
    *pi_num = p_sps->vui.i_time_scale;
932
717
    *pi_den = p_sps->vui.i_num_units_in_tick;
933
717
    return true;
934
4.02k
}
935
936
bool h264_get_aspect_ratio( const h264_sequence_parameter_set_t *p_sps,
937
                           unsigned *pi_num, unsigned *pi_den )
938
243k
{
939
243k
    if(!p_sps->vui.i_sar_num || !p_sps->vui.i_sar_den)
940
147k
        return false;
941
95.7k
    *pi_num = p_sps->vui.i_sar_num;
942
95.7k
    *pi_den = p_sps->vui.i_sar_den;
943
95.7k
    return true;
944
243k
}
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
243k
{
960
243k
    if( !p_sps->vui_parameters_present_flag )
961
131k
        return false;
962
111k
    *p_primaries =
963
111k
        iso_23001_8_cp_to_vlc_primaries( p_sps->vui.colour.i_colour_primaries );
964
111k
    *p_transfer =
965
111k
        iso_23001_8_tc_to_vlc_xfer( p_sps->vui.colour.i_transfer_characteristics );
966
111k
    *p_colorspace =
967
111k
        iso_23001_8_mc_to_vlc_coeffs( p_sps->vui.colour.i_matrix_coefficients );
968
111k
    *p_full_range = p_sps->vui.colour.b_full_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
969
111k
    return true;
970
243k
}
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
1.09k
{
1011
1.09k
    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
1.09k
    return true;
1016
1.09k
}
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
19.2k
{
1022
19.2k
    if( !p_sps->vui_parameters_present_flag )
1023
6.51k
        return false;
1024
1025
12.7k
    if( p_sps->vui.b_hrd_parameters_present_flag )
1026
7.73k
    {
1027
7.73k
        bs_skip( p_bs, p_sps->vui.i_cpb_removal_delay_length_minus1 + 1 );
1028
7.73k
        *output_delay =
1029
7.73k
            bs_read( p_bs, p_sps->vui.i_dpb_output_delay_length_minus1 + 1 );
1030
7.73k
    }
1031
1032
12.7k
    if( p_sps->vui.b_pic_struct_present_flag )
1033
7.49k
        *pic_struct = bs_read( p_bs, 4 );
1034
1035
    /* + unparsed remains */
1036
    return true;
1037
19.2k
}