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 static const enum AVPixelFormat pix_fmts_rgb[3] = {
49 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
50 };
51
av1_parser_parse(AVCodecParserContext * ctx,AVCodecContext * avctx,const uint8_t ** out_data,int * out_size,const uint8_t * data,int size)52 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 AV1ParseContext *s = ctx->priv_data;
58 CodedBitstreamFragment *td = &s->temporal_unit;
59 CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
60 AV1RawSequenceHeader *seq;
61 AV1RawColorConfig *color;
62 int ret;
63
64 *out_data = data;
65 *out_size = size;
66
67 ctx->key_frame = -1;
68 ctx->pict_type = AV_PICTURE_TYPE_NONE;
69 ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
70
71 s->cbc->log_ctx = avctx;
72
73 if (avctx->extradata_size && !s->parsed_extradata) {
74 s->parsed_extradata = 1;
75
76 ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
77 if (ret < 0) {
78 av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
79 }
80
81 ff_cbs_fragment_reset(s->cbc, td);
82 }
83
84 ret = ff_cbs_read(s->cbc, td, data, size);
85 if (ret < 0) {
86 av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
87 goto end;
88 }
89
90 if (!av1->sequence_header) {
91 av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
92 goto end;
93 }
94
95 seq = av1->sequence_header;
96 color = &seq->color_config;
97
98 for (int i = 0; i < td->nb_units; i++) {
99 CodedBitstreamUnit *unit = &td->units[i];
100 AV1RawOBU *obu = unit->content;
101 AV1RawFrameHeader *frame;
102 int frame_type;
103
104 if (unit->type == AV1_OBU_FRAME)
105 frame = &obu->obu.frame.header;
106 else if (unit->type == AV1_OBU_FRAME_HEADER)
107 frame = &obu->obu.frame_header;
108 else
109 continue;
110
111 if (obu->header.spatial_id > 0)
112 continue;
113
114 if (frame->show_existing_frame) {
115 AV1ReferenceFrameState *ref = &av1->ref[frame->frame_to_show_map_idx];
116
117 if (!ref->valid) {
118 av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
119 goto end;
120 }
121
122 ctx->width = ref->frame_width;
123 ctx->height = ref->frame_height;
124 frame_type = ref->frame_type;
125
126 ctx->key_frame = 0;
127 } else if (!frame->show_frame) {
128 continue;
129 } else {
130 ctx->width = av1->frame_width;
131 ctx->height = av1->frame_height;
132 frame_type = frame->frame_type;
133
134 ctx->key_frame = frame_type == AV1_FRAME_KEY;
135 }
136
137 switch (frame_type) {
138 case AV1_FRAME_KEY:
139 case AV1_FRAME_INTRA_ONLY:
140 ctx->pict_type = AV_PICTURE_TYPE_I;
141 break;
142 case AV1_FRAME_INTER:
143 ctx->pict_type = AV_PICTURE_TYPE_P;
144 break;
145 case AV1_FRAME_SWITCH:
146 ctx->pict_type = AV_PICTURE_TYPE_SP;
147 break;
148 }
149 ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
150 }
151
152 switch (av1->bit_depth) {
153 case 8:
154 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
155 : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
156 break;
157 case 10:
158 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
159 : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
160 break;
161 case 12:
162 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
163 : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
164 break;
165 }
166 av_assert2(ctx->format != AV_PIX_FMT_NONE);
167
168 if (!color->subsampling_x && !color->subsampling_y &&
169 color->matrix_coefficients == AVCOL_SPC_RGB &&
170 color->color_primaries == AVCOL_PRI_BT709 &&
171 color->transfer_characteristics == AVCOL_TRC_IEC61966_2_1)
172 ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit];
173
174 avctx->pix_fmt = ctx->format;
175
176 avctx->profile = seq->seq_profile;
177 avctx->level = seq->seq_level_idx[0];
178
179 avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
180 avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
181 avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
182 avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
183
184 if (ctx->width != avctx->width || ctx->height != avctx->height) {
185 ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
186 if (ret < 0)
187 goto end;
188 }
189
190 if (avctx->framerate.num)
191 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
192
193 end:
194 ff_cbs_fragment_reset(s->cbc, td);
195
196 s->cbc->log_ctx = NULL;
197
198 return size;
199 }
200
201 static const CodedBitstreamUnitType decompose_unit_types[] = {
202 AV1_OBU_TEMPORAL_DELIMITER,
203 AV1_OBU_SEQUENCE_HEADER,
204 AV1_OBU_FRAME_HEADER,
205 AV1_OBU_TILE_GROUP,
206 AV1_OBU_FRAME,
207 };
208
av1_parser_init(AVCodecParserContext * ctx)209 static av_cold int av1_parser_init(AVCodecParserContext *ctx)
210 {
211 AV1ParseContext *s = ctx->priv_data;
212 int ret;
213
214 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
215 if (ret < 0)
216 return ret;
217
218 s->cbc->decompose_unit_types = (CodedBitstreamUnitType *)decompose_unit_types;
219 s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
220
221 return 0;
222 }
223
av1_parser_close(AVCodecParserContext * ctx)224 static void av1_parser_close(AVCodecParserContext *ctx)
225 {
226 AV1ParseContext *s = ctx->priv_data;
227
228 ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
229 ff_cbs_close(&s->cbc);
230 }
231
av1_parser_split(AVCodecContext * avctx,const uint8_t * buf,int buf_size)232 static int av1_parser_split(AVCodecContext *avctx,
233 const uint8_t *buf, int buf_size)
234 {
235 AV1OBU obu;
236 const uint8_t *ptr = buf, *end = buf + buf_size;
237
238 while (ptr < end) {
239 int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
240 if (len < 0)
241 break;
242
243 if (obu.type == AV1_OBU_FRAME_HEADER ||
244 obu.type == AV1_OBU_FRAME) {
245 return ptr - buf;
246 }
247 ptr += len;
248 buf_size -= len;
249 }
250
251 return 0;
252 }
253
254 AVCodecParser ff_av1_parser = {
255 .codec_ids = { AV_CODEC_ID_AV1 },
256 .priv_data_size = sizeof(AV1ParseContext),
257 .parser_init = av1_parser_init,
258 .parser_close = av1_parser_close,
259 .parser_parse = av1_parser_parse,
260 .split = av1_parser_split,
261 };
262