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
27
29
33
36
37 #define NUM_CHROMA_FORMATS 4 // @see ISO_IEC_23094-1 section 6.2 table 2
38
41 };
42
45 };
46
49 };
50
53 };
54
57 };
58
61 };
62
64 const uint8_t *buf, int buf_size)
65 {
68 int nalu_type, tid;
70
71 if (buf_size <= 0) {
74 }
75
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 if (
get_bits1(&gb)) {
// forbidden_zero_bit
85 }
86
91 }
92
94 skip_bits(&gb, 5);
// nuh_reserved_zero_5bits
96
97 switch (nalu_type) {
103 }
104 break;
110 }
111 break;
112 case EVC_IDR_NUT:
// Coded slice of a IDR or non-IDR picture
118
123 }
124
126 sps =
ctx->ps.sps[
pps->pps_seq_parameter_set_id];
128
129 s->coded_width =
sps->pic_width_in_luma_samples;
130 s->coded_height =
sps->pic_height_in_luma_samples;
131
132 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 s->width =
sps->pic_width_in_luma_samples;
137 s->height =
sps->pic_height_in_luma_samples;
138 }
139
143 break;
144 }
147 break;
148 }
151 break;
152 }
153 default: {
155 }
156 }
157
159
160 if (
sps->vui_parameters_present_flag &&
sps->vui_parameters.timing_info_present_flag) {
161 int64_t num =
sps->vui_parameters.num_units_in_tick;
163 if (num != 0 && den != 0)
165 } else
167
170
172 case 8:
174 break;
175 case 9:
177 break;
178 case 10:
180 break;
181 case 12:
183 break;
184 case 14:
186 break;
187 case 16:
189 break;
190 }
191
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
199
200 s->output_picture_number =
ctx->poc.PicOrderCntVal;
201
202 break;
203 }
204 case EVC_SEI_NUT:
// Supplemental Enhancement Information
207 default:
208 break;
209 }
210
211 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 */
223 {
224 const uint8_t *
data = buf;
225 int data_size = buf_size;
226
227 while (data_size > 0) {
228 int nalu_size = 0;
230
231 // Buffer size is not enough for buffer to store NAL unit 4-bytes prefix (length)
234
236
237
240
241 if (data_size < nalu_size)
243
248 }
249
251 data_size -= nalu_size;
252 }
253 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
259 {
264
266
268 return -1;
269
270 // extradata is encoded as evcC format.
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
280 }
281
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 nalu_length_field_size = (bytestream2_get_byte(&gb) & 3) + 1;
288 if( nalu_length_field_size != 1 &&
289 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);
293 }
294
295 num_of_arrays = bytestream2_get_byte(&gb);
296
297 /* Decode nal units from evcC. */
298 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 int nal_unit_type = bytestream2_get_byte(&gb) & 0x3f;
305 int num_nalus = bytestream2_get_be16(&gb);
306
307 for (int j = 0; j < num_nalus; j++) {
308
309 int nal_unit_length = bytestream2_get_be16(&gb);
310
314 }
315
323 }
324 }
325
327 }
328 }
329 } else
330 return -1;
331
333 }
334
336 const uint8_t **poutbuf, int *poutbuf_size,
337 const uint8_t *buf, int buf_size)
338 {
339 int next;
342
345
348 ctx->parsed_extradata = 1;
349 }
350
351 next = buf_size;
352
356 *poutbuf_size = 0;
357 return buf_size;
358 }
359
360 // poutbuf contains just one Access Unit
361 *poutbuf = buf;
362 *poutbuf_size = buf_size;
363
364 return next;
365 }
366
368 {
370
372 }
373
379 };