33 #define UNCHECKED_BITSTREAM_READER 1
35 #include "config_components.h"
47 #define classic_shift_luma_table_size 42
49 34, 36, 35, 69, 135, 232, 9, 16, 10, 24, 11, 23, 12, 16, 13, 10,
50 14, 8, 15, 8, 16, 8, 17, 20, 16, 10, 207, 206, 205, 236, 11, 8,
51 10, 21, 9, 23, 8, 8, 199, 70, 69, 68, 0,
55 #define classic_shift_chroma_table_size 59
57 66, 36, 37, 38, 39, 40, 41, 75, 76, 77, 110, 239, 144, 81, 82, 83,
58 84, 85, 118, 183, 56, 57, 88, 89, 56, 89, 154, 57, 58, 57, 26, 141,
59 57, 56, 58, 57, 58, 57, 184, 119, 214, 245, 116, 83, 82, 49, 80, 79,
60 78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, 0,
65 3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
66 73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
67 68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
68 35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
69 37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
70 35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
71 27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
72 15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
73 12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
74 12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
75 18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
76 28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
77 28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
78 62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
79 54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
80 46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
84 3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
85 7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
86 11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
87 43, 45, 76, 81, 46, 82, 75, 55, 56, 144, 58, 80, 60, 74, 147, 63,
88 143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
89 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
90 17, 14, 5, 6, 100, 54, 47, 50, 51, 53, 106, 107, 108, 109, 110, 111,
91 112, 113, 114, 115, 4, 117, 118, 92, 94, 121, 122, 3, 124, 103, 2, 1,
92 0, 129, 130, 131, 120, 119, 126, 125, 136, 137, 138, 139, 140, 141, 142, 134,
93 135, 132, 133, 104, 64, 101, 62, 57, 102, 95, 93, 59, 61, 28, 97, 96,
94 52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
95 19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
96 7, 128, 127, 105, 123, 116, 35, 34, 33, 145, 31, 79, 42, 146, 78, 26,
97 83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
98 14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
99 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
106 for (
i = 0;
i < n;) {
132 if (
s->bitstream_bpp < 24 ||
s->version > 2) {
134 for (p = 0; p < 4; p++) {
135 int p0 =
s->version > 2 ? p : 0;
136 for (
i = y = 0; y <
s->vlc_n; y++) {
137 int len0 =
s->len[p0][y];
139 if (
limit <= 0 || !len0)
143 for (
u = 0;
u <
s->vlc_n;
u++) {
144 int len1 =
s->len[p][
u];
145 if (len1 >
limit || !len1)
150 len[
i] = len0 + len1;
151 bits[
i] = (
s->bits[p0][y] << len1) +
s->bits[p][
u];
152 symbols[
i] = (y << 8) + (
u & 0xFF);
158 bits, 2, 2, symbols, 2, 2, 0)) < 0)
162 uint8_t (*
map)[4] = (uint8_t(*)[4])
s->pix_bgr_map;
164 int p0 =
s->decorrelate;
165 int p1 = !
s->decorrelate;
169 for (
i = 0,
g = -16;
g < 16;
g++) {
170 int len0 =
s->len[p0][
g & 255];
172 if (limit0 < 2 || !len0)
174 for (
b = -16;
b < 16;
b++) {
175 int len1 =
s->len[p1][
b & 255];
176 int limit1 = limit0 - len1;
177 if (limit1 < 1 || !len1)
179 code = (
s->bits[p0][
g & 255] << len1) +
s->bits[p1][
b & 255];
180 for (
r = -16;
r < 16;
r++) {
181 int len2 =
s->len[2][
r & 255];
182 if (len2 > limit1 || !len2)
185 len[
i] = len0 + len1 + len2;
187 if (
s->decorrelate) {
221 count = 1 +
s->alpha + 2*
s->chroma;
223 for (
i = 0;
i < count;
i++) {
230 s->bits[
i], 4, 4, 0)) < 0)
255 for (
i = 0;
i < 256;
i++)
260 if (
s->bitstream_bpp >= 24) {
261 memcpy(
s->bits[1],
s->bits[0], 256 *
sizeof(uint32_t));
262 memcpy(
s->len[1],
s->len[0], 256 *
sizeof(uint8_t));
264 memcpy(
s->bits[2],
s->bits[1], 256 *
sizeof(uint32_t));
265 memcpy(
s->len[2],
s->len[1], 256 *
sizeof(uint8_t));
267 for (
i = 0;
i < 4;
i++) {
270 s->bits[
i], 4, 4, 0)) < 0)
288 for (
i = 0;
i < 8;
i++)
305 memset(
s->vlc, 0, 4 *
sizeof(
VLC));
307 s->interlaced = avctx->
height > 288;
325 if (
s->version >= 2) {
326 int method, interlace;
332 s->decorrelate = method & 64 ? 1 : 0;
333 s->predictor = method & 63;
334 if (
s->version == 2) {
336 if (
s->bitstream_bpp == 0)
343 s->chroma_v_shift = (avctx->
extradata[1] >> 2) & 3;
348 interlace = (avctx->
extradata[2] & 0x30) >> 4;
349 s->interlaced = (interlace == 1) ? 1 : (interlace == 2) ? 0 :
s->interlaced;
350 s->context = avctx->
extradata[2] & 0x40 ? 1 : 0;
385 if (
s->version <= 2) {
386 switch (
s->bitstream_bpp) {
416 switch ( (
s->chroma<<10) | (
s->yuv<<9) | (
s->alpha<<8) | ((
s->bps-1)<<4) |
s->chroma_h_shift | (
s->chroma_v_shift<<2)) {
557 "for this combination of colorspace and predictor type.\n");
568 #define VLC_INTERN(dst, table, gb, name, bits, max_depth) \
569 code = table[index].sym; \
570 n = table[index].len; \
571 if (max_depth > 1 && n < 0) { \
572 LAST_SKIP_BITS(name, gb, bits); \
573 UPDATE_CACHE(name, gb); \
576 index = SHOW_UBITS(name, gb, nb_bits) + code; \
577 code = table[index].sym; \
578 n = table[index].len; \
579 if (max_depth > 2 && n < 0) { \
580 LAST_SKIP_BITS(name, gb, nb_bits); \
581 UPDATE_CACHE(name, gb); \
584 index = SHOW_UBITS(name, gb, nb_bits) + code; \
585 code = table[index].sym; \
586 n = table[index].len; \
590 LAST_SKIP_BITS(name, gb, n)
593 #define GET_VLC_DUAL(dst0, dst1, name, gb, dtable, table1, table2, \
594 bits, max_depth, OP) \
596 unsigned int index = SHOW_UBITS(name, gb, bits); \
597 int code, n = dtable[index].len; \
601 VLC_INTERN(dst0, table1, gb, name, bits, max_depth); \
603 UPDATE_CACHE(re, gb); \
604 index = SHOW_UBITS(name, gb, bits); \
605 VLC_INTERN(dst1, table2, gb, name, bits, max_depth); \
607 code = dtable[index].sym; \
608 OP(dst0, dst1, code); \
609 LAST_SKIP_BITS(name, gb, n); \
613 #define OP8bits(dst0, dst1, code) dst0 = code>>8; dst1 = code
615 #define READ_2PIX(dst0, dst1, plane1) \
616 UPDATE_CACHE(re, &s->gb); \
617 GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane1].table, \
618 s->vlc[0].table, s->vlc[plane1].table, VLC_BITS, 3, OP8bits)
627 if (count >= icount) {
628 for (
i = 0;
i < icount;
i++) {
637 for (;
i < count;
i++)
638 s->temp[0][2 *
i ] =
s->temp[1][
i] =
639 s->temp[0][2 *
i + 1] =
s->temp[2][
i] = 0;
641 for (
i = 0;
i < count;
i++) {
649 #define READ_2PIX_PLANE(dst0, dst1, plane, OP) \
650 UPDATE_CACHE(re, &s->gb); \
651 GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane].table, \
652 s->vlc[plane].table, s->vlc[plane].table, VLC_BITS, 3, OP)
654 #define OP14bits(dst0, dst1, code) dst0 = code>>8; dst1 = sign_extend(code, 8)
658 #define READ_2PIX_PLANE16(dst0, dst1, plane){\
659 dst0 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
660 dst0 += get_bits(&s->gb, 2);\
661 dst1 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
662 dst1 += get_bits(&s->gb, 2);\
675 for(
i=0;
i<count;
i++){
681 int nb_bits,
code, n;
688 }
else if (
s->bps <= 14) {
695 for(
i=0;
i<count;
i++){
701 int nb_bits,
code, n;
714 for(
i=0;
i<count;
i++){
736 for (
i = 0;
i < count;
i++) {
751 int code, n, nb_bits;
755 n =
s->vlc[4].table[
index].len;
759 *(uint32_t *) &
s->temp[0][4 *
i] =
s->pix_bgr_map[
code];
769 s->temp[0][4 *
i +
B] =
code +
s->temp[0][4 *
i +
G];
774 s->temp[0][4 *
i +
R] =
code +
s->temp[0][4 *
i +
G];
796 s->temp[0][4 *
i +
A] = 0;
803 if (
s->decorrelate) {
804 if (
s->bitstream_bpp == 24)
809 if (
s->bitstream_bpp == 24)
821 if (!
s->avctx->draw_horiz_band)
824 h = y -
s->last_slice_end;
827 if (
s->bitstream_bpp == 12)
841 s->last_slice_end = y +
h;
847 return s->llviddsp.add_left_pred(dst,
src,
w,
acc);
849 return s->llviddsp.add_left_pred_int16(( uint16_t *)dst, (
const uint16_t *)
src,
s->n-1,
w,
acc);
856 s->llviddsp.add_bytes(dst,
src,
w);
858 s->hdsp.add_int16((uint16_t*)dst, (
const uint16_t*)
src,
s->n - 1,
w);
865 s->llviddsp.add_median_pred(dst,
src,
diff,
w,
left, left_top);
867 s->hdsp.add_hfyu_median_pred_int16((uint16_t *)dst, (
const uint16_t *)
src, (
const uint16_t *)
diff,
s->n-1,
w,
left, left_top);
872 int buf_size,
int y_offset,
int table_size)
875 int fake_ystride, fake_ustride, fake_vstride;
876 const int width =
s->width;
877 const int width2 =
s->width >> 1;
880 if ((
ret =
init_get_bits8(&
s->gb,
s->bitstream_buffer + table_size, buf_size - table_size)) < 0)
887 if (
s->version > 2) {
889 for(plane = 0; plane < 1 + 2*
s->chroma +
s->alpha; plane++) {
890 int left, lefttop, y;
893 int fake_stride = fake_ystride;
895 if (
s->chroma && (plane == 1 || plane == 2)) {
896 w >>=
s->chroma_h_shift;
897 h >>=
s->chroma_v_shift;
898 fake_stride = plane == 1 ? fake_ustride : fake_vstride;
901 switch (
s->predictor) {
907 for (y = 1; y <
h; y++) {
912 if (
s->predictor ==
PLANE) {
913 if (y >
s->interlaced) {
937 lefttop = p->
data[plane][0];
956 }
else if (
s->bitstream_bpp < 24) {
958 int lefty, leftu, leftv;
959 int lefttopy, lefttopu, lefttopv;
968 "YUY2 output is not implemented yet\n");
979 switch (
s->predictor) {
983 lefty =
s->llviddsp.add_left_pred(p->
data[0] + p->
linesize[0] * y_offset + 2,
s->temp[0],
986 leftu =
s->llviddsp.add_left_pred(p->
data[1] + p->
linesize[1] * y_offset + 1,
s->temp[1], width2 - 1, leftu);
987 leftv =
s->llviddsp.add_left_pred(p->
data[2] + p->
linesize[2] * y_offset + 1,
s->temp[2], width2 - 1, leftv);
990 for (cy = y = 1; y <
height; y++, cy++) {
991 uint8_t *ydst, *udst, *vdst;
993 if (
s->bitstream_bpp == 12) {
998 lefty =
s->llviddsp.add_left_pred(ydst,
s->temp[0],
1000 if (
s->predictor ==
PLANE) {
1001 if (y >
s->interlaced)
1002 s->llviddsp.add_bytes(ydst, ydst - fake_ystride,
width);
1016 lefty =
s->llviddsp.add_left_pred(ydst,
s->temp[0],
1019 leftu =
s->llviddsp.add_left_pred(udst,
s->temp[1], width2, leftu);
1020 leftv =
s->llviddsp.add_left_pred(vdst,
s->temp[2], width2, leftv);
1022 if (
s->predictor ==
PLANE) {
1023 if (cy >
s->interlaced) {
1024 s->llviddsp.add_bytes(ydst, ydst - fake_ystride,
width);
1026 s->llviddsp.add_bytes(udst, udst - fake_ustride, width2);
1027 s->llviddsp.add_bytes(vdst, vdst - fake_vstride, width2);
1038 lefty =
s->llviddsp.add_left_pred(p->
data[0] + 2,
s->temp[0],
1041 leftu =
s->llviddsp.add_left_pred(p->
data[1] + 1,
s->temp[1], width2 - 1, leftu);
1042 leftv =
s->llviddsp.add_left_pred(p->
data[2] + 1,
s->temp[2], width2 - 1, leftv);
1050 if (
s->interlaced) {
1052 lefty =
s->llviddsp.add_left_pred(p->
data[0] + p->
linesize[0],
1053 s->temp[0],
width, lefty);
1055 leftu =
s->llviddsp.add_left_pred(p->
data[1] + p->
linesize[2],
s->temp[1], width2, leftu);
1056 leftv =
s->llviddsp.add_left_pred(p->
data[2] + p->
linesize[1],
s->temp[2], width2, leftv);
1066 lefty =
s->llviddsp.add_left_pred(p->
data[0] + fake_ystride,
1067 s->temp[0], 4, lefty);
1069 leftu =
s->llviddsp.add_left_pred(p->
data[1] + fake_ustride,
s->temp[1], 2, leftu);
1070 leftv =
s->llviddsp.add_left_pred(p->
data[2] + fake_vstride,
s->temp[2], 2, leftv);
1074 lefttopy = p->
data[0][3];
1076 s->llviddsp.add_median_pred(p->
data[0] + fake_ystride + 4,
1077 p->
data[0] + 4,
s->temp[0],
1078 width - 4, &lefty, &lefttopy);
1080 lefttopu = p->
data[1][1];
1081 lefttopv = p->
data[2][1];
1082 s->llviddsp.add_median_pred(p->
data[1] + fake_ustride + 2, p->
data[1] + 2,
s->temp[1], width2 - 2, &leftu, &lefttopu);
1083 s->llviddsp.add_median_pred(p->
data[2] + fake_vstride + 2, p->
data[2] + 2,
s->temp[2], width2 - 2, &leftv, &lefttopv);
1088 for (; y <
height; y++, cy++) {
1089 uint8_t *ydst, *udst, *vdst;
1091 if (
s->bitstream_bpp == 12) {
1092 while (2 * cy > y) {
1095 s->llviddsp.add_median_pred(ydst, ydst - fake_ystride,
1111 s->llviddsp.add_median_pred(ydst, ydst - fake_ystride,
1115 s->llviddsp.add_median_pred(udst, udst - fake_ustride,
s->temp[1], width2, &leftu, &lefttopu);
1116 s->llviddsp.add_median_pred(vdst, vdst - fake_vstride,
s->temp[2], width2, &leftv, &lefttopv);
1129 if (
s->bitstream_bpp == 32) {
1143 switch (
s->predictor) {
1147 s->hdsp.add_hfyu_left_pred_bgr32(p->
data[0] + last_line + 4,
1150 for (y =
height - 2; y >= 0; y--) {
1153 s->hdsp.add_hfyu_left_pred_bgr32(p->
data[0] + p->
linesize[0] * (y + y_offset),
1155 if (
s->predictor ==
PLANE) {
1156 if (
s->bitstream_bpp != 32)
1159 s->llviddsp.add_bytes(p->
data[0] + p->
linesize[0] * (y + y_offset),
1161 fake_ystride, 4 *
width);
1170 "prediction type not supported!\n");
1174 "BGR24 output is not implemented yet\n");
1185 const uint8_t *buf = avpkt->
data;
1186 int buf_size = avpkt->
size;
1188 const int width =
s->width;
1190 int slice, table_size = 0,
ret, nb_slices;
1191 unsigned slices_info_offset;
1198 &
s->bitstream_buffer_size,
1200 if (!
s->bitstream_buffer)
1203 s->bdsp.bswap_buf((uint32_t *)
s->bitstream_buffer,
1204 (
const uint32_t *) buf, buf_size / 4);
1215 if ((
unsigned) (buf_size - table_size) >= INT_MAX / 8)
1218 s->last_slice_end = 0;
1221 (buf_size > 32 &&
AV_RL32(avpkt->
data + buf_size - 16) == 0)) {
1222 slices_info_offset =
AV_RL32(avpkt->
data + buf_size - 4);
1223 slice_height =
AV_RL32(avpkt->
data + buf_size - 8);
1225 if (nb_slices * 8LL + slices_info_offset > buf_size - 16 ||
1226 s->chroma_v_shift ||
1227 slice_height <= 0 || nb_slices * (uint64_t)slice_height >
height)
1234 for (slice = 0; slice < nb_slices; slice++) {
1235 int y_offset, slice_offset, slice_size;
1237 if (nb_slices > 1) {
1238 slice_offset =
AV_RL32(avpkt->
data + slices_info_offset + slice * 8);
1239 slice_size =
AV_RL32(avpkt->
data + slices_info_offset + slice * 8 + 4);
1241 if (slice_offset < 0 || slice_size <= 0 || (slice_offset&3) ||
1242 slice_offset + (int64_t)slice_size > buf_size)
1245 y_offset =
height - (slice + 1) * slice_height;
1246 s->bdsp.bswap_buf((uint32_t *)
s->bitstream_buffer,
1247 (
const uint32_t *)(buf + slice_offset), slice_size / 4);
1251 slice_size = buf_size;
1254 ret =
decode_slice(avctx, p, slice_height, slice_size, y_offset, table_size);
1266 .
p.
name =
"huffyuv",
1279 #if CONFIG_FFVHUFF_DECODER
1281 .
p.
name =
"ffvhuff",
1295 #if CONFIG_HYMT_DECODER