FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1_parser.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 80 94 85.1%
Branches: 34 56 60.7%

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 "libavutil/avassert.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 static const enum AVPixelFormat pix_fmts_rgb[3] = {
49 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
50 };
51
52 375 static int av1_parser_parse(AVCodecParserContext *ctx,
53 AVCodecContext *avctx,
54 const uint8_t **out_data, int *out_size,
55 const uint8_t *data, int size)
56 {
57 375 AV1ParseContext *s = ctx->priv_data;
58 375 CodedBitstreamFragment *td = &s->temporal_unit;
59 375 const CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
60 const AV1RawSequenceHeader *seq;
61 const AV1RawColorConfig *color;
62 int ret;
63
64 375 *out_data = data;
65 375 *out_size = size;
66
67 375 ctx->key_frame = -1;
68 375 ctx->pict_type = AV_PICTURE_TYPE_NONE;
69 375 ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
70
71 375 s->cbc->log_ctx = avctx;
72
73
4/4
✓ Branch 0 taken 356 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 334 times.
375 if (avctx->extradata_size && !s->parsed_extradata) {
74 22 s->parsed_extradata = 1;
75
76 22 ret = ff_cbs_read_extradata_from_codec(s->cbc, td, avctx);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0) {
78 av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
79 }
80
81 22 ff_cbs_fragment_reset(td);
82 }
83
84 375 ret = ff_cbs_read(s->cbc, td, data, size);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
375 if (ret < 0) {
86 av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
87 goto end;
88 }
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
375 if (!av1->sequence_header) {
91 av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
92 goto end;
93 }
94
95 375 seq = av1->sequence_header;
96 375 color = &seq->color_config;
97
98
2/2
✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 375 times.
1404 for (int i = 0; i < td->nb_units; i++) {
99 1029 const CodedBitstreamUnit *unit = &td->units[i];
100 1029 const AV1RawOBU *obu = unit->content;
101 const AV1RawFrameHeader *frame;
102
103
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 679 times.
1029 if (unit->type == AV1_OBU_FRAME)
104 350 frame = &obu->obu.frame.header;
105
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 588 times.
679 else if (unit->type == AV1_OBU_FRAME_HEADER)
106 91 frame = &obu->obu.frame_header;
107 else
108 588 continue;
109
110
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 425 times.
441 if (obu->header.spatial_id > 0)
111 16 continue;
112
113
4/4
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 302 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 51 times.
425 if (!frame->show_frame && !frame->show_existing_frame)
114 72 continue;
115
116 353 ctx->width = frame->frame_width_minus_1 + 1;
117 353 ctx->height = frame->frame_height_minus_1 + 1;
118
119
3/4
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 291 times.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
353 ctx->key_frame = frame->frame_type == AV1_FRAME_KEY && !frame->show_existing_frame;
120
121
3/4
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 290 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
353 switch (frame->frame_type) {
122 62 case AV1_FRAME_KEY:
123 case AV1_FRAME_INTRA_ONLY:
124 62 ctx->pict_type = AV_PICTURE_TYPE_I;
125 62 break;
126 290 case AV1_FRAME_INTER:
127 290 ctx->pict_type = AV_PICTURE_TYPE_P;
128 290 break;
129 1 case AV1_FRAME_SWITCH:
130 1 ctx->pict_type = AV_PICTURE_TYPE_SP;
131 1 break;
132 }
133 353 ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
134 }
135
136
2/4
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
375 switch (av1->bit_depth) {
137 364 case 8:
138 728 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
139
1/2
✓ Branch 0 taken 364 times.
✗ Branch 1 not taken.
364 : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
140 364 break;
141 11 case 10:
142 22 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
143
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
144 11 break;
145 case 12:
146 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
147 : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
148 break;
149 }
150 av_assert2(ctx->format != AV_PIX_FMT_NONE);
151
152
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
375 if (!color->subsampling_x && !color->subsampling_y &&
153 color->matrix_coefficients == AVCOL_SPC_RGB &&
154 color->color_primaries == AVCOL_PRI_BT709 &&
155 color->transfer_characteristics == AVCOL_TRC_IEC61966_2_1)
156 ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit];
157
158 375 avctx->profile = seq->seq_profile;
159 375 avctx->level = seq->seq_level_idx[0];
160
161 375 avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
162 375 avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
163 375 avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
375 avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
165
166
2/2
✓ Branch 0 taken 275 times.
✓ Branch 1 taken 100 times.
375 if (avctx->framerate.num)
167 100 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
168
169 275 end:
170 375 ff_cbs_fragment_reset(td);
171
172 375 s->cbc->log_ctx = NULL;
173
174 375 return size;
175 }
176
177 static const CodedBitstreamUnitType decompose_unit_types[] = {
178 AV1_OBU_TEMPORAL_DELIMITER,
179 AV1_OBU_SEQUENCE_HEADER,
180 AV1_OBU_FRAME_HEADER,
181 AV1_OBU_TILE_GROUP,
182 AV1_OBU_FRAME,
183 };
184
185 22 static av_cold int av1_parser_init(AVCodecParserContext *ctx)
186 {
187 22 AV1ParseContext *s = ctx->priv_data;
188 int ret;
189
190 22 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
192 return ret;
193
194 22 s->cbc->decompose_unit_types = decompose_unit_types;
195 22 s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
196
197 22 return 0;
198 }
199
200 22 static void av1_parser_close(AVCodecParserContext *ctx)
201 {
202 22 AV1ParseContext *s = ctx->priv_data;
203
204 22 ff_cbs_fragment_free(&s->temporal_unit);
205 22 ff_cbs_close(&s->cbc);
206 22 }
207
208 const AVCodecParser ff_av1_parser = {
209 .codec_ids = { AV_CODEC_ID_AV1 },
210 .priv_data_size = sizeof(AV1ParseContext),
211 .parser_init = av1_parser_init,
212 .parser_close = av1_parser_close,
213 .parser_parse = av1_parser_parse,
214 };
215