FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/evc.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 149 166 89.8%
Functions: 6 6 100.0%
Branches: 52 73 71.2%

Line Branch Exec Source
1 /*
2 * EVC helper functions for muxers
3 * Copyright (c) 2022 Dawid Kozinski <d.kozinski@samsung.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/mem.h"
23 #include "libavcodec/get_bits.h"
24 #include "libavcodec/golomb.h"
25 #include "libavcodec/evc.h"
26 #include "avio.h"
27 #include "evc.h"
28
29 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.1
30 enum {
31 SPS_INDEX,
32 PPS_INDEX,
33 APS_INDEX,
34 SEI_INDEX,
35 NB_ARRAYS
36 };
37
38 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
39 typedef struct EVCNALUnitArray {
40 uint8_t array_completeness; // when equal to 1 indicates that all NAL units of the given type are in the following array
41 uint8_t NAL_unit_type; // indicates the type of the NAL units in the following array
42 uint16_t numNalus; // indicates the number of NAL units of the indicated type
43 uint16_t *nalUnitLength; // indicates the length in bytes of the NAL unit
44 uint8_t **nalUnit; // contains an SPS, PPS, APS or a SEI NAL unit, as specified in ISO/IEC 23094-1
45 } EVCNALUnitArray;
46
47 /**
48 * @brief Specifies the decoder configuration information for ISO/IEC 23094-1 video content.
49 * @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.2
50 * Carriage of network abstraction layer (NAL) unit structured video in the ISO base media file format
51 */
52 typedef struct EVCDecoderConfigurationRecord {
53 uint8_t configurationVersion; // 8 bits
54 uint8_t profile_idc; // 8 bits
55 uint8_t level_idc; // 8 bits
56 uint32_t toolset_idc_h; // 32 bits
57 uint32_t toolset_idc_l; // 32 bits
58 uint8_t chroma_format_idc; // 2 bits
59 uint8_t bit_depth_luma_minus8; // 3 bits
60 uint8_t bit_depth_chroma_minus8; // 3 bits
61 uint16_t pic_width_in_luma_samples; // 16 bits
62 uint16_t pic_height_in_luma_samples; // 16 bits
63 uint8_t reserved; // 6 bits '000000'b
64 uint8_t lengthSizeMinusOne; // 2 bits
65 uint8_t num_of_arrays; // 8 bits
66 EVCNALUnitArray arrays[NB_ARRAYS];
67 } EVCDecoderConfigurationRecord;
68
69 typedef struct NALU {
70 int offset;
71 uint32_t size;
72 } NALU;
73
74 typedef struct NALUList {
75 NALU *nalus;
76 unsigned nalus_array_size;
77 unsigned nb_nalus; ///< valid entries in nalus
78 } NALUList;
79
80 // @see ISO_IEC_23094-1 (7.3.2.1 SPS RBSP syntax)
81 1 static int evcc_parse_sps(const uint8_t *bs, int bs_size, EVCDecoderConfigurationRecord *evcc)
82 {
83 GetBitContext gb;
84 unsigned sps_seq_parameter_set_id;
85 int ret;
86
87 1 bs += EVC_NALU_HEADER_SIZE;
88 1 bs_size -= EVC_NALU_HEADER_SIZE;
89
90 1 ret = init_get_bits8(&gb, bs, bs_size);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
92 return ret;
93
94 1 sps_seq_parameter_set_id = get_ue_golomb_long(&gb);
95
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (sps_seq_parameter_set_id >= EVC_MAX_SPS_COUNT)
97 return AVERROR_INVALIDDATA;
98
99 // the Baseline profile is indicated by profile_idc eqal to 0
100 // the Main profile is indicated by profile_idc eqal to 1
101 1 evcc->profile_idc = get_bits(&gb, 8);
102
103 1 evcc->level_idc = get_bits(&gb, 8);
104
105 1 evcc->toolset_idc_h = get_bits_long(&gb, 32);
106 1 evcc->toolset_idc_l = get_bits_long(&gb, 32);
107
108 // 0 - monochrome
109 // 1 - 4:2:0
110 // 2 - 4:2:2
111 // 3 - 4:4:4
112 1 evcc->chroma_format_idc = get_ue_golomb_long(&gb);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (evcc->chroma_format_idc > 3)
114 return AVERROR_INVALIDDATA;
115
116 1 evcc->pic_width_in_luma_samples = get_ue_golomb_long(&gb);
117 1 evcc->pic_height_in_luma_samples = get_ue_golomb_long(&gb);
118
119 1 evcc->bit_depth_luma_minus8 = get_ue_golomb_long(&gb);
120 1 evcc->bit_depth_chroma_minus8 = get_ue_golomb_long(&gb);
121 // EVCDecoderConfigurationRecord can't store values > 7. Limit it to bit depth 14.
122
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (evcc->bit_depth_luma_minus8 > 6 || evcc->bit_depth_chroma_minus8 > 6)
123 return AVERROR_INVALIDDATA;
124
125 1 return 0;
126 }
127
128 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
129 3 static int evcc_array_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size,
130 uint8_t nal_type, int ps_array_completeness,
131 EVCNALUnitArray *array)
132 {
133 int ret;
134 3 uint16_t numNalus = array->numNalus;
135
136 3 ret = av_reallocp_array(&array->nalUnit, numNalus + 1, sizeof(uint8_t *));
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
138 return ret;
139
140 3 ret = av_reallocp_array(&array->nalUnitLength, numNalus + 1, sizeof(uint16_t));
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
142 return ret;
143
144 3 array->nalUnit [numNalus] = (uint8_t *)nal_buf;
145 3 array->nalUnitLength[numNalus] = nal_size;
146 3 array->NAL_unit_type = nal_type;
147 3 array->numNalus++;
148
149 /*
150 * When the sample entry name is 'evc1', the default and mandatory value of
151 * array_completeness is 1 for arrays of all types of parameter sets, and 0
152 * for all other arrays.
153 */
154
5/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
3 if (nal_type == EVC_SPS_NUT || nal_type == EVC_PPS_NUT || nal_type == EVC_APS_NUT)
155 2 array->array_completeness = ps_array_completeness;
156
157 3 return 0;
158 }
159
160 1 static void evcc_init(EVCDecoderConfigurationRecord *evcc)
161 {
162 1 memset(evcc, 0, sizeof(EVCDecoderConfigurationRecord));
163 1 evcc->configurationVersion = 1;
164 1 evcc->lengthSizeMinusOne = 3; // 4 bytes
165 1 }
166
167 1 static void evcc_close(EVCDecoderConfigurationRecord *evcc)
168 {
169
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
170 4 EVCNALUnitArray *const array = &evcc->arrays[i];
171 4 array->numNalus = 0;
172 4 av_freep(&array->nalUnit);
173 4 av_freep(&array->nalUnitLength);
174 }
175 1 }
176
177 1 static int evcc_write(AVIOContext *pb, EVCDecoderConfigurationRecord *evcc)
178 {
179 uint16_t sps_count;
180
181 1 av_log(NULL, AV_LOG_TRACE, "configurationVersion: %"PRIu8"\n",
182 1 evcc->configurationVersion);
183 1 av_log(NULL, AV_LOG_TRACE, "profile_idc: %"PRIu8"\n",
184 1 evcc->profile_idc);
185 1 av_log(NULL, AV_LOG_TRACE, "level_idc: %"PRIu8"\n",
186 1 evcc->level_idc);
187 1 av_log(NULL, AV_LOG_TRACE, "toolset_idc_h: %"PRIu32"\n",
188 evcc->toolset_idc_h);
189 1 av_log(NULL, AV_LOG_TRACE, "toolset_idc_l: %"PRIu32"\n",
190 evcc->toolset_idc_l);
191 1 av_log(NULL, AV_LOG_TRACE, "chroma_format_idc: %"PRIu8"\n",
192 1 evcc->chroma_format_idc);
193 1 av_log(NULL, AV_LOG_TRACE, "bit_depth_luma_minus8: %"PRIu8"\n",
194 1 evcc->bit_depth_luma_minus8);
195 1 av_log(NULL, AV_LOG_TRACE, "bit_depth_chroma_minus8: %"PRIu8"\n",
196 1 evcc->bit_depth_chroma_minus8);
197 1 av_log(NULL, AV_LOG_TRACE, "pic_width_in_luma_samples: %"PRIu16"\n",
198 1 evcc->pic_width_in_luma_samples);
199 1 av_log(NULL, AV_LOG_TRACE, "pic_height_in_luma_samples: %"PRIu16"\n",
200 1 evcc->pic_height_in_luma_samples);
201 1 av_log(NULL, AV_LOG_TRACE, "lengthSizeMinusOne: %"PRIu8"\n",
202 1 evcc->lengthSizeMinusOne);
203 1 av_log(NULL, AV_LOG_TRACE, "num_of_arrays: %"PRIu8"\n",
204 1 evcc->num_of_arrays);
205
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
206 4 const EVCNALUnitArray *const array = &evcc->arrays[i];
207
208
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if(array->numNalus == 0)
209 1 continue;
210
211 3 av_log(NULL, AV_LOG_TRACE, "array_completeness[%"PRIu8"]: %"PRIu8"\n",
212 3 i, array->array_completeness);
213 3 av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%"PRIu8"]: %"PRIu8"\n",
214 3 i, array->NAL_unit_type);
215 3 av_log(NULL, AV_LOG_TRACE, "numNalus[%"PRIu8"]: %"PRIu16"\n",
216 3 i, array->numNalus);
217
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for ( unsigned j = 0; j < array->numNalus; j++)
218 3 av_log(NULL, AV_LOG_TRACE,
219 "nalUnitLength[%"PRIu8"][%"PRIu16"]: %"PRIu16"\n",
220 3 i, j, array->nalUnitLength[j]);
221 }
222
223 /*
224 * We need at least one SPS.
225 */
226 1 sps_count = evcc->arrays[SPS_INDEX].numNalus;
227
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!sps_count || sps_count > EVC_MAX_SPS_COUNT)
228 return AVERROR_INVALIDDATA;
229
230 /* unsigned int(8) configurationVersion = 1; */
231 1 avio_w8(pb, evcc->configurationVersion);
232
233 /* unsigned int(8) profile_idc */
234 1 avio_w8(pb, evcc->profile_idc);
235
236 /* unsigned int(8) profile_idc */
237 1 avio_w8(pb, evcc->level_idc);
238
239 /* unsigned int(32) toolset_idc_h */
240 1 avio_wb32(pb, evcc->toolset_idc_h);
241
242 /* unsigned int(32) toolset_idc_l */
243 1 avio_wb32(pb, evcc->toolset_idc_l);
244
245 /*
246 * unsigned int(2) chroma_format_idc;
247 * unsigned int(3) bit_depth_luma_minus8;
248 * unsigned int(3) bit_depth_chroma_minus8;
249 */
250 1 avio_w8(pb, evcc->chroma_format_idc << 6 |
251 1 evcc->bit_depth_luma_minus8 << 3 |
252 1 evcc->bit_depth_chroma_minus8);
253
254 /* unsigned int(16) pic_width_in_luma_samples; */
255 1 avio_wb16(pb, evcc->pic_width_in_luma_samples);
256
257 /* unsigned int(16) pic_width_in_luma_samples; */
258 1 avio_wb16(pb, evcc->pic_height_in_luma_samples);
259
260 /*
261 * bit(6) reserved = '111111'b;
262 * unsigned int(2) chromaFormat;
263 */
264 1 avio_w8(pb, evcc->lengthSizeMinusOne | 0xfc);
265
266 /* unsigned int(8) numOfArrays; */
267 1 avio_w8(pb, evcc->num_of_arrays);
268
269
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
270 4 const EVCNALUnitArray *const array = &evcc->arrays[i];
271
272
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (!array->numNalus)
273 1 continue;
274
275 /*
276 * bit(1) array_completeness;
277 * unsigned int(1) reserved = 0;
278 * unsigned int(6) NAL_unit_type;
279 */
280 3 avio_w8(pb, array->array_completeness << 7 |
281 3 array->NAL_unit_type & 0x3f);
282
283 /* unsigned int(16) numNalus; */
284 3 avio_wb16(pb, array->numNalus);
285
286
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (unsigned j = 0; j < array->numNalus; j++) {
287 /* unsigned int(16) nalUnitLength; */
288 3 avio_wb16(pb, array->nalUnitLength[j]);
289
290 /* bit(8*nalUnitLength) nalUnit; */
291 3 avio_write(pb, array->nalUnit[j],
292 3 array->nalUnitLength[j]);
293 }
294 }
295
296 1 return 0;
297 }
298
299 1 int ff_isom_write_evcc(AVIOContext *pb, const uint8_t *data,
300 int size, int ps_array_completeness)
301 {
302 EVCDecoderConfigurationRecord evcc;
303 int nalu_type;
304 size_t nalu_size;
305 1 int bytes_to_read = size;
306 unsigned array_index;
307
308 1 int ret = 0;
309
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size < 8) {
311 /* We can't write a valid evcC from the provided data */
312 return AVERROR_INVALIDDATA;
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (*data == 1) {
314 /* Data is already evcC-formatted */
315 avio_write(pb, data, size);
316 return 0;
317 }
318
319 1 evcc_init(&evcc);
320
321
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while (bytes_to_read > EVC_NALU_LENGTH_PREFIX_SIZE) {
322 4 nalu_size = evc_read_nal_unit_length(data, EVC_NALU_LENGTH_PREFIX_SIZE);
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (nalu_size == 0) break;
324
325 4 data += EVC_NALU_LENGTH_PREFIX_SIZE;
326 4 bytes_to_read -= EVC_NALU_LENGTH_PREFIX_SIZE;
327
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (bytes_to_read < nalu_size) break;
329
330 4 nalu_type = evc_get_nalu_type(data, bytes_to_read);
331
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (nalu_type < EVC_NOIDR_NUT || nalu_type > EVC_UNSPEC_NUT62) {
332 ret = AVERROR_INVALIDDATA;
333 goto end;
334 }
335
336 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
337 // NAL_unit_type indicates the type of the NAL units in the following array (which shall be all of that type);
338 // - it takes a value as defined in ISO/IEC 23094-1;
339 // - it is restricted to take one of the values indicating a SPS, PPS, APS, or SEI NAL unit.
340
4/5
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
4 switch (nalu_type) {
341 1 case EVC_SPS_NUT:
342 1 array_index = SPS_INDEX;
343 1 break;
344 1 case EVC_PPS_NUT:
345 1 array_index = PPS_INDEX;
346 1 break;
347 case EVC_APS_NUT:
348 array_index = APS_INDEX;
349 break;
350 1 case EVC_SEI_NUT:
351 1 array_index = SEI_INDEX;
352 1 break;
353 1 default:
354 1 array_index = -1;
355 1 break;
356 }
357
358
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
4 if( (array_index == SPS_INDEX) ||
359
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (array_index == PPS_INDEX) ||
360
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 (array_index == APS_INDEX) ||
361 (array_index == SEI_INDEX) ) {
362
363 3 ret = evcc_array_add_nal_unit(data, nalu_size, nalu_type, ps_array_completeness, &(evcc.arrays[array_index]));
364
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
366 goto end;
367
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (evcc.arrays[array_index].numNalus == 1)
368 3 evcc.num_of_arrays++;
369
370
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if(nalu_type == EVC_SPS_NUT) {
371 1 ret = evcc_parse_sps(data, nalu_size, &evcc);
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
373 goto end;
374 }
375 }
376
377 4 data += nalu_size;
378 4 bytes_to_read -= nalu_size;
379 }
380
381 1 ret = evcc_write(pb, &evcc);
382
383 1 end:
384 1 evcc_close(&evcc);
385 1 return ret;
386 }
387