Commit 10b7b472 authored by Fabrice Bellard's avatar Fabrice Bellard
Browse files

added MJPEG decoder, renamed mjpegenc.c to mjpeg.c


Originally committed as revision 34 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 1d20b11a
......@@ -4,7 +4,7 @@ CFLAGS= $(OPTFLAGS) -Wall -g -DHAVE_AV_CONFIG_H
LDFLAGS= -g
OBJS= common.o utils.o mpegvideo.o h263.o jrevdct.o jfdctfst.o \
mpegaudio.o ac3enc.o mjpegenc.o resample.o dsputil.o \
mpegaudio.o ac3enc.o mjpeg.o resample.o dsputil.o \
motion_est.o imgconvert.o imgresample.o msmpeg4.o \
mpeg12.o h263dec.o rv10.o
ASM_OBJS=
......
/*
* MJPEG encoder
* Copyright (c) 2000 Gerard Lantau.
* MJPEG encoder and decoder
* Copyright (c) 2000, 2001 Gerard Lantau.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -415,3 +415,510 @@ void mjpeg_encode_mb(MpegEncContext *s,
encode_block(s, block[i], i);
}
}
/******************************************/
/* decoding */
//#define DEBUG
#ifdef DEBUG
#define dprintf(fmt,args...) printf(fmt, ## args)
#else
#define dprintf(fmt,args...)
#endif
/* compressed picture size */
#define PICTURE_BUFFER_SIZE 100000
#define MAX_COMPONENTS 4
typedef struct MJpegDecodeContext {
GetBitContext gb;
UINT32 header_state;
int start_code; /* current start code */
UINT8 *buf_ptr;
int buffer_size;
int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
INT16 quant_matrixes[4][64];
VLC vlcs[2][4];
int width, height;
int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
int v_count[MAX_COMPONENTS];
int h_max, v_max; /* maximum h and v counts */
int quant_index[4]; /* quant table index for each component */
int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
int linesize[MAX_COMPONENTS];
DCTELEM block[64] __align8;
UINT8 buffer[PICTURE_BUFFER_SIZE];
} MJpegDecodeContext;
static int mjpeg_decode_init(AVCodecContext *avctx)
{
MJpegDecodeContext *s = avctx->priv_data;
s->header_state = 0;
s->mpeg_enc_ctx_allocated = 0;
s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
account FF 00 case */
s->start_code = -1;
s->buf_ptr = s->buffer;
return 0;
}
/* quantize tables */
static int mjpeg_decode_dqt(MJpegDecodeContext *s,
UINT8 *buf, int buf_size)
{
int len, index, i;
init_get_bits(&s->gb, buf, buf_size);
len = get_bits(&s->gb, 16);
len -= 2;
while (len >= 65) {
/* only 8 bit precision handled */
if (get_bits(&s->gb, 4) != 0)
return -1;
index = get_bits(&s->gb, 4);
if (index >= 4)
return -1;
dprintf("index=%d\n", index);
/* read quant table */
for(i=0;i<64;i++)
s->quant_matrixes[index][i] = get_bits(&s->gb, 8);
len -= 65;
}
return 0;
}
/* decode huffman tables and build VLC decoders */
static int mjpeg_decode_dht(MJpegDecodeContext *s,
UINT8 *buf, int buf_size)
{
int len, index, i, class, n, v, code_max;
UINT8 bits_table[17];
UINT8 val_table[256];
UINT8 huff_size[256];
UINT16 huff_code[256];
init_get_bits(&s->gb, buf, buf_size);
len = get_bits(&s->gb, 16);
len -= 2;
while (len > 0) {
if (len < 17)
return -1;
class = get_bits(&s->gb, 4);
if (class >= 2)
return -1;
index = get_bits(&s->gb, 4);
if (index >= 4)
return -1;
n = 0;
for(i=1;i<=16;i++) {
bits_table[i] = get_bits(&s->gb, 8);
n += bits_table[i];
}
len -= 17;
if (len < n || n > 256)
return -1;
code_max = 0;
for(i=0;i<n;i++) {
v = get_bits(&s->gb, 8);
if (v > code_max)
code_max = v;
val_table[i] = v;
}
len -= n;
/* now build size/code table */
memset(huff_size, 0, sizeof(huff_size));
build_huffman_codes(huff_size, huff_code, bits_table, val_table);
/* build VLC and flush previous vlc if present */
free_vlc(&s->vlcs[class][index]);
dprintf("class=%d index=%d nb_codes=%d\n",
class, index, code_max + 1);
init_vlc(&s->vlcs[class][index], 9, code_max + 1,
huff_size, 1, 1,
huff_code, 2, 2);
}
return 0;
}
static int mjpeg_decode_sof0(MJpegDecodeContext *s,
UINT8 *buf, int buf_size)
{
int len, nb_components, i, index, width, height;
init_get_bits(&s->gb, buf, buf_size);
/* XXX: verify len field validity */
len = get_bits(&s->gb, 16);
/* only 8 bits/component accepted */
if (get_bits(&s->gb, 8) != 8)
return -1;
height = get_bits(&s->gb, 16);
width = get_bits(&s->gb, 16);
nb_components = get_bits(&s->gb, 8);
if (nb_components <= 0 ||
nb_components > MAX_COMPONENTS)
return -1;
s->h_max = 1;
s->v_max = 1;
for(i=0;i<nb_components;i++) {
/* component id */
index = get_bits(&s->gb, 8) - 1;
/* XXX: avoid this limitation */
if (index < 0 || index >= MAX_COMPONENTS)
return -1;
s->h_count[i] = get_bits(&s->gb, 4);
s->v_count[i] = get_bits(&s->gb, 4);
/* compute hmax and vmax (only used in interleaved case) */
if (s->h_count[i] > s->h_max)
s->h_max = s->h_count[i];
if (s->v_count[i] > s->v_max)
s->v_max = s->v_count[i];
/* XXX: only 420 is accepted */
if ((i == 0 && (s->h_count[i] != 2 || s->v_count[i] != 2)) ||
(i != 0 && (s->h_count[i] != 1 || s->v_count[i] != 1)))
return -1;
s->quant_index[i] = get_bits(&s->gb, 8);
if (s->quant_index[i] >= 4)
return -1;
dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]);
}
/* if different size, realloc/alloc picture */
/* XXX: also check h_count and v_count */
if (width != s->width || height != s->height) {
for(i=0;i<MAX_COMPONENTS;i++) {
free(s->current_picture[i]);
s->current_picture[i] = NULL;
}
s->width = width;
s->height = height;
for(i=0;i<nb_components;i++) {
int w, h, hh, vv;
hh = s->h_max / s->h_count[i];
vv = s->v_max / s->v_count[i];
w = (s->width + 8 * hh - 1) / (8 * hh);
h = (s->height + 8 * vv - 1) / (8 * vv);
w = w * 8;
h = h * 8;
s->linesize[i] = w;
/* memory test is done in mjpeg_decode_sos() */
s->current_picture[i] = av_mallocz(w * h);
}
}
return 0;
}
static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
{
VLC *dc_vlc;
int code, diff;
dc_vlc = &s->vlcs[0][dc_index];
code = get_vlc(&s->gb, dc_vlc);
if (code < 0)
return 0xffff;
if (code == 0) {
diff = 0;
} else {
diff = get_bits(&s->gb, code);
if ((diff & (1 << (code - 1))) == 0)
diff = (-1 << code) | (diff + 1);
}
return diff;
}
/* decode block and dequantize */
static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
int component, int dc_index, int ac_index, int quant_index)
{
int nbits, code, i, j, level;
int run, val;
VLC *ac_vlc;
INT16 *quant_matrix;
quant_matrix = s->quant_matrixes[quant_index];
/* DC coef */
val = decode_dc(s, dc_index);
if (val == 0xffff) {
dprintf("error dc\n");
return -1;
}
val = val * quant_matrix[0] + s->last_dc[component];
s->last_dc[component] = val;
block[0] = val;
/* AC coefs */
ac_vlc = &s->vlcs[1][ac_index];
i = 1;
for(;;) {
code = get_vlc(&s->gb, ac_vlc);
if (code < 0) {
dprintf("error ac\n");
return -1;
}
/* EOB */
if (code == 0)
break;
if (code == 0xf0) {
i += 16;
} else {
run = code >> 4;
nbits = code & 0xf;
level = get_bits(&s->gb, nbits);
if ((level & (1 << (nbits - 1))) == 0)
level = (-1 << nbits) | (level + 1);
i += run;
if (i >= 64) {
dprintf("error count: %d\n", i);
return -1;
}
j = zigzag_direct[i];
block[j] = level * quant_matrix[j];
i++;
}
}
return 0;
}
static int mjpeg_decode_sos(MJpegDecodeContext *s,
UINT8 *buf, int buf_size)
{
int len, nb_components, i, j, n, h, v;
int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index;
int comp_index[4];
int dc_index[4];
int ac_index[4];
int nb_blocks[4];
int h_count[4];
int v_count[4];
init_get_bits(&s->gb, buf, buf_size);
/* XXX: verify len field validity */
len = get_bits(&s->gb, 16);
nb_components = get_bits(&s->gb, 8);
/* XXX: only interleaved scan accepted */
if (nb_components != 3)
return -1;
vmax = 0;
hmax = 0;
for(i=0;i<nb_components;i++) {
index = get_bits(&s->gb, 8) - 1;
/* XXX: this limitation is not OK */
if (index < 0 || index >= 4)
return -1;
comp_index[i] = index;
nb_blocks[i] = s->h_count[index] * s->v_count[index];
h_count[i] = s->h_count[index];
v_count[i] = s->v_count[index];
dc_index[i] = get_bits(&s->gb, 4);
if (dc_index[i] >= 4)
return -1;
ac_index[i] = get_bits(&s->gb, 4);
if (ac_index[i] >= 4)
return -1;
}
get_bits(&s->gb, 8); /* Ss */
get_bits(&s->gb, 8); /* Se */
get_bits(&s->gb, 8); /* not used */
for(i=0;i<nb_components;i++)
s->last_dc[i] = 1024;
if (nb_components > 1) {
/* interleaved stream */
mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
} else {
h = s->h_max / s->h_count[comp_index[0]];
v = s->v_max / s->v_count[comp_index[0]];
mb_width = (s->width + h * 8 - 1) / (h * 8);
mb_height = (s->height + v * 8 - 1) / (v * 8);
nb_blocks[0] = 1;
h_count[0] = 1;
v_count[0] = 1;
}
for(mb_y = 0; mb_y < mb_height; mb_y++) {
for(mb_x = 0; mb_x < mb_width; mb_x++) {
for(i=0;i<nb_components;i++) {
UINT8 *ptr;
int x, y, c;
n = nb_blocks[i];
c = comp_index[i];
h = h_count[i];
v = v_count[i];
x = 0;
y = 0;
for(j=0;j<n;j++) {
memset(s->block, 0, sizeof(s->block));
if (decode_block(s, s->block, i,
dc_index[i], ac_index[i],
s->quant_index[c]) < 0) {
dprintf("error %d %d\n", mb_y, mb_x);
return -1;
}
ff_idct (s->block);
ptr = s->current_picture[c] +
(s->linesize[c] * (v * mb_y + y) * 8) +
(h * mb_x + x) * 8;
put_pixels_clamped(s->block, ptr, s->linesize[c]);
if (++x == h) {
x = 0;
y++;
}
}
}
}
}
return 0;
}
/* return the 8 bit start code value and update the search
state. Return -1 if no start code found */
static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
UINT32 *header_state)
{
UINT8 *buf_ptr;
unsigned int state, v;
int val;
state = *header_state;
buf_ptr = *pbuf_ptr;
if (state) {
/* get marker */
found:
if (buf_ptr < buf_end) {
val = *buf_ptr++;
state = 0;
} else {
val = -1;
}
} else {
while (buf_ptr < buf_end) {
v = *buf_ptr++;
if (v == 0xff) {
state = 1;
goto found;
}
}
val = -1;
}
*pbuf_ptr = buf_ptr;
*header_state = state;
return val;
}
static int mjpeg_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
UINT8 *buf, int buf_size)
{
MJpegDecodeContext *s = avctx->priv_data;
UINT8 *buf_end, *buf_ptr, *buf_start;
int len, code, start_code, input_size, i;
AVPicture *picture = data;
/* no supplementary picture */
if (buf_size == 0) {
*data_size = 0;
return 0;
}
buf_ptr = buf;
buf_end = buf + buf_size;
while (buf_ptr < buf_end) {
buf_start = buf_ptr;
/* find start next marker */
code = find_marker(&buf_ptr, buf_end, &s->header_state);
/* copy to buffer */
len = buf_ptr - buf_start;
if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
/* data too big : flush */
s->buf_ptr = s->buffer;
if (code > 0)
s->start_code = code;
} else {
memcpy(s->buf_ptr, buf_start, len);
s->buf_ptr += len;
/* if we got FF 00, we copy FF to the stream to unescape FF 00 */
if (code == 0) {
s->buf_ptr--;
} else if (code > 0) {
/* prepare data for next start code */
input_size = s->buf_ptr - s->buffer;
start_code = s->start_code;
s->buf_ptr = s->buffer;
s->start_code = code;
switch(start_code) {
case SOI:
/* nothing to do on SOI */
break;
case DQT:
mjpeg_decode_dqt(s, s->buffer, input_size);
break;
case DHT:
mjpeg_decode_dht(s, s->buffer, input_size);
break;
case SOF0:
mjpeg_decode_sof0(s, s->buffer, input_size);
break;
case SOS:
mjpeg_decode_sos(s, s->buffer, input_size);
if (s->start_code == EOI) {
/* XXX: YUV420 hardcoded */
for(i=0;i<3;i++) {
picture->data[i] = s->current_picture[i];
picture->linesize[i] = s->linesize[i];
}
*data_size = sizeof(AVPicture);
avctx->height = s->height;
avctx->width = s->width;
avctx->pix_fmt = PIX_FMT_YUV420P;
goto the_end;
}
break;
}
}
}
}
the_end:
return buf_ptr - buf;
}
static int mjpeg_decode_end(AVCodecContext *avctx)
{
MJpegDecodeContext *s = avctx->priv_data;
int i, j;
for(i=0;i<MAX_COMPONENTS;i++)
free(s->current_picture[i]);
for(i=0;i<2;i++) {
for(j=0;j<4;j++)
free_vlc(&s->vlcs[i][j]);
}
return 0;
}
AVCodec mjpeg_decoder = {
"mjpeg",
CODEC_TYPE_VIDEO,
CODEC_ID_MJPEG,
sizeof(MJpegDecodeContext),
mjpeg_decode_init,
NULL,
mjpeg_decode_end,
mjpeg_decode_frame,
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment