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 "libavutil/avassert.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 
47 static const enum AVPixelFormat pix_fmts_rgb[3] = {
49 };
50 
52  AVCodecContext *avctx,
53  const uint8_t **out_data, int *out_size,
54  const uint8_t *data, int size)
55 {
57  CodedBitstreamFragment *td = &s->temporal_unit;
58  const CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
59  const AV1RawSequenceHeader *seq;
60  const AV1RawColorConfig *color;
61  int ret;
62 
63  *out_data = data;
64  *out_size = size;
65 
66  ctx->key_frame = -1;
67  ctx->pict_type = AV_PICTURE_TYPE_NONE;
68  ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
69 
70  s->cbc->log_ctx = avctx;
71 
72  if (avctx->extradata_size && !s->parsed_extradata) {
73  s->parsed_extradata = 1;
74 
75  ret = ff_cbs_read_extradata_from_codec(s->cbc, td, avctx);
76  if (ret < 0) {
77  av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
78  }
79 
81  }
82 
83  ret = ff_cbs_read(s->cbc, td, data, size);
84  if (ret < 0) {
85  av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
86  goto end;
87  }
88 
89  if (!av1->sequence_header) {
90  av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
91  goto end;
92  }
93 
94  seq = av1->sequence_header;
95  color = &seq->color_config;
96 
97  for (int i = 0; i < td->nb_units; i++) {
98  const CodedBitstreamUnit *unit = &td->units[i];
99  const AV1RawOBU *obu = unit->content;
100  const AV1RawFrameHeader *frame;
101 
102  if (unit->type == AV1_OBU_FRAME)
103  frame = &obu->obu.frame.header;
104  else if (unit->type == AV1_OBU_FRAME_HEADER)
105  frame = &obu->obu.frame_header;
106  else
107  continue;
108 
109  if (obu->header.spatial_id > 0)
110  continue;
111 
112  if (!frame->show_frame && !frame->show_existing_frame)
113  continue;
114 
115  ctx->width = frame->frame_width_minus_1 + 1;
116  ctx->height = frame->frame_height_minus_1 + 1;
117 
118  ctx->key_frame = frame->frame_type == AV1_FRAME_KEY && !frame->show_existing_frame;
119 
120  switch (frame->frame_type) {
121  case AV1_FRAME_KEY:
123  ctx->pict_type = AV_PICTURE_TYPE_I;
124  break;
125  case AV1_FRAME_INTER:
126  ctx->pict_type = AV_PICTURE_TYPE_P;
127  break;
128  case AV1_FRAME_SWITCH:
129  ctx->pict_type = AV_PICTURE_TYPE_SP;
130  break;
131  }
132  ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
133  }
134 
135  switch (av1->bit_depth) {
136  case 8:
137  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
138  : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
139  break;
140  case 10:
141  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
142  : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
143  break;
144  case 12:
145  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
146  : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
147  break;
148  }
149  av_assert2(ctx->format != AV_PIX_FMT_NONE);
150 
151  if (!color->subsampling_x && !color->subsampling_y &&
152  color->matrix_coefficients == AVCOL_SPC_RGB &&
153  color->color_primaries == AVCOL_PRI_BT709 &&
154  color->transfer_characteristics == AVCOL_TRC_IEC61966_2_1)
155  ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit];
156 
157  avctx->profile = seq->seq_profile;
158  avctx->level = seq->seq_level_idx[0];
159 
160  avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
161  avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
162  avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
163  avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
164 
166  const AV1RawTimingInfo *timing = &seq->timing_info;
167  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
168  timing->num_units_in_display_tick, timing->time_scale, INT_MAX);
169  }
170 
171 end:
173 
174  s->cbc->log_ctx = NULL;
175 
176  return size;
177 }
178 
185 };
186 
188 {
190  int ret;
191 
192  ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
193  if (ret < 0)
194  return ret;
195 
196  s->cbc->decompose_unit_types = decompose_unit_types;
197  s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
198 
199  return 0;
200 }
201 
203 {
205 
206  ff_cbs_fragment_free(&s->temporal_unit);
207  ff_cbs_close(&s->cbc);
208 }
209 
211  .codec_ids = { AV_CODEC_ID_AV1 },
212  .priv_data_size = sizeof(AV1ParseContext),
213  .parser_init = av1_parser_init,
214  .parser_close = av1_parser_close,
215  .parser_parse = av1_parser_parse,
216 };
AV1RawTimingInfo::num_units_in_display_tick
uint32_t num_units_in_display_tick
Definition: cbs_av1.h:59
AV1RawTimingInfo::time_scale
uint32_t time_scale
Definition: cbs_av1.h:60
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
td
#define td
Definition: regdef.h:70
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
pix_fmts_rgb
static enum AVPixelFormat pix_fmts_rgb[3]
Definition: av1_parser.c:47
AV1RawSequenceHeader::seq_level_idx
uint8_t seq_level_idx[AV1_MAX_OPERATING_POINTS]
Definition: cbs_av1.h:87
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1002
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:558
AV1RawSequenceHeader::timing_info_present_flag
uint8_t timing_info_present_flag
Definition: cbs_av1.h:78
AV1RawSequenceHeader
Definition: cbs_av1.h:73
color
Definition: vf_paletteuse.c:509
AV1_FRAME_INTRA_ONLY
@ AV1_FRAME_INTRA_ONLY
Definition: av1.h:55
ff_cbs_fragment_free
av_cold void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:171
AV_PICTURE_STRUCTURE_UNKNOWN
@ AV_PICTURE_STRUCTURE_UNKNOWN
unknown
Definition: avcodec.h:2806
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:106
out_size
int out_size
Definition: movenc.c:55
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:995
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:661
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(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:157
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:173
data
const char data[16]
Definition: mxf.c:146
AV1RawSequenceHeader::timing_info
AV1RawTimingInfo timing_info
Definition: cbs_av1.h:83
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:459
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:588
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:73
cbs.h
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:533
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:187
AV1RawOBU::header
AV1RawOBUHeader header
Definition: cbs_av1.h:392
AV1RawSequenceHeader::seq_profile
uint8_t seq_profile
Definition: cbs_av1.h:74
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:69
ff_cbs_close
av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:127
AV1RawFrame::header
AV1RawFrameHeader header
Definition: cbs_av1.h:304
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1750
AV1RawColorConfig
Definition: cbs_av1.h:41
AVCOL_TRC_IEC61966_2_1
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:572
AV1_OBU_FRAME_HEADER
@ AV1_OBU_FRAME_HEADER
Definition: av1.h:32
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:475
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:310
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:462
cbs_av1.h
avassert.h
AV_PICTURE_STRUCTURE_FRAME
@ AV_PICTURE_STRUCTURE_FRAME
coded as frame
Definition: avcodec.h:2809
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:988
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:528
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:121
s
#define s(width, name)
Definition: cbs_vp9.c:256
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:440
if
if(ret)
Definition: filter_design.txt:179
AV1RawOBU::obu
union AV1RawOBU::@27 obu
AV1RawOBU
Definition: cbs_av1.h:391
NULL
#define NULL
Definition: coverity.c:32
AV1RawTimingInfo
Definition: cbs_av1.h:58
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1009
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:283
AV1RawFrameHeader
Definition: cbs_av1.h:165
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:535
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:51
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_SWITCH
@ AV1_FRAME_SWITCH
Definition: av1.h:56
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:460
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVCodecContext::level
int level
level
Definition: avcodec.h:1691
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:202
AV1_OBU_FRAME
@ AV1_OBU_FRAME
Definition: av1.h:35
AV1ParseContext
Definition: av1_parser.c:28
AV1_FRAME_INTER
@ AV1_FRAME_INTER
Definition: av1.h:54
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2972
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:464
AV1RawOBUHeader::spatial_id
uint8_t spatial_id
Definition: cbs_av1.h:37
size
int size
Definition: twinvq_data.h:10344
color
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:466
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
ff_av1_parser
const AVCodecParser ff_av1_parser
Definition: av1_parser.c:210
AV1RawOBU::frame_header
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:398
ff_cbs_read_extradata_from_codec
int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecContext *avctx)
Definition: cbs.c:280
AV1_FRAME_KEY
@ AV1_FRAME_KEY
Definition: av1.h:53
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:179
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
pix_fmts_12bit
static enum AVPixelFormat pix_fmts_12bit[2][2]
Definition: av1_parser.c:42
CodedBitstreamAV1Context::bit_depth
int bit_depth
Definition: cbs_av1.h:442
AV1ParseContext::cbc
CodedBitstreamContext * cbc
Definition: av1_parser.c:29
AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:476
AVColorSpace
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:587
AV1RawSequenceHeader::color_config
AV1RawColorConfig color_config
Definition: cbs_av1.h:128
parser.h
AV1ParseContext::parsed_extradata
int parsed_extradata
Definition: av1_parser.c:31
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:644
AVCodecParserContext
Definition: avcodec.h:2812
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
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:463
AVCodecContext
main external API structure.
Definition: avcodec.h:426
CodedBitstreamAV1Context::sequence_header
AV1RawSequenceHeader * sequence_header
Definition: cbs_av1.h:430
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:46
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1565
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_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AV1RawOBU::frame
AV1RawFrame frame
Definition: cbs_av1.h:399
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
ff_cbs_init
av_cold 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:76
AVCodecParser
Definition: avcodec.h:2971
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:441
AV1ParseContext::temporal_unit
CodedBitstreamFragment temporal_unit
Definition: av1_parser.c:30
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1132
CodedBitstreamAV1Context
Definition: cbs_av1.h:427