FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/notchlc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 314 0.0%
Functions: 0 5 0.0%
Branches: 0 186 0.0%

Line Branch Exec Source
1 /*
2 * NotchLC decoder
3 * Copyright (c) 2020 Paul B Mahol
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 <stdio.h>
23 #include <string.h>
24
25 #define BITSTREAM_READER_LE
26 #include "libavutil/mem.h"
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "codec_internal.h"
30 #include "decode.h"
31 #include "get_bits.h"
32 #include "lzf.h"
33 #include "thread.h"
34
35 typedef struct NotchLCContext {
36 unsigned compressed_size;
37 unsigned format;
38
39 uint8_t *uncompressed_buffer;
40 unsigned uncompressed_size;
41
42 uint8_t *lzf_buffer;
43 int64_t lzf_size;
44
45 unsigned texture_size_x;
46 unsigned texture_size_y;
47 unsigned y_data_row_offsets;
48 unsigned uv_offset_data_offset;
49 unsigned y_control_data_offset;
50 unsigned a_control_word_offset;
51 unsigned y_data_offset;
52 unsigned uv_data_offset;
53 unsigned y_data_size;
54 unsigned a_data_offset;
55 unsigned uv_count_offset;
56 unsigned a_count_size;
57 unsigned data_end;
58
59 GetByteContext gb;
60 PutByteContext pb;
61 } NotchLCContext;
62
63 static av_cold int decode_init(AVCodecContext *avctx)
64 {
65 avctx->pix_fmt = AV_PIX_FMT_YUVA444P12;
66 avctx->color_range = AVCOL_RANGE_JPEG;
67 avctx->colorspace = AVCOL_SPC_RGB;
68 avctx->color_primaries = AVCOL_PRI_BT709;
69 avctx->color_trc = AVCOL_TRC_IEC61966_2_1;
70
71 return 0;
72 }
73
74 #define HISTORY_SIZE (64 * 1024)
75
76 static int lz4_decompress(AVCodecContext *avctx,
77 GetByteContext *gb,
78 PutByteContext *pb)
79 {
80 unsigned reference_pos, delta, pos = 0;
81 uint8_t history[64 * 1024];
82 int match_length;
83
84 while (bytestream2_get_bytes_left(gb) > 0) {
85 uint8_t token = bytestream2_get_byte(gb);
86 int num_literals = token >> 4;
87
88 if (num_literals == 15) {
89 unsigned char current;
90 do {
91 current = bytestream2_get_byte(gb);
92 num_literals += current;
93 } while (current == 255);
94 }
95
96 if (bytestream2_get_bytes_left(gb) < num_literals)
97 return AVERROR_INVALIDDATA;
98
99 if (pos + num_literals < HISTORY_SIZE) {
100 bytestream2_get_buffer(gb, history + pos, num_literals);
101 pos += num_literals;
102 } else {
103 while (num_literals-- > 0) {
104 history[pos++] = bytestream2_get_byte(gb);
105 if (pos == HISTORY_SIZE) {
106 bytestream2_put_buffer(pb, history, HISTORY_SIZE);
107 pos = 0;
108 }
109 }
110 }
111
112 if (bytestream2_get_bytes_left(gb) <= 0)
113 break;
114
115 delta = bytestream2_get_le16(gb);
116 if (delta == 0)
117 return 0;
118 match_length = 4 + (token & 0x0F);
119 if (match_length == 4 + 0x0F) {
120 uint8_t current;
121
122 do {
123 current = bytestream2_get_byte(gb);
124 match_length += current;
125 } while (current == 255);
126 }
127 reference_pos = (pos >= delta) ? (pos - delta) : (HISTORY_SIZE + pos - delta);
128 if (pos + match_length < HISTORY_SIZE && reference_pos + match_length < HISTORY_SIZE) {
129 if (pos >= reference_pos + match_length || reference_pos >= pos + match_length) {
130 memcpy(history + pos, history + reference_pos, match_length);
131 pos += match_length;
132 } else {
133 while (match_length-- > 0)
134 history[pos++] = history[reference_pos++];
135 }
136 } else {
137 while (match_length-- > 0) {
138 history[pos++] = history[reference_pos++];
139 if (pos == HISTORY_SIZE) {
140 bytestream2_put_buffer(pb, history, HISTORY_SIZE);
141 pos = 0;
142 }
143 reference_pos %= HISTORY_SIZE;
144 }
145 }
146 }
147
148 bytestream2_put_buffer(pb, history, pos);
149
150 return bytestream2_tell_p(pb);
151 }
152
153 static int decode_blocks(AVCodecContext *avctx, AVFrame *p,
154 unsigned uncompressed_size)
155 {
156 NotchLCContext *s = avctx->priv_data;
157 GetByteContext rgb, dgb, *gb = &s->gb;
158 GetBitContext bit;
159 int ylinesize, ulinesize, vlinesize, alinesize;
160 uint16_t *dsty, *dstu, *dstv, *dsta;
161 int ret;
162
163 s->texture_size_x = bytestream2_get_le32(gb);
164 s->texture_size_y = bytestream2_get_le32(gb);
165
166 ret = ff_set_dimensions(avctx, s->texture_size_x, s->texture_size_y);
167 if (ret < 0)
168 return ret;
169
170 s->uv_offset_data_offset = bytestream2_get_le32(gb);
171 if (s->uv_offset_data_offset >= UINT_MAX / 4)
172 return AVERROR_INVALIDDATA;
173 s->uv_offset_data_offset *= 4;
174 if (s->uv_offset_data_offset >= uncompressed_size)
175 return AVERROR_INVALIDDATA;
176
177 s->y_control_data_offset = bytestream2_get_le32(gb);
178 if (s->y_control_data_offset >= UINT_MAX / 4)
179 return AVERROR_INVALIDDATA;
180 s->y_control_data_offset *= 4;
181 if (s->y_control_data_offset >= uncompressed_size)
182 return AVERROR_INVALIDDATA;
183
184 s->a_control_word_offset = bytestream2_get_le32(gb);
185 if (s->a_control_word_offset >= UINT_MAX / 4)
186 return AVERROR_INVALIDDATA;
187 s->a_control_word_offset *= 4;
188 if (s->a_control_word_offset >= uncompressed_size)
189 return AVERROR_INVALIDDATA;
190
191 s->uv_data_offset = bytestream2_get_le32(gb);
192 if (s->uv_data_offset >= UINT_MAX / 4)
193 return AVERROR_INVALIDDATA;
194 s->uv_data_offset *= 4;
195 if (s->uv_data_offset >= uncompressed_size)
196 return AVERROR_INVALIDDATA;
197
198 s->y_data_size = bytestream2_get_le32(gb);
199 if (s->y_data_size >= UINT_MAX / 4)
200 return AVERROR_INVALIDDATA;
201
202 s->a_data_offset = bytestream2_get_le32(gb);
203 if (s->a_data_offset >= UINT_MAX / 4)
204 return AVERROR_INVALIDDATA;
205 s->a_data_offset *= 4;
206 if (s->a_data_offset >= uncompressed_size)
207 return AVERROR_INVALIDDATA;
208
209 s->a_count_size = bytestream2_get_le32(gb);
210 if (s->a_count_size >= UINT_MAX / 4)
211 return AVERROR_INVALIDDATA;
212 s->a_count_size *= 4;
213 if (s->a_count_size >= uncompressed_size)
214 return AVERROR_INVALIDDATA;
215
216 s->data_end = bytestream2_get_le32(gb);
217 if (s->data_end > uncompressed_size)
218 return AVERROR_INVALIDDATA;
219
220 s->y_data_row_offsets = bytestream2_tell(gb);
221 if (s->data_end <= s->y_data_size)
222 return AVERROR_INVALIDDATA;
223 s->y_data_offset = s->data_end - s->y_data_size;
224 if (s->y_data_offset <= s->a_data_offset)
225 return AVERROR_INVALIDDATA;
226 s->uv_count_offset = s->y_data_offset - s->a_data_offset;
227
228 if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
229 return ret;
230
231 rgb = *gb;
232 dgb = *gb;
233 bytestream2_seek(&rgb, s->y_data_row_offsets, SEEK_SET);
234 bytestream2_seek(gb, s->y_control_data_offset, SEEK_SET);
235
236 if (bytestream2_get_bytes_left(gb) < (avctx->height + 3) / 4 * ((avctx->width + 3) / 4) * 4)
237 return AVERROR_INVALIDDATA;
238
239 dsty = (uint16_t *)p->data[0];
240 dsta = (uint16_t *)p->data[3];
241 ylinesize = p->linesize[0] / 2;
242 alinesize = p->linesize[3] / 2;
243
244 for (int y = 0; y < avctx->height; y += 4) {
245 const unsigned row_offset = bytestream2_get_le32(&rgb);
246
247 bytestream2_seek(&dgb, s->y_data_offset + row_offset, SEEK_SET);
248
249 ret = init_get_bits8(&bit, dgb.buffer, bytestream2_get_bytes_left(&dgb));
250 if (ret < 0)
251 return ret;
252 for (int x = 0; x < avctx->width; x += 4) {
253 unsigned item = bytestream2_get_le32(gb);
254 unsigned y_min = item & 4095;
255 unsigned y_max = (item >> 12) & 4095;
256 unsigned y_diff = y_max - y_min;
257 unsigned control[4];
258
259 control[0] = (item >> 24) & 3;
260 control[1] = (item >> 26) & 3;
261 control[2] = (item >> 28) & 3;
262 control[3] = (item >> 30) & 3;
263
264 for (int i = 0; i < 4; i++) {
265 const int nb_bits = control[i] + 1;
266 const int div = (1 << nb_bits) - 1;
267 const int add = div - 1;
268
269 dsty[x + i * ylinesize + 0] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
270 dsty[x + i * ylinesize + 1] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
271 dsty[x + i * ylinesize + 2] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
272 dsty[x + i * ylinesize + 3] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
273 }
274 }
275
276 dsty += 4 * ylinesize;
277 }
278
279 rgb = *gb;
280 dgb = *gb;
281 bytestream2_seek(gb, s->a_control_word_offset, SEEK_SET);
282 if (s->uv_count_offset == s->a_control_word_offset) {
283 for (int y = 0; y < avctx->height; y++) {
284 for (int x = 0; x < avctx->width; x++)
285 dsta[x] = 4095;
286 dsta += alinesize;
287 }
288 } else {
289 if (bytestream2_get_bytes_left(gb) < (avctx->height + 15) / 16 * ((avctx->width + 15) / 16) * 8)
290 return AVERROR_INVALIDDATA;
291
292 for (int y = 0; y < avctx->height; y += 16) {
293 for (int x = 0; x < avctx->width; x += 16) {
294 unsigned m = bytestream2_get_le32(gb);
295 unsigned offset = bytestream2_get_le32(gb);
296 unsigned alpha0, alpha1;
297 uint64_t control;
298
299 if (offset >= UINT_MAX / 4)
300 return AVERROR_INVALIDDATA;
301 offset = offset * 4 + s->uv_data_offset + s->a_data_offset;
302 if (offset >= s->data_end)
303 return AVERROR_INVALIDDATA;
304
305 bytestream2_seek(&dgb, offset, SEEK_SET);
306 control = bytestream2_get_le64(&dgb);
307 alpha0 = control & 0xFF;
308 alpha1 = (control >> 8) & 0xFF;
309 control = control >> 16;
310
311 for (int by = 0; by < 4; by++) {
312 for (int bx = 0; bx < 4; bx++) {
313 switch (m & 3) {
314 case 0:
315 for (int i = 0; i < 4; i++) {
316 for (int j = 0; j < 4; j++) {
317 dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 0;
318 }
319 }
320 break;
321 case 1:
322 for (int i = 0; i < 4; i++) {
323 for (int j = 0; j < 4; j++) {
324 dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 4095;
325 }
326 }
327 break;
328 case 2:
329 for (int i = 0; i < 4; i++) {
330 for (int j = 0; j < 4; j++) {
331 dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
332 }
333 }
334 break;
335 default:
336 return AVERROR_INVALIDDATA;
337 }
338
339 control >>= 3;
340 m >>= 2;
341 }
342 }
343 }
344
345 dsta += 16 * alinesize;
346 }
347 }
348
349 bytestream2_seek(&rgb, s->uv_offset_data_offset, SEEK_SET);
350
351 dstu = (uint16_t *)p->data[1];
352 dstv = (uint16_t *)p->data[2];
353 ulinesize = p->linesize[1] / 2;
354 vlinesize = p->linesize[2] / 2;
355
356 for (int y = 0; y < avctx->height; y += 16) {
357 for (int x = 0; x < avctx->width; x += 16) {
358 unsigned offset = bytestream2_get_le32(&rgb) * 4;
359 int u[16][16] = { 0 }, v[16][16] = { 0 };
360 int u0, v0, u1, v1, udif, vdif;
361 unsigned escape, is8x8, loc;
362
363 bytestream2_seek(&dgb, s->uv_data_offset + offset, SEEK_SET);
364
365 is8x8 = bytestream2_get_le16(&dgb);
366 escape = bytestream2_get_le16(&dgb);
367
368 if (escape == 0 && is8x8 == 0) {
369 u0 = bytestream2_get_byte(&dgb);
370 v0 = bytestream2_get_byte(&dgb);
371 u1 = bytestream2_get_byte(&dgb);
372 v1 = bytestream2_get_byte(&dgb);
373 loc = bytestream2_get_le32(&dgb);
374 u0 = (u0 << 4) | (u0 & 0xF);
375 v0 = (v0 << 4) | (v0 & 0xF);
376 u1 = (u1 << 4) | (u1 & 0xF);
377 v1 = (v1 << 4) | (v1 & 0xF);
378 udif = u1 - u0;
379 vdif = v1 - v0;
380
381 for (int i = 0; i < 16; i += 4) {
382 for (int j = 0; j < 16; j += 4) {
383 for (int ii = 0; ii < 4; ii++) {
384 for (int jj = 0; jj < 4; jj++) {
385 u[i + ii][j + jj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
386 v[i + ii][j + jj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
387 }
388 }
389
390 loc >>= 2;
391 }
392 }
393 } else {
394 for (int i = 0; i < 16; i += 8) {
395 for (int j = 0; j < 16; j += 8) {
396 if (is8x8 & 1) {
397 u0 = bytestream2_get_byte(&dgb);
398 v0 = bytestream2_get_byte(&dgb);
399 u1 = bytestream2_get_byte(&dgb);
400 v1 = bytestream2_get_byte(&dgb);
401 loc = bytestream2_get_le32(&dgb);
402 u0 = (u0 << 4) | (u0 & 0xF);
403 v0 = (v0 << 4) | (v0 & 0xF);
404 u1 = (u1 << 4) | (u1 & 0xF);
405 v1 = (v1 << 4) | (v1 & 0xF);
406 udif = u1 - u0;
407 vdif = v1 - v0;
408
409 for (int ii = 0; ii < 8; ii += 2) {
410 for (int jj = 0; jj < 8; jj += 2) {
411 for (int iii = 0; iii < 2; iii++) {
412 for (int jjj = 0; jjj < 2; jjj++) {
413 u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
414 v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
415 }
416 }
417
418 loc >>= 2;
419 }
420 }
421 } else if (escape) {
422 for (int ii = 0; ii < 8; ii += 4) {
423 for (int jj = 0; jj < 8; jj += 4) {
424 u0 = bytestream2_get_byte(&dgb);
425 v0 = bytestream2_get_byte(&dgb);
426 u1 = bytestream2_get_byte(&dgb);
427 v1 = bytestream2_get_byte(&dgb);
428 loc = bytestream2_get_le32(&dgb);
429 u0 = (u0 << 4) | (u0 & 0xF);
430 v0 = (v0 << 4) | (v0 & 0xF);
431 u1 = (u1 << 4) | (u1 & 0xF);
432 v1 = (v1 << 4) | (v1 & 0xF);
433 udif = u1 - u0;
434 vdif = v1 - v0;
435
436 for (int iii = 0; iii < 4; iii++) {
437 for (int jjj = 0; jjj < 4; jjj++) {
438 u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
439 v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
440
441 loc >>= 2;
442 }
443 }
444 }
445 }
446 }
447
448 is8x8 >>= 1;
449 }
450 }
451 }
452
453 for (int i = 0; i < 16; i++) {
454 for (int j = 0; j < 16; j++) {
455 dstu[x + i * ulinesize + j] = u[i][j];
456 dstv[x + i * vlinesize + j] = v[i][j];
457 }
458 }
459 }
460
461 dstu += 16 * ulinesize;
462 dstv += 16 * vlinesize;
463 }
464
465 return 0;
466 }
467
468 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
469 int *got_frame, AVPacket *avpkt)
470 {
471 NotchLCContext *s = avctx->priv_data;
472 GetByteContext *gb = &s->gb;
473 PutByteContext *pb = &s->pb;
474 unsigned uncompressed_size;
475 int ret;
476
477 if (avpkt->size <= 40)
478 return AVERROR_INVALIDDATA;
479
480 bytestream2_init(gb, avpkt->data, avpkt->size);
481
482 if (bytestream2_get_le32(gb) != MKBETAG('N','L','C','1'))
483 return AVERROR_INVALIDDATA;
484
485 uncompressed_size = bytestream2_get_le32(gb);
486 s->compressed_size = bytestream2_get_le32(gb);
487 s->format = bytestream2_get_le32(gb);
488
489 if (s->format > 2)
490 return AVERROR_PATCHWELCOME;
491
492 if (s->format == 0) {
493 ret = ff_lzf_uncompress(gb, &s->lzf_buffer, &s->lzf_size);
494 if (ret < 0)
495 return ret;
496
497 if (uncompressed_size > s->lzf_size)
498 return AVERROR_INVALIDDATA;
499
500 bytestream2_init(gb, s->lzf_buffer, uncompressed_size);
501 } else if (s->format == 1) {
502 if (bytestream2_get_bytes_left(gb) < uncompressed_size / 255)
503 return AVERROR_INVALIDDATA;
504
505 av_fast_padded_malloc(&s->uncompressed_buffer, &s->uncompressed_size,
506 uncompressed_size);
507 if (!s->uncompressed_buffer)
508 return AVERROR(ENOMEM);
509
510 bytestream2_init_writer(pb, s->uncompressed_buffer, s->uncompressed_size);
511
512 ret = lz4_decompress(avctx, gb, pb);
513 if (ret != uncompressed_size)
514 return AVERROR_INVALIDDATA;
515
516 bytestream2_init(gb, s->uncompressed_buffer, uncompressed_size);
517 }
518
519 ret = decode_blocks(avctx, p, uncompressed_size);
520 if (ret < 0)
521 return ret;
522
523 *got_frame = 1;
524
525 return avpkt->size;
526 }
527
528 static av_cold int decode_end(AVCodecContext *avctx)
529 {
530 NotchLCContext *s = avctx->priv_data;
531
532 av_freep(&s->uncompressed_buffer);
533 s->uncompressed_size = 0;
534 av_freep(&s->lzf_buffer);
535 s->lzf_size = 0;
536
537 return 0;
538 }
539
540 const FFCodec ff_notchlc_decoder = {
541 .p.name = "notchlc",
542 CODEC_LONG_NAME("NotchLC"),
543 .p.type = AVMEDIA_TYPE_VIDEO,
544 .p.id = AV_CODEC_ID_NOTCHLC,
545 .priv_data_size = sizeof(NotchLCContext),
546 .init = decode_init,
547 .close = decode_end,
548 FF_CODEC_DECODE_CB(decode_frame),
549 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
550 };
551