48 const uint8_t *val_table,
int nb_codes,
49 int use_static,
int is_ac)
52 uint16_t huff_code[256];
53 uint16_t huff_sym[256];
60 for (i = 0; i < 256; i++)
61 huff_sym[i] = i + 16 * is_ac;
64 huff_sym[0] = 16 * 256;
67 huff_code, 2, 2, huff_sym, 2, 2, use_static);
116 "error using external huffman table, switching back to internal\n");
149 for (i = 0; i < 64; i++) {
184 for (i = 1; i <= 16; i++) {
189 if (len < n || n > 256)
193 for (i = 0; i <
n; i++) {
204 class, index, code_max + 1);
205 if ((ret =
build_vlc(&s->
vlcs[
class][index], bits_table, val_table,
206 code_max + 1, 0,
class > 0)) < 0)
211 if ((ret =
build_vlc(&s->
vlcs[2][index], bits_table, val_table,
212 code_max + 1, 0, 0)) < 0)
255 if (nb_components <= 0 ||
261 "nb_components changing in interlaced picture\n");
265 if (s->
ls && !(s->
bits <= 8 || nb_components == 1)) {
267 "JPEG-LS that is not <= 8 "
268 "bits/component or 16-bit gray");
274 memset(h_count, 0,
sizeof(h_count));
275 memset(v_count, 0,
sizeof(v_count));
276 for (i = 0; i < nb_components; i++) {
282 if (h_count[i] > s->
h_max)
283 s->
h_max = h_count[i];
284 if (v_count[i] > s->
v_max)
285 s->
v_max = v_count[i];
291 if (!h_count[i] || !v_count[i]) {
293 "Invalid sampling factor in component %d %d:%d\n",
294 i, h_count[i], v_count[i]);
299 i, h_count[i], v_count[i],
311 || memcmp(s->
h_count, h_count,
sizeof(h_count))
312 || memcmp(s->
v_count, v_count,
sizeof(v_count))) {
316 memcpy(s->
h_count, h_count,
sizeof(h_count));
317 memcpy(s->
v_count, v_count,
sizeof(v_count));
343 if (s->
v_max == 1 && s->
h_max == 1 && s->
lossless==1 && (nb_components==3 || nb_components==4))
355 if (!(pix_fmt_id & 0xD0D0D0D0))
356 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
357 if (!(pix_fmt_id & 0x0D0D0D0D))
358 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
360 switch (pix_fmt_id) {
391 s->
upscale_h = (pix_fmt_id == 0x22122100);
400 s->
upscale_v = (pix_fmt_id == 0x22211200);
434 s->
upscale_h = (pix_fmt_id == 0x22211100) * 2 + (pix_fmt_id == 0x22112100);
448 s->
upscale_v = (pix_fmt_id == 0x22121100) + 1;
474 else if (s->
bits <= 8)
487 for (i = 0; i < 3; i++)
494 if (len != (8 + (3 * nb_components)))
505 int bw = (width + s->
h_max * 8 - 1) / (s->
h_max * 8);
506 int bh = (height + s->
v_max * 8 - 1) / (s->
v_max * 8);
526 if (code < 0 || code > 16) {
528 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
529 0, dc_index, &s->
vlcs[0][dc_index]);
541 int dc_index,
int ac_index, int16_t *quant_matrix)
547 if (val == 0xfffff) {
551 val = val * quant_matrix[0] + s->
last_dc[component];
561 i += ((unsigned)code) >> 4;
569 int sign = (~cache) >> 31;
570 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
580 block[j] = level * quant_matrix[j];
589 int component,
int dc_index,
590 int16_t *quant_matrix,
int Al)
595 if (val == 0xfffff) {
599 val = (val * quant_matrix[0] << Al) + s->
last_dc[component];
607 uint8_t *last_nnz,
int ac_index,
608 int16_t *quant_matrix,
609 int ss,
int se,
int Al,
int *EOBRUN)
620 for (i = ss; ; i++) {
624 run = ((unsigned) code) >> 4;
633 int sign = (~cache) >> 31;
634 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
642 block[j] = level * quant_matrix[j] << Al;
649 block[j] = level * quant_matrix[j] << Al;
678 #define REFINE_BIT(j) { \
679 UPDATE_CACHE(re, &s->gb); \
680 sign = block[j] >> 15; \
681 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
682 ((quant_matrix[j] ^ sign) - sign) << Al; \
683 LAST_SKIP_BITS(re, &s->gb, 1); \
691 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
696 j = s->scantable.permutated[i]; \
699 else if (run-- == 0) \
706 int ac_index, int16_t *quant_matrix,
707 int ss,
int se,
int Al,
int *EOBRUN)
709 int code, i = ss, j, sign,
val,
run;
710 int last =
FFMIN(se, *last_nnz);
721 run = ((unsigned) code) >> 4;
728 block[j] = ((quant_matrix[j]^
val) - val) << Al;
736 run = ((unsigned) code) >> 4;
757 for (; i <= last; i++) {
778 for (i = 0; i < nb_components; i++)
792 for (i = 0; i < nb_components; i++)
807 int left[4], top[4], topleft[4];
808 const int linesize = s->
linesize[0];
809 const int mask = ((1 << s->
bits) - 1) << point_transform;
825 for (i = 0; i < 4; i++)
828 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
832 ptr += linesize >> 1;
834 for (i = 0; i < 4; i++)
835 top[i] = left[i] = topleft[i] =
buffer[0][i];
837 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
845 top[i] = left[i]= topleft[i]= 1 << (s->
bits - 1);
847 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
848 modified_predictor = 1;
850 for (i=0;i<nb_components;i++) {
856 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
862 left[i] =
buffer[mb_x][i] =
863 mask & (pred + (dc << point_transform));
872 for(i=0; i<nb_components; i++) {
875 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
876 ptr[4*mb_x+3-
c] =
buffer[mb_x][i];
878 }
else if(s->
bits == 9) {
881 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
882 ((uint16_t*)ptr)[4*mb_x+
c] =
buffer[mb_x][i];
887 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
888 ptr[3*mb_x + 1] =
buffer[mb_x][0] - ((
buffer[mb_x][1] +
buffer[mb_x][2] - 0x200) >> 2);
889 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
890 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
893 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
895 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
896 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
899 for(i=0; i<nb_components; i++) {
902 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
903 ptr[3*mb_x+2-
c] =
buffer[mb_x][i];
905 }
else if(s->
bits == 9) {
908 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
909 ((uint16_t*)ptr)[3*mb_x+2-
c] =
buffer[mb_x][i];
919 int point_transform,
int nb_components)
921 int i, mb_x, mb_y,
mask;
926 point_transform += bits - s->
bits;
927 mask = ((1 << s->
bits) - 1) << point_transform;
929 av_assert0(nb_components>=1 && nb_components<=4);
931 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
932 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
939 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
940 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
941 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
942 for (i = 0; i < nb_components; i++) {
945 int n, h,
v, x,
y,
c, j, linesize;
954 if(bits>8) linesize /= 2;
966 pred= 1 << (bits - 1);
972 pred= ptr[-linesize];
974 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
979 ptr += linesize >> 1;
981 *ptr= pred + (dc << point_transform);
983 ptr16 = (uint16_t*)(s->
picture_ptr->
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
986 pred= 1 << (bits - 1);
992 pred= ptr16[-linesize];
994 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
999 ptr16 += linesize >> 1;
1001 *ptr16= pred + (dc << point_transform);
1010 for (i = 0; i < nb_components; i++) {
1013 int n, h,
v, x,
y,
c, j, linesize,
dc;
1022 if(bits>8) linesize /= 2;
1024 for (j = 0; j <
n; j++) {
1032 (linesize * (v * mb_y +
y)) +
1034 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1037 *ptr = pred + (dc << point_transform);
1039 ptr16 = (uint16_t*)(s->
picture_ptr->
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
1040 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1043 *ptr16= pred + (dc << point_transform);
1064 int linesize,
int lowres)
1069 case 1:
copy_block4(dst, src, linesize, linesize, 4);
1071 case 2:
copy_block2(dst, src, linesize, linesize, 2);
1073 case 3: *dst = *
src;
1080 int block_x, block_y;
1083 for (block_y=0; block_y<
size; block_y++)
1084 for (block_x=0; block_x<
size; block_x++)
1085 *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->
bits;
1087 for (block_y=0; block_y<
size; block_y++)
1088 for (block_x=0; block_x<
size; block_x++)
1089 *(ptr + block_x + block_y*linesize) <<= 8 - s->
bits;
1094 int Al,
const uint8_t *mb_bitmask,
1102 int bytes_per_pixel = 1 + (s->
bits > 8);
1112 for (i = 0; i < nb_components; i++) {
1115 reference_data[
c] = reference ? reference->
data[
c] : NULL;
1128 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1129 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1130 const int copy_mb = mb_bitmask && !
get_bits1(&mb_bitmask_gb);
1140 for (i = 0; i < nb_components; i++) {
1142 int n, h,
v, x,
y,
c, j;
1150 for (j = 0; j <
n; j++) {
1151 block_offset = (((linesize[
c] * (v * mb_y +
y) * 8) +
1152 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->
avctx->
lowres);
1155 block_offset += linesize[
c] >> 1;
1156 ptr = data[
c] + block_offset;
1168 "error y=%d x=%d\n", mb_y, mb_x);
1186 "error y=%d x=%d\n", mb_y, mb_x);
1190 av_dlog(s->
avctx,
"mb: %d %d processed\n", mb_y, mb_x);
1193 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1208 int se,
int Ah,
int Al)
1217 int bytes_per_pixel = 1 + (s->
bits > 8);
1220 if (se < ss || se > 63) {
1231 data += linesize >> 1;
1235 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1240 for (mb_x = 0; mb_x < s->
mb_width; mb_x++,
block++, last_nnz++) {
1247 quant_matrix, ss, se, Al, &EOBRUN);
1250 quant_matrix, ss, se, Al, &EOBRUN);
1253 "error y=%d x=%d\n", mb_y, mb_x);
1273 int len, nb_components, i, h,
v,
predictor, point_transform;
1275 const int block_size = s->
lossless ? 1 : 8;
1276 int ilv, prev_shift;
1280 "Can not process SOS before SOF, skipping\n");
1290 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1293 if (len != 6 + 2 * nb_components) {
1297 for (i = 0; i < nb_components; i++) {
1306 "decode_sos: index(%d) out of components\n", index);
1322 index = (index+2)%3;
1342 prev_shift = point_transform = 0;
1344 if (nb_components > 1) {
1348 }
else if (!s->
ls) {
1351 s->
mb_width = (s->
width + h * block_size - 1) / (h * block_size);
1360 s->
lossless ?
"lossless" :
"sequential DCT", s->
rgb ?
"RGB" :
"",
1370 for (i = 0; i < nb_components; i++)
1375 if (CONFIG_JPEGLS_DECODER && s->
ls) {
1380 point_transform, ilv)) < 0)
1389 nb_components)) < 0)
1398 point_transform)) < 0)
1402 prev_shift, point_transform,
1403 mb_bitmask, reference)) < 0)
1484 int t_w, t_h, v1, v2;
1495 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1504 if (len -10 - (t_w * t_h * 3) > 0)
1505 len -= t_w * t_h * 3;
1525 "Pegasus lossless jpeg header found\n");
1545 if (
id ==
AV_RL32(
"colr") && len > 0) {
1552 if (
id ==
AV_RL32(
"xfrm") && len > 0) {
1563 int ret,
le, ifd_offset, bytes_read;
1622 "mjpeg: error, decode_app parser read over the end\n");
1636 for (i = 0; i < len - 2; i++)
1638 if (i > 0 && cbuf[i - 1] ==
'\n')
1647 if (!strncmp(cbuf,
"AVID", 4)) {
1649 if (len > 14 && cbuf[12] == 1)
1651 }
else if (!strcmp(cbuf,
"CS=ITU601"))
1653 else if ((!strncmp(cbuf,
"Intel(R) JPEG Library, version 1", 32)) ||
1654 (!strncmp(cbuf,
"Metasoft MJPEG Codec", 20)))
1673 buf_ptr = *pbuf_ptr;
1674 while (buf_end - buf_ptr > 1) {
1677 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1686 av_dlog(NULL,
"find_marker skipped %d bytes\n", skipped);
1687 *pbuf_ptr = buf_ptr;
1693 const uint8_t **unescaped_buf_ptr,
1694 int *unescaped_buf_size)
1704 if (start_code ==
SOS && !s->
ls) {
1708 while (src < buf_end) {
1714 while (src < buf_end && x == 0xff)
1717 if (x >= 0xd0 && x <= 0xd7)
1724 *unescaped_buf_ptr = s->
buffer;
1725 *unescaped_buf_size = dst - s->
buffer;
1726 memset(s->
buffer + *unescaped_buf_size, 0,
1730 (buf_end - *buf_ptr) - (dst - s->
buffer));
1731 }
else if (start_code ==
SOS && s->
ls) {
1739 while (src + t < buf_end) {
1742 while ((src + t < buf_end) && x == 0xff)
1765 *unescaped_buf_ptr = dst;
1766 *unescaped_buf_size = (bit_count + 7) >> 3;
1767 memset(s->
buffer + *unescaped_buf_size, 0,
1770 *unescaped_buf_ptr = *buf_ptr;
1771 *unescaped_buf_size = buf_end - *buf_ptr;
1781 int buf_size = avpkt->
size;
1783 const uint8_t *buf_end, *buf_ptr;
1784 const uint8_t *unescaped_buf_ptr;
1786 int unescaped_buf_size;
1794 buf_end = buf + buf_size;
1795 while (buf_ptr < buf_end) {
1799 &unescaped_buf_size);
1801 if (start_code < 0) {
1803 }
else if (unescaped_buf_size > INT_MAX / 8) {
1805 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1806 start_code, unescaped_buf_size, buf_size);
1810 start_code, buf_end - buf_ptr);
1824 if (start_code >= 0xd0 && start_code <= 0xd7)
1826 "restart marker: %d\n", start_code & 0x0f);
1828 else if (start_code >=
APP0 && start_code <=
APP15)
1831 else if (start_code ==
COM)
1836 if (!CONFIG_JPEGLS_DECODER &&
1837 (start_code ==
SOF48 || start_code ==
LSE)) {
1842 switch (start_code) {
1887 if (!CONFIG_JPEGLS_DECODER ||
1896 "Found EOI before any SOF, ignoring\n");
1914 int qpw = (s->
width + 15) / 16;
1917 memset(qp_table_buf->
data, qp, qpw);
1946 "mjpeg: unsupported coding type (%x)\n", start_code);
1953 "marker parser used %d bytes (%d bits)\n",
1973 for (index = s->
width - 1; index; index--)
1974 line[
index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
1982 w = s->
width >> hshift;
1987 for (i = s->
height - 1; i; i--) {
1991 memcpy(dst, src1, w);
1993 for (index = 0; index < w; index++)
1994 dst[index] = (src1[index] + src2[index]) >> 1;
2002 for (index=0; index<4; index++) {
2006 if(index && index<3){
2012 for (i=0; i<h/2; i++) {
2014 FFSWAP(
int, dst[j], dst2[j]);
2028 return buf_ptr -
buf;
2050 for (i = 0; i < 3; i++) {
2051 for (j = 0; j < 4; j++)
2068 #if CONFIG_MJPEG_DECODER
2069 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2070 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2072 {
"extern_huff",
"Use external huffman table.",
2077 static const AVClass mjpegdec_class = {
2096 .priv_class = &mjpegdec_class,
2099 #if CONFIG_THP_DECODER