FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
huffyuvdec.c
Go to the documentation of this file.
1 /*
2  * huffyuv decoder
3  *
4  * Copyright (c) 2002-2014 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
7  * the algorithm used
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  *
25  * yuva, gray, 4:4:4, 4:1:1, 4:1:0 and >8 bit per sample support sponsored by NOA
26  */
27 
28 /**
29  * @file
30  * huffyuv decoder
31  */
32 
33 #define UNCHECKED_BITSTREAM_READER 1
34 
35 #include "avcodec.h"
36 #include "get_bits.h"
37 #include "huffyuv.h"
38 #include "huffyuvdsp.h"
39 #include "thread.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/pixdesc.h"
42 
43 #define classic_shift_luma_table_size 42
45  34, 36, 35, 69, 135, 232, 9, 16, 10, 24, 11, 23, 12, 16, 13, 10,
46  14, 8, 15, 8, 16, 8, 17, 20, 16, 10, 207, 206, 205, 236, 11, 8,
47  10, 21, 9, 23, 8, 8, 199, 70, 69, 68, 0,
48  0,0,0,0,0,0,0,0,
49 };
50 
51 #define classic_shift_chroma_table_size 59
53  66, 36, 37, 38, 39, 40, 41, 75, 76, 77, 110, 239, 144, 81, 82, 83,
54  84, 85, 118, 183, 56, 57, 88, 89, 56, 89, 154, 57, 58, 57, 26, 141,
55  57, 56, 58, 57, 58, 57, 184, 119, 214, 245, 116, 83, 82, 49, 80, 79,
56  78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, 0,
57  0,0,0,0,0,0,0,0,
58 };
59 
60 static const unsigned char classic_add_luma[256] = {
61  3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
62  73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
63  68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
64  35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
65  37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
66  35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
67  27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
68  15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
69  12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
70  12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
71  18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
72  28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
73  28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
74  62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
75  54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
76  46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
77 };
78 
79 static const unsigned char classic_add_chroma[256] = {
80  3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
81  7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
82  11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
83  43, 45, 76, 81, 46, 82, 75, 55, 56, 144, 58, 80, 60, 74, 147, 63,
84  143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
85  80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
86  17, 14, 5, 6, 100, 54, 47, 50, 51, 53, 106, 107, 108, 109, 110, 111,
87  112, 113, 114, 115, 4, 117, 118, 92, 94, 121, 122, 3, 124, 103, 2, 1,
88  0, 129, 130, 131, 120, 119, 126, 125, 136, 137, 138, 139, 140, 141, 142, 134,
89  135, 132, 133, 104, 64, 101, 62, 57, 102, 95, 93, 59, 61, 28, 97, 96,
90  52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
91  19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
92  7, 128, 127, 105, 123, 116, 35, 34, 33, 145, 31, 79, 42, 146, 78, 26,
93  83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
94  14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
95  6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
96 };
97 
98 static int read_len_table(uint8_t *dst, GetBitContext *gb, int n)
99 {
100  int i, val, repeat;
101 
102  for (i = 0; i < n;) {
103  repeat = get_bits(gb, 3);
104  val = get_bits(gb, 5);
105  if (repeat == 0)
106  repeat = get_bits(gb, 8);
107  if (i + repeat > n || get_bits_left(gb) < 0) {
108  av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n");
109  return AVERROR_INVALIDDATA;
110  }
111  while (repeat--)
112  dst[i++] = val;
113  }
114  return 0;
115 }
116 
118 {
119  int ret;
120  uint16_t *symbols = av_mallocz(5 << VLC_BITS);
121  uint16_t *bits;
122  uint8_t *len;
123  if (!symbols)
124  return AVERROR(ENOMEM);
125  bits = symbols + (1 << VLC_BITS);
126  len = (uint8_t *)(bits + (1 << VLC_BITS));
127 
128  if (s->bitstream_bpp < 24 || s->version > 2) {
129  int p, i, y, u;
130  for (p = 0; p < 4; p++) {
131  int p0 = s->version > 2 ? p : 0;
132  for (i = y = 0; y < s->vlc_n; y++) {
133  int len0 = s->len[p0][y];
134  int limit = VLC_BITS - len0;
135  if (limit <= 0 || !len0)
136  continue;
137  if ((sign_extend(y, 8) & (s->vlc_n-1)) != y)
138  continue;
139  for (u = 0; u < s->vlc_n; u++) {
140  int len1 = s->len[p][u];
141  if (len1 > limit || !len1)
142  continue;
143  if ((sign_extend(u, 8) & (s->vlc_n-1)) != u)
144  continue;
145  av_assert0(i < (1 << VLC_BITS));
146  len[i] = len0 + len1;
147  bits[i] = (s->bits[p0][y] << len1) + s->bits[p][u];
148  symbols[i] = (y << 8) + (u & 0xFF);
149  i++;
150  }
151  }
152  ff_free_vlc(&s->vlc[4 + p]);
153  if ((ret = ff_init_vlc_sparse(&s->vlc[4 + p], VLC_BITS, i, len, 1, 1,
154  bits, 2, 2, symbols, 2, 2, 0)) < 0)
155  goto out;
156  }
157  } else {
158  uint8_t (*map)[4] = (uint8_t(*)[4]) s->pix_bgr_map;
159  int i, b, g, r, code;
160  int p0 = s->decorrelate;
161  int p1 = !s->decorrelate;
162  /* Restrict the range to +/-16 because that's pretty much guaranteed
163  * to cover all the combinations that fit in 11 bits total, and it
164  * does not matter if we miss a few rare codes. */
165  for (i = 0, g = -16; g < 16; g++) {
166  int len0 = s->len[p0][g & 255];
167  int limit0 = VLC_BITS - len0;
168  if (limit0 < 2 || !len0)
169  continue;
170  for (b = -16; b < 16; b++) {
171  int len1 = s->len[p1][b & 255];
172  int limit1 = limit0 - len1;
173  if (limit1 < 1 || !len1)
174  continue;
175  code = (s->bits[p0][g & 255] << len1) + s->bits[p1][b & 255];
176  for (r = -16; r < 16; r++) {
177  int len2 = s->len[2][r & 255];
178  if (len2 > limit1 || !len2)
179  continue;
180  av_assert0(i < (1 << VLC_BITS));
181  len[i] = len0 + len1 + len2;
182  bits[i] = (code << len2) + s->bits[2][r & 255];
183  if (s->decorrelate) {
184  map[i][G] = g;
185  map[i][B] = g + b;
186  map[i][R] = g + r;
187  } else {
188  map[i][B] = g;
189  map[i][G] = b;
190  map[i][R] = r;
191  }
192  i++;
193  }
194  }
195  }
196  ff_free_vlc(&s->vlc[4]);
197  if ((ret = init_vlc(&s->vlc[4], VLC_BITS, i, len, 1, 1,
198  bits, 2, 2, 0)) < 0)
199  goto out;
200  }
201  ret = 0;
202 out:
203  av_freep(&symbols);
204  return ret;
205 }
206 
208 {
209  GetBitContext gb;
210  int i, ret;
211  int count = 3;
212 
213  if ((ret = init_get_bits(&gb, src, length * 8)) < 0)
214  return ret;
215 
216  if (s->version > 2)
217  count = 1 + s->alpha + 2*s->chroma;
218 
219  for (i = 0; i < count; i++) {
220  if ((ret = read_len_table(s->len[i], &gb, s->vlc_n)) < 0)
221  return ret;
222  if ((ret = ff_huffyuv_generate_bits_table(s->bits[i], s->len[i], s->vlc_n)) < 0)
223  return ret;
224  ff_free_vlc(&s->vlc[i]);
225  if ((ret = init_vlc(&s->vlc[i], VLC_BITS, s->vlc_n, s->len[i], 1, 1,
226  s->bits[i], 4, 4, 0)) < 0)
227  return ret;
228  }
229 
230  if ((ret = generate_joint_tables(s)) < 0)
231  return ret;
232 
233  return (get_bits_count(&gb) + 7) / 8;
234 }
235 
237 {
238  GetBitContext gb;
239  int i, ret;
240 
243  if ((ret = read_len_table(s->len[0], &gb, 256)) < 0)
244  return ret;
245 
248  if ((ret = read_len_table(s->len[1], &gb, 256)) < 0)
249  return ret;
250 
251  for (i = 0; i < 256; i++)
252  s->bits[0][i] = classic_add_luma[i];
253  for (i = 0; i < 256; i++)
254  s->bits[1][i] = classic_add_chroma[i];
255 
256  if (s->bitstream_bpp >= 24) {
257  memcpy(s->bits[1], s->bits[0], 256 * sizeof(uint32_t));
258  memcpy(s->len[1], s->len[0], 256 * sizeof(uint8_t));
259  }
260  memcpy(s->bits[2], s->bits[1], 256 * sizeof(uint32_t));
261  memcpy(s->len[2], s->len[1], 256 * sizeof(uint8_t));
262 
263  for (i = 0; i < 4; i++) {
264  ff_free_vlc(&s->vlc[i]);
265  if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1,
266  s->bits[i], 4, 4, 0)) < 0)
267  return ret;
268  }
269 
270  if ((ret = generate_joint_tables(s)) < 0)
271  return ret;
272 
273  return 0;
274 }
275 
277 {
278  HYuvContext *s = avctx->priv_data;
279  int i;
280 
283 
284  for (i = 0; i < 8; i++)
285  ff_free_vlc(&s->vlc[i]);
286 
287  return 0;
288 }
289 
291 {
292  HYuvContext *s = avctx->priv_data;
293  int ret;
294 
295  ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
296  if (ret < 0)
297  return ret;
298 
300  memset(s->vlc, 0, 4 * sizeof(VLC));
301 
302  s->interlaced = avctx->height > 288;
303  s->bgr32 = 1;
304 
305  if (avctx->extradata_size) {
306  if ((avctx->bits_per_coded_sample & 7) &&
307  avctx->bits_per_coded_sample != 12)
308  s->version = 1; // do such files exist at all?
309  else if (avctx->extradata_size > 3 && avctx->extradata[3] == 0)
310  s->version = 2;
311  else
312  s->version = 3;
313  } else
314  s->version = 0;
315 
316  s->bps = 8;
317  s->n = 1<<s->bps;
318  s->vlc_n = FFMIN(s->n, MAX_VLC_N);
319  s->chroma = 1;
320  if (s->version >= 2) {
321  int method, interlace;
322 
323  if (avctx->extradata_size < 4)
324  return AVERROR_INVALIDDATA;
325 
326  method = avctx->extradata[0];
327  s->decorrelate = method & 64 ? 1 : 0;
328  s->predictor = method & 63;
329  if (s->version == 2) {
330  s->bitstream_bpp = avctx->extradata[1];
331  if (s->bitstream_bpp == 0)
332  s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
333  } else {
334  s->bps = (avctx->extradata[1] >> 4) + 1;
335  s->n = 1<<s->bps;
336  s->vlc_n = FFMIN(s->n, MAX_VLC_N);
337  s->chroma_h_shift = avctx->extradata[1] & 3;
338  s->chroma_v_shift = (avctx->extradata[1] >> 2) & 3;
339  s->yuv = !!(avctx->extradata[2] & 1);
340  s->chroma= !!(avctx->extradata[2] & 3);
341  s->alpha = !!(avctx->extradata[2] & 4);
342  }
343  interlace = (avctx->extradata[2] & 0x30) >> 4;
344  s->interlaced = (interlace == 1) ? 1 : (interlace == 2) ? 0 : s->interlaced;
345  s->context = avctx->extradata[2] & 0x40 ? 1 : 0;
346 
347  if ((ret = read_huffman_tables(s, avctx->extradata + 4,
348  avctx->extradata_size - 4)) < 0)
349  goto error;
350  } else {
351  switch (avctx->bits_per_coded_sample & 7) {
352  case 1:
353  s->predictor = LEFT;
354  s->decorrelate = 0;
355  break;
356  case 2:
357  s->predictor = LEFT;
358  s->decorrelate = 1;
359  break;
360  case 3:
361  s->predictor = PLANE;
362  s->decorrelate = avctx->bits_per_coded_sample >= 24;
363  break;
364  case 4:
365  s->predictor = MEDIAN;
366  s->decorrelate = 0;
367  break;
368  default:
369  s->predictor = LEFT; // OLD
370  s->decorrelate = 0;
371  break;
372  }
373  s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
374  s->context = 0;
375 
376  if ((ret = read_old_huffman_tables(s)) < 0)
377  goto error;
378  }
379 
380  if (s->version <= 2) {
381  switch (s->bitstream_bpp) {
382  case 12:
383  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
384  s->yuv = 1;
385  break;
386  case 16:
387  if (s->yuy2)
388  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
389  else
390  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
391  s->yuv = 1;
392  break;
393  case 24:
394  if (s->bgr32)
395  avctx->pix_fmt = AV_PIX_FMT_0RGB32;
396  else
397  avctx->pix_fmt = AV_PIX_FMT_BGR24;
398  break;
399  case 32:
400  av_assert0(s->bgr32);
401  avctx->pix_fmt = AV_PIX_FMT_RGB32;
402  s->alpha = 1;
403  break;
404  default:
405  ret = AVERROR_INVALIDDATA;
406  goto error;
407  }
409  &s->chroma_h_shift,
410  &s->chroma_v_shift);
411  } else {
412  switch ( (s->chroma<<10) | (s->yuv<<9) | (s->alpha<<8) | ((s->bps-1)<<4) | s->chroma_h_shift | (s->chroma_v_shift<<2)) {
413  case 0x070:
414  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
415  break;
416  case 0x0F0:
417  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
418  break;
419  case 0x170:
420  avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
421  break;
422  case 0x470:
423  avctx->pix_fmt = AV_PIX_FMT_GBRP;
424  break;
425  case 0x480:
426  avctx->pix_fmt = AV_PIX_FMT_GBRP9;
427  break;
428  case 0x490:
429  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
430  break;
431  case 0x4B0:
432  avctx->pix_fmt = AV_PIX_FMT_GBRP12;
433  break;
434  case 0x4D0:
435  avctx->pix_fmt = AV_PIX_FMT_GBRP14;
436  break;
437  case 0x4F0:
438  avctx->pix_fmt = AV_PIX_FMT_GBRP16;
439  break;
440  case 0x570:
441  avctx->pix_fmt = AV_PIX_FMT_GBRAP;
442  break;
443  case 0x670:
444  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
445  break;
446  case 0x680:
447  avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
448  break;
449  case 0x690:
450  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
451  break;
452  case 0x6B0:
453  avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
454  break;
455  case 0x6D0:
456  avctx->pix_fmt = AV_PIX_FMT_YUV444P14;
457  break;
458  case 0x6F0:
459  avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
460  break;
461  case 0x671:
462  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
463  break;
464  case 0x681:
465  avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
466  break;
467  case 0x691:
468  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
469  break;
470  case 0x6B1:
471  avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
472  break;
473  case 0x6D1:
474  avctx->pix_fmt = AV_PIX_FMT_YUV422P14;
475  break;
476  case 0x6F1:
477  avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
478  break;
479  case 0x672:
480  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
481  break;
482  case 0x674:
483  avctx->pix_fmt = AV_PIX_FMT_YUV440P;
484  break;
485  case 0x675:
486  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
487  break;
488  case 0x685:
489  avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
490  break;
491  case 0x695:
492  avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
493  break;
494  case 0x6B5:
495  avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
496  break;
497  case 0x6D5:
498  avctx->pix_fmt = AV_PIX_FMT_YUV420P14;
499  break;
500  case 0x6F5:
501  avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
502  break;
503  case 0x67A:
504  avctx->pix_fmt = AV_PIX_FMT_YUV410P;
505  break;
506  case 0x770:
507  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
508  break;
509  case 0x780:
510  avctx->pix_fmt = AV_PIX_FMT_YUVA444P9;
511  break;
512  case 0x790:
514  break;
515  case 0x7F0:
517  break;
518  case 0x771:
519  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
520  break;
521  case 0x781:
522  avctx->pix_fmt = AV_PIX_FMT_YUVA422P9;
523  break;
524  case 0x791:
526  break;
527  case 0x7F1:
529  break;
530  case 0x775:
531  avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
532  break;
533  case 0x785:
534  avctx->pix_fmt = AV_PIX_FMT_YUVA420P9;
535  break;
536  case 0x795:
538  break;
539  case 0x7F5:
541  break;
542  default:
543  ret = AVERROR_INVALIDDATA;
544  goto error;
545  }
546  }
547 
548  ff_huffyuv_common_init(avctx);
549 
550  if ((avctx->pix_fmt == AV_PIX_FMT_YUV422P || avctx->pix_fmt == AV_PIX_FMT_YUV420P) && avctx->width & 1) {
551  av_log(avctx, AV_LOG_ERROR, "width must be even for this colorspace\n");
552  ret = AVERROR_INVALIDDATA;
553  goto error;
554  }
555  if (s->predictor == MEDIAN && avctx->pix_fmt == AV_PIX_FMT_YUV422P &&
556  avctx->width % 4) {
557  av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 4 "
558  "for this combination of colorspace and predictor type.\n");
559  ret = AVERROR_INVALIDDATA;
560  goto error;
561  }
562 
563  if ((ret = ff_huffyuv_alloc_temp(s)) < 0) {
565  goto error;
566  }
567 
568  return 0;
569  error:
570  decode_end(avctx);
571  return ret;
572 }
573 
574 #if HAVE_THREADS
575 static av_cold int decode_init_thread_copy(AVCodecContext *avctx)
576 {
577  HYuvContext *s = avctx->priv_data;
578  int i, ret;
579 
580  if ((ret = ff_huffyuv_alloc_temp(s)) < 0) {
582  return ret;
583  }
584 
585  for (i = 0; i < 8; i++)
586  s->vlc[i].table = NULL;
587 
588  if (s->version >= 2) {
589  if ((ret = read_huffman_tables(s, avctx->extradata + 4,
590  avctx->extradata_size)) < 0)
591  return ret;
592  } else {
593  if ((ret = read_old_huffman_tables(s)) < 0)
594  return ret;
595  }
596 
597  return 0;
598 }
599 #endif
600 
601 /** Subset of GET_VLC for use in hand-roller VLC code */
602 #define VLC_INTERN(dst, table, gb, name, bits, max_depth) \
603  code = table[index][0]; \
604  n = table[index][1]; \
605  if (max_depth > 1 && n < 0) { \
606  LAST_SKIP_BITS(name, gb, bits); \
607  UPDATE_CACHE(name, gb); \
608  \
609  nb_bits = -n; \
610  index = SHOW_UBITS(name, gb, nb_bits) + code; \
611  code = table[index][0]; \
612  n = table[index][1]; \
613  if (max_depth > 2 && n < 0) { \
614  LAST_SKIP_BITS(name, gb, nb_bits); \
615  UPDATE_CACHE(name, gb); \
616  \
617  nb_bits = -n; \
618  index = SHOW_UBITS(name, gb, nb_bits) + code; \
619  code = table[index][0]; \
620  n = table[index][1]; \
621  } \
622  } \
623  dst = code; \
624  LAST_SKIP_BITS(name, gb, n)
625 
626 
627 #define GET_VLC_DUAL(dst0, dst1, name, gb, dtable, table1, table2, \
628  bits, max_depth, OP) \
629  do { \
630  unsigned int index = SHOW_UBITS(name, gb, bits); \
631  int code, n = dtable[index][1]; \
632  \
633  if (n<=0) { \
634  int nb_bits; \
635  VLC_INTERN(dst0, table1, gb, name, bits, max_depth); \
636  \
637  UPDATE_CACHE(re, gb); \
638  index = SHOW_UBITS(name, gb, bits); \
639  VLC_INTERN(dst1, table2, gb, name, bits, max_depth); \
640  } else { \
641  code = dtable[index][0]; \
642  OP(dst0, dst1, code); \
643  LAST_SKIP_BITS(name, gb, n); \
644  } \
645  } while (0)
646 
647 #define OP8bits(dst0, dst1, code) dst0 = code>>8; dst1 = code
648 
649 #define READ_2PIX(dst0, dst1, plane1) \
650  UPDATE_CACHE(re, &s->gb); \
651  GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane1].table, \
652  s->vlc[0].table, s->vlc[plane1].table, VLC_BITS, 3, OP8bits)
653 
655 {
656  int i, icount;
657  OPEN_READER(re, &s->gb);
658  count /= 2;
659 
660  icount = get_bits_left(&s->gb) / (32 * 4);
661  if (count >= icount) {
662  for (i = 0; i < icount; i++) {
663  READ_2PIX(s->temp[0][2 * i], s->temp[1][i], 1);
664  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
665  }
666  for (; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
667  READ_2PIX(s->temp[0][2 * i ], s->temp[1][i], 1);
668  if (BITS_LEFT(re, &s->gb) <= 0) break;
669  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
670  }
671  for (; i < count; i++)
672  s->temp[0][2 * i ] = s->temp[1][i] =
673  s->temp[0][2 * i + 1] = s->temp[2][i] = 0;
674  } else {
675  for (i = 0; i < count; i++) {
676  READ_2PIX(s->temp[0][2 * i], s->temp[1][i], 1);
677  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
678  }
679  }
680  CLOSE_READER(re, &s->gb);
681 }
682 
683 #define READ_2PIX_PLANE(dst0, dst1, plane, OP) \
684  UPDATE_CACHE(re, &s->gb); \
685  GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane].table, \
686  s->vlc[plane].table, s->vlc[plane].table, VLC_BITS, 3, OP)
687 
688 #define OP14bits(dst0, dst1, code) dst0 = code>>8; dst1 = sign_extend(code, 8)
689 
690 /* TODO instead of restarting the read when the code isn't in the first level
691  * of the joint table, jump into the 2nd level of the individual table. */
692 #define READ_2PIX_PLANE16(dst0, dst1, plane){\
693  dst0 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
694  dst0 += get_bits(&s->gb, 2);\
695  dst1 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
696  dst1 += get_bits(&s->gb, 2);\
697 }
699 {
700  int i, count = width/2;
701 
702  if (s->bps <= 8) {
703  OPEN_READER(re, &s->gb);
704  if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
705  for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
706  READ_2PIX_PLANE(s->temp[0][2 * i], s->temp[0][2 * i + 1], plane, OP8bits);
707  }
708  } else {
709  for(i=0; i<count; i++){
710  READ_2PIX_PLANE(s->temp[0][2 * i], s->temp[0][2 * i + 1], plane, OP8bits);
711  }
712  }
713  if( width&1 && BITS_LEFT(re, &s->gb)>0 ) {
714  unsigned int index;
715  int nb_bits, code, n;
716  UPDATE_CACHE(re, &s->gb);
717  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
718  VLC_INTERN(s->temp[0][width-1], s->vlc[plane].table,
719  &s->gb, re, VLC_BITS, 3);
720  }
721  CLOSE_READER(re, &s->gb);
722  } else if (s->bps <= 14) {
723  OPEN_READER(re, &s->gb);
724  if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
725  for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
726  READ_2PIX_PLANE(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane, OP14bits);
727  }
728  } else {
729  for(i=0; i<count; i++){
730  READ_2PIX_PLANE(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane, OP14bits);
731  }
732  }
733  if( width&1 && BITS_LEFT(re, &s->gb)>0 ) {
734  unsigned int index;
735  int nb_bits, code, n;
736  UPDATE_CACHE(re, &s->gb);
737  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
738  VLC_INTERN(s->temp16[0][width-1], s->vlc[plane].table,
739  &s->gb, re, VLC_BITS, 3);
740  }
741  CLOSE_READER(re, &s->gb);
742  } else {
743  if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
744  for (i = 0; i < count && get_bits_left(&s->gb) > 0; i++) {
745  READ_2PIX_PLANE16(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane);
746  }
747  } else {
748  for(i=0; i<count; i++){
749  READ_2PIX_PLANE16(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane);
750  }
751  }
752  if( width&1 && get_bits_left(&s->gb)>0 ) {
753  int dst = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;
754  s->temp16[0][width-1] = dst + get_bits(&s->gb, 2);
755  }
756  }
757 }
758 
760 {
761  int i;
762  OPEN_READER(re, &s->gb);
763  count /= 2;
764 
765  if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
766  for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
767  READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
768  }
769  } else {
770  for (i = 0; i < count; i++) {
771  READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
772  }
773  }
774  CLOSE_READER(re, &s->gb);
775 }
776 
778  int decorrelate, int alpha)
779 {
780  int i;
781  OPEN_READER(re, &s->gb);
782 
783  for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
784  unsigned int index;
785  int code, n, nb_bits;
786 
787  UPDATE_CACHE(re, &s->gb);
788  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
789  n = s->vlc[4].table[index][1];
790 
791  if (n>0) {
792  code = s->vlc[4].table[index][0];
793  *(uint32_t *) &s->temp[0][4 * i] = s->pix_bgr_map[code];
794  LAST_SKIP_BITS(re, &s->gb, n);
795  } else {
796  if (decorrelate) {
797  VLC_INTERN(s->temp[0][4 * i + G], s->vlc[1].table,
798  &s->gb, re, VLC_BITS, 3);
799 
800  UPDATE_CACHE(re, &s->gb);
801  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
802  VLC_INTERN(code, s->vlc[0].table, &s->gb, re, VLC_BITS, 3);
803  s->temp[0][4 * i + B] = code + s->temp[0][4 * i + G];
804 
805  UPDATE_CACHE(re, &s->gb);
806  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
807  VLC_INTERN(code, s->vlc[2].table, &s->gb, re, VLC_BITS, 3);
808  s->temp[0][4 * i + R] = code + s->temp[0][4 * i + G];
809  } else {
810  VLC_INTERN(s->temp[0][4 * i + B], s->vlc[0].table,
811  &s->gb, re, VLC_BITS, 3);
812 
813  UPDATE_CACHE(re, &s->gb);
814  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
815  VLC_INTERN(s->temp[0][4 * i + G], s->vlc[1].table,
816  &s->gb, re, VLC_BITS, 3);
817 
818  UPDATE_CACHE(re, &s->gb);
819  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
820  VLC_INTERN(s->temp[0][4 * i + R], s->vlc[2].table,
821  &s->gb, re, VLC_BITS, 3);
822  }
823  }
824  if (alpha) {
825  UPDATE_CACHE(re, &s->gb);
826  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
827  VLC_INTERN(s->temp[0][4 * i + A], s->vlc[2].table,
828  &s->gb, re, VLC_BITS, 3);
829  } else
830  s->temp[0][4 * i + A] = 0;
831  }
832  CLOSE_READER(re, &s->gb);
833 }
834 
836 {
837  if (s->decorrelate) {
838  if (s->bitstream_bpp == 24)
839  decode_bgr_1(s, count, 1, 0);
840  else
841  decode_bgr_1(s, count, 1, 1);
842  } else {
843  if (s->bitstream_bpp == 24)
844  decode_bgr_1(s, count, 0, 0);
845  else
846  decode_bgr_1(s, count, 0, 1);
847  }
848 }
849 
850 static void draw_slice(HYuvContext *s, AVFrame *frame, int y)
851 {
852  int h, cy, i;
854 
855  if (!s->avctx->draw_horiz_band)
856  return;
857 
858  h = y - s->last_slice_end;
859  y -= h;
860 
861  if (s->bitstream_bpp == 12)
862  cy = y >> 1;
863  else
864  cy = y;
865 
866  offset[0] = frame->linesize[0] * y;
867  offset[1] = frame->linesize[1] * cy;
868  offset[2] = frame->linesize[2] * cy;
869  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
870  offset[i] = 0;
871  emms_c();
872 
873  s->avctx->draw_horiz_band(s->avctx, frame, offset, y, 3, h);
874 
875  s->last_slice_end = y + h;
876 }
877 
878 static int left_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int acc)
879 {
880  if (s->bps <= 8) {
881  return s->hdsp.add_hfyu_left_pred(dst, src, w, acc);
882  } else {
883  return s->llviddsp.add_hfyu_left_pred_int16(( uint16_t *)dst, (const uint16_t *)src, s->n-1, w, acc);
884  }
885 }
886 
887 static void add_bytes(HYuvContext *s, uint8_t *dst, uint8_t *src, int w)
888 {
889  if (s->bps <= 8) {
890  s->hdsp.add_bytes(dst, src, w);
891  } else {
892  s->llviddsp.add_int16((uint16_t*)dst, (const uint16_t*)src, s->n - 1, w);
893  }
894 }
895 
896 static void add_median_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, const uint8_t *diff, int w, int *left, int *left_top)
897 {
898  if (s->bps <= 8) {
899  s->hdsp.add_hfyu_median_pred(dst, src, diff, w, left, left_top);
900  } else {
901  s->llviddsp.add_hfyu_median_pred_int16((uint16_t *)dst, (const uint16_t *)src, (const uint16_t *)diff, s->n-1, w, left, left_top);
902  }
903 }
904 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
905  AVPacket *avpkt)
906 {
907  const uint8_t *buf = avpkt->data;
908  int buf_size = avpkt->size;
909  HYuvContext *s = avctx->priv_data;
910  const int width = s->width;
911  const int width2 = s->width >> 1;
912  const int height = s->height;
913  int fake_ystride, fake_ustride, fake_vstride;
914  ThreadFrame frame = { .f = data };
915  AVFrame *const p = data;
916  int table_size = 0, ret;
917 
920  buf_size);
921  if (!s->bitstream_buffer)
922  return AVERROR(ENOMEM);
923 
924  s->bdsp.bswap_buf((uint32_t *) s->bitstream_buffer,
925  (const uint32_t *) buf, buf_size / 4);
926 
927  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
928  return ret;
929 
930  if (s->context) {
931  table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
932  if (table_size < 0)
933  return table_size;
934  }
935 
936  if ((unsigned) (buf_size - table_size) >= INT_MAX / 8)
937  return AVERROR_INVALIDDATA;
938 
939  if ((ret = init_get_bits(&s->gb, s->bitstream_buffer + table_size,
940  (buf_size - table_size) * 8)) < 0)
941  return ret;
942 
943  fake_ystride = s->interlaced ? p->linesize[0] * 2 : p->linesize[0];
944  fake_ustride = s->interlaced ? p->linesize[1] * 2 : p->linesize[1];
945  fake_vstride = s->interlaced ? p->linesize[2] * 2 : p->linesize[2];
946 
947  s->last_slice_end = 0;
948 
949  if (s->version > 2) {
950  int plane;
951  for(plane = 0; plane < 1 + 2*s->chroma + s->alpha; plane++) {
952  int left, lefttop, y;
953  int w = width;
954  int h = height;
955  int fake_stride = fake_ystride;
956 
957  if (s->chroma && (plane == 1 || plane == 2)) {
958  w >>= s->chroma_h_shift;
959  h >>= s->chroma_v_shift;
960  fake_stride = plane == 1 ? fake_ustride : fake_vstride;
961  }
962 
963  switch (s->predictor) {
964  case LEFT:
965  case PLANE:
966  decode_plane_bitstream(s, w, plane);
967  left = left_prediction(s, p->data[plane], s->temp[0], w, 0);
968 
969  for (y = 1; y < h; y++) {
970  uint8_t *dst = p->data[plane] + p->linesize[plane]*y;
971 
972  decode_plane_bitstream(s, w, plane);
973  left = left_prediction(s, dst, s->temp[0], w, left);
974  if (s->predictor == PLANE) {
975  if (y > s->interlaced) {
976  add_bytes(s, dst, dst - fake_stride, w);
977  }
978  }
979  }
980 
981  break;
982  case MEDIAN:
983  decode_plane_bitstream(s, w, plane);
984  left= left_prediction(s, p->data[plane], s->temp[0], w, 0);
985 
986  y = 1;
987 
988  /* second line is left predicted for interlaced case */
989  if (s->interlaced) {
990  decode_plane_bitstream(s, w, plane);
991  left = left_prediction(s, p->data[plane] + p->linesize[plane], s->temp[0], w, left);
992  y++;
993  }
994 
995  lefttop = p->data[plane][0];
996  decode_plane_bitstream(s, w, plane);
997  add_median_prediction(s, p->data[plane] + fake_stride, p->data[plane], s->temp[0], w, &left, &lefttop);
998  y++;
999 
1000  for (; y<h; y++) {
1001  uint8_t *dst;
1002 
1003  decode_plane_bitstream(s, w, plane);
1004 
1005  dst = p->data[plane] + p->linesize[plane] * y;
1006 
1007  add_median_prediction(s, dst, dst - fake_stride, s->temp[0], w, &left, &lefttop);
1008  }
1009 
1010  break;
1011  }
1012  }
1013  draw_slice(s, p, height);
1014  } else if (s->bitstream_bpp < 24) {
1015  int y, cy;
1016  int lefty, leftu, leftv;
1017  int lefttopy, lefttopu, lefttopv;
1018 
1019  if (s->yuy2) {
1020  p->data[0][3] = get_bits(&s->gb, 8);
1021  p->data[0][2] = get_bits(&s->gb, 8);
1022  p->data[0][1] = get_bits(&s->gb, 8);
1023  p->data[0][0] = get_bits(&s->gb, 8);
1024 
1025  av_log(avctx, AV_LOG_ERROR,
1026  "YUY2 output is not implemented yet\n");
1027  return AVERROR_PATCHWELCOME;
1028  } else {
1029  leftv =
1030  p->data[2][0] = get_bits(&s->gb, 8);
1031  lefty =
1032  p->data[0][1] = get_bits(&s->gb, 8);
1033  leftu =
1034  p->data[1][0] = get_bits(&s->gb, 8);
1035  p->data[0][0] = get_bits(&s->gb, 8);
1036 
1037  switch (s->predictor) {
1038  case LEFT:
1039  case PLANE:
1040  decode_422_bitstream(s, width - 2);
1041  lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + 2, s->temp[0],
1042  width - 2, lefty);
1043  if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1044  leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
1045  leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
1046  }
1047 
1048  for (cy = y = 1; y < s->height; y++, cy++) {
1049  uint8_t *ydst, *udst, *vdst;
1050 
1051  if (s->bitstream_bpp == 12) {
1052  decode_gray_bitstream(s, width);
1053 
1054  ydst = p->data[0] + p->linesize[0] * y;
1055 
1056  lefty = s->hdsp.add_hfyu_left_pred(ydst, s->temp[0],
1057  width, lefty);
1058  if (s->predictor == PLANE) {
1059  if (y > s->interlaced)
1060  s->hdsp.add_bytes(ydst, ydst - fake_ystride, width);
1061  }
1062  y++;
1063  if (y >= s->height)
1064  break;
1065  }
1066 
1067  draw_slice(s, p, y);
1068 
1069  ydst = p->data[0] + p->linesize[0] * y;
1070  udst = p->data[1] + p->linesize[1] * cy;
1071  vdst = p->data[2] + p->linesize[2] * cy;
1072 
1073  decode_422_bitstream(s, width);
1074  lefty = s->hdsp.add_hfyu_left_pred(ydst, s->temp[0],
1075  width, lefty);
1076  if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1077  leftu = s->hdsp.add_hfyu_left_pred(udst, s->temp[1], width2, leftu);
1078  leftv = s->hdsp.add_hfyu_left_pred(vdst, s->temp[2], width2, leftv);
1079  }
1080  if (s->predictor == PLANE) {
1081  if (cy > s->interlaced) {
1082  s->hdsp.add_bytes(ydst, ydst - fake_ystride, width);
1083  if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1084  s->hdsp.add_bytes(udst, udst - fake_ustride, width2);
1085  s->hdsp.add_bytes(vdst, vdst - fake_vstride, width2);
1086  }
1087  }
1088  }
1089  }
1090  draw_slice(s, p, height);
1091 
1092  break;
1093  case MEDIAN:
1094  /* first line except first 2 pixels is left predicted */
1095  decode_422_bitstream(s, width - 2);
1096  lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + 2, s->temp[0],
1097  width - 2, lefty);
1098  if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1099  leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
1100  leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
1101  }
1102 
1103  cy = y = 1;
1104 
1105  /* second line is left predicted for interlaced case */
1106  if (s->interlaced) {
1107  decode_422_bitstream(s, width);
1108  lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + p->linesize[0],
1109  s->temp[0], width, lefty);
1110  if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1111  leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
1112  leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
1113  }
1114  y++;
1115  cy++;
1116  }
1117 
1118  /* next 4 pixels are left predicted too */
1119  decode_422_bitstream(s, 4);
1120  lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + fake_ystride,
1121  s->temp[0], 4, lefty);
1122  if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1123  leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
1124  leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
1125  }
1126 
1127  /* next line except the first 4 pixels is median predicted */
1128  lefttopy = p->data[0][3];
1129  decode_422_bitstream(s, width - 4);
1130  s->hdsp.add_hfyu_median_pred(p->data[0] + fake_ystride + 4,
1131  p->data[0] + 4, s->temp[0],
1132  width - 4, &lefty, &lefttopy);
1133  if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1134  lefttopu = p->data[1][1];
1135  lefttopv = p->data[2][1];
1136  s->hdsp.add_hfyu_median_pred(p->data[1] + fake_ustride + 2, p->data[1] + 2, s->temp[1], width2 - 2, &leftu, &lefttopu);
1137  s->hdsp.add_hfyu_median_pred(p->data[2] + fake_vstride + 2, p->data[2] + 2, s->temp[2], width2 - 2, &leftv, &lefttopv);
1138  }
1139  y++;
1140  cy++;
1141 
1142  for (; y < height; y++, cy++) {
1143  uint8_t *ydst, *udst, *vdst;
1144 
1145  if (s->bitstream_bpp == 12) {
1146  while (2 * cy > y) {
1147  decode_gray_bitstream(s, width);
1148  ydst = p->data[0] + p->linesize[0] * y;
1149  s->hdsp.add_hfyu_median_pred(ydst, ydst - fake_ystride,
1150  s->temp[0], width,
1151  &lefty, &lefttopy);
1152  y++;
1153  }
1154  if (y >= height)
1155  break;
1156  }
1157  draw_slice(s, p, y);
1158 
1159  decode_422_bitstream(s, width);
1160 
1161  ydst = p->data[0] + p->linesize[0] * y;
1162  udst = p->data[1] + p->linesize[1] * cy;
1163  vdst = p->data[2] + p->linesize[2] * cy;
1164 
1165  s->hdsp.add_hfyu_median_pred(ydst, ydst - fake_ystride,
1166  s->temp[0], width,
1167  &lefty, &lefttopy);
1168  if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1169  s->hdsp.add_hfyu_median_pred(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
1170  s->hdsp.add_hfyu_median_pred(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
1171  }
1172  }
1173 
1174  draw_slice(s, p, height);
1175  break;
1176  }
1177  }
1178  } else {
1179  int y;
1180  uint8_t left[4];
1181  const int last_line = (height - 1) * p->linesize[0];
1182 
1183  if (s->bitstream_bpp == 32) {
1184  left[A] = p->data[0][last_line + A] = get_bits(&s->gb, 8);
1185  left[R] = p->data[0][last_line + R] = get_bits(&s->gb, 8);
1186  left[G] = p->data[0][last_line + G] = get_bits(&s->gb, 8);
1187  left[B] = p->data[0][last_line + B] = get_bits(&s->gb, 8);
1188  } else {
1189  left[R] = p->data[0][last_line + R] = get_bits(&s->gb, 8);
1190  left[G] = p->data[0][last_line + G] = get_bits(&s->gb, 8);
1191  left[B] = p->data[0][last_line + B] = get_bits(&s->gb, 8);
1192  left[A] = p->data[0][last_line + A] = 255;
1193  skip_bits(&s->gb, 8);
1194  }
1195 
1196  if (s->bgr32) {
1197  switch (s->predictor) {
1198  case LEFT:
1199  case PLANE:
1200  decode_bgr_bitstream(s, width - 1);
1201  s->hdsp.add_hfyu_left_pred_bgr32(p->data[0] + last_line + 4,
1202  s->temp[0], width - 1, left);
1203 
1204  for (y = s->height - 2; y >= 0; y--) { // Yes it is stored upside down.
1205  decode_bgr_bitstream(s, width);
1206 
1207  s->hdsp.add_hfyu_left_pred_bgr32(p->data[0] + p->linesize[0] * y,
1208  s->temp[0], width, left);
1209  if (s->predictor == PLANE) {
1210  if (s->bitstream_bpp != 32)
1211  left[A] = 0;
1212  if (y < s->height - 1 - s->interlaced) {
1213  s->hdsp.add_bytes(p->data[0] + p->linesize[0] * y,
1214  p->data[0] + p->linesize[0] * y +
1215  fake_ystride, 4 * width);
1216  }
1217  }
1218  }
1219  // just 1 large slice as this is not possible in reverse order
1220  draw_slice(s, p, height);
1221  break;
1222  default:
1223  av_log(avctx, AV_LOG_ERROR,
1224  "prediction type not supported!\n");
1225  }
1226  } else {
1227  av_log(avctx, AV_LOG_ERROR,
1228  "BGR24 output is not implemented yet\n");
1229  return AVERROR_PATCHWELCOME;
1230  }
1231  }
1232  emms_c();
1233 
1234  *got_frame = 1;
1235 
1236  return (get_bits_count(&s->gb) + 31) / 32 * 4 + table_size;
1237 }
1238 
1240  .name = "huffyuv",
1241  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
1242  .type = AVMEDIA_TYPE_VIDEO,
1243  .id = AV_CODEC_ID_HUFFYUV,
1244  .priv_data_size = sizeof(HYuvContext),
1245  .init = decode_init,
1246  .close = decode_end,
1247  .decode = decode_frame,
1250  .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1251 };
1252 
1253 #if CONFIG_FFVHUFF_DECODER
1254 AVCodec ff_ffvhuff_decoder = {
1255  .name = "ffvhuff",
1256  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
1257  .type = AVMEDIA_TYPE_VIDEO,
1258  .id = AV_CODEC_ID_FFVHUFF,
1259  .priv_data_size = sizeof(HYuvContext),
1260  .init = decode_init,
1261  .close = decode_end,
1262  .decode = decode_frame,
1265  .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1266 };
1267 #endif /* CONFIG_FFVHUFF_DECODER */
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
int plane
Definition: avisynth_c.h:291
static void add_median_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, const uint8_t *diff, int w, int *left, int *left_top)
Definition: huffyuvdec.c:896
#define OP14bits(dst0, dst1, code)
Definition: huffyuvdec.c:688
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:378
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_NUM_DATA_POINTERS
Definition: frame.h:185
static void decode_plane_bitstream(HYuvContext *s, int width, int plane)
Definition: huffyuvdec.c:698
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:372
#define READ_2PIX_PLANE(dst0, dst1, plane, OP)
Definition: huffyuvdec.c:683
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:374
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:351
float re
Definition: fft.c:73
int yuy2
Definition: huffyuv.h:64
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:375
int bitstream_bpp
Definition: huffyuv.h:62
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:390
const char * g
Definition: vf_curves.c:108
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int acc
Definition: yuv2rgb.c:546
static void decode_422_bitstream(HYuvContext *s, int count)
Definition: huffyuvdec.c:654
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:275
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
int size
Definition: avcodec.h:1581
const char * b
Definition: vf_curves.c:109
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:357
static av_always_inline void decode_bgr_1(HYuvContext *s, int count, int decorrelate, int alpha)
Definition: huffyuvdec.c:777
#define VLC_BITS
Definition: asvdec.c:37
av_cold void ff_huffyuvdsp_init(HuffYUVDSPContext *c)
Definition: huffyuvdsp.c:108
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1877
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:120
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:345
#define MAX_VLC_N
Definition: huffyuv.h:46
int context
Definition: huffyuv.h:76
#define READ_2PIX_PLANE16(dst0, dst1, plane)
Definition: huffyuvdec.c:692
unsigned int bitstream_buffer_size
Definition: huffyuv.h:87
static int generate_joint_tables(HYuvContext *s)
Definition: huffyuvdec.c:117
AVCodec.
Definition: avcodec.h:3542
int height
Definition: huffyuv.h:74
#define READ_2PIX(dst0, dst1, plane1)
Definition: huffyuvdec.c:649
static int read_old_huffman_tables(HYuvContext *s)
Definition: huffyuvdec.c:236
static void draw_slice(HYuvContext *s, AVFrame *frame, int y)
Definition: huffyuvdec.c:850
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint32_t pix_bgr_map[1<< VLC_BITS]
Definition: huffyuv.h:84
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t bits
Definition: crc.c:296
uint8_t
#define av_cold
Definition: attributes.h:82
static void decode_bgr_bitstream(HYuvContext *s, int count)
Definition: huffyuvdec.c:835
int bps
Definition: huffyuv.h:66
Multithreading support functions.
Definition: vf_geq.c:46
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1764
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:371
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:356
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1580
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
bitstream reader API header.
int vlc_n
Definition: huffyuv.h:68
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:354
int chroma_h_shift
Definition: huffyuv.h:72
void(* add_hfyu_median_pred)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, intptr_t w, int *left, int *left_top)
Definition: huffyuvdsp.h:40
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3042
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:346
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:377
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:858
int(* add_hfyu_left_pred_int16)(uint16_t *dst, const uint16_t *src, unsigned mask, int w, unsigned left)
#define A(x)
Definition: vp56_arith.h:28
uint16_t * temp16[3]
identical to temp but 16bit type
Definition: huffyuv.h:80
#define av_log(a,...)
uint8_t len[4][MAX_VLC_N]
Definition: huffyuv.h:82
av_cold int ff_huffyuv_alloc_temp(HYuvContext *s)
Definition: huffyuv.c:58
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:568
void(* add_bytes)(uint8_t *dst, uint8_t *src, intptr_t w)
Definition: huffyuvdsp.h:38
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:187
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:99
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int chroma_v_shift
Definition: huffyuv.h:73
Definition: huffyuv.h:50
av_cold void ff_huffyuv_common_end(HYuvContext *s)
Definition: huffyuv.c:87
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: vlc.h:38
static const unsigned char classic_add_luma[256]
Definition: huffyuvdec.c:60
uint8_t * bitstream_buffer
Definition: huffyuv.h:86
#define AVERROR(e)
Definition: error.h:43
int flags
Definition: huffyuv.h:75
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2250
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * r
Definition: vf_curves.c:107
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:379
int bgr32
Definition: huffyuv.h:65
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3549
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:344
int chroma
Definition: huffyuv.h:70
VLC vlc[8]
Definition: huffyuv.h:85
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
#define CLOSE_READER(name, gb)
Definition: get_bits.h:131
int(* add_hfyu_left_pred)(uint8_t *dst, const uint8_t *src, intptr_t w, int left)
Definition: huffyuvdsp.h:43
huffyuv codec for libavcodec.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1019
#define classic_shift_chroma_table_size
Definition: huffyuvdec.c:51
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:215
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:339
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:251
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition: avcodec.h:950
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, 'draw_horiz_band' is called by the libavcodec decoder to draw a horizontal band...
Definition: avcodec.h:1910
int decorrelate
Definition: huffyuv.h:61
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:360
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:157
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:325
#define FFMIN(a, b)
Definition: common.h:96
int width
Definition: huffyuv.h:74
int last_slice_end
Definition: huffyuv.h:78
#define VLC_INTERN(dst, table, gb, name, bits, max_depth)
Subset of GET_VLC for use in hand-roller VLC code.
Definition: huffyuvdec.c:602
#define width
int width
picture width / height.
Definition: avcodec.h:1836
static const unsigned char classic_shift_chroma[classic_shift_chroma_table_size+AV_INPUT_BUFFER_PADDING_SIZE]
Definition: huffyuvdec.c:52
int ff_huffyuv_generate_bits_table(uint32_t *dst, const uint8_t *len_table, int n)
Definition: huffyuv.c:39
void(* add_int16)(uint16_t *dst, const uint16_t *src, unsigned mask, int w)
Definition: vf_geq.c:46
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:181
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:535
int n
Definition: avisynth_c.h:547
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:376
uint8_t * temp[3]
Definition: huffyuv.h:79
#define src
Definition: vp9dsp.c:530
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:340
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:359
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:193
int alpha
Definition: huffyuv.h:69
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:352
static void add_bytes(HYuvContext *s, uint8_t *dst, uint8_t *src, int w)
Definition: huffyuvdec.c:887
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:349
Definition: vf_geq.c:46
AVCodec ff_huffyuv_decoder
Definition: huffyuvdec.c:1239
Libavcodec external API header.
int yuv
Definition: huffyuv.h:71
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
LLVidDSPContext llviddsp
Definition: huffyuv.h:91
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:188
HuffYUVDSPContext hdsp
Definition: huffyuv.h:89
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:1649
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:318
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:63
#define OPEN_READER(name, gb)
Definition: get_bits.h:120
void * buf
Definition: avisynth_c.h:553
int interlaced
Definition: huffyuv.h:60
int extradata_size
Definition: avcodec.h:1765
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:341
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:68
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
int index
Definition: gxfenc.c:89
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:406
static void decode_gray_bitstream(HYuvContext *s, int count)
Definition: huffyuvdec.c:759
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:338
#define classic_shift_luma_table_size
Definition: huffyuvdec.c:43
const VDPAUPixFmtMap * map
#define u(width,...)
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:139
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:350
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:358
int version
Definition: huffyuv.h:63
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:342
void(* add_hfyu_median_pred_int16)(uint16_t *dst, const uint16_t *top, const uint16_t *diff, unsigned mask, int w, int *left, int *left_top)
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:348
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
Predictor predictor
Definition: huffyuv.h:57
static av_cold int decode_init(AVCodecContext *avctx)
Definition: huffyuvdec.c:290
AVCodecContext * avctx
Definition: huffyuv.h:56
GetBitContext gb
Definition: huffyuv.h:58
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:722
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
Definition: huffyuv.h:51
if(ret< 0)
Definition: vf_mcdeint.c:282
#define BITS_LEFT(name, gb)
Definition: get_bits.h:173
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:228
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:373
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: huffyuvdec.c:904
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
av_cold void ff_huffyuv_common_init(AVCodecContext *avctx)
Definition: huffyuv.c:71
static av_cold int decode_end(AVCodecContext *avctx)
Definition: huffyuvdec.c:276
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:731
void * priv_data
Definition: avcodec.h:1691
static av_always_inline int diff(const uint32_t a, const uint32_t b)
void(* add_hfyu_left_pred_bgr32)(uint8_t *dst, const uint8_t *src, intptr_t w, uint8_t *left)
Definition: huffyuvdsp.h:45
int len
static const unsigned char classic_add_chroma[256]
Definition: huffyuvdec.c:79
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
static int left_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int acc)
Definition: huffyuvdec.c:878
static const unsigned char classic_shift_luma[classic_shift_luma_table_size+AV_INPUT_BUFFER_PADDING_SIZE]
Definition: huffyuvdec.c:44
FILE * out
Definition: movenc.c:54
#define av_freep(p)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
#define av_always_inline
Definition: attributes.h:39
uint32_t bits[4][MAX_VLC_N]
Definition: huffyuv.h:83
#define LEFT
Definition: cdgraphics.c:168
This structure stores compressed data.
Definition: avcodec.h:1557
static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median)
Definition: snowenc.c:1326
BswapDSPContext bdsp
Definition: huffyuv.h:88
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:360
static int read_len_table(uint8_t *dst, GetBitContext *gb, int n)
Definition: huffyuvdec.c:98
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
#define OP8bits(dst0, dst1, code)
Definition: huffyuvdec.c:647
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:353
static int read_huffman_tables(HYuvContext *s, const uint8_t *src, int length)
Definition: huffyuvdec.c:207
for(j=16;j >0;--j)
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:322