GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/av1_parser.c Lines: 91 116 78.4 %
Date: 2020-04-02 05:41:20 Branches: 35 58 60.3 %

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

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

349
        switch (frame_type) {
134
61
        case AV1_FRAME_KEY:
135
        case AV1_FRAME_INTRA_ONLY:
136
61
            ctx->pict_type = AV_PICTURE_TYPE_I;
137
61
            break;
138
287
        case AV1_FRAME_INTER:
139
287
            ctx->pict_type = AV_PICTURE_TYPE_P;
140
287
            break;
141
1
        case AV1_FRAME_SWITCH:
142
1
            ctx->pict_type = AV_PICTURE_TYPE_SP;
143
1
            break;
144
        }
145
349
        ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
146
147

349
        switch (av1->bit_depth) {
148
339
        case 8:
149
678
            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
150
339
                                             : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
151
339
            break;
152
10
        case 10:
153
20
            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
154
10
                                             : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
155
10
            break;
156
        case 12:
157
            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
158
                                             : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
159
            break;
160
        }
161
349
        av_assert2(ctx->format != AV_PIX_FMT_NONE);
162
163
349
        avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
164
349
        avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
165
349
        avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
166
349
        avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
167
168

349
        if (ctx->width != avctx->width || ctx->height != avctx->height) {
169
4
            ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
170
4
            if (ret < 0)
171
                goto end;
172
        }
173
    }
174
175
370
    if (avctx->framerate.num)
176
12
        avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
177
178
358
end:
179
370
    ff_cbs_fragment_reset(s->cbc, td);
180
181
370
    s->cbc->log_ctx = NULL;
182
183
370
    return size;
184
}
185
186
static const CodedBitstreamUnitType decompose_unit_types[] = {
187
    AV1_OBU_TEMPORAL_DELIMITER,
188
    AV1_OBU_SEQUENCE_HEADER,
189
    AV1_OBU_FRAME_HEADER,
190
    AV1_OBU_TILE_GROUP,
191
    AV1_OBU_FRAME,
192
};
193
194
22
static av_cold int av1_parser_init(AVCodecParserContext *ctx)
195
{
196
22
    AV1ParseContext *s = ctx->priv_data;
197
    int ret;
198
199
22
    ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
200
22
    if (ret < 0)
201
        return ret;
202
203
22
    s->cbc->decompose_unit_types    = (CodedBitstreamUnitType *)decompose_unit_types;
204
22
    s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
205
206
22
    return 0;
207
}
208
209
22
static void av1_parser_close(AVCodecParserContext *ctx)
210
{
211
22
    AV1ParseContext *s = ctx->priv_data;
212
213
22
    ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
214
22
    ff_cbs_close(&s->cbc);
215
22
}
216
217
static int av1_parser_split(AVCodecContext *avctx,
218
                            const uint8_t *buf, int buf_size)
219
{
220
    AV1OBU obu;
221
    const uint8_t *ptr = buf, *end = buf + buf_size;
222
223
    while (ptr < end) {
224
        int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
225
        if (len < 0)
226
            break;
227
228
        if (obu.type == AV1_OBU_FRAME_HEADER ||
229
            obu.type == AV1_OBU_FRAME) {
230
            return ptr - buf;
231
        }
232
        ptr      += len;
233
        buf_size -= len;
234
    }
235
236
    return 0;
237
}
238
239
AVCodecParser ff_av1_parser = {
240
    .codec_ids      = { AV_CODEC_ID_AV1 },
241
    .priv_data_size = sizeof(AV1ParseContext),
242
    .parser_init    = av1_parser_init,
243
    .parser_close   = av1_parser_close,
244
    .parser_parse   = av1_parser_parse,
245
    .split          = av1_parser_split,
246
};