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