45 const uint8_t *val_table,
int nb_codes,
46 int use_static,
int is_ac)
49 uint16_t huff_code[256];
50 uint16_t huff_sym[256];
57 for (i = 0; i < 256; i++)
58 huff_sym[i] = i + 16 * is_ac;
61 huff_sym[0] = 16 * 256;
64 huff_code, 2, 2, huff_sym, 2, 2, use_static);
110 "error using external huffman table, switching back to internal\n");
143 for (i = 0; i < 64; i++) {
178 for (i = 1; i <= 16; i++) {
183 if (len < n || n > 256)
187 for (i = 0; i <
n; i++) {
198 class, index, code_max + 1);
199 if ((ret =
build_vlc(&s->
vlcs[
class][index], bits_table, val_table,
200 code_max + 1, 0,
class > 0)) < 0)
205 if ((ret =
build_vlc(&s->
vlcs[2][index], bits_table, val_table,
206 code_max + 1, 0, 0)) < 0)
253 if (nb_components <= 0 ||
262 if (s->
ls && !(s->
bits <= 8 || nb_components == 1)) {
264 "JPEG-LS that is not <= 8 "
265 "bits/component or 16-bit gray");
271 memset(h_count, 0,
sizeof(h_count));
272 memset(v_count, 0,
sizeof(v_count));
273 for (i = 0; i < nb_components; i++) {
279 if (h_count[i] > s->
h_max)
280 s->
h_max = h_count[i];
281 if (v_count[i] > s->
v_max)
282 s->
v_max = v_count[i];
288 if (!h_count[i] || !v_count[i]) {
290 "Invalid sampling factor in component %d %d:%d\n",
291 i, h_count[i], v_count[i]);
296 i, h_count[i], v_count[i],
308 || memcmp(s->
h_count, h_count,
sizeof(h_count))
309 || memcmp(s->
v_count, v_count,
sizeof(v_count))) {
313 memcpy(s->
h_count, h_count,
sizeof(h_count));
314 memcpy(s->
v_count, v_count,
sizeof(v_count));
352 if (!(pix_fmt_id & 0xD0D0D0D0))
353 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
354 if (!(pix_fmt_id & 0x0D0D0D0D))
355 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
357 switch (pix_fmt_id) {
376 s->
upscale_h = (pix_fmt_id == 0x22122100);
383 s->
upscale_v = (pix_fmt_id == 0x22211200);
413 s->
upscale_h = (pix_fmt_id == 0x22211100) * 2 + (pix_fmt_id == 0x22112100);
424 s->
upscale_v = (pix_fmt_id == 0x22121100) + 1;
446 else if (s->
bits <= 8)
459 for (i = 0; i < 3; i++)
466 if (len != (8 + (3 * nb_components)))
477 int bw = (width + s->
h_max * 8 - 1) / (s->
h_max * 8);
478 int bh = (height + s->
v_max * 8 - 1) / (s->
v_max * 8);
496 if (code < 0 || code > 16) {
498 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
499 0, dc_index, &s->
vlcs[0][dc_index]);
511 int dc_index,
int ac_index, int16_t *quant_matrix)
521 val = val * quant_matrix[0] + s->
last_dc[component];
531 i += ((unsigned)code) >> 4;
539 int sign = (~cache) >> 31;
540 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
550 block[j] = level * quant_matrix[j];
559 int component,
int dc_index,
560 int16_t *quant_matrix,
int Al)
569 val = (val * quant_matrix[0] << Al) + s->
last_dc[component];
577 uint8_t *last_nnz,
int ac_index,
578 int16_t *quant_matrix,
579 int ss,
int se,
int Al,
int *EOBRUN)
590 for (i = ss; ; i++) {
594 run = ((unsigned) code) >> 4;
603 int sign = (~cache) >> 31;
604 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
612 block[j] = level * quant_matrix[j] << Al;
619 block[j] = level * quant_matrix[j] << Al;
648 #define REFINE_BIT(j) { \
649 UPDATE_CACHE(re, &s->gb); \
650 sign = block[j] >> 15; \
651 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
652 ((quant_matrix[j] ^ sign) - sign) << Al; \
653 LAST_SKIP_BITS(re, &s->gb, 1); \
661 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
666 j = s->scantable.permutated[i]; \
669 else if (run-- == 0) \
676 int ac_index, int16_t *quant_matrix,
677 int ss,
int se,
int Al,
int *EOBRUN)
679 int code, i = ss, j, sign,
val,
run;
680 int last =
FFMIN(se, *last_nnz);
691 run = ((unsigned) code) >> 4;
698 block[j] = ((quant_matrix[j]^
val) - val) << Al;
706 run = ((unsigned) code) >> 4;
727 for (; i <= last; i++) {
748 for (i = 0; i < nb_components; i++)
762 for (i = 0; i < nb_components; i++)
777 int left[3], top[3], topleft[3];
778 const int linesize = s->
linesize[0];
779 const int mask = (1 << s->
bits) - 1;
789 for (i = 0; i < 3; i++)
792 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
796 ptr += linesize >> 1;
798 for (i = 0; i < 3; i++)
799 top[i] = left[i] = topleft[i] =
buffer[0][i];
801 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
809 top[i] = left[i]= topleft[i]= 1 << (s->
bits - 1);
811 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
812 modified_predictor = 1;
814 for (i=0;i<nb_components;i++) {
820 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
826 left[i] =
buffer[mb_x][i] =
827 mask & (pred + (dc << point_transform));
837 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
838 ptr[3*mb_x + 1] =
buffer[mb_x][0] - ((
buffer[mb_x][1] +
buffer[mb_x][2] - 0x200) >> 2);
839 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
840 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
843 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
845 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
846 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
849 for(i=0; i<nb_components; i++) {
851 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
852 ptr[3*mb_x+2-
c] =
buffer[mb_x][i];
861 int point_transform,
int nb_components)
868 point_transform += bits - s->
bits;
870 av_assert0(nb_components>=1 && nb_components<=3);
872 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
873 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
880 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
881 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
882 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
883 for (i = 0; i < nb_components; i++) {
886 int n, h,
v, x,
y,
c, j, linesize;
895 if(bits>8) linesize /= 2;
904 ptr = s->
picture.
data[
c] + (linesize * (v * mb_y +
y)) + (h * mb_x + x);
907 pred= 1 << (bits - 1);
913 pred= ptr[-linesize];
915 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
920 ptr += linesize >> 1;
921 pred &= (-1)<<(8-s->
bits);
922 *ptr= pred + (dc << point_transform);
924 ptr16 = (uint16_t*)(s->
picture.
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
927 pred= 1 << (bits - 1);
933 pred= ptr16[-linesize];
935 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
940 ptr16 += linesize >> 1;
941 pred &= (-1)<<(16-s->
bits);
942 *ptr16= pred + (dc << point_transform);
951 for (i = 0; i < nb_components; i++) {
954 int n, h,
v, x,
y,
c, j, linesize,
dc;
963 if(bits>8) linesize /= 2;
965 for (j = 0; j <
n; j++) {
973 (linesize * (v * mb_y +
y)) +
975 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
977 pred &= (-1)<<(8-s->
bits);
978 *ptr = pred + (dc << point_transform);
980 ptr16 = (uint16_t*)(s->
picture.
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
981 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
983 pred &= (-1)<<(16-s->
bits);
984 *ptr16= pred + (dc << point_transform);
1005 int linesize,
int lowres)
1010 case 1:
copy_block4(dst, src, linesize, linesize, 4);
1012 case 2:
copy_block2(dst, src, linesize, linesize, 2);
1014 case 3: *dst = *
src;
1020 int Al,
const uint8_t *mb_bitmask,
1037 for (i = 0; i < nb_components; i++) {
1040 reference_data[
c] = reference ? reference->
data[
c] : NULL;
1053 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1054 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1055 const int copy_mb = mb_bitmask && !
get_bits1(&mb_bitmask_gb);
1065 for (i = 0; i < nb_components; i++) {
1067 int n, h,
v, x,
y,
c, j;
1075 for (j = 0; j <
n; j++) {
1076 block_offset = (((linesize[
c] * (v * mb_y +
y) * 8) +
1080 block_offset += linesize[
c] >> 1;
1081 ptr = data[
c] + block_offset;
1093 "error y=%d x=%d\n", mb_y, mb_x);
1109 "error y=%d x=%d\n", mb_y, mb_x);
1113 av_dlog(s->
avctx,
"mb: %d %d processed\n", mb_y, mb_x);
1116 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1131 int se,
int Ah,
int Al)
1142 if (se < ss || se > 63) {
1153 data += linesize >> 1;
1157 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1162 for (mb_x = 0; mb_x < s->
mb_width; mb_x++,
block++, last_nnz++) {
1169 quant_matrix, ss, se, Al, &EOBRUN);
1172 quant_matrix, ss, se, Al, &EOBRUN);
1175 "error y=%d x=%d\n", mb_y, mb_x);
1193 int len, nb_components, i, h,
v,
predictor, point_transform;
1195 const int block_size = s->
lossless ? 1 : 8;
1196 int ilv, prev_shift;
1200 "Can not process SOS before SOF, skipping\n");
1210 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1213 if (len != 6 + 2 * nb_components) {
1217 for (i = 0; i < nb_components; i++) {
1226 "decode_sos: index(%d) out of components\n", index);
1237 index = (index+2)%3;
1261 prev_shift = point_transform = 0;
1263 if (nb_components > 1) {
1267 }
else if (!s->
ls) {
1270 s->
mb_width = (s->
width + h * block_size - 1) / (h * block_size);
1279 s->
lossless ?
"lossless" :
"sequential DCT", s->
rgb ?
"RGB" :
"",
1289 for (i = 0; i < nb_components; i++)
1294 if (CONFIG_JPEGLS_DECODER && s->
ls) {
1299 point_transform, ilv)) < 0)
1308 nb_components)) < 0)
1317 point_transform)) < 0)
1321 prev_shift, point_transform,
1322 mb_bitmask, reference)) < 0)
1403 int t_w, t_h, v1, v2;
1414 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1423 if (len -10 - (t_w * t_h * 3) > 0)
1424 len -= t_w * t_h * 3;
1444 "Pegasus lossless jpeg header found\n");
1490 "mjpeg: error, decode_app parser read over the end\n");
1504 for (i = 0; i < len - 2; i++)
1506 if (i > 0 && cbuf[i - 1] ==
'\n')
1515 if (!strncmp(cbuf,
"AVID", 4)) {
1517 if (len > 14 && cbuf[12] == 1)
1519 }
else if (!strcmp(cbuf,
"CS=ITU601"))
1521 else if ((!strncmp(cbuf,
"Intel(R) JPEG Library, version 1", 32)) ||
1522 (!strncmp(cbuf,
"Metasoft MJPEG Codec", 20)))
1541 buf_ptr = *pbuf_ptr;
1542 while (buf_ptr < buf_end) {
1545 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1553 av_dlog(NULL,
"find_marker skipped %d bytes\n", skipped);
1554 *pbuf_ptr = buf_ptr;
1560 const uint8_t **unescaped_buf_ptr,
1561 int *unescaped_buf_size)
1571 if (start_code ==
SOS && !s->
ls) {
1575 while (src < buf_end) {
1581 while (src < buf_end && x == 0xff)
1584 if (x >= 0xd0 && x <= 0xd7)
1591 *unescaped_buf_ptr = s->
buffer;
1592 *unescaped_buf_size = dst - s->
buffer;
1593 memset(s->
buffer + *unescaped_buf_size, 0,
1597 (buf_end - *buf_ptr) - (dst - s->
buffer));
1598 }
else if (start_code ==
SOS && s->
ls) {
1608 while (src + t < buf_end) {
1611 while ((src + t < buf_end) && x == 0xff)
1634 *unescaped_buf_ptr = dst;
1635 *unescaped_buf_size = (bit_count + 7) >> 3;
1636 memset(s->
buffer + *unescaped_buf_size, 0,
1639 *unescaped_buf_ptr = *buf_ptr;
1640 *unescaped_buf_size = buf_end - *buf_ptr;
1650 int buf_size = avpkt->
size;
1652 const uint8_t *buf_end, *buf_ptr;
1653 const uint8_t *unescaped_buf_ptr;
1655 int unescaped_buf_size;
1661 buf_end = buf + buf_size;
1662 while (buf_ptr < buf_end) {
1666 &unescaped_buf_size);
1668 if (start_code < 0) {
1670 }
else if (unescaped_buf_size > INT_MAX / 8) {
1672 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1673 start_code, unescaped_buf_size, buf_size);
1677 start_code, buf_end - buf_ptr);
1691 if (start_code >= 0xd0 && start_code <= 0xd7)
1693 "restart marker: %d\n", start_code & 0x0f);
1695 else if (start_code >=
APP0 && start_code <=
APP15)
1698 else if (start_code ==
COM)
1703 if (!CONFIG_JPEGLS_DECODER &&
1704 (start_code ==
SOF48 || start_code ==
LSE)) {
1709 switch (start_code) {
1754 if (!CONFIG_JPEGLS_DECODER ||
1763 "Found EOI before any SOF, ignoring\n");
1781 int qpw = (s->
width + 15) / 16;
1784 memset(qp_table_buf->
data, qp, qpw);
1812 "mjpeg: unsupported coding type (%x)\n", start_code);
1819 "marker parser used %d bytes (%d bits)\n",
1839 for (index = s->
width - 1; index; index--)
1840 line[
index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
1848 w = s->
width >> hshift;
1853 for (i = s->
height - 1; i; i--) {
1857 memcpy(dst, src1, w);
1859 for (index = 0; index < w; index++)
1860 dst[index] = (src1[index] + src2[index]) >> 1;
1868 for (index=0; index<4; index++) {
1872 if(index && index<3){
1878 for (i=0; i<h/2; i++) {
1880 FFSWAP(
int, dst[j], dst2[j]);
1891 return buf_ptr -
buf;
1906 for (i = 0; i < 3; i++) {
1907 for (j = 0; j < 4; j++)
1923 #if CONFIG_MJPEG_DECODER
1924 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1925 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1927 {
"extern_huff",
"Use external huffman table.",
1932 static const AVClass mjpegdec_class = {
1951 .priv_class = &mjpegdec_class,
1954 #if CONFIG_THP_DECODER