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-2003 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 
26 /**
27  * @file
28  * huffyuv decoder
29  */
30 
31 #include "avcodec.h"
32 #include "get_bits.h"
33 #include "huffyuv.h"
34 #include "thread.h"
35 
36 #define classic_shift_luma_table_size 42
38  34,36,35,69,135,232,9,16,10,24,11,23,12,16,13,10,14,8,15,8,
39  16,8,17,20,16,10,207,206,205,236,11,8,10,21,9,23,8,8,199,70,
40  69,68, 0,
41  0,0,0,0,0,0,0,0,
42 };
43 
44 #define classic_shift_chroma_table_size 59
46  66,36,37,38,39,40,41,75,76,77,110,239,144,81,82,83,84,85,118,183,
47  56,57,88,89,56,89,154,57,58,57,26,141,57,56,58,57,58,57,184,119,
48  214,245,116,83,82,49,80,79,78,77,44,75,41,40,39,38,37,36,34, 0,
49  0,0,0,0,0,0,0,0,
50 };
51 
52 static const unsigned char classic_add_luma[256] = {
53  3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
54  73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
55  68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
56  35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
57  37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
58  35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
59  27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
60  15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
61  12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
62  12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
63  18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
64  28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
65  28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
66  62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
67  54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
68  46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
69 };
70 
71 static const unsigned char classic_add_chroma[256] = {
72  3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
73  7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
74  11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
75  43, 45, 76, 81, 46, 82, 75, 55, 56,144, 58, 80, 60, 74,147, 63,
76  143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
77  80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
78  17, 14, 5, 6,100, 54, 47, 50, 51, 53,106,107,108,109,110,111,
79  112,113,114,115, 4,117,118, 92, 94,121,122, 3,124,103, 2, 1,
80  0,129,130,131,120,119,126,125,136,137,138,139,140,141,142,134,
81  135,132,133,104, 64,101, 62, 57,102, 95, 93, 59, 61, 28, 97, 96,
82  52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
83  19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
84  7,128,127,105,123,116, 35, 34, 33,145, 31, 79, 42,146, 78, 26,
85  83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
86  14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
87  6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
88 };
89 
90 static int read_len_table(uint8_t *dst, GetBitContext *gb)
91 {
92  int i, val, repeat;
93 
94  for (i = 0; i < 256;) {
95  repeat = get_bits(gb, 3);
96  val = get_bits(gb, 5);
97  if (repeat == 0)
98  repeat = get_bits(gb, 8);
99  if (i + repeat > 256 || get_bits_left(gb) < 0) {
100  av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n");
101  return -1;
102  }
103  while (repeat--)
104  dst[i++] = val;
105  }
106  return 0;
107 }
108 
110 {
111  uint16_t symbols[1 << VLC_BITS];
112  uint16_t bits[1 << VLC_BITS];
113  uint8_t len[1 << VLC_BITS];
114  int ret;
115 
116  if (s->bitstream_bpp < 24) {
117  int p, i, y, u;
118  for (p = 0; p < 3; p++) {
119  for (i = y = 0; y < 256; y++) {
120  int len0 = s->len[0][y];
121  int limit = VLC_BITS - len0;
122  if(limit <= 0 || !len0)
123  continue;
124  for (u = 0; u < 256; u++) {
125  int len1 = s->len[p][u];
126  if (len1 > limit || !len1)
127  continue;
128  av_assert0(i < (1 << VLC_BITS));
129  len[i] = len0 + len1;
130  bits[i] = (s->bits[0][y] << len1) + s->bits[p][u];
131  symbols[i] = (y << 8) + u;
132  if(symbols[i] != 0xffff) // reserved to mean "invalid"
133  i++;
134  }
135  }
136  ff_free_vlc(&s->vlc[3 + p]);
137  if ((ret = ff_init_vlc_sparse(&s->vlc[3 + p], VLC_BITS, i, len, 1, 1,
138  bits, 2, 2, symbols, 2, 2, 0)) < 0)
139  return ret;
140  }
141  } else {
142  uint8_t (*map)[4] = (uint8_t(*)[4])s->pix_bgr_map;
143  int i, b, g, r, code;
144  int p0 = s->decorrelate;
145  int p1 = !s->decorrelate;
146  // restrict the range to +/-16 because that's pretty much guaranteed to
147  // cover all the combinations that fit in 11 bits total, and it doesn't
148  // matter if we miss a few rare codes.
149  for (i = 0, g = -16; g < 16; g++) {
150  int len0 = s->len[p0][g & 255];
151  int limit0 = VLC_BITS - len0;
152  if (limit0 < 2 || !len0)
153  continue;
154  for (b = -16; b < 16; b++) {
155  int len1 = s->len[p1][b & 255];
156  int limit1 = limit0 - len1;
157  if (limit1 < 1 || !len1)
158  continue;
159  code = (s->bits[p0][g & 255] << len1) + s->bits[p1][b & 255];
160  for (r = -16; r < 16; r++) {
161  int len2 = s->len[2][r & 255];
162  if (len2 > limit1 || !len2)
163  continue;
164  av_assert0(i < (1 << VLC_BITS));
165  len[i] = len0 + len1 + len2;
166  bits[i] = (code << len2) + s->bits[2][r & 255];
167  if (s->decorrelate) {
168  map[i][G] = g;
169  map[i][B] = g + b;
170  map[i][R] = g + r;
171  } else {
172  map[i][B] = g;
173  map[i][G] = b;
174  map[i][R] = r;
175  }
176  i++;
177  }
178  }
179  }
180  ff_free_vlc(&s->vlc[3]);
181  if ((ret = init_vlc(&s->vlc[3], VLC_BITS, i, len, 1, 1, bits, 2, 2, 0)) < 0)
182  return ret;
183  }
184  return 0;
185 }
186 
188 {
189  GetBitContext gb;
190  int i;
191  int ret;
192 
193  init_get_bits(&gb, src, length * 8);
194 
195  for (i = 0; i < 3; i++) {
196  if (read_len_table(s->len[i], &gb) < 0)
197  return -1;
198  if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0) {
199  return -1;
200  }
201  ff_free_vlc(&s->vlc[i]);
202  if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1,
203  s->bits[i], 4, 4, 0)) < 0)
204  return ret;
205  }
206 
207  if ((ret = generate_joint_tables(s)) < 0)
208  return ret;
209 
210  return (get_bits_count(&gb) + 7) / 8;
211 }
212 
214 {
215  GetBitContext gb;
216  int i;
217  int ret;
218 
221  if (read_len_table(s->len[0], &gb) < 0)
222  return -1;
223 
226  if (read_len_table(s->len[1], &gb) < 0)
227  return -1;
228 
229  for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma [i];
230  for(i=0; i<256; i++) s->bits[1][i] = classic_add_chroma[i];
231 
232  if (s->bitstream_bpp >= 24) {
233  memcpy(s->bits[1], s->bits[0], 256 * sizeof(uint32_t));
234  memcpy(s->len[1] , s->len [0], 256 * sizeof(uint8_t));
235  }
236  memcpy(s->bits[2], s->bits[1], 256 * sizeof(uint32_t));
237  memcpy(s->len[2] , s->len [1], 256 * sizeof(uint8_t));
238 
239  for (i = 0; i < 3; i++) {
240  ff_free_vlc(&s->vlc[i]);
241  if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1,
242  s->bits[i], 4, 4, 0)) < 0)
243  return ret;
244  }
245 
246  if ((ret = generate_joint_tables(s)) < 0)
247  return ret;
248 
249  return 0;
250 }
251 
253 {
254  HYuvContext *s = avctx->priv_data;
255 
256  ff_huffyuv_common_init(avctx);
257  memset(s->vlc, 0, 3 * sizeof(VLC));
258 
259  s->interlaced = s->height > 288;
260 
261  s->bgr32 = 1;
262 
263  if (avctx->extradata_size) {
264  if ((avctx->bits_per_coded_sample & 7) &&
265  avctx->bits_per_coded_sample != 12)
266  s->version = 1; // do such files exist at all?
267  else
268  s->version = 2;
269  } else
270  s->version = 0;
271 
272  if (s->version == 2) {
273  int method, interlace;
274 
275  if (avctx->extradata_size < 4)
276  return -1;
277 
278  method = ((uint8_t*)avctx->extradata)[0];
279  s->decorrelate = method & 64 ? 1 : 0;
280  s->predictor = method & 63;
281  s->bitstream_bpp = ((uint8_t*)avctx->extradata)[1];
282  if (s->bitstream_bpp == 0)
283  s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
284  interlace = (((uint8_t*)avctx->extradata)[2] & 0x30) >> 4;
285  s->interlaced = (interlace == 1) ? 1 : (interlace == 2) ? 0 : s->interlaced;
286  s->context = ((uint8_t*)avctx->extradata)[2] & 0x40 ? 1 : 0;
287 
288  if ( read_huffman_tables(s, ((uint8_t*)avctx->extradata) + 4,
289  avctx->extradata_size - 4) < 0)
290  return AVERROR_INVALIDDATA;
291  }else{
292  switch (avctx->bits_per_coded_sample & 7) {
293  case 1:
294  s->predictor = LEFT;
295  s->decorrelate = 0;
296  break;
297  case 2:
298  s->predictor = LEFT;
299  s->decorrelate = 1;
300  break;
301  case 3:
302  s->predictor = PLANE;
303  s->decorrelate = avctx->bits_per_coded_sample >= 24;
304  break;
305  case 4:
306  s->predictor = MEDIAN;
307  s->decorrelate = 0;
308  break;
309  default:
310  s->predictor = LEFT; //OLD
311  s->decorrelate = 0;
312  break;
313  }
314  s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
315  s->context = 0;
316 
317  if (read_old_huffman_tables(s) < 0)
318  return AVERROR_INVALIDDATA;
319  }
320 
321  switch (s->bitstream_bpp) {
322  case 12:
323  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
324  break;
325  case 16:
326  if (s->yuy2) {
327  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
328  } else {
329  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
330  }
331  break;
332  case 24:
333  case 32:
334  if (s->bgr32) {
335  avctx->pix_fmt = AV_PIX_FMT_RGB32;
336  } else {
337  avctx->pix_fmt = AV_PIX_FMT_BGR24;
338  }
339  break;
340  default:
341  return AVERROR_INVALIDDATA;
342  }
343 
344  if ((avctx->pix_fmt == AV_PIX_FMT_YUV422P || avctx->pix_fmt == AV_PIX_FMT_YUV420P) && avctx->width & 1) {
345  av_log(avctx, AV_LOG_ERROR, "width must be even for this colorspace\n");
346  return AVERROR_INVALIDDATA;
347  }
348  if (s->predictor == MEDIAN && avctx->pix_fmt == AV_PIX_FMT_YUV422P && avctx->width%4) {
349  av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 4 this colorspace and predictor\n");
350  return AVERROR_INVALIDDATA;
351  }
352  if (ff_huffyuv_alloc_temp(s)) {
354  return AVERROR(ENOMEM);
355  }
356 
357  return 0;
358 }
359 
361 {
362  HYuvContext *s = avctx->priv_data;
363  int i;
364 
365  if (ff_huffyuv_alloc_temp(s)) {
367  return AVERROR(ENOMEM);
368  }
369 
370  for (i = 0; i < 6; i++)
371  s->vlc[i].table = NULL;
372 
373  if (s->version == 2) {
374  if (read_huffman_tables(s, ((uint8_t*)avctx->extradata) + 4,
375  avctx->extradata_size) < 0)
376  return AVERROR_INVALIDDATA;
377  } else {
378  if (read_old_huffman_tables(s) < 0)
379  return AVERROR_INVALIDDATA;
380  }
381 
382  return 0;
383 }
384 
385 /* TODO instead of restarting the read when the code isn't in the first level
386  * of the joint table, jump into the 2nd level of the individual table. */
387 #define READ_2PIX(dst0, dst1, plane1){\
388  uint16_t code = get_vlc2(&s->gb, s->vlc[3+plane1].table, VLC_BITS, 1);\
389  if(code != 0xffff){\
390  dst0 = code>>8;\
391  dst1 = code;\
392  }else{\
393  dst0 = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);\
394  dst1 = get_vlc2(&s->gb, s->vlc[plane1].table, VLC_BITS, 3);\
395  }\
396 }
397 
399 {
400  int i;
401 
402  count /= 2;
403 
404  if (count >= (get_bits_left(&s->gb)) / (31 * 4)) {
405  for (i = 0; i < count && get_bits_left(&s->gb) > 0; i++) {
406  READ_2PIX(s->temp[0][2 * i ], s->temp[1][i], 1);
407  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
408  }
409  } else {
410  for (i = 0; i < count; i++) {
411  READ_2PIX(s->temp[0][2 * i ], s->temp[1][i], 1);
412  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
413  }
414  }
415 }
416 
418 {
419  int i;
420 
421  count/=2;
422 
423  if (count >= (get_bits_left(&s->gb)) / (31 * 2)) {
424  for (i = 0; i < count && get_bits_left(&s->gb) > 0; i++) {
425  READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
426  }
427  } else {
428  for(i=0; i<count; i++){
429  READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
430  }
431  }
432 }
433 
435  int decorrelate, int alpha)
436 {
437  int i;
438  for (i = 0; i < count; i++) {
439  int code = get_vlc2(&s->gb, s->vlc[3].table, VLC_BITS, 1);
440  if (code != -1) {
441  *(uint32_t*)&s->temp[0][4 * i] = s->pix_bgr_map[code];
442  } else if(decorrelate) {
443  s->temp[0][4 * i + G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
444  s->temp[0][4 * i + B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) +
445  s->temp[0][4 * i + G];
446  s->temp[0][4 * i + R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) +
447  s->temp[0][4 * i + G];
448  } else {
449  s->temp[0][4 * i + B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
450  s->temp[0][4 * i + G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
451  s->temp[0][4 * i + R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
452  }
453  if (alpha)
454  s->temp[0][4 * i + A] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
455  }
456 }
457 
459 {
460  if (s->decorrelate) {
461  if (s->bitstream_bpp==24)
462  decode_bgr_1(s, count, 1, 0);
463  else
464  decode_bgr_1(s, count, 1, 1);
465  } else {
466  if (s->bitstream_bpp==24)
467  decode_bgr_1(s, count, 0, 0);
468  else
469  decode_bgr_1(s, count, 0, 1);
470  }
471 }
472 
473 static void draw_slice(HYuvContext *s, AVFrame *frame, int y)
474 {
475  int h, cy, i;
477 
478  if (s->avctx->draw_horiz_band==NULL)
479  return;
480 
481  h = y - s->last_slice_end;
482  y -= h;
483 
484  if (s->bitstream_bpp == 12) {
485  cy = y>>1;
486  } else {
487  cy = y;
488  }
489 
490  offset[0] = frame->linesize[0] * y;
491  offset[1] = frame->linesize[1] * cy;
492  offset[2] = frame->linesize[2] * cy;
493  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
494  offset[i] = 0;
495  emms_c();
496 
497  s->avctx->draw_horiz_band(s->avctx, frame, offset, y, 3, h);
498 
499  s->last_slice_end = y + h;
500 }
501 
502 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
503  AVPacket *avpkt)
504 {
505  const uint8_t *buf = avpkt->data;
506  int buf_size = avpkt->size;
507  HYuvContext *s = avctx->priv_data;
508  const int width = s->width;
509  const int width2 = s->width>>1;
510  const int height = s->height;
511  int fake_ystride, fake_ustride, fake_vstride;
512  ThreadFrame frame = { .f = data };
513  AVFrame * const p = data;
514  int table_size = 0, ret;
515 
518  buf_size);
519  if (!s->bitstream_buffer)
520  return AVERROR(ENOMEM);
521 
522  s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer,
523  (const uint32_t*)buf, buf_size / 4);
524 
525  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
526  return ret;
527 
528  if (s->context) {
529  table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
530  if (table_size < 0)
531  return AVERROR_INVALIDDATA;
532  }
533 
534  if ((unsigned)(buf_size-table_size) >= INT_MAX / 8)
535  return AVERROR_INVALIDDATA;
536 
537  init_get_bits(&s->gb, s->bitstream_buffer+table_size,
538  (buf_size-table_size) * 8);
539 
540  fake_ystride = s->interlaced ? p->linesize[0] * 2 : p->linesize[0];
541  fake_ustride = s->interlaced ? p->linesize[1] * 2 : p->linesize[1];
542  fake_vstride = s->interlaced ? p->linesize[2] * 2 : p->linesize[2];
543 
544  s->last_slice_end = 0;
545 
546  if (s->bitstream_bpp < 24) {
547  int y, cy;
548  int lefty, leftu, leftv;
549  int lefttopy, lefttopu, lefttopv;
550 
551  if (s->yuy2) {
552  p->data[0][3] = get_bits(&s->gb, 8);
553  p->data[0][2] = get_bits(&s->gb, 8);
554  p->data[0][1] = get_bits(&s->gb, 8);
555  p->data[0][0] = get_bits(&s->gb, 8);
556 
557  av_log(avctx, AV_LOG_ERROR,
558  "YUY2 output is not implemented yet\n");
559  return AVERROR_PATCHWELCOME;
560  } else {
561 
562  leftv = p->data[2][0] = get_bits(&s->gb, 8);
563  lefty = p->data[0][1] = get_bits(&s->gb, 8);
564  leftu = p->data[1][0] = get_bits(&s->gb, 8);
565  p->data[0][0] = get_bits(&s->gb, 8);
566 
567  switch (s->predictor) {
568  case LEFT:
569  case PLANE:
570  decode_422_bitstream(s, width-2);
571  lefty = s->dsp.add_hfyu_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
572  if (!(s->flags&CODEC_FLAG_GRAY)) {
573  leftu = s->dsp.add_hfyu_left_prediction(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
574  leftv = s->dsp.add_hfyu_left_prediction(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
575  }
576 
577  for (cy = y = 1; y < s->height; y++, cy++) {
578  uint8_t *ydst, *udst, *vdst;
579 
580  if (s->bitstream_bpp == 12) {
581  decode_gray_bitstream(s, width);
582 
583  ydst = p->data[0] + p->linesize[0] * y;
584 
585  lefty = s->dsp.add_hfyu_left_prediction(ydst, s->temp[0], width, lefty);
586  if (s->predictor == PLANE) {
587  if (y > s->interlaced)
588  s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
589  }
590  y++;
591  if (y >= s->height) break;
592  }
593 
594  draw_slice(s, p, y);
595 
596  ydst = p->data[0] + p->linesize[0]*y;
597  udst = p->data[1] + p->linesize[1]*cy;
598  vdst = p->data[2] + p->linesize[2]*cy;
599 
600  decode_422_bitstream(s, width);
601  lefty = s->dsp.add_hfyu_left_prediction(ydst, s->temp[0], width, lefty);
602  if (!(s->flags & CODEC_FLAG_GRAY)) {
603  leftu= s->dsp.add_hfyu_left_prediction(udst, s->temp[1], width2, leftu);
604  leftv= s->dsp.add_hfyu_left_prediction(vdst, s->temp[2], width2, leftv);
605  }
606  if (s->predictor == PLANE) {
607  if (cy > s->interlaced) {
608  s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
609  if (!(s->flags & CODEC_FLAG_GRAY)) {
610  s->dsp.add_bytes(udst, udst - fake_ustride, width2);
611  s->dsp.add_bytes(vdst, vdst - fake_vstride, width2);
612  }
613  }
614  }
615  }
616  draw_slice(s, p, height);
617 
618  break;
619  case MEDIAN:
620  /* first line except first 2 pixels is left predicted */
621  decode_422_bitstream(s, width - 2);
622  lefty= s->dsp.add_hfyu_left_prediction(p->data[0] + 2, s->temp[0], width - 2, lefty);
623  if (!(s->flags & CODEC_FLAG_GRAY)) {
624  leftu = s->dsp.add_hfyu_left_prediction(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
625  leftv = s->dsp.add_hfyu_left_prediction(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
626  }
627 
628  cy = y = 1;
629 
630  /* second line is left predicted for interlaced case */
631  if (s->interlaced) {
632  decode_422_bitstream(s, width);
633  lefty = s->dsp.add_hfyu_left_prediction(p->data[0] + p->linesize[0], s->temp[0], width, lefty);
634  if (!(s->flags & CODEC_FLAG_GRAY)) {
635  leftu = s->dsp.add_hfyu_left_prediction(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
636  leftv = s->dsp.add_hfyu_left_prediction(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
637  }
638  y++; cy++;
639  }
640 
641  /* next 4 pixels are left predicted too */
642  decode_422_bitstream(s, 4);
643  lefty = s->dsp.add_hfyu_left_prediction(p->data[0] + fake_ystride, s->temp[0], 4, lefty);
644  if (!(s->flags&CODEC_FLAG_GRAY)) {
645  leftu = s->dsp.add_hfyu_left_prediction(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
646  leftv = s->dsp.add_hfyu_left_prediction(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
647  }
648 
649  /* next line except the first 4 pixels is median predicted */
650  lefttopy = p->data[0][3];
651  decode_422_bitstream(s, width - 4);
652  s->dsp.add_hfyu_median_prediction(p->data[0] + fake_ystride+4, p->data[0]+4, s->temp[0], width-4, &lefty, &lefttopy);
653  if (!(s->flags&CODEC_FLAG_GRAY)) {
654  lefttopu = p->data[1][1];
655  lefttopv = p->data[2][1];
656  s->dsp.add_hfyu_median_prediction(p->data[1] + fake_ustride+2, p->data[1] + 2, s->temp[1], width2 - 2, &leftu, &lefttopu);
657  s->dsp.add_hfyu_median_prediction(p->data[2] + fake_vstride+2, p->data[2] + 2, s->temp[2], width2 - 2, &leftv, &lefttopv);
658  }
659  y++; cy++;
660 
661  for (; y<height; y++, cy++) {
662  uint8_t *ydst, *udst, *vdst;
663 
664  if (s->bitstream_bpp == 12) {
665  while (2 * cy > y) {
666  decode_gray_bitstream(s, width);
667  ydst = p->data[0] + p->linesize[0] * y;
668  s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
669  y++;
670  }
671  if (y >= height) break;
672  }
673  draw_slice(s, p, y);
674 
675  decode_422_bitstream(s, width);
676 
677  ydst = p->data[0] + p->linesize[0] * y;
678  udst = p->data[1] + p->linesize[1] * cy;
679  vdst = p->data[2] + p->linesize[2] * cy;
680 
681  s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
682  if (!(s->flags & CODEC_FLAG_GRAY)) {
683  s->dsp.add_hfyu_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
684  s->dsp.add_hfyu_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
685  }
686  }
687 
688  draw_slice(s, p, height);
689  break;
690  }
691  }
692  } else {
693  int y;
694  int leftr, leftg, leftb, lefta;
695  const int last_line = (height - 1) * p->linesize[0];
696 
697  if (s->bitstream_bpp == 32) {
698  lefta = p->data[0][last_line+A] = get_bits(&s->gb, 8);
699  leftr = p->data[0][last_line+R] = get_bits(&s->gb, 8);
700  leftg = p->data[0][last_line+G] = get_bits(&s->gb, 8);
701  leftb = p->data[0][last_line+B] = get_bits(&s->gb, 8);
702  } else {
703  leftr = p->data[0][last_line+R] = get_bits(&s->gb, 8);
704  leftg = p->data[0][last_line+G] = get_bits(&s->gb, 8);
705  leftb = p->data[0][last_line+B] = get_bits(&s->gb, 8);
706  lefta = p->data[0][last_line+A] = 255;
707  skip_bits(&s->gb, 8);
708  }
709 
710  if (s->bgr32) {
711  switch (s->predictor) {
712  case LEFT:
713  case PLANE:
714  decode_bgr_bitstream(s, width - 1);
715  s->dsp.add_hfyu_left_prediction_bgr32(p->data[0] + last_line+4, s->temp[0], width - 1, &leftr, &leftg, &leftb, &lefta);
716 
717  for (y = s->height - 2; y >= 0; y--) { //Yes it is stored upside down.
718  decode_bgr_bitstream(s, width);
719 
720  s->dsp.add_hfyu_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb, &lefta);
721  if (s->predictor == PLANE) {
722  if (s->bitstream_bpp != 32) lefta = 0;
723  if ((y & s->interlaced) == 0 &&
724  y < s->height - 1 - s->interlaced) {
725  s->dsp.add_bytes(p->data[0] + p->linesize[0] * y,
726  p->data[0] + p->linesize[0] * y +
727  fake_ystride, fake_ystride);
728  }
729  }
730  }
731  // just 1 large slice as this is not possible in reverse order
732  draw_slice(s, p, height);
733  break;
734  default:
735  av_log(avctx, AV_LOG_ERROR,
736  "prediction type not supported!\n");
737  }
738  }else{
739  av_log(avctx, AV_LOG_ERROR,
740  "BGR24 output is not implemented yet\n");
741  return AVERROR_PATCHWELCOME;
742  }
743  }
744  emms_c();
745 
746  *got_frame = 1;
747 
748  return (get_bits_count(&s->gb) + 31) / 32 * 4 + table_size;
749 }
750 
752 {
753  HYuvContext *s = avctx->priv_data;
754  int i;
755 
758 
759  for (i = 0; i < 6; i++) {
760  ff_free_vlc(&s->vlc[i]);
761  }
762 
763  return 0;
764 }
765 
766 #if CONFIG_HUFFYUV_DECODER
767 AVCodec ff_huffyuv_decoder = {
768  .name = "huffyuv",
769  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
770  .type = AVMEDIA_TYPE_VIDEO,
771  .id = AV_CODEC_ID_HUFFYUV,
772  .priv_data_size = sizeof(HYuvContext),
773  .init = decode_init,
774  .close = decode_end,
775  .decode = decode_frame,
776  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
779 };
780 #endif
781 
782 #if CONFIG_FFVHUFF_DECODER
783 AVCodec ff_ffvhuff_decoder = {
784  .name = "ffvhuff",
785  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
786  .type = AVMEDIA_TYPE_VIDEO,
787  .id = AV_CODEC_ID_FFVHUFF,
788  .priv_data_size = sizeof(HYuvContext),
789  .init = decode_init,
790  .close = decode_end,
791  .decode = decode_frame,
792  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
795 };
796 #endif