FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "libavutil/pixdesc.h"
26 #include "libavutil/qsort.h"
27 
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "get_bits.h"
31 #include "huffyuvdsp.h"
32 #include "internal.h"
33 #include "lossless_videodsp.h"
34 #include "thread.h"
35 
36 typedef struct Slice {
37  uint32_t start;
38  uint32_t size;
39 } Slice;
40 
41 typedef enum Prediction {
42  LEFT = 1,
45 } Prediction;
46 
47 typedef struct HuffEntry {
48  uint16_t sym;
50  uint32_t code;
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  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  uint8_t len[4][4096]; // table of code lengths for each plane
70  VLC vlc[4]; // VLC for each plane
72  int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
73  int j, int threadnr);
76 
77 static int huff_cmp_len(const void *a, const void *b)
78 {
79  const HuffEntry *aa = a, *bb = b;
80  return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
81 }
82 
83 static int huff_cmp_len10(const void *a, const void *b)
84 {
85  const HuffEntry *aa = a, *bb = b;
86  return (aa->len - bb->len) * 1024 + aa->sym - bb->sym;
87 }
88 
89 static int huff_cmp_len12(const void *a, const void *b)
90 {
91  const HuffEntry *aa = a, *bb = b;
92  return (aa->len - bb->len) * 4096 + aa->sym - bb->sym;
93 }
94 
95 static int huff_build10(VLC *vlc, uint8_t *len)
96 {
97  HuffEntry he[1024];
98  uint32_t codes[1024];
99  uint8_t bits[1024];
100  uint16_t syms[1024];
101  uint32_t code;
102  int i;
103 
104  for (i = 0; i < 1024; i++) {
105  he[i].sym = 1023 - i;
106  he[i].len = len[i];
107  if (len[i] == 0 || len[i] > 32)
108  return AVERROR_INVALIDDATA;
109  }
110  AV_QSORT(he, 1024, HuffEntry, huff_cmp_len10);
111 
112  code = 1;
113  for (i = 1023; i >= 0; i--) {
114  codes[i] = code >> (32 - he[i].len);
115  bits[i] = he[i].len;
116  syms[i] = he[i].sym;
117  code += 0x80000000u >> (he[i].len - 1);
118  }
119 
120  ff_free_vlc(vlc);
121  return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024,
122  bits, sizeof(*bits), sizeof(*bits),
123  codes, sizeof(*codes), sizeof(*codes),
124  syms, sizeof(*syms), sizeof(*syms), 0);
125 }
126 
127 static int huff_build12(VLC *vlc, uint8_t *len)
128 {
129  HuffEntry he[4096];
130  uint32_t codes[4096];
131  uint8_t bits[4096];
132  uint16_t syms[4096];
133  uint32_t code;
134  int i;
135 
136  for (i = 0; i < 4096; i++) {
137  he[i].sym = 4095 - i;
138  he[i].len = len[i];
139  if (len[i] == 0 || len[i] > 32)
140  return AVERROR_INVALIDDATA;
141  }
142  AV_QSORT(he, 4096, HuffEntry, huff_cmp_len12);
143 
144  code = 1;
145  for (i = 4095; i >= 0; i--) {
146  codes[i] = code >> (32 - he[i].len);
147  bits[i] = he[i].len;
148  syms[i] = he[i].sym;
149  code += 0x80000000u >> (he[i].len - 1);
150  }
151 
152  ff_free_vlc(vlc);
153  return ff_init_vlc_sparse(vlc, FFMIN(he[4095].len, 14), 4096,
154  bits, sizeof(*bits), sizeof(*bits),
155  codes, sizeof(*codes), sizeof(*codes),
156  syms, sizeof(*syms), sizeof(*syms), 0);
157 }
158 
159 static int huff_build(VLC *vlc, uint8_t *len)
160 {
161  HuffEntry he[256];
162  uint32_t codes[256];
163  uint8_t bits[256];
164  uint8_t syms[256];
165  uint32_t code;
166  int i;
167 
168  for (i = 0; i < 256; i++) {
169  he[i].sym = 255 - i;
170  he[i].len = len[i];
171  if (len[i] == 0 || len[i] > 32)
172  return AVERROR_INVALIDDATA;
173  }
174  AV_QSORT(he, 256, HuffEntry, huff_cmp_len);
175 
176  code = 1;
177  for (i = 255; i >= 0; i--) {
178  codes[i] = code >> (32 - he[i].len);
179  bits[i] = he[i].len;
180  syms[i] = he[i].sym;
181  code += 0x80000000u >> (he[i].len - 1);
182  }
183 
184  ff_free_vlc(vlc);
185  return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
186  bits, sizeof(*bits), sizeof(*bits),
187  codes, sizeof(*codes), sizeof(*codes),
188  syms, sizeof(*syms), sizeof(*syms), 0);
189 }
190 
191 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
192  const uint16_t *diff, intptr_t w,
193  int *left, int *left_top, int max)
194 {
195  int i;
196  uint16_t l, lt;
197 
198  l = *left;
199  lt = *left_top;
200 
201  for (i = 0; i < w; i++) {
202  l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
203  l &= max;
204  lt = src1[i];
205  dst[i] = l;
206  }
207 
208  *left = l;
209  *left_top = lt;
210 }
211 
212 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
213  int j, int threadnr)
214 {
215  MagicYUVContext *s = avctx->priv_data;
216  int interlaced = s->interlaced;
217  const int bps = s->bps;
218  const int max = s->max - 1;
219  AVFrame *p = s->p;
220  int i, k, x;
221  GetBitContext gb;
222  uint16_t *dst;
223 
224  for (i = 0; i < s->planes; i++) {
225  int left, lefttop, top;
226  int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
227  int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
228  int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
229  ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
230  ptrdiff_t stride = p->linesize[i] / 2;
231  int flags, pred;
232  int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
233  s->slices[i][j].size);
234 
235  if (ret < 0)
236  return ret;
237 
238  flags = get_bits(&gb, 8);
239  pred = get_bits(&gb, 8);
240 
241  dst = (uint16_t *)p->data[i] + j * sheight * stride;
242  if (flags & 1) {
243  for (k = 0; k < height; k++) {
244  for (x = 0; x < width; x++)
245  dst[x] = get_bits(&gb, bps);
246 
247  dst += stride;
248  }
249  } else {
250  for (k = 0; k < height; k++) {
251  for (x = 0; x < width; x++) {
252  int pix;
253  if (get_bits_left(&gb) <= 0)
254  return AVERROR_INVALIDDATA;
255 
256  pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
257  if (pix < 0)
258  return AVERROR_INVALIDDATA;
259 
260  dst[x] = max - pix;
261  }
262  dst += stride;
263  }
264  }
265 
266  switch (pred) {
267  case LEFT:
268  dst = (uint16_t *)p->data[i] + j * sheight * stride;
269  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
270  dst += stride;
271  if (interlaced) {
272  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
273  dst += stride;
274  }
275  for (k = 1 + interlaced; k < height; k++) {
276  s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
277  dst += stride;
278  }
279  break;
280  case GRADIENT:
281  dst = (uint16_t *)p->data[i] + j * sheight * stride;
282  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
283  left = lefttop = 0;
284  dst += stride;
285  if (interlaced) {
286  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
287  left = lefttop = 0;
288  dst += stride;
289  }
290  for (k = 1 + interlaced; k < height; k++) {
291  top = dst[-fake_stride];
292  left = top + dst[0];
293  dst[0] = left & max;
294  for (x = 1; x < width; x++) {
295  top = dst[x - fake_stride];
296  lefttop = dst[x - (fake_stride + 1)];
297  left += top - lefttop + dst[x];
298  dst[x] = left & max;
299  }
300  dst += stride;
301  }
302  break;
303  case MEDIAN:
304  dst = (uint16_t *)p->data[i] + j * sheight * stride;
305  lefttop = left = dst[0];
306  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
307  dst += stride;
308  if (interlaced) {
309  lefttop = left = dst[0];
310  s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
311  dst += stride;
312  }
313  for (k = 1 + interlaced; k < height; k++) {
314  magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
315  lefttop = left = dst[0];
316  dst += stride;
317  }
318  break;
319  default:
320  avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
321  }
322  }
323 
324  if (s->decorrelate) {
325  int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
326  int width = avctx->coded_width;
327  uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
328  uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
329  uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
330 
331  for (i = 0; i < height; i++) {
332  for (k = 0; k < width; k++) {
333  b[k] = (b[k] + g[k]) & max;
334  r[k] = (r[k] + g[k]) & max;
335  }
336  b += p->linesize[0] / 2;
337  g += p->linesize[1] / 2;
338  r += p->linesize[2] / 2;
339  }
340  }
341 
342  return 0;
343 }
344 
345 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
346  int j, int threadnr)
347 {
348  MagicYUVContext *s = avctx->priv_data;
349  int interlaced = s->interlaced;
350  AVFrame *p = s->p;
351  int i, k, x;
352  GetBitContext gb;
353  uint8_t *dst;
354 
355  for (i = 0; i < s->planes; i++) {
356  int left, lefttop, top;
357  int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
358  int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
359  int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
360  ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
361  ptrdiff_t stride = p->linesize[i];
362  int flags, pred;
363  int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
364  s->slices[i][j].size);
365 
366  if (ret < 0)
367  return ret;
368 
369  flags = get_bits(&gb, 8);
370  pred = get_bits(&gb, 8);
371 
372  dst = p->data[i] + j * sheight * stride;
373  if (flags & 1) {
374  for (k = 0; k < height; k++) {
375  for (x = 0; x < width; x++)
376  dst[x] = get_bits(&gb, 8);
377 
378  dst += stride;
379  }
380  } else {
381  for (k = 0; k < height; k++) {
382  for (x = 0; x < width; x++) {
383  int pix;
384  if (get_bits_left(&gb) <= 0)
385  return AVERROR_INVALIDDATA;
386 
387  pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
388  if (pix < 0)
389  return AVERROR_INVALIDDATA;
390 
391  dst[x] = 255 - pix;
392  }
393  dst += stride;
394  }
395  }
396 
397  switch (pred) {
398  case LEFT:
399  dst = p->data[i] + j * sheight * stride;
400  s->llviddsp.add_left_pred(dst, dst, width, 0);
401  dst += stride;
402  if (interlaced) {
403  s->llviddsp.add_left_pred(dst, dst, width, 0);
404  dst += stride;
405  }
406  for (k = 1 + interlaced; k < height; k++) {
407  s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
408  dst += stride;
409  }
410  break;
411  case GRADIENT:
412  dst = p->data[i] + j * sheight * stride;
413  s->llviddsp.add_left_pred(dst, dst, width, 0);
414  left = lefttop = 0;
415  dst += stride;
416  if (interlaced) {
417  s->llviddsp.add_left_pred(dst, dst, width, 0);
418  left = lefttop = 0;
419  dst += stride;
420  }
421  for (k = 1 + interlaced; k < height; k++) {
422  top = dst[-fake_stride];
423  left = top + dst[0];
424  dst[0] = left;
425  for (x = 1; x < width; x++) {
426  top = dst[x - fake_stride];
427  lefttop = dst[x - (fake_stride + 1)];
428  left += top - lefttop + dst[x];
429  dst[x] = left;
430  }
431  dst += stride;
432  }
433  break;
434  case MEDIAN:
435  dst = p->data[i] + j * sheight * stride;
436  lefttop = left = dst[0];
437  s->llviddsp.add_left_pred(dst, dst, width, 0);
438  dst += stride;
439  if (interlaced) {
440  lefttop = left = dst[0];
441  s->llviddsp.add_left_pred(dst, dst, width, 0);
442  dst += stride;
443  }
444  for (k = 1 + interlaced; k < height; k++) {
445  s->llviddsp.add_median_pred(dst, dst - fake_stride,
446  dst, width, &left, &lefttop);
447  lefttop = left = dst[0];
448  dst += stride;
449  }
450  break;
451  default:
452  avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
453  }
454  }
455 
456  if (s->decorrelate) {
457  int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
458  int width = avctx->coded_width;
459  uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
460  uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
461  uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
462 
463  for (i = 0; i < height; i++) {
464  s->llviddsp.add_bytes(b, g, width);
465  s->llviddsp.add_bytes(r, g, width);
466  b += p->linesize[0];
467  g += p->linesize[1];
468  r += p->linesize[2];
469  }
470  }
471 
472  return 0;
473 }
474 
475 static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max)
476 {
477  MagicYUVContext *s = avctx->priv_data;
478  int i = 0, j = 0, k;
479 
480  memset(s->len, 0, sizeof(s->len));
481  while (get_bits_left(gbit) >= 8) {
482  int b = get_bits(gbit, 1);
483  int x = get_bits(gbit, 7);
484  int l = get_bitsz(gbit, b * 8) + 1;
485 
486  for (k = 0; k < l; k++)
487  if (j + k < max)
488  s->len[i][j + k] = x;
489 
490  j += l;
491  if (j == max) {
492  j = 0;
493  if (s->huff_build(&s->vlc[i], s->len[i])) {
494  av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
495  return AVERROR_INVALIDDATA;
496  }
497  i++;
498  if (i == s->planes) {
499  break;
500  }
501  } else if (j > max) {
502  av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
503  return AVERROR_INVALIDDATA;
504  }
505  }
506 
507  if (i != s->planes) {
508  av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
509  return AVERROR_INVALIDDATA;
510  }
511 
512  return 0;
513 }
514 
515 static int magy_decode_frame(AVCodecContext *avctx, void *data,
516  int *got_frame, AVPacket *avpkt)
517 {
518  MagicYUVContext *s = avctx->priv_data;
519  ThreadFrame frame = { .f = data };
520  AVFrame *p = data;
521  GetByteContext gbyte;
522  GetBitContext gbit;
523  uint32_t first_offset, offset, next_offset, header_size, slice_width;
524  int width, height, format, version, table_size;
525  int ret, i, j;
526 
527  bytestream2_init(&gbyte, avpkt->data, avpkt->size);
528  if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
529  return AVERROR_INVALIDDATA;
530 
531  header_size = bytestream2_get_le32(&gbyte);
532  if (header_size < 32 || header_size >= avpkt->size) {
533  av_log(avctx, AV_LOG_ERROR,
534  "header or packet too small %"PRIu32"\n", header_size);
535  return AVERROR_INVALIDDATA;
536  }
537 
538  version = bytestream2_get_byte(&gbyte);
539  if (version != 7) {
540  avpriv_request_sample(avctx, "Version %d", version);
541  return AVERROR_PATCHWELCOME;
542  }
543 
544  s->hshift[1] =
545  s->vshift[1] =
546  s->hshift[2] =
547  s->vshift[2] = 0;
548  s->decorrelate = 0;
549  s->max = 256;
550  s->bps = 8;
551  s->huff_build = huff_build;
553 
554  format = bytestream2_get_byte(&gbyte);
555  switch (format) {
556  case 0x65:
557  avctx->pix_fmt = AV_PIX_FMT_GBRP;
558  s->decorrelate = 1;
559  break;
560  case 0x66:
561  avctx->pix_fmt = AV_PIX_FMT_GBRAP;
562  s->decorrelate = 1;
563  break;
564  case 0x67:
565  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
566  break;
567  case 0x68:
568  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
569  s->hshift[1] =
570  s->hshift[2] = 1;
571  break;
572  case 0x69:
573  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
574  s->hshift[1] =
575  s->vshift[1] =
576  s->hshift[2] =
577  s->vshift[2] = 1;
578  break;
579  case 0x6a:
580  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
581  break;
582  case 0x6b:
583  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
584  break;
585  case 0x6c:
586  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
587  s->hshift[1] =
588  s->hshift[2] = 1;
589  s->max = 1024;
592  s->bps = 10;
593  break;
594  case 0x6d:
595  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
596  s->decorrelate = 1;
597  s->max = 1024;
600  s->bps = 10;
601  break;
602  case 0x6e:
603  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
604  s->decorrelate = 1;
605  s->max = 1024;
608  s->bps = 10;
609  break;
610  case 0x6f:
611  avctx->pix_fmt = AV_PIX_FMT_GBRP12;
612  s->decorrelate = 1;
613  s->max = 4096;
616  s->bps = 12;
617  break;
618  case 0x70:
619  avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
620  s->decorrelate = 1;
621  s->max = 4096;
624  s->bps = 12;
625  break;
626  case 0x73:
627  avctx->pix_fmt = AV_PIX_FMT_GRAY10;
628  s->max = 1024;
631  s->bps = 10;
632  break;
633  default:
634  avpriv_request_sample(avctx, "Format 0x%X", format);
635  return AVERROR_PATCHWELCOME;
636  }
638 
639  bytestream2_skip(&gbyte, 1);
640  s->color_matrix = bytestream2_get_byte(&gbyte);
641  s->flags = bytestream2_get_byte(&gbyte);
642  s->interlaced = !!(s->flags & 2);
643  bytestream2_skip(&gbyte, 3);
644 
645  width = bytestream2_get_le32(&gbyte);
646  height = bytestream2_get_le32(&gbyte);
647  ret = ff_set_dimensions(avctx, width, height);
648  if (ret < 0)
649  return ret;
650 
651  slice_width = bytestream2_get_le32(&gbyte);
652  if (slice_width != avctx->coded_width) {
653  avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
654  return AVERROR_PATCHWELCOME;
655  }
656  s->slice_height = bytestream2_get_le32(&gbyte);
657  if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
658  av_log(avctx, AV_LOG_ERROR,
659  "invalid slice height: %d\n", s->slice_height);
660  return AVERROR_INVALIDDATA;
661  }
662 
663  bytestream2_skip(&gbyte, 4);
664 
665  s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
666  if (s->nb_slices > INT_MAX / sizeof(Slice)) {
667  av_log(avctx, AV_LOG_ERROR,
668  "invalid number of slices: %d\n", s->nb_slices);
669  return AVERROR_INVALIDDATA;
670  }
671 
672  for (i = 0; i < s->planes; i++) {
673  av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
674  if (!s->slices[i])
675  return AVERROR(ENOMEM);
676 
677  offset = bytestream2_get_le32(&gbyte);
678  if (offset >= avpkt->size - header_size)
679  return AVERROR_INVALIDDATA;
680 
681  if (i == 0)
682  first_offset = offset;
683 
684  for (j = 0; j < s->nb_slices - 1; j++) {
685  s->slices[i][j].start = offset + header_size;
686 
687  next_offset = bytestream2_get_le32(&gbyte);
688  if (next_offset <= offset || next_offset >= avpkt->size - header_size)
689  return AVERROR_INVALIDDATA;
690 
691  s->slices[i][j].size = next_offset - offset;
692  offset = next_offset;
693  }
694 
695  s->slices[i][j].start = offset + header_size;
696  s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
697  }
698 
699  if (bytestream2_get_byte(&gbyte) != s->planes)
700  return AVERROR_INVALIDDATA;
701 
702  bytestream2_skip(&gbyte, s->nb_slices * s->planes);
703 
704  table_size = header_size + first_offset - bytestream2_tell(&gbyte);
705  if (table_size < 2)
706  return AVERROR_INVALIDDATA;
707 
708  ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
709  if (ret < 0)
710  return ret;
711 
712  ret = build_huffman(avctx, &gbit, s->max);
713  if (ret < 0)
714  return ret;
715 
717  p->key_frame = 1;
718 
719  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
720  return ret;
721 
722  s->buf = avpkt->data;
723  s->p = p;
724  avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
725 
726  if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
727  avctx->pix_fmt == AV_PIX_FMT_GBRAP ||
728  avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
729  avctx->pix_fmt == AV_PIX_FMT_GBRAP10||
730  avctx->pix_fmt == AV_PIX_FMT_GBRAP12||
731  avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
732  FFSWAP(uint8_t*, p->data[0], p->data[1]);
733  FFSWAP(int, p->linesize[0], p->linesize[1]);
734  } else {
735  switch (s->color_matrix) {
736  case 1:
738  break;
739  case 2:
741  break;
742  }
744  }
745 
746  *got_frame = 1;
747 
748  return avpkt->size;
749 }
750 
751 #if HAVE_THREADS
752 static int magy_init_thread_copy(AVCodecContext *avctx)
753 {
754  MagicYUVContext *s = avctx->priv_data;
755  int i;
756 
757  for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
758  s->slices[i] = NULL;
759  s->slices_size[i] = 0;
760  }
761 
762  return 0;
763 }
764 #endif
765 
767 {
768  MagicYUVContext *s = avctx->priv_data;
770  return 0;
771 }
772 
774 {
775  MagicYUVContext * const s = avctx->priv_data;
776  int i;
777 
778  for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
779  av_freep(&s->slices[i]);
780  s->slices_size[i] = 0;
781  ff_free_vlc(&s->vlc[i]);
782  }
783 
784  return 0;
785 }
786 
788  .name = "magicyuv",
789  .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
790  .type = AVMEDIA_TYPE_VIDEO,
791  .id = AV_CODEC_ID_MAGICYUV,
792  .priv_data_size = sizeof(MagicYUVContext),
794  .init_thread_copy = ONLY_IF_THREADS_ENABLED(magy_init_thread_copy),
795  .close = magy_decode_end,
796  .decode = magy_decode_frame,
797  .capabilities = AV_CODEC_CAP_DR1 |
800  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
801 };
int(* add_left_pred_int16)(uint16_t *dst, const uint16_t *src, unsigned mask, ptrdiff_t w, unsigned left)
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:486
#define NULL
Definition: coverity.c:32
static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max)
Definition: magicyuv.c:475
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int vshift[4]
Definition: magicyuv.c:66
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
int hshift[4]
Definition: magicyuv.c:65
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1963
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:399
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:392
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2459
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:211
const char * g
Definition: vf_curves.c:112
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:490
int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:268
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
int size
Definition: avcodec.h:1680
const char * b
Definition: vf_curves.c:113
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:395
static int huff_build10(VLC *vlc, uint8_t *len)
Definition: magicyuv.c:95
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
int version
Definition: avisynth_c.h:766
unsigned int slices_size[4]
Definition: magicyuv.c:68
uint8_t len[4][4096]
Definition: magicyuv.c:69
AVCodec.
Definition: avcodec.h:3739
LLVidDSPContext llviddsp
Definition: magicyuv.c:74
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:361
void(* add_bytes)(uint8_t *dst, uint8_t *src, ptrdiff_t w)
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#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:40
uint8_t bits
Definition: crc.c:296
uint8_t
#define av_cold
Definition: attributes.h:82
Multithreading support functions.
int(* magy_decode_slice)(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition: magicyuv.c:72
static AVFrame * frame
Prediction
Definition: magicyuv.c:41
#define height
uint8_t * data
Definition: avcodec.h:1679
static int flags
Definition: log.c:57
bitstream reader API header.
uint32_t code
Definition: magicyuv.c:50
static int huff_cmp_len12(const void *a, const void *b)
Definition: magicyuv.c:89
#define av_log(a,...)
static int magy_decode_slice(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition: magicyuv.c:345
static int huff_cmp_len(const void *a, const void *b)
Definition: magicyuv.c:77
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int slice_height
Definition: magicyuv.c:57
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
const char * r
Definition: vf_curves.c:111
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:446
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:457
uint16_t width
Definition: gdv.c:47
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:400
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1065
Definition: vlc.h:26
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:218
Slice * slices[4]
Definition: magicyuv.c:67
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:469
static int huff_build12(VLC *vlc, uint8_t *len)
Definition: magicyuv.c:127
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:284
#define FFMIN(a, b)
Definition: common.h:96
uint8_t interlaced
Definition: mxfenc.c:1898
static int huff_cmp_len10(const void *a, const void *b)
Definition: magicyuv.c:83
Definition: magicyuv.c:36
AVFrame * p
Definition: magicyuv.c:54
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:554
#define FF_ARRAY_ELEMS(a)
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:510
int bits
Definition: vlc.h:27
static const float pred[4]
Definition: siprdata.h:259
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1069
static av_cold int magy_decode_init(AVCodecContext *avctx)
Definition: magicyuv.c:766
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
#define src1
Definition: h264pred.c:139
uint8_t len
Definition: magicyuv.c:49
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:456
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:189
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1761
static int magy_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: magicyuv.c:515
uint32_t size
Definition: magicyuv.c:38
void ff_llviddsp_init(LLVidDSPContext *c)
uint32_t start
Definition: magicyuv.c:37
int coded_height
Definition: avcodec.h:1963
static const char * format
Definition: movenc.c:47
AVCodec ff_magicyuv_decoder
Definition: magicyuv.c:787
Definition: magicyuv.c:42
#define mid_pred
Definition: mathops.h:97
#define u(width,...)
VLC vlc[4]
Definition: magicyuv.c:70
static int magy_decode_slice10(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition: magicyuv.c:212
uint8_t * buf
Definition: magicyuv.c:64
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:396
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:380
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:509
static av_cold int magy_decode_end(AVCodecContext *avctx)
Definition: magicyuv.c:773
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
int
int(* huff_build)(VLC *vlc, uint8_t *len)
Definition: magicyuv.c:71
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:279
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:229
uint16_t sym
Definition: magicyuv.c:48
unsigned bps
Definition: movenc.c:1410
void * priv_data
Definition: avcodec.h:1803
static int huff_build(VLC *vlc, uint8_t *len)
Definition: magicyuv.c:159
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int(* add_left_pred)(uint8_t *dst, const uint8_t *src, ptrdiff_t w, int left)
int len
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:3252
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:279
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:191
#define av_freep(p)
#define FFSWAP(type, a, b)
Definition: common.h:99
#define stride
#define MKTAG(a, b, c, d)
Definition: common.h:342
This structure stores compressed data.
Definition: avcodec.h:1656
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:1002
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
Definition: qsort.h:33
for(j=16;j >0;--j)
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:276
void(* add_median_pred)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, ptrdiff_t w, int *left, int *left_top)
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
int color_matrix
Definition: magicyuv.c:61