00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00025 #define ALT_BITSTREAM_READER_LE
00026 #include "avcodec.h"
00027 #include "bitstream.h"
00028 #include "indeo2data.h"
00029
00030 typedef struct Ir2Context{
00031 AVCodecContext *avctx;
00032 AVFrame picture;
00033 GetBitContext gb;
00034 int decode_delta;
00035 } Ir2Context;
00036
00037 #define CODE_VLC_BITS 14
00038 static VLC ir2_vlc;
00039
00040
00041 static inline int ir2_get_code(GetBitContext *gb)
00042 {
00043 return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
00044 }
00045
00046 static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst, int stride,
00047 const uint8_t *table)
00048 {
00049 int i;
00050 int j;
00051 int out = 0;
00052 int c;
00053 int t;
00054
00055 if(width&1)
00056 return -1;
00057
00058
00059 while (out < width){
00060 c = ir2_get_code(&ctx->gb);
00061 if(c >= 0x80) {
00062 c -= 0x7F;
00063 if(out + c*2 > width)
00064 return -1;
00065 for (i = 0; i < c * 2; i++)
00066 dst[out++] = 0x80;
00067 } else {
00068 dst[out++] = table[c * 2];
00069 dst[out++] = table[(c * 2) + 1];
00070 }
00071 }
00072 dst += stride;
00073
00074 for (j = 1; j < height; j++){
00075 out = 0;
00076 while (out < width){
00077 c = ir2_get_code(&ctx->gb);
00078 if(c >= 0x80) {
00079 c -= 0x7F;
00080 if(out + c*2 > width)
00081 return -1;
00082 for (i = 0; i < c * 2; i++) {
00083 dst[out] = dst[out - stride];
00084 out++;
00085 }
00086 } else {
00087 t = dst[out - stride] + (table[c * 2] - 128);
00088 t= clip_uint8(t);
00089 dst[out] = t;
00090 out++;
00091 t = dst[out - stride] + (table[(c * 2) + 1] - 128);
00092 t= clip_uint8(t);
00093 dst[out] = t;
00094 out++;
00095 }
00096 }
00097 dst += stride;
00098 }
00099 return 0;
00100 }
00101
00102 static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst, int stride,
00103 const uint8_t *table)
00104 {
00105 int j;
00106 int out = 0;
00107 int c;
00108 int t;
00109
00110 if(width&1)
00111 return -1;
00112
00113 for (j = 0; j < height; j++){
00114 out = 0;
00115 while (out < width){
00116 c = ir2_get_code(&ctx->gb);
00117 if(c >= 0x80) {
00118 c -= 0x7F;
00119 out += c * 2;
00120 } else {
00121 t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
00122 t= clip_uint8(t);
00123 dst[out] = t;
00124 out++;
00125 t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
00126 t= clip_uint8(t);
00127 dst[out] = t;
00128 out++;
00129 }
00130 }
00131 dst += stride;
00132 }
00133 return 0;
00134 }
00135
00136 static int ir2_decode_frame(AVCodecContext *avctx,
00137 void *data, int *data_size,
00138 uint8_t *buf, int buf_size)
00139 {
00140 Ir2Context * const s = avctx->priv_data;
00141 AVFrame *picture = data;
00142 AVFrame * const p= (AVFrame*)&s->picture;
00143 int start;
00144 int i;
00145
00146 if(p->data[0])
00147 avctx->release_buffer(avctx, p);
00148
00149 p->reference = 1;
00150 p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00151 if (avctx->reget_buffer(avctx, p)) {
00152 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00153 return -1;
00154 }
00155
00156 s->decode_delta = buf[18];
00157
00158
00159 #ifndef ALT_BITSTREAM_READER_LE
00160 for (i = 0; i < buf_size; i++)
00161 buf[i] = ff_reverse[buf[i]];
00162 #endif
00163 start = 48;
00164
00165 init_get_bits(&s->gb, buf + start, buf_size - start);
00166
00167 if (s->decode_delta) {
00168 ir2_decode_plane(s, avctx->width, avctx->height,
00169 s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
00170
00171 ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
00172 s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
00173 ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
00174 s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
00175 } else {
00176 ir2_decode_plane_inter(s, avctx->width, avctx->height,
00177 s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
00178
00179 ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
00180 s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
00181 ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
00182 s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
00183 }
00184
00185 *picture= *(AVFrame*)&s->picture;
00186 *data_size = sizeof(AVPicture);
00187
00188 return buf_size;
00189 }
00190
00191 static int ir2_decode_init(AVCodecContext *avctx){
00192 Ir2Context * const ic = avctx->priv_data;
00193
00194 ic->avctx = avctx;
00195
00196 avctx->pix_fmt= PIX_FMT_YUV410P;
00197
00198 if (!ir2_vlc.table)
00199 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
00200 &ir2_codes[0][1], 4, 2,
00201 #ifdef ALT_BITSTREAM_READER_LE
00202 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00203 #else
00204 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_STATIC);
00205 #endif
00206
00207 return 0;
00208 }
00209
00210 AVCodec indeo2_decoder = {
00211 "indeo2",
00212 CODEC_TYPE_VIDEO,
00213 CODEC_ID_INDEO2,
00214 sizeof(Ir2Context),
00215 ir2_decode_init,
00216 NULL,
00217 NULL,
00218 ir2_decode_frame,
00219 CODEC_CAP_DR1,
00220 };