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;
57  int nb_slices;
58  int planes; // number of encoded planes in bitstream
59  int decorrelate; // postprocessing work
60  int color_matrix; // video color matrix
61  int flags;
62  int interlaced; // video is interlaced
63  uint8_t *buf; // pointer to AVPacket->data
64  int hshift[4];
65  int vshift[4];
66  Slice *slices[4]; // slice bitstream positions for each plane
67  unsigned int slices_size[4]; // slice sizes for each plane
68  uint8_t len[4][1024]; // table of code lengths for each plane
69  VLC vlc[4]; // VLC for each plane
71  int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
72  int j, int threadnr);
75 
76 static int huff_cmp_len(const void *a, const void *b)
77 {
78  const HuffEntry *aa = a, *bb = b;
79  return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
80 }
81 
82 static int huff_cmp_len10(const void *a, const void *b)
83 {
84  const HuffEntry *aa = a, *bb = b;
85  return (aa->len - bb->len) * 1024 + aa->sym - bb->sym;
86 }
87 
88 static int huff_build10(VLC *vlc, uint8_t *len)
89 {
90  HuffEntry he[1024];
91  uint32_t codes[1024];
92  uint8_t bits[1024];
93  uint16_t syms[1024];
94  uint32_t code;
95  int i;
96 
97  for (i = 0; i < 1024; i++) {
98  he[i].sym = 1023 - i;
99  he[i].len = len[i];
100  if (len[i] == 0 || len[i] > 32)
101  return AVERROR_INVALIDDATA;
102  }
103  AV_QSORT(he, 1024, HuffEntry, huff_cmp_len10);
104 
105  code = 1;
106  for (i = 1023; i >= 0; i--) {
107  codes[i] = code >> (32 - he[i].len);
108  bits[i] = he[i].len;
109  syms[i] = he[i].sym;
110  code += 0x80000000u >> (he[i].len - 1);
111  }
112 
113  ff_free_vlc(vlc);
114  return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024,
115  bits, sizeof(*bits), sizeof(*bits),
116  codes, sizeof(*codes), sizeof(*codes),
117  syms, sizeof(*syms), sizeof(*syms), 0);
118 }
119 
120 static int huff_build(VLC *vlc, uint8_t *len)
121 {
122  HuffEntry he[256];
123  uint32_t codes[256];
124  uint8_t bits[256];
125  uint8_t syms[256];
126  uint32_t code;
127  int i;
128 
129  for (i = 0; i < 256; i++) {
130  he[i].sym = 255 - i;
131  he[i].len = len[i];
132  if (len[i] == 0 || len[i] > 32)
133  return AVERROR_INVALIDDATA;
134  }
135  AV_QSORT(he, 256, HuffEntry, huff_cmp_len);
136 
137  code = 1;
138  for (i = 255; i >= 0; i--) {
139  codes[i] = code >> (32 - he[i].len);
140  bits[i] = he[i].len;
141  syms[i] = he[i].sym;
142  code += 0x80000000u >> (he[i].len - 1);
143  }
144 
145  ff_free_vlc(vlc);
146  return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
147  bits, sizeof(*bits), sizeof(*bits),
148  codes, sizeof(*codes), sizeof(*codes),
149  syms, sizeof(*syms), sizeof(*syms), 0);
150 }
151 
152 static void magicyuv_median_pred10(uint16_t *dst, const uint16_t *src1,
153  const uint16_t *diff, intptr_t w,
154  int *left, int *left_top)
155 {
156  int i;
157  uint16_t l, lt;
158 
159  l = *left;
160  lt = *left_top;
161 
162  for (i = 0; i < w; i++) {
163  l = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
164  l &= 0x3FF;
165  lt = src1[i];
166  dst[i] = l;
167  }
168 
169  *left = l;
170  *left_top = lt;
171 }
172 
173 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
174  int j, int threadnr)
175 {
176  MagicYUVContext *s = avctx->priv_data;
177  int interlaced = s->interlaced;
178  AVFrame *p = s->p;
179  int i, k, x;
180  GetBitContext gb;
181  uint16_t *dst;
182 
183  for (i = 0; i < s->planes; i++) {
184  int left, lefttop, top;
185  int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
186  int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
187  int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
188  ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
189  ptrdiff_t stride = p->linesize[i] / 2;
190  int flags, pred;
191  int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
192  s->slices[i][j].size);
193 
194  if (ret < 0)
195  return ret;
196 
197  flags = get_bits(&gb, 8);
198  pred = get_bits(&gb, 8);
199 
200  dst = (uint16_t *)p->data[i] + j * sheight * stride;
201  if (flags & 1) {
202  for (k = 0; k < height; k++) {
203  for (x = 0; x < width; x++)
204  dst[x] = get_bits(&gb, 10);
205 
206  dst += stride;
207  }
208  } else {
209  for (k = 0; k < height; k++) {
210  for (x = 0; x < width; x++) {
211  int pix;
212  if (get_bits_left(&gb) <= 0)
213  return AVERROR_INVALIDDATA;
214 
215  pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
216  if (pix < 0)
217  return AVERROR_INVALIDDATA;
218 
219  dst[x] = 1023 - pix;
220  }
221  dst += stride;
222  }
223  }
224 
225  switch (pred) {
226  case LEFT:
227  dst = (uint16_t *)p->data[i] + j * sheight * stride;
228  s->llviddsp.add_left_pred_int16(dst, dst, 1023, width, 0);
229  dst += stride;
230  if (interlaced) {
231  s->llviddsp.add_left_pred_int16(dst, dst, 1023, width, 0);
232  dst += stride;
233  }
234  for (k = 1 + interlaced; k < height; k++) {
235  s->llviddsp.add_left_pred_int16(dst, dst, 1023, width, dst[-fake_stride]);
236  dst += stride;
237  }
238  break;
239  case GRADIENT:
240  dst = (uint16_t *)p->data[i] + j * sheight * stride;
241  s->llviddsp.add_left_pred_int16(dst, dst, 1023, width, 0);
242  left = lefttop = 0;
243  dst += stride;
244  if (interlaced) {
245  s->llviddsp.add_left_pred_int16(dst, dst, 1023, width, 0);
246  left = lefttop = 0;
247  dst += stride;
248  }
249  for (k = 1 + interlaced; k < height; k++) {
250  top = dst[-fake_stride];
251  left = top + dst[0];
252  dst[0] = left & 0x3FF;
253  for (x = 1; x < width; x++) {
254  top = dst[x - fake_stride];
255  lefttop = dst[x - (fake_stride + 1)];
256  left += top - lefttop + dst[x];
257  dst[x] = left & 0x3FF;
258  }
259  dst += stride;
260  }
261  break;
262  case MEDIAN:
263  dst = (uint16_t *)p->data[i] + j * sheight * stride;
264  lefttop = left = dst[0];
265  s->llviddsp.add_left_pred_int16(dst, dst, 1023, width, 0);
266  dst += stride;
267  if (interlaced) {
268  lefttop = left = dst[0];
269  s->llviddsp.add_left_pred_int16(dst, dst, 1023, width, 0);
270  dst += stride;
271  }
272  for (k = 1 + interlaced; k < height; k++) {
273  magicyuv_median_pred10(dst, dst - fake_stride, dst, width, &left, &lefttop);
274  lefttop = left = dst[0];
275  dst += stride;
276  }
277  break;
278  default:
279  avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
280  }
281  }
282 
283  if (s->decorrelate) {
284  int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
285  int width = avctx->coded_width;
286  uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
287  uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
288  uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
289 
290  for (i = 0; i < height; i++) {
291  for (k = 0; k < width; k++) {
292  b[k] = (b[k] + g[k]) & 0x3FF;
293  r[k] = (r[k] + g[k]) & 0x3FF;
294  }
295  b += p->linesize[0] / 2;
296  g += p->linesize[1] / 2;
297  r += p->linesize[2] / 2;
298  }
299  }
300 
301  return 0;
302 }
303 
304 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
305  int j, int threadnr)
306 {
307  MagicYUVContext *s = avctx->priv_data;
308  int interlaced = s->interlaced;
309  AVFrame *p = s->p;
310  int i, k, x;
311  GetBitContext gb;
312  uint8_t *dst;
313 
314  for (i = 0; i < s->planes; i++) {
315  int left, lefttop, top;
316  int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
317  int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
318  int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
319  ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
320  ptrdiff_t stride = p->linesize[i];
321  int flags, pred;
322  int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
323  s->slices[i][j].size);
324 
325  if (ret < 0)
326  return ret;
327 
328  flags = get_bits(&gb, 8);
329  pred = get_bits(&gb, 8);
330 
331  dst = p->data[i] + j * sheight * stride;
332  if (flags & 1) {
333  for (k = 0; k < height; k++) {
334  for (x = 0; x < width; x++)
335  dst[x] = get_bits(&gb, 8);
336 
337  dst += stride;
338  }
339  } else {
340  for (k = 0; k < height; k++) {
341  for (x = 0; x < width; x++) {
342  int pix;
343  if (get_bits_left(&gb) <= 0)
344  return AVERROR_INVALIDDATA;
345 
346  pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
347  if (pix < 0)
348  return AVERROR_INVALIDDATA;
349 
350  dst[x] = 255 - pix;
351  }
352  dst += stride;
353  }
354  }
355 
356  switch (pred) {
357  case LEFT:
358  dst = p->data[i] + j * sheight * stride;
359  s->llviddsp.add_left_pred(dst, dst, width, 0);
360  dst += stride;
361  if (interlaced) {
362  s->llviddsp.add_left_pred(dst, dst, width, 0);
363  dst += stride;
364  }
365  for (k = 1 + interlaced; k < height; k++) {
366  s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
367  dst += stride;
368  }
369  break;
370  case GRADIENT:
371  dst = p->data[i] + j * sheight * stride;
372  s->llviddsp.add_left_pred(dst, dst, width, 0);
373  left = lefttop = 0;
374  dst += stride;
375  if (interlaced) {
376  s->llviddsp.add_left_pred(dst, dst, width, 0);
377  left = lefttop = 0;
378  dst += stride;
379  }
380  for (k = 1 + interlaced; k < height; k++) {
381  top = dst[-fake_stride];
382  left = top + dst[0];
383  dst[0] = left;
384  for (x = 1; x < width; x++) {
385  top = dst[x - fake_stride];
386  lefttop = dst[x - (fake_stride + 1)];
387  left += top - lefttop + dst[x];
388  dst[x] = left;
389  }
390  dst += stride;
391  }
392  break;
393  case MEDIAN:
394  dst = p->data[i] + j * sheight * stride;
395  lefttop = left = dst[0];
396  s->llviddsp.add_left_pred(dst, dst, width, 0);
397  dst += stride;
398  if (interlaced) {
399  lefttop = left = dst[0];
400  s->llviddsp.add_left_pred(dst, dst, width, 0);
401  dst += stride;
402  }
403  for (k = 1 + interlaced; k < height; k++) {
404  s->llviddsp.add_median_pred(dst, dst - fake_stride,
405  dst, width, &left, &lefttop);
406  lefttop = left = dst[0];
407  dst += stride;
408  }
409  break;
410  default:
411  avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
412  }
413  }
414 
415  if (s->decorrelate) {
416  int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
417  int width = avctx->coded_width;
418  uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
419  uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
420  uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
421 
422  for (i = 0; i < height; i++) {
423  s->llviddsp.add_bytes(b, g, width);
424  s->llviddsp.add_bytes(r, g, width);
425  b += p->linesize[0];
426  g += p->linesize[1];
427  r += p->linesize[2];
428  }
429  }
430 
431  return 0;
432 }
433 
434 static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max)
435 {
436  MagicYUVContext *s = avctx->priv_data;
437  int i = 0, j = 0, k;
438 
439  memset(s->len, 0, sizeof(s->len));
440  while (get_bits_left(gbit) >= 8) {
441  int b = get_bits(gbit, 4);
442  int x = get_bits(gbit, 4);
443  int l = get_bitsz(gbit, b) + 1;
444 
445  for (k = 0; k < l; k++)
446  if (j + k < max)
447  s->len[i][j + k] = x;
448 
449  j += l;
450  if (j == max) {
451  j = 0;
452  if (s->huff_build(&s->vlc[i], s->len[i])) {
453  av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
454  return AVERROR_INVALIDDATA;
455  }
456  i++;
457  if (i == s->planes) {
458  break;
459  }
460  } else if (j > max) {
461  return AVERROR_INVALIDDATA;
462  }
463  }
464 
465  if (i != s->planes) {
466  av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
467  return AVERROR_INVALIDDATA;
468  }
469 
470  return 0;
471 }
472 
473 static int magy_decode_frame(AVCodecContext *avctx, void *data,
474  int *got_frame, AVPacket *avpkt)
475 {
476  MagicYUVContext *s = avctx->priv_data;
477  ThreadFrame frame = { .f = data };
478  AVFrame *p = data;
479  GetByteContext gbyte;
480  GetBitContext gbit;
481  uint32_t first_offset, offset, next_offset, header_size, slice_width;
482  int width, height, format, version, table_size;
483  int ret, i, j;
484 
485  bytestream2_init(&gbyte, avpkt->data, avpkt->size);
486  if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
487  return AVERROR_INVALIDDATA;
488 
489  header_size = bytestream2_get_le32(&gbyte);
490  if (header_size < 32 || header_size >= avpkt->size) {
491  av_log(avctx, AV_LOG_ERROR,
492  "header or packet too small %"PRIu32"\n", header_size);
493  return AVERROR_INVALIDDATA;
494  }
495 
496  version = bytestream2_get_byte(&gbyte);
497  if (version != 7) {
498  avpriv_request_sample(avctx, "Version %d", version);
499  return AVERROR_PATCHWELCOME;
500  }
501 
502  s->hshift[1] =
503  s->vshift[1] =
504  s->hshift[2] =
505  s->vshift[2] = 0;
506  s->decorrelate = 0;
507  s->max = 256;
508  s->huff_build = huff_build;
510 
511  format = bytestream2_get_byte(&gbyte);
512  switch (format) {
513  case 0x65:
514  avctx->pix_fmt = AV_PIX_FMT_GBRP;
515  s->decorrelate = 1;
516  break;
517  case 0x66:
518  avctx->pix_fmt = AV_PIX_FMT_GBRAP;
519  s->decorrelate = 1;
520  break;
521  case 0x67:
522  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
523  break;
524  case 0x68:
525  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
526  s->hshift[1] =
527  s->hshift[2] = 1;
528  break;
529  case 0x69:
530  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
531  s->hshift[1] =
532  s->vshift[1] =
533  s->hshift[2] =
534  s->vshift[2] = 1;
535  break;
536  case 0x6a:
537  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
538  break;
539  case 0x6b:
540  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
541  break;
542  case 0x6c:
543  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
544  s->hshift[1] =
545  s->hshift[2] = 1;
546  s->max = 1024;
549  break;
550  case 0x6d:
551  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
552  s->decorrelate = 1;
553  s->max = 1024;
556  break;
557  case 0x6e:
558  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
559  s->decorrelate = 1;
560  s->max = 1024;
563  break;
564  case 0x73:
565  avctx->pix_fmt = AV_PIX_FMT_GRAY10;
566  s->max = 1024;
569  break;
570  default:
571  avpriv_request_sample(avctx, "Format 0x%X", format);
572  return AVERROR_PATCHWELCOME;
573  }
575 
576  bytestream2_skip(&gbyte, 1);
577  s->color_matrix = bytestream2_get_byte(&gbyte);
578  s->flags = bytestream2_get_byte(&gbyte);
579  s->interlaced = !!(s->flags & 2);
580  bytestream2_skip(&gbyte, 3);
581 
582  width = bytestream2_get_le32(&gbyte);
583  height = bytestream2_get_le32(&gbyte);
584  ret = ff_set_dimensions(avctx, width, height);
585  if (ret < 0)
586  return ret;
587 
588  slice_width = bytestream2_get_le32(&gbyte);
589  if (slice_width != avctx->coded_width) {
590  avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
591  return AVERROR_PATCHWELCOME;
592  }
593  s->slice_height = bytestream2_get_le32(&gbyte);
594  if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
595  av_log(avctx, AV_LOG_ERROR,
596  "invalid slice height: %d\n", s->slice_height);
597  return AVERROR_INVALIDDATA;
598  }
599 
600  bytestream2_skip(&gbyte, 4);
601 
602  s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
603  if (s->nb_slices > INT_MAX / sizeof(Slice)) {
604  av_log(avctx, AV_LOG_ERROR,
605  "invalid number of slices: %d\n", s->nb_slices);
606  return AVERROR_INVALIDDATA;
607  }
608 
609  for (i = 0; i < s->planes; i++) {
610  av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
611  if (!s->slices[i])
612  return AVERROR(ENOMEM);
613 
614  offset = bytestream2_get_le32(&gbyte);
615  if (offset >= avpkt->size - header_size)
616  return AVERROR_INVALIDDATA;
617 
618  if (i == 0)
619  first_offset = offset;
620 
621  for (j = 0; j < s->nb_slices - 1; j++) {
622  s->slices[i][j].start = offset + header_size;
623 
624  next_offset = bytestream2_get_le32(&gbyte);
625  if (next_offset <= offset || next_offset >= avpkt->size - header_size)
626  return AVERROR_INVALIDDATA;
627 
628  s->slices[i][j].size = next_offset - offset;
629  offset = next_offset;
630  }
631 
632  s->slices[i][j].start = offset + header_size;
633  s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
634  }
635 
636  if (bytestream2_get_byte(&gbyte) != s->planes)
637  return AVERROR_INVALIDDATA;
638 
639  bytestream2_skip(&gbyte, s->nb_slices * s->planes);
640 
641  table_size = header_size + first_offset - bytestream2_tell(&gbyte);
642  if (table_size < 2)
643  return AVERROR_INVALIDDATA;
644 
645  ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
646  if (ret < 0)
647  return ret;
648 
649  ret = build_huffman(avctx, &gbit, s->max);
650  if (ret < 0)
651  return ret;
652 
654  p->key_frame = 1;
655 
656  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
657  return ret;
658 
659  s->buf = avpkt->data;
660  s->p = p;
661  avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
662 
663  if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
664  avctx->pix_fmt == AV_PIX_FMT_GBRAP ||
665  avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
666  avctx->pix_fmt == AV_PIX_FMT_GBRAP10) {
667  FFSWAP(uint8_t*, p->data[0], p->data[1]);
668  FFSWAP(int, p->linesize[0], p->linesize[1]);
669  } else {
670  switch (s->color_matrix) {
671  case 1:
673  break;
674  case 2:
676  break;
677  }
679  }
680 
681  *got_frame = 1;
682 
683  return avpkt->size;
684 }
685 
686 #if HAVE_THREADS
687 static int magy_init_thread_copy(AVCodecContext *avctx)
688 {
689  MagicYUVContext *s = avctx->priv_data;
690  int i;
691 
692  for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
693  s->slices[i] = NULL;
694  s->slices_size[i] = 0;
695  }
696 
697  return 0;
698 }
699 #endif
700 
702 {
703  MagicYUVContext *s = avctx->priv_data;
705  return 0;
706 }
707 
709 {
710  MagicYUVContext * const s = avctx->priv_data;
711  int i;
712 
713  for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
714  av_freep(&s->slices[i]);
715  s->slices_size[i] = 0;
716  ff_free_vlc(&s->vlc[i]);
717  }
718 
719  return 0;
720 }
721 
723  .name = "magicyuv",
724  .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
725  .type = AVMEDIA_TYPE_VIDEO,
726  .id = AV_CODEC_ID_MAGICYUV,
727  .priv_data_size = sizeof(MagicYUVContext),
729  .init_thread_copy = ONLY_IF_THREADS_ENABLED(magy_init_thread_copy),
730  .close = magy_decode_end,
731  .decode = magy_decode_frame,
732  .capabilities = AV_CODEC_CAP_DR1 |
735  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
736 };
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:453
#define NULL
Definition: coverity.c:32
static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max)
Definition: magicyuv.c:434
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:65
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
int hshift[4]
Definition: magicyuv.c:64
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
uint8_t len[4][1024]
Definition: magicyuv.c:68
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1934
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:371
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:2373
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:210
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:457
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:1658
const char * b
Definition: vf_curves.c:113
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:367
static int huff_build10(VLC *vlc, uint8_t *len)
Definition: magicyuv.c:88
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1960
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:67
AVCodec.
Definition: avcodec.h:3681
static void magicyuv_median_pred10(uint16_t *dst, const uint16_t *src1, const uint16_t *diff, intptr_t w, int *left, int *left_top)
Definition: magicyuv.c:152
LLVidDSPContext llviddsp
Definition: magicyuv.c:73
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:333
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:71
static AVFrame * frame
Prediction
Definition: magicyuv.c:41
#define height
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
bitstream reader API header.
uint32_t code
Definition: magicyuv.c:50
#define av_log(a,...)
static int magy_decode_slice(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition: magicyuv.c:304
static int huff_cmp_len(const void *a, const void *b)
Definition: magicyuv.c:76
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:56
#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:423
enum AVColorSpace colorspace
YUV colorspace type.
Definition: frame.h:434
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
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:1057
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:66
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
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:261
#define FFMIN(a, b)
Definition: common.h:96
uint8_t interlaced
Definition: mxfenc.c:1822
#define width
static int huff_cmp_len10(const void *a, const void *b)
Definition: magicyuv.c:82
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:476
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:1061
static av_cold int magy_decode_init(AVCodecContext *avctx)
Definition: magicyuv.c:701
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:218
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:1732
static int magy_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: magicyuv.c:473
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:1934
static const char * format
Definition: movenc.c:47
AVCodec ff_magicyuv_decoder
Definition: magicyuv.c:722
Definition: magicyuv.c:42
#define mid_pred
Definition: mathops.h:97
#define u(width,...)
VLC vlc[4]
Definition: magicyuv.c:69
static int magy_decode_slice10(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition: magicyuv.c:173
uint8_t * buf
Definition: magicyuv.c:63
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:352
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:475
static av_cold int magy_decode_end(AVCodecContext *avctx)
Definition: magicyuv.c:708
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
int
int(* huff_build)(VLC *vlc, uint8_t *len)
Definition: magicyuv.c:70
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:282
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:229
uint16_t sym
Definition: magicyuv.c:48
void * priv_data
Definition: avcodec.h:1774
static int huff_build(VLC *vlc, uint8_t *len)
Definition: magicyuv.c:120
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:3221
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:256
#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:1634
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:994
#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:60