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);
89 if (len > 14 && buf[12] == 1)
91 if (len > 14 && buf[12] == 2)
127 "error using external huffman table, switching back to internal\n");
170 for (i = 0; i < 64; i++) {
205 for (i = 1; i <= 16; i++) {
210 if (len < n || n > 256)
214 for (i = 0; i <
n; i++) {
225 class, index, code_max + 1);
226 if ((ret =
build_vlc(&s->
vlcs[
class][index], bits_table, val_table,
227 code_max + 1, 0,
class > 0)) < 0)
232 if ((ret =
build_vlc(&s->
vlcs[2][index], bits_table, val_table,
233 code_max + 1, 0, 0)) < 0)
276 if (nb_components <= 0 ||
282 "nb_components changing in interlaced picture\n");
286 if (s->
ls && !(s->
bits <= 8 || nb_components == 1)) {
288 "JPEG-LS that is not <= 8 "
289 "bits/component or 16-bit gray");
295 memset(h_count, 0,
sizeof(h_count));
296 memset(v_count, 0,
sizeof(v_count));
297 for (i = 0; i < nb_components; i++) {
303 if (h_count[i] > s->
h_max)
304 s->
h_max = h_count[i];
305 if (v_count[i] > s->
v_max)
306 s->
v_max = v_count[i];
312 if (!h_count[i] || !v_count[i]) {
314 "Invalid sampling factor in component %d %d:%d\n",
315 i, h_count[i], v_count[i]);
320 i, h_count[i], v_count[i],
332 || memcmp(s->
h_count, h_count,
sizeof(h_count))
333 || memcmp(s->
v_count, v_count,
sizeof(v_count))) {
337 memcpy(s->
h_count, h_count,
sizeof(h_count));
338 memcpy(s->
v_count, v_count,
sizeof(v_count));
366 if (s->
v_max == 1 && s->
h_max == 1 && s->
lossless==1 && (nb_components==3 || nb_components==4))
378 if (!(pix_fmt_id & 0xD0D0D0D0))
379 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
380 if (!(pix_fmt_id & 0x0D0D0D0D))
381 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
383 switch (pix_fmt_id) {
418 s->
upscale_h = (pix_fmt_id == 0x22122100);
427 s->
upscale_v = (pix_fmt_id == 0x22211200);
461 s->
upscale_h = (pix_fmt_id == 0x22211100) * 2 + (pix_fmt_id == 0x22112100);
475 s->
upscale_v = (pix_fmt_id == 0x22121100) + 1;
501 else if (s->
bits <= 8)
520 for (i = 0; i < 4; i++)
527 if (len != (8 + (3 * nb_components)))
538 int bw = (width + s->
h_max * 8 - 1) / (s->
h_max * 8);
539 int bh = (height + s->
v_max * 8 - 1) / (s->
v_max * 8);
559 if (code < 0 || code > 16) {
561 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
562 0, dc_index, &s->
vlcs[0][dc_index]);
574 int dc_index,
int ac_index, int16_t *quant_matrix)
580 if (val == 0xfffff) {
584 val = val * quant_matrix[0] + s->
last_dc[component];
594 i += ((unsigned)code) >> 4;
602 int sign = (~cache) >> 31;
603 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
613 block[j] = level * quant_matrix[j];
622 int component,
int dc_index,
623 int16_t *quant_matrix,
int Al)
628 if (val == 0xfffff) {
632 val = (val * quant_matrix[0] << Al) + s->
last_dc[component];
640 uint8_t *last_nnz,
int ac_index,
641 int16_t *quant_matrix,
642 int ss,
int se,
int Al,
int *EOBRUN)
653 for (i = ss; ; i++) {
657 run = ((unsigned) code) >> 4;
666 int sign = (~cache) >> 31;
667 level = (
NEG_USR32(sign ^ cache,code) ^ sign) - sign;
675 block[j] = level * quant_matrix[j] << Al;
682 block[j] = level * quant_matrix[j] << Al;
711 #define REFINE_BIT(j) { \
712 UPDATE_CACHE(re, &s->gb); \
713 sign = block[j] >> 15; \
714 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
715 ((quant_matrix[j] ^ sign) - sign) << Al; \
716 LAST_SKIP_BITS(re, &s->gb, 1); \
724 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
729 j = s->scantable.permutated[i]; \
732 else if (run-- == 0) \
739 int ac_index, int16_t *quant_matrix,
740 int ss,
int se,
int Al,
int *EOBRUN)
742 int code, i = ss, j, sign,
val,
run;
743 int last =
FFMIN(se, *last_nnz);
754 run = ((unsigned) code) >> 4;
761 block[j] = ((quant_matrix[j]^
val) - val) << Al;
769 run = ((unsigned) code) >> 4;
790 for (; i <= last; i++) {
811 for (i = 0; i < nb_components; i++)
825 for (i = 0; i < nb_components; i++)
840 int left[4], top[4], topleft[4];
841 const int linesize = s->
linesize[0];
842 const int mask = ((1 << s->
bits) - 1) << point_transform;
858 for (i = 0; i < 4; i++)
861 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
865 ptr += linesize >> 1;
867 for (i = 0; i < 4; i++)
868 top[i] = left[i] = topleft[i] =
buffer[0][i];
870 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
878 top[i] = left[i]= topleft[i]= 1 << (s->
bits - 1);
880 if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
881 modified_predictor = 1;
883 for (i=0;i<nb_components;i++) {
889 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
895 left[i] =
buffer[mb_x][i] =
896 mask & (pred + (dc << point_transform));
905 for(i=0; i<nb_components; i++) {
908 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
909 ptr[4*mb_x+3-
c] =
buffer[mb_x][i];
911 }
else if(s->
bits == 9) {
914 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
915 ((uint16_t*)ptr)[4*mb_x+
c] =
buffer[mb_x][i];
920 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
921 ptr[3*mb_x + 1] =
buffer[mb_x][0] - ((
buffer[mb_x][1] +
buffer[mb_x][2] - 0x200) >> 2);
922 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
923 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
926 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
928 ptr[3*mb_x + 0] =
buffer[mb_x][1] + ptr[3*mb_x + 1];
929 ptr[3*mb_x + 2] =
buffer[mb_x][2] + ptr[3*mb_x + 1];
932 for(i=0; i<nb_components; i++) {
935 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
936 ptr[3*mb_x+2-
c] =
buffer[mb_x][i];
938 }
else if(s->
bits == 9) {
941 for(mb_x = 0; mb_x < s->
mb_width; mb_x++) {
942 ((uint16_t*)ptr)[3*mb_x+2-
c] =
buffer[mb_x][i];
952 int point_transform,
int nb_components)
954 int i, mb_x, mb_y,
mask;
959 point_transform += bits - s->
bits;
960 mask = ((1 << s->
bits) - 1) << point_transform;
962 av_assert0(nb_components>=1 && nb_components<=4);
964 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
965 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
972 if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
973 int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
974 int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
975 for (i = 0; i < nb_components; i++) {
978 int n, h,
v, x,
y,
c, j, linesize;
987 if(bits>8) linesize /= 2;
999 pred= 1 << (bits - 1);
1004 if(x==0 && leftcol){
1005 pred= ptr[-linesize];
1007 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1012 ptr += linesize >> 1;
1014 *ptr= pred + (dc << point_transform);
1016 ptr16 = (uint16_t*)(s->
picture_ptr->
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
1018 if(x==0 && leftcol){
1019 pred= 1 << (bits - 1);
1024 if(x==0 && leftcol){
1025 pred= ptr16[-linesize];
1027 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1032 ptr16 += linesize >> 1;
1034 *ptr16= pred + (dc << point_transform);
1043 for (i = 0; i < nb_components; i++) {
1046 int n, h,
v, x,
y,
c, j, linesize,
dc;
1055 if(bits>8) linesize /= 2;
1057 for (j = 0; j <
n; j++) {
1065 (linesize * (v * mb_y +
y)) +
1067 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1070 *ptr = pred + (dc << point_transform);
1072 ptr16 = (uint16_t*)(s->
picture_ptr->
data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x));
1073 PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1076 *ptr16= pred + (dc << point_transform);
1097 int linesize,
int lowres)
1102 case 1:
copy_block4(dst, src, linesize, linesize, 4);
1104 case 2:
copy_block2(dst, src, linesize, linesize, 2);
1106 case 3: *dst = *
src;
1113 int block_x, block_y;
1116 for (block_y=0; block_y<
size; block_y++)
1117 for (block_x=0; block_x<
size; block_x++)
1118 *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->
bits;
1120 for (block_y=0; block_y<
size; block_y++)
1121 for (block_x=0; block_x<
size; block_x++)
1122 *(ptr + block_x + block_y*linesize) <<= 8 - s->
bits;
1127 int Al,
const uint8_t *mb_bitmask,
1128 int mb_bitmask_size,
1136 int bytes_per_pixel = 1 + (s->
bits > 8);
1148 for (i = 0; i < nb_components; i++) {
1151 reference_data[
c] = reference ? reference->
data[
c] : NULL;
1156 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1157 for (mb_x = 0; mb_x < s->
mb_width; mb_x++) {
1168 for (i = 0; i < nb_components; i++) {
1170 int n, h,
v, x,
y,
c, j;
1178 for (j = 0; j <
n; j++) {
1179 block_offset = (((linesize[
c] * (v * mb_y +
y) * 8) +
1180 (h * mb_x + x) * 8 * bytes_per_pixel) >> s->
avctx->
lowres);
1183 block_offset += linesize[
c] >> 1;
1184 ptr = data[
c] + block_offset;
1196 "error y=%d x=%d\n", mb_y, mb_x);
1214 "error y=%d x=%d\n", mb_y, mb_x);
1218 av_dlog(s->
avctx,
"mb: %d %d processed\n", mb_y, mb_x);
1221 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1236 int se,
int Ah,
int Al)
1245 int bytes_per_pixel = 1 + (s->
bits > 8);
1248 if (se < ss || se > 63) {
1259 data += linesize >> 1;
1263 for (mb_y = 0; mb_y < s->
mb_height; mb_y++) {
1268 for (mb_x = 0; mb_x < s->
mb_width; mb_x++,
block++, last_nnz++) {
1275 quant_matrix, ss, se, Al, &EOBRUN);
1278 quant_matrix, ss, se, Al, &EOBRUN);
1281 "error y=%d x=%d\n", mb_y, mb_x);
1299 int mb_bitmask_size,
const AVFrame *reference)
1301 int len, nb_components, i, h,
v,
predictor, point_transform;
1303 const int block_size = s->
lossless ? 1 : 8;
1304 int ilv, prev_shift;
1308 "Can not process SOS before SOF, skipping\n");
1318 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1321 if (len != 6 + 2 * nb_components) {
1325 for (i = 0; i < nb_components; i++) {
1334 "decode_sos: index(%d) out of components\n", index);
1350 index = (index+2)%3;
1370 prev_shift = point_transform = 0;
1372 if (nb_components > 1) {
1376 }
else if (!s->
ls) {
1379 s->
mb_width = (s->
width + h * block_size - 1) / (h * block_size);
1388 s->
lossless ?
"lossless" :
"sequential DCT", s->
rgb ?
"RGB" :
"",
1398 for (i = 0; i < nb_components; i++)
1403 if (CONFIG_JPEGLS_DECODER && s->
ls) {
1408 point_transform, ilv)) < 0)
1417 nb_components)) < 0)
1426 point_transform)) < 0)
1430 prev_shift, point_transform,
1431 mb_bitmask, mb_bitmask_size, reference)) < 0)
1515 int t_w, t_h, v1, v2;
1526 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1535 if (len -10 - (t_w * t_h * 3) > 0)
1536 len -= t_w * t_h * 3;
1556 "Pegasus lossless jpeg header found\n");
1576 if (
id ==
AV_RL32(
"colr") && len > 0) {
1583 if (
id ==
AV_RL32(
"xfrm") && len > 0) {
1611 }
else if (type == 1) {
1623 if (!(flags & 0x04)) {
1633 int ret,
le, ifd_offset, bytes_read;
1692 "mjpeg: error, decode_app parser read over the end\n");
1706 for (i = 0; i < len - 2; i++)
1708 if (i > 0 && cbuf[i - 1] ==
'\n')
1717 if (!strncmp(cbuf,
"AVID", 4)) {
1719 }
else if (!strcmp(cbuf,
"CS=ITU601"))
1721 else if ((!strncmp(cbuf,
"Intel(R) JPEG Library, version 1", 32)) ||
1722 (!strncmp(cbuf,
"Metasoft MJPEG Codec", 20)))
1741 buf_ptr = *pbuf_ptr;
1742 while (buf_end - buf_ptr > 1) {
1745 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1754 av_dlog(NULL,
"find_marker skipped %d bytes\n", skipped);
1755 *pbuf_ptr = buf_ptr;
1761 const uint8_t **unescaped_buf_ptr,
1762 int *unescaped_buf_size)
1772 if (start_code ==
SOS && !s->
ls) {
1776 while (src < buf_end) {
1782 while (src < buf_end && x == 0xff)
1785 if (x >= 0xd0 && x <= 0xd7)
1792 *unescaped_buf_ptr = s->
buffer;
1793 *unescaped_buf_size = dst - s->
buffer;
1794 memset(s->
buffer + *unescaped_buf_size, 0,
1798 (buf_end - *buf_ptr) - (dst - s->
buffer));
1799 }
else if (start_code ==
SOS && s->
ls) {
1807 while (src + t < buf_end) {
1810 while ((src + t < buf_end) && x == 0xff)
1833 *unescaped_buf_ptr = dst;
1834 *unescaped_buf_size = (bit_count + 7) >> 3;
1835 memset(s->
buffer + *unescaped_buf_size, 0,
1838 *unescaped_buf_ptr = *buf_ptr;
1839 *unescaped_buf_size = buf_end - *buf_ptr;
1850 int buf_size = avpkt->
size;
1852 const uint8_t *buf_end, *buf_ptr;
1853 const uint8_t *unescaped_buf_ptr;
1855 int unescaped_buf_size;
1865 buf_end = buf + buf_size;
1866 while (buf_ptr < buf_end) {
1870 &unescaped_buf_size);
1872 if (start_code < 0) {
1874 }
else if (unescaped_buf_size > INT_MAX / 8) {
1876 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1877 start_code, unescaped_buf_size, buf_size);
1881 start_code, buf_end - buf_ptr);
1895 if (start_code >= 0xd0 && start_code <= 0xd7)
1897 "restart marker: %d\n", start_code & 0x0f);
1899 else if (start_code >=
APP0 && start_code <=
APP15)
1902 else if (start_code ==
COM)
1907 if (!CONFIG_JPEGLS_DECODER &&
1908 (start_code ==
SOF48 || start_code ==
LSE)) {
1913 switch (start_code) {
1958 if (!CONFIG_JPEGLS_DECODER ||
1967 "Found EOI before any SOF, ignoring\n");
1985 int qpw = (s->
width + 15) / 16;
1988 memset(qp_table_buf->
data, qp, qpw);
2017 "mjpeg: unsupported coding type (%x)\n", start_code);
2024 "marker parser used %d bytes (%d bits)\n",
2044 for (index = s->
width - 1; index; index--)
2045 line[
index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2053 w = s->
width >> hshift;
2058 for (i = s->
height - 1; i; i--) {
2062 memcpy(dst, src1, w);
2064 for (index = 0; index < w; index++)
2065 dst[index] = (src1[index] + src2[index]) >> 1;
2073 for (index=0; index<4; index++) {
2077 if(index && index<3){
2083 for (i=0; i<h/2; i++) {
2085 FFSWAP(
int, dst[j], dst2[j]);
2095 for (i=0; i<h; i++) {
2098 for (index=0; index<4; index++) {
2102 for (j=0; j<w; j++) {
2104 int r = dst[0][j] * k;
2105 int g = dst[1][j] * k;
2106 int b = dst[2][j] * k;
2107 dst[0][j] = g*257 >> 16;
2108 dst[1][j] = b*257 >> 16;
2109 dst[2][j] = r*257 >> 16;
2130 return buf_ptr -
buf;
2153 for (i = 0; i < 3; i++) {
2154 for (j = 0; j < 4; j++)
2171 #if CONFIG_MJPEG_DECODER
2172 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2173 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2175 {
"extern_huff",
"Use external huffman table.",
2180 static const AVClass mjpegdec_class = {
2199 .priv_class = &mjpegdec_class,
2202 #if CONFIG_THP_DECODER