00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/attributes.h"
00027 #include "libavutil/mem.h"
00028
00029 #include "asv.h"
00030 #include "avcodec.h"
00031 #include "put_bits.h"
00032 #include "dsputil.h"
00033 #include "internal.h"
00034 #include "mathops.h"
00035 #include "mpeg12data.h"
00036
00037 #define VLC_BITS 6
00038 #define ASV2_LEVEL_VLC_BITS 10
00039
00040 static VLC ccp_vlc;
00041 static VLC level_vlc;
00042 static VLC dc_ccp_vlc;
00043 static VLC ac_ccp_vlc;
00044 static VLC asv2_level_vlc;
00045
00046 static av_cold void init_vlcs(ASV1Context *a){
00047 static int done = 0;
00048
00049 if (!done) {
00050 done = 1;
00051
00052 INIT_VLC_STATIC(&ccp_vlc, VLC_BITS, 17,
00053 &ff_asv_ccp_tab[0][1], 2, 1,
00054 &ff_asv_ccp_tab[0][0], 2, 1, 64);
00055 INIT_VLC_STATIC(&dc_ccp_vlc, VLC_BITS, 8,
00056 &ff_asv_dc_ccp_tab[0][1], 2, 1,
00057 &ff_asv_dc_ccp_tab[0][0], 2, 1, 64);
00058 INIT_VLC_STATIC(&ac_ccp_vlc, VLC_BITS, 16,
00059 &ff_asv_ac_ccp_tab[0][1], 2, 1,
00060 &ff_asv_ac_ccp_tab[0][0], 2, 1, 64);
00061 INIT_VLC_STATIC(&level_vlc, VLC_BITS, 7,
00062 &ff_asv_level_tab[0][1], 2, 1,
00063 &ff_asv_level_tab[0][0], 2, 1, 64);
00064 INIT_VLC_STATIC(&asv2_level_vlc, ASV2_LEVEL_VLC_BITS, 63,
00065 &ff_asv2_level_tab[0][1], 2, 1,
00066 &ff_asv2_level_tab[0][0], 2, 1, 1024);
00067 }
00068 }
00069
00070
00071 static inline int asv2_get_bits(GetBitContext *gb, int n){
00072 return ff_reverse[ get_bits(gb, n) << (8-n) ];
00073 }
00074
00075 static inline int asv1_get_level(GetBitContext *gb){
00076 int code= get_vlc2(gb, level_vlc.table, VLC_BITS, 1);
00077
00078 if(code==3) return get_sbits(gb, 8);
00079 else return code - 3;
00080 }
00081
00082 static inline int asv2_get_level(GetBitContext *gb){
00083 int code= get_vlc2(gb, asv2_level_vlc.table, ASV2_LEVEL_VLC_BITS, 1);
00084
00085 if(code==31) return (int8_t)asv2_get_bits(gb, 8);
00086 else return code - 31;
00087 }
00088
00089 static inline int asv1_decode_block(ASV1Context *a, DCTELEM block[64]){
00090 int i;
00091
00092 block[0]= 8*get_bits(&a->gb, 8);
00093
00094 for(i=0; i<11; i++){
00095 const int ccp= get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1);
00096
00097 if(ccp){
00098 if(ccp == 16) break;
00099 if(ccp < 0 || i>=10){
00100 av_log(a->avctx, AV_LOG_ERROR, "coded coeff pattern damaged\n");
00101 return -1;
00102 }
00103
00104 if(ccp&8) block[a->scantable.permutated[4*i+0]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+0])>>4;
00105 if(ccp&4) block[a->scantable.permutated[4*i+1]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+1])>>4;
00106 if(ccp&2) block[a->scantable.permutated[4*i+2]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+2])>>4;
00107 if(ccp&1) block[a->scantable.permutated[4*i+3]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+3])>>4;
00108 }
00109 }
00110
00111 return 0;
00112 }
00113
00114 static inline int asv2_decode_block(ASV1Context *a, DCTELEM block[64]){
00115 int i, count, ccp;
00116
00117 count= asv2_get_bits(&a->gb, 4);
00118
00119 block[0]= 8*asv2_get_bits(&a->gb, 8);
00120
00121 ccp= get_vlc2(&a->gb, dc_ccp_vlc.table, VLC_BITS, 1);
00122 if(ccp){
00123 if(ccp&4) block[a->scantable.permutated[1]]= (asv2_get_level(&a->gb) * a->intra_matrix[1])>>4;
00124 if(ccp&2) block[a->scantable.permutated[2]]= (asv2_get_level(&a->gb) * a->intra_matrix[2])>>4;
00125 if(ccp&1) block[a->scantable.permutated[3]]= (asv2_get_level(&a->gb) * a->intra_matrix[3])>>4;
00126 }
00127
00128 for(i=1; i<count+1; i++){
00129 const int ccp= get_vlc2(&a->gb, ac_ccp_vlc.table, VLC_BITS, 1);
00130
00131 if(ccp){
00132 if(ccp&8) block[a->scantable.permutated[4*i+0]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+0])>>4;
00133 if(ccp&4) block[a->scantable.permutated[4*i+1]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+1])>>4;
00134 if(ccp&2) block[a->scantable.permutated[4*i+2]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+2])>>4;
00135 if(ccp&1) block[a->scantable.permutated[4*i+3]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+3])>>4;
00136 }
00137 }
00138
00139 return 0;
00140 }
00141
00142 static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64]){
00143 int i;
00144
00145 a->dsp.clear_blocks(block[0]);
00146
00147 if(a->avctx->codec_id == AV_CODEC_ID_ASV1){
00148 for(i=0; i<6; i++){
00149 if( asv1_decode_block(a, block[i]) < 0)
00150 return -1;
00151 }
00152 }else{
00153 for(i=0; i<6; i++){
00154 if( asv2_decode_block(a, block[i]) < 0)
00155 return -1;
00156 }
00157 }
00158 return 0;
00159 }
00160
00161 static inline void idct_put(ASV1Context *a, int mb_x, int mb_y){
00162 DCTELEM (*block)[64]= a->block;
00163 int linesize= a->picture.linesize[0];
00164
00165 uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
00166 uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
00167 uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
00168
00169 a->dsp.idct_put(dest_y , linesize, block[0]);
00170 a->dsp.idct_put(dest_y + 8, linesize, block[1]);
00171 a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
00172 a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
00173
00174 if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
00175 a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
00176 a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
00177 }
00178 }
00179
00180 static int decode_frame(AVCodecContext *avctx,
00181 void *data, int *got_frame,
00182 AVPacket *avpkt)
00183 {
00184 const uint8_t *buf = avpkt->data;
00185 int buf_size = avpkt->size;
00186 ASV1Context * const a = avctx->priv_data;
00187 AVFrame *picture = data;
00188 AVFrame * const p= &a->picture;
00189 int mb_x, mb_y;
00190
00191 if(p->data[0])
00192 avctx->release_buffer(avctx, p);
00193
00194 p->reference= 0;
00195 if(ff_get_buffer(avctx, p) < 0){
00196 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00197 return -1;
00198 }
00199 p->pict_type= AV_PICTURE_TYPE_I;
00200 p->key_frame= 1;
00201
00202 av_fast_padded_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size,
00203 buf_size);
00204 if (!a->bitstream_buffer)
00205 return AVERROR(ENOMEM);
00206
00207 if(avctx->codec_id == AV_CODEC_ID_ASV1)
00208 a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
00209 else{
00210 int i;
00211 for(i=0; i<buf_size; i++)
00212 a->bitstream_buffer[i]= ff_reverse[ buf[i] ];
00213 }
00214
00215 init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
00216
00217 for(mb_y=0; mb_y<a->mb_height2; mb_y++){
00218 for(mb_x=0; mb_x<a->mb_width2; mb_x++){
00219 if( decode_mb(a, a->block) <0)
00220 return -1;
00221
00222 idct_put(a, mb_x, mb_y);
00223 }
00224 }
00225
00226 if(a->mb_width2 != a->mb_width){
00227 mb_x= a->mb_width2;
00228 for(mb_y=0; mb_y<a->mb_height2; mb_y++){
00229 if( decode_mb(a, a->block) <0)
00230 return -1;
00231
00232 idct_put(a, mb_x, mb_y);
00233 }
00234 }
00235
00236 if(a->mb_height2 != a->mb_height){
00237 mb_y= a->mb_height2;
00238 for(mb_x=0; mb_x<a->mb_width; mb_x++){
00239 if( decode_mb(a, a->block) <0)
00240 return -1;
00241
00242 idct_put(a, mb_x, mb_y);
00243 }
00244 }
00245
00246 *picture = a->picture;
00247 *got_frame = 1;
00248
00249 emms_c();
00250
00251 return (get_bits_count(&a->gb)+31)/32*4;
00252 }
00253
00254 static av_cold int decode_init(AVCodecContext *avctx){
00255 ASV1Context * const a = avctx->priv_data;
00256 AVFrame *p= &a->picture;
00257 int i;
00258 const int scale= avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
00259
00260 ff_asv_common_init(avctx);
00261 init_vlcs(a);
00262 ff_init_scantable(a->dsp.idct_permutation, &a->scantable, ff_asv_scantab);
00263 avctx->pix_fmt= AV_PIX_FMT_YUV420P;
00264
00265 if(avctx->extradata_size < 1 || (a->inv_qscale= avctx->extradata[0]) == 0){
00266 av_log(avctx, AV_LOG_ERROR, "illegal qscale 0\n");
00267 if(avctx->codec_id == AV_CODEC_ID_ASV1)
00268 a->inv_qscale= 6;
00269 else
00270 a->inv_qscale= 10;
00271 }
00272
00273 for(i=0; i<64; i++){
00274 int index = ff_asv_scantab[i];
00275
00276 a->intra_matrix[i]= 64*scale*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
00277 }
00278
00279 p->qstride= a->mb_width;
00280 p->qscale_table= av_malloc( p->qstride * a->mb_height);
00281 p->quality= (32*scale + a->inv_qscale/2)/a->inv_qscale;
00282 memset(p->qscale_table, p->quality, p->qstride*a->mb_height);
00283
00284 return 0;
00285 }
00286
00287 static av_cold int decode_end(AVCodecContext *avctx){
00288 ASV1Context * const a = avctx->priv_data;
00289
00290 av_freep(&a->bitstream_buffer);
00291 av_freep(&a->picture.qscale_table);
00292 a->bitstream_buffer_size=0;
00293
00294 if(a->picture.data[0])
00295 avctx->release_buffer(avctx, &a->picture);
00296
00297 return 0;
00298 }
00299
00300 #if CONFIG_ASV1_DECODER
00301 AVCodec ff_asv1_decoder = {
00302 .name = "asv1",
00303 .type = AVMEDIA_TYPE_VIDEO,
00304 .id = AV_CODEC_ID_ASV1,
00305 .priv_data_size = sizeof(ASV1Context),
00306 .init = decode_init,
00307 .close = decode_end,
00308 .decode = decode_frame,
00309 .capabilities = CODEC_CAP_DR1,
00310 .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
00311 };
00312 #endif
00313
00314 #if CONFIG_ASV2_DECODER
00315 AVCodec ff_asv2_decoder = {
00316 .name = "asv2",
00317 .type = AVMEDIA_TYPE_VIDEO,
00318 .id = AV_CODEC_ID_ASV2,
00319 .priv_data_size = sizeof(ASV1Context),
00320 .init = decode_init,
00321 .close = decode_end,
00322 .decode = decode_frame,
00323 .capabilities = CODEC_CAP_DR1,
00324 .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
00325 };
00326 #endif
00327