GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/hevc_parser.c Lines: 149 193 77.2 %
Date: 2020-11-28 20:53:16 Branches: 96 148 64.9 %

Line Branch Exec Source
1
/*
2
 * HEVC Annex B format parser
3
 *
4
 * Copyright (C) 2012 - 2013 Guillaume Martres
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#include "libavutil/common.h"
24
25
#include "golomb.h"
26
#include "hevc.h"
27
#include "hevc_parse.h"
28
#include "hevc_ps.h"
29
#include "hevc_sei.h"
30
#include "h2645_parse.h"
31
#include "internal.h"
32
#include "parser.h"
33
34
#define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
35
36
#define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
37
#define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
38
39
typedef struct HEVCParserContext {
40
    ParseContext pc;
41
42
    H2645Packet pkt;
43
    HEVCParamSets ps;
44
    HEVCSEI sei;
45
46
    int is_avc;
47
    int nal_length_size;
48
    int parsed_extradata;
49
50
    int poc;
51
    int pocTid0;
52
} HEVCParserContext;
53
54
11469
static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
55
                                   AVCodecContext *avctx)
56
{
57
11469
    HEVCParserContext *ctx = s->priv_data;
58
11469
    HEVCParamSets *ps = &ctx->ps;
59
11469
    HEVCSEI *sei = &ctx->sei;
60
11469
    GetBitContext *gb = &nal->gb;
61
    const HEVCWindow *ow;
62
11469
    int i, num = 0, den = 0;
63
64
    unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
65
    enum HEVCSliceType slice_type;
66
67
11469
    first_slice_in_pic_flag = get_bits1(gb);
68
11469
    s->picture_structure = sei->picture_timing.picture_struct;
69
11469
    s->field_order = sei->picture_timing.picture_struct;
70
71

11469
    if (IS_IRAP_NAL(nal)) {
72
380
        s->key_frame = 1;
73
380
        skip_bits1(gb); // no_output_of_prior_pics_flag
74
    }
75
76
11469
    pps_id = get_ue_golomb(gb);
77

11469
    if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
78
22
        av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
79
22
        return AVERROR_INVALIDDATA;
80
    }
81
11447
    ps->pps = (HEVCPPS*)ps->pps_list[pps_id]->data;
82
83

11447
    if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
84
        av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
85
        return AVERROR_INVALIDDATA;
86
    }
87
11447
    if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
88
198
        ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
89
198
        ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
90
    }
91
11447
    ow  = &ps->sps->output_window;
92
93
11447
    s->coded_width  = ps->sps->width;
94
11447
    s->coded_height = ps->sps->height;
95
11447
    s->width        = ps->sps->width  - ow->left_offset - ow->right_offset;
96
11447
    s->height       = ps->sps->height - ow->top_offset  - ow->bottom_offset;
97
11447
    s->format       = ps->sps->pix_fmt;
98
11447
    avctx->profile  = ps->sps->ptl.general_ptl.profile_idc;
99
11447
    avctx->level    = ps->sps->ptl.general_ptl.level_idc;
100
101
11447
    if (ps->vps->vps_timing_info_present_flag) {
102
355
        num = ps->vps->vps_num_units_in_tick;
103
355
        den = ps->vps->vps_time_scale;
104
11092
    } else if (ps->sps->vui.vui_timing_info_present_flag) {
105
307
        num = ps->sps->vui.vui_num_units_in_tick;
106
307
        den = ps->sps->vui.vui_time_scale;
107
    }
108
109

11447
    if (num != 0 && den != 0)
110
662
        av_reduce(&avctx->framerate.den, &avctx->framerate.num,
111
                  num, den, 1 << 30);
112
113
11447
    if (!first_slice_in_pic_flag) {
114
        unsigned int slice_segment_addr;
115
        int slice_address_length;
116
117
        if (ps->pps->dependent_slice_segments_enabled_flag)
118
            dependent_slice_segment_flag = get_bits1(gb);
119
        else
120
            dependent_slice_segment_flag = 0;
121
122
        slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
123
                                              ps->sps->ctb_height);
124
        slice_segment_addr = get_bitsz(gb, slice_address_length);
125
        if (slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
126
            av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
127
                   slice_segment_addr);
128
            return AVERROR_INVALIDDATA;
129
        }
130
    } else
131
11447
        dependent_slice_segment_flag = 0;
132
133
11447
    if (dependent_slice_segment_flag)
134
        return 0; /* break; */
135
136
11450
    for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
137
3
        skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
138
139
11447
    slice_type = get_ue_golomb_31(gb);
140

11447
    if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P ||
141
          slice_type == HEVC_SLICE_B)) {
142
        av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
143
               slice_type);
144
        return AVERROR_INVALIDDATA;
145
    }
146
14563
    s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
147
3116
                   slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
148
                                                AV_PICTURE_TYPE_I;
149
150
11447
    if (ps->pps->output_flag_present_flag)
151
700
        skip_bits1(gb); // pic_output_flag
152
153
11447
    if (ps->sps->separate_colour_plane_flag)
154
        skip_bits(gb, 2);   // colour_plane_id
155
156

11447
    if (!IS_IDR_NAL(nal)) {
157
11246
        int pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
158
11246
        s->output_picture_number = ctx->poc =
159
11246
            ff_hevc_compute_poc(ps->sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
160
    } else
161
201
        s->output_picture_number = ctx->poc = 0;
162
163
11447
    if (nal->temporal_id == 0 &&
164
11084
        nal->type != HEVC_NAL_TRAIL_N &&
165
8140
        nal->type != HEVC_NAL_TSA_N &&
166
8140
        nal->type != HEVC_NAL_STSA_N &&
167
8140
        nal->type != HEVC_NAL_RADL_N &&
168
8108
        nal->type != HEVC_NAL_RASL_N &&
169
7699
        nal->type != HEVC_NAL_RADL_R &&
170
7671
        nal->type != HEVC_NAL_RASL_R)
171
7059
        ctx->pocTid0 = ctx->poc;
172
173
11447
    return 1; /* no need to evaluate the rest */
174
}
175
176
/**
177
 * Parse NAL units of found picture and decode some basic information.
178
 *
179
 * @param s parser context.
180
 * @param avctx codec context.
181
 * @param buf buffer with field/frame data.
182
 * @param buf_size size of the buffer.
183
 */
184
11470
static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
185
                           int buf_size, AVCodecContext *avctx)
186
{
187
11470
    HEVCParserContext *ctx = s->priv_data;
188
11470
    HEVCParamSets *ps = &ctx->ps;
189
11470
    HEVCSEI *sei = &ctx->sei;
190
    int ret, i;
191
192
    /* set some sane default values */
193
11470
    s->pict_type         = AV_PICTURE_TYPE_I;
194
11470
    s->key_frame         = 0;
195
11470
    s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
196
197
11470
    ff_hevc_reset_sei(sei);
198
199
11470
    ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, ctx->is_avc,
200
                                ctx->nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
201
11470
    if (ret < 0)
202
        return ret;
203
204
14727
    for (i = 0; i < ctx->pkt.nb_nals; i++) {
205
14726
        H2645NAL *nal = &ctx->pkt.nals[i];
206
14726
        GetBitContext *gb = &nal->gb;
207
208
14726
        if (nal->nuh_layer_id > 0)
209
            continue;
210
211

14726
        switch (nal->type) {
212
226
        case HEVC_NAL_VPS:
213
226
            ff_hevc_decode_nal_vps(gb, avctx, ps);
214
226
            break;
215
225
        case HEVC_NAL_SPS:
216
225
            ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
217
225
            break;
218
1348
        case HEVC_NAL_PPS:
219
1348
            ff_hevc_decode_nal_pps(gb, avctx, ps);
220
1348
            break;
221
813
        case HEVC_NAL_SEI_PREFIX:
222
        case HEVC_NAL_SEI_SUFFIX:
223
813
            ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
224
813
            break;
225
11469
        case HEVC_NAL_TRAIL_N:
226
        case HEVC_NAL_TRAIL_R:
227
        case HEVC_NAL_TSA_N:
228
        case HEVC_NAL_TSA_R:
229
        case HEVC_NAL_STSA_N:
230
        case HEVC_NAL_STSA_R:
231
        case HEVC_NAL_BLA_W_LP:
232
        case HEVC_NAL_BLA_W_RADL:
233
        case HEVC_NAL_BLA_N_LP:
234
        case HEVC_NAL_IDR_W_RADL:
235
        case HEVC_NAL_IDR_N_LP:
236
        case HEVC_NAL_CRA_NUT:
237
        case HEVC_NAL_RADL_N:
238
        case HEVC_NAL_RADL_R:
239
        case HEVC_NAL_RASL_N:
240
        case HEVC_NAL_RASL_R:
241
11469
            if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
242
                s->repeat_pict = 1;
243
11469
            } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
244
                s->repeat_pict = 2;
245
            }
246
11469
            ret = hevc_parse_slice_header(s, nal, avctx);
247
11469
            if (ret)
248
11469
                return ret;
249
            break;
250
        }
251
3257
    }
252
    /* didn't find a picture! */
253
1
    av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
254
1
    return -1;
255
}
256
257
/**
258
 * Find the end of the current frame in the bitstream.
259
 * @return the position of the first byte of the next frame, or END_NOT_FOUND
260
 */
261
88440
static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
262
                               int buf_size)
263
{
264
88440
    HEVCParserContext *ctx = s->priv_data;
265
88440
    ParseContext       *pc = &ctx->pc;
266
    int i;
267
268
78780442
    for (i = 0; i < buf_size; i++) {
269
        int nut;
270
271
78703282
        pc->state64 = (pc->state64 << 8) | buf[i];
272
273
78703282
        if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
274
78647342
            continue;
275
276
55940
        nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
277
        // Beginning of access unit
278


55940
        if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
279

51044
            (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
280
4896
            if (pc->frame_start_found) {
281
1640
                pc->frame_start_found = 0;
282
1640
                return i - 5;
283
            }
284

51044
        } else if (nut <= HEVC_NAL_RASL_R ||
285
9214
                   (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
286
42853
            int first_slice_segment_in_pic_flag = buf[i] >> 7;
287
42853
            if (first_slice_segment_in_pic_flag) {
288
21109
                if (!pc->frame_start_found) {
289
11469
                    pc->frame_start_found = 1;
290
                } else { // First slice of next frame found
291
9640
                    pc->frame_start_found = 0;
292
9640
                    return i - 5;
293
                }
294
            }
295
        }
296
    }
297
298
77160
    return END_NOT_FOUND;
299
}
300
301
88440
static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
302
                      const uint8_t **poutbuf, int *poutbuf_size,
303
                      const uint8_t *buf, int buf_size)
304
{
305
    int next;
306
88440
    HEVCParserContext *ctx = s->priv_data;
307
88440
    ParseContext *pc = &ctx->pc;
308
88440
    int is_dummy_buf = !buf_size;
309
88440
    const uint8_t *dummy_buf = buf;
310
311

88440
    if (avctx->extradata && !ctx->parsed_extradata) {
312
179
        ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
313
                                 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
314
                                 1, avctx);
315
179
        ctx->parsed_extradata = 1;
316
    }
317
318
88440
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
319
        next = buf_size;
320
    } else {
321
88440
        next = hevc_find_frame_end(s, buf, buf_size);
322
88440
        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
323
76780
            *poutbuf      = NULL;
324
76780
            *poutbuf_size = 0;
325
76780
            return buf_size;
326
        }
327
    }
328
329
11660
    is_dummy_buf &= (dummy_buf == buf);
330
331
11660
    if (!is_dummy_buf)
332
11470
        parse_nal_units(s, buf, buf_size, avctx);
333
334
11660
    *poutbuf      = buf;
335
11660
    *poutbuf_size = buf_size;
336
11660
    return next;
337
}
338
339
// Split after the parameter sets at the beginning of the stream if they exist.
340
static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
341
{
342
    const uint8_t *ptr = buf, *end = buf + buf_size;
343
    uint32_t state = -1;
344
    int has_vps = 0;
345
    int has_sps = 0;
346
    int has_pps = 0;
347
    int nut;
348
349
    while (ptr < end) {
350
        ptr = avpriv_find_start_code(ptr, end, &state);
351
        if ((state >> 8) != START_CODE)
352
            break;
353
        nut = (state >> 1) & 0x3F;
354
        if (nut == HEVC_NAL_VPS)
355
            has_vps = 1;
356
        else if (nut == HEVC_NAL_SPS)
357
            has_sps = 1;
358
        else if (nut == HEVC_NAL_PPS)
359
            has_pps = 1;
360
        else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
361
                  nut != HEVC_NAL_AUD) {
362
            if (has_vps && has_sps) {
363
                while (ptr - 4 > buf && ptr[-5] == 0)
364
                    ptr--;
365
                return ptr - 4 - buf;
366
            }
367
        }
368
    }
369
    return 0;
370
}
371
372
195
static void hevc_parser_close(AVCodecParserContext *s)
373
{
374
195
    HEVCParserContext *ctx = s->priv_data;
375
376
195
    ff_hevc_ps_uninit(&ctx->ps);
377
195
    ff_h2645_packet_uninit(&ctx->pkt);
378
195
    ff_hevc_reset_sei(&ctx->sei);
379
380
195
    av_freep(&ctx->pc.buffer);
381
195
}
382
383
AVCodecParser ff_hevc_parser = {
384
    .codec_ids      = { AV_CODEC_ID_HEVC },
385
    .priv_data_size = sizeof(HEVCParserContext),
386
    .parser_parse   = hevc_parse,
387
    .parser_close   = hevc_parser_close,
388
    .split          = hevc_split,
389
};