FFmpeg  4.4.7
magicyuv.c
Go to the documentation of this file.
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/pixdesc.h"
28 
29 #include "avcodec.h"
30 #include "bytestream.h"
31 #include "get_bits.h"
32 #include "huffyuvdsp.h"
33 #include "internal.h"
34 #include "lossless_videodsp.h"
35 #include "thread.h"
36 
37 typedef struct Slice {
38  uint32_t start;
39  uint32_t size;
40 } Slice;
41 
42 typedef enum Prediction {
43  LEFT = 1,
46 } Prediction;
47 
48 typedef struct HuffEntry {
49  uint8_t len;
50  uint16_t sym;
51 } HuffEntry;
52 
53 typedef struct MagicYUVContext {
55  int max;
56  int bps;
58  int nb_slices;
59  int planes; // number of encoded planes in bitstream
60  int decorrelate; // postprocessing work
61  int color_matrix; // video color matrix
62  int flags;
63  int interlaced; // video is interlaced
64  const uint8_t *buf; // pointer to AVPacket->data
65  int hshift[4];
66  int vshift[4];
67  Slice *slices[4]; // slice bitstream positions for each plane
68  unsigned int slices_size[4]; // slice sizes for each plane
69  VLC vlc[4]; // VLC for each plane
70  int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
71  int j, int threadnr);
74 
75 static int huff_build(const uint8_t len[], uint16_t codes_pos[33],
76  VLC *vlc, int nb_elems, void *logctx)
77 {
78  HuffEntry he[4096];
79 
80  for (int i = 31; i > 0; i--)
81  codes_pos[i] += codes_pos[i + 1];
82 
83  for (unsigned i = nb_elems; i-- > 0;)
84  he[--codes_pos[len[i]]] = (HuffEntry){ len[i], i };
85 
86  ff_free_vlc(vlc);
87  return ff_init_vlc_from_lengths(vlc, FFMIN(he[0].len, 12), nb_elems,
88  &he[0].len, sizeof(he[0]),
89  &he[0].sym, sizeof(he[0]), sizeof(he[0].sym),
90  0, 0, logctx);
91 }
92 
93 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
94  const uint16_t *diff, intptr_t w,
95  int *left, int *left_top, int max)
96 {
97  int i;
98  uint16_t l, lt;
99 
100  l = *left;
101  lt = *left_top;
102 
103  for (i = 0; i < w; i++) {
104  l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
105  l &= max;
106  lt = src1[i];
107  dst[i] = l;
108  }
109 
110  *left = l;
111  *left_top = lt;
112 }
113 
114 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
115  int j, int threadnr)
116 {
117  MagicYUVContext *s = avctx->priv_data;
118  int interlaced = s->interlaced;
119  const int bps = s->bps;
120  const int max = s->max - 1;
121  AVFrame *p = s->p;
122  int i, k, x;
123  GetBitContext gb;
124  uint16_t *dst;
125 
126  for (i = 0; i < s->planes; i++) {
127  int left, lefttop, top;
128  int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
129  int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
130  int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
131  ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
132  ptrdiff_t stride = p->linesize[i] / 2;
133  int flags, pred;
134  int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
135  s->slices[i][j].size);
136 
137  if (ret < 0)
138  return ret;
139 
140  flags = get_bits(&gb, 8);
141  pred = get_bits(&gb, 8);
142 
143  dst = (uint16_t *)p->data[i] + j * sheight * stride;
144  if (flags & 1) {
145  if (get_bits_left(&gb) < bps * width * height)
146  return AVERROR_INVALIDDATA;
147  for (k = 0; k < height; k++) {
148  for (x = 0; x < width; x++)
149  dst[x] = get_bits(&gb, bps);
150 
151  dst += stride;
152  }
153  } else {
154  for (k = 0; k < height; k++) {
155  for (x = 0; x < width; x++) {
156  int pix;
157  if (get_bits_left(&gb) <= 0)
158  return AVERROR_INVALIDDATA;
159 
160  pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
161  if (pix < 0)
162  return AVERROR_INVALIDDATA;
163 
164  dst[x] = pix;
165  }
166  dst += stride;
167  }
168  }
169 
170  switch (pred) {
171  case LEFT:
172  dst = (uint16_t *)p->data[i] + j * sheight * stride;
173  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
174  dst += stride;
175  if (interlaced) {
176  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
177  dst += stride;
178  }
179  for (k = 1 + interlaced; k < height; k++) {
180  s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
181  dst += stride;
182  }
183  break;
184  case GRADIENT:
185  dst = (uint16_t *)p->data[i] + j * sheight * stride;
186  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
187  dst += stride;
188  if (interlaced) {
189  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
190  dst += stride;
191  }
192  for (k = 1 + interlaced; k < height; k++) {
193  top = dst[-fake_stride];
194  left = top + dst[0];
195  dst[0] = left & max;
196  for (x = 1; x < width; x++) {
197  top = dst[x - fake_stride];
198  lefttop = dst[x - (fake_stride + 1)];
199  left += top - lefttop + dst[x];
200  dst[x] = left & max;
201  }
202  dst += stride;
203  }
204  break;
205  case MEDIAN:
206  dst = (uint16_t *)p->data[i] + j * sheight * stride;
207  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
208  dst += stride;
209  if (interlaced) {
210  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
211  dst += stride;
212  }
213  lefttop = left = dst[0];
214  for (k = 1 + interlaced; k < height; k++) {
215  magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
216  lefttop = left = dst[0];
217  dst += stride;
218  }
219  break;
220  default:
221  avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
222  }
223  }
224 
225  if (s->decorrelate) {
226  int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
227  int width = avctx->coded_width;
228  uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
229  uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
230  uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
231 
232  for (i = 0; i < height; i++) {
233  for (k = 0; k < width; k++) {
234  b[k] = (b[k] + g[k]) & max;
235  r[k] = (r[k] + g[k]) & max;
236  }
237  b += p->linesize[0] / 2;
238  g += p->linesize[1] / 2;
239  r += p->linesize[2] / 2;
240  }
241  }
242 
243  return 0;
244 }
245 
246 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
247  int j, int threadnr)
248 {
249  MagicYUVContext *s = avctx->priv_data;
250  int interlaced = s->interlaced;
251  AVFrame *p = s->p;
252  int i, k, x, min_width;
253  GetBitContext gb;
254  uint8_t *dst;
255 
256  for (i = 0; i < s->planes; i++) {
257  int left, lefttop, top;
258  int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
259  int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
260  int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
261  ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
262  ptrdiff_t stride = p->linesize[i];
263  const uint8_t *slice = s->buf + s->slices[i][j].start;
264  int flags, pred;
265 
266  flags = bytestream_get_byte(&slice);
267  pred = bytestream_get_byte(&slice);
268 
269  dst = p->data[i] + j * sheight * stride;
270  if (flags & 1) {
271  if (s->slices[i][j].size - 2 < width * height)
272  return AVERROR_INVALIDDATA;
273  for (k = 0; k < height; k++) {
274  bytestream_get_buffer(&slice, dst, width);
275  dst += stride;
276  }
277  } else {
278  int ret = init_get_bits8(&gb, slice, s->slices[i][j].size - 2);
279 
280  if (ret < 0)
281  return ret;
282 
283  for (k = 0; k < height; k++) {
284  for (x = 0; x < width; x++) {
285  int pix;
286  if (get_bits_left(&gb) <= 0)
287  return AVERROR_INVALIDDATA;
288 
289  pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
290  if (pix < 0)
291  return AVERROR_INVALIDDATA;
292 
293  dst[x] = pix;
294  }
295  dst += stride;
296  }
297  }
298 
299  switch (pred) {
300  case LEFT:
301  dst = p->data[i] + j * sheight * stride;
302  s->llviddsp.add_left_pred(dst, dst, width, 0);
303  dst += stride;
304  if (interlaced) {
305  s->llviddsp.add_left_pred(dst, dst, width, 0);
306  dst += stride;
307  }
308  for (k = 1 + interlaced; k < height; k++) {
309  s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
310  dst += stride;
311  }
312  break;
313  case GRADIENT:
314  dst = p->data[i] + j * sheight * stride;
315  s->llviddsp.add_left_pred(dst, dst, width, 0);
316  dst += stride;
317  if (interlaced) {
318  s->llviddsp.add_left_pred(dst, dst, width, 0);
319  dst += stride;
320  }
321  min_width = FFMIN(width, 32);
322  for (k = 1 + interlaced; k < height; k++) {
323  top = dst[-fake_stride];
324  left = top + dst[0];
325  dst[0] = left;
326  for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
327  top = dst[x - fake_stride];
328  lefttop = dst[x - (fake_stride + 1)];
329  left += top - lefttop + dst[x];
330  dst[x] = left;
331  }
332  if (width > 32)
333  s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
334  dst += stride;
335  }
336  break;
337  case MEDIAN:
338  dst = p->data[i] + j * sheight * stride;
339  s->llviddsp.add_left_pred(dst, dst, width, 0);
340  dst += stride;
341  if (interlaced) {
342  s->llviddsp.add_left_pred(dst, dst, width, 0);
343  dst += stride;
344  }
345  if (1 + interlaced < height)
346  lefttop = left = dst[0];
347  for (k = 1 + interlaced; k < height; k++) {
348  s->llviddsp.add_median_pred(dst, dst - fake_stride,
349  dst, width, &left, &lefttop);
350  lefttop = left = dst[0];
351  dst += stride;
352  }
353  break;
354  default:
355  avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
356  }
357  }
358 
359  if (s->decorrelate) {
360  int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
361  int width = avctx->coded_width;
362  uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
363  uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
364  uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
365 
366  for (i = 0; i < height; i++) {
367  s->llviddsp.add_bytes(b, g, width);
368  s->llviddsp.add_bytes(r, g, width);
369  b += p->linesize[0];
370  g += p->linesize[1];
371  r += p->linesize[2];
372  }
373  }
374 
375  return 0;
376 }
377 
378 static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
379  int table_size, int max)
380 {
381  MagicYUVContext *s = avctx->priv_data;
382  GetByteContext gb;
383  uint8_t len[4096];
384  uint16_t length_count[33] = { 0 };
385  int i = 0, j = 0, k;
386 
387  bytestream2_init(&gb, table, table_size);
388 
389  while (bytestream2_get_bytes_left(&gb) > 0) {
390  int b = bytestream2_peek_byteu(&gb) & 0x80;
391  int x = bytestream2_get_byteu(&gb) & ~0x80;
392  int l = 1;
393 
394  if (b) {
395  if (bytestream2_get_bytes_left(&gb) <= 0)
396  break;
397  l += bytestream2_get_byteu(&gb);
398  }
399  k = j + l;
400  if (k > max || x == 0 || x > 32) {
401  av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
402  return AVERROR_INVALIDDATA;
403  }
404 
405  length_count[x] += l;
406  for (; j < k; j++)
407  len[j] = x;
408 
409  if (j == max) {
410  j = 0;
411  if (huff_build(len, length_count, &s->vlc[i], max, avctx)) {
412  av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
413  return AVERROR_INVALIDDATA;
414  }
415  i++;
416  if (i == s->planes) {
417  break;
418  }
419  memset(length_count, 0, sizeof(length_count));
420  }
421  }
422 
423  if (i != s->planes) {
424  av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
425  return AVERROR_INVALIDDATA;
426  }
427 
428  return 0;
429 }
430 
431 static int magy_decode_frame(AVCodecContext *avctx, void *data,
432  int *got_frame, AVPacket *avpkt)
433 {
434  MagicYUVContext *s = avctx->priv_data;
435  ThreadFrame frame = { .f = data };
436  AVFrame *p = data;
437  GetByteContext gb;
438  uint32_t first_offset, offset, next_offset, header_size, slice_width;
439  int width, height, format, version, table_size;
440  int ret, i, j;
441 
442  if (avpkt->size < 36)
443  return AVERROR_INVALIDDATA;
444 
445  bytestream2_init(&gb, avpkt->data, avpkt->size);
446  if (bytestream2_get_le32u(&gb) != MKTAG('M', 'A', 'G', 'Y'))
447  return AVERROR_INVALIDDATA;
448 
449  header_size = bytestream2_get_le32u(&gb);
450  if (header_size < 32 || header_size >= avpkt->size) {
451  av_log(avctx, AV_LOG_ERROR,
452  "header or packet too small %"PRIu32"\n", header_size);
453  return AVERROR_INVALIDDATA;
454  }
455 
456  version = bytestream2_get_byteu(&gb);
457  if (version != 7) {
458  avpriv_request_sample(avctx, "Version %d", version);
459  return AVERROR_PATCHWELCOME;
460  }
461 
462  s->hshift[1] =
463  s->vshift[1] =
464  s->hshift[2] =
465  s->vshift[2] = 0;
466  s->decorrelate = 0;
467  s->bps = 8;
468 
469  format = bytestream2_get_byteu(&gb);
470  switch (format) {
471  case 0x65:
472  avctx->pix_fmt = AV_PIX_FMT_GBRP;
473  s->decorrelate = 1;
474  break;
475  case 0x66:
476  avctx->pix_fmt = AV_PIX_FMT_GBRAP;
477  s->decorrelate = 1;
478  break;
479  case 0x67:
480  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
481  break;
482  case 0x68:
483  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
484  s->hshift[1] =
485  s->hshift[2] = 1;
486  break;
487  case 0x69:
488  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
489  s->hshift[1] =
490  s->vshift[1] =
491  s->hshift[2] =
492  s->vshift[2] = 1;
493  break;
494  case 0x6a:
495  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
496  break;
497  case 0x6b:
498  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
499  break;
500  case 0x6c:
501  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
502  s->hshift[1] =
503  s->hshift[2] = 1;
504  s->bps = 10;
505  break;
506  case 0x76:
507  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
508  s->bps = 10;
509  break;
510  case 0x6d:
511  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
512  s->decorrelate = 1;
513  s->bps = 10;
514  break;
515  case 0x6e:
516  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
517  s->decorrelate = 1;
518  s->bps = 10;
519  break;
520  case 0x6f:
521  avctx->pix_fmt = AV_PIX_FMT_GBRP12;
522  s->decorrelate = 1;
523  s->bps = 12;
524  break;
525  case 0x70:
526  avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
527  s->decorrelate = 1;
528  s->bps = 12;
529  break;
530  case 0x73:
531  avctx->pix_fmt = AV_PIX_FMT_GRAY10;
532  s->bps = 10;
533  break;
534  case 0x7b:
535  avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
536  s->hshift[1] =
537  s->vshift[1] =
538  s->hshift[2] =
539  s->vshift[2] = 1;
540  s->bps = 10;
541  break;
542  default:
543  avpriv_request_sample(avctx, "Format 0x%X", format);
544  return AVERROR_PATCHWELCOME;
545  }
546  s->max = 1 << s->bps;
547  s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10;
548  s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
549 
550  bytestream2_skipu(&gb, 1);
551  s->color_matrix = bytestream2_get_byteu(&gb);
552  s->flags = bytestream2_get_byteu(&gb);
553  s->interlaced = !!(s->flags & 2);
554  bytestream2_skipu(&gb, 3);
555 
556  width = bytestream2_get_le32u(&gb);
557  height = bytestream2_get_le32u(&gb);
558  ret = ff_set_dimensions(avctx, width, height);
559  if (ret < 0)
560  return ret;
561 
562  slice_width = bytestream2_get_le32u(&gb);
563  if (slice_width != avctx->coded_width) {
564  avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
565  return AVERROR_PATCHWELCOME;
566  }
567  s->slice_height = bytestream2_get_le32u(&gb);
568  if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
569  av_log(avctx, AV_LOG_ERROR,
570  "invalid slice height: %d\n", s->slice_height);
571  return AVERROR_INVALIDDATA;
572  }
573 
574  bytestream2_skipu(&gb, 4);
575 
576  s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
577  if (s->nb_slices > INT_MAX / FFMAX(sizeof(Slice), 4 * 5)) {
578  av_log(avctx, AV_LOG_ERROR,
579  "invalid number of slices: %d\n", s->nb_slices);
580  return AVERROR_INVALIDDATA;
581  }
582 
583  if (s->interlaced) {
584  if ((s->slice_height >> s->vshift[1]) < 2) {
585  av_log(avctx, AV_LOG_ERROR, "impossible slice height\n");
586  return AVERROR_INVALIDDATA;
587  }
588  if ((avctx->coded_height % s->slice_height) && ((avctx->coded_height % s->slice_height) >> s->vshift[1]) < 2) {
589  av_log(avctx, AV_LOG_ERROR, "impossible height\n");
590  return AVERROR_INVALIDDATA;
591  }
592  }
593 
594  if (bytestream2_get_bytes_left(&gb) <= s->nb_slices * s->planes * 5)
595  return AVERROR_INVALIDDATA;
596  for (i = 0; i < s->planes; i++) {
597  av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
598  if (!s->slices[i])
599  return AVERROR(ENOMEM);
600 
601  offset = bytestream2_get_le32u(&gb);
602  if (offset >= avpkt->size - header_size)
603  return AVERROR_INVALIDDATA;
604 
605  if (i == 0)
606  first_offset = offset;
607 
608  for (j = 0; j < s->nb_slices - 1; j++) {
609  s->slices[i][j].start = offset + header_size;
610 
611  next_offset = bytestream2_get_le32u(&gb);
612  if (next_offset <= offset || next_offset >= avpkt->size - header_size)
613  return AVERROR_INVALIDDATA;
614 
615  s->slices[i][j].size = next_offset - offset;
616  if (s->slices[i][j].size < 2)
617  return AVERROR_INVALIDDATA;
618  offset = next_offset;
619  }
620 
621  s->slices[i][j].start = offset + header_size;
622  s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
623 
624  if (s->slices[i][j].size < 2)
625  return AVERROR_INVALIDDATA;
626  }
627 
628  if (bytestream2_get_byteu(&gb) != s->planes)
629  return AVERROR_INVALIDDATA;
630 
631  bytestream2_skipu(&gb, s->nb_slices * s->planes);
632 
633  table_size = header_size + first_offset - bytestream2_tell(&gb);
634  if (table_size < 2)
635  return AVERROR_INVALIDDATA;
636 
637  ret = build_huffman(avctx, avpkt->data + bytestream2_tell(&gb),
638  table_size, s->max);
639  if (ret < 0)
640  return ret;
641 
643  p->key_frame = 1;
644 
645  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
646  return ret;
647 
648  s->buf = avpkt->data;
649  s->p = p;
650  avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
651 
652  if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
653  avctx->pix_fmt == AV_PIX_FMT_GBRAP ||
654  avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
655  avctx->pix_fmt == AV_PIX_FMT_GBRAP10||
656  avctx->pix_fmt == AV_PIX_FMT_GBRAP12||
657  avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
658  FFSWAP(uint8_t*, p->data[0], p->data[1]);
659  FFSWAP(int, p->linesize[0], p->linesize[1]);
660  } else {
661  switch (s->color_matrix) {
662  case 1:
664  break;
665  case 2:
667  break;
668  }
669  p->color_range = (s->flags & 4) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
670  }
671 
672  *got_frame = 1;
673 
674  return avpkt->size;
675 }
676 
678 {
679  MagicYUVContext *s = avctx->priv_data;
680  ff_llviddsp_init(&s->llviddsp);
681  return 0;
682 }
683 
685 {
686  MagicYUVContext * const s = avctx->priv_data;
687  int i;
688 
689  for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
690  av_freep(&s->slices[i]);
691  s->slices_size[i] = 0;
692  ff_free_vlc(&s->vlc[i]);
693  }
694 
695  return 0;
696 }
697 
699  .name = "magicyuv",
700  .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
701  .type = AVMEDIA_TYPE_VIDEO,
702  .id = AV_CODEC_ID_MAGICYUV,
703  .priv_data_size = sizeof(MagicYUVContext),
705  .close = magy_decode_end,
707  .capabilities = AV_CODEC_CAP_DR1 |
710  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
711 };
static const char *const format[]
Definition: af_aiir.c:456
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:431
int ff_init_vlc_from_lengths(VLC *vlc_arg, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: bitstream.c:381
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:363
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define FFMAX(a, b)
Definition: common.h:103
#define NULL
Definition: coverity.c:32
#define max(a, b)
Definition: cuda_runtime.h:33
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
int
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:112
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:108
@ AV_CODEC_ID_MAGICYUV
Definition: codec_id.h:269
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:502
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
for(j=16;j >0;--j)
int i
Definition: input.c:407
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
version
Definition: libkvazaar.c:326
uint8_t w
Definition: llviddspenc.c:39
void ff_llviddsp_init(LLVidDSPContext *c)
int stride
Definition: mace.c:144
static av_cold int magy_decode_init(AVCodecContext *avctx)
Definition: magicyuv.c:677
static av_cold int magy_decode_end(AVCodecContext *avctx)
Definition: magicyuv.c:684
static int magy_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: magicyuv.c:431
static int magy_decode_slice10(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition: magicyuv.c:114
static int huff_build(const uint8_t len[], uint16_t codes_pos[33], VLC *vlc, int nb_elems, void *logctx)
Definition: magicyuv.c:75
static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1, const uint16_t *diff, intptr_t w, int *left, int *left_top, int max)
Definition: magicyuv.c:93
Prediction
Definition: magicyuv.c:42
@ LEFT
Definition: magicyuv.c:43
@ GRADIENT
Definition: magicyuv.c:44
@ MEDIAN
Definition: magicyuv.c:45
AVCodec ff_magicyuv_decoder
Definition: magicyuv.c:698
static int build_huffman(AVCodecContext *avctx, const uint8_t *table, int table_size, int max)
Definition: magicyuv.c:378
static int magy_decode_slice(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition: magicyuv.c:246
#define mid_pred
Definition: mathops.h:97
unsigned bps
Definition: movenc.c:1612
const char data[16]
Definition: mxf.c:142
uint8_t interlaced
Definition: mxfenc.c:2208
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:420
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:569
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:415
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:416
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:419
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:514
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:518
static const uint16_t table[]
Definition: prosumer.c:206
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
#define FF_ARRAY_ELEMS(a)
static const float pred[4]
Definition: siprdata.h:259
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int coded_height
Definition: avcodec.h:724
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:1848
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:724
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:562
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:573
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Definition: exr.c:93
uint8_t len
Definition: exr.c:94
uint16_t sym
Definition: exr.c:95
int color_matrix
Definition: magicyuv.c:61
int slice_height
Definition: magicyuv.c:57
int hshift[4]
Definition: magicyuv.c:65
int vshift[4]
Definition: magicyuv.c:66
AVFrame * p
Definition: magicyuv.c:54
int(* magy_decode_slice)(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition: magicyuv.c:70
VLC vlc[4]
Definition: magicyuv.c:69
LLVidDSPContext llviddsp
Definition: magicyuv.c:72
const uint8_t * buf
Definition: magicyuv.c:64
unsigned int slices_size[4]
Definition: magicyuv.c:68
Slice * slices[4]
Definition: magicyuv.c:67
Definition: magicyuv.c:37
uint32_t start
Definition: magicyuv.c:38
uint32_t size
Definition: magicyuv.c:39
Definition: vlc.h:26
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
#define src1
Definition: h264pred.c:140
#define height
#define width
const char * b
Definition: vf_curves.c:118
const char * g
Definition: vf_curves.c:117
const char * r
Definition: vf_curves.c:116
if(ret< 0)
Definition: vf_mcdeint.c:282
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len