FFmpeg coverage


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