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 
260  s->interlaced = s->height > 288;
261 
262  s->bgr32 = 1;
263 
264  if (avctx->extradata_size) {
265  if ((avctx->bits_per_coded_sample & 7) &&
266  avctx->bits_per_coded_sample != 12)
267  s->version = 1; // do such files exist at all?
268  else
269  s->version = 2;
270  } else
271  s->version = 0;
272 
273  if (s->version == 2) {
274  int method, interlace;
275 
276  if (avctx->extradata_size < 4)
277  return -1;
278 
279  method = ((uint8_t*)avctx->extradata)[0];
280  s->decorrelate = method & 64 ? 1 : 0;
281  s->predictor = method & 63;
282  s->bitstream_bpp = ((uint8_t*)avctx->extradata)[1];
283  if (s->bitstream_bpp == 0)
284  s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
285  interlace = (((uint8_t*)avctx->extradata)[2] & 0x30) >> 4;
286  s->interlaced = (interlace == 1) ? 1 : (interlace == 2) ? 0 : s->interlaced;
287  s->context = ((uint8_t*)avctx->extradata)[2] & 0x40 ? 1 : 0;
288 
289  if ( read_huffman_tables(s, ((uint8_t*)avctx->extradata) + 4,
290  avctx->extradata_size - 4) < 0)
291  return AVERROR_INVALIDDATA;
292  }else{
293  switch (avctx->bits_per_coded_sample & 7) {
294  case 1:
295  s->predictor = LEFT;
296  s->decorrelate = 0;
297  break;
298  case 2:
299  s->predictor = LEFT;
300  s->decorrelate = 1;
301  break;
302  case 3:
303  s->predictor = PLANE;
304  s->decorrelate = avctx->bits_per_coded_sample >= 24;
305  break;
306  case 4:
307  s->predictor = MEDIAN;
308  s->decorrelate = 0;
309  break;
310  default:
311  s->predictor = LEFT; //OLD
312  s->decorrelate = 0;
313  break;
314  }
315  s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
316  s->context = 0;
317 
318  if (read_old_huffman_tables(s) < 0)
319  return AVERROR_INVALIDDATA;
320  }
321 
322  switch (s->bitstream_bpp) {
323  case 12:
324  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
325  break;
326  case 16:
327  if (s->yuy2) {
328  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
329  } else {
330  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
331  }
332  break;
333  case 24:
334  case 32:
335  if (s->bgr32) {
336  avctx->pix_fmt = AV_PIX_FMT_RGB32;
337  } else {
338  avctx->pix_fmt = AV_PIX_FMT_BGR24;
339  }
340  break;
341  default:
342  return AVERROR_INVALIDDATA;
343  }
344 
345  if ((avctx->pix_fmt == AV_PIX_FMT_YUV422P || avctx->pix_fmt == AV_PIX_FMT_YUV420P) && avctx->width & 1) {
346  av_log(avctx, AV_LOG_ERROR, "width must be even for this colorspace\n");
347  return AVERROR_INVALIDDATA;
348  }
349  if (s->predictor == MEDIAN && avctx->pix_fmt == AV_PIX_FMT_YUV422P && avctx->width%4) {
350  av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 4 this colorspace and predictor\n");
351  return AVERROR_INVALIDDATA;
352  }
353  if (ff_huffyuv_alloc_temp(s)) {
355  return AVERROR(ENOMEM);
356  }
357 
358  return 0;
359 }
360 
362 {
363  HYuvContext *s = avctx->priv_data;
364  int i;
365 
366  if (ff_huffyuv_alloc_temp(s)) {
368  return AVERROR(ENOMEM);
369  }
370 
371  for (i = 0; i < 6; i++)
372  s->vlc[i].table = NULL;
373 
374  if (s->version == 2) {
375  if (read_huffman_tables(s, ((uint8_t*)avctx->extradata) + 4,
376  avctx->extradata_size) < 0)
377  return AVERROR_INVALIDDATA;
378  } else {
379  if (read_old_huffman_tables(s) < 0)
380  return AVERROR_INVALIDDATA;
381  }
382 
383  return 0;
384 }
385 
386 /* TODO instead of restarting the read when the code isn't in the first level
387  * of the joint table, jump into the 2nd level of the individual table. */
388 #define READ_2PIX(dst0, dst1, plane1){\
389  uint16_t code = get_vlc2(&s->gb, s->vlc[3+plane1].table, VLC_BITS, 1);\
390  if(code != 0xffff){\
391  dst0 = code>>8;\
392  dst1 = code;\
393  }else{\
394  dst0 = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);\
395  dst1 = get_vlc2(&s->gb, s->vlc[plane1].table, VLC_BITS, 3);\
396  }\
397 }
398 
400 {
401  int i;
402 
403  count /= 2;
404 
405  if (count >= (get_bits_left(&s->gb)) / (31 * 4)) {
406  for (i = 0; i < count && get_bits_left(&s->gb) > 0; i++) {
407  READ_2PIX(s->temp[0][2 * i ], s->temp[1][i], 1);
408  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
409  }
410  } else {
411  for (i = 0; i < count; i++) {
412  READ_2PIX(s->temp[0][2 * i ], s->temp[1][i], 1);
413  READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
414  }
415  }
416 }
417 
419 {
420  int i;
421 
422  count/=2;
423 
424  if (count >= (get_bits_left(&s->gb)) / (31 * 2)) {
425  for (i = 0; i < count && get_bits_left(&s->gb) > 0; i++) {
426  READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
427  }
428  } else {
429  for(i=0; i<count; i++){
430  READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
431  }
432  }
433 }
434 
436  int decorrelate, int alpha)
437 {
438  int i;
439  for (i = 0; i < count; i++) {
440  int code = get_vlc2(&s->gb, s->vlc[3].table, VLC_BITS, 1);
441  if (code != -1) {
442  *(uint32_t*)&s->temp[0][4 * i] = s->pix_bgr_map[code];
443  } else if(decorrelate) {
444  s->temp[0][4 * i + G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
445  s->temp[0][4 * i + B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) +
446  s->temp[0][4 * i + G];
447  s->temp[0][4 * i + R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) +
448  s->temp[0][4 * i + G];
449  } else {
450  s->temp[0][4 * i + B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
451  s->temp[0][4 * i + G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
452  s->temp[0][4 * i + R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
453  }
454  if (alpha)
455  s->temp[0][4 * i + A] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
456  }
457 }
458 
460 {
461  if (s->decorrelate) {
462  if (s->bitstream_bpp==24)
463  decode_bgr_1(s, count, 1, 0);
464  else
465  decode_bgr_1(s, count, 1, 1);
466  } else {
467  if (s->bitstream_bpp==24)
468  decode_bgr_1(s, count, 0, 0);
469  else
470  decode_bgr_1(s, count, 0, 1);
471  }
472 }
473 
474 static void draw_slice(HYuvContext *s, AVFrame *frame, int y)
475 {
476  int h, cy, i;
478 
479  if (s->avctx->draw_horiz_band==NULL)
480  return;
481 
482  h = y - s->last_slice_end;
483  y -= h;
484 
485  if (s->bitstream_bpp == 12) {
486  cy = y>>1;
487  } else {
488  cy = y;
489  }
490 
491  offset[0] = frame->linesize[0] * y;
492  offset[1] = frame->linesize[1] * cy;
493  offset[2] = frame->linesize[2] * cy;
494  for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
495  offset[i] = 0;
496  emms_c();
497 
498  s->avctx->draw_horiz_band(s->avctx, frame, offset, y, 3, h);
499 
500  s->last_slice_end = y + h;
501 }
502 
503 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
504  AVPacket *avpkt)
505 {
506  const uint8_t *buf = avpkt->data;
507  int buf_size = avpkt->size;
508  HYuvContext *s = avctx->priv_data;
509  const int width = s->width;
510  const int width2 = s->width>>1;
511  const int height = s->height;
512  int fake_ystride, fake_ustride, fake_vstride;
513  ThreadFrame frame = { .f = data };
514  AVFrame * const p = data;
515  int table_size = 0, ret;
516 
519  buf_size);
520  if (!s->bitstream_buffer)
521  return AVERROR(ENOMEM);
522 
523  s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer,
524  (const uint32_t*)buf, buf_size / 4);
525 
526  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
527  return ret;
528 
529  if (s->context) {
530  table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
531  if (table_size < 0)
532  return AVERROR_INVALIDDATA;
533  }
534 
535  if ((unsigned)(buf_size-table_size) >= INT_MAX / 8)
536  return AVERROR_INVALIDDATA;
537 
538  init_get_bits(&s->gb, s->bitstream_buffer+table_size,
539  (buf_size-table_size) * 8);
540 
541  fake_ystride = s->interlaced ? p->linesize[0] * 2 : p->linesize[0];
542  fake_ustride = s->interlaced ? p->linesize[1] * 2 : p->linesize[1];
543  fake_vstride = s->interlaced ? p->linesize[2] * 2 : p->linesize[2];
544 
545  s->last_slice_end = 0;
546 
547  if (s->bitstream_bpp < 24) {
548  int y, cy;
549  int lefty, leftu, leftv;
550  int lefttopy, lefttopu, lefttopv;
551 
552  if (s->yuy2) {
553  p->data[0][3] = get_bits(&s->gb, 8);
554  p->data[0][2] = get_bits(&s->gb, 8);
555  p->data[0][1] = get_bits(&s->gb, 8);
556  p->data[0][0] = get_bits(&s->gb, 8);
557 
558  av_log(avctx, AV_LOG_ERROR,
559  "YUY2 output is not implemented yet\n");
560  return AVERROR_PATCHWELCOME;
561  } else {
562 
563  leftv = p->data[2][0] = get_bits(&s->gb, 8);
564  lefty = p->data[0][1] = get_bits(&s->gb, 8);
565  leftu = p->data[1][0] = get_bits(&s->gb, 8);
566  p->data[0][0] = get_bits(&s->gb, 8);
567 
568  switch (s->predictor) {
569  case LEFT:
570  case PLANE:
571  decode_422_bitstream(s, width-2);
572  lefty = s->dsp.add_hfyu_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
573  if (!(s->flags&CODEC_FLAG_GRAY)) {
574  leftu = s->dsp.add_hfyu_left_prediction(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
575  leftv = s->dsp.add_hfyu_left_prediction(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
576  }
577 
578  for (cy = y = 1; y < s->height; y++, cy++) {
579  uint8_t *ydst, *udst, *vdst;
580 
581  if (s->bitstream_bpp == 12) {
582  decode_gray_bitstream(s, width);
583 
584  ydst = p->data[0] + p->linesize[0] * y;
585 
586  lefty = s->dsp.add_hfyu_left_prediction(ydst, s->temp[0], width, lefty);
587  if (s->predictor == PLANE) {
588  if (y > s->interlaced)
589  s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
590  }
591  y++;
592  if (y >= s->height) break;
593  }
594 
595  draw_slice(s, p, y);
596 
597  ydst = p->data[0] + p->linesize[0]*y;
598  udst = p->data[1] + p->linesize[1]*cy;
599  vdst = p->data[2] + p->linesize[2]*cy;
600 
601  decode_422_bitstream(s, width);
602  lefty = s->dsp.add_hfyu_left_prediction(ydst, s->temp[0], width, lefty);
603  if (!(s->flags & CODEC_FLAG_GRAY)) {
604  leftu= s->dsp.add_hfyu_left_prediction(udst, s->temp[1], width2, leftu);
605  leftv= s->dsp.add_hfyu_left_prediction(vdst, s->temp[2], width2, leftv);
606  }
607  if (s->predictor == PLANE) {
608  if (cy > s->interlaced) {
609  s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
610  if (!(s->flags & CODEC_FLAG_GRAY)) {
611  s->dsp.add_bytes(udst, udst - fake_ustride, width2);
612  s->dsp.add_bytes(vdst, vdst - fake_vstride, width2);
613  }
614  }
615  }
616  }
617  draw_slice(s, p, height);
618 
619  break;
620  case MEDIAN:
621  /* first line except first 2 pixels is left predicted */
622  decode_422_bitstream(s, width - 2);
623  lefty= s->dsp.add_hfyu_left_prediction(p->data[0] + 2, s->temp[0], width - 2, lefty);
624  if (!(s->flags & CODEC_FLAG_GRAY)) {
625  leftu = s->dsp.add_hfyu_left_prediction(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
626  leftv = s->dsp.add_hfyu_left_prediction(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
627  }
628 
629  cy = y = 1;
630 
631  /* second line is left predicted for interlaced case */
632  if (s->interlaced) {
633  decode_422_bitstream(s, width);
634  lefty = s->dsp.add_hfyu_left_prediction(p->data[0] + p->linesize[0], s->temp[0], width, lefty);
635  if (!(s->flags & CODEC_FLAG_GRAY)) {
636  leftu = s->dsp.add_hfyu_left_prediction(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
637  leftv = s->dsp.add_hfyu_left_prediction(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
638  }
639  y++; cy++;
640  }
641 
642  /* next 4 pixels are left predicted too */
643  decode_422_bitstream(s, 4);
644  lefty = s->dsp.add_hfyu_left_prediction(p->data[0] + fake_ystride, s->temp[0], 4, lefty);
645  if (!(s->flags&CODEC_FLAG_GRAY)) {
646  leftu = s->dsp.add_hfyu_left_prediction(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
647  leftv = s->dsp.add_hfyu_left_prediction(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
648  }
649 
650  /* next line except the first 4 pixels is median predicted */
651  lefttopy = p->data[0][3];
652  decode_422_bitstream(s, width - 4);
653  s->dsp.add_hfyu_median_prediction(p->data[0] + fake_ystride+4, p->data[0]+4, s->temp[0], width-4, &lefty, &lefttopy);
654  if (!(s->flags&CODEC_FLAG_GRAY)) {
655  lefttopu = p->data[1][1];
656  lefttopv = p->data[2][1];
657  s->dsp.add_hfyu_median_prediction(p->data[1] + fake_ustride+2, p->data[1] + 2, s->temp[1], width2 - 2, &leftu, &lefttopu);
658  s->dsp.add_hfyu_median_prediction(p->data[2] + fake_vstride+2, p->data[2] + 2, s->temp[2], width2 - 2, &leftv, &lefttopv);
659  }
660  y++; cy++;
661 
662  for (; y<height; y++, cy++) {
663  uint8_t *ydst, *udst, *vdst;
664 
665  if (s->bitstream_bpp == 12) {
666  while (2 * cy > y) {
667  decode_gray_bitstream(s, width);
668  ydst = p->data[0] + p->linesize[0] * y;
669  s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
670  y++;
671  }
672  if (y >= height) break;
673  }
674  draw_slice(s, p, y);
675 
676  decode_422_bitstream(s, width);
677 
678  ydst = p->data[0] + p->linesize[0] * y;
679  udst = p->data[1] + p->linesize[1] * cy;
680  vdst = p->data[2] + p->linesize[2] * cy;
681 
682  s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
683  if (!(s->flags & CODEC_FLAG_GRAY)) {
684  s->dsp.add_hfyu_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
685  s->dsp.add_hfyu_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
686  }
687  }
688 
689  draw_slice(s, p, height);
690  break;
691  }
692  }
693  } else {
694  int y;
695  int leftr, leftg, leftb, lefta;
696  const int last_line = (height - 1) * p->linesize[0];
697 
698  if (s->bitstream_bpp == 32) {
699  lefta = p->data[0][last_line+A] = get_bits(&s->gb, 8);
700  leftr = p->data[0][last_line+R] = get_bits(&s->gb, 8);
701  leftg = p->data[0][last_line+G] = get_bits(&s->gb, 8);
702  leftb = p->data[0][last_line+B] = get_bits(&s->gb, 8);
703  } else {
704  leftr = p->data[0][last_line+R] = get_bits(&s->gb, 8);
705  leftg = p->data[0][last_line+G] = get_bits(&s->gb, 8);
706  leftb = p->data[0][last_line+B] = get_bits(&s->gb, 8);
707  lefta = p->data[0][last_line+A] = 255;
708  skip_bits(&s->gb, 8);
709  }
710 
711  if (s->bgr32) {
712  switch (s->predictor) {
713  case LEFT:
714  case PLANE:
715  decode_bgr_bitstream(s, width - 1);
716  s->dsp.add_hfyu_left_prediction_bgr32(p->data[0] + last_line+4, s->temp[0], width - 1, &leftr, &leftg, &leftb, &lefta);
717 
718  for (y = s->height - 2; y >= 0; y--) { //Yes it is stored upside down.
719  decode_bgr_bitstream(s, width);
720 
721  s->dsp.add_hfyu_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb, &lefta);
722  if (s->predictor == PLANE) {
723  if (s->bitstream_bpp != 32) lefta = 0;
724  if ((y & s->interlaced) == 0 &&
725  y < s->height - 1 - s->interlaced) {
726  s->dsp.add_bytes(p->data[0] + p->linesize[0] * y,
727  p->data[0] + p->linesize[0] * y +
728  fake_ystride, fake_ystride);
729  }
730  }
731  }
732  // just 1 large slice as this is not possible in reverse order
733  draw_slice(s, p, height);
734  break;
735  default:
736  av_log(avctx, AV_LOG_ERROR,
737  "prediction type not supported!\n");
738  }
739  }else{
740  av_log(avctx, AV_LOG_ERROR,
741  "BGR24 output is not implemented yet\n");
742  return AVERROR_PATCHWELCOME;
743  }
744  }
745  emms_c();
746 
747  *got_frame = 1;
748 
749  return (get_bits_count(&s->gb) + 31) / 32 * 4 + table_size;
750 }
751 
753 {
754  HYuvContext *s = avctx->priv_data;
755  int i;
756 
759 
760  for (i = 0; i < 6; i++) {
761  ff_free_vlc(&s->vlc[i]);
762  }
763 
764  return 0;
765 }
766 
767 #if CONFIG_HUFFYUV_DECODER
768 AVCodec ff_huffyuv_decoder = {
769  .name = "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  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
780 };
781 #endif
782 
783 #if CONFIG_FFVHUFF_DECODER
784 AVCodec ff_ffvhuff_decoder = {
785  .name = "ffvhuff",
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  .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
796 };
797 #endif