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)
284 if (nb_components <= 0 ||
290 "nb_components changing in interlaced picture\n");
294 if (s->
ls && !(s->
bits <= 8 || nb_components == 1)) {
296 "JPEG-LS that is not <= 8 "
297 "bits/component or 16-bit gray");
303 memset(h_count, 0,
sizeof(h_count));
304 memset(v_count, 0,
sizeof(v_count));
305 for (i = 0; i < nb_components; i++) {
311 if (h_count[i] > s->
h_max)
312 s->
h_max = h_count[i];
313 if (v_count[i] > s->
v_max)
314 s->
v_max = v_count[i];
320 if (!h_count[i] || !v_count[i]) {
322 "Invalid sampling factor in component %d %d:%d\n",
323 i, h_count[i], v_count[i]);
328 i, h_count[i], v_count[i],
340 || memcmp(s->
h_count, h_count,
sizeof(h_count))
341 || memcmp(s->
v_count, v_count,
sizeof(v_count))) {
345 memcpy(s->
h_count, h_count,
sizeof(h_count));
346 memcpy(s->
v_count, v_count,
sizeof(v_count));
374 if (s->
v_max == 1 && s->
h_max == 1 && s->
lossless==1 && (nb_components==3 || nb_components==4))
386 if (!(pix_fmt_id & 0xD0D0D0D0))
387 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
388 if (!(pix_fmt_id & 0x0D0D0D0D))
389 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
391 for (i = 0; i < 8; i++) {
392 int j = 6 + (i&1) - (i&6);
393 int is = (pix_fmt_id >> (4*i)) & 0xF;
394 int js = (pix_fmt_id >> (4*j)) & 0xF;
396 if (is == 1 && js != 2 && (i < 2 || i > 5))
397 js = (pix_fmt_id >> ( 8 + 4*(i&1))) & 0xF;
398 if (is == 1 && js != 2 && (i < 2 || i > 5))
399 js = (pix_fmt_id >> (16 + 4*(i&1))) & 0xF;
401 if (is == 1 && js == 2) {
407 switch (pix_fmt_id) {
514 if (pix_fmt_id == 0x42111100) {
541 else if (s->
bits <= 8)
560 for (i = 0; i < 4; i++)
567 if (len != (8 + (3 * nb_components)))
578 int bw = (width + s->
h_max * 8 - 1) / (s->
h_max * 8);
579 int bh = (height + s->
v_max * 8 - 1) / (s->
v_max * 8);
599 if (code < 0 || code > 16) {
601 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
602 0, dc_index, &s->
vlcs[0][dc_index]);
614 int dc_index,
int ac_index, int16_t *quant_matrix)
620 if (val == 0xfffff) {
624 val = val * quant_matrix[0] + s->
last_dc[component];
634 i += ((unsigned)code) >> 4;
642 int sign = (~cache) >> 31;
643 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
653 block[j] = level * quant_matrix[j];
662 int component,
int dc_index,
663 int16_t *quant_matrix,
int Al)
668 if (val == 0xfffff) {
672 val = (val * quant_matrix[0] << Al) + s->
last_dc[component];
680 uint8_t *last_nnz,
int ac_index,
681 int16_t *quant_matrix,
682 int ss,
int se,
int Al,
int *EOBRUN)
693 for (i = ss; ; i++) {
697 run = ((unsigned) code) >> 4;
706 int sign = (~cache) >> 31;
707 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
715 block[j] = level * quant_matrix[j] << Al;
722 block[j] = level * quant_matrix[j] << Al;
751 #define REFINE_BIT(j) { \
752 UPDATE_CACHE(re, &s->gb); \
753 sign = block[j] >> 15; \
754 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
755 ((quant_matrix[j] ^ sign) - sign) << Al; \
756 LAST_SKIP_BITS(re, &s->gb, 1); \
764 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
769 j = s->scantable.permutated[i]; \
772 else if (run-- == 0) \
779 int ac_index, int16_t *quant_matrix,
780 int ss,
int se,
int Al,
int *EOBRUN)
782 int code, i = ss, j, sign,
val,
run;
783 int last =
FFMIN(se, *last_nnz);
794 run = ((unsigned) code) >> 4;
801 block[j] = ((quant_matrix[j]^
val) - val) << Al;
809 run = ((unsigned) code) >> 4;
830 for (; i <= last; i++) {
851 for (i = 0; i < nb_components; i++)
865 for (i = 0; i < nb_components; i++)
880 int left[4], top[4], topleft[4];
881 const int linesize = s->
linesize[0];
882 const int mask = ((1 << s->
bits) - 1) << point_transform;
898 for (i = 0; i < 4; i++)
901 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
905 ptr += linesize >> 1;
907 for (i = 0; i < 4; i++)
908 top[i] = left[i] = topleft[i] =
buffer[0][i];
910 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
918 top[i] = left[i]= topleft[i]= 1 << (s->
bits - 1);
920 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
921 modified_predictor = 1;
923 for (i=0;i<nb_components;i++) {
929 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
935 left[i] =
buffer[mb_x][i] =
936 mask & (pred + (dc << point_transform));
945 for(i=0; i<nb_components; i++) {
948 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
949 ptr[4*mb_x+3-
c] =
buffer[mb_x][i];
951 }
else if(s->
bits == 9) {
954 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
955 ((uint16_t*)ptr)[4*mb_x+
c] =
buffer[mb_x][i];
960 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
961 ptr[3*mb_x + 1] =
buffer[mb_x][0] - ((
buffer[mb_x][1] +
buffer[mb_x][2] - 0x200) >> 2);
962 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
963 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
966 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
968 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
969 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
972 for(i=0; i<nb_components; i++) {
975 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
976 ptr[3*mb_x+2-
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)[3*mb_x+2-
c] =
buffer[mb_x][i];
992 int point_transform,
int nb_components)
994 int i, mb_x, mb_y,
mask;
999 point_transform += bits - s->
bits;
1000 mask = ((1 << s->
bits) - 1) << point_transform;
1002 av_assert0(nb_components>=1 && nb_components<=4);
1004 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1005 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1012 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
1013 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
1014 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1015 for (i = 0; i < nb_components; i++) {
1018 int n, h,
v, x,
y,
c, j, linesize;
1027 if(bits>8) linesize /= 2;
1029 for(j=0; j<
n; j++) {
1038 if(x==0 && leftcol){
1039 pred= 1 << (bits - 1);
1044 if(x==0 && leftcol){
1045 pred= ptr[-linesize];
1047 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1052 ptr += linesize >> 1;
1054 *ptr= pred + (dc << point_transform);
1056 ptr16 = (uint16_t*)(s->
picture_ptr->
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
1058 if(x==0 && leftcol){
1059 pred= 1 << (bits - 1);
1064 if(x==0 && leftcol){
1065 pred= ptr16[-linesize];
1067 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1072 ptr16 += linesize >> 1;
1074 *ptr16= pred + (dc << point_transform);
1083 for (i = 0; i < nb_components; i++) {
1086 int n, h,
v, x,
y,
c, j, linesize,
dc;
1095 if(bits>8) linesize /= 2;
1097 for (j = 0; j <
n; j++) {
1105 (linesize * (v * mb_y +
y)) +
1107 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1110 *ptr = pred + (dc << point_transform);
1112 ptr16 = (uint16_t*)(s->
picture_ptr->
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
1113 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1116 *ptr16= pred + (dc << point_transform);
1137 int linesize,
int lowres)
1142 case 1:
copy_block4(dst, src, linesize, linesize, 4);
1144 case 2:
copy_block2(dst, src, linesize, linesize, 2);
1146 case 3: *dst = *
src;
1153 int block_x, block_y;
1156 for (block_y=0; block_y<
size; block_y++)
1157 for (block_x=0; block_x<
size; block_x++)
1158 *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->
bits;
1160 for (block_y=0; block_y<
size; block_y++)
1161 for (block_x=0; block_x<
size; block_x++)
1162 *(ptr + block_x + block_y*linesize) <<= 8 - s->
bits;
1167 int Al,
const uint8_t *mb_bitmask,
1168 int mb_bitmask_size,
1176 int bytes_per_pixel = 1 + (s->
bits > 8);
1188 for (i = 0; i < nb_components; i++) {
1191 reference_data[
c] = reference ? reference->
data[
c] : NULL;
1196 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1197 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1208 for (i = 0; i < nb_components; i++) {
1210 int n, h,
v, x,
y,
c, j;
1218 for (j = 0; j <
n; j++) {
1219 block_offset = (((linesize[
c] * (v * mb_y +
y) * 8) +
1220 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->
avctx->
lowres);
1223 block_offset += linesize[
c] >> 1;
1224 ptr = data[
c] + block_offset;
1236 "error y=%d x=%d\n", mb_y, mb_x);
1254 "error y=%d x=%d\n", mb_y, mb_x);
1258 av_dlog(s->
avctx,
"mb: %d %d processed\n", mb_y, mb_x);
1261 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1276 int se,
int Ah,
int Al)
1285 int bytes_per_pixel = 1 + (s->
bits > 8);
1288 if (se < ss || se > 63) {
1299 data += linesize >> 1;
1303 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1308 for (mb_x = 0; mb_x < s->
mb_width; mb_x++,
block++, last_nnz++) {
1315 quant_matrix, ss, se, Al, &EOBRUN);
1318 quant_matrix, ss, se, Al, &EOBRUN);
1321 "error y=%d x=%d\n", mb_y, mb_x);
1339 int mb_bitmask_size,
const AVFrame *reference)
1341 int len, nb_components, i, h,
v,
predictor, point_transform;
1343 const int block_size = s->
lossless ? 1 : 8;
1344 int ilv, prev_shift;
1348 "Can not process SOS before SOF, skipping\n");
1358 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1361 if (len != 6 + 2 * nb_components) {
1365 for (i = 0; i < nb_components; i++) {
1374 "decode_sos: index(%d) out of components\n", index);
1390 index = (index+2)%3;
1410 prev_shift = point_transform = 0;
1412 if (nb_components > 1) {
1416 }
else if (!s->
ls) {
1419 s->
mb_width = (s->
width + h * block_size - 1) / (h * block_size);
1428 s->
lossless ?
"lossless" :
"sequential DCT", s->
rgb ?
"RGB" :
"",
1438 for (i = 0; i < nb_components; i++)
1443 if (CONFIG_JPEGLS_DECODER && s->
ls) {
1448 point_transform, ilv)) < 0)
1457 nb_components)) < 0)
1466 point_transform)) < 0)
1470 prev_shift, point_transform,
1471 mb_bitmask, mb_bitmask_size, reference)) < 0)
1555 int t_w, t_h, v1, v2;
1567 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1576 if (len -10 - (t_w * t_h * 3) > 0)
1577 len -= t_w * t_h * 3;
1597 "Pegasus lossless jpeg header found\n");
1617 if (
id ==
AV_RL32(
"colr") && len > 0) {
1624 if (
id ==
AV_RL32(
"xfrm") && len > 0) {
1652 }
else if (type == 1) {
1664 if (!(flags & 0x04)) {
1674 int ret,
le, ifd_offset, bytes_read;
1731 "mjpeg: error, decode_app parser read over the end\n");
1745 for (i = 0; i < len - 2; i++)
1747 if (i > 0 && cbuf[i - 1] ==
'\n')
1756 if (!strncmp(cbuf,
"AVID", 4)) {
1758 }
else if (!strcmp(cbuf,
"CS=ITU601"))
1760 else if ((!strncmp(cbuf,
"Intel(R) JPEG Library, version 1", 32) && s->
avctx->
codec_tag) ||
1761 (!strncmp(cbuf,
"Metasoft MJPEG Codec", 20)))
1780 buf_ptr = *pbuf_ptr;
1781 while (buf_end - buf_ptr > 1) {
1784 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1793 av_dlog(NULL,
"find_marker skipped %d bytes\n", skipped);
1794 *pbuf_ptr = buf_ptr;
1800 const uint8_t **unescaped_buf_ptr,
1801 int *unescaped_buf_size)
1811 if (start_code ==
SOS && !s->
ls) {
1815 while (src < buf_end) {
1821 while (src < buf_end && x == 0xff)
1824 if (x >= 0xd0 && x <= 0xd7)
1831 *unescaped_buf_ptr = s->
buffer;
1832 *unescaped_buf_size = dst - s->
buffer;
1833 memset(s->
buffer + *unescaped_buf_size, 0,
1837 (buf_end - *buf_ptr) - (dst - s->
buffer));
1838 }
else if (start_code ==
SOS && s->
ls) {
1846 while (src + t < buf_end) {
1849 while ((src + t < buf_end) && x == 0xff)
1872 *unescaped_buf_ptr = dst;
1873 *unescaped_buf_size = (bit_count + 7) >> 3;
1874 memset(s->
buffer + *unescaped_buf_size, 0,
1877 *unescaped_buf_ptr = *buf_ptr;
1878 *unescaped_buf_size = buf_end - *buf_ptr;
1889 int buf_size = avpkt->
size;
1891 const uint8_t *buf_end, *buf_ptr;
1892 const uint8_t *unescaped_buf_ptr;
1894 int unescaped_buf_size;
1905 buf_end = buf + buf_size;
1906 while (buf_ptr < buf_end) {
1910 &unescaped_buf_size);
1912 if (start_code < 0) {
1914 }
else if (unescaped_buf_size > INT_MAX / 8) {
1916 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1917 start_code, unescaped_buf_size, buf_size);
1921 start_code, buf_end - buf_ptr);
1935 if (start_code >= 0xd0 && start_code <= 0xd7)
1937 "restart marker: %d\n", start_code & 0x0f);
1939 else if (start_code >=
APP0 && start_code <=
APP15)
1942 else if (start_code ==
COM)
1947 if (!CONFIG_JPEGLS_DECODER &&
1948 (start_code ==
SOF48 || start_code ==
LSE)) {
1953 switch (start_code) {
1998 if (!CONFIG_JPEGLS_DECODER ||
2007 "Found EOI before any SOF, ignoring\n");
2025 int qpw = (s->
width + 15) / 16;
2028 memset(qp_table_buf->
data, qp, qpw);
2057 "mjpeg: unsupported coding type (%x)\n", start_code);
2064 "marker parser used %d bytes (%d bits)\n",
2093 for (p = 0; p<4; p++) {
2102 if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2];
2103 else line[w - 1] = line[(w - 1) / 2];
2104 for (index = w - 2; index > 0; index--) {
2106 ((uint16_t*)line)[
index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1;
2108 line[
index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2124 for (p = 0; p < 4; p++) {
2131 for (i = s->
height - 1; i; i--) {
2134 if (src1 == src2 || i == s->
height - 1) {
2135 memcpy(dst, src1, w);
2137 for (index = 0; index < w; index++)
2138 dst[index] = (src1[index] + src2[index]) >> 1;
2147 for (index=0; index<4; index++) {
2151 if(index && index<3){
2157 for (i=0; i<h/2; i++) {
2159 FFSWAP(
int, dst[j], dst2[j]);
2169 for (i=0; i<h; i++) {
2172 for (index=0; index<4; index++) {
2176 for (j=0; j<w; j++) {
2178 int r = dst[0][j] * k;
2179 int g = dst[1][j] * k;
2180 int b = dst[2][j] * k;
2181 dst[0][j] = g*257 >> 16;
2182 dst[1][j] = b*257 >> 16;
2183 dst[2][j] = r*257 >> 16;
2191 for (i=0; i<h; i++) {
2194 for (index=0; index<4; index++) {
2198 for (j=0; j<w; j++) {
2200 int r = (255 - dst[0][j]) * k;
2201 int g = (128 - dst[1][j]) * k;
2202 int b = (128 - dst[2][j]) * k;
2203 dst[0][j] = r*257 >> 16;
2204 dst[1][j] = (g*257 >> 16) + 128;
2205 dst[2][j] = (b*257 >> 16) + 128;
2226 return buf_ptr -
buf;
2249 for (i = 0; i < 3; i++) {
2250 for (j = 0; j < 4; j++)
2267 #if CONFIG_MJPEG_DECODER
2268 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2269 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2271 {
"extern_huff",
"Use external huffman table.",
2276 static const AVClass mjpegdec_class = {
2295 .priv_class = &mjpegdec_class,
2298 #if CONFIG_THP_DECODER