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