4 * Copyright (c) 2002-2014 Michael Niedermayer <michaelni@gmx.at>
6 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
9 * This file is part of FFmpeg.
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.
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.
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
25 * yuva, gray, 4:4:4, 4:1:1, 4:1:0 and >8 bit per sample support sponsored by NOA
33 #define UNCHECKED_BITSTREAM_READER 1
38 #include "huffyuvdsp.h"
40 #include "libavutil/pixdesc.h"
42 #define classic_shift_luma_table_size 42
43 static const unsigned char classic_shift_luma[classic_shift_luma_table_size + FF_INPUT_BUFFER_PADDING_SIZE] = {
44 34, 36, 35, 69, 135, 232, 9, 16, 10, 24, 11, 23, 12, 16, 13, 10,
45 14, 8, 15, 8, 16, 8, 17, 20, 16, 10, 207, 206, 205, 236, 11, 8,
46 10, 21, 9, 23, 8, 8, 199, 70, 69, 68, 0,
50 #define classic_shift_chroma_table_size 59
51 static const unsigned char classic_shift_chroma[classic_shift_chroma_table_size + FF_INPUT_BUFFER_PADDING_SIZE] = {
52 66, 36, 37, 38, 39, 40, 41, 75, 76, 77, 110, 239, 144, 81, 82, 83,
53 84, 85, 118, 183, 56, 57, 88, 89, 56, 89, 154, 57, 58, 57, 26, 141,
54 57, 56, 58, 57, 58, 57, 184, 119, 214, 245, 116, 83, 82, 49, 80, 79,
55 78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, 0,
59 static const unsigned char classic_add_luma[256] = {
60 3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
61 73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
62 68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
63 35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
64 37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
65 35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
66 27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
67 15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
68 12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
69 12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
70 18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
71 28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
72 28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
73 62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
74 54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
75 46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
78 static const unsigned char classic_add_chroma[256] = {
79 3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
80 7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
81 11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
82 43, 45, 76, 81, 46, 82, 75, 55, 56, 144, 58, 80, 60, 74, 147, 63,
83 143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
84 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
85 17, 14, 5, 6, 100, 54, 47, 50, 51, 53, 106, 107, 108, 109, 110, 111,
86 112, 113, 114, 115, 4, 117, 118, 92, 94, 121, 122, 3, 124, 103, 2, 1,
87 0, 129, 130, 131, 120, 119, 126, 125, 136, 137, 138, 139, 140, 141, 142, 134,
88 135, 132, 133, 104, 64, 101, 62, 57, 102, 95, 93, 59, 61, 28, 97, 96,
89 52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
90 19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
91 7, 128, 127, 105, 123, 116, 35, 34, 33, 145, 31, 79, 42, 146, 78, 26,
92 83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
93 14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
94 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
97 static int read_len_table(uint8_t *dst, GetBitContext *gb, int n)
101 for (i = 0; i < n;) {
102 repeat = get_bits(gb, 3);
103 val = get_bits(gb, 5);
105 repeat = get_bits(gb, 8);
106 if (i + repeat > n || get_bits_left(gb) < 0) {
107 av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n");
108 return AVERROR_INVALIDDATA;
116 static int generate_joint_tables(HYuvContext *s)
118 uint16_t symbols[1 << VLC_BITS];
119 uint16_t bits[1 << VLC_BITS];
120 uint8_t len[1 << VLC_BITS];
123 if (s->bitstream_bpp < 24 || s->version > 2) {
125 for (p = 0; p < 4; p++) {
126 int p0 = s->version > 2 ? p : 0;
127 for (i = y = 0; y < s->vlc_n; y++) {
128 int len0 = s->len[p0][y];
129 int limit = VLC_BITS - len0;
130 if (limit <= 0 || !len0)
132 if ((sign_extend(y, 8) & (s->vlc_n-1)) != y)
134 for (u = 0; u < s->vlc_n; u++) {
135 int len1 = s->len[p][u];
136 if (len1 > limit || !len1)
138 if ((sign_extend(u, 8) & (s->vlc_n-1)) != u)
140 av_assert0(i < (1 << VLC_BITS));
141 len[i] = len0 + len1;
142 bits[i] = (s->bits[p0][y] << len1) + s->bits[p][u];
143 symbols[i] = (y << 8) + (u & 0xFF);
147 ff_free_vlc(&s->vlc[4 + p]);
148 if ((ret = ff_init_vlc_sparse(&s->vlc[4 + p], VLC_BITS, i, len, 1, 1,
149 bits, 2, 2, symbols, 2, 2, 0)) < 0)
153 uint8_t (*map)[4] = (uint8_t(*)[4]) s->pix_bgr_map;
154 int i, b, g, r, code;
155 int p0 = s->decorrelate;
156 int p1 = !s->decorrelate;
157 /* Restrict the range to +/-16 because that's pretty much guaranteed
158 * to cover all the combinations that fit in 11 bits total, and it
159 * does not matter if we miss a few rare codes. */
160 for (i = 0, g = -16; g < 16; g++) {
161 int len0 = s->len[p0][g & 255];
162 int limit0 = VLC_BITS - len0;
163 if (limit0 < 2 || !len0)
165 for (b = -16; b < 16; b++) {
166 int len1 = s->len[p1][b & 255];
167 int limit1 = limit0 - len1;
168 if (limit1 < 1 || !len1)
170 code = (s->bits[p0][g & 255] << len1) + s->bits[p1][b & 255];
171 for (r = -16; r < 16; r++) {
172 int len2 = s->len[2][r & 255];
173 if (len2 > limit1 || !len2)
175 av_assert0(i < (1 << VLC_BITS));
176 len[i] = len0 + len1 + len2;
177 bits[i] = (code << len2) + s->bits[2][r & 255];
178 if (s->decorrelate) {
191 ff_free_vlc(&s->vlc[4]);
192 if ((ret = init_vlc(&s->vlc[4], VLC_BITS, i, len, 1, 1,
199 static int read_huffman_tables(HYuvContext *s, const uint8_t *src, int length)
205 if ((ret = init_get_bits(&gb, src, length * 8)) < 0)
209 count = 1 + s->alpha + 2*s->chroma;
211 for (i = 0; i < count; i++) {
212 if ((ret = read_len_table(s->len[i], &gb, s->vlc_n)) < 0)
214 if ((ret = ff_huffyuv_generate_bits_table(s->bits[i], s->len[i], s->vlc_n)) < 0)
216 ff_free_vlc(&s->vlc[i]);
217 if ((ret = init_vlc(&s->vlc[i], VLC_BITS, s->vlc_n, s->len[i], 1, 1,
218 s->bits[i], 4, 4, 0)) < 0)
222 if ((ret = generate_joint_tables(s)) < 0)
225 return (get_bits_count(&gb) + 7) / 8;
228 static int read_old_huffman_tables(HYuvContext *s)
233 init_get_bits(&gb, classic_shift_luma,
234 classic_shift_luma_table_size * 8);
235 if ((ret = read_len_table(s->len[0], &gb, 256)) < 0)
238 init_get_bits(&gb, classic_shift_chroma,
239 classic_shift_chroma_table_size * 8);
240 if ((ret = read_len_table(s->len[1], &gb, 256)) < 0)
243 for (i = 0; i < 256; i++)
244 s->bits[0][i] = classic_add_luma[i];
245 for (i = 0; i < 256; i++)
246 s->bits[1][i] = classic_add_chroma[i];
248 if (s->bitstream_bpp >= 24) {
249 memcpy(s->bits[1], s->bits[0], 256 * sizeof(uint32_t));
250 memcpy(s->len[1], s->len[0], 256 * sizeof(uint8_t));
252 memcpy(s->bits[2], s->bits[1], 256 * sizeof(uint32_t));
253 memcpy(s->len[2], s->len[1], 256 * sizeof(uint8_t));
255 for (i = 0; i < 4; i++) {
256 ff_free_vlc(&s->vlc[i]);
257 if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1,
258 s->bits[i], 4, 4, 0)) < 0)
262 if ((ret = generate_joint_tables(s)) < 0)
268 static av_cold int decode_init(AVCodecContext *avctx)
270 HYuvContext *s = avctx->priv_data;
273 ff_huffyuvdsp_init(&s->hdsp);
274 memset(s->vlc, 0, 4 * sizeof(VLC));
276 s->interlaced = avctx->height > 288;
279 if (avctx->extradata_size) {
280 if ((avctx->bits_per_coded_sample & 7) &&
281 avctx->bits_per_coded_sample != 12)
282 s->version = 1; // do such files exist at all?
283 else if (avctx->extradata_size > 3 && avctx->extradata[3] == 0)
292 s->vlc_n = FFMIN(s->n, MAX_VLC_N);
294 if (s->version >= 2) {
295 int method, interlace;
297 if (avctx->extradata_size < 4)
298 return AVERROR_INVALIDDATA;
300 method = avctx->extradata[0];
301 s->decorrelate = method & 64 ? 1 : 0;
302 s->predictor = method & 63;
303 if (s->version == 2) {
304 s->bitstream_bpp = avctx->extradata[1];
305 if (s->bitstream_bpp == 0)
306 s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
308 s->bps = (avctx->extradata[1] >> 4) + 1;
310 s->vlc_n = FFMIN(s->n, MAX_VLC_N);
311 s->chroma_h_shift = avctx->extradata[1] & 3;
312 s->chroma_v_shift = (avctx->extradata[1] >> 2) & 3;
313 s->yuv = !!(avctx->extradata[2] & 1);
314 s->chroma= !!(avctx->extradata[2] & 3);
315 s->alpha = !!(avctx->extradata[2] & 4);
317 interlace = (avctx->extradata[2] & 0x30) >> 4;
318 s->interlaced = (interlace == 1) ? 1 : (interlace == 2) ? 0 : s->interlaced;
319 s->context = avctx->extradata[2] & 0x40 ? 1 : 0;
321 if ((ret = read_huffman_tables(s, avctx->extradata + 4,
322 avctx->extradata_size - 4)) < 0)
325 switch (avctx->bits_per_coded_sample & 7) {
335 s->predictor = PLANE;
336 s->decorrelate = avctx->bits_per_coded_sample >= 24;
339 s->predictor = MEDIAN;
343 s->predictor = LEFT; // OLD
347 s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
350 if ((ret = read_old_huffman_tables(s)) < 0)
354 if (s->version <= 2) {
355 switch (s->bitstream_bpp) {
357 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
362 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
364 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
369 avctx->pix_fmt = AV_PIX_FMT_0RGB32;
371 avctx->pix_fmt = AV_PIX_FMT_BGR24;
374 av_assert0(s->bgr32);
375 avctx->pix_fmt = AV_PIX_FMT_RGB32;
379 return AVERROR_INVALIDDATA;
381 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
385 switch ( (s->chroma<<10) | (s->yuv<<9) | (s->alpha<<8) | ((s->bps-1)<<4) | s->chroma_h_shift | (s->chroma_v_shift<<2)) {
387 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
390 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
393 avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
396 avctx->pix_fmt = AV_PIX_FMT_GBRP;
399 avctx->pix_fmt = AV_PIX_FMT_GBRP9;
402 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
405 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
408 avctx->pix_fmt = AV_PIX_FMT_GBRP14;
411 avctx->pix_fmt = AV_PIX_FMT_GBRP16;
414 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
417 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
420 avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
423 avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
426 avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
429 avctx->pix_fmt = AV_PIX_FMT_YUV444P14;
432 avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
435 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
438 avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
441 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
444 avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
447 avctx->pix_fmt = AV_PIX_FMT_YUV422P14;
450 avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
453 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
456 avctx->pix_fmt = AV_PIX_FMT_YUV440P;
459 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
462 avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
465 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
468 avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
471 avctx->pix_fmt = AV_PIX_FMT_YUV420P14;
474 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
477 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
480 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
483 avctx->pix_fmt = AV_PIX_FMT_YUVA444P9;
486 avctx->pix_fmt = AV_PIX_FMT_YUVA444P10;
489 avctx->pix_fmt = AV_PIX_FMT_YUVA444P16;
492 avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
495 avctx->pix_fmt = AV_PIX_FMT_YUVA422P9;
498 avctx->pix_fmt = AV_PIX_FMT_YUVA422P10;
501 avctx->pix_fmt = AV_PIX_FMT_YUVA422P16;
504 avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
507 avctx->pix_fmt = AV_PIX_FMT_YUVA420P9;
510 avctx->pix_fmt = AV_PIX_FMT_YUVA420P10;
513 avctx->pix_fmt = AV_PIX_FMT_YUVA420P16;
516 return AVERROR_INVALIDDATA;
520 ff_huffyuv_common_init(avctx);
522 if ((avctx->pix_fmt == AV_PIX_FMT_YUV422P || avctx->pix_fmt == AV_PIX_FMT_YUV420P) && avctx->width & 1) {
523 av_log(avctx, AV_LOG_ERROR, "width must be even for this colorspace\n");
524 return AVERROR_INVALIDDATA;
526 if (s->predictor == MEDIAN && avctx->pix_fmt == AV_PIX_FMT_YUV422P &&
528 av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 4 "
529 "for this combination of colorspace and predictor type.\n");
530 return AVERROR_INVALIDDATA;
533 if ((ret = ff_huffyuv_alloc_temp(s)) < 0) {
534 ff_huffyuv_common_end(s);
541 static av_cold int decode_init_thread_copy(AVCodecContext *avctx)
543 HYuvContext *s = avctx->priv_data;
546 if ((ret = ff_huffyuv_alloc_temp(s)) < 0) {
547 ff_huffyuv_common_end(s);
551 for (i = 0; i < 8; i++)
552 s->vlc[i].table = NULL;
554 if (s->version >= 2) {
555 if ((ret = read_huffman_tables(s, avctx->extradata + 4,
556 avctx->extradata_size)) < 0)
559 if ((ret = read_old_huffman_tables(s)) < 0)
566 /** Subset of GET_VLC for use in hand-roller VLC code */
567 #define VLC_INTERN(dst, table, gb, name, bits, max_depth) \
568 code = table[index][0]; \
569 n = table[index][1]; \
570 if (max_depth > 1 && n < 0) { \
571 LAST_SKIP_BITS(name, gb, bits); \
572 UPDATE_CACHE(name, gb); \
575 index = SHOW_UBITS(name, gb, nb_bits) + code; \
576 code = table[index][0]; \
577 n = table[index][1]; \
578 if (max_depth > 2 && n < 0) { \
579 LAST_SKIP_BITS(name, gb, nb_bits); \
580 UPDATE_CACHE(name, gb); \
583 index = SHOW_UBITS(name, gb, nb_bits) + code; \
584 code = table[index][0]; \
585 n = table[index][1]; \
589 LAST_SKIP_BITS(name, gb, n)
592 #define GET_VLC_DUAL(dst0, dst1, name, gb, dtable, table1, table2, \
593 bits, max_depth, OP) \
595 unsigned int index = SHOW_UBITS(name, gb, bits); \
596 int code, n = dtable[index][1]; \
600 VLC_INTERN(dst0, table1, gb, name, bits, max_depth); \
602 UPDATE_CACHE(re, gb); \
603 index = SHOW_UBITS(name, gb, bits); \
604 VLC_INTERN(dst1, table2, gb, name, bits, max_depth); \
606 code = dtable[index][0]; \
607 OP(dst0, dst1, code); \
608 LAST_SKIP_BITS(name, gb, n); \
612 #define OP8bits(dst0, dst1, code) dst0 = code>>8; dst1 = code
614 #define READ_2PIX(dst0, dst1, plane1) \
615 UPDATE_CACHE(re, &s->gb); \
616 GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane1].table, \
617 s->vlc[0].table, s->vlc[plane1].table, VLC_BITS, 3, OP8bits)
619 static void decode_422_bitstream(HYuvContext *s, int count)
622 OPEN_READER(re, &s->gb);
625 icount = get_bits_left(&s->gb) / (32 * 4);
626 if (count >= icount) {
627 for (i = 0; i < icount; i++) {
628 READ_2PIX(s->temp[0][2 * i], s->temp[1][i], 1);
629 READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
631 for (; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
632 READ_2PIX(s->temp[0][2 * i ], s->temp[1][i], 1);
633 if (BITS_LEFT(re, &s->gb) <= 0) break;
634 READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
636 for (; i < count; i++)
637 s->temp[0][2 * i ] = s->temp[1][i] =
638 s->temp[0][2 * i + 1] = s->temp[2][i] = 0;
640 for (i = 0; i < count; i++) {
641 READ_2PIX(s->temp[0][2 * i], s->temp[1][i], 1);
642 READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
645 CLOSE_READER(re, &s->gb);
648 #define READ_2PIX_PLANE(dst0, dst1, plane, OP) \
649 UPDATE_CACHE(re, &s->gb); \
650 GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane].table, \
651 s->vlc[plane].table, s->vlc[plane].table, VLC_BITS, 3, OP)
653 #define OP14bits(dst0, dst1, code) dst0 = code>>8; dst1 = sign_extend(code, 8)
655 /* TODO instead of restarting the read when the code isn't in the first level
656 * of the joint table, jump into the 2nd level of the individual table. */
657 #define READ_2PIX_PLANE16(dst0, dst1, plane){\
658 dst0 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
659 dst0 += get_bits(&s->gb, 2);\
660 dst1 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
661 dst1 += get_bits(&s->gb, 2);\
663 static void decode_plane_bitstream(HYuvContext *s, int width, int plane)
665 int i, count = width/2;
668 OPEN_READER(re, &s->gb);
669 if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
670 for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
671 READ_2PIX_PLANE(s->temp[0][2 * i], s->temp[0][2 * i + 1], plane, OP8bits);
674 for(i=0; i<count; i++){
675 READ_2PIX_PLANE(s->temp[0][2 * i], s->temp[0][2 * i + 1], plane, OP8bits);
678 if( width&1 && BITS_LEFT(re, &s->gb)>0 ) {
680 int nb_bits, code, n;
681 UPDATE_CACHE(re, &s->gb);
682 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
683 VLC_INTERN(s->temp[0][width-1], s->vlc[plane].table,
684 &s->gb, re, VLC_BITS, 3);
686 CLOSE_READER(re, &s->gb);
687 } else if (s->bps <= 14) {
688 OPEN_READER(re, &s->gb);
689 if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
690 for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
691 READ_2PIX_PLANE(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane, OP14bits);
694 for(i=0; i<count; i++){
695 READ_2PIX_PLANE(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane, OP14bits);
698 if( width&1 && BITS_LEFT(re, &s->gb)>0 ) {
700 int nb_bits, code, n;
701 UPDATE_CACHE(re, &s->gb);
702 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
703 VLC_INTERN(s->temp16[0][width-1], s->vlc[plane].table,
704 &s->gb, re, VLC_BITS, 3);
706 CLOSE_READER(re, &s->gb);
708 if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
709 for (i = 0; i < count && get_bits_left(&s->gb) > 0; i++) {
710 READ_2PIX_PLANE16(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane);
713 for(i=0; i<count; i++){
714 READ_2PIX_PLANE16(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane);
717 if( width&1 && get_bits_left(&s->gb)>0 ) {
718 int dst = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;
719 s->temp16[0][width-1] = dst + get_bits(&s->gb, 2);
724 static void decode_gray_bitstream(HYuvContext *s, int count)
727 OPEN_READER(re, &s->gb);
730 if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
731 for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
732 READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
735 for (i = 0; i < count; i++) {
736 READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
739 CLOSE_READER(re, &s->gb);
742 static av_always_inline void decode_bgr_1(HYuvContext *s, int count,
743 int decorrelate, int alpha)
746 OPEN_READER(re, &s->gb);
748 for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
750 int code, n, nb_bits;
752 UPDATE_CACHE(re, &s->gb);
753 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
754 n = s->vlc[4].table[index][1];
757 code = s->vlc[4].table[index][0];
758 *(uint32_t *) &s->temp[0][4 * i] = s->pix_bgr_map[code];
759 LAST_SKIP_BITS(re, &s->gb, n);
762 VLC_INTERN(s->temp[0][4 * i + G], s->vlc[1].table,
763 &s->gb, re, VLC_BITS, 3);
765 UPDATE_CACHE(re, &s->gb);
766 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
767 VLC_INTERN(code, s->vlc[0].table, &s->gb, re, VLC_BITS, 3);
768 s->temp[0][4 * i + B] = code + s->temp[0][4 * i + G];
770 UPDATE_CACHE(re, &s->gb);
771 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
772 VLC_INTERN(code, s->vlc[2].table, &s->gb, re, VLC_BITS, 3);
773 s->temp[0][4 * i + R] = code + s->temp[0][4 * i + G];
775 VLC_INTERN(s->temp[0][4 * i + B], s->vlc[0].table,
776 &s->gb, re, VLC_BITS, 3);
778 UPDATE_CACHE(re, &s->gb);
779 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
780 VLC_INTERN(s->temp[0][4 * i + G], s->vlc[1].table,
781 &s->gb, re, VLC_BITS, 3);
783 UPDATE_CACHE(re, &s->gb);
784 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
785 VLC_INTERN(s->temp[0][4 * i + R], s->vlc[2].table,
786 &s->gb, re, VLC_BITS, 3);
790 UPDATE_CACHE(re, &s->gb);
791 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
792 VLC_INTERN(s->temp[0][4 * i + A], s->vlc[2].table,
793 &s->gb, re, VLC_BITS, 3);
795 s->temp[0][4 * i + A] = 0;
797 CLOSE_READER(re, &s->gb);
800 static void decode_bgr_bitstream(HYuvContext *s, int count)
802 if (s->decorrelate) {
803 if (s->bitstream_bpp == 24)
804 decode_bgr_1(s, count, 1, 0);
806 decode_bgr_1(s, count, 1, 1);
808 if (s->bitstream_bpp == 24)
809 decode_bgr_1(s, count, 0, 0);
811 decode_bgr_1(s, count, 0, 1);
815 static void draw_slice(HYuvContext *s, AVFrame *frame, int y)
818 int offset[AV_NUM_DATA_POINTERS];
820 if (!s->avctx->draw_horiz_band)
823 h = y - s->last_slice_end;
826 if (s->bitstream_bpp == 12)
831 offset[0] = frame->linesize[0] * y;
832 offset[1] = frame->linesize[1] * cy;
833 offset[2] = frame->linesize[2] * cy;
834 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
838 s->avctx->draw_horiz_band(s->avctx, frame, offset, y, 3, h);
840 s->last_slice_end = y + h;
843 static int left_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int acc)
846 return s->hdsp.add_hfyu_left_pred(dst, src, w, acc);
848 return s->llviddsp.add_hfyu_left_pred_int16(( uint16_t *)dst, (const uint16_t *)src, s->n-1, w, acc);
852 static void add_bytes(HYuvContext *s, uint8_t *dst, uint8_t *src, int w)
855 s->hdsp.add_bytes(dst, src, w);
857 s->llviddsp.add_int16((uint16_t*)dst, (const uint16_t*)src, s->n - 1, w);
861 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)
864 s->hdsp.add_hfyu_median_pred(dst, src, diff, w, left, left_top);
866 s->llviddsp.add_hfyu_median_pred_int16((uint16_t *)dst, (const uint16_t *)src, (const uint16_t *)diff, s->n-1, w, left, left_top);
869 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
872 const uint8_t *buf = avpkt->data;
873 int buf_size = avpkt->size;
874 HYuvContext *s = avctx->priv_data;
875 const int width = s->width;
876 const int width2 = s->width >> 1;
877 const int height = s->height;
878 int fake_ystride, fake_ustride, fake_vstride;
879 ThreadFrame frame = { .f = data };
880 AVFrame *const p = data;
881 int table_size = 0, ret;
883 av_fast_padded_malloc(&s->bitstream_buffer,
884 &s->bitstream_buffer_size,
886 if (!s->bitstream_buffer)
887 return AVERROR(ENOMEM);
889 s->bdsp.bswap_buf((uint32_t *) s->bitstream_buffer,
890 (const uint32_t *) buf, buf_size / 4);
892 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
896 table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
901 if ((unsigned) (buf_size - table_size) >= INT_MAX / 8)
902 return AVERROR_INVALIDDATA;
904 if ((ret = init_get_bits(&s->gb, s->bitstream_buffer + table_size,
905 (buf_size - table_size) * 8)) < 0)
908 fake_ystride = s->interlaced ? p->linesize[0] * 2 : p->linesize[0];
909 fake_ustride = s->interlaced ? p->linesize[1] * 2 : p->linesize[1];
910 fake_vstride = s->interlaced ? p->linesize[2] * 2 : p->linesize[2];
912 s->last_slice_end = 0;
914 if (s->version > 2) {
916 for(plane = 0; plane < 1 + 2*s->chroma + s->alpha; plane++) {
917 int left, lefttop, y;
920 int fake_stride = fake_ystride;
922 if (s->chroma && (plane == 1 || plane == 2)) {
923 w >>= s->chroma_h_shift;
924 h >>= s->chroma_v_shift;
925 fake_stride = plane == 1 ? fake_ustride : fake_vstride;
928 switch (s->predictor) {
931 decode_plane_bitstream(s, w, plane);
932 left = left_prediction(s, p->data[plane], s->temp[0], w, 0);
934 for (y = 1; y < h; y++) {
935 uint8_t *dst = p->data[plane] + p->linesize[plane]*y;
937 decode_plane_bitstream(s, w, plane);
938 left = left_prediction(s, dst, s->temp[0], w, left);
939 if (s->predictor == PLANE) {
940 if (y > s->interlaced) {
941 add_bytes(s, dst, dst - fake_stride, w);
948 decode_plane_bitstream(s, w, plane);
949 left= left_prediction(s, p->data[plane], s->temp[0], w, 0);
953 /* second line is left predicted for interlaced case */
955 decode_plane_bitstream(s, w, plane);
956 left = left_prediction(s, p->data[plane] + p->linesize[plane], s->temp[0], w, left);
960 lefttop = p->data[plane][0];
961 decode_plane_bitstream(s, w, plane);
962 add_median_prediction(s, p->data[plane] + fake_stride, p->data[plane], s->temp[0], w, &left, &lefttop);
968 decode_plane_bitstream(s, w, plane);
970 dst = p->data[plane] + p->linesize[plane] * y;
972 add_median_prediction(s, dst, dst - fake_stride, s->temp[0], w, &left, &lefttop);
978 draw_slice(s, p, height);
979 } else if (s->bitstream_bpp < 24) {
981 int lefty, leftu, leftv;
982 int lefttopy, lefttopu, lefttopv;
985 p->data[0][3] = get_bits(&s->gb, 8);
986 p->data[0][2] = get_bits(&s->gb, 8);
987 p->data[0][1] = get_bits(&s->gb, 8);
988 p->data[0][0] = get_bits(&s->gb, 8);
990 av_log(avctx, AV_LOG_ERROR,
991 "YUY2 output is not implemented yet\n");
992 return AVERROR_PATCHWELCOME;
995 p->data[2][0] = get_bits(&s->gb, 8);
997 p->data[0][1] = get_bits(&s->gb, 8);
999 p->data[1][0] = get_bits(&s->gb, 8);
1000 p->data[0][0] = get_bits(&s->gb, 8);
1002 switch (s->predictor) {
1005 decode_422_bitstream(s, width - 2);
1006 lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + 2, s->temp[0],
1008 if (!(s->flags & CODEC_FLAG_GRAY)) {
1009 leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
1010 leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
1013 for (cy = y = 1; y < s->height; y++, cy++) {
1014 uint8_t *ydst, *udst, *vdst;
1016 if (s->bitstream_bpp == 12) {
1017 decode_gray_bitstream(s, width);
1019 ydst = p->data[0] + p->linesize[0] * y;
1021 lefty = s->hdsp.add_hfyu_left_pred(ydst, s->temp[0],
1023 if (s->predictor == PLANE) {
1024 if (y > s->interlaced)
1025 s->hdsp.add_bytes(ydst, ydst - fake_ystride, width);
1032 draw_slice(s, p, y);
1034 ydst = p->data[0] + p->linesize[0] * y;
1035 udst = p->data[1] + p->linesize[1] * cy;
1036 vdst = p->data[2] + p->linesize[2] * cy;
1038 decode_422_bitstream(s, width);
1039 lefty = s->hdsp.add_hfyu_left_pred(ydst, s->temp[0],
1041 if (!(s->flags & CODEC_FLAG_GRAY)) {
1042 leftu = s->hdsp.add_hfyu_left_pred(udst, s->temp[1], width2, leftu);
1043 leftv = s->hdsp.add_hfyu_left_pred(vdst, s->temp[2], width2, leftv);
1045 if (s->predictor == PLANE) {
1046 if (cy > s->interlaced) {
1047 s->hdsp.add_bytes(ydst, ydst - fake_ystride, width);
1048 if (!(s->flags & CODEC_FLAG_GRAY)) {
1049 s->hdsp.add_bytes(udst, udst - fake_ustride, width2);
1050 s->hdsp.add_bytes(vdst, vdst - fake_vstride, width2);
1055 draw_slice(s, p, height);
1059 /* first line except first 2 pixels is left predicted */
1060 decode_422_bitstream(s, width - 2);
1061 lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + 2, s->temp[0],
1063 if (!(s->flags & CODEC_FLAG_GRAY)) {
1064 leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
1065 leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
1070 /* second line is left predicted for interlaced case */
1071 if (s->interlaced) {
1072 decode_422_bitstream(s, width);
1073 lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + p->linesize[0],
1074 s->temp[0], width, lefty);
1075 if (!(s->flags & CODEC_FLAG_GRAY)) {
1076 leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
1077 leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
1083 /* next 4 pixels are left predicted too */
1084 decode_422_bitstream(s, 4);
1085 lefty = s->hdsp.add_hfyu_left_pred(p->data[0] + fake_ystride,
1086 s->temp[0], 4, lefty);
1087 if (!(s->flags & CODEC_FLAG_GRAY)) {
1088 leftu = s->hdsp.add_hfyu_left_pred(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
1089 leftv = s->hdsp.add_hfyu_left_pred(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
1092 /* next line except the first 4 pixels is median predicted */
1093 lefttopy = p->data[0][3];
1094 decode_422_bitstream(s, width - 4);
1095 s->hdsp.add_hfyu_median_pred(p->data[0] + fake_ystride + 4,
1096 p->data[0] + 4, s->temp[0],
1097 width - 4, &lefty, &lefttopy);
1098 if (!(s->flags & CODEC_FLAG_GRAY)) {
1099 lefttopu = p->data[1][1];
1100 lefttopv = p->data[2][1];
1101 s->hdsp.add_hfyu_median_pred(p->data[1] + fake_ustride + 2, p->data[1] + 2, s->temp[1], width2 - 2, &leftu, &lefttopu);
1102 s->hdsp.add_hfyu_median_pred(p->data[2] + fake_vstride + 2, p->data[2] + 2, s->temp[2], width2 - 2, &leftv, &lefttopv);
1107 for (; y < height; y++, cy++) {
1108 uint8_t *ydst, *udst, *vdst;
1110 if (s->bitstream_bpp == 12) {
1111 while (2 * cy > y) {
1112 decode_gray_bitstream(s, width);
1113 ydst = p->data[0] + p->linesize[0] * y;
1114 s->hdsp.add_hfyu_median_pred(ydst, ydst - fake_ystride,
1122 draw_slice(s, p, y);
1124 decode_422_bitstream(s, width);
1126 ydst = p->data[0] + p->linesize[0] * y;
1127 udst = p->data[1] + p->linesize[1] * cy;
1128 vdst = p->data[2] + p->linesize[2] * cy;
1130 s->hdsp.add_hfyu_median_pred(ydst, ydst - fake_ystride,
1133 if (!(s->flags & CODEC_FLAG_GRAY)) {
1134 s->hdsp.add_hfyu_median_pred(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
1135 s->hdsp.add_hfyu_median_pred(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
1139 draw_slice(s, p, height);
1146 const int last_line = (height - 1) * p->linesize[0];
1148 if (s->bitstream_bpp == 32) {
1149 left[A] = p->data[0][last_line + A] = get_bits(&s->gb, 8);
1150 left[R] = p->data[0][last_line + R] = get_bits(&s->gb, 8);
1151 left[G] = p->data[0][last_line + G] = get_bits(&s->gb, 8);
1152 left[B] = p->data[0][last_line + B] = get_bits(&s->gb, 8);
1154 left[R] = p->data[0][last_line + R] = get_bits(&s->gb, 8);
1155 left[G] = p->data[0][last_line + G] = get_bits(&s->gb, 8);
1156 left[B] = p->data[0][last_line + B] = get_bits(&s->gb, 8);
1157 left[A] = p->data[0][last_line + A] = 255;
1158 skip_bits(&s->gb, 8);
1162 switch (s->predictor) {
1165 decode_bgr_bitstream(s, width - 1);
1166 s->hdsp.add_hfyu_left_pred_bgr32(p->data[0] + last_line + 4,
1167 s->temp[0], width - 1, left);
1169 for (y = s->height - 2; y >= 0; y--) { // Yes it is stored upside down.
1170 decode_bgr_bitstream(s, width);
1172 s->hdsp.add_hfyu_left_pred_bgr32(p->data[0] + p->linesize[0] * y,
1173 s->temp[0], width, left);
1174 if (s->predictor == PLANE) {
1175 if (s->bitstream_bpp != 32)
1177 if ((y & s->interlaced) == 0 &&
1178 y < s->height - 1 - s->interlaced) {
1179 s->hdsp.add_bytes(p->data[0] + p->linesize[0] * y,
1180 p->data[0] + p->linesize[0] * y +
1181 fake_ystride, fake_ystride);
1185 // just 1 large slice as this is not possible in reverse order
1186 draw_slice(s, p, height);
1189 av_log(avctx, AV_LOG_ERROR,
1190 "prediction type not supported!\n");
1193 av_log(avctx, AV_LOG_ERROR,
1194 "BGR24 output is not implemented yet\n");
1195 return AVERROR_PATCHWELCOME;
1202 return (get_bits_count(&s->gb) + 31) / 32 * 4 + table_size;
1205 static av_cold int decode_end(AVCodecContext *avctx)
1207 HYuvContext *s = avctx->priv_data;
1210 ff_huffyuv_common_end(s);
1211 av_freep(&s->bitstream_buffer);
1213 for (i = 0; i < 8; i++)
1214 ff_free_vlc(&s->vlc[i]);
1219 AVCodec ff_huffyuv_decoder = {
1221 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
1222 .type = AVMEDIA_TYPE_VIDEO,
1223 .id = AV_CODEC_ID_HUFFYUV,
1224 .priv_data_size = sizeof(HYuvContext),
1225 .init = decode_init,
1226 .close = decode_end,
1227 .decode = decode_frame,
1228 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
1229 CODEC_CAP_FRAME_THREADS,
1230 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1233 #if CONFIG_FFVHUFF_DECODER
1234 AVCodec ff_ffvhuff_decoder = {
1236 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
1237 .type = AVMEDIA_TYPE_VIDEO,
1238 .id = AV_CODEC_ID_FFVHUFF,
1239 .priv_data_size = sizeof(HYuvContext),
1240 .init = decode_init,
1241 .close = decode_end,
1242 .decode = decode_frame,
1243 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
1244 CODEC_CAP_FRAME_THREADS,
1245 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1247 #endif /* CONFIG_FFVHUFF_DECODER */