FFmpeg  4.4.5
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 
48 static const enum AVPixelFormat pix_fmts_rgb[3] = {
50 };
51 
53  AVCodecContext *avctx,
54  const uint8_t **out_data, int *out_size,
55  const uint8_t *data, int size)
56 {
58  CodedBitstreamFragment *td = &s->temporal_unit;
59  CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
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_extradata_from_codec(s->cbc, td, avctx);
77  if (ret < 0) {
78  av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
79  }
80 
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;
102 
103  if (unit->type == AV1_OBU_FRAME)
104  frame = &obu->obu.frame.header;
105  else if (unit->type == AV1_OBU_FRAME_HEADER)
106  frame = &obu->obu.frame_header;
107  else
108  continue;
109 
110  if (obu->header.spatial_id > 0)
111  continue;
112 
113  if (!frame->show_frame && !frame->show_existing_frame)
114  continue;
115 
116  ctx->width = frame->frame_width_minus_1 + 1;
117  ctx->height = frame->frame_height_minus_1 + 1;
118 
119  ctx->key_frame = frame->frame_type == AV1_FRAME_KEY && !frame->show_existing_frame;
120 
121  switch (frame->frame_type) {
122  case AV1_FRAME_KEY:
124  ctx->pict_type = AV_PICTURE_TYPE_I;
125  break;
126  case AV1_FRAME_INTER:
127  ctx->pict_type = AV_PICTURE_TYPE_P;
128  break;
129  case AV1_FRAME_SWITCH:
130  ctx->pict_type = AV_PICTURE_TYPE_SP;
131  break;
132  }
133  ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
134  }
135 
136  switch (av1->bit_depth) {
137  case 8:
138  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
139  : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
140  break;
141  case 10:
142  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
143  : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
144  break;
145  case 12:
146  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
147  : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
148  break;
149  }
150  av_assert2(ctx->format != AV_PIX_FMT_NONE);
151 
152  if (!color->subsampling_x && !color->subsampling_y &&
153  color->matrix_coefficients == AVCOL_SPC_RGB &&
154  color->color_primaries == AVCOL_PRI_BT709 &&
155  color->transfer_characteristics == AVCOL_TRC_IEC61966_2_1)
156  ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit];
157 
158  avctx->profile = seq->seq_profile;
159  avctx->level = seq->seq_level_idx[0];
160 
161  avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
162  avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
163  avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
164  avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
165 
166  if (avctx->framerate.num)
167  avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
168 
169 end:
171 
172  s->cbc->log_ctx = NULL;
173 
174  return size;
175 }
176 
183 };
184 
186 {
188  int ret;
189 
190  ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
191  if (ret < 0)
192  return ret;
193 
194  s->cbc->decompose_unit_types = decompose_unit_types;
195  s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
196 
197  return 0;
198 }
199 
201 {
203 
204  ff_cbs_fragment_free(&s->temporal_unit);
205  ff_cbs_close(&s->cbc);
206 }
207 
209  const uint8_t *buf, int buf_size)
210 {
211  AV1OBU obu;
212  const uint8_t *ptr = buf, *end = buf + buf_size;
213 
214  while (ptr < end) {
215  int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
216  if (len < 0)
217  break;
218 
219  if (obu.type == AV1_OBU_FRAME_HEADER ||
220  obu.type == AV1_OBU_FRAME) {
221  return ptr - buf;
222  }
223  ptr += len;
224  buf_size -= len;
225  }
226 
227  return 0;
228 }
229 
231  .codec_ids = { AV_CODEC_ID_AV1 },
232  .priv_data_size = sizeof(AV1ParseContext),
233  .parser_init = av1_parser_init,
234  .parser_close = av1_parser_close,
235  .parser_parse = av1_parser_parse,
237 };
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
#define av_cold
Definition: attributes.h:88
uint8_t
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
static int av1_parser_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: av1_parser.c:208
AVCodecParser ff_av1_parser
Definition: av1_parser.c:230
static enum AVPixelFormat pix_fmts_rgb[3]
Definition: av1_parser.c:48
static enum AVPixelFormat pix_fmts_12bit[2][2]
Definition: av1_parser.c:43
static const CodedBitstreamUnitType decompose_unit_types[]
Definition: av1_parser.c:177
static enum AVPixelFormat pix_fmts_8bit[2][2]
Definition: av1_parser.c:35
static enum AVPixelFormat pix_fmts_10bit[2][2]
Definition: av1_parser.c:39
static void av1_parser_close(AVCodecParserContext *ctx)
Definition: av1_parser.c:200
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:52
static av_cold int av1_parser_init(AVCodecParserContext *ctx)
Definition: av1_parser.c:185
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecContext *avctx)
Read the extradata bitstream found in a codec context into a fragment, then split into units and deco...
Definition: cbs.c:279
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:126
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:296
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:156
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:75
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:170
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
#define s(width, name)
Definition: cbs_vp9.c:257
#define NULL
Definition: coverity.c:32
static AVFrame * frame
@ AV_CODEC_ID_AV1
Definition: codec_id.h:279
@ AV_PICTURE_STRUCTURE_FRAME
Definition: avcodec.h:3374
@ AV_PICTURE_STRUCTURE_UNKNOWN
Definition: avcodec.h:3371
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:273
@ AV_PICTURE_TYPE_SP
Switching Predicted.
Definition: avutil.h:279
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
int i
Definition: input.c:407
@ AV1_OBU_TEMPORAL_DELIMITER
Definition: av1.h:31
@ AV1_OBU_TILE_GROUP
Definition: av1.h:33
@ AV1_OBU_FRAME_HEADER
Definition: av1.h:32
@ AV1_OBU_FRAME
Definition: av1.h:35
@ AV1_OBU_SEQUENCE_HEADER
Definition: av1.h:30
@ AV1_FRAME_KEY
Definition: av1.h:53
@ AV1_FRAME_INTER
Definition: av1.h:54
@ AV1_FRAME_INTRA_ONLY
Definition: av1.h:55
@ AV1_FRAME_SWITCH
Definition: av1.h:56
common internal API header
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:92
const char data[16]
Definition: mxf.c:142
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:569
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:415
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:416
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ 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_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ 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
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:458
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:460
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:483
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:497
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:512
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:513
#define td
Definition: regdef.h:70
#define FF_ARRAY_ELEMS(a)
int type
Definition: av1_parse.h:51
CodedBitstreamFragment temporal_unit
Definition: av1_parser.c:31
CodedBitstreamContext * cbc
Definition: av1_parser.c:30
int parsed_extradata
Definition: av1_parser.c:32
AV1RawFrameHeader header
Definition: cbs_av1.h:304
uint8_t spatial_id
Definition: cbs_av1.h:37
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:398
AV1RawOBUHeader header
Definition: cbs_av1.h:392
AV1RawFrame frame
Definition: cbs_av1.h:399
union AV1RawOBU::@25 obu
uint8_t seq_profile
Definition: cbs_av1.h:74
AV1RawColorConfig color_config
Definition: cbs_av1.h:128
uint8_t seq_level_idx[AV1_MAX_OPERATING_POINTS]
Definition: cbs_av1.h:87
main external API structure.
Definition: avcodec.h:536
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1171
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1150
AVRational framerate
Definition: avcodec.h:2071
int level
level
Definition: avcodec.h:1984
int profile
profile
Definition: avcodec.h:1858
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1164
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1157
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:659
int extradata_size
Definition: avcodec.h:638
int codec_ids[5]
Definition: avcodec.h:3545
void * priv_data
Format private data.
Definition: avformat.h:1260
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
AV1RawSequenceHeader * sequence_header
Definition: cbs_av1.h:430
Context structure for coded bitstream operations.
Definition: cbs.h:170
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:118
Coded bitstream unit structure.
Definition: cbs.h:66
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:103
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:70
#define av_log(a,...)
int out_size
Definition: movenc.c:55
AVFormatContext * ctx
Definition: movenc.c:48
int size
if(ret< 0)
Definition: vf_mcdeint.c:282
int len