FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/evc_parser.c
Date: 2025-10-10 03:51:19
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
28 #include "libavutil/attributes.h"
29
30 typedef struct EVCParserContext {
31 EVCParamSets ps;
32 EVCParserPoc poc;
33
34 int parsed_extradata;
35 } EVCParserContext;
36
37 #define NUM_CHROMA_FORMATS 4 // @see ISO_IEC_23094-1 section 6.2 table 2
38
39 static const enum AVPixelFormat pix_fmts_8bit[NUM_CHROMA_FORMATS] = {
40 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
41 };
42
43 static const enum AVPixelFormat pix_fmts_9bit[NUM_CHROMA_FORMATS] = {
44 AV_PIX_FMT_GRAY9, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9
45 };
46
47 static const enum AVPixelFormat pix_fmts_10bit[NUM_CHROMA_FORMATS] = {
48 AV_PIX_FMT_GRAY10, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10
49 };
50
51 static const enum AVPixelFormat pix_fmts_12bit[NUM_CHROMA_FORMATS] = {
52 AV_PIX_FMT_GRAY12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12
53 };
54
55 static const enum AVPixelFormat pix_fmts_14bit[NUM_CHROMA_FORMATS] = {
56 AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14
57 };
58
59 static const enum AVPixelFormat pix_fmts_16bit[NUM_CHROMA_FORMATS] = {
60 AV_PIX_FMT_GRAY16, AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16
61 };
62
63 609 static int parse_nal_unit(AVCodecParserContext *s, AVCodecContext *avctx,
64 const uint8_t *buf, int buf_size)
65 {
66 609 EVCParserContext *ctx = s->priv_data;
67 GetBitContext gb;
68 int nalu_type, tid;
69 int ret;
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 609 times.
609 if (buf_size <= 0) {
72 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size: (%d)\n", buf_size);
73 return AVERROR_INVALIDDATA;
74 }
75
76 609 ret = init_get_bits8(&gb, buf, buf_size);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 609 times.
609 if (ret < 0)
78 return ret;
79
80 // @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)
81 // @see enum EVCNALUnitType in evc.h
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 609 times.
609 if (get_bits1(&gb)) {// forbidden_zero_bit
83 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit header\n");
84 return AVERROR_INVALIDDATA;
85 }
86
87 609 nalu_type = get_bits(&gb, 6) - 1;
88
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) {
89 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit type: (%d)\n", nalu_type);
90 return AVERROR_INVALIDDATA;
91 }
92
93 609 tid = get_bits(&gb, 3);
94 609 skip_bits(&gb, 5); // nuh_reserved_zero_5bits
95 609 skip_bits1(&gb); // nuh_extension_flag
96
97
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) {
98 3 case EVC_SPS_NUT:
99 3 ret = ff_evc_parse_sps(&gb, &ctx->ps);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
101 av_log(avctx, AV_LOG_ERROR, "SPS parsing error\n");
102 return ret;
103 }
104 3 break;
105 3 case EVC_PPS_NUT:
106 3 ret = ff_evc_parse_pps(&gb, &ctx->ps);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
108 av_log(avctx, AV_LOG_ERROR, "PPS parsing error\n");
109 return ret;
110 }
111 3 break;
112 600 case EVC_IDR_NUT: // Coded slice of a IDR or non-IDR picture
113 case EVC_NOIDR_NUT: {
114 const EVCParserPPS *pps;
115 const EVCParserSPS *sps;
116 EVCParserSliceHeader sh;
117 int bit_depth;
118
119 600 ret = ff_evc_parse_slice_header(&gb, &sh, &ctx->ps, nalu_type);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (ret < 0) {
121 av_log(avctx, AV_LOG_ERROR, "Slice header parsing error\n");
122 return ret;
123 }
124
125 600 pps = ctx->ps.pps[sh.slice_pic_parameter_set_id];
126 600 sps = ctx->ps.sps[pps->pps_seq_parameter_set_id];
127
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);
128
129 600 s->coded_width = sps->pic_width_in_luma_samples;
130 600 s->coded_height = sps->pic_height_in_luma_samples;
131
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (sps->picture_cropping_flag) {
133 s->width = sps->pic_width_in_luma_samples - sps->picture_crop_left_offset - sps->picture_crop_right_offset;
134 s->height = sps->pic_height_in_luma_samples - sps->picture_crop_top_offset - sps->picture_crop_bottom_offset;
135 } else {
136 600 s->width = sps->pic_width_in_luma_samples;
137 600 s->height = sps->pic_height_in_luma_samples;
138 }
139
140
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) {
141 598 case EVC_SLICE_TYPE_B: {
142 598 s->pict_type = AV_PICTURE_TYPE_B;
143 598 break;
144 }
145 case EVC_SLICE_TYPE_P: {
146 s->pict_type = AV_PICTURE_TYPE_P;
147 break;
148 }
149 2 case EVC_SLICE_TYPE_I: {
150 2 s->pict_type = AV_PICTURE_TYPE_I;
151 2 break;
152 }
153 default: {
154 s->pict_type = AV_PICTURE_TYPE_NONE;
155 }
156 }
157
158 600 avctx->profile = sps->profile_idc;
159
160
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) {
161 int64_t num = sps->vui_parameters.num_units_in_tick;
162 int64_t den = sps->vui_parameters.time_scale;
163 if (num != 0 && den != 0)
164 av_reduce(&avctx->framerate.den, &avctx->framerate.num, num, den, 1 << 30);
165 } else
166 600 avctx->framerate = (AVRational) { 0, 1 };
167
168 600 bit_depth = sps->bit_depth_chroma_minus8 + 8;
169 600 s->format = AV_PIX_FMT_NONE;
170
171
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) {
172 case 8:
173 s->format = pix_fmts_8bit[sps->chroma_format_idc];
174 break;
175 case 9:
176 s->format = pix_fmts_9bit[sps->chroma_format_idc];
177 break;
178 600 case 10:
179 600 s->format = pix_fmts_10bit[sps->chroma_format_idc];
180 600 break;
181 case 12:
182 s->format = pix_fmts_12bit[sps->chroma_format_idc];
183 break;
184 case 14:
185 s->format = pix_fmts_14bit[sps->chroma_format_idc];
186 break;
187 case 16:
188 s->format = pix_fmts_16bit[sps->chroma_format_idc];
189 break;
190 }
191
192 600 s->key_frame = (nalu_type == EVC_IDR_NUT) ? 1 : 0;
193
194 // POC (picture order count of the current picture) derivation
195 // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count
196 600 ret = ff_evc_derive_poc(&ctx->ps, &sh, &ctx->poc, nalu_type, tid);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (ret < 0)
198 return ret;
199
200 600 s->output_picture_number = ctx->poc.PicOrderCntVal;
201
202 600 break;
203 }
204 3 case EVC_SEI_NUT: // Supplemental Enhancement Information
205 case EVC_APS_NUT: // Adaptation parameter set
206 case EVC_FD_NUT: // Filler data
207 default:
208 3 break;
209 }
210
211 609 return 0;
212 }
213
214 /**
215 * Parse NAL units of found picture and decode some basic information.
216 *
217 * @param s codec parser context
218 * @param avctx codec context
219 * @param buf buffer with field/frame data
220 * @param buf_size size of the buffer
221 */
222 602 static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
223 {
224 602 const uint8_t *data = buf;
225 602 int data_size = buf_size;
226
227
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 602 times.
1208 while (data_size > 0) {
228 606 int nalu_size = 0;
229 int ret;
230
231 // Buffer size is not enough for buffer to store NAL unit 4-bytes prefix (length)
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (data_size < EVC_NALU_LENGTH_PREFIX_SIZE)
233 return AVERROR_INVALIDDATA;
234
235 606 nalu_size = evc_read_nal_unit_length(data, data_size, avctx);
236
237
238 606 data += EVC_NALU_LENGTH_PREFIX_SIZE;
239 606 data_size -= EVC_NALU_LENGTH_PREFIX_SIZE;
240
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (data_size < nalu_size)
242 return AVERROR_INVALIDDATA;
243
244 606 ret = parse_nal_unit(s, avctx, data, nalu_size);
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (ret < 0) {
246 av_log(avctx, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
247 return AVERROR_INVALIDDATA;
248 }
249
250 606 data += nalu_size;
251 606 data_size -= nalu_size;
252 }
253 602 return 0;
254 }
255
256 // Decoding nal units from evcC (EVCDecoderConfigurationRecord)
257 // @see @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.2
258 1 static int decode_extradata(AVCodecParserContext *s, AVCodecContext *avctx)
259 {
260 1 const uint8_t *data = avctx->extradata;
261 1 int size = avctx->extradata_size;
262 1 int ret = 0;
263 GetByteContext gb;
264
265 1 bytestream2_init(&gb, data, size);
266
267
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)
268 return -1;
269
270 // extradata is encoded as evcC format.
271
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (data[0] == 1) {
272 int num_of_arrays; // indicates the number of arrays of NAL units of the indicated type(s)
273
274 int nalu_length_field_size; // indicates the length in bytes of the NALUnitLenght field in EVC video stream sample in the stream
275 // 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.
276
277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (bytestream2_get_bytes_left(&gb) < 18) {
278 av_log(avctx, AV_LOG_ERROR, "evcC %d too short\n", size);
279 return AVERROR_INVALIDDATA;
280 }
281
282 1 bytestream2_skip(&gb, 16);
283
284 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
285 // 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.
286 // 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.
287 1 nalu_length_field_size = (bytestream2_get_byte(&gb) & 3) + 1;
288
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 &&
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 nalu_length_field_size != 2 &&
290 nalu_length_field_size != 4 ) {
291 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);
292 return AVERROR_INVALIDDATA;
293 }
294
295 1 num_of_arrays = bytestream2_get_byte(&gb);
296
297 /* Decode nal units from evcC. */
298
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = 0; i < num_of_arrays; i++) {
299
300 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
301 // NAL_unit_type indicates the type of the NAL units in the following array (which shall be all of that type);
302 // - it takes a value as defined in ISO/IEC 23094-1;
303 // - it is restricted to take one of the values indicating a SPS, PPS, APS, or SEI NAL unit.
304 3 int nal_unit_type = bytestream2_get_byte(&gb) & 0x3f;
305 3 int num_nalus = bytestream2_get_be16(&gb);
306
307
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (int j = 0; j < num_nalus; j++) {
308
309 3 int nal_unit_length = bytestream2_get_be16(&gb);
310
311
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (bytestream2_get_bytes_left(&gb) < nal_unit_length) {
312 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size in extradata.\n");
313 return AVERROR_INVALIDDATA;
314 }
315
316
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 ||
317
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 nal_unit_type == EVC_PPS_NUT ||
318
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 nal_unit_type == EVC_APS_NUT ||
319 nal_unit_type == EVC_SEI_NUT ) {
320
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (parse_nal_unit(s, avctx, gb.buffer, nal_unit_length) != 0) {
321 av_log(avctx, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
322 return AVERROR_INVALIDDATA;
323 }
324 }
325
326 3 bytestream2_skip(&gb, nal_unit_length);
327 }
328 }
329 } else
330 return -1;
331
332 1 return ret;
333 }
334
335 602 static int evc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
336 const uint8_t **poutbuf, int *poutbuf_size,
337 const uint8_t *buf, int buf_size)
338 {
339 int next;
340 int ret;
341 602 EVCParserContext *ctx = s->priv_data;
342
343 602 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
344 602 s->key_frame = 0;
345
346
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) {
347 1 decode_extradata(s, avctx);
348 1 ctx->parsed_extradata = 1;
349 }
350
351 602 next = buf_size;
352
353 602 ret = parse_nal_units(s, avctx, buf, buf_size);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 602 times.
602 if(ret < 0) {
355 *poutbuf = NULL;
356 *poutbuf_size = 0;
357 return buf_size;
358 }
359
360 // poutbuf contains just one Access Unit
361 602 *poutbuf = buf;
362 602 *poutbuf_size = buf_size;
363
364 602 return next;
365 }
366
367 2 static av_cold void evc_parser_close(AVCodecParserContext *s)
368 {
369 2 EVCParserContext *ctx = s->priv_data;
370
371 2 ff_evc_ps_free(&ctx->ps);
372 2 }
373
374 const AVCodecParser ff_evc_parser = {
375 .codec_ids = { AV_CODEC_ID_EVC },
376 .priv_data_size = sizeof(EVCParserContext),
377 .parser_parse = evc_parse,
378 .parser_close = evc_parser_close,
379 };
380