00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "avcodec.h"
00029 #include "dsputil.h"
00030 #include "mpegvideo.h"
00031
00032
00033
00034
00035 typedef struct MDECContext{
00036 AVCodecContext *avctx;
00037 DSPContext dsp;
00038 AVFrame picture;
00039 PutBitContext pb;
00040 GetBitContext gb;
00041 ScanTable scantable;
00042 int version;
00043 int qscale;
00044 int last_dc[3];
00045 int mb_width;
00046 int mb_height;
00047 int mb_x, mb_y;
00048 DCTELEM __align8 block[6][64];
00049 uint16_t __align8 intra_matrix[64];
00050 int __align8 q_intra_matrix[64];
00051 uint8_t *bitstream_buffer;
00052 int bitstream_buffer_size;
00053 int block_last_index[6];
00054 } MDECContext;
00055
00056
00057 static inline int mdec_decode_block_intra(MDECContext *a, DCTELEM *block, int n)
00058 {
00059 int level, diff, i, j, run;
00060 int component;
00061 RLTable *rl = &rl_mpeg1;
00062 uint8_t * const scantable= a->scantable.permutated;
00063 const uint16_t *quant_matrix= ff_mpeg1_default_intra_matrix;
00064 const int qscale= a->qscale;
00065
00066
00067 if(a->version==2){
00068 block[0]= 2*get_sbits(&a->gb, 10) + 1024;
00069 }else{
00070 component = (n <= 3 ? 0 : n - 4 + 1);
00071 diff = decode_dc(&a->gb, component);
00072 if (diff >= 0xffff)
00073 return -1;
00074 a->last_dc[component]+= diff;
00075 block[0] = a->last_dc[component]<<3;
00076 }
00077
00078 i = 0;
00079 {
00080 OPEN_READER(re, &a->gb);
00081
00082 for(;;) {
00083 UPDATE_CACHE(re, &a->gb);
00084 GET_RL_VLC(level, run, re, &a->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00085
00086 if(level == 127){
00087 break;
00088 } else if(level != 0) {
00089 i += run;
00090 j = scantable[i];
00091 level= (level*qscale*quant_matrix[j])>>3;
00092
00093 level = (level ^ SHOW_SBITS(re, &a->gb, 1)) - SHOW_SBITS(re, &a->gb, 1);
00094 LAST_SKIP_BITS(re, &a->gb, 1);
00095 } else {
00096
00097 run = SHOW_UBITS(re, &a->gb, 6)+1; LAST_SKIP_BITS(re, &a->gb, 6);
00098 UPDATE_CACHE(re, &a->gb);
00099 level = SHOW_SBITS(re, &a->gb, 10); SKIP_BITS(re, &a->gb, 10);
00100 i += run;
00101 j = scantable[i];
00102 if(level<0){
00103 level= -level;
00104 level= (level*qscale*quant_matrix[j])>>3;
00105 level= (level-1)|1;
00106 level= -level;
00107 }else{
00108 level= (level*qscale*quant_matrix[j])>>3;
00109 level= (level-1)|1;
00110 }
00111 }
00112 if (i > 63){
00113 av_log(a->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", a->mb_x, a->mb_y);
00114 return -1;
00115 }
00116
00117 block[j] = level;
00118 }
00119 CLOSE_READER(re, &a->gb);
00120 }
00121 a->block_last_index[n] = i;
00122 return 0;
00123 }
00124
00125 static inline int decode_mb(MDECContext *a, DCTELEM block[6][64]){
00126 int i;
00127 const int block_index[6]= {5,4,0,1,2,3};
00128
00129 a->dsp.clear_blocks(block[0]);
00130
00131 for(i=0; i<6; i++){
00132 if( mdec_decode_block_intra(a, block[ block_index[i] ], block_index[i]) < 0)
00133 return -1;
00134 }
00135 return 0;
00136 }
00137
00138 static inline void idct_put(MDECContext *a, int mb_x, int mb_y){
00139 DCTELEM (*block)[64]= a->block;
00140 int linesize= a->picture.linesize[0];
00141
00142 uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
00143 uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
00144 uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
00145
00146 a->dsp.idct_put(dest_y , linesize, block[0]);
00147 a->dsp.idct_put(dest_y + 8, linesize, block[1]);
00148 a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
00149 a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
00150
00151 if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
00152 a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
00153 a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
00154 }
00155 }
00156
00157 static int decode_frame(AVCodecContext *avctx,
00158 void *data, int *data_size,
00159 uint8_t *buf, int buf_size)
00160 {
00161 MDECContext * const a = avctx->priv_data;
00162 AVFrame *picture = data;
00163 AVFrame * const p= (AVFrame*)&a->picture;
00164 int i;
00165
00166 if(p->data[0])
00167 avctx->release_buffer(avctx, p);
00168
00169 p->reference= 0;
00170 if(avctx->get_buffer(avctx, p) < 0){
00171 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00172 return -1;
00173 }
00174 p->pict_type= I_TYPE;
00175 p->key_frame= 1;
00176 a->last_dc[0]=
00177 a->last_dc[1]=
00178 a->last_dc[2]= 0;
00179
00180 a->bitstream_buffer= av_fast_realloc(a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00181 for(i=0; i<buf_size; i+=2){
00182 a->bitstream_buffer[i] = buf[i+1];
00183 a->bitstream_buffer[i+1]= buf[i ];
00184 }
00185 init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
00186
00187
00188 skip_bits(&a->gb, 32);
00189
00190 a->qscale= get_bits(&a->gb, 16);
00191 a->version= get_bits(&a->gb, 16);
00192
00193
00194
00195 for(a->mb_x=0; a->mb_x<a->mb_width; a->mb_x++){
00196 for(a->mb_y=0; a->mb_y<a->mb_height; a->mb_y++){
00197 if( decode_mb(a, a->block) <0)
00198 return -1;
00199
00200 idct_put(a, a->mb_x, a->mb_y);
00201 }
00202 }
00203
00204
00205
00206
00207 *picture= *(AVFrame*)&a->picture;
00208 *data_size = sizeof(AVPicture);
00209
00210 emms_c();
00211
00212 return (get_bits_count(&a->gb)+31)/32*4;
00213 }
00214
00215 static void mdec_common_init(AVCodecContext *avctx){
00216 MDECContext * const a = avctx->priv_data;
00217
00218 dsputil_init(&a->dsp, avctx);
00219
00220 a->mb_width = (avctx->coded_width + 15) / 16;
00221 a->mb_height = (avctx->coded_height + 15) / 16;
00222
00223 avctx->coded_frame= (AVFrame*)&a->picture;
00224 a->avctx= avctx;
00225 }
00226
00227 static int decode_init(AVCodecContext *avctx){
00228 MDECContext * const a = avctx->priv_data;
00229 AVFrame *p= (AVFrame*)&a->picture;
00230
00231 mdec_common_init(avctx);
00232 init_vlcs();
00233 ff_init_scantable(a->dsp.idct_permutation, &a->scantable, ff_zigzag_direct);
00234
00235
00236
00237
00238
00239
00240 p->qstride= a->mb_width;
00241 p->qscale_table= av_mallocz( p->qstride * a->mb_height);
00242
00243 return 0;
00244 }
00245
00246 static int decode_end(AVCodecContext *avctx){
00247 MDECContext * const a = avctx->priv_data;
00248
00249 av_freep(&a->bitstream_buffer);
00250 av_freep(&a->picture.qscale_table);
00251 a->bitstream_buffer_size=0;
00252
00253 return 0;
00254 }
00255
00256 AVCodec mdec_decoder = {
00257 "mdec",
00258 CODEC_TYPE_VIDEO,
00259 CODEC_ID_MDEC,
00260 sizeof(MDECContext),
00261 decode_init,
00262 NULL,
00263 decode_end,
00264 decode_frame,
00265 CODEC_CAP_DR1,
00266 };
00267