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);
109 "error using external huffman table, switching back to internal\n");
142 for (i = 0; i < 64; i++) {
160 int len,
index, i,
class, n,
v, code_max;
177 for (i = 1; i <= 16; i++) {
182 if (len < n || n > 256)
186 for (i = 0; i < n; i++) {
197 class, index, code_max + 1);
198 if ((ret =
build_vlc(&s->
vlcs[
class][index], bits_table, val_table,
199 code_max + 1, 0,
class > 0)) < 0)
204 if ((ret =
build_vlc(&s->
vlcs[2][index], bits_table, val_table,
205 code_max + 1, 0, 0)) < 0)
252 if (nb_components <= 0 ||
261 if (s->
ls && !(s->
bits <= 8 || nb_components == 1)) {
263 "For JPEG-LS anything except <= 8 bits/component"
264 " or 16-bit gray", 0);
270 memset(h_count, 0,
sizeof(h_count));
271 memset(v_count, 0,
sizeof(v_count));
272 for (i = 0; i < nb_components; i++) {
278 if (h_count[i] > s->
h_max)
279 s->
h_max = h_count[i];
280 if (v_count[i] > s->
v_max)
281 s->
v_max = v_count[i];
282 if (!h_count[i] || !v_count[i]) {
292 i, h_count[i], v_count[i],
304 || memcmp(s->
h_count, h_count,
sizeof(h_count))
305 || memcmp(s->
v_count, v_count,
sizeof(v_count))) {
310 memcpy(s->
h_count, h_count,
sizeof(h_count));
311 memcpy(s->
v_count, v_count,
sizeof(v_count));
350 if (!(pix_fmt_id & 0xD0D0D0D0))
351 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
352 if (!(pix_fmt_id & 0x0D0D0D0D))
353 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
355 switch (pix_fmt_id) {
374 s->
upscale_h = (pix_fmt_id == 0x22122100);
381 s->
upscale_v = (pix_fmt_id == 0x22211200);
411 s->
upscale_h = (pix_fmt_id == 0x22211100) * 2 + (pix_fmt_id == 0x22112100);
422 s->
upscale_v = (pix_fmt_id == 0x22121100) + 1;
440 else if (s->
bits <= 8)
457 for (i = 0; i < 3; i++)
464 if (len != (8 + (3 * nb_components)))
475 int bw = (width + s->
h_max * 8 - 1) / (s->
h_max * 8);
476 int bh = (height + s->
v_max * 8 - 1) / (s->
v_max * 8);
496 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
497 0, dc_index, &s->
vlcs[0][dc_index]);
509 int dc_index,
int ac_index, int16_t *quant_matrix)
511 int code, i, j,
level, val;
519 val = val * quant_matrix[0] + s->
last_dc[component];
529 i += ((unsigned)code) >> 4;
537 int sign = (~cache) >> 31;
538 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
548 block[j] = level * quant_matrix[j];
557 int component,
int dc_index,
558 int16_t *quant_matrix,
int Al)
567 val = (val * quant_matrix[0] << Al) + s->
last_dc[component];
575 uint8_t *last_nnz,
int ac_index,
576 int16_t *quant_matrix,
577 int ss,
int se,
int Al,
int *EOBRUN)
588 for (i = ss; ; i++) {
592 run = ((unsigned) code) >> 4;
601 int sign = (~cache) >> 31;
602 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
610 block[j] = level * quant_matrix[j] << Al;
617 block[j] = level * quant_matrix[j] << Al;
646 #define REFINE_BIT(j) { \
647 UPDATE_CACHE(re, &s->gb); \
648 sign = block[j] >> 15; \
649 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
650 ((quant_matrix[j] ^ sign) - sign) << Al; \
651 LAST_SKIP_BITS(re, &s->gb, 1); \
659 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
664 j = s->scantable.permutated[i]; \
667 else if (run-- == 0) \
674 int ac_index, int16_t *quant_matrix,
675 int ss,
int se,
int Al,
int *EOBRUN)
677 int code, i = ss, j, sign, val,
run;
678 int last =
FFMIN(se, *last_nnz);
689 run = ((unsigned) code) >> 4;
696 block[j] = ((quant_matrix[j]^val) - val) << Al;
704 run = ((unsigned) code) >> 4;
725 for (; i <= last; i++) {
744 for (i = 0; i < nb_components; i++)
758 for (i = 0; i < nb_components; i++)
771 int left[3], top[3], topleft[3];
772 const int linesize = s->
linesize[0];
773 const int mask = (1 << s->
bits) - 1;
783 for (i = 0; i < 3; i++)
786 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
790 ptr += linesize >> 1;
792 for (i = 0; i < 3; i++)
793 top[i] = left[i] = topleft[i] =
buffer[0][i];
795 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
803 top[i] = left[i]= topleft[i]= 1 << (s->
bits - 1);
805 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
806 modified_predictor = 1;
808 for (i=0;i<nb_components;i++) {
814 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
820 left[i] =
buffer[mb_x][i] =
821 mask & (pred + (dc << point_transform));
831 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
832 ptr[3*mb_x + 1] =
buffer[mb_x][0] - ((
buffer[mb_x][1] +
buffer[mb_x][2] - 0x200) >> 2);
833 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
834 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
837 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
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(i=0; i<nb_components; i++) {
845 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
846 ptr[3*mb_x+2-
c] =
buffer[mb_x][i];
862 point_transform += bits - s->
bits;
864 av_assert0(nb_components>=1 && nb_components<=3);
866 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
867 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
874 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
875 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
876 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
877 for (i = 0; i < nb_components; i++) {
880 int n, h,
v, x,
y,
c, j, linesize;
889 if(bits>8) linesize /= 2;
898 ptr = s->
picture.
data[
c] + (linesize * (v * mb_y +
y)) + (h * mb_x + x);
901 pred= 1 << (bits - 1);
907 pred= ptr[-linesize];
909 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
914 ptr += linesize >> 1;
915 pred &= (-1)<<(8-s->
bits);
916 *ptr= pred + (dc << point_transform);
918 ptr16 = (uint16_t*)(s->
picture.
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
921 pred= 1 << (bits - 1);
927 pred= ptr16[-linesize];
929 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
934 ptr16 += linesize >> 1;
935 pred &= (-1)<<(16-s->
bits);
936 *ptr16= pred + (dc << point_transform);
945 for (i = 0; i < nb_components; i++) {
948 int n, h,
v, x,
y,
c, j, linesize,
dc;
957 if(bits>8) linesize /= 2;
959 for (j = 0; j < n; j++) {
967 (linesize * (v * mb_y +
y)) +
969 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
971 pred &= (-1)<<(8-s->
bits);
972 *ptr = pred + (dc << point_transform);
974 ptr16 = (uint16_t*)(s->
picture.
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
975 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
977 pred &= (-1)<<(16-s->
bits);
978 *ptr16= pred + (dc << point_transform);
1004 case 1:
copy_block4(dst, src, linesize, linesize, 4);
1006 case 2:
copy_block2(dst, src, linesize, linesize, 2);
1008 case 3: *dst = *src;
1014 int Al,
const uint8_t *mb_bitmask,
1031 for (i = 0; i < nb_components; i++) {
1034 reference_data[
c] = reference ? reference->
data[
c] :
NULL;
1047 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1048 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1049 const int copy_mb = mb_bitmask && !
get_bits1(&mb_bitmask_gb);
1059 for (i = 0; i < nb_components; i++) {
1061 int n, h,
v, x,
y,
c, j;
1069 for (j = 0; j < n; j++) {
1070 block_offset = (((linesize[
c] * (v * mb_y +
y) * 8) +
1074 block_offset += linesize[
c] >> 1;
1075 ptr = data[
c] + block_offset;
1087 "error y=%d x=%d\n", mb_y, mb_x);
1103 "error y=%d x=%d\n", mb_y, mb_x);
1107 av_dlog(s->
avctx,
"mb: %d %d processed\n", mb_y, mb_x);
1110 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1125 int se,
int Ah,
int Al)
1146 data += linesize >> 1;
1150 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1155 for (mb_x = 0; mb_x < s->
mb_width; mb_x++,
block++, last_nnz++) {
1162 quant_matrix, ss, se, Al, &EOBRUN);
1165 quant_matrix, ss, se, Al, &EOBRUN);
1168 "error y=%d x=%d\n", mb_y, mb_x);
1185 int len, nb_components, i, h,
v,
predictor, point_transform;
1187 const int block_size = s->
lossless ? 1 : 8;
1188 int ilv, prev_shift;
1192 "Can not process SOS before SOF, skipping\n");
1202 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1205 if (len != 6 + 2 * nb_components) {
1209 for (i = 0; i < nb_components; i++) {
1218 "decode_sos: index(%d) out of components\n", index);
1229 index = (index+2)%3;
1253 prev_shift = point_transform = 0;
1255 if (nb_components > 1) {
1259 }
else if (!s->
ls) {
1262 s->
mb_width = (s->
width + h * block_size - 1) / (h * block_size);
1271 s->
lossless ?
"lossless" :
"sequential DCT", s->
rgb ?
"RGB" :
"",
1281 for (i = 0; i < nb_components; i++)
1286 if (CONFIG_JPEGLS_DECODER && s->
ls) {
1291 point_transform, ilv)) < 0)
1307 point_transform)) < 0)
1311 prev_shift, point_transform,
1312 mb_bitmask, reference)) < 0)
1393 int t_w, t_h, v1, v2;
1404 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1413 if (len -10 - (t_w * t_h * 3) > 0)
1414 len -= t_w * t_h * 3;
1434 "Pegasus lossless jpeg header found\n");
1480 "mjpeg: error, decode_app parser read over the end\n");
1494 for (i = 0; i < len - 2; i++)
1496 if (i > 0 && cbuf[i - 1] ==
'\n')
1505 if (!strcmp(cbuf,
"AVID")) {
1507 }
else if (!strcmp(cbuf,
"CS=ITU601"))
1509 else if ((len > 31 && !strncmp(cbuf,
"Intel(R) JPEG Library, version 1", 32)) ||
1510 (len > 19 && !strncmp(cbuf,
"Metasoft MJPEG Codec", 20)))
1529 buf_ptr = *pbuf_ptr;
1530 while (buf_ptr < buf_end) {
1533 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1541 av_dlog(
NULL,
"find_marker skipped %d bytes\n", skipped);
1542 *pbuf_ptr = buf_ptr;
1548 const uint8_t **unescaped_buf_ptr,
1549 int *unescaped_buf_size)
1559 if (start_code ==
SOS && !s->
ls) {
1560 const uint8_t *src = *buf_ptr;
1563 while (src < buf_end) {
1569 while (src < buf_end && x == 0xff)
1572 if (x >= 0xd0 && x <= 0xd7)
1579 *unescaped_buf_ptr = s->
buffer;
1580 *unescaped_buf_size = dst - s->
buffer;
1581 memset(s->
buffer + *unescaped_buf_size, 0,
1585 (buf_end - *buf_ptr) - (dst - s->
buffer));
1586 }
else if (start_code ==
SOS && s->
ls) {
1587 const uint8_t *src = *buf_ptr;
1596 while (src + t < buf_end) {
1599 while ((src + t < buf_end) && x == 0xff)
1622 *unescaped_buf_ptr =
dst;
1623 *unescaped_buf_size = (bit_count + 7) >> 3;
1624 memset(s->
buffer + *unescaped_buf_size, 0,
1627 *unescaped_buf_ptr = *buf_ptr;
1628 *unescaped_buf_size = buf_end - *buf_ptr;
1638 int buf_size = avpkt->
size;
1640 const uint8_t *buf_end, *buf_ptr;
1641 const uint8_t *unescaped_buf_ptr;
1643 int unescaped_buf_size;
1650 buf_end = buf + buf_size;
1651 while (buf_ptr < buf_end) {
1655 &unescaped_buf_size);
1657 if (start_code < 0) {
1659 }
else if (unescaped_buf_size > (1
U<<28)) {
1660 av_log(avctx,
AV_LOG_ERROR,
"MJPEG packet 0x%x too big (0x%x/0x%x), corrupt data?\n",
1661 start_code, unescaped_buf_size, buf_size);
1665 start_code, buf_end - buf_ptr);
1674 if (start_code >= 0xd0 && start_code <= 0xd7)
1676 "restart marker: %d\n", start_code & 0x0f);
1678 else if (start_code >=
APP0 && start_code <=
APP15)
1681 else if (start_code ==
COM)
1685 switch (start_code) {
1730 if (!CONFIG_JPEGLS_DECODER ||
1739 "Found EOI before any SOF, ignoring\n");
1759 (s->
width + 15) / 16);
1762 "QP: %d\n", picture->
quality);
1786 "mjpeg: unsupported coding type (%x)\n", start_code);
1793 "marker parser used %d bytes (%d bits)\n",
1814 for (index = s->
width - 1; index; index--)
1815 line[
index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
1823 w = s->
width >> hshift;
1828 for (i = s->
height - 1; i; i--) {
1832 memcpy(dst, src1, w);
1834 for (index = 0; index < w; index++)
1835 dst[index] = (src1[index] + src2[index]) >> 1;
1843 for (index=0; index<4; index++) {
1847 if(index && index<3){
1848 w = -((-w) >> hshift);
1849 h = -((-h) >> vshift);
1853 for (i=0; i<h/2; i++) {
1855 FFSWAP(
int, dst[j], dst2[j]);
1866 return buf_ptr - buf;
1882 for (i = 0; i < 3; i++) {
1883 for (j = 0; j < 4; j++)
1899 #if CONFIG_MJPEG_DECODER
1900 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1901 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1903 {
"extern_huff",
"Use external huffman table.",
1908 static const AVClass mjpegdec_class = {
1927 .priv_class = &mjpegdec_class,
1930 #if CONFIG_THP_DECODER