FFmpeg  4.3
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 "internal.h"
27 #include "parser.h"
28 
29 typedef struct AV1ParseContext {
34 
35 static const enum AVPixelFormat pix_fmts_8bit[2][2] = {
38 };
39 static const enum AVPixelFormat pix_fmts_10bit[2][2] = {
42 };
43 static const enum AVPixelFormat pix_fmts_12bit[2][2] = {
46 };
47 
49  AVCodecContext *avctx,
50  const uint8_t **out_data, int *out_size,
51  const uint8_t *data, int size)
52 {
53  AV1ParseContext *s = ctx->priv_data;
56  int ret;
57 
58  *out_data = data;
59  *out_size = size;
60 
61  ctx->key_frame = -1;
64 
65  s->cbc->log_ctx = avctx;
66 
67  if (avctx->extradata_size && !s->parsed_extradata) {
68  s->parsed_extradata = 1;
69 
70  ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
71  if (ret < 0) {
72  av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
73  }
74 
75  ff_cbs_fragment_reset(s->cbc, td);
76  }
77 
78  ret = ff_cbs_read(s->cbc, td, data, size);
79  if (ret < 0) {
80  av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
81  goto end;
82  }
83 
84  if (!av1->sequence_header) {
85  av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
86  goto end;
87  }
88 
89  for (int i = 0; i < td->nb_units; i++) {
90  CodedBitstreamUnit *unit = &td->units[i];
91  AV1RawOBU *obu = unit->content;
95  int frame_type;
96 
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;
101  else
102  continue;
103 
104  if (obu->header.spatial_id > 0)
105  continue;
106 
107  if (frame->show_existing_frame) {
109 
110  if (!ref->valid) {
111  av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
112  goto end;
113  }
114 
115  ctx->width = ref->frame_width;
116  ctx->height = ref->frame_height;
117  frame_type = ref->frame_type;
118 
119  ctx->key_frame = 0;
120  } else if (!frame->show_frame) {
121  continue;
122  } else {
123  ctx->width = av1->frame_width;
124  ctx->height = av1->frame_height;
125  frame_type = frame->frame_type;
126 
127  ctx->key_frame = frame_type == AV1_FRAME_KEY;
128  }
129 
130  avctx->profile = seq->seq_profile;
131  avctx->level = seq->seq_level_idx[0];
132 
133  switch (frame_type) {
134  case AV1_FRAME_KEY:
137  break;
138  case AV1_FRAME_INTER:
140  break;
141  case AV1_FRAME_SWITCH:
143  break;
144  }
146 
147  switch (av1->bit_depth) {
148  case 8:
149  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
150  : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
151  break;
152  case 10:
153  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
154  : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
155  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  }
162 
163  avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
164  avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
167 
168  if (ctx->width != avctx->width || ctx->height != avctx->height) {
169  ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
170  if (ret < 0)
171  goto end;
172  }
173  }
174 
175  if (avctx->framerate.num)
176  avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
177 
178 end:
179  ff_cbs_fragment_reset(s->cbc, td);
180 
181  s->cbc->log_ctx = NULL;
182 
183  return size;
184 }
185 
192 };
193 
195 {
196  AV1ParseContext *s = ctx->priv_data;
197  int ret;
198 
199  ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
200  if (ret < 0)
201  return ret;
202 
205 
206  return 0;
207 }
208 
210 {
211  AV1ParseContext *s = ctx->priv_data;
212 
214  ff_cbs_close(&s->cbc);
215 }
216 
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 
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,
246 };
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:2069
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
int size
if(ret< 0)
Definition: vf_mcdeint.c:279
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
uint8_t mono_chrome
Definition: cbs_av1.h:44
int width
Dimensions of the decoded video intended for presentation.
Definition: avcodec.h:3500
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
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1161
int num
Numerator.
Definition: rational.h:59
int codec_ids[5]
Definition: avcodec.h:3521
int out_size
Definition: movenc.c:55
static enum AVPixelFormat pix_fmts_10bit[2][2]
Definition: av1_parser.c:39
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:401
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
uint8_t color_range
Definition: cbs_av1.h:51
int profile
profile
Definition: avcodec.h:1859
uint8_t seq_profile
Definition: cbs_av1.h:74
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:480
static const CodedBitstreamUnitType decompose_unit_types[]
Definition: av1_parser.c:186
static enum AVPixelFormat pix_fmts_12bit[2][2]
Definition: av1_parser.c:43
frame_type
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:649
Undefined.
Definition: avutil.h:273
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:378
CodedBitstreamFragment temporal_unit
Definition: av1_parser.c:31
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:379
uint8_t matrix_coefficients
Definition: cbs_av1.h:49
enum AVPictureStructure picture_structure
Indicate whether a picture is coded as a frame, top field or bottom field.
Definition: avcodec.h:3487
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:509
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
static AVFrame * frame
const char data[16]
Definition: mxf.c:91
Coded bitstream unit structure.
Definition: cbs.h:64
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:402
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:455
static int av1_parser_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: av1_parser.c:217
AVCodecParser ff_av1_parser
Definition: av1_parser.c:239
#define av_log(a,...)
static enum AVPixelFormat pix_fmts_8bit[2][2]
Definition: av1_parser.c:35
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
int parsed_extradata
Definition: av1_parser.c:32
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define td
Definition: regdef.h:70
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:48
AV1RawFrame frame
Definition: cbs_av1.h:395
union AV1RawOBU::@25 obu
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:400
static av_cold int av1_parser_init(AVCodecParserContext *ctx)
Definition: av1_parser.c:194
static void av1_parser_close(AVCodecParserContext *ctx)
Definition: av1_parser.c:209
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
AV1RawColorConfig color_config
Definition: cbs_av1.h:128
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:173
uint8_t frame_type
Definition: cbs_av1.h:139
int width
picture width / height.
Definition: avcodec.h:699
AV1ReferenceFrameState * ref
Definition: cbs_av1.h:444
AV1RawOBUHeader header
Definition: cbs_av1.h:388
AVFormatContext * ctx
Definition: movenc.c:48
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1140
#define s(width, name)
Definition: cbs_vp9.c:257
int level
level
Definition: avcodec.h:1982
uint8_t color_primaries
Definition: cbs_av1.h:47
#define FF_ARRAY_ELEMS(a)
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:535
uint8_t subsampling_y
Definition: cbs_av1.h:53
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:201
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
uint8_t subsampling_x
Definition: cbs_av1.h:52
main external API structure.
Definition: avcodec.h:526
int extradata_size
Definition: avcodec.h:628
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:397
Switching Predicted.
Definition: avutil.h:279
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment&#39;s own data buffer, but not the units a...
Definition: cbs.c:142
Context structure for coded bitstream operations.
Definition: cbs.h:168
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1154
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1147
int type
Definition: av1_parse.h:48
uint8_t show_existing_frame
Definition: cbs_av1.h:134
CodedBitstreamContext * cbc
Definition: av1_parser.c:30
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:115
AV1RawSequenceHeader * sequence_header
Definition: cbs_av1.h:419
uint8_t spatial_id
Definition: cbs_av1.h:37
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:404
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:534
void * priv_data
Internal codec-specific data.
Definition: cbs.h:189
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
common internal api header.
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:197
int len
int format
The format of the coded data, corresponds to enum AVPixelFormat for video and for enum AVSampleFormat...
Definition: avcodec.h:3517
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:394
uint8_t show_frame
Definition: cbs_av1.h:140
uint8_t frame_to_show_map_idx
Definition: cbs_av1.h:135
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
AV1RawFrameHeader header
Definition: cbs_av1.h:300
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3402
uint8_t seq_level_idx[AV1_MAX_OPERATING_POINTS]
Definition: cbs_av1.h:87
uint8_t transfer_characteristics
Definition: cbs_av1.h:48
Predicted.
Definition: avutil.h:275