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