4 * Copyright (C) 2018 James Almer <jamrial@gmail.com>
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "av1_parse.h"
29 typedef struct AV1ParseContext {
30 CodedBitstreamContext *cbc;
31 CodedBitstreamFragment temporal_unit;
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 },
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 },
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 },
48 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)
53 AV1ParseContext *s = ctx->priv_data;
54 CodedBitstreamFragment *td = &s->temporal_unit;
55 CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
62 ctx->pict_type = AV_PICTURE_TYPE_NONE;
63 ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
65 s->cbc->log_ctx = avctx;
67 if (avctx->extradata_size && !s->parsed_extradata) {
68 s->parsed_extradata = 1;
70 ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
72 av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
75 ff_cbs_fragment_reset(s->cbc, td);
78 ret = ff_cbs_read(s->cbc, td, data, size);
80 av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
84 if (!av1->sequence_header) {
85 av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
89 for (int i = 0; i < td->nb_units; i++) {
90 CodedBitstreamUnit *unit = &td->units[i];
91 AV1RawOBU *obu = unit->content;
92 AV1RawSequenceHeader *seq = av1->sequence_header;
93 AV1RawColorConfig *color = &seq->color_config;
94 AV1RawFrameHeader *frame;
97 if (unit->type == AV1_OBU_FRAME)
98 frame = &obu->obu.frame.header;
99 else if (unit->type == AV1_OBU_FRAME_HEADER)
100 frame = &obu->obu.frame_header;
104 if (frame->show_existing_frame) {
105 AV1ReferenceFrameState *ref = &av1->ref[frame->frame_to_show_map_idx];
108 av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
112 ctx->width = ref->frame_width;
113 ctx->height = ref->frame_height;
114 frame_type = ref->frame_type;
117 } else if (!frame->show_frame) {
120 ctx->width = av1->frame_width;
121 ctx->height = av1->frame_height;
122 frame_type = frame->frame_type;
124 ctx->key_frame = frame_type == AV1_FRAME_KEY;
127 avctx->profile = seq->seq_profile;
128 avctx->level = seq->seq_level_idx[0];
130 switch (frame_type) {
132 case AV1_FRAME_INTRA_ONLY:
133 ctx->pict_type = AV_PICTURE_TYPE_I;
135 case AV1_FRAME_INTER:
136 ctx->pict_type = AV_PICTURE_TYPE_P;
138 case AV1_FRAME_SWITCH:
139 ctx->pict_type = AV_PICTURE_TYPE_SP;
142 ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
144 switch (av1->bit_depth) {
146 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
147 : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
150 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
151 : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
154 ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
155 : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
158 av_assert2(ctx->format != AV_PIX_FMT_NONE);
160 if (ctx->width != avctx->width || ctx->height != avctx->height) {
161 ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
167 if (avctx->framerate.num)
168 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
171 ff_cbs_fragment_reset(s->cbc, td);
173 s->cbc->log_ctx = NULL;
178 static const CodedBitstreamUnitType decompose_unit_types[] = {
179 AV1_OBU_TEMPORAL_DELIMITER,
180 AV1_OBU_SEQUENCE_HEADER,
181 AV1_OBU_FRAME_HEADER,
186 static av_cold int av1_parser_init(AVCodecParserContext *ctx)
188 AV1ParseContext *s = ctx->priv_data;
191 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
195 s->cbc->decompose_unit_types = (CodedBitstreamUnitType *)decompose_unit_types;
196 s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
201 static void av1_parser_close(AVCodecParserContext *ctx)
203 AV1ParseContext *s = ctx->priv_data;
205 ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
206 ff_cbs_close(&s->cbc);
209 static int av1_parser_split(AVCodecContext *avctx,
210 const uint8_t *buf, int buf_size)
213 const uint8_t *ptr = buf, *end = buf + buf_size;
216 int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
220 if (obu.type == AV1_OBU_FRAME_HEADER ||
221 obu.type == AV1_OBU_FRAME) {
231 AVCodecParser ff_av1_parser = {
232 .codec_ids = { AV_CODEC_ID_AV1 },
233 .priv_data_size = sizeof(AV1ParseContext),
234 .parser_init = av1_parser_init,
235 .parser_close = av1_parser_close,
236 .parser_parse = av1_parser_parse,
237 .split = av1_parser_split,