FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/evc_parser.c
Date: 2026-01-16 07:34:38
Exec Total Coverage
Lines: 112 169 66.3%
Functions: 5 5 100.0%
Branches: 49 91 53.8%

Line Branch Exec Source
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
29 #include "libavutil/attributes.h"
30
31 typedef struct EVCParserContext {
32 EVCParamSets ps;
33 EVCParserPoc poc;
34
35 int parsed_extradata;
36 } EVCParserContext;
37
38 #define NUM_CHROMA_FORMATS 4 // @see ISO_IEC_23094-1 section 6.2 table 2
39
40 static const enum AVPixelFormat pix_fmts_8bit[NUM_CHROMA_FORMATS] = {
41 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
42 };
43
44 static const enum AVPixelFormat pix_fmts_9bit[NUM_CHROMA_FORMATS] = {
45 AV_PIX_FMT_GRAY9, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9
46 };
47
48 static const enum AVPixelFormat pix_fmts_10bit[NUM_CHROMA_FORMATS] = {
49 AV_PIX_FMT_GRAY10, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10
50 };
51
52 static const enum AVPixelFormat pix_fmts_12bit[NUM_CHROMA_FORMATS] = {
53 AV_PIX_FMT_GRAY12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12
54 };
55
56 static const enum AVPixelFormat pix_fmts_14bit[NUM_CHROMA_FORMATS] = {
57 AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14
58 };
59
60 static const enum AVPixelFormat pix_fmts_16bit[NUM_CHROMA_FORMATS] = {
61 AV_PIX_FMT_GRAY16, AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16
62 };
63
64 609 static int parse_nal_unit(AVCodecParserContext *s, AVCodecContext *avctx,
65 const uint8_t *buf, int buf_size)
66 {
67 609 EVCParserContext *ctx = s->priv_data;
68 GetBitContext gb;
69 int nalu_type, tid;
70 int ret;
71
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 609 times.
609 if (buf_size <= 0) {
73 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size: (%d)\n", buf_size);
74 return AVERROR_INVALIDDATA;
75 }
76
77 609 ret = init_get_bits8(&gb, buf, buf_size);
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 609 times.
609 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 609 times.
609 if (get_bits1(&gb)) {// forbidden_zero_bit
84 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit header\n");
85 return AVERROR_INVALIDDATA;
86 }
87
88 609 nalu_type = get_bits(&gb, 6) - 1;
89
2/4
✓ Branch 0 taken 609 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 609 times.
609 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);
91 return AVERROR_INVALIDDATA;
92 }
93
94 609 tid = get_bits(&gb, 3);
95 609 skip_bits(&gb, 5); // nuh_reserved_zero_5bits
96 609 skip_bits1(&gb); // nuh_extension_flag
97
98
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 600 times.
✓ Branch 3 taken 3 times.
609 switch (nalu_type) {
99 3 case EVC_SPS_NUT:
100 3 ret = ff_evc_parse_sps(&gb, &ctx->ps);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
102 av_log(avctx, AV_LOG_ERROR, "SPS parsing error\n");
103 return ret;
104 }
105 3 break;
106 3 case EVC_PPS_NUT:
107 3 ret = ff_evc_parse_pps(&gb, &ctx->ps);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
109 av_log(avctx, AV_LOG_ERROR, "PPS parsing error\n");
110 return ret;
111 }
112 3 break;
113 600 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;
117 EVCParserSliceHeader sh;
118 int bit_depth;
119
120 600 ret = ff_evc_parse_slice_header(&gb, &sh, &ctx->ps, nalu_type);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (ret < 0) {
122 av_log(avctx, AV_LOG_ERROR, "Slice header parsing error\n");
123 return ret;
124 }
125
126 600 pps = ctx->ps.pps[sh.slice_pic_parameter_set_id];
127 600 sps = ctx->ps.sps[pps->pps_seq_parameter_set_id];
128
2/4
✓ Branch 0 taken 600 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 600 times.
600 av_assert0(sps && pps);
129
130 600 s->coded_width = sps->pic_width_in_luma_samples;
131 600 s->coded_height = sps->pic_height_in_luma_samples;
132
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 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 600 s->width = sps->pic_width_in_luma_samples;
138 600 s->height = sps->pic_height_in_luma_samples;
139 }
140
141
2/4
✓ Branch 0 taken 598 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
600 switch (sh.slice_type) {
142 598 case EVC_SLICE_TYPE_B: {
143 598 s->pict_type = AV_PICTURE_TYPE_B;
144 598 break;
145 }
146 case EVC_SLICE_TYPE_P: {
147 s->pict_type = AV_PICTURE_TYPE_P;
148 break;
149 }
150 2 case EVC_SLICE_TYPE_I: {
151 2 s->pict_type = AV_PICTURE_TYPE_I;
152 2 break;
153 }
154 default: {
155 s->pict_type = AV_PICTURE_TYPE_NONE;
156 }
157 }
158
159 600 avctx->profile = sps->profile_idc;
160
161
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
600 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 600 avctx->framerate = (AVRational) { 0, 1 };
168
169 600 bit_depth = sps->bit_depth_chroma_minus8 + 8;
170 600 s->format = AV_PIX_FMT_NONE;
171
172
1/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 600 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
600 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 600 case 10:
180 600 s->format = pix_fmts_10bit[sps->chroma_format_idc];
181 600 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 600 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 600 ret = ff_evc_derive_poc(&ctx->ps, &sh, &ctx->poc, nalu_type, tid);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (ret < 0)
199 return ret;
200
201 600 s->output_picture_number = ctx->poc.PicOrderCntVal;
202
203 600 break;
204 }
205 3 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 3 break;
210 }
211
212 609 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 */
223 602 static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
224 {
225 602 const uint8_t *data = buf;
226 602 int data_size = buf_size;
227
228
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 602 times.
1208 while (data_size > 0) {
229 606 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (data_size < EVC_NALU_LENGTH_PREFIX_SIZE)
234 return AVERROR_INVALIDDATA;
235
236 606 nalu_size = evc_read_nal_unit_length(data, data_size, avctx);
237
238
239 606 data += EVC_NALU_LENGTH_PREFIX_SIZE;
240 606 data_size -= EVC_NALU_LENGTH_PREFIX_SIZE;
241
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (data_size < nalu_size)
243 return AVERROR_INVALIDDATA;
244
245 606 ret = parse_nal_unit(s, avctx, data, nalu_size);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (ret < 0) {
247 av_log(avctx, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
248 return AVERROR_INVALIDDATA;
249 }
250
251 606 data += nalu_size;
252 606 data_size -= nalu_size;
253 }
254 602 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
259 1 static int decode_extradata(AVCodecParserContext *s, AVCodecContext *avctx)
260 {
261 1 const uint8_t *data = avctx->extradata;
262 1 int size = avctx->extradata_size;
263 1 int ret = 0;
264 GetByteContext gb;
265
266 1 bytestream2_init(&gb, data, size);
267
268
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!data || size <= 0)
269 return -1;
270
271 // extradata is encoded as evcC format.
272
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 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 1 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 1 nalu_length_field_size = (bytestream2_get_byte(&gb) & 3) + 1;
289
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if( nalu_length_field_size != 1 &&
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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 1 num_of_arrays = bytestream2_get_byte(&gb);
297
298 /* Decode nal units from evcC. */
299
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 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 3 int nal_unit_type = bytestream2_get_byte(&gb) & 0x3f;
306 3 int num_nalus = bytestream2_get_be16(&gb);
307
308
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (int j = 0; j < num_nalus; j++) {
309
310 3 int nal_unit_length = bytestream2_get_be16(&gb);
311
312
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 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
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 if( nal_unit_type == EVC_SPS_NUT ||
318
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 nal_unit_type == EVC_PPS_NUT ||
319
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 nal_unit_type == EVC_APS_NUT ||
320 nal_unit_type == EVC_SEI_NUT ) {
321
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 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 3 bytestream2_skip(&gb, nal_unit_length);
328 }
329 }
330 } else
331 return -1;
332
333 1 return ret;
334 }
335
336 602 static int evc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
337 const uint8_t **poutbuf, int *poutbuf_size,
338 const uint8_t *buf, int buf_size)
339 {
340 int next;
341 int ret;
342 602 EVCParserContext *ctx = s->priv_data;
343
344 602 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
345 602 s->key_frame = 0;
346
347
4/4
✓ Branch 0 taken 301 times.
✓ Branch 1 taken 301 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 300 times.
602 if (avctx->extradata && !ctx->parsed_extradata) {
348 1 decode_extradata(s, avctx);
349 1 ctx->parsed_extradata = 1;
350 }
351
352 602 next = buf_size;
353
354 602 ret = parse_nal_units(s, avctx, buf, buf_size);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 602 times.
602 if(ret < 0) {
356 *poutbuf = NULL;
357 *poutbuf_size = 0;
358 return buf_size;
359 }
360
361 // poutbuf contains just one Access Unit
362 602 *poutbuf = buf;
363 602 *poutbuf_size = buf_size;
364
365 602 return next;
366 }
367
368 2 static av_cold void evc_parser_close(AVCodecParserContext *s)
369 {
370 2 EVCParserContext *ctx = s->priv_data;
371
372 2 ff_evc_ps_free(&ctx->ps);
373 2 }
374
375 const FFCodecParser ff_evc_parser = {
376 PARSER_CODEC_LIST(AV_CODEC_ID_EVC),
377 .priv_data_size = sizeof(EVCParserContext),
378 .parse = evc_parse,
379 .close = evc_parser_close,
380 };
381