FFmpeg  4.1.11
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 {
52  AV1ParseContext *s = ctx->priv_data;
55  int ret;
56 
57  *out_data = data;
58  *out_size = size;
59 
60  ctx->key_frame = -1;
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 
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 (obu->header.spatial_id > 0)
104  continue;
105 
106  if (frame->show_existing_frame) {
108 
109  if (!ref->valid) {
110  av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
111  goto end;
112  }
113 
114  ctx->width = ref->frame_width;
115  ctx->height = ref->frame_height;
116  frame_type = ref->frame_type;
117 
118  ctx->key_frame = 0;
119  } else if (!frame->show_frame) {
120  continue;
121  } else {
122  ctx->width = av1->frame_width;
123  ctx->height = av1->frame_height;
124  frame_type = frame->frame_type;
125 
126  ctx->key_frame = frame_type == AV1_FRAME_KEY;
127  }
128 
129  avctx->profile = seq->seq_profile;
130  avctx->level = seq->seq_level_idx[0];
131 
132  switch (frame_type) {
133  case AV1_FRAME_KEY:
136  break;
137  case AV1_FRAME_INTER:
139  break;
140  case AV1_FRAME_SWITCH:
142  break;
143  }
145 
146  switch (av1->bit_depth) {
147  case 8:
148  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
149  : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
150  break;
151  case 10:
152  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
153  : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
154  break;
155  case 12:
156  ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
157  : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
158  break;
159  }
161  }
162 
163 end:
164  ff_cbs_fragment_uninit(s->cbc, td);
165 
166  s->cbc->log_ctx = NULL;
167 
168  return size;
169 }
170 
177 };
178 
180 {
181  AV1ParseContext *s = ctx->priv_data;
182  int ret;
183 
184  ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
185  if (ret < 0)
186  return ret;
187 
190 
191  return 0;
192 }
193 
195 {
196  AV1ParseContext *s = ctx->priv_data;
197 
198  ff_cbs_close(&s->cbc);
199 }
200 
202  const uint8_t *buf, int buf_size)
203 {
204  AV1OBU obu;
205  const uint8_t *ptr = buf, *end = buf + buf_size;
206 
207  while (ptr < end) {
208  int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
209  if (len < 0)
210  break;
211 
212  if (obu.type == AV1_OBU_FRAME_HEADER ||
213  obu.type == AV1_OBU_FRAME) {
214  return ptr - buf;
215  }
216  ptr += len;
217  buf_size -= len;
218  }
219 
220  return 0;
221 }
222 
224  .codec_ids = { AV_CODEC_ID_AV1 },
225  .priv_data_size = sizeof(AV1ParseContext),
226  .parser_init = av1_parser_init,
227  .parser_close = av1_parser_close,
228  .parser_parse = av1_parser_parse,
230 };
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
int size
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
uint8_t mono_chrome
Definition: cbs_av1.h:44
int width
Dimensions of the decoded video intended for presentation.
Definition: avcodec.h:5195
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
int codec_ids[5]
Definition: avcodec.h:5216
int out_size
Definition: movenc.c:55
static enum AVPixelFormat pix_fmts_10bit[2][2]
Definition: av1_parser.c:38
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:383
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
int profile
profile
Definition: avcodec.h:2859
uint8_t seq_profile
Definition: cbs_av1.h:74
static const CodedBitstreamUnitType decompose_unit_types[]
Definition: av1_parser.c:171
static enum AVPixelFormat pix_fmts_12bit[2][2]
Definition: av1_parser.c:42
Undefined.
Definition: avutil.h:273
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:360
CodedBitstreamFragment temporal_unit
Definition: av1_parser.c:30
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:361
enum AVPictureStructure picture_structure
Indicate whether a picture is coded as a frame, top field or bottom field.
Definition: avcodec.h:5182
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
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:1634
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.
Definition: cbs.h:153
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:384
static int av1_parser_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: av1_parser.c:201
AVCodecParser ff_av1_parser
Definition: av1_parser.c:223
#define av_log(a,...)
static enum AVPixelFormat pix_fmts_8bit[2][2]
Definition: av1_parser.c:34
int parsed_extradata
Definition: av1_parser.c:31
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define td
Definition: regdef.h:70
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:139
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
AV1RawFrame frame
Definition: cbs_av1.h:376
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:382
static av_cold int av1_parser_init(AVCodecParserContext *ctx)
Definition: av1_parser.c:179
static void av1_parser_close(AVCodecParserContext *ctx)
Definition: av1_parser.c:194
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:164
uint8_t frame_type
Definition: cbs_av1.h:139
AV1ReferenceFrameState * ref
Definition: cbs_av1.h:424
AV1RawOBUHeader header
Definition: cbs_av1.h:369
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
int level
level
Definition: avcodec.h:2969
#define FF_ARRAY_ELEMS(a)
uint8_t subsampling_y
Definition: cbs_av1.h:53
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:192
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:1533
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1635
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:379
Switching Predicted.
Definition: avutil.h:279
Context structure for coded bitstream operations.
Definition: cbs.h:159
int type
Definition: av1_parse.h:48
uint8_t show_existing_frame
Definition: cbs_av1.h:134
CodedBitstreamContext * cbc
Definition: av1_parser.c:29
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:113
AV1RawSequenceHeader * sequence_header
Definition: cbs_av1.h:399
uint8_t spatial_id
Definition: cbs_av1.h:37
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:380
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:386
void * priv_data
Internal codec-specific data.
Definition: cbs.h:180
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
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:188
int len
int format
The format of the coded data, corresponds to enum AVPixelFormat for video and for enum AVSampleFormat...
Definition: avcodec.h:5212
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:375
union AV1RawOBU::@47 obu
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:262
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:5097
uint8_t seq_level_idx[AV1_MAX_OPERATING_POINTS]
Definition: cbs_av1.h:87
Predicted.
Definition: avutil.h:275