FFmpeg coverage


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