FFmpeg
av1_parser.c
Go to the documentation of this file.
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 {
33 
34 static const enum AVPixelFormat pix_fmts_8bit[2][2] = {
37 };
38 static const enum AVPixelFormat pix_fmts_10bit[2][2] = {
41 };
42 static const enum AVPixelFormat pix_fmts_12bit[2][2] = {
45 };
46 
48  AVCodecContext *avctx,
49  const uint8_t **out_data, int *out_size,
50  const uint8_t *data, int size)
51 {
53  CodedBitstreamFragment *td = &s->temporal_unit;
54  CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
55  int ret;
56 
57  *out_data = data;
58  *out_size = size;
59 
60  ctx->key_frame = -1;
61  ctx->pict_type = AV_PICTURE_TYPE_NONE;
62  ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
63 
64  s->cbc->log_ctx = avctx;
65 
66  if (avctx->extradata_size && !s->parsed_extradata) {
67  s->parsed_extradata = 1;
68 
69  ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
70  if (ret < 0) {
71  av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
72  }
73 
74  ff_cbs_fragment_reset(s->cbc, td);
75  }
76 
77  ret = ff_cbs_read(s->cbc, td, data, size);
78  if (ret < 0) {
79  av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
80  goto end;
81  }
82 
83  if (!av1->sequence_header) {
84  av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
85  goto end;
86  }
87 
88  for (int i = 0; i < td->nb_units; i++) {
89  CodedBitstreamUnit *unit = &td->units[i];
90  AV1RawOBU *obu = unit->content;
94  int frame_type;
95 
96  if (unit->type == AV1_OBU_FRAME)
97  frame = &obu->obu.frame.header;
98  else if (unit->type == AV1_OBU_FRAME_HEADER)
99  frame = &obu->obu.frame_header;
100  else
101  continue;
102 
103  if (frame->show_existing_frame) {
104  AV1ReferenceFrameState *ref = &av1->ref[frame->frame_to_show_map_idx];
105 
106  if (!ref->valid) {
107  av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
108  goto end;
109  }
110 
111  ctx->width = ref->frame_width;
112  ctx->height = ref->frame_height;
113  frame_type = ref->frame_type;
114 
115  ctx->key_frame = 0;
116  } else if (!frame->show_frame) {
117  continue;
118  } else {
119  ctx->width = av1->frame_width;
120  ctx->height = av1->frame_height;
121  frame_type = frame->frame_type;
122 
123  ctx->key_frame = frame_type == AV1_FRAME_KEY;
124  }
125 
126  avctx->profile = seq->seq_profile;
127  avctx->level = seq->seq_level_idx[0];
128 
129  switch (frame_type) {
130  case AV1_FRAME_KEY:
132  ctx->pict_type = AV_PICTURE_TYPE_I;
133  break;
134  case AV1_FRAME_INTER:
135  ctx->pict_type = AV_PICTURE_TYPE_P;
136  break;
137  case AV1_FRAME_SWITCH:
138  ctx->pict_type = AV_PICTURE_TYPE_SP;
139  break;
140  }
141  ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
142 
143  switch (av1->bit_depth) {
144  case 8:
145  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
146  : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
147  break;
148  case 10:
149  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
150  : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
151  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  av_assert2(ctx->format != AV_PIX_FMT_NONE);
158  }
159 
160 end:
161  ff_cbs_fragment_reset(s->cbc, td);
162 
163  s->cbc->log_ctx = NULL;
164 
165  return size;
166 }
167 
174 };
175 
177 {
179  int ret;
180 
181  ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
182  if (ret < 0)
183  return ret;
184 
185  s->cbc->decompose_unit_types = (CodedBitstreamUnitType *)decompose_unit_types;
186  s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
187 
188  return 0;
189 }
190 
192 {
194 
195  ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
196  ff_cbs_close(&s->cbc);
197 }
198 
200  const uint8_t *buf, int buf_size)
201 {
202  AV1OBU obu;
203  const uint8_t *ptr = buf, *end = buf + buf_size;
204 
205  while (ptr < end) {
206  int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
207  if (len < 0)
208  break;
209 
210  if (obu.type == AV1_OBU_FRAME_HEADER ||
211  obu.type == AV1_OBU_FRAME) {
212  return ptr - buf;
213  }
214  ptr += len;
215  buf_size -= len;
216  }
217 
218  return 0;
219 }
220 
222  .codec_ids = { AV_CODEC_ID_AV1 },
223  .priv_data_size = sizeof(AV1ParseContext),
224  .parser_init = av1_parser_init,
225  .parser_close = av1_parser_close,
226  .parser_parse = av1_parser_parse,
228 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
td
#define td
Definition: regdef.h:70
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AV1RawSequenceHeader::seq_level_idx
uint8_t seq_level_idx[AV1_MAX_OPERATING_POINTS]
Definition: cbs_av1.h:87
AV1RawSequenceHeader
Definition: cbs_av1.h:73
color
Definition: vf_paletteuse.c:588
AV_PICTURE_STRUCTURE_UNKNOWN
@ AV_PICTURE_STRUCTURE_UNKNOWN
Definition: avcodec.h:5102
pix_fmts_8bit
static enum AVPixelFormat pix_fmts_8bit[2][2]
Definition: av1_parser.c:34
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
out_size
int out_size
Definition: movenc.c:55
end
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:168
ff_cbs_close
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:115
data
const char data[16]
Definition: mxf.c:91
AV1OBU
Definition: av1_parse.h:30
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:387
ff_cbs_fragment_free
void ff_cbs_fragment_free(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:157
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
cbs.h
AV1_OBU_TEMPORAL_DELIMITER
@ AV1_OBU_TEMPORAL_DELIMITER
Definition: av1.h:31
av1_parser_init
static av_cold int av1_parser_init(AVCodecParserContext *ctx)
Definition: av1_parser.c:176
AV1RawSequenceHeader::seq_profile
uint8_t seq_profile
Definition: cbs_av1.h:74
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:64
AV1RawFrame::header
AV1RawFrameHeader header
Definition: cbs_av1.h:300
AV1RawColorConfig
Definition: cbs_av1.h:41
av1_parse.h
AV1_OBU_FRAME_HEADER
@ AV1_OBU_FRAME_HEADER
Definition: av1.h:32
ff_cbs_read
int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Read a bitstream from a memory region into a fragment, then split into units and decompose.
Definition: cbs.c:269
av1_parser_split
static int av1_parser_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: av1_parser.c:199
ff_av1_parser
AVCodecParser ff_av1_parser
Definition: av1_parser.c:221
ff_av1_extract_obu
int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, void *logctx)
Extract an OBU from a raw bitstream.
Definition: av1_parse.c:29
AV1_FRAME_KEY
@ AV1_FRAME_KEY
Definition: av1.h:53
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:390
cbs_av1.h
AV_PICTURE_STRUCTURE_FRAME
@ AV_PICTURE_STRUCTURE_FRAME
Definition: avcodec.h:5105
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
av_cold
#define av_cold
Definition: attributes.h:84
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:1667
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
s
#define s(width, name)
Definition: cbs_vp9.c:257
CodedBitstreamAV1Context::frame_height
int frame_height
Definition: cbs_av1.h:433
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:368
AV1_FRAME_SWITCH
@ AV1_FRAME_SWITCH
Definition: av1.h:56
AV1RawOBU
Definition: cbs_av1.h:387
NULL
#define NULL
Definition: coverity.c:32
AVCodecParser::codec_ids
int codec_ids[5]
Definition: avcodec.h:5276
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: avcodec.h:443
AV1RawFrameHeader
Definition: cbs_av1.h:133
AV1RawOBU::obu
union AV1RawOBU::@48 obu
av1_parser_parse
static int av1_parser_parse(AVCodecParserContext *ctx, AVCodecContext *avctx, const uint8_t **out_data, int *out_size, const uint8_t *data, int size)
Definition: av1_parser.c:47
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AV1_OBU_TILE_GROUP
@ AV1_OBU_TILE_GROUP
Definition: av1.h:33
AV1_FRAME_INTER
@ AV1_FRAME_INTER
Definition: av1.h:54
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:388
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVCodecContext::level
int level
level
Definition: avcodec.h:3018
AV_PICTURE_TYPE_SP
@ AV_PICTURE_TYPE_SP
Switching Predicted.
Definition: avutil.h:279
av1_parser_close
static void av1_parser_close(AVCodecParserContext *ctx)
Definition: av1_parser.c:191
AV1_OBU_FRAME
@ AV1_OBU_FRAME
Definition: av1.h:35
AV1ParseContext
Definition: av1_parser.c:28
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:392
size
int size
Definition: twinvq_data.h:11134
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:394
AV1_OBU_SEQUENCE_HEADER
@ AV1_OBU_SEQUENCE_HEADER
Definition: av1.h:30
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:273
AV1ReferenceFrameState
Definition: cbs_av1.h:403
split
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
AV1_FRAME_INTRA_ONLY
@ AV1_FRAME_INTRA_ONLY
Definition: av1.h:55
AV1RawOBU::frame_header
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:394
CodedBitstreamAV1Context::frame_width
int frame_width
Definition: cbs_av1.h:432
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
decompose_unit_types
static const CodedBitstreamUnitType decompose_unit_types[]
Definition: av1_parser.c:168
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
pix_fmts_12bit
static enum AVPixelFormat pix_fmts_12bit[2][2]
Definition: av1_parser.c:42
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
CodedBitstreamAV1Context::bit_depth
int bit_depth
Definition: cbs_av1.h:431
AV1ParseContext::cbc
CodedBitstreamContext * cbc
Definition: av1_parser.c:29
uint8_t
uint8_t
Definition: audio_convert.c:194
AV1RawSequenceHeader::color_config
AV1RawColorConfig color_config
Definition: cbs_av1.h:128
parser.h
AV1ParseContext::parsed_extradata
int parsed_extradata
Definition: av1_parser.c:31
len
int len
Definition: vorbis_enc_data.h:452
AVCodecParserContext
Definition: avcodec.h:5108
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ff_cbs_init
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:74
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:391
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen_template.c:38
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
CodedBitstreamAV1Context::ref
AV1ReferenceFrameState * ref
Definition: cbs_av1.h:444
CodedBitstreamAV1Context::sequence_header
AV1RawSequenceHeader * sequence_header
Definition: cbs_av1.h:419
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:2898
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:142
AV1OBU::type
int type
Definition: av1_parse.h:48
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AV1RawOBU::frame
AV1RawFrame frame
Definition: cbs_av1.h:395
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
pix_fmts_10bit
static enum AVPixelFormat pix_fmts_10bit[2][2]
Definition: av1_parser.c:38
AVCodecParser
Definition: avcodec.h:5275
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:369
AV1ParseContext::temporal_unit
CodedBitstreamFragment temporal_unit
Definition: av1_parser.c:30
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1370
CodedBitstreamAV1Context
Definition: cbs_av1.h:418