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