FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/magicyuv.c
Date: 2026-06-16 12:54:33
Exec Total Coverage
Lines: 231 414 55.8%
Functions: 6 8 75.0%
Branches: 106 215 49.3%

Line Branch Exec Source
1 /*
2 * MagicYUV decoder
3 * Copyright (c) 2016 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 <stdlib.h>
23 #include <string.h>
24
25 #define CACHED_BITSTREAM_READER !ARCH_X86_32
26
27 #include "libavutil/mem.h"
28 #include "libavutil/pixdesc.h"
29
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 #include "get_bits.h"
35 #include "lossless_videodsp.h"
36 #include "thread.h"
37
38 #define VLC_BITS 12
39
40 typedef struct Slice {
41 uint32_t start;
42 uint32_t size;
43 } Slice;
44
45 typedef enum Prediction {
46 LEFT = 1,
47 GRADIENT,
48 MEDIAN,
49 } Prediction;
50
51 typedef struct HuffEntry {
52 uint8_t len;
53 uint16_t sym;
54 } HuffEntry;
55
56 typedef struct MagicYUVContext {
57 AVFrame *p;
58 int max;
59 int bps;
60 int slice_height;
61 int nb_slices;
62 int planes; // number of encoded planes in bitstream
63 int decorrelate; // postprocessing work
64 int color_matrix; // video color matrix
65 int flags;
66 int interlaced; // video is interlaced
67 const uint8_t *buf; // pointer to AVPacket->data
68 int hshift[4];
69 int vshift[4];
70 Slice *slices[4]; // slice bitstream positions for each plane
71 unsigned int slices_size[4]; // slice sizes for each plane
72 VLC vlc[4]; // VLC for each plane
73 VLC_MULTI multi[4]; // Buffer for joint VLC data
74 int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
75 int j, int threadnr);
76 LLVidDSPContext llviddsp;
77 HuffEntry he[1 << 14];
78 uint8_t len[1 << 14];
79 } MagicYUVContext;
80
81 552 static int huff_build(AVCodecContext *avctx,
82 const uint8_t len[], uint16_t codes_pos[33],
83 VLC *vlc, VLC_MULTI *multi, int nb_elems, void *logctx)
84 {
85 552 MagicYUVContext *s = avctx->priv_data;
86 552 HuffEntry *he = s->he;
87
88
2/2
✓ Branch 0 taken 17112 times.
✓ Branch 1 taken 552 times.
17664 for (int i = 31; i > 0; i--)
89 17112 codes_pos[i] += codes_pos[i + 1];
90
91
2/2
✓ Branch 0 taken 141312 times.
✓ Branch 1 taken 552 times.
141864 for (unsigned i = nb_elems; i-- > 0;)
92 141312 he[--codes_pos[len[i]]] = (HuffEntry){ len[i], i };
93
94 552 ff_vlc_free(vlc);
95 552 ff_vlc_free_multi(multi);
96 1104 return ff_vlc_init_multi_from_lengths(vlc, multi, FFMIN(he[0].len, VLC_BITS), nb_elems, nb_elems,
97 552 &he[0].len, sizeof(he[0]),
98 552 &he[0].sym, sizeof(he[0]), sizeof(he[0].sym),
99 0, 0, logctx);
100 }
101
102 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
103 const uint16_t *diff, intptr_t w,
104 int *left, int *left_top, int max)
105 {
106 int i;
107 uint16_t l, lt;
108
109 l = *left;
110 lt = *left_top;
111
112 for (i = 0; i < w; i++) {
113 l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
114 l &= max;
115 lt = src1[i];
116 dst[i] = l;
117 }
118
119 *left = l;
120 *left_top = lt;
121 }
122
123 #define READ_PLANE(dst, plane, b, c) \
124 { \
125 x = 0; \
126 for (; CACHED_BITSTREAM_READER && x < width-c && get_bits_left(&gb) > 0;) {\
127 ret = get_vlc_multi(&gb, (uint8_t *)dst + x * b, multi, \
128 vlc, vlc_bits, 3, b); \
129 if (ret <= 0) \
130 return AVERROR_INVALIDDATA; \
131 x += ret; \
132 } \
133 for (; x < width && get_bits_left(&gb) > 0; x++) \
134 dst[x] = get_vlc2(&gb, vlc, vlc_bits, 3); \
135 dst += stride; \
136 }
137
138 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
139 int j, int threadnr)
140 {
141 const MagicYUVContext *s = avctx->priv_data;
142 int interlaced = s->interlaced;
143 const int bps = s->bps;
144 const int max = s->max - 1;
145 AVFrame *p = s->p;
146 int i, k, x;
147 GetBitContext gb;
148 uint16_t *dst;
149
150 for (i = 0; i < s->planes; i++) {
151 int left, lefttop, top;
152 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
153 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
154 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
155 ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
156 ptrdiff_t stride = p->linesize[i] / 2;
157 const VLC_MULTI_ELEM *const multi = s->multi[i].table;
158 const VLCElem *const vlc = s->vlc[i].table;
159 const int vlc_bits = s->vlc[i].bits;
160 int flags, pred;
161 int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
162 s->slices[i][j].size);
163
164 if (ret < 0)
165 return ret;
166
167 flags = get_bits(&gb, 8);
168 pred = get_bits(&gb, 8);
169
170 dst = (uint16_t *)p->data[i] + j * sheight * stride;
171 if (flags & 1) {
172 if (get_bits_left(&gb) < bps * width * height)
173 return AVERROR_INVALIDDATA;
174 for (k = 0; k < height; k++) {
175 for (x = 0; x < width; x++)
176 dst[x] = get_bits(&gb, bps);
177
178 dst += stride;
179 }
180 } else {
181 for (k = 0; k < height; k++)
182 READ_PLANE(dst, i, 2, 3)
183 }
184
185 switch (pred) {
186 case LEFT:
187 dst = (uint16_t *)p->data[i] + j * sheight * stride;
188 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
189 dst += stride;
190 if (interlaced) {
191 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
192 dst += stride;
193 }
194 for (k = 1 + interlaced; k < height; k++) {
195 s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
196 dst += stride;
197 }
198 break;
199 case GRADIENT:
200 dst = (uint16_t *)p->data[i] + j * sheight * stride;
201 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
202 dst += stride;
203 if (interlaced) {
204 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
205 dst += stride;
206 }
207 for (k = 1 + interlaced; k < height; k++) {
208 top = dst[-fake_stride];
209 left = top + dst[0];
210 dst[0] = left & max;
211 for (x = 1; x < width; x++) {
212 top = dst[x - fake_stride];
213 lefttop = dst[x - (fake_stride + 1)];
214 left += top - lefttop + dst[x];
215 dst[x] = left & max;
216 }
217 dst += stride;
218 }
219 break;
220 case MEDIAN:
221 dst = (uint16_t *)p->data[i] + j * sheight * stride;
222 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
223 dst += stride;
224 if (interlaced) {
225 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
226 dst += stride;
227 }
228 if (1 + interlaced < height)
229 lefttop = left = dst[0];
230 for (k = 1 + interlaced; k < height; k++) {
231 magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
232 lefttop = left = dst[0];
233 dst += stride;
234 }
235 break;
236 default:
237 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
238 }
239 }
240
241 if (s->decorrelate) {
242 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
243 int width = avctx->coded_width;
244 uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
245 uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
246 uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
247
248 for (i = 0; i < height; i++) {
249 for (k = 0; k < width; k++) {
250 b[k] = (b[k] + g[k]) & max;
251 r[k] = (r[k] + g[k]) & max;
252 }
253 b += p->linesize[0] / 2;
254 g += p->linesize[1] / 2;
255 r += p->linesize[2] / 2;
256 }
257 }
258
259 return 0;
260 }
261
262 738 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
263 int j, int threadnr)
264 {
265 738 const MagicYUVContext *s = avctx->priv_data;
266 738 int interlaced = s->interlaced;
267 738 AVFrame *p = s->p;
268 int i, k, x, min_width;
269 GetBitContext gb;
270 uint8_t *dst;
271
272
2/2
✓ Branch 0 taken 1908 times.
✓ Branch 1 taken 738 times.
2646 for (i = 0; i < s->planes; i++) {
273 int left, lefttop, top;
274 1908 int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
275 1908 int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
276 1908 int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
277 1908 ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
278 1908 ptrdiff_t stride = p->linesize[i];
279 1908 const uint8_t *slice = s->buf + s->slices[i][j].start;
280 1908 const VLC_MULTI_ELEM *const multi = s->multi[i].table;
281 1908 const VLCElem *const vlc = s->vlc[i].table;
282 1908 const int vlc_bits = s->vlc[i].bits;
283 int flags, pred;
284
285 1908 flags = bytestream_get_byte(&slice);
286 1908 pred = bytestream_get_byte(&slice);
287
288 1908 dst = p->data[i] + j * sheight * stride;
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1908 times.
1908 if (flags & 1) {
290 if (s->slices[i][j].size - 2 < width * height)
291 return AVERROR_INVALIDDATA;
292 for (k = 0; k < height; k++) {
293 bytestream_get_buffer(&slice, dst, width);
294 dst += stride;
295 }
296 } else {
297 1908 int ret = init_get_bits8(&gb, slice, s->slices[i][j].size - 2);
298
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1908 times.
1908 if (ret < 0)
300 return ret;
301
302
2/2
✓ Branch 0 taken 102610 times.
✓ Branch 1 taken 1908 times.
104518 for (k = 0; k < height; k++)
303
7/10
✗ Branch 1 not taken.
✓ Branch 2 taken 13557862 times.
✓ Branch 3 taken 13557862 times.
✓ Branch 4 taken 102610 times.
✓ Branch 6 taken 13557862 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 590042 times.
✓ Branch 10 taken 102610 times.
✓ Branch 12 taken 590042 times.
✗ Branch 13 not taken.
14250514 READ_PLANE(dst, i, 1, 7)
304 }
305
306
3/4
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 243 times.
✓ Branch 2 taken 315 times.
✗ Branch 3 not taken.
1908 switch (pred) {
307 1350 case LEFT:
308 1350 dst = p->data[i] + j * sheight * stride;
309 1350 s->llviddsp.add_left_pred(dst, dst, width, 0);
310 1350 dst += stride;
311
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 1278 times.
1350 if (interlaced) {
312 72 s->llviddsp.add_left_pred(dst, dst, width, 0);
313 72 dst += stride;
314 }
315
2/2
✓ Branch 0 taken 45890 times.
✓ Branch 1 taken 1350 times.
47240 for (k = 1 + interlaced; k < height; k++) {
316 45890 s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
317 45890 dst += stride;
318 }
319 1350 break;
320 243 case GRADIENT:
321 243 dst = p->data[i] + j * sheight * stride;
322 243 s->llviddsp.add_left_pred(dst, dst, width, 0);
323 243 dst += stride;
324
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 225 times.
243 if (interlaced) {
325 18 s->llviddsp.add_left_pred(dst, dst, width, 0);
326 18 dst += stride;
327 }
328 243 min_width = FFMIN(width, 32);
329
2/2
✓ Branch 0 taken 46123 times.
✓ Branch 1 taken 243 times.
46366 for (k = 1 + interlaced; k < height; k++) {
330 46123 top = dst[-fake_stride];
331 46123 left = top + dst[0];
332 46123 dst[0] = left;
333
2/2
✓ Branch 0 taken 1429813 times.
✓ Branch 1 taken 46123 times.
1475936 for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
334 1429813 top = dst[x - fake_stride];
335 1429813 lefttop = dst[x - (fake_stride + 1)];
336 1429813 left += top - lefttop + dst[x];
337 1429813 dst[x] = left;
338 }
339
1/2
✓ Branch 0 taken 46123 times.
✗ Branch 1 not taken.
46123 if (width > 32)
340 46123 s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
341 46123 dst += stride;
342 }
343 243 break;
344 315 case MEDIAN:
345 315 dst = p->data[i] + j * sheight * stride;
346 315 s->llviddsp.add_left_pred(dst, dst, width, 0);
347 315 dst += stride;
348
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 261 times.
315 if (interlaced) {
349 54 s->llviddsp.add_left_pred(dst, dst, width, 0);
350 54 dst += stride;
351 }
352
1/2
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
315 if (1 + interlaced < height)
353 315 lefttop = left = dst[0];
354
2/2
✓ Branch 0 taken 8545 times.
✓ Branch 1 taken 315 times.
8860 for (k = 1 + interlaced; k < height; k++) {
355 8545 s->llviddsp.add_median_pred(dst, dst - fake_stride,
356 dst, width, &left, &lefttop);
357 8545 lefttop = left = dst[0];
358 8545 dst += stride;
359 }
360 315 break;
361 default:
362 avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
363 }
364 }
365
366
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 651 times.
738 if (s->decorrelate) {
367 87 int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
368 87 int width = avctx->coded_width;
369 87 uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
370 87 uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
371 87 uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
372
373
2/2
✓ Branch 0 taken 15616 times.
✓ Branch 1 taken 87 times.
15703 for (i = 0; i < height; i++) {
374 15616 s->llviddsp.add_bytes(b, g, width);
375 15616 s->llviddsp.add_bytes(r, g, width);
376 15616 b += p->linesize[0];
377 15616 g += p->linesize[1];
378 15616 r += p->linesize[2];
379 }
380 }
381
382 738 return 0;
383 }
384
385 218 static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
386 int table_size, int max)
387 {
388 218 MagicYUVContext *s = avctx->priv_data;
389 GetByteContext gb;
390 218 uint8_t *len = s->len;
391 218 uint16_t length_count[33] = { 0 };
392 218 int i = 0, j = 0, k;
393
394 218 bytestream2_init(&gb, table, table_size);
395
396
1/2
✓ Branch 1 taken 131596 times.
✗ Branch 2 not taken.
131814 while (bytestream2_get_bytes_left(&gb) > 0) {
397 131596 int b = bytestream2_peek_byteu(&gb) & 0x80;
398 131596 int x = bytestream2_get_byteu(&gb) & ~0x80;
399 131596 int l = 1;
400
401
2/2
✓ Branch 0 taken 424 times.
✓ Branch 1 taken 131172 times.
131596 if (b) {
402
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 424 times.
424 if (bytestream2_get_bytes_left(&gb) <= 0)
403 break;
404 424 l += bytestream2_get_byteu(&gb);
405 }
406 131596 k = j + l;
407
3/6
✓ Branch 0 taken 131596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 131596 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 131596 times.
131596 if (k > max || x == 0 || x > 32) {
408 av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
409 return AVERROR_INVALIDDATA;
410 }
411
412 131596 length_count[x] += l;
413
2/2
✓ Branch 0 taken 141312 times.
✓ Branch 1 taken 131596 times.
272908 for (; j < k; j++)
414 141312 len[j] = x;
415
416
2/2
✓ Branch 0 taken 131044 times.
✓ Branch 1 taken 552 times.
131596 if (j == max) {
417 552 j = 0;
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 552 times.
552 if (huff_build(avctx, len, length_count, &s->vlc[i], &s->multi[i], max, avctx)) {
419 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
420 return AVERROR_INVALIDDATA;
421 }
422 552 i++;
423
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 334 times.
552 if (i == s->planes) {
424 218 break;
425 }
426 334 memset(length_count, 0, sizeof(length_count));
427 }
428 }
429
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (i != s->planes) {
431 av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
432 return AVERROR_INVALIDDATA;
433 }
434
435 218 return 0;
436 }
437
438 218 static int magy_decode_frame(AVCodecContext *avctx, AVFrame *p,
439 int *got_frame, AVPacket *avpkt)
440 {
441 218 MagicYUVContext *s = avctx->priv_data;
442 GetByteContext gb;
443 uint32_t first_offset, offset, next_offset, header_size, slice_width;
444 int width, height, format, version, table_size;
445 int ret, i, j;
446
447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (avpkt->size < 36)
448 return AVERROR_INVALIDDATA;
449
450 218 bytestream2_init(&gb, avpkt->data, avpkt->size);
451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 218 times.
218 if (bytestream2_get_le32u(&gb) != MKTAG('M', 'A', 'G', 'Y'))
452 return AVERROR_INVALIDDATA;
453
454 218 header_size = bytestream2_get_le32u(&gb);
455
2/4
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 218 times.
218 if (header_size < 32 || header_size >= avpkt->size) {
456 av_log(avctx, AV_LOG_ERROR,
457 "header or packet too small %"PRIu32"\n", header_size);
458 return AVERROR_INVALIDDATA;
459 }
460
461 218 version = bytestream2_get_byteu(&gb);
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (version != 7) {
463 avpriv_request_sample(avctx, "Version %d", version);
464 return AVERROR_PATCHWELCOME;
465 }
466
467 218 format = bytestream2_get_byteu(&gb);
468
7/18
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 53 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 53 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
218 switch (format) {
469 53 case 0x65:
470 53 avctx->pix_fmt = AV_PIX_FMT_GBRP;
471 53 break;
472 2 case 0x66:
473 2 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
474 2 break;
475 53 case 0x67:
476 53 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
477 53 break;
478 2 case 0x68:
479 2 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
480 2 break;
481 53 case 0x69:
482 53 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
483 53 break;
484 2 case 0x6a:
485 2 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
486 2 break;
487 53 case 0x6b:
488 53 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
489 53 break;
490 case 0x6c:
491 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
492 break;
493 case 0x76:
494 avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
495 break;
496 case 0x6d:
497 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
498 break;
499 case 0x6e:
500 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
501 break;
502 case 0x6f:
503 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
504 break;
505 case 0x70:
506 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
507 break;
508 case 0x71:
509 avctx->pix_fmt = AV_PIX_FMT_GBRP14;
510 break;
511 case 0x72:
512 avctx->pix_fmt = AV_PIX_FMT_GBRAP14;
513 break;
514 case 0x73:
515 avctx->pix_fmt = AV_PIX_FMT_GRAY10;
516 break;
517 case 0x7b:
518 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
519 break;
520 default:
521 avpriv_request_sample(avctx, "Format 0x%X", format);
522 return AVERROR_PATCHWELCOME;
523 }
524 218 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
525 av_assert1(desc);
526 218 int is_rgb = s->decorrelate = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
527 218 s->hshift[1] = s->hshift[2] = desc->log2_chroma_w;
528 218 s->vshift[1] = s->vshift[2] = desc->log2_chroma_h;
529 218 s->bps = desc->comp[0].depth;
530 218 s->max = 1 << s->bps;
531
1/2
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
218 s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10;
532 218 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
533
534 218 bytestream2_skipu(&gb, 1);
535 218 s->color_matrix = bytestream2_get_byteu(&gb);
536 218 s->flags = bytestream2_get_byteu(&gb);
537 218 s->interlaced = !!(s->flags & 2);
538 218 bytestream2_skipu(&gb, 3);
539
540 218 width = bytestream2_get_le32u(&gb);
541 218 height = bytestream2_get_le32u(&gb);
542 218 ret = ff_set_dimensions(avctx, width, height);
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (ret < 0)
544 return ret;
545
546 218 slice_width = bytestream2_get_le32u(&gb);
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (slice_width != avctx->coded_width) {
548 avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
549 return AVERROR_PATCHWELCOME;
550 }
551 218 s->slice_height = bytestream2_get_le32u(&gb);
552
2/4
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 218 times.
218 if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
553 av_log(avctx, AV_LOG_ERROR,
554 "invalid slice height: %d\n", s->slice_height);
555 return AVERROR_INVALIDDATA;
556 }
557
3/4
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 165 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 53 times.
218 if (s->vshift[1] && (s->slice_height & ((1 << s->vshift[1]) - 1))) {
558 av_log(avctx, AV_LOG_ERROR,
559 "slice_height %d is not aligned to chroma vertical "
560 "subsampling (must be a multiple of %d)\n",
561 s->slice_height, 1 << s->vshift[1]);
562 return AVERROR_INVALIDDATA;
563 }
564
565 218 bytestream2_skipu(&gb, 4);
566
567 218 s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (s->nb_slices > INT_MAX / FFMAX(sizeof(Slice), 4 * 5)) {
569 av_log(avctx, AV_LOG_ERROR,
570 "invalid number of slices: %d\n", s->nb_slices);
571 return AVERROR_INVALIDDATA;
572 }
573
574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if ((s->slice_height >> s->vshift[1]) <= s->interlaced) {
575 av_log(avctx, AV_LOG_ERROR, "impossible slice height\n");
576 return AVERROR_INVALIDDATA;
577 }
578
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 212 times.
218 if (s->interlaced) {
579
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if ((avctx->coded_height % s->slice_height) && ((avctx->coded_height % s->slice_height) >> s->vshift[1]) < 2) {
580 av_log(avctx, AV_LOG_ERROR, "impossible height\n");
581 return AVERROR_INVALIDDATA;
582 }
583 }
584
585
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 218 times.
218 if (bytestream2_get_bytes_left(&gb) <= s->nb_slices * s->planes * 5)
586 return AVERROR_INVALIDDATA;
587
2/2
✓ Branch 0 taken 552 times.
✓ Branch 1 taken 218 times.
770 for (i = 0; i < s->planes; i++) {
588 552 av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 552 times.
552 if (!s->slices[i])
590 return AVERROR(ENOMEM);
591
592 552 offset = bytestream2_get_le32u(&gb);
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 552 times.
552 if (offset >= avpkt->size - header_size)
594 return AVERROR_INVALIDDATA;
595
596
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 334 times.
552 if (i == 0)
597 218 first_offset = offset;
598
599
2/2
✓ Branch 0 taken 1356 times.
✓ Branch 1 taken 552 times.
1908 for (j = 0; j < s->nb_slices - 1; j++) {
600 1356 s->slices[i][j].start = offset + header_size;
601
602 1356 next_offset = bytestream2_get_le32u(&gb);
603
2/4
✓ Branch 0 taken 1356 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1356 times.
1356 if (next_offset <= offset || next_offset >= avpkt->size - header_size)
604 return AVERROR_INVALIDDATA;
605
606 1356 s->slices[i][j].size = next_offset - offset;
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1356 times.
1356 if (s->slices[i][j].size < 2)
608 return AVERROR_INVALIDDATA;
609 1356 offset = next_offset;
610 }
611
612 552 s->slices[i][j].start = offset + header_size;
613 552 s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
614
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 552 times.
552 if (s->slices[i][j].size < 2)
616 return AVERROR_INVALIDDATA;
617 }
618
619
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 218 times.
218 if (bytestream2_get_byteu(&gb) != s->planes)
620 return AVERROR_INVALIDDATA;
621
622 218 bytestream2_skipu(&gb, s->nb_slices * s->planes);
623
624 218 table_size = header_size + first_offset - bytestream2_tell(&gb);
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (table_size < 2)
626 return AVERROR_INVALIDDATA;
627
628 218 ret = build_huffman(avctx, avpkt->data + bytestream2_tell(&gb),
629 table_size, s->max);
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (ret < 0)
631 return ret;
632
633
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 218 times.
218 if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
634 return ret;
635
636 218 s->buf = avpkt->data;
637 218 s->p = p;
638 218 avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
639
640
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 163 times.
218 if (is_rgb) {
641 55 FFSWAP(uint8_t*, p->data[0], p->data[1]);
642 55 FFSWAP(int, p->linesize[0], p->linesize[1]);
643 } else {
644
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 153 times.
163 switch (s->color_matrix) {
645 case 1:
646 p->colorspace = AVCOL_SPC_BT470BG;
647 break;
648 10 case 2:
649 10 p->colorspace = AVCOL_SPC_BT709;
650 10 break;
651 }
652
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 155 times.
163 p->color_range = (s->flags & 4) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
653 }
654
655 218 *got_frame = 1;
656
657 218 return avpkt->size;
658 }
659
660 22 static av_cold int magy_decode_init(AVCodecContext *avctx)
661 {
662 22 MagicYUVContext *s = avctx->priv_data;
663 22 ff_llviddsp_init(&s->llviddsp);
664 22 return 0;
665 }
666
667 22 static av_cold int magy_decode_end(AVCodecContext *avctx)
668 {
669 22 MagicYUVContext * const s = avctx->priv_data;
670 int i;
671
672
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 22 times.
110 for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
673 88 av_freep(&s->slices[i]);
674 88 s->slices_size[i] = 0;
675 88 ff_vlc_free(&s->vlc[i]);
676 88 ff_vlc_free_multi(&s->multi[i]);
677 }
678
679 22 return 0;
680 }
681
682 const FFCodec ff_magicyuv_decoder = {
683 .p.name = "magicyuv",
684 CODEC_LONG_NAME("MagicYUV video"),
685 .p.type = AVMEDIA_TYPE_VIDEO,
686 .p.id = AV_CODEC_ID_MAGICYUV,
687 .priv_data_size = sizeof(MagicYUVContext),
688 .init = magy_decode_init,
689 .close = magy_decode_end,
690 FF_CODEC_DECODE_CB(magy_decode_frame),
691 .p.capabilities = AV_CODEC_CAP_DR1 |
692 AV_CODEC_CAP_FRAME_THREADS |
693 AV_CODEC_CAP_SLICE_THREADS,
694 };
695