GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/av1_parser.c Lines: 78 106 73.6 %
Date: 2019-11-20 04:07:19 Branches: 26 48 54.2 %

Line Branch Exec Source
1
/*
2
 * AV1 parser
3
 *
4
 * Copyright (C) 2018 James Almer <jamrial@gmail.com>
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 "av1_parse.h"
24
#include "cbs.h"
25
#include "cbs_av1.h"
26
#include "parser.h"
27
28
typedef struct AV1ParseContext {
29
    CodedBitstreamContext *cbc;
30
    CodedBitstreamFragment temporal_unit;
31
    int parsed_extradata;
32
} AV1ParseContext;
33
34
static const enum AVPixelFormat pix_fmts_8bit[2][2] = {
35
    { AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE },
36
    { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P },
37
};
38
static const enum AVPixelFormat pix_fmts_10bit[2][2] = {
39
    { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_NONE },
40
    { AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10 },
41
};
42
static const enum AVPixelFormat pix_fmts_12bit[2][2] = {
43
    { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_NONE },
44
    { AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12 },
45
};
46
47
258
static int av1_parser_parse(AVCodecParserContext *ctx,
48
                            AVCodecContext *avctx,
49
                            const uint8_t **out_data, int *out_size,
50
                            const uint8_t *data, int size)
51
{
52
258
    AV1ParseContext *s = ctx->priv_data;
53
258
    CodedBitstreamFragment *td = &s->temporal_unit;
54
258
    CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
55
    int ret;
56
57
258
    *out_data = data;
58
258
    *out_size = size;
59
60
258
    ctx->key_frame         = -1;
61
258
    ctx->pict_type         = AV_PICTURE_TYPE_NONE;
62
258
    ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
63
64
258
    s->cbc->log_ctx = avctx;
65
66

258
    if (avctx->extradata_size && !s->parsed_extradata) {
67
11
        s->parsed_extradata = 1;
68
69
11
        ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
70
11
        if (ret < 0) {
71
            av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
72
        }
73
74
11
        ff_cbs_fragment_reset(s->cbc, td);
75
    }
76
77
258
    ret = ff_cbs_read(s->cbc, td, data, size);
78
258
    if (ret < 0) {
79
        av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
80
        goto end;
81
    }
82
83
258
    if (!av1->sequence_header) {
84
        av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
85
        goto end;
86
    }
87
88
939
    for (int i = 0; i < td->nb_units; i++) {
89
681
        CodedBitstreamUnit *unit = &td->units[i];
90
681
        AV1RawOBU *obu = unit->content;
91
681
        AV1RawSequenceHeader *seq = av1->sequence_header;
92
681
        AV1RawColorConfig *color = &seq->color_config;
93
        AV1RawFrameHeader *frame;
94
        int frame_type;
95
96
681
        if (unit->type == AV1_OBU_FRAME)
97
265
            frame = &obu->obu.frame.header;
98
416
        else if (unit->type == AV1_OBU_FRAME_HEADER)
99
40
            frame = &obu->obu.frame_header;
100
        else
101
376
            continue;
102
103
305
        if (frame->show_existing_frame) {
104
40
            AV1ReferenceFrameState *ref = &av1->ref[frame->frame_to_show_map_idx];
105
106
40
            if (!ref->valid) {
107
                av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
108
                goto end;
109
            }
110
111
40
            ctx->width  = ref->frame_width;
112
40
            ctx->height = ref->frame_height;
113
40
            frame_type  = ref->frame_type;
114
115
40
            ctx->key_frame = 0;
116
265
        } else if (!frame->show_frame) {
117
58
            continue;
118
        } else {
119
207
            ctx->width  = av1->frame_width;
120
207
            ctx->height = av1->frame_height;
121
207
            frame_type  = frame->frame_type;
122
123
207
            ctx->key_frame = frame_type == AV1_FRAME_KEY;
124
        }
125
126
247
        avctx->profile = seq->seq_profile;
127
247
        avctx->level   = seq->seq_level_idx[0];
128
129

247
        switch (frame_type) {
130
52
        case AV1_FRAME_KEY:
131
        case AV1_FRAME_INTRA_ONLY:
132
52
            ctx->pict_type = AV_PICTURE_TYPE_I;
133
52
            break;
134
195
        case AV1_FRAME_INTER:
135
195
            ctx->pict_type = AV_PICTURE_TYPE_P;
136
195
            break;
137
        case AV1_FRAME_SWITCH:
138
            ctx->pict_type = AV_PICTURE_TYPE_SP;
139
            break;
140
        }
141
247
        ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
142
143

247
        switch (av1->bit_depth) {
144
237
        case 8:
145
474
            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
146
237
                                             : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
147
237
            break;
148
10
        case 10:
149
20
            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
150
10
                                             : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
151
10
            break;
152
        case 12:
153
            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
154
                                             : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
155
            break;
156
        }
157
681
        av_assert2(ctx->format != AV_PIX_FMT_NONE);
158
    }
159
160
258
    if (avctx->framerate.num)
161
        avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
162
163
258
end:
164
258
    ff_cbs_fragment_reset(s->cbc, td);
165
166
258
    s->cbc->log_ctx = NULL;
167
168
258
    return size;
169
}
170
171
static const CodedBitstreamUnitType decompose_unit_types[] = {
172
    AV1_OBU_TEMPORAL_DELIMITER,
173
    AV1_OBU_SEQUENCE_HEADER,
174
    AV1_OBU_FRAME_HEADER,
175
    AV1_OBU_TILE_GROUP,
176
    AV1_OBU_FRAME,
177
};
178
179
11
static av_cold int av1_parser_init(AVCodecParserContext *ctx)
180
{
181
11
    AV1ParseContext *s = ctx->priv_data;
182
    int ret;
183
184
11
    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
185
11
    if (ret < 0)
186
        return ret;
187
188
11
    s->cbc->decompose_unit_types    = (CodedBitstreamUnitType *)decompose_unit_types;
189
11
    s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
190
191
11
    return 0;
192
}
193
194
11
static void av1_parser_close(AVCodecParserContext *ctx)
195
{
196
11
    AV1ParseContext *s = ctx->priv_data;
197
198
11
    ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
199
11
    ff_cbs_close(&s->cbc);
200
11
}
201
202
static int av1_parser_split(AVCodecContext *avctx,
203
                            const uint8_t *buf, int buf_size)
204
{
205
    AV1OBU obu;
206
    const uint8_t *ptr = buf, *end = buf + buf_size;
207
208
    while (ptr < end) {
209
        int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
210
        if (len < 0)
211
            break;
212
213
        if (obu.type == AV1_OBU_FRAME_HEADER ||
214
            obu.type == AV1_OBU_FRAME) {
215
            return ptr - buf;
216
        }
217
        ptr      += len;
218
        buf_size -= len;
219
    }
220
221
    return 0;
222
}
223
224
AVCodecParser ff_av1_parser = {
225
    .codec_ids      = { AV_CODEC_ID_AV1 },
226
    .priv_data_size = sizeof(AV1ParseContext),
227
    .parser_init    = av1_parser_init,
228
    .parser_close   = av1_parser_close,
229
    .parser_parse   = av1_parser_parse,
230
    .split          = av1_parser_split,
231
};