FFmpeg
Loading...
Searching...
No Matches
evc_parser.c
Go to the documentation of this file.
1/*
2 * EVC format parser
3 *
4 * Copyright (C) 2021 Dawid Kozinski <d.kozinski@samsung.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 "avcodec.h"
24#include "bytestream.h"
25#include "evc.h"
26#include "evc_parse.h"
27#include "parser_internal.h"
28
30
37
38#define NUM_CHROMA_FORMATS 4 // @see ISO_IEC_23094-1 section 6.2 table 2
39
43
47
51
55
59
63
65 const uint8_t *buf, int buf_size)
66{
67 EVCParserContext *ctx = s->priv_data;
69 int nalu_type, tid;
70 int ret;
71
72 if (buf_size <= 0) {
73 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size: (%d)\n", buf_size);
75 }
76
77 ret = init_get_bits8(&gb, buf, buf_size);
78 if (ret < 0)
79 return ret;
80
81 // @see ISO_IEC_23094-1_2020, 7.4.2.2 NAL unit header semantic (Table 4 - NAL unit type codes and NAL unit type classes)
82 // @see enum EVCNALUnitType in evc.h
83 if (get_bits1(&gb)) {// forbidden_zero_bit
84 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit header\n");
86 }
87
88 nalu_type = get_bits(&gb, 6) - 1;
89 if (nalu_type < EVC_NOIDR_NUT || nalu_type > EVC_UNSPEC_NUT62) {
90 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit type: (%d)\n", nalu_type);
92 }
93
94 tid = get_bits(&gb, 3);
95 skip_bits(&gb, 5); // nuh_reserved_zero_5bits
96 skip_bits1(&gb); // nuh_extension_flag
97
98 switch (nalu_type) {
99 case EVC_SPS_NUT:
100 ret = ff_evc_parse_sps(&gb, &ctx->ps);
101 if (ret < 0) {
102 av_log(avctx, AV_LOG_ERROR, "SPS parsing error\n");
103 return ret;
104 }
105 break;
106 case EVC_PPS_NUT:
107 ret = ff_evc_parse_pps(&gb, &ctx->ps);
108 if (ret < 0) {
109 av_log(avctx, AV_LOG_ERROR, "PPS parsing error\n");
110 return ret;
111 }
112 break;
113 case EVC_IDR_NUT: // Coded slice of a IDR or non-IDR picture
114 case EVC_NOIDR_NUT: {
115 const EVCParserPPS *pps;
116 const EVCParserSPS *sps;
118 int bit_depth;
119
120 ret = ff_evc_parse_slice_header(&gb, &sh, &ctx->ps, nalu_type);
121 if (ret < 0) {
122 av_log(avctx, AV_LOG_ERROR, "Slice header parsing error\n");
123 return ret;
124 }
125
126 pps = ctx->ps.pps[sh.slice_pic_parameter_set_id];
127 sps = ctx->ps.sps[pps->pps_seq_parameter_set_id];
128 av_assert0(sps && pps);
129
130 s->coded_width = sps->pic_width_in_luma_samples;
131 s->coded_height = sps->pic_height_in_luma_samples;
132
133 if (sps->picture_cropping_flag) {
134 s->width = sps->pic_width_in_luma_samples - sps->picture_crop_left_offset - sps->picture_crop_right_offset;
135 s->height = sps->pic_height_in_luma_samples - sps->picture_crop_top_offset - sps->picture_crop_bottom_offset;
136 } else {
137 s->width = sps->pic_width_in_luma_samples;
138 s->height = sps->pic_height_in_luma_samples;
139 }
140
141 switch (sh.slice_type) {
142 case EVC_SLICE_TYPE_B: {
143 s->pict_type = AV_PICTURE_TYPE_B;
144 break;
145 }
146 case EVC_SLICE_TYPE_P: {
147 s->pict_type = AV_PICTURE_TYPE_P;
148 break;
149 }
150 case EVC_SLICE_TYPE_I: {
151 s->pict_type = AV_PICTURE_TYPE_I;
152 break;
153 }
154 default: {
155 s->pict_type = AV_PICTURE_TYPE_NONE;
156 }
157 }
158
159 avctx->profile = sps->profile_idc;
160
161 if (sps->vui_parameters_present_flag && sps->vui_parameters.timing_info_present_flag) {
162 int64_t num = sps->vui_parameters.num_units_in_tick;
163 int64_t den = sps->vui_parameters.time_scale;
164 if (num != 0 && den != 0)
165 av_reduce(&avctx->framerate.den, &avctx->framerate.num, num, den, 1 << 30);
166 } else
167 avctx->framerate = (AVRational) { 0, 1 };
168
169 bit_depth = sps->bit_depth_chroma_minus8 + 8;
170 s->format = AV_PIX_FMT_NONE;
171
172 switch (bit_depth) {
173 case 8:
174 s->format = pix_fmts_8bit[sps->chroma_format_idc];
175 break;
176 case 9:
177 s->format = pix_fmts_9bit[sps->chroma_format_idc];
178 break;
179 case 10:
180 s->format = pix_fmts_10bit[sps->chroma_format_idc];
181 break;
182 case 12:
183 s->format = pix_fmts_12bit[sps->chroma_format_idc];
184 break;
185 case 14:
186 s->format = pix_fmts_14bit[sps->chroma_format_idc];
187 break;
188 case 16:
189 s->format = pix_fmts_16bit[sps->chroma_format_idc];
190 break;
191 }
192
193 s->key_frame = (nalu_type == EVC_IDR_NUT) ? 1 : 0;
194
195 // POC (picture order count of the current picture) derivation
196 // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count
197 ret = ff_evc_derive_poc(&ctx->ps, &sh, &ctx->poc, nalu_type, tid);
198 if (ret < 0)
199 return ret;
200
201 s->output_picture_number = ctx->poc.PicOrderCntVal;
202
203 break;
204 }
205 case EVC_SEI_NUT: // Supplemental Enhancement Information
206 case EVC_APS_NUT: // Adaptation parameter set
207 case EVC_FD_NUT: // Filler data
208 default:
209 break;
210 }
211
212 return 0;
213}
214
215/**
216 * Parse NAL units of found picture and decode some basic information.
217 *
218 * @param s codec parser context
219 * @param avctx codec context
220 * @param buf buffer with field/frame data
221 * @param buf_size size of the buffer
222 */
223static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
224{
225 const uint8_t *data = buf;
226 int data_size = buf_size;
227
228 while (data_size > 0) {
229 int nalu_size = 0;
230 int ret;
231
232 // Buffer size is not enough for buffer to store NAL unit 4-bytes prefix (length)
233 if (data_size < EVC_NALU_LENGTH_PREFIX_SIZE)
234 return AVERROR_INVALIDDATA;
235
236 nalu_size = evc_read_nal_unit_length(data, data_size, avctx);
237
238
240 data_size -= EVC_NALU_LENGTH_PREFIX_SIZE;
241
242 if (data_size < nalu_size)
243 return AVERROR_INVALIDDATA;
244
245 ret = parse_nal_unit(s, avctx, data, nalu_size);
246 if (ret < 0) {
247 av_log(avctx, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
248 return AVERROR_INVALIDDATA;
249 }
250
251 data += nalu_size;
252 data_size -= nalu_size;
253 }
254 return 0;
255}
256
257// Decoding nal units from evcC (EVCDecoderConfigurationRecord)
258// @see @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.2
260{
261 const uint8_t *data = avctx->extradata;
262 int size = avctx->extradata_size;
263 int ret = 0;
265
267
268 if (!data || size <= 0)
269 return -1;
270
271 // extradata is encoded as evcC format.
272 if (data[0] == 1) {
273 int num_of_arrays; // indicates the number of arrays of NAL units of the indicated type(s)
274
275 int nalu_length_field_size; // indicates the length in bytes of the NALUnitLenght field in EVC video stream sample in the stream
276 // The value of this field shall be one of 0, 1, or 3 corresponding to a length encoded with 1, 2, or 4 bytes, respectively.
277
278 if (bytestream2_get_bytes_left(&gb) < 18) {
279 av_log(avctx, AV_LOG_ERROR, "evcC %d too short\n", size);
280 return AVERROR_INVALIDDATA;
281 }
282
283 bytestream2_skip(&gb, 16);
284
285 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
286 // LengthSizeMinusOne plus 1 indicates the length in bytes of the NALUnitLength field in a EVC video stream sample in the stream to which this configuration record applies. For example, a size of one byte is indicated with a value of 0.
287 // The value of this field shall be one of 0, 1, or 3 corresponding to a length encoded with 1, 2, or 4 bytes, respectively.
288 nalu_length_field_size = (bytestream2_get_byte(&gb) & 3) + 1;
289 if( nalu_length_field_size != 1 &&
290 nalu_length_field_size != 2 &&
291 nalu_length_field_size != 4 ) {
292 av_log(avctx, AV_LOG_ERROR, "The length in bytes of the NALUnitLenght field in a EVC video stream has unsupported value of %d\n", nalu_length_field_size);
293 return AVERROR_INVALIDDATA;
294 }
295
296 num_of_arrays = bytestream2_get_byte(&gb);
297
298 /* Decode nal units from evcC. */
299 for (int i = 0; i < num_of_arrays; i++) {
300
301 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
302 // NAL_unit_type indicates the type of the NAL units in the following array (which shall be all of that type);
303 // - it takes a value as defined in ISO/IEC 23094-1;
304 // - it is restricted to take one of the values indicating a SPS, PPS, APS, or SEI NAL unit.
305 int nal_unit_type = bytestream2_get_byte(&gb) & 0x3f;
306 int num_nalus = bytestream2_get_be16(&gb);
307
308 for (int j = 0; j < num_nalus; j++) {
309
310 int nal_unit_length = bytestream2_get_be16(&gb);
311
312 if (bytestream2_get_bytes_left(&gb) < nal_unit_length) {
313 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size in extradata.\n");
314 return AVERROR_INVALIDDATA;
315 }
316
317 if( nal_unit_type == EVC_SPS_NUT ||
318 nal_unit_type == EVC_PPS_NUT ||
319 nal_unit_type == EVC_APS_NUT ||
320 nal_unit_type == EVC_SEI_NUT ) {
321 if (parse_nal_unit(s, avctx, gb.buffer, nal_unit_length) != 0) {
322 av_log(avctx, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
323 return AVERROR_INVALIDDATA;
324 }
325 }
326
327 bytestream2_skip(&gb, nal_unit_length);
328 }
329 }
330 } else
331 return -1;
332
333 return ret;
334}
335
337 const uint8_t **poutbuf, int *poutbuf_size,
338 const uint8_t *buf, int buf_size)
339{
340 int next;
341 int ret;
342 EVCParserContext *ctx = s->priv_data;
343
344 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
345 s->key_frame = 0;
346
347 if (avctx->extradata && !ctx->parsed_extradata) {
348 decode_extradata(s, avctx);
349 ctx->parsed_extradata = 1;
350 }
351
352 next = buf_size;
353
354 ret = parse_nal_units(s, avctx, buf, buf_size);
355 if(ret < 0) {
356 *poutbuf = NULL;
357 *poutbuf_size = 0;
358 return buf_size;
359 }
360
361 // poutbuf contains just one Access Unit
362 *poutbuf = buf;
363 *poutbuf_size = buf_size;
364
365 return next;
366}
367
369{
370 EVCParserContext *ctx = s->priv_data;
371
372 ff_evc_ps_free(&ctx->ps);
373}
374
377 .priv_data_size = sizeof(EVCParserContext),
378 .parse = evc_parse,
380};
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition af_astats.c:246
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 av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
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
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
uint64_t pps
Definition dovi_rpuenc.c:36
int ff_evc_parse_slice_header(GetBitContext *gb, EVCParserSliceHeader *sh, const EVCParamSets *ps, enum EVCNALUnitType nalu_type)
Definition evc_parse.c:24
int ff_evc_derive_poc(const EVCParamSets *ps, const EVCParserSliceHeader *sh, EVCParserPoc *poc, enum EVCNALUnitType nalu_type, int tid)
Definition evc_parse.c:140
EVC decoder/parser shared code.
static uint32_t evc_read_nal_unit_length(const uint8_t *bits, int bits_size, void *logctx)
Definition evc_parse.h:83
static enum AVPixelFormat pix_fmts_16bit[NUM_CHROMA_FORMATS]
Definition evc_parser.c:60
#define NUM_CHROMA_FORMATS
Definition evc_parser.c:38
static int evc_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition evc_parser.c:336
static int parse_nal_unit(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition evc_parser.c:64
static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse NAL units of found picture and decode some basic information.
Definition evc_parser.c:223
static av_cold void evc_parser_close(AVCodecParserContext *s)
Definition evc_parser.c:368
const FFCodecParser ff_evc_parser
Definition evc_parser.c:375
static enum AVPixelFormat pix_fmts_14bit[NUM_CHROMA_FORMATS]
Definition evc_parser.c:56
static enum AVPixelFormat pix_fmts_9bit[NUM_CHROMA_FORMATS]
Definition evc_parser.c:44
static int decode_extradata(AVCodecParserContext *s, AVCodecContext *avctx)
Definition evc_parser.c:259
int ff_evc_parse_pps(GetBitContext *gb, EVCParamSets *ps)
Definition evc_ps.c:352
void ff_evc_ps_free(EVCParamSets *ps)
Definition evc_ps.c:437
int ff_evc_parse_sps(GetBitContext *gb, EVCParamSets *ps)
Definition evc_ps.c:152
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
@ AV_CODEC_ID_EVC
Definition codec_id.h:316
@ AV_PICTURE_STRUCTURE_FRAME
coded as frame
Definition avcodec.h:2614
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#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_P
Predicted.
Definition avutil.h:279
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
@ EVC_UNSPEC_NUT62
Definition evc.h:96
@ EVC_SPS_NUT
Definition evc.h:58
@ EVC_APS_NUT
Definition evc.h:60
@ EVC_SEI_NUT
Definition evc.h:62
@ EVC_PPS_NUT
Definition evc.h:59
@ EVC_IDR_NUT
Definition evc.h:35
@ EVC_FD_NUT
Definition evc.h:61
@ EVC_NOIDR_NUT
Definition evc.h:34
#define EVC_NALU_LENGTH_PREFIX_SIZE
Definition evc.h:26
@ EVC_SLICE_TYPE_B
Definition evc.h:103
@ EVC_SLICE_TYPE_P
Definition evc.h:104
@ EVC_SLICE_TYPE_I
Definition evc.h:105
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_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
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
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
main external API structure.
Definition avcodec.h:443
AVRational framerate
Definition avcodec.h:563
int profile
profile
Definition avcodec.h:1636
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
EVCParamSets ps
Definition evc_parser.c:32
EVCParserPoc poc
Definition evc_parser.c:33
uint8_t slice_pic_parameter_set_id
Definition evc_parse.h:43
const uint8_t * buffer
Definition bytestream.h:34
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
int size