const uint8_t *val_table, int nb_codes,
int use_static, int is_ac)
{
- uint8_t huff_size[256];
+ uint8_t huff_size[256] = { 0 };
uint16_t huff_code[256];
uint16_t huff_sym[256];
int i;
assert(nb_codes <= 256);
- memset(huff_size, 0, sizeof(huff_size));
ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
for (i = 0; i < 256; i++)
if (is_ac)
huff_sym[0] = 16 * 256;
- return init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
- huff_code, 2, 2, huff_sym, 2, 2, use_static);
+ return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
+ huff_code, 2, 2, huff_sym, 2, 2, use_static);
}
static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
avcodec_get_frame_defaults(&s->picture);
s->avctx = avctx;
- dsputil_init(&s->dsp, avctx);
+ ff_dsputil_init(&s->dsp, avctx);
ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
s->buffer_size = 0;
s->buffer = NULL;
build_basic_mjpeg_vlc(s);
-#if FF_API_MJPEG_GLOBAL_OPTS
- if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
- s->extern_huff = 1;
-#endif
if (s->extern_huff) {
av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
len -= n;
/* build VLC and flush previous vlc if present */
- free_vlc(&s->vlcs[class][index]);
+ ff_free_vlc(&s->vlcs[class][index]);
av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
class, index, code_max + 1);
if (build_vlc(&s->vlcs[class][index], bits_table, val_table,
return -1;
if (class > 0) {
- free_vlc(&s->vlcs[2][index]);
+ ff_free_vlc(&s->vlcs[2][index]);
if (build_vlc(&s->vlcs[2][index], bits_table, val_table,
code_max + 1, 0, 0) < 0)
return -1;
int len, nb_components, i, width, height, pix_fmt_id;
s->cur_scan = 0;
+ s->upscale_h = s->upscale_v = 0;
/* XXX: verify len field validity */
len = get_bits(&s->gb, 16);
if (nb_components <= 0 ||
nb_components > MAX_COMPONENTS)
return -1;
+ if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
+ if (nb_components != s->nb_components) {
+ av_log(s->avctx, AV_LOG_ERROR, "nb_components changing in interlaced picture\n");
+ return AVERROR_INVALIDDATA;
+ }
+ }
if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
av_log(s->avctx, AV_LOG_ERROR,
"only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
s->h_max = s->h_count[i];
if (s->v_count[i] > s->v_max)
s->v_max = s->v_count[i];
+ if (!s->h_count[i] || !s->v_count[i]) {
+ av_log(s->avctx, AV_LOG_ERROR, "h/v_count is 0\n");
+ return -1;
+ }
s->quant_index[i] = get_bits(&s->gb, 8);
if (s->quant_index[i] >= 4)
return -1;
s->first_picture = 0;
}
- if (s->interlaced && (s->bottom_field == !s->interlace_polarity))
+ if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
+ if (s->progressive) {
+ av_log_ask_for_sample(s->avctx, "progressively coded interlaced pictures not supported\n");
+ return AVERROR_INVALIDDATA;
+ }
return 0;
+ }
/* XXX: not complete test ! */
pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
return -1;
}
if (s->ls) {
+ s->upscale_h = s->upscale_v = 0;
if (s->nb_components > 1)
s->avctx->pix_fmt = PIX_FMT_RGB24;
else if (s->bits <= 8)
int resync_mb_y = 0;
int resync_mb_x = 0;
+ s->restart_count = s->restart_interval;
+
av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
(unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
buffer = s->ljpeg_buffer;
for(i=0; i<3; i++)
top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
}
- if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x)
+ if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
modified_predictor = 1;
for (i=0;i<nb_components;i++) {
if (s->restart_interval && !s->restart_count)
s->restart_count = s->restart_interval;
- if (get_bits_count(&s->gb)>s->gb.size_in_bits) {
+ if (get_bits_left(&s->gb) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
- get_bits_count(&s->gb) - s->gb.size_in_bits);
+ -get_bits_left(&s->gb));
return -1;
}
for (i = 0; i < nb_components; i++) {
if (s->restart_interval) {
s->restart_count--;
+ if(s->restart_count == 0 && s->avctx->codec_id == CODEC_ID_THP){
+ align_get_bits(&s->gb);
+ for (i = 0; i < nb_components; i++) /* reset dc */
+ s->last_dc[i] = 1024;
+ }
+
i = 8 + ((-get_bits_count(&s->gb)) & 7);
/* skip RSTn */
if (show_bits(&s->gb, i) == (1 << i) - 1) {
if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == PIX_FMT_GBR24P)
index = (i+2)%3;
+ if(nb_components == 1 && s->nb_components == 3 && s->avctx->pix_fmt == PIX_FMT_GBR24P)
+ index = (index+2)%3;
s->comp_index[i] = index;
return -1;
}
}
- if (s->upscale_h) {
- uint8_t *line = s->picture_ptr->data[s->upscale_h];
- for (i = 0; i < s->chroma_height; i++) {
- for (index = s->width - 1; index; index--)
- line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
- line += s->linesize[s->upscale_h];
- }
- }
- if (s->upscale_v) {
- uint8_t *dst = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(s->height - 1) * s->linesize[s->upscale_v]];
- for (i = s->height - 1; i; i--) {
- uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[i / 2 * s->linesize[s->upscale_v]];
- uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(i + 1) / 2 * s->linesize[s->upscale_v]];
- if (src1 == src2) {
- memcpy(dst, src1, s->width);
- } else {
- for (index = 0; index < s->width; index++)
- dst[index] = (src1[index] + src2[index]) >> 1;
- }
- dst -= s->linesize[s->upscale_v];
- }
- }
emms_c();
return 0;
out_of_range:
len = get_bits(&s->gb, 16);
if (len < 5)
return -1;
- if (8 * len + get_bits_count(&s->gb) > s->gb.size_in_bits)
+ if (8 * len > get_bits_left(&s->gb))
return -1;
id = get_bits_long(&s->gb, 32);
static int mjpeg_decode_com(MJpegDecodeContext *s)
{
int len = get_bits(&s->gb, 16);
- if (len >= 2 &&
- 8 * len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
+ if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
char *cbuf = av_malloc(len - 1);
if (cbuf) {
int i;
int start_code;
start_code = find_marker(buf_ptr, buf_end);
- if ((buf_end - *buf_ptr) > s->buffer_size) {
- av_free(s->buffer);
- s->buffer_size = buf_end - *buf_ptr;
- s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
- av_log(s->avctx, AV_LOG_DEBUG,
- "buffer too small, expanding to %d bytes\n", s->buffer_size);
- }
+ av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
+ if (!s->buffer)
+ return AVERROR(ENOMEM);
/* unescape buffer of SOS, use special treatment for JPEG-LS */
if (start_code == SOS && !s->ls) {
const uint8_t *unescaped_buf_ptr;
int unescaped_buf_size;
int start_code;
+ int i, index;
AVFrame *picture = data;
s->got_picture = 0; // picture from previous image can not be reused
/* EOF */
if (start_code < 0) {
goto the_end;
+ } else if (unescaped_buf_size > (1U<<29)) {
+ av_log(avctx, AV_LOG_ERROR, "MJPEG packet 0x%x too big (0x%x/0x%x), corrupt data?\n",
+ start_code, unescaped_buf_size, buf_size);
+ return AVERROR_INVALIDDATA;
} else {
av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
start_code, buf_end - buf_ptr);
av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
return -1;
the_end:
+ if (s->upscale_h) {
+ uint8_t *line = s->picture_ptr->data[s->upscale_h];
+ av_assert0(avctx->pix_fmt == PIX_FMT_YUVJ444P ||
+ avctx->pix_fmt == PIX_FMT_YUV444P ||
+ avctx->pix_fmt == PIX_FMT_YUVJ440P ||
+ avctx->pix_fmt == PIX_FMT_YUV440P);
+ for (i = 0; i < s->chroma_height; i++) {
+ for (index = s->width - 1; index; index--)
+ line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
+ line += s->linesize[s->upscale_h];
+ }
+ }
+ if (s->upscale_v) {
+ uint8_t *dst = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(s->height - 1) * s->linesize[s->upscale_v]];
+ av_assert0(avctx->pix_fmt == PIX_FMT_YUVJ444P ||
+ avctx->pix_fmt == PIX_FMT_YUV444P ||
+ avctx->pix_fmt == PIX_FMT_YUVJ422P ||
+ avctx->pix_fmt == PIX_FMT_YUV422P);
+ for (i = s->height - 1; i; i--) {
+ uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[i / 2 * s->linesize[s->upscale_v]];
+ uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(i + 1) / 2 * s->linesize[s->upscale_v]];
+ if (src1 == src2) {
+ memcpy(dst, src1, s->width);
+ } else {
+ for (index = 0; index < s->width; index++)
+ dst[index] = (src1[index] + src2[index]) >> 1;
+ }
+ dst -= s->linesize[s->upscale_v];
+ }
+ }
av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n",
buf_end - buf_ptr);
// return buf_end - buf_ptr;
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++)
- free_vlc(&s->vlcs[i][j]);
+ ff_free_vlc(&s->vlcs[i][j]);
}
for (i = 0; i < MAX_COMPONENTS; i++) {
av_freep(&s->blocks[i]);