FFmpeg
Loading...
Searching...
No Matches
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
24#include "libavutil/avassert.h"
25
26#include "av1_parse.h"
27#include "avcodec.h"
28#include "cbs.h"
29#include "cbs_av1.h"
30#include "parser_internal.h"
31
37
50
54
56 AVCodecContext *avctx,
57 const uint8_t **out_data, int *out_size,
58 const uint8_t *data, int size)
59{
60 AV1ParseContext *s = ctx->priv_data;
61 CodedBitstreamFragment *td = &s->temporal_unit;
62 const CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
63 const AV1RawSequenceHeader *seq;
65 int ret;
66
67 *out_data = data;
68 *out_size = size;
69
70 ctx->key_frame = -1;
71 ctx->pict_type = AV_PICTURE_TYPE_NONE;
72 ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
73
74 s->cbc->log_ctx = avctx;
75
76 if (avctx->extradata_size && !s->parsed_extradata) {
77 s->parsed_extradata = 1;
78
79 ret = ff_cbs_read_extradata_from_codec(s->cbc, td, avctx);
80 if (ret < 0) {
81 av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
82 }
83
84 ff_cbs_fragment_reset(td);
85 }
86
87 ret = ff_cbs_read(s->cbc, td, NULL, data, size);
88 if (ret < 0) {
89 av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
90 goto end;
91 }
92
93 if (!av1->sequence_header) {
94 av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
95 goto end;
96 }
97
98 seq = av1->sequence_header;
99 color = &seq->color_config;
100
101 for (int i = 0; i < td->nb_units; i++) {
102 const CodedBitstreamUnit *unit = &td->units[i];
103 const AV1RawOBU *obu = unit->content;
105
106 if (unit->type == AV1_OBU_FRAME)
107 frame = &obu->obu.frame.header;
108 else if (unit->type == AV1_OBU_FRAME_HEADER)
109 frame = &obu->obu.frame_header;
110 else
111 continue;
112
113 if (obu->header.spatial_id > 0)
114 continue;
115
116 if (!frame->show_frame && !frame->show_existing_frame)
117 continue;
118
119 ctx->width = frame->frame_width_minus_1 + 1;
120 ctx->height = frame->frame_height_minus_1 + 1;
121
122 ctx->key_frame = frame->frame_type == AV1_FRAME_KEY && !frame->show_existing_frame;
123
124 switch (frame->frame_type) {
125 case AV1_FRAME_KEY:
127 ctx->pict_type = AV_PICTURE_TYPE_I;
128 break;
129 case AV1_FRAME_INTER:
130 ctx->pict_type = AV_PICTURE_TYPE_P;
131 break;
132 case AV1_FRAME_SWITCH:
133 ctx->pict_type = AV_PICTURE_TYPE_SP;
134 break;
135 }
136 ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
137
138 /* Extract SAR from render_height_minus_1 & render_width_minus_1 */
140 &avctx->sample_aspect_ratio.den,
141 (int64_t)ctx->height * (frame->render_width_minus_1 + 1),
142 (int64_t)ctx->width * (frame->render_height_minus_1 + 1),
143 INT_MAX);
144 }
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 }
160 av_assert2(ctx->format != AV_PIX_FMT_NONE);
161
162 if (!color->subsampling_x && !color->subsampling_y &&
163 color->matrix_coefficients == AVCOL_SPC_RGB &&
164 color->color_primaries == AVCOL_PRI_BT709 &&
165 color->transfer_characteristics == AVCOL_TRC_IEC61966_2_1)
166 ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit];
167
168 avctx->profile = seq->seq_profile;
169 avctx->level = seq->seq_level_idx[0];
170
171 avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
172 avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
173 avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
174 avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
175
180
181end:
182 ff_cbs_fragment_reset(td);
183
184 s->cbc->log_ctx = NULL;
185
186 return size;
187}
188
197
199{
200 AV1ParseContext *s = ctx->priv_data;
201 int ret;
202
203 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
204 if (ret < 0)
205 return ret;
206
207 s->cbc->decompose_unit_types = decompose_unit_types;
208 s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
209
210 return 0;
211}
212
214{
215 AV1ParseContext *s = ctx->priv_data;
216
217 ff_cbs_fragment_free(&s->temporal_unit);
218 ff_cbs_close(&s->cbc);
219}
220
static int out_size
static AVFormatContext * ctx
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static const CodedBitstreamUnitType decompose_unit_types[]
Definition apv_parser.c:178
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
AVRational ff_av1_framerate(int64_t ticks_per_frame, int64_t units_per_tick, int64_t time_scale)
Definition av1_parse.c:110
static enum AVPixelFormat pix_fmts_rgb[3]
Definition av1_parser.c:51
static enum AVPixelFormat pix_fmts_12bit[2][2]
Definition av1_parser.c:46
static enum AVPixelFormat pix_fmts_8bit[2][2]
Definition av1_parser.c:38
static enum AVPixelFormat pix_fmts_10bit[2][2]
Definition av1_parser.c:42
static av_cold void av1_parser_close(AVCodecParserContext *ctx)
Definition av1_parser.c:213
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:55
static av_cold int av1_parser_init(AVCodecParserContext *ctx)
Definition av1_parser.c:198
const FFCodecParser ff_av1_parser
Definition av1_parser.c:221
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_PICTURE_STRUCTURE_FRAME
coded as frame
Definition avcodec.h:2614
@ AV_PICTURE_STRUCTURE_UNKNOWN
unknown
Definition avcodec.h:2611
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
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
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition avutil.h:277
@ AV_PICTURE_TYPE_SP
Switching Predicted.
Definition avutil.h:283
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
@ AV1_OBU_TEMPORAL_DELIMITER
Definition av1.h:31
@ AV1_OBU_TILE_GROUP
Definition av1.h:33
@ AV1_OBU_REDUNDANT_FRAME_HEADER
Definition av1.h:36
@ 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
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition cbs.h:66
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
const char data[16]
Definition mxf.c:149
#define PARSER_CODEC_LIST(...)
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:642
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition pixfmt.h:644
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:672
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition pixfmt.h:686
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:706
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition pixfmt.h:707
#define FF_ARRAY_ELEMS(a)
CodedBitstreamFragment temporal_unit
Definition av1_parser.c:34
CodedBitstreamContext * cbc
Definition av1_parser.c:33
AV1RawFrameHeader header
Definition cbs_av1.h:319
uint8_t spatial_id
Definition cbs_av1.h:46
AV1RawFrameHeader frame_header
Definition cbs_av1.h:420
AV1RawOBUHeader header
Definition cbs_av1.h:414
AV1RawFrame frame
Definition cbs_av1.h:421
union AV1RawOBU::@016362317061177152367125047142162076164261250366 obu
uint8_t timing_info_present_flag
Definition cbs_av1.h:87
uint8_t seq_profile
Definition cbs_av1.h:83
AV1RawColorConfig color_config
Definition cbs_av1.h:137
AV1RawTimingInfo timing_info
Definition cbs_av1.h:92
uint8_t seq_level_idx[AV1_MAX_OPERATING_POINTS]
Definition cbs_av1.h:96
uint32_t time_scale
Definition cbs_av1.h:69
uint32_t num_units_in_display_tick
Definition cbs_av1.h:68
uint32_t num_ticks_per_picture_minus_1
Definition cbs_av1.h:72
main external API structure.
Definition avcodec.h:443
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:657
AVRational framerate
Definition avcodec.h:563
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition avcodec.h:628
int level
Encoding level descriptor.
Definition avcodec.h:1646
int profile
profile
Definition avcodec.h:1636
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
int extradata_size
Definition avcodec.h:527
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
AV1RawSequenceHeader * sequence_header
Definition cbs_av1.h:460
Context structure for coded bitstream operations.
Definition cbs.h:226
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition cbs.h:175
int nb_units
Number of units in this fragment.
Definition cbs.h:160
Coded bitstream unit structure.
Definition cbs.h:77
void * content
Pointer to the decomposed form of this unit.
Definition cbs.h:114
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition cbs.h:81
#define av_log(a,...)
int size