Coverage Report

Created: 2025-07-18 08:04

/src/vlc/modules/packetizer/h264_nal.c
Line
Count
Source (jump to first uncovered line)
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 size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
111
0
{
112
0
    size_t i_total = 0;
113
114
0
    if( i_buf < H264_MIN_AVCC_SIZE )
115
0
        return 0;
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 0;
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 0;
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 0;
141
0
    }
142
0
    return i_total;
143
0
}
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
0
{
148
0
    *pi_result = get_avcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does check min size */
149
0
    if( *pi_result == 0 )
150
0
        return NULL;
151
152
    /* Read infos in first 6 bytes */
153
0
    if ( pi_nal_length_size )
154
0
        *pi_nal_length_size = (p_buf[4] & 0x03) + 1;
155
156
0
    uint8_t *p_ret;
157
0
    uint8_t *p_out_buf = p_ret = malloc( *pi_result );
158
0
    if( !p_out_buf )
159
0
    {
160
0
        *pi_result = 0;
161
0
        return NULL;
162
0
    }
163
164
0
    p_buf += 5;
165
166
0
    for ( unsigned int j = 0; j < 2; j++ )
167
0
    {
168
0
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
169
0
        p_buf++;
170
171
0
        for ( unsigned int i = 0; i < i_loop_end; i++)
172
0
        {
173
0
            uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
174
0
            p_buf += 2;
175
176
0
            memcpy( p_out_buf, annexb_startcode4, 4 );
177
0
            p_out_buf += 4;
178
179
0
            memcpy( p_out_buf, p_buf, i_nal_size );
180
0
            p_out_buf += i_nal_size;
181
0
            p_buf += i_nal_size;
182
0
        }
183
0
    }
184
185
0
    return p_ret;
186
0
}
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
92.8k
{
270
92.8k
    free( p_sps );
271
92.8k
}
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
92.8k
{
278
92.8k
    int i_tmp;
279
280
92.8k
    int i_profile_idc = bs_read( p_bs, 8 );
281
92.8k
    p_sps->i_profile = i_profile_idc;
282
92.8k
    p_sps->i_constraint_set_flags = bs_read( p_bs, 8 );
283
92.8k
    p_sps->i_level = bs_read( p_bs, 8 );
284
    /* sps id */
285
92.8k
    uint32_t i_sps_id = bs_read_ue( p_bs );
286
92.8k
    if( i_sps_id > H264_SPS_ID_MAX )
287
886
        return false;
288
91.9k
    p_sps->i_id = i_sps_id;
289
290
91.9k
    if( i_profile_idc == PROFILE_H264_HIGH ||
291
91.9k
        i_profile_idc == PROFILE_H264_HIGH_10 ||
292
91.9k
        i_profile_idc == PROFILE_H264_HIGH_422 ||
293
91.9k
        i_profile_idc == PROFILE_H264_HIGH_444 || /* Old one, no longer on spec */
294
91.9k
        i_profile_idc == PROFILE_H264_HIGH_444_PREDICTIVE ||
295
91.9k
        i_profile_idc == PROFILE_H264_CAVLC_INTRA ||
296
91.9k
        i_profile_idc == PROFILE_H264_SVC_BASELINE ||
297
91.9k
        i_profile_idc == PROFILE_H264_SVC_HIGH ||
298
91.9k
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_HIGH ||
299
91.9k
        i_profile_idc == PROFILE_H264_MVC_STEREO_HIGH ||
300
91.9k
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH ||
301
91.9k
        i_profile_idc == PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH ||
302
91.9k
        i_profile_idc == PROFILE_H264_MFC_HIGH )
303
40.9k
    {
304
        /* chroma_format_idc */
305
40.9k
        p_sps->i_chroma_idc = bs_read_ue( p_bs );
306
40.9k
        if( p_sps->i_chroma_idc == 3 )
307
4.78k
            p_sps->b_separate_colour_planes_flag = bs_read1( p_bs );
308
36.1k
        else
309
36.1k
            p_sps->b_separate_colour_planes_flag = 0;
310
        /* bit_depth_luma_minus8 */
311
40.9k
        p_sps->i_bit_depth_luma = bs_read_ue( p_bs ) + 8;
312
        /* bit_depth_chroma_minus8 */
313
40.9k
        p_sps->i_bit_depth_chroma = bs_read_ue( p_bs ) + 8;
314
        /* qpprime_y_zero_transform_bypass_flag */
315
40.9k
        bs_skip( p_bs, 1 );
316
        /* seq_scaling_matrix_present_flag */
317
40.9k
        i_tmp = bs_read( p_bs, 1 );
318
40.9k
        if( i_tmp )
319
10.2k
        {
320
95.8k
            for( int i = 0; i < ((3 != p_sps->i_chroma_idc) ? 8 : 12); i++ )
321
85.6k
            {
322
                /* seq_scaling_list_present_flag[i] */
323
85.6k
                i_tmp = bs_read( p_bs, 1 );
324
85.6k
                if( !i_tmp )
325
61.3k
                    continue;
326
24.3k
                const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
327
                /* scaling_list (...) */
328
24.3k
                int i_lastscale = 8;
329
24.3k
                int i_nextscale = 8;
330
551k
                for( int j = 0; j < i_size_of_scaling_list; j++ )
331
527k
                {
332
527k
                    if( i_nextscale != 0 )
333
501k
                    {
334
                        /* delta_scale */
335
501k
                        i_tmp = bs_read_se( p_bs );
336
501k
                        i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
337
                        /* useDefaultScalingMatrixFlag = ... */
338
501k
                    }
339
                    /* scalinglist[j] */
340
527k
                    i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
341
527k
                }
342
24.3k
            }
343
10.2k
        }
344
40.9k
    }
345
50.9k
    else
346
50.9k
    {
347
50.9k
        p_sps->i_chroma_idc = 1; /* Not present == inferred to 4:2:0 */
348
50.9k
        p_sps->i_bit_depth_luma = 8;
349
50.9k
        p_sps->i_bit_depth_chroma = 8;
350
50.9k
    }
351
352
    /* Skip i_log2_max_frame_num */
353
91.9k
    p_sps->i_log2_max_frame_num = bs_read_ue( p_bs );
354
91.9k
    if( p_sps->i_log2_max_frame_num > 12)
355
8.27k
        p_sps->i_log2_max_frame_num = 12;
356
    /* Read poc_type */
357
91.9k
    p_sps->i_pic_order_cnt_type = bs_read_ue( p_bs );
358
91.9k
    if( p_sps->i_pic_order_cnt_type == 0 )
359
54.4k
    {
360
        /* skip i_log2_max_poc_lsb */
361
54.4k
        p_sps->i_log2_max_pic_order_cnt_lsb = bs_read_ue( p_bs );
362
54.4k
        if( p_sps->i_log2_max_pic_order_cnt_lsb > 12 )
363
2.60k
            p_sps->i_log2_max_pic_order_cnt_lsb = 12;
364
54.4k
    }
365
37.4k
    else if( p_sps->i_pic_order_cnt_type == 1 )
366
16.7k
    {
367
16.7k
        p_sps->i_delta_pic_order_always_zero_flag = bs_read( p_bs, 1 );
368
16.7k
        p_sps->offset_for_non_ref_pic = bs_read_se( p_bs );
369
16.7k
        p_sps->offset_for_top_to_bottom_field = bs_read_se( p_bs );
370
16.7k
        p_sps->i_num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue( p_bs );
371
16.7k
        if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 255 )
372
601
            return false;
373
99.0k
        for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ )
374
82.9k
            p_sps->offset_for_ref_frame[i] = bs_read_se( p_bs );
375
16.1k
    }
376
    /* i_num_ref_frames */
377
91.3k
    bs_read_ue( p_bs );
378
    /* b_gaps_in_frame_num_value_allowed */
379
91.3k
    bs_skip( p_bs, 1 );
380
381
    /* Read size */
382
91.3k
    p_sps->pic_width_in_mbs_minus1 = bs_read_ue( p_bs );
383
91.3k
    p_sps->pic_height_in_map_units_minus1 = bs_read_ue( p_bs );
384
385
    /* b_frame_mbs_only */
386
91.3k
    p_sps->frame_mbs_only_flag = bs_read( p_bs, 1 );
387
91.3k
    if( !p_sps->frame_mbs_only_flag )
388
48.6k
        p_sps->mb_adaptive_frame_field_flag = bs_read( p_bs, 1 );
389
390
    /* b_direct8x8_inference */
391
91.3k
    bs_skip( p_bs, 1 );
392
393
    /* crop */
394
91.3k
    if( bs_read1( p_bs ) ) /* frame_cropping_flag */
395
33.3k
    {
396
33.3k
        p_sps->frame_crop.left_offset = bs_read_ue( p_bs );
397
33.3k
        p_sps->frame_crop.right_offset = bs_read_ue( p_bs );
398
33.3k
        p_sps->frame_crop.top_offset = bs_read_ue( p_bs );
399
33.3k
        p_sps->frame_crop.bottom_offset = bs_read_ue( p_bs );
400
33.3k
    }
401
402
    /* vui */
403
91.3k
    p_sps->vui_parameters_present_flag = bs_read( p_bs, 1 );
404
91.3k
    if( p_sps->vui_parameters_present_flag )
405
49.2k
    {
406
        /* read the aspect ratio part if any */
407
49.2k
        i_tmp = bs_read( p_bs, 1 );
408
49.2k
        if( i_tmp )
409
30.1k
        {
410
30.1k
            static const struct { int w, h; } sar[17] =
411
30.1k
            {
412
30.1k
                { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
413
30.1k
                { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
414
30.1k
                { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
415
30.1k
                { 64, 33 }, { 160,99 }, {  4,  3 }, {  3,  2 },
416
30.1k
                {  2,  1 },
417
30.1k
            };
418
30.1k
            int i_sar = bs_read( p_bs, 8 );
419
30.1k
            int w, h;
420
421
30.1k
            if( i_sar < 17 )
422
2.02k
            {
423
2.02k
                w = sar[i_sar].w;
424
2.02k
                h = sar[i_sar].h;
425
2.02k
            }
426
28.1k
            else if( i_sar == 255 )
427
6.71k
            {
428
6.71k
                w = bs_read( p_bs, 16 );
429
6.71k
                h = bs_read( p_bs, 16 );
430
6.71k
            }
431
21.3k
            else
432
21.3k
            {
433
21.3k
                w = 0;
434
21.3k
                h = 0;
435
21.3k
            }
436
437
30.1k
            if( w != 0 && h != 0 )
438
6.99k
            {
439
6.99k
                p_sps->vui.i_sar_num = w;
440
6.99k
                p_sps->vui.i_sar_den = h;
441
6.99k
            }
442
23.1k
            else
443
23.1k
            {
444
23.1k
                p_sps->vui.i_sar_num = 1;
445
23.1k
                p_sps->vui.i_sar_den = 1;
446
23.1k
            }
447
30.1k
        }
448
449
        /* overscan */
450
49.2k
        i_tmp = bs_read( p_bs, 1 );
451
49.2k
        if ( i_tmp )
452
20.2k
            bs_skip( p_bs, 1 );
453
454
        /* video signal type */
455
49.2k
        i_tmp = bs_read( p_bs, 1 );
456
49.2k
        if( i_tmp )
457
23.1k
        {
458
23.1k
            bs_skip( p_bs, 3 );
459
23.1k
            p_sps->vui.colour.b_full_range = bs_read( p_bs, 1 );
460
            /* colour desc */
461
23.1k
            i_tmp = bs_read( p_bs, 1 );
462
23.1k
            if ( i_tmp )
463
10.7k
            {
464
10.7k
                p_sps->vui.colour.i_colour_primaries = bs_read( p_bs, 8 );
465
10.7k
                p_sps->vui.colour.i_transfer_characteristics = bs_read( p_bs, 8 );
466
10.7k
                p_sps->vui.colour.i_matrix_coefficients = bs_read( p_bs, 8 );
467
10.7k
            }
468
12.4k
            else
469
12.4k
            {
470
12.4k
                p_sps->vui.colour.i_colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
471
12.4k
                p_sps->vui.colour.i_transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
472
12.4k
                p_sps->vui.colour.i_matrix_coefficients = ISO_23001_8_MC_UNSPECIFIED;
473
12.4k
            }
474
23.1k
        }
475
476
        /* chroma loc info */
477
49.2k
        i_tmp = bs_read( p_bs, 1 );
478
49.2k
        if( i_tmp )
479
16.7k
        {
480
16.7k
            bs_read_ue( p_bs );
481
16.7k
            bs_read_ue( p_bs );
482
16.7k
        }
483
484
        /* timing info */
485
49.2k
        p_sps->vui.b_timing_info_present_flag = bs_read( p_bs, 1 );
486
49.2k
        if( p_sps->vui.b_timing_info_present_flag )
487
13.5k
        {
488
13.5k
            p_sps->vui.i_num_units_in_tick = bs_read( p_bs, 32 );
489
13.5k
            p_sps->vui.i_time_scale = bs_read( p_bs, 32 );
490
13.5k
            p_sps->vui.b_fixed_frame_rate = bs_read( p_bs, 1 );
491
13.5k
        }
492
493
        /* Nal hrd & VC1 hrd parameters */
494
49.2k
        p_sps->vui.b_hrd_parameters_present_flag = false;
495
133k
        for ( int i=0; i<2; i++ )
496
94.3k
        {
497
94.3k
            i_tmp = bs_read( p_bs, 1 );
498
94.3k
            if( i_tmp )
499
29.2k
            {
500
29.2k
                p_sps->vui.b_hrd_parameters_present_flag = true;
501
29.2k
                uint32_t count = bs_read_ue( p_bs ) + 1;
502
29.2k
                if( count > 31 )
503
2.14k
                    return false;
504
27.0k
                bs_skip( p_bs, 4 );
505
27.0k
                bs_skip( p_bs, 4 );
506
94.9k
                for( uint32_t j = 0; j < count; j++ )
507
75.4k
                {
508
75.4k
                    bs_read_ue( p_bs );
509
75.4k
                    bs_read_ue( p_bs );
510
75.4k
                    bs_skip( p_bs, 1 );
511
75.4k
                    if( bs_error( p_bs ) )
512
7.56k
                        return false;
513
75.4k
                }
514
19.5k
                bs_skip( p_bs, 5 );
515
19.5k
                p_sps->vui.i_cpb_removal_delay_length_minus1 = bs_read( p_bs, 5 );
516
19.5k
                p_sps->vui.i_dpb_output_delay_length_minus1 = bs_read( p_bs, 5 );
517
19.5k
                bs_skip( p_bs, 5 );
518
19.5k
            }
519
94.3k
        }
520
521
39.5k
        if( p_sps->vui.b_hrd_parameters_present_flag )
522
12.3k
            bs_skip( p_bs, 1 ); /* low delay hrd */
523
524
        /* pic struct info */
525
39.5k
        p_sps->vui.b_pic_struct_present_flag = bs_read( p_bs, 1 );
526
527
39.5k
        p_sps->vui.b_bitstream_restriction_flag = bs_read( p_bs, 1 );
528
39.5k
        if( p_sps->vui.b_bitstream_restriction_flag )
529
9.45k
        {
530
9.45k
            bs_skip( p_bs, 1 ); /* motion vector pic boundaries */
531
9.45k
            bs_read_ue( p_bs ); /* max bytes per pic */
532
9.45k
            bs_read_ue( p_bs ); /* max bits per mb */
533
9.45k
            bs_read_ue( p_bs ); /* log2 max mv h */
534
9.45k
            bs_read_ue( p_bs ); /* log2 max mv v */
535
9.45k
            p_sps->vui.i_max_num_reorder_frames = bs_read_ue( p_bs );
536
9.45k
            p_sps->vui.i_max_dec_frame_buffering = bs_read_ue( p_bs );
537
9.45k
        }
538
39.5k
    }
539
540
81.6k
    return !bs_error( p_bs );
541
91.3k
}
542
543
void h264_release_pps( h264_picture_parameter_set_t *p_pps )
544
83.3k
{
545
83.3k
    free( p_pps );
546
83.3k
}
547
548
static bool h264_parse_picture_parameter_set_rbsp( bs_t *p_bs,
549
                                                   h264_picture_parameter_set_t *p_pps )
550
83.3k
{
551
83.3k
    uint32_t i_pps_id = bs_read_ue( p_bs ); // pps id
552
83.3k
    uint32_t i_sps_id = bs_read_ue( p_bs ); // sps id
553
83.3k
    if( i_pps_id > H264_PPS_ID_MAX || i_sps_id > H264_SPS_ID_MAX )
554
4.79k
        return false;
555
78.6k
    p_pps->i_id = i_pps_id;
556
78.6k
    p_pps->i_sps_id = i_sps_id;
557
558
78.6k
    bs_skip( p_bs, 1 ); // entropy coding mode flag
559
78.6k
    p_pps->i_pic_order_present_flag = bs_read( p_bs, 1 );
560
561
78.6k
    unsigned num_slice_groups = bs_read_ue( p_bs ) + 1;
562
78.6k
    if( num_slice_groups > 8 ) /* never has value > 7. Annex A, G & J */
563
4.09k
        return false;
564
74.5k
    if( num_slice_groups > 1 )
565
22.4k
    {
566
22.4k
        unsigned slice_group_map_type = bs_read_ue( p_bs );
567
22.4k
        if( slice_group_map_type == 0 )
568
6.44k
        {
569
39.2k
            for( unsigned i = 0; i < num_slice_groups; i++ )
570
32.7k
                bs_read_ue( p_bs ); /* run_length_minus1[group] */
571
6.44k
        }
572
16.0k
        else if( slice_group_map_type == 2 )
573
3.14k
        {
574
16.7k
            for( unsigned i = 0; i < num_slice_groups; i++ )
575
13.5k
            {
576
13.5k
                bs_read_ue( p_bs ); /* top_left[group] */
577
13.5k
                bs_read_ue( p_bs ); /* bottom_right[group] */
578
13.5k
            }
579
3.14k
        }
580
12.8k
        else if( slice_group_map_type > 2 && slice_group_map_type < 6 )
581
4.16k
        {
582
4.16k
            bs_read1( p_bs );   /* slice_group_change_direction_flag */
583
4.16k
            bs_read_ue( p_bs ); /* slice_group_change_rate_minus1 */
584
4.16k
        }
585
8.72k
        else if( slice_group_map_type == 6 )
586
2.10k
        {
587
2.10k
            unsigned pic_size_in_maps_units = bs_read_ue( p_bs ) + 1;
588
2.10k
            unsigned sliceGroupSize = 1;
589
6.67k
            while(num_slice_groups > 1)
590
4.57k
            {
591
4.57k
                sliceGroupSize++;
592
4.57k
                num_slice_groups = ((num_slice_groups - 1) >> 1) + 1;
593
4.57k
            }
594
1.44G
            for( unsigned i = 0; i < pic_size_in_maps_units; i++ )
595
1.44G
            {
596
1.44G
                bs_skip( p_bs, sliceGroupSize );
597
1.44G
            }
598
2.10k
        }
599
22.4k
    }
600
601
74.5k
    p_pps->num_ref_idx_l01_default_active_minus1[0] = bs_read_ue( p_bs );
602
74.5k
    p_pps->num_ref_idx_l01_default_active_minus1[1] = bs_read_ue( p_bs );
603
74.5k
    p_pps->weighted_pred_flag = bs_read( p_bs, 1 );
604
74.5k
    p_pps->weighted_bipred_idc = bs_read( p_bs, 2 );
605
74.5k
    bs_read_se( p_bs ); /* pic_init_qp_minus26 */
606
74.5k
    bs_read_se( p_bs ); /* pic_init_qs_minus26 */
607
74.5k
    bs_read_se( p_bs ); /* chroma_qp_index_offset */
608
74.5k
    bs_skip( p_bs, 1 ); /* deblocking_filter_control_present_flag */
609
74.5k
    bs_skip( p_bs, 1 ); /* constrained_intra_pred_flag */
610
74.5k
    p_pps->i_redundant_pic_present_flag = bs_read( p_bs, 1 );
611
612
    /* TODO */
613
614
74.5k
    return true;
615
78.6k
}
616
617
static bool h264_parse_sequence_parameter_set_extension_rbsp( bs_t *p_bs,
618
                                 h264_sequence_parameter_set_extension_t *p_sps_ext )
619
0
{
620
0
    p_sps_ext->i_sps_id = bs_read_ue( p_bs );
621
0
    if( p_sps_ext->i_sps_id > H264_SPSEXT_ID_MAX )
622
0
        return false;
623
0
    return true;
624
0
}
625
626
void h264_release_sps_extension( h264_sequence_parameter_set_extension_t *p_sps_ext )
627
0
{
628
0
    free( p_sps_ext );
629
0
}
630
631
#define IMPL_h264_generic_decode( name, h264type, decode, release ) \
632
    h264type * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
633
176k
    { \
634
176k
        h264type *p_h264type = calloc(1, sizeof(h264type)); \
635
176k
        if(likely(p_h264type)) \
636
176k
        { \
637
176k
            bs_t bs; \
638
176k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
639
176k
            if( b_escaped ) \
640
176k
            { \
641
176k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
642
176k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
643
176k
            } \
644
176k
            else bs_init( &bs, p_buf, i_buf ); \
645
176k
            bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
646
176k
            if( !decode( &bs, p_h264type ) ) \
647
176k
            { \
648
76.2k
                release( p_h264type ); \
649
76.2k
                p_h264type = NULL; \
650
76.2k
            } \
651
176k
        } \
652
176k
        return p_h264type; \
653
176k
    }
h264_decode_sps
Line
Count
Source
633
92.8k
    { \
634
92.8k
        h264type *p_h264type = calloc(1, sizeof(h264type)); \
635
92.8k
        if(likely(p_h264type)) \
636
92.8k
        { \
637
92.8k
            bs_t bs; \
638
92.8k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
639
92.8k
            if( b_escaped ) \
640
92.8k
            { \
641
92.8k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
642
92.8k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
643
92.8k
            } \
644
92.8k
            else bs_init( &bs, p_buf, i_buf ); \
645
92.8k
            bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
646
92.8k
            if( !decode( &bs, p_h264type ) ) \
647
92.8k
            { \
648
67.3k
                release( p_h264type ); \
649
67.3k
                p_h264type = NULL; \
650
67.3k
            } \
651
92.8k
        } \
652
92.8k
        return p_h264type; \
653
92.8k
    }
h264_decode_pps
Line
Count
Source
633
83.3k
    { \
634
83.3k
        h264type *p_h264type = calloc(1, sizeof(h264type)); \
635
83.3k
        if(likely(p_h264type)) \
636
83.3k
        { \
637
83.3k
            bs_t bs; \
638
83.3k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
639
83.3k
            if( b_escaped ) \
640
83.3k
            { \
641
83.3k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
642
83.3k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
643
83.3k
            } \
644
83.3k
            else bs_init( &bs, p_buf, i_buf ); \
645
83.3k
            bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
646
83.3k
            if( !decode( &bs, p_h264type ) ) \
647
83.3k
            { \
648
8.88k
                release( p_h264type ); \
649
8.88k
                p_h264type = NULL; \
650
8.88k
            } \
651
83.3k
        } \
652
83.3k
        return p_h264type; \
653
83.3k
    }
Unexecuted instantiation: h264_decode_sps_extension
654
655
IMPL_h264_generic_decode( h264_decode_sps, h264_sequence_parameter_set_t,
656
                          h264_parse_sequence_parameter_set_rbsp, h264_release_sps )
657
658
IMPL_h264_generic_decode( h264_decode_pps, h264_picture_parameter_set_t,
659
                          h264_parse_picture_parameter_set_rbsp, h264_release_pps )
660
661
IMPL_h264_generic_decode( h264_decode_sps_extension, h264_sequence_parameter_set_extension_t,
662
                          h264_parse_sequence_parameter_set_extension_rbsp, h264_release_sps_extension )
663
664
block_t *h264_NAL_to_avcC( uint8_t i_nal_length_size,
665
                           const uint8_t **pp_sps_buf,
666
                           const size_t *p_sps_size, uint8_t i_sps_count,
667
                           const uint8_t **pp_pps_buf,
668
                           const size_t *p_pps_size, uint8_t i_pps_count,
669
                           const uint8_t **pp_sps_ext_buf,
670
                           const size_t *p_sps_ext_size, uint8_t i_sps_ext_count )
671
0
{
672
    /* The length of the NAL size is encoded using 1, 2 or 4 bytes */
673
0
    if( i_nal_length_size != 1 && i_nal_length_size != 2
674
0
     && i_nal_length_size != 4 )
675
0
        return NULL;
676
0
    if( i_sps_count == 0 || i_sps_count > H264_SPS_ID_MAX || i_pps_count == 0 )
677
0
        return NULL;
678
679
    /* Calculate the total size of all SPS and PPS NALs */
680
0
    size_t i_spspps_size = 0;
681
0
    for( size_t i = 0; i < i_sps_count; ++i )
682
0
    {
683
0
        assert( pp_sps_buf[i] && p_sps_size[i] );
684
0
        if( p_sps_size[i] < 4 || p_sps_size[i] > UINT16_MAX )
685
0
            return NULL;
686
0
        i_spspps_size += p_sps_size[i] +  2 /* 16be size place holder */;
687
0
    }
688
0
    for( size_t i = 0; i < i_pps_count; ++i )
689
0
    {
690
0
        assert( pp_pps_buf[i] && p_pps_size[i] );
691
0
        if( p_pps_size[i] > UINT16_MAX)
692
0
            return NULL;
693
0
        i_spspps_size += p_pps_size[i] +  2 /* 16be size place holder */;
694
0
    }
695
696
0
    bo_t bo;
697
    /* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
698
0
    if( bo_init( &bo, 7 + i_spspps_size ) != true )
699
0
        return NULL;
700
701
0
    bo_add_8( &bo, 1 ); /* configuration version */
702
0
    bo_add_mem( &bo, 3, &pp_sps_buf[0][1] ); /* i_profile/profile_compatibility/level */
703
0
    bo_add_8( &bo, 0xfc | (i_nal_length_size - 1) ); /* 0b11111100 | lengthsize - 1*/
704
705
0
    bo_add_8( &bo, 0xe0 | i_sps_count ); /* 0b11100000 | sps_count */
706
0
    for( size_t i = 0; i < i_sps_count; ++i )
707
0
    {
708
0
        bo_add_16be( &bo, p_sps_size[i] );
709
0
        bo_add_mem( &bo, p_sps_size[i], pp_sps_buf[i] );
710
0
    }
711
712
0
    bo_add_8( &bo, i_pps_count ); /* pps_count */
713
0
    for( size_t i = 0; i < i_pps_count; ++i )
714
0
    {
715
0
        bo_add_16be( &bo, p_pps_size[i] );
716
0
        bo_add_mem( &bo, p_pps_size[i], pp_pps_buf[i] );
717
0
    }
718
719
0
    const uint8_t i_profile = pp_sps_buf[0][1];
720
0
    if( i_profile == PROFILE_H264_HIGH ||
721
0
        i_profile == PROFILE_H264_HIGH_10 ||
722
0
        i_profile == PROFILE_H264_HIGH_422 ||
723
0
        i_profile == PROFILE_H264_HIGH_444 )
724
0
    {
725
0
        h264_sequence_parameter_set_t *p_sps = h264_decode_sps( pp_sps_buf[0], p_sps_size[0], true );
726
0
        bo_add_8( &bo, 0xfc | (p_sps ? p_sps->i_chroma_idc : 0) );
727
0
        bo_add_8( &bo, 0xf8 | (p_sps ? (p_sps->i_bit_depth_luma - 8) : 0) );
728
0
        bo_add_8( &bo, 0xf8 | (p_sps ? (p_sps->i_bit_depth_chroma - 8) : 0) );
729
0
        if( p_sps )
730
0
            h264_release_sps( p_sps );
731
0
        bo_add_8( &bo, i_sps_ext_count );
732
0
        for( size_t i = 0; i < i_sps_ext_count; ++i )
733
0
        {
734
0
            bo_add_16be( &bo, p_sps_ext_size[i] );
735
0
            bo_add_mem( &bo, p_sps_ext_size[i], pp_sps_ext_buf[i] );
736
0
        }
737
0
    }
738
739
0
    return bo.b;
740
0
}
741
742
bool h264_get_xps_id( const uint8_t *p_buf, size_t i_buf, uint8_t *pi_id )
743
216k
{
744
216k
    if( i_buf < 2 )
745
7.50k
        return false;
746
747
    /* No need to lookup convert from emulation for that data */
748
209k
    uint8_t i_max, i_offset;
749
209k
    switch( h264_getNALType(p_buf) )
750
209k
    {
751
104k
        case H264_NAL_SPS:
752
104k
            i_offset = 1 + 3 /* profile constraint level */;
753
104k
            i_max = H264_SPS_ID_MAX;
754
104k
            break;
755
100k
        case H264_NAL_PPS:
756
100k
            i_offset = 1;
757
100k
            i_max = H264_PPS_ID_MAX;
758
100k
            break;
759
3.65k
        case H264_NAL_SPS_EXT:
760
3.65k
            i_offset = 1;
761
3.65k
            i_max = H264_SPSEXT_ID_MAX;
762
3.65k
            break;
763
0
        default:
764
0
            return false;
765
209k
    }
766
767
209k
    if( i_buf <= i_offset )
768
3.80k
        return false;
769
770
205k
    bs_t bs;
771
205k
    bs_init( &bs, &p_buf[i_offset], i_buf - i_offset );
772
205k
    *pi_id = bs_read_ue( &bs );
773
774
205k
    return !bs_error( &bs ) && *pi_id <= i_max;
775
209k
}
776
777
uint8_t h264_get_sps_id( const h264_sequence_parameter_set_t *p_sps )
778
0
{
779
0
    return p_sps->i_id;
780
0
}
781
782
uint8_t h264_get_pps_sps_id( const h264_picture_parameter_set_t *p_pps )
783
530k
{
784
530k
    return p_pps->i_sps_id;
785
530k
}
786
787
static const h264_level_limits_t * h264_get_level_limits( const h264_sequence_parameter_set_t *p_sps )
788
0
{
789
0
    uint16_t i_level_number = p_sps->i_level;
790
0
    if( i_level_number == H264_LEVEL_NUMBER_1_1 &&
791
0
       (p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3)) )
792
0
    {
793
0
        i_level_number = H264_LEVEL_NUMBER_1_B;
794
0
    }
795
796
0
    for( size_t i=0; i< ARRAY_SIZE(h264_levels_limits); i++ )
797
0
        if( h264_levels_limits[i].i_level == i_level_number )
798
0
            return & h264_levels_limits[i].limits;
799
800
0
    return NULL;
801
0
}
802
803
static uint8_t h264_get_max_dpb_frames( const h264_sequence_parameter_set_t *p_sps )
804
0
{
805
0
    const h264_level_limits_t *limits = h264_get_level_limits( p_sps );
806
0
    if( limits )
807
0
    {
808
0
        unsigned i_frame_height_in_mbs = ( p_sps->pic_height_in_map_units_minus1 + 1 ) *
809
0
                                         ( 2 - p_sps->frame_mbs_only_flag );
810
0
        unsigned i_den = ( p_sps->pic_width_in_mbs_minus1 + 1 ) * i_frame_height_in_mbs;
811
0
        uint8_t i_max_dpb_frames = limits->i_max_dpb_mbs / i_den;
812
0
        if( i_max_dpb_frames < 16 )
813
0
            return i_max_dpb_frames;
814
0
    }
815
0
    return 16;
816
0
}
817
818
bool h264_get_dpb_values( const h264_sequence_parameter_set_t *p_sps,
819
                          uint8_t *pi_max_num_reorder, uint8_t *pi_max_dec_buffering )
820
0
{
821
0
    uint8_t i_max_num_reorder_frames = p_sps->vui.i_max_num_reorder_frames;
822
0
    uint8_t i_max_dec_frame_buffering = p_sps->vui.i_max_dec_frame_buffering;
823
0
    if( !p_sps->vui.b_bitstream_restriction_flag )
824
0
    {
825
0
        switch( p_sps->i_profile ) /* E-2.1 */
826
0
        {
827
0
            case PROFILE_H264_BASELINE:
828
0
                i_max_num_reorder_frames = 0; /* only I & P */
829
0
                break;
830
0
            case PROFILE_H264_CAVLC_INTRA:
831
0
            case PROFILE_H264_SVC_HIGH:
832
0
            case PROFILE_H264_HIGH:
833
0
            case PROFILE_H264_HIGH_10:
834
0
            case PROFILE_H264_HIGH_422:
835
0
            case PROFILE_H264_HIGH_444_PREDICTIVE:
836
0
                if( p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3) )
837
0
                {
838
0
                    i_max_num_reorder_frames =
839
0
                    i_max_dec_frame_buffering = 0; /* all IDR */
840
0
                    break;
841
0
                }
842
                /* fallthrough */
843
0
            default:
844
0
                i_max_num_reorder_frames =
845
0
                i_max_dec_frame_buffering = h264_get_max_dpb_frames( p_sps );
846
0
                break;
847
0
        }
848
0
    }
849
850
0
    if( i_max_num_reorder_frames > i_max_dec_frame_buffering )
851
0
        i_max_num_reorder_frames = i_max_dec_frame_buffering;
852
853
0
    *pi_max_num_reorder = i_max_num_reorder_frames;
854
0
    *pi_max_dec_buffering = i_max_dec_frame_buffering;
855
856
0
    return true;
857
0
}
858
859
unsigned h264_get_max_frame_num( const h264_sequence_parameter_set_t *p_sps )
860
1.86k
{
861
1.86k
    return 1 << (p_sps->i_log2_max_frame_num + 4);
862
1.86k
}
863
864
bool h264_is_frames_only( const h264_sequence_parameter_set_t *p_sps )
865
204k
{
866
204k
    return p_sps->frame_mbs_only_flag;
867
204k
}
868
869
bool h264_using_adaptive_frames( const h264_sequence_parameter_set_t *p_sps )
870
0
{
871
0
    return p_sps->mb_adaptive_frame_field_flag;
872
0
}
873
874
875
bool h264_get_sps_profile_tier_level( const h264_sequence_parameter_set_t *p_sps,
876
                                      uint8_t *pi_profile, uint8_t *pi_level)
877
246k
{
878
246k
    *pi_profile = p_sps->i_profile;
879
246k
    *pi_level = p_sps->i_level;
880
246k
    return true;
881
246k
}
882
883
bool h264_get_picture_size( const h264_sequence_parameter_set_t *p_sps,
884
                            unsigned *p_ox, unsigned *p_oy,
885
                            unsigned *p_w, unsigned *p_h,
886
                            unsigned *p_vw, unsigned *p_vh )
887
246k
{
888
246k
    unsigned CropUnitX = 1;
889
246k
    unsigned CropUnitY = 2 - p_sps->frame_mbs_only_flag;
890
246k
    if( p_sps->b_separate_colour_planes_flag != 1 )
891
243k
    {
892
243k
        if( p_sps->i_chroma_idc > 0 )
893
217k
        {
894
217k
            unsigned SubWidthC = 2;
895
217k
            unsigned SubHeightC = 2;
896
217k
            if( p_sps->i_chroma_idc > 1 )
897
125k
            {
898
125k
                SubHeightC = 1;
899
125k
                if( p_sps->i_chroma_idc > 2 )
900
16.4k
                    SubWidthC = 1;
901
125k
            }
902
217k
            CropUnitX *= SubWidthC;
903
217k
            CropUnitY *= SubHeightC;
904
217k
        }
905
243k
    }
906
907
246k
    *p_w = 16 * p_sps->pic_width_in_mbs_minus1 + 16;
908
246k
    *p_h = 16 * p_sps->pic_height_in_map_units_minus1 + 16;
909
246k
    *p_h *= ( 2 - p_sps->frame_mbs_only_flag );
910
911
246k
    *p_ox = p_sps->frame_crop.left_offset * CropUnitX;
912
246k
    *p_oy = p_sps->frame_crop.top_offset * CropUnitY;
913
246k
    *p_vw = *p_w - ( p_sps->frame_crop.left_offset + p_sps->frame_crop.right_offset ) * CropUnitX;
914
246k
    *p_vh = *p_h - ( p_sps->frame_crop.bottom_offset + p_sps->frame_crop.top_offset ) * CropUnitY;
915
916
246k
    return true;
917
246k
}
918
919
bool h264_get_frame_rate( const h264_sequence_parameter_set_t *p_sps,
920
                         unsigned *pi_num, unsigned *pi_den )
921
3.41k
{
922
3.41k
    if(!p_sps->vui_parameters_present_flag || !p_sps->vui.i_num_units_in_tick ||
923
3.41k
        p_sps->vui.i_time_scale <= 1)
924
2.74k
        return false;
925
677
    *pi_num = p_sps->vui.i_time_scale;
926
677
    *pi_den = p_sps->vui.i_num_units_in_tick;
927
677
    return true;
928
3.41k
}
929
930
bool h264_get_aspect_ratio( const h264_sequence_parameter_set_t *p_sps,
931
                           unsigned *pi_num, unsigned *pi_den )
932
246k
{
933
246k
    if(!p_sps->vui.i_sar_num || !p_sps->vui.i_sar_den)
934
183k
        return false;
935
62.9k
    *pi_num = p_sps->vui.i_sar_num;
936
62.9k
    *pi_den = p_sps->vui.i_sar_den;
937
62.9k
    return true;
938
246k
}
939
940
bool h264_get_chroma_luma( const h264_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format,
941
                           uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma )
942
0
{
943
0
    *pi_chroma_format = p_sps->i_chroma_idc;
944
0
    *pi_depth_luma = p_sps->i_bit_depth_luma;
945
0
    *pi_depth_chroma = p_sps->i_bit_depth_chroma;
946
0
    return true;
947
0
}
948
bool h264_get_colorimetry( const h264_sequence_parameter_set_t *p_sps,
949
                           video_color_primaries_t *p_primaries,
950
                           video_transfer_func_t *p_transfer,
951
                           video_color_space_t *p_colorspace,
952
                           video_color_range_t *p_full_range )
953
246k
{
954
246k
    if( !p_sps->vui_parameters_present_flag )
955
166k
        return false;
956
79.8k
    *p_primaries =
957
79.8k
        iso_23001_8_cp_to_vlc_primaries( p_sps->vui.colour.i_colour_primaries );
958
79.8k
    *p_transfer =
959
79.8k
        iso_23001_8_tc_to_vlc_xfer( p_sps->vui.colour.i_transfer_characteristics );
960
79.8k
    *p_colorspace =
961
79.8k
        iso_23001_8_mc_to_vlc_coeffs( p_sps->vui.colour.i_matrix_coefficients );
962
79.8k
    *p_full_range = p_sps->vui.colour.b_full_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
963
79.8k
    return true;
964
246k
}
965
966
967
bool h264_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
968
                            uint8_t *pi_level, uint8_t *pi_nal_length_size)
969
0
{
970
0
    uint8_t *p = (uint8_t*)p_fmt->p_extra;
971
0
    if(p_fmt->i_extra < 8)
972
0
        return false;
973
974
    /* Check the profile / level */
975
0
    if (p[0] == 1 && p_fmt->i_extra >= 12)
976
0
    {
977
0
        if (pi_nal_length_size)
978
0
            *pi_nal_length_size = 1 + (p[4]&0x03);
979
0
        p += 8;
980
0
    }
981
0
    else if(!p[0] && !p[1]) /* FIXME: WTH is setting AnnexB data here ? */
982
0
    {
983
0
        if (!p[2] && p[3] == 1)
984
0
            p += 4;
985
0
        else if (p[2] == 1)
986
0
            p += 3;
987
0
        else
988
0
            return false;
989
0
    }
990
0
    else return false;
991
992
0
    if ( ((*p++)&0x1f) != 7) return false;
993
994
0
    if (pi_profile)
995
0
        *pi_profile = p[0];
996
997
0
    if (pi_level)
998
0
        *pi_level = p[2];
999
1000
0
    return true;
1001
0
}
1002
1003
bool h264_decode_sei_recovery_point( bs_t *p_bs, h264_sei_recovery_point_t *p_reco )
1004
1.68k
{
1005
1.68k
    p_reco->i_frames = bs_read_ue( p_bs );
1006
    //bool b_exact_match = bs_read( p_bs, 1 );
1007
    //bool b_broken_link = bs_read( p_bs, 1 );
1008
    //int i_changing_slice_group = bs_read( p_bs, 2 );
1009
1.68k
    return true;
1010
1.68k
}
1011
1012
bool h264_decode_sei_pic_timing(  bs_t *p_bs,
1013
                                  const h264_sequence_parameter_set_t *p_sps,
1014
                                  uint8_t *pic_struct, uint8_t *output_delay  )
1015
21.8k
{
1016
21.8k
    if( !p_sps->vui_parameters_present_flag )
1017
8.14k
        return false;
1018
1019
13.6k
    if( p_sps->vui.b_hrd_parameters_present_flag )
1020
7.96k
    {
1021
7.96k
        bs_skip( p_bs, p_sps->vui.i_cpb_removal_delay_length_minus1 + 1 );
1022
7.96k
        *output_delay =
1023
7.96k
            bs_read( p_bs, p_sps->vui.i_dpb_output_delay_length_minus1 + 1 );
1024
7.96k
    }
1025
1026
13.6k
    if( p_sps->vui.b_pic_struct_present_flag )
1027
7.64k
        *pic_struct = bs_read( p_bs, 4 );
1028
1029
    /* + unparsed remains */
1030
13.6k
    return true;
1031
21.8k
}