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 uint8_t *bitstream_buffer;
00049 unsigned int bitstream_buffer_size;
00050 int block_last_index[6];
00051 } MDECContext;
00052
00053
00054 static inline int mdec_decode_block_intra(MDECContext *a, DCTELEM *block, int n)
00055 {
00056 int level, diff, i, j, run;
00057 int component;
00058 RLTable *rl = &ff_rl_mpeg1;
00059 uint8_t * const scantable= a->scantable.permutated;
00060 const uint16_t *quant_matrix= ff_mpeg1_default_intra_matrix;
00061 const int qscale= a->qscale;
00062
00063
00064 if(a->version==2){
00065 block[0]= 2*get_sbits(&a->gb, 10) + 1024;
00066 }else{
00067 component = (n <= 3 ? 0 : n - 4 + 1);
00068 diff = decode_dc(&a->gb, component);
00069 if (diff >= 0xffff)
00070 return -1;
00071 a->last_dc[component]+= diff;
00072 block[0] = a->last_dc[component]<<3;
00073 }
00074
00075 i = 0;
00076 {
00077 OPEN_READER(re, &a->gb);
00078
00079 for(;;) {
00080 UPDATE_CACHE(re, &a->gb);
00081 GET_RL_VLC(level, run, re, &a->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00082
00083 if(level == 127){
00084 break;
00085 } else if(level != 0) {
00086 i += run;
00087 j = scantable[i];
00088 level= (level*qscale*quant_matrix[j])>>3;
00089 level = (level ^ SHOW_SBITS(re, &a->gb, 1)) - SHOW_SBITS(re, &a->gb, 1);
00090 LAST_SKIP_BITS(re, &a->gb, 1);
00091 } else {
00092
00093 run = SHOW_UBITS(re, &a->gb, 6)+1; LAST_SKIP_BITS(re, &a->gb, 6);
00094 UPDATE_CACHE(re, &a->gb);
00095 level = SHOW_SBITS(re, &a->gb, 10); SKIP_BITS(re, &a->gb, 10);
00096 i += run;
00097 j = scantable[i];
00098 if(level<0){
00099 level= -level;
00100 level= (level*qscale*quant_matrix[j])>>3;
00101 level= (level-1)|1;
00102 level= -level;
00103 }else{
00104 level= (level*qscale*quant_matrix[j])>>3;
00105 level= (level-1)|1;
00106 }
00107 }
00108 if (i > 63){
00109 av_log(a->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", a->mb_x, a->mb_y);
00110 return -1;
00111 }
00112
00113 block[j] = level;
00114 }
00115 CLOSE_READER(re, &a->gb);
00116 }
00117 a->block_last_index[n] = i;
00118 return 0;
00119 }
00120
00121 static inline int decode_mb(MDECContext *a, DCTELEM block[6][64]){
00122 int i;
00123 const int block_index[6]= {5,4,0,1,2,3};
00124
00125 a->dsp.clear_blocks(block[0]);
00126
00127 for(i=0; i<6; i++){
00128 if( mdec_decode_block_intra(a, block[ block_index[i] ], block_index[i]) < 0)
00129 return -1;
00130 }
00131 return 0;
00132 }
00133
00134 static inline void idct_put(MDECContext *a, int mb_x, int mb_y){
00135 DCTELEM (*block)[64]= a->block;
00136 int linesize= a->picture.linesize[0];
00137
00138 uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
00139 uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
00140 uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
00141
00142 a->dsp.idct_put(dest_y , linesize, block[0]);
00143 a->dsp.idct_put(dest_y + 8, linesize, block[1]);
00144 a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
00145 a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
00146
00147 if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
00148 a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
00149 a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
00150 }
00151 }
00152
00153 static int decode_frame(AVCodecContext *avctx,
00154 void *data, int *data_size,
00155 AVPacket *avpkt)
00156 {
00157 const uint8_t *buf = avpkt->data;
00158 int buf_size = avpkt->size;
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 av_fast_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00176 if (!a->bitstream_buffer)
00177 return AVERROR(ENOMEM);
00178 for(i=0; i<buf_size; i+=2){
00179 a->bitstream_buffer[i] = buf[i+1];
00180 a->bitstream_buffer[i+1]= buf[i ];
00181 }
00182 init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
00183
00184
00185 skip_bits(&a->gb, 32);
00186
00187 a->qscale= get_bits(&a->gb, 16);
00188 a->version= get_bits(&a->gb, 16);
00189
00190 a->last_dc[0]=
00191 a->last_dc[1]=
00192 a->last_dc[2]= 128;
00193
00194 for(a->mb_x=0; a->mb_x<a->mb_width; a->mb_x++){
00195 for(a->mb_y=0; a->mb_y<a->mb_height; a->mb_y++){
00196 if( decode_mb(a, a->block) <0)
00197 return -1;
00198
00199 idct_put(a, a->mb_x, a->mb_y);
00200 }
00201 }
00202
00203 p->quality= a->qscale * FF_QP2LAMBDA;
00204 memset(p->qscale_table, a->qscale, a->mb_width);
00205
00206 *picture = a->picture;
00207 *data_size = sizeof(AVPicture);
00208
00209 return (get_bits_count(&a->gb)+31)/32*4;
00210 }
00211
00212 static av_cold void mdec_common_init(AVCodecContext *avctx){
00213 MDECContext * const a = avctx->priv_data;
00214
00215 dsputil_init(&a->dsp, avctx);
00216
00217 a->mb_width = (avctx->coded_width + 15) / 16;
00218 a->mb_height = (avctx->coded_height + 15) / 16;
00219
00220 avctx->coded_frame= &a->picture;
00221 a->avctx= avctx;
00222 }
00223
00224 static av_cold int decode_init(AVCodecContext *avctx){
00225 MDECContext * const a = avctx->priv_data;
00226 AVFrame *p= &a->picture;
00227
00228 mdec_common_init(avctx);
00229 ff_mpeg12_init_vlcs();
00230 ff_init_scantable(a->dsp.idct_permutation, &a->scantable, ff_zigzag_direct);
00231
00232 p->qstride= 0;
00233 p->qscale_table= av_mallocz(a->mb_width);
00234 avctx->pix_fmt= PIX_FMT_YUV420P;
00235
00236 return 0;
00237 }
00238
00239 static av_cold int decode_end(AVCodecContext *avctx){
00240 MDECContext * const a = avctx->priv_data;
00241
00242 if(a->picture.data[0])
00243 avctx->release_buffer(avctx, &a->picture);
00244 av_freep(&a->bitstream_buffer);
00245 av_freep(&a->picture.qscale_table);
00246 a->bitstream_buffer_size=0;
00247
00248 return 0;
00249 }
00250
00251 AVCodec mdec_decoder = {
00252 "mdec",
00253 AVMEDIA_TYPE_VIDEO,
00254 CODEC_ID_MDEC,
00255 sizeof(MDECContext),
00256 decode_init,
00257 NULL,
00258 decode_end,
00259 decode_frame,
00260 CODEC_CAP_DR1,
00261 .long_name= NULL_IF_CONFIG_SMALL("Sony PlayStation MDEC (Motion DECoder)"),
00262 };
00263