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
31
34
35 #define NUM_CHROMA_FORMATS 4 // @see ISO_IEC_23094-1 section 6.2 table 2
36
39 };
40
43 };
44
47 };
48
51 };
52
55 };
56
59 };
60
62 const uint8_t *buf, int buf_size)
63 {
66 int nalu_type, tid;
68
69 if (buf_size <= 0) {
72 }
73
77
78 // @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)
79 // @see enum EVCNALUnitType in evc.h
80 if (
get_bits1(&gb)) {
// forbidden_zero_bit
83 }
84
89 }
90
92 skip_bits(&gb, 5);
// nuh_reserved_zero_5bits
94
95 switch (nalu_type) {
101 }
102 break;
108 }
109 break;
110 case EVC_IDR_NUT:
// Coded slice of a IDR or non-IDR picture
116
121 }
122
124 sps =
ctx->ps.sps[
pps->pps_seq_parameter_set_id];
126
127 s->coded_width =
sps->pic_width_in_luma_samples;
128 s->coded_height =
sps->pic_height_in_luma_samples;
129
130 if (
sps->picture_cropping_flag) {
131 s->width =
sps->pic_width_in_luma_samples -
sps->picture_crop_left_offset -
sps->picture_crop_right_offset;
132 s->height =
sps->pic_height_in_luma_samples -
sps->picture_crop_top_offset -
sps->picture_crop_bottom_offset;
133 } else {
134 s->width =
sps->pic_width_in_luma_samples;
135 s->height =
sps->pic_height_in_luma_samples;
136 }
137
141 break;
142 }
145 break;
146 }
149 break;
150 }
151 default: {
153 }
154 }
155
157
158 if (
sps->vui_parameters_present_flag &&
sps->vui_parameters.timing_info_present_flag) {
159 int64_t num =
sps->vui_parameters.num_units_in_tick;
161 if (num != 0 && den != 0)
163 } else
165
168
170 case 8:
172 break;
173 case 9:
175 break;
176 case 10:
178 break;
179 case 12:
181 break;
182 case 14:
184 break;
185 case 16:
187 break;
188 }
189
191
192 // POC (picture order count of the current picture) derivation
193 // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count
197
198 s->output_picture_number =
ctx->poc.PicOrderCntVal;
199
200 break;
201 }
202 case EVC_SEI_NUT:
// Supplemental Enhancement Information
205 default:
206 break;
207 }
208
209 return 0;
210 }
211
212 /**
213 * Parse NAL units of found picture and decode some basic information.
214 *
215 * @param s codec parser context
216 * @param avctx codec context
217 * @param buf buffer with field/frame data
218 * @param buf_size size of the buffer
219 */
221 {
222 const uint8_t *
data = buf;
223 int data_size = buf_size;
224
225 while (data_size > 0) {
226 int nalu_size = 0;
228
229 // Buffer size is not enough for buffer to store NAL unit 4-bytes prefix (length)
232
234
235
238
239 if (data_size < nalu_size)
241
246 }
247
249 data_size -= nalu_size;
250 }
251 return 0;
252 }
253
254 // Decoding nal units from evcC (EVCDecoderConfigurationRecord)
255 // @see @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.2
257 {
262
264
266 return -1;
267
268 // extradata is encoded as evcC format.
270 int num_of_arrays; // indicates the number of arrays of NAL units of the indicated type(s)
271
272 int nalu_length_field_size; // indicates the length in bytes of the NALUnitLenght field in EVC video stream sample in the stream
273 // 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.
274
278 }
279
281
282 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
283 // 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.
284 // 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.
285 nalu_length_field_size = (bytestream2_get_byte(&gb) & 3) + 1;
286 if( nalu_length_field_size != 1 &&
287 nalu_length_field_size != 2 &&
288 nalu_length_field_size != 4 ) {
289 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);
291 }
292
293 num_of_arrays = bytestream2_get_byte(&gb);
294
295 /* Decode nal units from evcC. */
296 for (
int i = 0;
i < num_of_arrays;
i++) {
297
298 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
299 // NAL_unit_type indicates the type of the NAL units in the following array (which shall be all of that type);
300 // - it takes a value as defined in ISO/IEC 23094-1;
301 // - it is restricted to take one of the values indicating a SPS, PPS, APS, or SEI NAL unit.
302 int nal_unit_type = bytestream2_get_byte(&gb) & 0x3f;
303 int num_nalus = bytestream2_get_be16(&gb);
304
305 for (int j = 0; j < num_nalus; j++) {
306
307 int nal_unit_length = bytestream2_get_be16(&gb);
308
312 }
313
321 }
322 }
323
325 }
326 }
327 } else
328 return -1;
329
331 }
332
334 const uint8_t **poutbuf, int *poutbuf_size,
335 const uint8_t *buf, int buf_size)
336 {
337 int next;
340
343
346 ctx->parsed_extradata = 1;
347 }
348
349 next = buf_size;
350
354 *poutbuf_size = 0;
355 return buf_size;
356 }
357
358 // poutbuf contains just one Access Unit
359 *poutbuf = buf;
360 *poutbuf_size = buf_size;
361
362 return next;
363 }
364
366 {
368
370 }
371
377 };