Coverage Report

Created: 2026-12-01 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/h264_slice.c
Line
Count
Source
1
/*****************************************************************************
2
 * h264_slice.c: h264 slice parser
3
 *****************************************************************************
4
 * Copyright (C) 2001-17 VLC authors and VideoLAN
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 <vlc_common.h>
25
#include <vlc_bits.h>
26
27
#include "h264_nal.h"
28
#include "h264_slice.h"
29
#include "hxxx_nal.h"
30
#include "hxxx_ep3b.h"
31
32
struct h264_slice_s
33
{
34
    int i_nal_type;
35
    int i_nal_ref_idc;
36
37
    int type;
38
    int i_pic_parameter_set_id;
39
    unsigned i_frame_num;
40
41
    int i_field_pic_flag;
42
    int i_bottom_field_flag;
43
44
    int i_idr_pic_id;
45
46
    int i_pic_order_cnt_type;
47
    int i_pic_order_cnt_lsb;
48
    int i_delta_pic_order_cnt_bottom;
49
50
    int i_delta_pic_order_cnt0;
51
    int i_delta_pic_order_cnt1;
52
53
    bool no_output_of_prior_pics_flag;
54
    bool has_mmco5;
55
};
56
57
enum h264_slice_type_e h264_get_slice_type( const h264_slice_t *p_slice )
58
676k
{
59
676k
    return p_slice->type;
60
676k
}
61
62
bool h264_has_mmco5( const h264_slice_t *p_slice )
63
0
{
64
0
    return p_slice->has_mmco5;
65
0
}
66
67
bool h264_is_field_pic( const h264_slice_t *p_slice )
68
0
{
69
0
    return p_slice->i_field_pic_flag;
70
0
}
71
72
int h264_get_slice_pps_id( const h264_slice_t *p_slice )
73
263k
{
74
263k
    return p_slice->i_pic_parameter_set_id;
75
263k
}
76
77
unsigned h264_get_frame_num( const h264_slice_t *p_slice )
78
203k
{
79
203k
    return p_slice->i_frame_num;
80
203k
}
81
82
unsigned h264_get_nal_ref_idc( const h264_slice_t *p_slice )
83
0
{
84
0
    return p_slice->i_nal_ref_idc;
85
0
}
86
87
void h264_slice_release( h264_slice_t *p_slice )
88
727k
{
89
727k
    free( p_slice );
90
727k
}
91
92
void h264_slice_copy_idr_id( const h264_slice_t *src, h264_slice_t *dst )
93
263k
{
94
263k
    if( !src || src->i_nal_type != H264_NAL_SLICE_IDR ) /* value only set on IDR */
95
227k
        return;
96
36.4k
    dst->i_idr_pic_id = src->i_idr_pic_id;
97
36.4k
}
98
99
h264_slice_t * h264_decode_slice( const uint8_t *p_buffer, size_t i_buffer,
100
                                  void (* get_sps_pps)(uint8_t, void *,
101
                                             const h264_sequence_parameter_set_t **,
102
                                             const h264_picture_parameter_set_t ** ),
103
                                  void *priv )
104
497k
{
105
497k
    h264_slice_t *p_slice = calloc( 1, sizeof(*p_slice) );
106
497k
    if( !p_slice )
107
0
        return NULL;
108
109
497k
    int i_slice_type;
110
497k
    bs_t s;
111
497k
    struct hxxx_bsfw_ep3b_ctx_s bsctx;
112
497k
    hxxx_bsfw_ep3b_ctx_init( &bsctx );
113
497k
    bs_init_custom( &s, p_buffer, i_buffer, &hxxx_bsfw_ep3b_callbacks, &bsctx );
114
115
    /* nal unit header */
116
497k
    bs_skip( &s, 1 );
117
497k
    const uint8_t i_nal_ref_idc = bs_read( &s, 2 );
118
497k
    const uint8_t i_nal_type = bs_read( &s, 5 );
119
120
    /* first_mb_in_slice */
121
    /* int i_first_mb = */ bs_read_ue( &s );
122
123
    /* slice_type */
124
497k
    i_slice_type = bs_read_ue( &s );
125
497k
    if( i_slice_type > 9 )
126
48.6k
        goto error;
127
448k
    p_slice->type = i_slice_type % 5;
128
129
    /* */
130
448k
    p_slice->i_nal_type = i_nal_type;
131
448k
    p_slice->i_nal_ref_idc = i_nal_ref_idc;
132
133
448k
    p_slice->i_pic_parameter_set_id = bs_read_ue( &s );
134
448k
    if( p_slice->i_pic_parameter_set_id > H264_PPS_ID_MAX )
135
16.8k
        goto error;
136
137
431k
    const h264_sequence_parameter_set_t *p_sps;
138
431k
    const h264_picture_parameter_set_t *p_pps;
139
140
    /* Bind matched/referred PPS and SPS */
141
431k
    get_sps_pps( p_slice->i_pic_parameter_set_id, priv, &p_sps, &p_pps );
142
431k
    if( !p_sps || !p_pps )
143
98.9k
        goto error;
144
145
332k
    p_slice->i_frame_num = bs_read( &s, p_sps->i_log2_max_frame_num + 4 );
146
147
332k
    if( !p_sps->frame_mbs_only_flag )
148
165k
    {
149
        /* field_pic_flag */
150
165k
        p_slice->i_field_pic_flag = bs_read( &s, 1 );
151
165k
        if( p_slice->i_field_pic_flag )
152
66.9k
            p_slice->i_bottom_field_flag = bs_read( &s, 1 );
153
165k
    }
154
155
332k
    if( p_slice->i_nal_type == H264_NAL_SLICE_IDR )
156
75.7k
    {
157
75.7k
        p_slice->i_idr_pic_id = bs_read_ue( &s );
158
75.7k
        if( p_slice->i_idr_pic_id > 65535 )
159
1.43k
            goto error;
160
75.7k
    }
161
162
331k
    p_slice->i_pic_order_cnt_type = p_sps->i_pic_order_cnt_type;
163
331k
    if( p_sps->i_pic_order_cnt_type == 0 )
164
201k
    {
165
201k
        p_slice->i_pic_order_cnt_lsb = bs_read( &s, p_sps->i_log2_max_pic_order_cnt_lsb + 4 );
166
201k
        if( p_pps->i_pic_order_present_flag && !p_slice->i_field_pic_flag )
167
112k
            p_slice->i_delta_pic_order_cnt_bottom = bs_read_se( &s );
168
201k
    }
169
129k
    else if( (p_sps->i_pic_order_cnt_type == 1) &&
170
95.0k
             (!p_sps->i_delta_pic_order_always_zero_flag) )
171
71.8k
    {
172
71.8k
        p_slice->i_delta_pic_order_cnt0 = bs_read_se( &s );
173
71.8k
        if( p_pps->i_pic_order_present_flag && !p_slice->i_field_pic_flag )
174
32.6k
            p_slice->i_delta_pic_order_cnt1 = bs_read_se( &s );
175
71.8k
    }
176
177
331k
    if( p_pps->i_redundant_pic_present_flag )
178
164k
        bs_read_ue( &s ); /* redudant_pic_count */
179
180
331k
    uint32_t num_ref_idx_l01_active_minus1[2] = { p_pps->num_ref_idx_l01_default_active_minus1[0],
181
331k
                                                  p_pps->num_ref_idx_l01_default_active_minus1[1] };
182
183
331k
    if( i_slice_type == 1 || i_slice_type == 6 ) /* B slices */
184
45.2k
        bs_skip( &s, 1 ); /* direct_spatial_mv_pred_flag */
185
331k
    if( i_slice_type == 0 || i_slice_type == 5 ||
186
126k
        i_slice_type == 3 || i_slice_type == 8 ||
187
122k
        i_slice_type == 1 || i_slice_type == 6 ) /* P SP B slices */
188
254k
    {
189
254k
        if( bs_read1( &s ) ) /* num_ref_idx_active_override_flag */
190
94.7k
        {
191
94.7k
            num_ref_idx_l01_active_minus1[0] = bs_read_ue( &s );
192
94.7k
            if (num_ref_idx_l01_active_minus1[0] > 31)
193
3.71k
                num_ref_idx_l01_active_minus1[0] = p_pps->num_ref_idx_l01_default_active_minus1[0];
194
94.7k
            if( i_slice_type == 1 || i_slice_type == 6 ) /* B slices */
195
16.1k
            {
196
16.1k
                num_ref_idx_l01_active_minus1[1] = bs_read_ue( &s );
197
16.1k
                if (num_ref_idx_l01_active_minus1[1] > 31)
198
2.57k
                    num_ref_idx_l01_active_minus1[1] = p_pps->num_ref_idx_l01_default_active_minus1[1];
199
16.1k
            }
200
94.7k
        }
201
254k
    }
202
203
    /* BELOW, Further processing up to assert MMCO 5 presence for POC */
204
331k
    if( p_slice->i_nal_type == 5 || p_slice->i_nal_ref_idc == 0 )
205
172k
    {
206
        /* Early END, don't waste parsing below */
207
172k
        p_slice->has_mmco5 = false;
208
172k
        return p_slice;
209
172k
    }
210
211
    /* ref_pic_list_[mvc_]modification() */
212
158k
    const bool b_mvc = (p_slice->i_nal_type == 20 || p_slice->i_nal_type == 21 );
213
158k
    unsigned i = 0;
214
158k
    if( i_slice_type % 5 != 2 && i_slice_type % 5 != 4 )
215
145k
        i++;
216
158k
    if( i_slice_type % 5 == 1 )
217
39.4k
        i++;
218
219
343k
    for( ; i>0; i-- )
220
185k
    {
221
185k
        if( bs_read1( &s ) ) /* ref_pic_list_modification_flag_l{0,1} */
222
62.8k
        {
223
62.8k
            uint32_t mod;
224
62.8k
            do
225
1.49M
            {
226
1.49M
                mod = bs_read_ue( &s );
227
1.49M
                if( mod < 3 || ( b_mvc && (mod == 4 || mod == 5) ) )
228
1.21M
                    bs_read_ue( &s ); /* abs_diff_pic_num_minus1, long_term_pic_num, abs_diff_view_idx_min1 */
229
1.49M
            }
230
1.49M
            while( mod != 3 && !bs_eof( &s ) );
231
62.8k
        }
232
185k
    }
233
234
158k
    if( bs_error( &s ) )
235
37.7k
        goto error;
236
237
    /* pred_weight_table() */
238
120k
    if( ( p_pps->weighted_pred_flag && ( i_slice_type == 0 || i_slice_type == 5 || /* P, SP */
239
19.3k
                                         i_slice_type == 3 || i_slice_type == 8 ) ) ||
240
74.7k
        ( p_pps->weighted_bipred_idc == 1 && ( i_slice_type == 1 || i_slice_type == 6 ) /* B */ ) )
241
56.6k
    {
242
56.6k
        bs_read_ue( &s ); /* luma_log2_weight_denom */
243
56.6k
        if( !p_sps->b_separate_colour_planes_flag ) /* ChromaArrayType != 0 */
244
55.4k
            bs_read_ue( &s ); /* chroma_log2_weight_denom */
245
246
56.6k
        const unsigned i_num_layers = ( i_slice_type % 5 == 1 ) ? 2 : 1;
247
123k
        for( unsigned j=0; j < i_num_layers; j++ )
248
67.1k
        {
249
344k
            for( unsigned k=0; k<=num_ref_idx_l01_active_minus1[j]; k++ )
250
277k
            {
251
277k
                if( bs_read1( &s ) ) /* luma_weight_l{0,1}_flag */
252
92.1k
                {
253
92.1k
                    bs_read_se( &s );
254
92.1k
                    bs_read_se( &s );
255
92.1k
                }
256
277k
                if( !p_sps->b_separate_colour_planes_flag ) /* ChromaArrayType != 0 */
257
271k
                {
258
271k
                    if( bs_read1( &s ) ) /* chroma_weight_l{0,1}_flag */
259
76.4k
                    {
260
76.4k
                        bs_read_se( &s );
261
76.4k
                        bs_read_se( &s );
262
76.4k
                        bs_read_se( &s );
263
76.4k
                        bs_read_se( &s );
264
76.4k
                    }
265
271k
                }
266
277k
            }
267
67.1k
        }
268
56.6k
    }
269
270
    /* dec_ref_pic_marking() */
271
120k
    if( p_slice->i_nal_type == 5 ) /* IdrFlag */
272
0
    {
273
0
        p_slice->no_output_of_prior_pics_flag = bs_read1( &s );
274
0
        bs_skip( &s, 1 ); /* long_term_reference_flag */
275
0
    }
276
120k
    else
277
120k
    {
278
120k
        if( bs_read1( &s ) ) /* adaptive_ref_pic_marking_mode_flag */
279
47.5k
        {
280
47.5k
            uint32_t mmco;
281
47.5k
            do
282
67.7k
            {
283
67.7k
                mmco = bs_read_ue( &s );
284
67.7k
                if( mmco > 6 )
285
11.4k
                    goto error;
286
56.2k
                if( mmco == 1 || mmco == 3 )
287
6.74k
                    bs_read_ue( &s ); /* diff_pics_minus1 */
288
56.2k
                if( mmco == 2 )
289
8.26k
                    bs_read_ue( &s ); /* long_term_pic_num */
290
56.2k
                if( mmco == 3 || mmco == 6 )
291
2.02k
                    bs_read_ue( &s ); /* long_term_frame_idx */
292
56.2k
                if( mmco == 4 )
293
4.27k
                    bs_read_ue( &s ); /* max_long_term_frame_idx_plus1 */
294
56.2k
                if( mmco == 5 )
295
1.71k
                {
296
1.71k
                    p_slice->has_mmco5 = true;
297
1.71k
                    break; /* Early END */
298
1.71k
                }
299
56.2k
            }
300
54.5k
            while( mmco > 0 );
301
47.5k
        }
302
120k
    }
303
304
    /* If you need to store anything else than MMCO presence above, care of "Early END" cases */
305
306
109k
    if(bs_error( &s ))
307
18.5k
        goto error;
308
90.7k
    return p_slice;
309
310
233k
error:
311
233k
    h264_slice_release( p_slice );
312
233k
    return NULL;
313
109k
}
314
315
316
void h264_compute_poc( const h264_sequence_parameter_set_t *p_sps,
317
                       const h264_slice_t *p_slice, h264_poc_context_t *p_ctx,
318
                       int *p_PictureOrderCount, int *p_tFOC, int *p_bFOC )
319
203k
{
320
203k
    *p_tFOC = *p_bFOC = 0;
321
322
203k
    if( p_sps->i_pic_order_cnt_type == 0 )
323
128k
    {
324
128k
        unsigned maxPocLSB = 1U << (p_sps->i_log2_max_pic_order_cnt_lsb  + 4);
325
326
        /* POC reference */
327
128k
        if( p_slice->i_nal_type == H264_NAL_SLICE_IDR )
328
43.6k
        {
329
43.6k
            p_ctx->prevPicOrderCnt.lsb = 0;
330
43.6k
            p_ctx->prevPicOrderCnt.msb = 0;
331
43.6k
        }
332
84.9k
        else if( p_ctx->prevRefPictureHasMMCO5 )
333
3.18k
        {
334
3.18k
            p_ctx->prevPicOrderCnt.msb = 0;
335
3.18k
            if( !p_ctx->prevRefPictureIsBottomField )
336
3.02k
                p_ctx->prevPicOrderCnt.lsb = p_ctx->prevRefPictureTFOC;
337
156
            else
338
156
                p_ctx->prevPicOrderCnt.lsb = 0;
339
3.18k
        }
340
341
        /* 8.2.1.1 */
342
128k
        int pocMSB = p_ctx->prevPicOrderCnt.msb;
343
128k
        int64_t orderDiff = p_slice->i_pic_order_cnt_lsb - p_ctx->prevPicOrderCnt.lsb;
344
128k
        if( orderDiff < 0 && -orderDiff >= maxPocLSB / 2 )
345
7.56k
            pocMSB += maxPocLSB;
346
121k
        else if( orderDiff > maxPocLSB / 2 )
347
51.5k
            pocMSB -= maxPocLSB;
348
349
128k
        *p_tFOC = *p_bFOC = pocMSB + p_slice->i_pic_order_cnt_lsb;
350
128k
        if( p_slice->i_field_pic_flag )
351
21.3k
            *p_bFOC += p_slice->i_delta_pic_order_cnt_bottom;
352
353
        /* Save from ref picture */
354
128k
        if( p_slice->i_nal_ref_idc /* Is reference */ )
355
58.9k
        {
356
58.9k
            p_ctx->prevRefPictureIsBottomField = (p_slice->i_field_pic_flag &&
357
9.20k
                                                  p_slice->i_bottom_field_flag);
358
58.9k
            p_ctx->prevRefPictureHasMMCO5 = p_slice->has_mmco5;
359
58.9k
            p_ctx->prevRefPictureTFOC = *p_tFOC;
360
58.9k
            p_ctx->prevPicOrderCnt.lsb = p_slice->i_pic_order_cnt_lsb;
361
58.9k
            p_ctx->prevPicOrderCnt.msb = pocMSB;
362
58.9k
        }
363
128k
    }
364
74.6k
    else
365
74.6k
    {
366
74.6k
        unsigned maxFrameNum = 1 << (p_sps->i_log2_max_frame_num + 4);
367
74.6k
        unsigned frameNumOffset;
368
74.6k
        unsigned expectedPicOrderCnt = 0;
369
370
74.6k
        if( p_slice->i_nal_type == H264_NAL_SLICE_IDR )
371
16.1k
            frameNumOffset = 0;
372
58.5k
        else if( p_ctx->prevFrameNum > p_slice->i_frame_num )
373
16.3k
            frameNumOffset = p_ctx->prevFrameNumOffset + maxFrameNum;
374
42.1k
        else
375
42.1k
            frameNumOffset = p_ctx->prevFrameNumOffset;
376
377
74.6k
        if( p_sps->i_pic_order_cnt_type == 1 )
378
56.3k
        {
379
56.3k
            unsigned absFrameNum;
380
381
56.3k
            if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 0 )
382
39.1k
                absFrameNum = frameNumOffset + p_slice->i_frame_num;
383
17.2k
            else
384
17.2k
                absFrameNum = 0;
385
386
56.3k
            if( p_slice->i_nal_ref_idc == 0 && absFrameNum > 0 )
387
13.6k
                absFrameNum--;
388
389
56.3k
            if( absFrameNum > 0 )
390
38.9k
            {
391
38.9k
                int32_t expectedDeltaPerPicOrderCntCycle = 0;
392
385k
                for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ )
393
346k
                    expectedDeltaPerPicOrderCntCycle += p_sps->offset_for_ref_frame[i];
394
395
38.9k
                unsigned picOrderCntCycleCnt = 0;
396
38.9k
                unsigned frameNumInPicOrderCntCycle = 0;
397
38.9k
                if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle )
398
38.9k
                {
399
38.9k
                    picOrderCntCycleCnt = ( absFrameNum - 1 ) / p_sps->i_num_ref_frames_in_pic_order_cnt_cycle;
400
38.9k
                    frameNumInPicOrderCntCycle = ( absFrameNum - 1 ) % p_sps->i_num_ref_frames_in_pic_order_cnt_cycle;
401
38.9k
                }
402
403
38.9k
                expectedPicOrderCnt = picOrderCntCycleCnt * expectedDeltaPerPicOrderCntCycle;
404
237k
                for( unsigned i=0; i <= frameNumInPicOrderCntCycle; i++ )
405
198k
                    expectedPicOrderCnt = expectedPicOrderCnt + p_sps->offset_for_ref_frame[i];
406
38.9k
            }
407
408
56.3k
            if( p_slice->i_nal_ref_idc == 0 )
409
17.8k
                expectedPicOrderCnt = expectedPicOrderCnt + p_sps->offset_for_non_ref_pic;
410
411
56.3k
            *p_tFOC = expectedPicOrderCnt + p_slice->i_delta_pic_order_cnt0;
412
56.3k
            if( !p_slice->i_field_pic_flag )
413
45.3k
                *p_bFOC = *p_tFOC + p_sps->offset_for_top_to_bottom_field + p_slice->i_delta_pic_order_cnt1;
414
10.9k
            else if( p_slice->i_bottom_field_flag )
415
6.16k
                *p_bFOC = expectedPicOrderCnt + p_sps->offset_for_top_to_bottom_field + p_slice->i_delta_pic_order_cnt0;
416
56.3k
        }
417
18.2k
        else if( p_sps->i_pic_order_cnt_type == 2 )
418
16.2k
        {
419
16.2k
            unsigned tempPicOrderCnt;
420
421
16.2k
            if( p_slice->i_nal_type == H264_NAL_SLICE_IDR )
422
4.08k
                tempPicOrderCnt = 0;
423
12.1k
            else if( p_slice->i_nal_ref_idc == 0 )
424
4.40k
                tempPicOrderCnt = 2 * ( frameNumOffset + p_slice->i_frame_num ) - 1;
425
7.79k
            else
426
7.79k
                tempPicOrderCnt = 2 * ( frameNumOffset + p_slice->i_frame_num );
427
428
16.2k
            *p_bFOC = *p_tFOC = tempPicOrderCnt;
429
16.2k
        }
430
431
74.6k
        p_ctx->prevFrameNum = p_slice->i_frame_num;
432
74.6k
        if( p_slice->has_mmco5 )
433
979
            p_ctx->prevFrameNumOffset = 0;
434
73.6k
        else
435
73.6k
            p_ctx->prevFrameNumOffset = frameNumOffset;
436
74.6k
    }
437
438
    /* 8.2.1 (8-1) */
439
203k
    if( !p_slice->i_field_pic_flag ) /* progressive or contains both fields */
440
162k
        *p_PictureOrderCount = __MIN( *p_bFOC, *p_tFOC );
441
40.5k
    else /* split top or bottom field */
442
40.5k
    if ( p_slice->i_bottom_field_flag )
443
22.5k
        *p_PictureOrderCount = *p_bFOC;
444
17.9k
    else
445
17.9k
        *p_PictureOrderCount = *p_tFOC;
446
203k
}
447
448
static uint8_t h264_infer_pic_struct( const h264_sequence_parameter_set_t *p_sps,
449
                                      const h264_slice_t *p_slice,
450
                                      uint8_t i_pic_struct, int tFOC, int bFOC )
451
203k
{
452
    /* See D-1 and note 6 */
453
203k
    if( !p_sps->vui.b_pic_struct_present_flag || i_pic_struct >= 9 )
454
195k
    {
455
195k
        if( p_slice->i_field_pic_flag )
456
36.4k
            i_pic_struct = 1 + p_slice->i_bottom_field_flag;
457
159k
        else if( tFOC == bFOC )
458
126k
            i_pic_struct = 0;
459
33.1k
        else if( tFOC < bFOC )
460
17.8k
            i_pic_struct = 3;
461
15.2k
        else
462
15.2k
            i_pic_struct = 4;
463
195k
    }
464
465
203k
    return i_pic_struct;
466
203k
}
467
468
uint8_t h264_get_num_ts( const h264_sequence_parameter_set_t *p_sps,
469
                         const h264_slice_t *p_slice, uint8_t i_pic_struct,
470
                         int tFOC, int bFOC )
471
203k
{
472
203k
    i_pic_struct = h264_infer_pic_struct( p_sps, p_slice, i_pic_struct, tFOC, bFOC );
473
    /* !WARN modified with nuit field based multiplier for values 0, 7 and 8 */
474
203k
    const uint8_t rgi_numclock[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 };
475
203k
    return rgi_numclock[ i_pic_struct ];
476
203k
}
477
478
bool h264_slice_top_field( const h264_slice_t *p_slice )
479
2.37k
{
480
2.37k
    return !p_slice->i_bottom_field_flag;
481
2.37k
}
482
483
bool h264_IsFirstVCLNALUnit( const h264_slice_t *p_prev, const h264_slice_t *p_cur )
484
263k
{
485
    /* Detection of the first VCL NAL unit of a primary coded picture
486
     * (cf. 7.4.1.2.4) */
487
263k
    if( !p_prev )
488
142k
        return true;
489
121k
    if( p_cur->i_frame_num != p_prev->i_frame_num ||
490
61.7k
        p_cur->i_pic_parameter_set_id != p_prev->i_pic_parameter_set_id ||
491
60.4k
        p_cur->i_field_pic_flag != p_prev->i_field_pic_flag ||
492
58.5k
        !p_cur->i_nal_ref_idc != !p_prev->i_nal_ref_idc )
493
63.4k
        return true;
494
57.9k
    if( p_cur->i_field_pic_flag && /* present in both and differs in value */
495
13.5k
        p_cur->i_bottom_field_flag != p_prev->i_bottom_field_flag )
496
2.36k
        return true;
497
55.5k
    if( p_cur->i_pic_order_cnt_type == p_prev->i_pic_order_cnt_type )
498
55.5k
    {
499
55.5k
        if( p_cur->i_pic_order_cnt_type == 0 &&
500
32.2k
            ( p_cur->i_pic_order_cnt_lsb != p_prev->i_pic_order_cnt_lsb ||
501
21.1k
             p_cur->i_delta_pic_order_cnt_bottom != p_prev->i_delta_pic_order_cnt_bottom ) )
502
16.1k
            return true;
503
39.3k
        else if( p_cur->i_pic_order_cnt_type == 1 &&
504
18.9k
                 ( p_cur->i_delta_pic_order_cnt0 != p_prev->i_delta_pic_order_cnt0 ||
505
15.6k
                  p_cur->i_delta_pic_order_cnt1 != p_prev->i_delta_pic_order_cnt1 ) )
506
4.06k
            return true;
507
55.5k
    }
508
35.3k
    if( ( p_cur->i_nal_type == H264_NAL_SLICE_IDR || p_prev->i_nal_type == H264_NAL_SLICE_IDR ) &&
509
9.81k
        ( p_cur->i_nal_type != p_prev->i_nal_type || p_cur->i_idr_pic_id != p_prev->i_idr_pic_id ) )
510
1.90k
        return true;
511
33.4k
    return false;
512
35.3k
}
513
514
bool h264_CanSwapPTSWithDTS( const h264_slice_t *p_slice, const h264_sequence_parameter_set_t *p_sps )
515
117k
{
516
117k
    if( p_slice->i_nal_ref_idc == 0 && p_slice->type == H264_SLICE_TYPE_B )
517
2.79k
        return true;
518
114k
    else if( p_sps->vui_parameters_present_flag )
519
73.6k
        return p_sps->vui.i_max_num_reorder_frames == 0;
520
41.1k
    else
521
41.1k
        return p_sps->i_profile == PROFILE_H264_CAVLC_INTRA;
522
117k
}