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