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/pixdesc.h"
41 
42 #define classic_shift_luma_table_size 42
44  34, 36, 35, 69, 135, 232, 9, 16, 10, 24, 11, 23, 12, 16, 13, 10,
45  14, 8, 15, 8, 16, 8, 17, 20, 16, 10, 207, 206, 205, 236, 11, 8,
46  10, 21, 9, 23, 8, 8, 199, 70, 69, 68, 0,
47  0,0,0,0,0,0,0,0,
48 };
49 
50 #define classic_shift_chroma_table_size 59
52  66, 36, 37, 38, 39, 40, 41, 75, 76, 77, 110, 239, 144, 81, 82, 83,
53  84, 85, 118, 183, 56, 57, 88, 89, 56, 89, 154, 57, 58, 57, 26, 141,
54  57, 56, 58, 57, 58, 57, 184, 119, 214, 245, 116, 83, 82, 49, 80, 79,
55  78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, 0,
56  0,0,0,0,0,0,0,0,
57 };
58 
59 static const unsigned char classic_add_luma[256] = {
60  3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
61  73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
62  68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
63  35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
64  37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
65  35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
66  27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
67  15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
68  12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
69  12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
70  18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
71  28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
72  28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
73  62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
74  54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
75  46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
76 };
77 
78 static const unsigned char classic_add_chroma[256] = {
79  3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
80  7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
81  11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
82  43, 45, 76, 81, 46, 82, 75, 55, 56, 144, 58, 80, 60, 74, 147, 63,
83  143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
84  80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
85  17, 14, 5, 6, 100, 54, 47, 50, 51, 53, 106, 107, 108, 109, 110, 111,
86  112, 113, 114, 115, 4, 117, 118, 92, 94, 121, 122, 3, 124, 103, 2, 1,
87  0, 129, 130, 131, 120, 119, 126, 125, 136, 137, 138, 139, 140, 141, 142, 134,
88  135, 132, 133, 104, 64, 101, 62, 57, 102, 95, 93, 59, 61, 28, 97, 96,
89  52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
90  19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
91  7, 128, 127, 105, 123, 116, 35, 34, 33, 145, 31, 79, 42, 146, 78, 26,
92  83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
93  14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
94  6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
95 };
96 
97 static int read_len_table(uint8_t *dst, GetBitContext *gb, int n)
98 {
99  int i, val, repeat;
100 
101  for (i = 0; i < n;) {
102  repeat = get_bits(gb, 3);
103  val = get_bits(gb, 5);
104  if (repeat == 0)
105  repeat = get_bits(gb, 8);
106  if (i + repeat > n || get_bits_left(gb) < 0) {
107  av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n");
108  return AVERROR_INVALIDDATA;
109  }
110  while (repeat--)
111  dst[i++] = val;
112  }
113  return 0;
114 }
115 
117 {
118  uint16_t symbols[1 << VLC_BITS];
119  uint16_t bits[1 << VLC_BITS];
120  uint8_t len[1 << VLC_BITS];
121  int ret;
122 
123  if (s->bitstream_bpp < 24 || s->version > 2) {
124  int p, i, y, u;
125  for (p = 0; p < 4; p++) {
126  int p0 = s->version > 2 ? p : 0;
127  for (i = y = 0; y < s->vlc_n; y++) {
128  int len0 = s->len[p0][y];
129  int limit = VLC_BITS - len0;
130  if (limit <= 0 || !len0)
131  continue;
132  if ((sign_extend(y, 8) & (s->vlc_n-1)) != y)
133  continue;
134  for (u = 0; u < s->vlc_n; u++) {
135  int len1 = s->len[p][u];
136  if (len1 > limit || !len1)
137  continue;
138  if ((sign_extend(u, 8) & (s->vlc_n-1)) != u)
139  continue;
140  av_assert0(i < (1 << VLC_BITS));
141  len[i] = len0 + len1;
142  bits[i] = (s->bits[p0][y] << len1) + s->bits[p][u];
143  symbols[i] = (y << 8) + (u & 0xFF);
144  i++;
145  }
146  }
147  ff_free_vlc(&s->vlc[4 + p]);
148  if ((ret = ff_init_vlc_sparse(&s->vlc[4 + p], VLC_BITS, i, len, 1, 1,
149  bits, 2, 2, symbols, 2, 2, 0)) < 0)
150  return ret;
151  }
152  } else {
153  uint8_t (*map)[4] = (uint8_t(*)[4]) s->pix_bgr_map;
154  int i, b, g, r, code;
155  int p0 = s->decorrelate;
156  int p1 = !s->decorrelate;
157  /* Restrict the range to +/-16 because that's pretty much guaranteed
158  * to cover all the combinations that fit in 11 bits total, and it
159  * does not matter if we miss a few rare codes. */
160  for (i = 0, g = -16; g < 16; g++) {
161  int len0 = s->len[p0][g & 255];
162  int limit0 = VLC_BITS - len0;
163  if (limit0 < 2 || !len0)
164  continue;
165  for (b = -16; b < 16; b++) {
166  int len1 = s->len[p1][b & 255];
167  int limit1 = limit0 - len1;
168  if (limit1 < 1 || !len1)
169  continue;
170  code = (s->bits[p0][g & 255] << len1) + s->bits[p1][b & 255];
171  for (r = -16; r < 16; r++) {
172  int len2 = s->len[2][r & 255];
173  if (len2 > limit1 || !len2)
174  continue;
175  av_assert0(i < (1 << VLC_BITS));
176  len[i] = len0 + len1 + len2;
177  bits[i] = (code << len2) + s->bits[2][r & 255];
178  if (s->decorrelate) {
179  map[i][G] = g;
180  map[i][B] = g + b;
181  map[i][R] = g + r;
182  } else {
183  map[i][B] = g;
184  map[i][G] = b;
185  map[i][R] = r;
186  }
187  i++;
188  }
189  }
190  }
191  ff_free_vlc(&s->vlc[4]);
192  if ((ret = init_vlc(&s->vlc[4], VLC_BITS, i, len, 1, 1,
193  bits, 2, 2, 0)) < 0)
194  return ret;
195  }
196  return 0;
197 }
198 
200 {
201  GetBitContext gb;
202  int i, ret;
203  int count = 3;
204 
205  if ((ret = init_get_bits(&gb, src, length * 8)) < 0)
206  return ret;
207 
208  if (s->version > 2)
209  count = 1 + s->alpha + 2*s->chroma;
210 
211  for (i = 0; i < count; i++) {
212  if ((ret = read_len_table(s->len[i], &gb, s->vlc_n)) < 0)
213  return ret;
214  if ((ret = ff_huffyuv_generate_bits_table(s->bits[i], s->len[i], s->vlc_n)) < 0)
215  return ret;
216  ff_free_vlc(&s->vlc[i]);
217  if ((ret = init_vlc(&s->vlc[i], VLC_BITS, s->vlc_n, s->len[i], 1, 1,
218  s->bits[i], 4, 4, 0)) < 0)
219  return ret;
220  }
221 
222  if ((ret = generate_joint_tables(s)) < 0)
223  return ret;
224 
225  return (get_bits_count(&gb) + 7) / 8;
226 }
227 
229 {
230  GetBitContext gb;
231  int i, ret;
232 
235  if ((ret = read_len_table(s->len[0], &gb, 256)) < 0)
236  return ret;
237 
240  if ((ret = read_len_table(s->len[1], &gb, 256)) < 0)
241  return ret;
242 
243  for (i = 0; i < 256; i++)
244  s->bits[0][i] = classic_add_luma[i];
245  for (i = 0; i < 256; i++)
246  s->bits[1][i] = classic_add_chroma[i];
247 
248  if (s->bitstream_bpp >= 24) {
249  memcpy(s->bits[1], s->bits[0], 256 * sizeof(uint32_t));
250  memcpy(s->len[1], s->len[0], 256 * sizeof(uint8_t));
251  }
252  memcpy(s->bits[2], s->bits[1], 256 * sizeof(uint32_t));
253  memcpy(s->len[2], s->len[1], 256 * sizeof(uint8_t));
254 
255  for (i = 0; i < 4; i++) {
256  ff_free_vlc(&s->vlc[i]);
257  if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1,
258  s->bits[i], 4, 4, 0)) < 0)
259  return ret;
260  }
261 
262  if ((ret = generate_joint_tables(s)) < 0)
263  return ret;
264 
265  return 0;
266 }
267 
269 {
270  HYuvContext *s = avctx->priv_data;
271  int ret;
272 
274  memset(s->vlc, 0, 4 * sizeof(VLC));
275 
276  s->interlaced = avctx->height > 288;
277  s->bgr32 = 1;
278 
279  if (avctx->extradata_size) {
280  if ((avctx->bits_per_coded_sample & 7) &&
281  avctx->bits_per_coded_sample != 12)
282  s->version = 1; // do such files exist at all?
283  else if (avctx->extradata_size > 3 && avctx->extradata[3] == 0)
284  s->version = 2;
285  else
286  s->version = 3;
287  } else
288  s->version = 0;
289 
290  s->bps = 8;
291  s->n = 1<<s->bps;
292  s->vlc_n = FFMIN(s->n, MAX_VLC_N);
293  s->chroma = 1;
294  if (s->version >= 2) {
295  int method, interlace;
296 
297  if (avctx->extradata_size < 4)
298  return AVERROR_INVALIDDATA;
299 
300  method = avctx->extradata[0];
301  s->decorrelate = method & 64 ? 1 : 0;
302  s->predictor = method & 63;
303  if (s->version == 2) {
304  s->bitstream_bpp = avctx->extradata[1];
305  if (s->bitstream_bpp == 0)
306  s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
307  } else {
308  s->bps = (avctx->extradata[1] >> 4) + 1;
309  s->n = 1<<s->bps;
310  s->vlc_n = FFMIN(s->n, MAX_VLC_N);
311  s->chroma_h_shift = avctx->extradata[1] & 3;
312  s->chroma_v_shift = (avctx->extradata[1] >> 2) & 3;
313  s->yuv = !!(avctx->extradata[2] & 1);
314  s->chroma= !!(avctx->extradata[2] & 3);
315  s->alpha = !!(avctx->extradata[2] & 4);
316  }
317  interlace = (avctx->extradata[2] & 0x30) >> 4;
318  s->interlaced = (interlace == 1) ? 1 : (interlace == 2) ? 0 : s->interlaced;
319  s->context = avctx->extradata[2] & 0x40 ? 1 : 0;
320 
321  if ((ret = read_huffman_tables(s, avctx->extradata + 4,
322  avctx->extradata_size - 4)) < 0)
323  return ret;
324  } else {
325  switch (avctx->bits_per_coded_sample & 7) {
326  case 1:
327  s->predictor = LEFT;
328  s->decorrelate = 0;
329  break;
330  case 2:
331  s->predictor = LEFT;
332  s->decorrelate = 1;
333  break;
334  case 3:
335  s->predictor = PLANE;
336  s->decorrelate = avctx->bits_per_coded_sample >= 24;
337  break;
338  case 4:
339  s->predictor = MEDIAN;
340  s->decorrelate = 0;
341  break;
342  default:
343  s->predictor = LEFT; // OLD
344  s->decorrelate = 0;
345  break;
346  }
347  s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
348  s->context = 0;
349 
350  if ((ret = read_old_huffman_tables(s)) < 0)
351  return ret;
352  }
353 
354  if (s->version <= 2) {
355  switch (s->bitstream_bpp) {
356  case 12:
357  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
358  s->yuv = 1;
359  break;
360  case 16:
361  if (s->yuy2)
362  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
363  else
364  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
365  s->yuv = 1;
366  break;
367  case 24:
368  if (s->bgr32)
369  avctx->pix_fmt = AV_PIX_FMT_0RGB32;
370  else
371  avctx->pix_fmt = AV_PIX_FMT_BGR24;
372  break;
373  case 32:
374  av_assert0(s->bgr32);
375  avctx->pix_fmt = AV_PIX_FMT_RGB32;
376  s->alpha = 1;
377  break;
378  default:
379  return AVERROR_INVALIDDATA;
380  }
382  &s->chroma_h_shift,
383  &s->chroma_v_shift);
384  } else {
385  switch ( (s->chroma<<10) | (s->yuv<<9) | (s->alpha<<8) | ((s->bps-1)<<4) | s->chroma_h_shift | (s->chroma_v_shift<<2)) {
386  case 0x070:
387  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
388  break;
389  case 0x0F0:
390  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
391  break;
392  case 0x170:
393  avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
394  break;
395  case 0x470:
396  avctx->pix_fmt = AV_PIX_FMT_GBRP;
397  break;
398  case 0x480:
399  avctx->pix_fmt = AV_PIX_FMT_GBRP9;
400  break;
401  case 0x490:
402  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
403  break;
404  case 0x4B0:
405  avctx->pix_fmt = AV_PIX_FMT_GBRP12;
406  break;
407  case 0x4D0:
408  avctx->pix_fmt = AV_PIX_FMT_GBRP14;
409  break;
410  case 0x4F0:
411  avctx->pix_fmt = AV_PIX_FMT_GBRP16;
412  break;
413  case 0x570:
414  avctx->pix_fmt = AV_PIX_FMT_GBRAP;
415  break;
416  case 0x670:
417  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
418  break;
419  case 0x680:
420  avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
421  break;
422  case 0x690:
423  avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
424  break;
425  case 0x6B0:
426  avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
427  break;
428  case 0x6D0:
429  avctx->pix_fmt = AV_PIX_FMT_YUV444P14;
430  break;
431  case 0x6F0:
432  avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
433  break;
434  case 0x671:
435  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
436  break;
437  case 0x681:
438  avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
439  break;
440  case 0x691:
441  avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
442  break;
443  case 0x6B1:
444  avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
445  break;
446  case 0x6D1:
447  avctx->pix_fmt = AV_PIX_FMT_YUV422P14;
448  break;
449  case 0x6F1:
450  avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
451  break;
452  case 0x672:
453  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
454  break;
455  case 0x674:
456  avctx->pix_fmt = AV_PIX_FMT_YUV440P;
457  break;
458  case 0x675:
459  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
460  break;
461  case 0x685:
462  avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
463  break;
464  case 0x695:
465  avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
466  break;
467  case 0x6B5:
468  avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
469  break;
470  case 0x6D5:
471  avctx->pix_fmt = AV_PIX_FMT_YUV420P14;
472  break;
473  case 0x6F5:
474  avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
475  break;
476  case 0x67A:
477  avctx->pix_fmt = AV_PIX_FMT_YUV410P;
478  break;
479  case 0x770:
480  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
481  break;
482  case 0x780:
483  avctx->pix_fmt = AV_PIX_FMT_YUVA444P9;
484  break;
485  case 0x790:
487  break;
488  case 0x7F0:
490  break;
491  case 0x771:
492  avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
493  break;
494  case 0x781:
495  avctx->pix_fmt = AV_PIX_FMT_YUVA422P9;
496  break;
497  case 0x791:
499  break;
500  case 0x7F1:
502  break;
503  case 0x775:
504  avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
505  break;
506  case 0x785:
507  avctx->pix_fmt = AV_PIX_FMT_YUVA420P9;
508  break;
509  case 0x795:
511  break;
512  case 0x7F5:
514  break;
515  default:
516  return AVERROR_INVALIDDATA;
517  }
518  }
519 
520  ff_huffyuv_common_init(avctx);
521 
522  if ((avctx->pix_fmt == AV_PIX_FMT_YUV422P || avctx->pix_fmt == AV_PIX_FMT_YUV420P) && avctx->width & 1) {
523  av_log(avctx, AV_LOG_ERROR, "width must be even for this colorspace\n");
524  return AVERROR_INVALIDDATA;
525  }
526  if (s->predictor == MEDIAN && avctx->pix_fmt == AV_PIX_FMT_YUV422P &&
527  avctx->width % 4) {
528  av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 4 "
529  "for this combination of colorspace and predictor type.\n");
530  return AVERROR_INVALIDDATA;
531  }
532 
533  if ((ret = ff_huffyuv_alloc_temp(s)) < 0) {
535  return ret;
536  }
537 
538  return 0;
539 }
540 
542 {
543  HYuvContext *s = avctx->priv_data;
544  int i, ret;
545 
546  if ((ret = ff_huffyuv_alloc_temp(s)) < 0) {
548  return ret;
549  }
550 
551  for (i = 0; i < 8; i++)
552  s->vlc[i].table = NULL;
553 
554  if (s->version >= 2) {
555  if ((ret = read_huffman_tables(s, avctx->extradata + 4,
556  avctx->extradata_size)) < 0)
557  return ret;
558  } else {
559  if ((ret = read_old_huffman_tables(s)) < 0)
560  return ret;
561  }
562 
563  return 0;
564 }
565 
566 /** Subset of GET_VLC for use in hand-roller VLC code */
567 #define VLC_INTERN(dst, table, gb, name, bits, max_depth) \
568  code = table[index][0]; \
569  n = table[index][1]; \
570  if (max_depth > 1 && n < 0) { \
571  LAST_SKIP_BITS(name, gb, bits); \
572  UPDATE_CACHE(name, gb); \
573  \
574  nb_bits = -n; \
575  index = SHOW_UBITS(name, gb, nb_bits) + code; \
576  code = table[index][0]; \
577  n = table[index][1]; \
578  if (max_depth > 2 && n < 0) { \
579  LAST_SKIP_BITS(name, gb, nb_bits); \
580  UPDATE_CACHE(name, gb); \
581  \
582  nb_bits = -n; \
583  index = SHOW_UBITS(name, gb, nb_bits) + code; \
584  code = table[index][0]; \
585  n = table[index][1]; \
586  } \
587  } \
588  dst = code; \
589  LAST_SKIP_BITS(name, gb, n)
590 
591 
592 #define GET_VLC_DUAL(dst0, dst1, name, gb, dtable, table1, table2, \
593  bits, max_depth, OP) \
594  do { \
595  unsigned int index = SHOW_UBITS(name, gb, bits); \
596  int code, n = dtable[index][1]; \
597  \
598  if (n<=0) { \
599  int nb_bits; \
600  VLC_INTERN(dst0, table1, gb, name, bits, max_depth); \
601  \
602  UPDATE_CACHE(re, gb); \
603  index = SHOW_UBITS(name, gb, bits); \
604  VLC_INTERN(dst1, table2, gb, name, bits, max_depth); \
605  } else { \
606  code = dtable[index][0]; \
607  OP(dst0, dst1, code); \
608  LAST_SKIP_BITS(name, gb, n); \
609  } \
610  } while (0)
611 
612 #define OP8bits(dst0, dst1, code) dst0 = code>>8; dst1 = code
613 
614 #define READ_2PIX(dst0, dst1, plane1) \
615  UPDATE_CACHE(re, &s->gb); \
616  GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane1].table, \
617  s->vlc[0].table, s->vlc[plane1].table, VLC_BITS, 3, OP8bits)
618 
620 {
621  int i, icount;
622  OPEN_READER(re, &s->gb);
623  count /= 2;
624 
625  icount = get_bits_left(&s->gb) / (32 * 4);
626  if (count >= icount) {
627  for (i = 0; i < icount; i++) {
628  READ_2PIX(s->temp[0][2 * i], s->temp[1][i], 1);
629  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
630  }
631  for (; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
632  READ_2PIX(s->temp[0][2 * i ], s->temp[1][i], 1);
633  if (BITS_LEFT(re, &s->gb) <= 0) break;
634  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
635  }
636  for (; i < count; i++)
637  s->temp[0][2 * i ] = s->temp[1][i] =
638  s->temp[0][2 * i + 1] = s->temp[2][i] = 0;
639  } else {
640  for (i = 0; i < count; i++) {
641  READ_2PIX(s->temp[0][2 * i], s->temp[1][i], 1);
642  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
643  }
644  }
645  CLOSE_READER(re, &s->gb);
646 }
647 
648 #define READ_2PIX_PLANE(dst0, dst1, plane, OP) \
649  UPDATE_CACHE(re, &s->gb); \
650  GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane].table, \
651  s->vlc[plane].table, s->vlc[plane].table, VLC_BITS, 3, OP)
652 
653 #define OP14bits(dst0, dst1, code) dst0 = code>>8; dst1 = sign_extend(code, 8)
654 
655 /* TODO instead of restarting the read when the code isn't in the first level
656  * of the joint table, jump into the 2nd level of the individual table. */
657 #define READ_2PIX_PLANE16(dst0, dst1, plane){\
658  dst0 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
659  dst0 += get_bits(&s->gb, 2);\
660  dst1 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
661  dst1 += get_bits(&s->gb, 2);\
662 }
663 static void decode_plane_bitstream(HYuvContext *s, int width, int plane)
664 {
665  int i, count = width/2;
666 
667  if (s->bps <= 8) {
668  OPEN_READER(re, &s->gb);
669  if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
670  for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
671  READ_2PIX_PLANE(s->temp[0][2 * i], s->temp[0][2 * i + 1], plane, OP8bits);
672  }
673  } else {
674  for(i=0; i<count; i++){
675  READ_2PIX_PLANE(s->temp[0][2 * i], s->temp[0][2 * i + 1], plane, OP8bits);
676  }
677  }
678  if( width&1 && BITS_LEFT(re, &s->gb)>0 ) {
679  unsigned int index;
680  int nb_bits, code, n;
681  UPDATE_CACHE(re, &s->gb);
682  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
683  VLC_INTERN(s->temp[0][width-1], s->vlc[plane].table,
684  &s->gb, re, VLC_BITS, 3);
685  }
686  CLOSE_READER(re, &s->gb);
687  } else if (s->bps <= 14) {
688  OPEN_READER(re, &s->gb);
689  if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
690  for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
691  READ_2PIX_PLANE(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane, OP14bits);
692  }
693  } else {
694  for(i=0; i<count; i++){
695  READ_2PIX_PLANE(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane, OP14bits);
696  }
697  }
698  if( width&1 && BITS_LEFT(re, &s->gb)>0 ) {
699  unsigned int index;
700  int nb_bits, code, n;
701  UPDATE_CACHE(re, &s->gb);
702  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
703  VLC_INTERN(s->temp16[0][width-1], s->vlc[plane].table,
704  &s->gb, re, VLC_BITS, 3);
705  }
706  CLOSE_READER(re, &s->gb);
707  } else {
708  if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
709  for (i = 0; i < count && get_bits_left(&s->gb) > 0; i++) {
710  READ_2PIX_PLANE16(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane);
711  }
712  } else {
713  for(i=0; i<count; i++){
714  READ_2PIX_PLANE16(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane);
715  }
716  }
717  if( width&1 && get_bits_left(&s->gb)>0 ) {
718  int dst = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;
719  s->temp16[0][width-1] = dst + get_bits(&s->gb, 2);
720  }
721  }
722 }
723 
725 {
726  int i;
727  OPEN_READER(re, &s->gb);
728  count /= 2;
729 
730  if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
731  for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
732  READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
733  }
734  } else {
735  for (i = 0; i < count; i++) {
736  READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
737  }
738  }
739  CLOSE_READER(re, &s->gb);
740 }
741 
743  int decorrelate, int alpha)
744 {
745  int i;
746  OPEN_READER(re, &s->gb);
747 
748  for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
749  unsigned int index;
750  int code, n, nb_bits;
751 
752  UPDATE_CACHE(re, &s->gb);
753  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
754  n = s->vlc[4].table[index][1];
755 
756  if (n>0) {
757  code = s->vlc[4].table[index][0];
758  *(uint32_t *) &s->temp[0][4 * i] = s->pix_bgr_map[code];
759  LAST_SKIP_BITS(re, &s->gb, n);
760  } else {
761  if (decorrelate) {
762  VLC_INTERN(s->temp[0][4 * i + G], s->vlc[1].table,
763  &s->gb, re, VLC_BITS, 3);
764 
765  UPDATE_CACHE(re, &s->gb);
766  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
767  VLC_INTERN(code, s->vlc[0].table, &s->gb, re, VLC_BITS, 3);
768  s->temp[0][4 * i + B] = code + s->temp[0][4 * i + G];
769 
770  UPDATE_CACHE(re, &s->gb);
771  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
772  VLC_INTERN(code, s->vlc[2].table, &s->gb, re, VLC_BITS, 3);
773  s->temp[0][4 * i + R] = code + s->temp[0][4 * i + G];
774  } else {
775  VLC_INTERN(s->temp[0][4 * i + B], s->vlc[0].table,
776  &s->gb, re, VLC_BITS, 3);
777 
778  UPDATE_CACHE(re, &s->gb);
779  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
780  VLC_INTERN(s->temp[0][4 * i + G], s->vlc[1].table,
781  &s->gb, re, VLC_BITS, 3);
782 
783  UPDATE_CACHE(re, &s->gb);
784  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
785  VLC_INTERN(s->temp[0][4 * i + R], s->vlc[2].table,
786  &s->gb, re, VLC_BITS, 3);
787  }
788  }
789  if (alpha) {
790  UPDATE_CACHE(re, &s->gb);
791  index = SHOW_UBITS(re, &s->gb, VLC_BITS);
792  VLC_INTERN(s->temp[0][4 * i + A], s->vlc[2].table,
793  &s->gb, re, VLC_BITS, 3);
794  } else
795  s->temp[0][4 * i + A] = 0;
796  }
797  CLOSE_READER(re, &s->gb);
798 }
799 
801 {
802  if (s->decorrelate) {
803  if (s->bitstream_bpp == 24)
804  decode_bgr_1(s, count, 1, 0);
805  else
806  decode_bgr_1(s, count, 1, 1);
807  } else {
808  if (s->bitstream_bpp == 24)
809  decode_bgr_1(s, count, 0, 0);
810  else
811  decode_bgr_1(s, count, 0, 1);
812  }
813 }
814 
815 static void draw_slice(HYuvContext *s, AVFrame *frame, int y)
816 {
817  int h, cy, i;
819 
820  if (!s->avctx->draw_horiz_band)
821  return;
822 
823  h = y - s->last_slice_end;
824  y -= h;
825 
826  if (s->bitstream_bpp == 12)
827  cy = y >> 1;
828  else
829  cy = y;
830 
831  offset[0] = frame->linesize[0] * y;
832  offset[1] = frame->linesize[1] * cy;
833  offset[2] = frame->linesize[2] * cy;
834  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
835  offset[i] = 0;
836  emms_c();
837 
838  s->avctx->draw_horiz_band(s->avctx, frame, offset, y, 3, h);
839 
840  s->last_slice_end = y + h;
841 }
842 
843 static int left_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int acc)
844 {
845  if (s->bps <= 8) {
846  return s->hdsp.add_hfyu_left_pred(dst, src, w, acc);
847  } else {
848  return s->llviddsp.add_hfyu_left_pred_int16(( uint16_t *)dst, (const uint16_t *)src, s->n-1, w, acc);
849  }
850 }
851 
852 static void add_bytes(HYuvContext *s, uint8_t *dst, uint8_t *src, int w)
853 {
854  if (s->bps <= 8) {
855  s->hdsp.add_bytes(dst, src, w);
856  } else {
857  s->llviddsp.add_int16((uint16_t*)dst, (const uint16_t*)src, s->n - 1, w);
858  }
859 }
860 
861 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)
862 {
863  if (s->bps <= 8) {
864  s->hdsp.add_hfyu_median_pred(dst, src, diff, w, left, left_top);
865  } else {
866  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);
867  }
868 }
869 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
870  AVPacket *avpkt)
871 {
872  const uint8_t *buf = avpkt->data;
873  int buf_size = avpkt->size;
874  HYuvContext *s = avctx->priv_data;
875  const int width = s->width;
876  const int width2 = s->width >> 1;
877  const int height = s->height;
878  int fake_ystride, fake_ustride, fake_vstride;
879  ThreadFrame frame = { .f = data };
880  AVFrame *const p = data;
881  int table_size = 0, ret;
882 
885  buf_size);
886  if (!s->bitstream_buffer)
887  return AVERROR(ENOMEM);
888 
889  s->bdsp.bswap_buf((uint32_t *) s->bitstream_buffer,
890  (const uint32_t *) buf, buf_size / 4);
891 
892  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
893  return ret;
894 
895  if (s->context) {
896  table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
897  if (table_size < 0)
898  return table_size;
899  }
900 
901  if ((unsigned) (buf_size - table_size) >= INT_MAX / 8)
902  return AVERROR_INVALIDDATA;
903 
904  if ((ret = init_get_bits(&s->gb, s->bitstream_buffer + table_size,
905  (buf_size - table_size) * 8)) < 0)
906  return ret;
907 
908  fake_ystride = s->interlaced ? p->linesize[0] * 2 : p->linesize[0];
909  fake_ustride = s->interlaced ? p->linesize[1] * 2 : p->linesize[1];
910  fake_vstride = s->interlaced ? p->linesize[2] * 2 : p->linesize[2];
911 
912  s->last_slice_end = 0;
913 
914  if (s->version > 2) {
915  int plane;
916  for(plane = 0; plane < 1 + 2*s->chroma + s->alpha; plane++) {
917  int left, lefttop, y;
918  int w = width;
919  int h = height;
920  int fake_stride = fake_ystride;
921 
922  if (s->chroma && (plane == 1 || plane == 2)) {
923  w >>= s->chroma_h_shift;
924  h >>= s->chroma_v_shift;
925  fake_stride = plane == 1 ? fake_ustride : fake_vstride;
926  }
927 
928  switch (s->predictor) {
929  case LEFT:
930  case PLANE:
931  decode_plane_bitstream(s, w, plane);
932  left = left_prediction(s, p->data[plane], s->temp[0], w, 0);
933 
934  for (y = 1; y < h; y++) {
935  uint8_t *dst = p->data[plane] + p->linesize[plane]*y;
936 
937  decode_plane_bitstream(s, w, plane);
938  left = left_prediction(s, dst, s->temp[0], w, left);
939  if (s->predictor == PLANE) {
940  if (y > s->interlaced) {
941  add_bytes(s, dst, dst - fake_stride, w);
942  }
943  }
944  }
945 
946  break;
947  case MEDIAN:
948  decode_plane_bitstream(s, w, plane);
949  left= left_prediction(s, p->data[plane], s->temp[0], w, 0);
950 
951  y = 1;
952 
953  /* second line is left predicted for interlaced case */
954  if (s->interlaced) {
955  decode_plane_bitstream(s, w, plane);
956  left = left_prediction(s, p->data[plane] + p->linesize[plane], s->temp[0], w, left);
957  y++;
958  }
959 
960  lefttop = p->data[plane][0];
961  decode_plane_bitstream(s, w, plane);
962  add_median_prediction(s, p->data[plane] + fake_stride, p->data[plane], s->temp[0], w, &left, &lefttop);
963  y++;
964 
965  for (; y<h; y++) {
966  uint8_t *dst;
967 
968  decode_plane_bitstream(s, w, plane);
969 
970  dst = p->data[plane] + p->linesize[plane] * y;
971 
972  add_median_prediction(s, dst, dst - fake_stride, s->temp[0], w, &left, &lefttop);
973  }
974 
975  break;
976  }
977  }
978  draw_slice(s, p, height);
979  } else if (s->bitstream_bpp < 24) {
980  int y, cy;
981  int lefty, leftu, leftv;
982  int lefttopy, lefttopu, lefttopv;
983 
984  if (s->yuy2) {
985  p->data[0][3] = get_bits(&s->gb, 8);
986  p->data[0][2] = get_bits(&s->gb, 8);
987  p->data[0][1] = get_bits(&s->gb, 8);
988  p->data[0][0] = get_bits(&s->gb, 8);
989 
990  av_log(avctx, AV_LOG_ERROR,
991  "YUY2 output is not implemented yet\n");
992  return AVERROR_PATCHWELCOME;
993  } else {
994  leftv =
995  p->data[2][0] = get_bits(&s->gb, 8);
996  lefty =
997  p->data[0][1] = get_bits(&s->gb, 8);
998  leftu =
999  p->data[1][0] = get_bits(&s->gb, 8);
1000  p->data[0][0] = get_bits(&s->gb, 8);
1001 
1002  switch (s->predictor) {
1003  case LEFT:
1004  case PLANE:
1005  decode_422_bitstream(s, width - 2);
1006  lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + 2, s->temp[0],
1007  width - 2, lefty);
1008  if (!(s->flags & CODEC_FLAG_GRAY)) {
1009  leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
1010  leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
1011  }
1012 
1013  for (cy = y = 1; y < s->height; y++, cy++) {
1014  uint8_t *ydst, *udst, *vdst;
1015 
1016  if (s->bitstream_bpp == 12) {
1017  decode_gray_bitstream(s, width);
1018 
1019  ydst = p->data[0] + p->linesize[0] * y;
1020 
1021  lefty = s->hdsp.add_hfyu_left_pred(ydst, s->temp[0],
1022  width, lefty);
1023  if (s->predictor == PLANE) {
1024  if (y > s->interlaced)
1025  s->hdsp.add_bytes(ydst, ydst - fake_ystride, width);
1026  }
1027  y++;
1028  if (y >= s->height)
1029  break;
1030  }
1031 
1032  draw_slice(s, p, y);
1033 
1034  ydst = p->data[0] + p->linesize[0] * y;
1035  udst = p->data[1] + p->linesize[1] * cy;
1036  vdst = p->data[2] + p->linesize[2] * cy;
1037 
1038  decode_422_bitstream(s, width);
1039  lefty = s->hdsp.add_hfyu_left_pred(ydst, s->temp[0],
1040  width, lefty);
1041  if (!(s->flags & CODEC_FLAG_GRAY)) {
1042  leftu = s->hdsp.add_hfyu_left_pred(udst, s->temp[1], width2, leftu);
1043  leftv = s->hdsp.add_hfyu_left_pred(vdst, s->temp[2], width2, leftv);
1044  }
1045  if (s->predictor == PLANE) {
1046  if (cy > s->interlaced) {
1047  s->hdsp.add_bytes(ydst, ydst - fake_ystride, width);
1048  if (!(s->flags & CODEC_FLAG_GRAY)) {
1049  s->hdsp.add_bytes(udst, udst - fake_ustride, width2);
1050  s->hdsp.add_bytes(vdst, vdst - fake_vstride, width2);
1051  }
1052  }
1053  }
1054  }
1055  draw_slice(s, p, height);
1056 
1057  break;
1058  case MEDIAN:
1059  /* first line except first 2 pixels is left predicted */
1060  decode_422_bitstream(s, width - 2);
1061  lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + 2, s->temp[0],
1062  width - 2, lefty);
1063  if (!(s->flags & CODEC_FLAG_GRAY)) {
1064  leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
1065  leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
1066  }
1067 
1068  cy = y = 1;
1069 
1070  /* second line is left predicted for interlaced case */
1071  if (s->interlaced) {
1072  decode_422_bitstream(s, width);
1073  lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + p->linesize[0],
1074  s->temp[0], width, lefty);
1075  if (!(s->flags & CODEC_FLAG_GRAY)) {
1076  leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
1077  leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
1078  }
1079  y++;
1080  cy++;
1081  }
1082 
1083  /* next 4 pixels are left predicted too */
1084  decode_422_bitstream(s, 4);
1085  lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + fake_ystride,
1086  s->temp[0], 4, lefty);
1087  if (!(s->flags & CODEC_FLAG_GRAY)) {
1088  leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
1089  leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
1090  }
1091 
1092  /* next line except the first 4 pixels is median predicted */
1093  lefttopy = p->data[0][3];
1094  decode_422_bitstream(s, width - 4);
1095  s->hdsp.add_hfyu_median_pred(p->data[0] + fake_ystride + 4,
1096  p->data[0] + 4, s->temp[0],
1097  width - 4, &lefty, &lefttopy);
1098  if (!(s->flags & CODEC_FLAG_GRAY)) {
1099  lefttopu = p->data[1][1];
1100  lefttopv = p->data[2][1];
1101  s->hdsp.add_hfyu_median_pred(p->data[1] + fake_ustride + 2, p->data[1] + 2, s->temp[1], width2 - 2, &leftu, &lefttopu);
1102  s->hdsp.add_hfyu_median_pred(p->data[2] + fake_vstride + 2, p->data[2] + 2, s->temp[2], width2 - 2, &leftv, &lefttopv);
1103  }
1104  y++;
1105  cy++;
1106 
1107  for (; y < height; y++, cy++) {
1108  uint8_t *ydst, *udst, *vdst;
1109 
1110  if (s->bitstream_bpp == 12) {
1111  while (2 * cy > y) {
1112  decode_gray_bitstream(s, width);
1113  ydst = p->data[0] + p->linesize[0] * y;
1114  s->hdsp.add_hfyu_median_pred(ydst, ydst - fake_ystride,
1115  s->temp[0], width,
1116  &lefty, &lefttopy);
1117  y++;
1118  }
1119  if (y >= height)
1120  break;
1121  }
1122  draw_slice(s, p, y);
1123 
1124  decode_422_bitstream(s, width);
1125 
1126  ydst = p->data[0] + p->linesize[0] * y;
1127  udst = p->data[1] + p->linesize[1] * cy;
1128  vdst = p->data[2] + p->linesize[2] * cy;
1129 
1130  s->hdsp.add_hfyu_median_pred(ydst, ydst - fake_ystride,
1131  s->temp[0], width,
1132  &lefty, &lefttopy);
1133  if (!(s->flags & CODEC_FLAG_GRAY)) {
1134  s->hdsp.add_hfyu_median_pred(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
1135  s->hdsp.add_hfyu_median_pred(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
1136  }
1137  }
1138 
1139  draw_slice(s, p, height);
1140  break;
1141  }
1142  }
1143  } else {
1144  int y;
1145  uint8_t left[4];
1146  const int last_line = (height - 1) * p->linesize[0];
1147 
1148  if (s->bitstream_bpp == 32) {
1149  left[A] = p->data[0][last_line + A] = get_bits(&s->gb, 8);
1150  left[R] = p->data[0][last_line + R] = get_bits(&s->gb, 8);
1151  left[G] = p->data[0][last_line + G] = get_bits(&s->gb, 8);
1152  left[B] = p->data[0][last_line + B] = get_bits(&s->gb, 8);
1153  } else {
1154  left[R] = p->data[0][last_line + R] = get_bits(&s->gb, 8);
1155  left[G] = p->data[0][last_line + G] = get_bits(&s->gb, 8);
1156  left[B] = p->data[0][last_line + B] = get_bits(&s->gb, 8);
1157  left[A] = p->data[0][last_line + A] = 255;
1158  skip_bits(&s->gb, 8);
1159  }
1160 
1161  if (s->bgr32) {
1162  switch (s->predictor) {
1163  case LEFT:
1164  case PLANE:
1165  decode_bgr_bitstream(s, width - 1);
1166  s->hdsp.add_hfyu_left_pred_bgr32(p->data[0] + last_line + 4,
1167  s->temp[0], width - 1, left);
1168 
1169  for (y = s->height - 2; y >= 0; y--) { // Yes it is stored upside down.
1170  decode_bgr_bitstream(s, width);
1171 
1172  s->hdsp.add_hfyu_left_pred_bgr32(p->data[0] + p->linesize[0] * y,
1173  s->temp[0], width, left);
1174  if (s->predictor == PLANE) {
1175  if (s->bitstream_bpp != 32)
1176  left[A] = 0;
1177  if ((y & s->interlaced) == 0 &&
1178  y < s->height - 1 - s->interlaced) {
1179  s->hdsp.add_bytes(p->data[0] + p->linesize[0] * y,
1180  p->data[0] + p->linesize[0] * y +
1181  fake_ystride, fake_ystride);
1182  }
1183  }
1184  }
1185  // just 1 large slice as this is not possible in reverse order
1186  draw_slice(s, p, height);
1187  break;
1188  default:
1189  av_log(avctx, AV_LOG_ERROR,
1190  "prediction type not supported!\n");
1191  }
1192  } else {
1193  av_log(avctx, AV_LOG_ERROR,
1194  "BGR24 output is not implemented yet\n");
1195  return AVERROR_PATCHWELCOME;
1196  }
1197  }
1198  emms_c();
1199 
1200  *got_frame = 1;
1201 
1202  return (get_bits_count(&s->gb) + 31) / 32 * 4 + table_size;
1203 }
1204 
1206 {
1207  HYuvContext *s = avctx->priv_data;
1208  int i;
1209 
1212 
1213  for (i = 0; i < 8; i++)
1214  ff_free_vlc(&s->vlc[i]);
1215 
1216  return 0;
1217 }
1218 
1220  .name = "huffyuv",
1221  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
1222  .type = AVMEDIA_TYPE_VIDEO,
1223  .id = AV_CODEC_ID_HUFFYUV,
1224  .priv_data_size = sizeof(HYuvContext),
1225  .init = decode_init,
1226  .close = decode_end,
1227  .decode = decode_frame,
1228  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
1231 };
1232 
1233 #if CONFIG_FFVHUFF_DECODER
1234 AVCodec ff_ffvhuff_decoder = {
1235  .name = "ffvhuff",
1236  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
1237  .type = AVMEDIA_TYPE_VIDEO,
1238  .id = AV_CODEC_ID_FFVHUFF,
1239  .priv_data_size = sizeof(HYuvContext),
1240  .init = decode_init,
1241  .close = decode_end,
1242  .decode = decode_frame,
1243  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
1246 };
1247 #endif /* CONFIG_FFVHUFF_DECODER */