50 const uint8_t *val_table,
int nb_codes,
51 int use_static,
int is_ac)
54 uint16_t huff_code[256];
55 uint16_t huff_sym[256];
62 for (i = 0; i < 256; i++)
63 huff_sym[i] = i + 16 * is_ac;
66 huff_sym[0] = 16 * 256;
69 huff_code, 2, 2, huff_sym, 2, 2, use_static);
91 if (len > 14 && buf[12] == 1)
93 if (len > 14 && buf[12] == 2)
132 "error using external huffman table, switching back to internal\n");
175 for (i = 0; i < 64; i++) {
210 for (i = 1; i <= 16; i++) {
215 if (len < n || n > 256)
219 for (i = 0; i <
n; i++) {
230 class, index, code_max + 1);
231 if ((ret =
build_vlc(&s->
vlcs[
class][index], bits_table, val_table,
232 code_max + 1, 0,
class > 0)) < 0)
237 if ((ret =
build_vlc(&s->
vlcs[2][index], bits_table, val_table,
238 code_max + 1, 0, 0)) < 0)
260 if (bits > 16 || bits < 1) {
290 if (nb_components <= 0 ||
296 "nb_components changing in interlaced picture\n");
300 if (s->
ls && !(bits <= 8 || nb_components == 1)) {
302 "JPEG-LS that is not <= 8 "
303 "bits/component or 16-bit gray");
309 for (i = 0; i < nb_components; i++) {
315 if (h_count[i] > s->
h_max)
316 s->
h_max = h_count[i];
317 if (v_count[i] > s->
v_max)
318 s->
v_max = v_count[i];
324 if (!h_count[i] || !v_count[i]) {
326 "Invalid sampling factor in component %d %d:%d\n",
327 i, h_count[i], v_count[i]);
332 i, h_count[i], v_count[i],
344 memcmp(s->
h_count, h_count,
sizeof(h_count)) ||
345 memcmp(s->
v_count, v_count,
sizeof(v_count))) {
350 memcpy(s->
h_count, h_count,
sizeof(h_count));
351 memcpy(s->
v_count, v_count,
sizeof(v_count));
379 if (s->
v_max == 1 && s->
h_max == 1 && s->
lossless==1 && (nb_components==3 || nb_components==4))
384 pix_fmt_id = ((unsigned)s->
h_count[0] << 28) | (s->
v_count[0] << 24) |
391 if (!(pix_fmt_id & 0xD0D0D0D0))
392 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
393 if (!(pix_fmt_id & 0x0D0D0D0D))
394 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
396 for (i = 0; i < 8; i++) {
397 int j = 6 + (i&1) - (i&6);
398 int is = (pix_fmt_id >> (4*i)) & 0xF;
399 int js = (pix_fmt_id >> (4*j)) & 0xF;
401 if (is == 1 && js != 2 && (i < 2 || i > 5))
402 js = (pix_fmt_id >> ( 8 + 4*(i&1))) & 0xF;
403 if (is == 1 && js != 2 && (i < 2 || i > 5))
404 js = (pix_fmt_id >> (16 + 4*(i&1))) & 0xF;
406 if (is == 1 && js == 2) {
412 switch (pix_fmt_id) {
500 if (pix_fmt_id == 0x14111100)
533 if (pix_fmt_id == 0x42111100) {
537 }
else if (pix_fmt_id == 0x24111100) {
568 else if (s->
bits <= 8)
587 for (i = 0; i < 4; i++)
594 if (len != (8 + (3 * nb_components)))
605 int bw = (width + s->
h_max * 8 - 1) / (s->
h_max * 8);
606 int bh = (height + s->
v_max * 8 - 1) / (s->
v_max * 8);
626 if (code < 0 || code > 16) {
628 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
629 0, dc_index, &s->
vlcs[0][dc_index]);
641 int dc_index,
int ac_index, int16_t *quant_matrix)
647 if (val == 0xfffff) {
651 val = val * quant_matrix[0] + s->
last_dc[component];
661 i += ((unsigned)code) >> 4;
669 int sign = (~cache) >> 31;
670 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
680 block[j] = level * quant_matrix[j];
689 int component,
int dc_index,
690 int16_t *quant_matrix,
int Al)
695 if (val == 0xfffff) {
699 val = (val * quant_matrix[0] << Al) + s->
last_dc[component];
707 uint8_t *last_nnz,
int ac_index,
708 int16_t *quant_matrix,
709 int ss,
int se,
int Al,
int *EOBRUN)
720 for (i = ss; ; i++) {
724 run = ((unsigned) code) >> 4;
733 int sign = (~cache) >> 31;
734 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
742 block[j] = level * quant_matrix[j] << Al;
749 block[j] = level * quant_matrix[j] << Al;
778 #define REFINE_BIT(j) { \
779 UPDATE_CACHE(re, &s->gb); \
780 sign = block[j] >> 15; \
781 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
782 ((quant_matrix[j] ^ sign) - sign) << Al; \
783 LAST_SKIP_BITS(re, &s->gb, 1); \
791 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
796 j = s->scantable.permutated[i]; \
799 else if (run-- == 0) \
806 int ac_index, int16_t *quant_matrix,
807 int ss,
int se,
int Al,
int *EOBRUN)
809 int code, i = ss, j, sign,
val,
run;
810 int last =
FFMIN(se, *last_nnz);
821 run = ((unsigned) code) >> 4;
828 block[j] = ((quant_matrix[j]^
val) - val) << Al;
836 run = ((unsigned) code) >> 4;
857 for (; i <= last; i++) {
878 for (i = 0; i < nb_components; i++)
892 for (i = 0; i < nb_components; i++)
907 int left[4], top[4], topleft[4];
908 const int linesize = s->
linesize[0];
909 const int mask = ((1 << s->
bits) - 1) << point_transform;
925 for (i = 0; i < 4; i++)
928 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
932 ptr += linesize >> 1;
934 for (i = 0; i < 4; i++)
935 top[i] = left[i] = topleft[i] =
buffer[0][i];
937 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
945 top[i] = left[i]= topleft[i]= 1 << (s->
bits - 1);
947 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
948 modified_predictor = 1;
950 for (i=0;i<nb_components;i++) {
956 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
962 left[i] =
buffer[mb_x][i] =
963 mask & (pred + (dc << point_transform));
972 for(i=0; i<nb_components; i++) {
975 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
976 ptr[4*mb_x+3-
c] =
buffer[mb_x][i];
978 }
else if(s->
bits == 9) {
981 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
982 ((uint16_t*)ptr)[4*mb_x+
c] =
buffer[mb_x][i];
987 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
988 ptr[3*mb_x + 1] =
buffer[mb_x][0] - ((
buffer[mb_x][1] +
buffer[mb_x][2] - 0x200) >> 2);
989 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
990 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
993 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
995 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
996 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
999 for(i=0; i<nb_components; i++) {
1002 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1003 ptr[3*mb_x+2-
c] =
buffer[mb_x][i];
1005 }
else if(s->
bits == 9) {
1008 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1009 ((uint16_t*)ptr)[3*mb_x+2-
c] =
buffer[mb_x][i];
1019 int point_transform,
int nb_components)
1021 int i, mb_x, mb_y,
mask;
1023 int resync_mb_y = 0;
1024 int resync_mb_x = 0;
1026 point_transform += bits - s->
bits;
1027 mask = ((1 << s->
bits) - 1) << point_transform;
1029 av_assert0(nb_components>=1 && nb_components<=4);
1031 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1032 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1039 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->
interlaced){
1040 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
1041 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1042 for (i = 0; i < nb_components; i++) {
1045 int n, h,
v, x,
y,
c, j, linesize;
1054 if(bits>8) linesize /= 2;
1056 for(j=0; j<
n; j++) {
1065 if(x==0 && leftcol){
1066 pred= 1 << (bits - 1);
1071 if(x==0 && leftcol){
1072 pred= ptr[-linesize];
1074 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1079 ptr += linesize >> 1;
1081 *ptr= pred + (dc << point_transform);
1083 ptr16 = (uint16_t*)(s->
picture_ptr->
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
1085 if(x==0 && leftcol){
1086 pred= 1 << (bits - 1);
1091 if(x==0 && leftcol){
1092 pred= ptr16[-linesize];
1094 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1099 ptr16 += linesize >> 1;
1101 *ptr16= pred + (dc << point_transform);
1110 for (i = 0; i < nb_components; i++) {
1113 int n, h,
v, x,
y,
c, j, linesize,
dc;
1122 if(bits>8) linesize /= 2;
1124 for (j = 0; j <
n; j++) {
1132 (linesize * (v * mb_y +
y)) +
1134 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1137 *ptr = pred + (dc << point_transform);
1139 ptr16 = (uint16_t*)(s->
picture_ptr->
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
1140 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1143 *ptr16= pred + (dc << point_transform);
1164 int linesize,
int lowres)
1169 case 1:
copy_block4(dst, src, linesize, linesize, 4);
1171 case 2:
copy_block2(dst, src, linesize, linesize, 2);
1173 case 3: *dst = *
src;
1180 int block_x, block_y;
1183 for (block_y=0; block_y<
size; block_y++)
1184 for (block_x=0; block_x<
size; block_x++)
1185 *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->
bits;
1187 for (block_y=0; block_y<
size; block_y++)
1188 for (block_x=0; block_x<
size; block_x++)
1189 *(ptr + block_x + block_y*linesize) <<= 8 - s->
bits;
1194 int Al,
const uint8_t *mb_bitmask,
1195 int mb_bitmask_size,
1203 int bytes_per_pixel = 1 + (s->
bits > 8);
1215 for (i = 0; i < nb_components; i++) {
1218 reference_data[
c] = reference ? reference->
data[
c] :
NULL;
1223 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1224 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1235 for (i = 0; i < nb_components; i++) {
1237 int n, h,
v, x,
y,
c, j;
1245 for (j = 0; j <
n; j++) {
1246 block_offset = (((linesize[
c] * (v * mb_y +
y) * 8) +
1247 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->
avctx->
lowres);
1250 block_offset += linesize[
c] >> 1;
1251 if ( 8*(h * mb_x + x) < s->
width
1252 && 8*(v * mb_y +
y) < s->
height) {
1253 ptr = data[
c] + block_offset;
1268 "error y=%d x=%d\n", mb_y, mb_x);
1288 "error y=%d x=%d\n", mb_y, mb_x);
1292 av_dlog(s->
avctx,
"mb: %d %d processed\n", mb_y, mb_x);
1295 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1310 int se,
int Ah,
int Al)
1319 int bytes_per_pixel = 1 + (s->
bits > 8);
1322 if (se < ss || se > 63) {
1335 data += linesize >> 1;
1339 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1344 for (mb_x = 0; mb_x < s->
mb_width; mb_x++,
block++, last_nnz++) {
1351 quant_matrix, ss, se, Al, &EOBRUN);
1354 quant_matrix, ss, se, Al, &EOBRUN);
1357 "error y=%d x=%d\n", mb_y, mb_x);
1375 int mb_bitmask_size,
const AVFrame *reference)
1377 int len, nb_components, i, h,
v,
predictor, point_transform;
1379 const int block_size = s->
lossless ? 1 : 8;
1380 int ilv, prev_shift;
1384 "Can not process SOS before SOF, skipping\n");
1394 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1397 if (len != 6 + 2 * nb_components) {
1401 for (i = 0; i < nb_components; i++) {
1410 "decode_sos: index(%d) out of components\n", index);
1426 index = (index+2)%3;
1446 prev_shift = point_transform = 0;
1448 if (nb_components > 1) {
1452 }
else if (!s->
ls) {
1455 s->
mb_width = (s->
width + h * block_size - 1) / (h * block_size);
1464 s->
lossless ?
"lossless" :
"sequential DCT", s->
rgb ?
"RGB" :
"",
1474 for (i = 0; i < nb_components; i++)
1479 if (CONFIG_JPEGLS_DECODER && s->
ls) {
1484 point_transform, ilv)) < 0)
1493 nb_components)) < 0)
1502 point_transform)) < 0)
1506 prev_shift, point_transform,
1507 mb_bitmask, mb_bitmask_size, reference)) < 0)
1591 int t_w, t_h, v1, v2;
1603 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1612 if (len -10 - (t_w * t_h * 3) > 0)
1613 len -= t_w * t_h * 3;
1635 "Pegasus lossless jpeg header found\n");
1665 if (
id ==
AV_RL32(
"colr") && len > 0) {
1672 if (
id ==
AV_RL32(
"xfrm") && len > 0) {
1700 }
else if (type == 1) {
1712 if (!(flags & 0x04)) {
1722 int ret,
le, ifd_offset, bytes_read;
1779 "mjpeg: error, decode_app parser read over the end\n");
1793 for (i = 0; i < len - 2; i++)
1795 if (i > 0 && cbuf[i - 1] ==
'\n')
1804 if (!strncmp(cbuf,
"AVID", 4)) {
1806 }
else if (!strcmp(cbuf,
"CS=ITU601"))
1808 else if ((!strncmp(cbuf,
"Intel(R) JPEG Library, version 1", 32) && s->
avctx->
codec_tag) ||
1809 (!strncmp(cbuf,
"Metasoft MJPEG Codec", 20)))
1828 buf_ptr = *pbuf_ptr;
1829 while (buf_end - buf_ptr > 1) {
1832 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1841 av_dlog(
NULL,
"find_marker skipped %d bytes\n", skipped);
1842 *pbuf_ptr = buf_ptr;
1848 const uint8_t **unescaped_buf_ptr,
1849 int *unescaped_buf_size)
1859 if (start_code ==
SOS && !s->
ls) {
1863 while (src < buf_end) {
1869 while (src < buf_end && x == 0xff)
1872 if (x >= 0xd0 && x <= 0xd7)
1879 *unescaped_buf_ptr = s->
buffer;
1880 *unescaped_buf_size = dst - s->
buffer;
1881 memset(s->
buffer + *unescaped_buf_size, 0,
1885 (buf_end - *buf_ptr) - (dst - s->
buffer));
1886 }
else if (start_code ==
SOS && s->
ls) {
1894 while (src + t < buf_end) {
1897 while ((src + t < buf_end) && x == 0xff)
1924 *unescaped_buf_ptr = dst;
1925 *unescaped_buf_size = (bit_count + 7) >> 3;
1926 memset(s->
buffer + *unescaped_buf_size, 0,
1929 *unescaped_buf_ptr = *buf_ptr;
1930 *unescaped_buf_size = buf_end - *buf_ptr;
1941 int buf_size = avpkt->
size;
1943 const uint8_t *buf_end, *buf_ptr;
1944 const uint8_t *unescaped_buf_ptr;
1946 int unescaped_buf_size;
1957 buf_end = buf + buf_size;
1958 while (buf_ptr < buf_end) {
1962 &unescaped_buf_size);
1964 if (start_code < 0) {
1966 }
else if (unescaped_buf_size > INT_MAX / 8) {
1968 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1969 start_code, unescaped_buf_size, buf_size);
1973 start_code, buf_end - buf_ptr);
1987 if (start_code >= 0xd0 && start_code <= 0xd7)
1989 "restart marker: %d\n", start_code & 0x0f);
1991 else if (start_code >=
APP0 && start_code <=
APP15)
1994 else if (start_code ==
COM)
1999 if (!CONFIG_JPEGLS_DECODER &&
2000 (start_code ==
SOF48 || start_code ==
LSE)) {
2005 switch (start_code) {
2050 if (!CONFIG_JPEGLS_DECODER ||
2059 "Found EOI before any SOF, ignoring\n");
2077 int qpw = (s->
width + 15) / 16;
2080 memset(qp_table_buf->
data, qp, qpw);
2109 "mjpeg: unsupported coding type (%x)\n", start_code);
2116 "marker parser used %d bytes (%d bits)\n",
2148 for (p = 0; p<4; p++) {
2161 for (i = 0; i < h; i++) {
2162 if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2];
2163 else line[w - 1] = line[(w - 1) / 2];
2164 for (index = w - 2; index > 0; index--) {
2166 ((uint16_t*)line)[
index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1;
2168 line[
index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2191 for (p = 0; p < 4; p++) {
2202 for (i = h - 1; i; i--) {
2205 if (src1 == src2 || i == h - 1) {
2206 memcpy(dst, src1, w);
2208 for (index = 0; index < w; index++)
2209 dst[index] = (src1[index] + src2[index]) >> 1;
2218 for (index=0; index<4; index++) {
2222 if(index && index<3){
2228 for (i=0; i<h/2; i++) {
2230 FFSWAP(
int, dst[j], dst2[j]);
2240 for (i=0; i<h; i++) {
2243 for (index=0; index<4; index++) {
2247 for (j=0; j<w; j++) {
2249 int r = dst[0][j] * k;
2250 int g = dst[1][j] * k;
2251 int b = dst[2][j] * k;
2252 dst[0][j] = g*257 >> 16;
2253 dst[1][j] = b*257 >> 16;
2254 dst[2][j] = r*257 >> 16;
2262 for (i=0; i<h; i++) {
2265 for (index=0; index<4; index++) {
2269 for (j=0; j<w; j++) {
2271 int r = (255 - dst[0][j]) * k;
2272 int g = (128 - dst[1][j]) * k;
2273 int b = (128 - dst[2][j]) * k;
2274 dst[0][j] = r*257 >> 16;
2275 dst[1][j] = (g*257 >> 16) + 128;
2276 dst[2][j] = (b*257 >> 16) + 128;
2297 return buf_ptr -
buf;
2320 for (i = 0; i < 3; i++) {
2321 for (j = 0; j < 4; j++)
2338 #if CONFIG_MJPEG_DECODER
2339 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2340 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2342 {
"extern_huff",
"Use external huffman table.",
2347 static const AVClass mjpegdec_class = {
2366 .priv_class = &mjpegdec_class,
2369 #if CONFIG_THP_DECODER