00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "internal.h"
00029 #include "libavutil/common.h"
00030 #include "put_bits.h"
00031 #include "dsputil.h"
00032 #include "mpeg12data.h"
00033
00034
00035
00036
00037 #define VLC_BITS 6
00038 #define ASV2_LEVEL_VLC_BITS 10
00039
00040 typedef struct ASV1Context{
00041 AVCodecContext *avctx;
00042 DSPContext dsp;
00043 AVFrame picture;
00044 PutBitContext pb;
00045 GetBitContext gb;
00046 ScanTable scantable;
00047 int inv_qscale;
00048 int mb_width;
00049 int mb_height;
00050 int mb_width2;
00051 int mb_height2;
00052 DECLARE_ALIGNED(16, DCTELEM, block)[6][64];
00053 uint16_t intra_matrix[64];
00054 int q_intra_matrix[64];
00055 uint8_t *bitstream_buffer;
00056 unsigned int bitstream_buffer_size;
00057 } ASV1Context;
00058
00059 static const uint8_t scantab[64]={
00060 0x00,0x08,0x01,0x09,0x10,0x18,0x11,0x19,
00061 0x02,0x0A,0x03,0x0B,0x12,0x1A,0x13,0x1B,
00062 0x04,0x0C,0x05,0x0D,0x20,0x28,0x21,0x29,
00063 0x06,0x0E,0x07,0x0F,0x14,0x1C,0x15,0x1D,
00064 0x22,0x2A,0x23,0x2B,0x30,0x38,0x31,0x39,
00065 0x16,0x1E,0x17,0x1F,0x24,0x2C,0x25,0x2D,
00066 0x32,0x3A,0x33,0x3B,0x26,0x2E,0x27,0x2F,
00067 0x34,0x3C,0x35,0x3D,0x36,0x3E,0x37,0x3F,
00068 };
00069
00070
00071 static const uint8_t ccp_tab[17][2]={
00072 {0x2,2}, {0x7,5}, {0xB,5}, {0x3,5},
00073 {0xD,5}, {0x5,5}, {0x9,5}, {0x1,5},
00074 {0xE,5}, {0x6,5}, {0xA,5}, {0x2,5},
00075 {0xC,5}, {0x4,5}, {0x8,5}, {0x3,2},
00076 {0xF,5},
00077 };
00078
00079 static const uint8_t level_tab[7][2]={
00080 {3,4}, {3,3}, {3,2}, {0,3}, {2,2}, {2,3}, {2,4}
00081 };
00082
00083 static const uint8_t dc_ccp_tab[8][2]={
00084 {0x1,2}, {0xD,4}, {0xF,4}, {0xC,4},
00085 {0x5,3}, {0xE,4}, {0x4,3}, {0x0,2},
00086 };
00087
00088 static const uint8_t ac_ccp_tab[16][2]={
00089 {0x00,2}, {0x3B,6}, {0x0A,4}, {0x3A,6},
00090 {0x02,3}, {0x39,6}, {0x3C,6}, {0x38,6},
00091 {0x03,3}, {0x3D,6}, {0x08,4}, {0x1F,5},
00092 {0x09,4}, {0x0B,4}, {0x0D,4}, {0x0C,4},
00093 };
00094
00095 static const uint8_t asv2_level_tab[63][2]={
00096 {0x3F,10},{0x2F,10},{0x37,10},{0x27,10},{0x3B,10},{0x2B,10},{0x33,10},{0x23,10},
00097 {0x3D,10},{0x2D,10},{0x35,10},{0x25,10},{0x39,10},{0x29,10},{0x31,10},{0x21,10},
00098 {0x1F, 8},{0x17, 8},{0x1B, 8},{0x13, 8},{0x1D, 8},{0x15, 8},{0x19, 8},{0x11, 8},
00099 {0x0F, 6},{0x0B, 6},{0x0D, 6},{0x09, 6},
00100 {0x07, 4},{0x05, 4},
00101 {0x03, 2},
00102 {0x00, 5},
00103 {0x02, 2},
00104 {0x04, 4},{0x06, 4},
00105 {0x08, 6},{0x0C, 6},{0x0A, 6},{0x0E, 6},
00106 {0x10, 8},{0x18, 8},{0x14, 8},{0x1C, 8},{0x12, 8},{0x1A, 8},{0x16, 8},{0x1E, 8},
00107 {0x20,10},{0x30,10},{0x28,10},{0x38,10},{0x24,10},{0x34,10},{0x2C,10},{0x3C,10},
00108 {0x22,10},{0x32,10},{0x2A,10},{0x3A,10},{0x26,10},{0x36,10},{0x2E,10},{0x3E,10},
00109 };
00110
00111
00112 static VLC ccp_vlc;
00113 static VLC level_vlc;
00114 static VLC dc_ccp_vlc;
00115 static VLC ac_ccp_vlc;
00116 static VLC asv2_level_vlc;
00117
00118 static av_cold void init_vlcs(ASV1Context *a){
00119 static int done = 0;
00120
00121 if (!done) {
00122 done = 1;
00123
00124 INIT_VLC_STATIC(&ccp_vlc, VLC_BITS, 17,
00125 &ccp_tab[0][1], 2, 1,
00126 &ccp_tab[0][0], 2, 1, 64);
00127 INIT_VLC_STATIC(&dc_ccp_vlc, VLC_BITS, 8,
00128 &dc_ccp_tab[0][1], 2, 1,
00129 &dc_ccp_tab[0][0], 2, 1, 64);
00130 INIT_VLC_STATIC(&ac_ccp_vlc, VLC_BITS, 16,
00131 &ac_ccp_tab[0][1], 2, 1,
00132 &ac_ccp_tab[0][0], 2, 1, 64);
00133 INIT_VLC_STATIC(&level_vlc, VLC_BITS, 7,
00134 &level_tab[0][1], 2, 1,
00135 &level_tab[0][0], 2, 1, 64);
00136 INIT_VLC_STATIC(&asv2_level_vlc, ASV2_LEVEL_VLC_BITS, 63,
00137 &asv2_level_tab[0][1], 2, 1,
00138 &asv2_level_tab[0][0], 2, 1, 1024);
00139 }
00140 }
00141
00142
00143 static inline int asv2_get_bits(GetBitContext *gb, int n){
00144 return av_reverse[ get_bits(gb, n) << (8-n) ];
00145 }
00146
00147 static inline void asv2_put_bits(PutBitContext *pb, int n, int v){
00148 put_bits(pb, n, av_reverse[ v << (8-n) ]);
00149 }
00150
00151 static inline int asv1_get_level(GetBitContext *gb){
00152 int code= get_vlc2(gb, level_vlc.table, VLC_BITS, 1);
00153
00154 if(code==3) return get_sbits(gb, 8);
00155 else return code - 3;
00156 }
00157
00158 static inline int asv2_get_level(GetBitContext *gb){
00159 int code= get_vlc2(gb, asv2_level_vlc.table, ASV2_LEVEL_VLC_BITS, 1);
00160
00161 if(code==31) return (int8_t)asv2_get_bits(gb, 8);
00162 else return code - 31;
00163 }
00164
00165 static inline void asv1_put_level(PutBitContext *pb, int level){
00166 unsigned int index= level + 3;
00167
00168 if(index <= 6) put_bits(pb, level_tab[index][1], level_tab[index][0]);
00169 else{
00170 put_bits(pb, level_tab[3][1], level_tab[3][0]);
00171 put_sbits(pb, 8, level);
00172 }
00173 }
00174
00175 static inline void asv2_put_level(PutBitContext *pb, int level){
00176 unsigned int index= level + 31;
00177
00178 if(index <= 62) put_bits(pb, asv2_level_tab[index][1], asv2_level_tab[index][0]);
00179 else{
00180 put_bits(pb, asv2_level_tab[31][1], asv2_level_tab[31][0]);
00181 asv2_put_bits(pb, 8, level&0xFF);
00182 }
00183 }
00184
00185 static inline int asv1_decode_block(ASV1Context *a, DCTELEM block[64]){
00186 int i;
00187
00188 block[0]= 8*get_bits(&a->gb, 8);
00189
00190 for(i=0; i<11; i++){
00191 const int ccp= get_vlc2(&a->gb, ccp_vlc.table, VLC_BITS, 1);
00192
00193 if(ccp){
00194 if(ccp == 16) break;
00195 if(ccp < 0 || i>=10){
00196 av_log(a->avctx, AV_LOG_ERROR, "coded coeff pattern damaged\n");
00197 return -1;
00198 }
00199
00200 if(ccp&8) block[a->scantable.permutated[4*i+0]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+0])>>4;
00201 if(ccp&4) block[a->scantable.permutated[4*i+1]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+1])>>4;
00202 if(ccp&2) block[a->scantable.permutated[4*i+2]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+2])>>4;
00203 if(ccp&1) block[a->scantable.permutated[4*i+3]]= (asv1_get_level(&a->gb) * a->intra_matrix[4*i+3])>>4;
00204 }
00205 }
00206
00207 return 0;
00208 }
00209
00210 static inline int asv2_decode_block(ASV1Context *a, DCTELEM block[64]){
00211 int i, count, ccp;
00212
00213 count= asv2_get_bits(&a->gb, 4);
00214
00215 block[0]= 8*asv2_get_bits(&a->gb, 8);
00216
00217 ccp= get_vlc2(&a->gb, dc_ccp_vlc.table, VLC_BITS, 1);
00218 if(ccp){
00219 if(ccp&4) block[a->scantable.permutated[1]]= (asv2_get_level(&a->gb) * a->intra_matrix[1])>>4;
00220 if(ccp&2) block[a->scantable.permutated[2]]= (asv2_get_level(&a->gb) * a->intra_matrix[2])>>4;
00221 if(ccp&1) block[a->scantable.permutated[3]]= (asv2_get_level(&a->gb) * a->intra_matrix[3])>>4;
00222 }
00223
00224 for(i=1; i<count+1; i++){
00225 const int ccp= get_vlc2(&a->gb, ac_ccp_vlc.table, VLC_BITS, 1);
00226
00227 if(ccp){
00228 if(ccp&8) block[a->scantable.permutated[4*i+0]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+0])>>4;
00229 if(ccp&4) block[a->scantable.permutated[4*i+1]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+1])>>4;
00230 if(ccp&2) block[a->scantable.permutated[4*i+2]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+2])>>4;
00231 if(ccp&1) block[a->scantable.permutated[4*i+3]]= (asv2_get_level(&a->gb) * a->intra_matrix[4*i+3])>>4;
00232 }
00233 }
00234
00235 return 0;
00236 }
00237
00238 static inline void asv1_encode_block(ASV1Context *a, DCTELEM block[64]){
00239 int i;
00240 int nc_count=0;
00241
00242 put_bits(&a->pb, 8, (block[0] + 32)>>6);
00243 block[0]= 0;
00244
00245 for(i=0; i<10; i++){
00246 const int index= scantab[4*i];
00247 int ccp=0;
00248
00249 if( (block[index + 0] = (block[index + 0]*a->q_intra_matrix[index + 0] + (1<<15))>>16) ) ccp |= 8;
00250 if( (block[index + 8] = (block[index + 8]*a->q_intra_matrix[index + 8] + (1<<15))>>16) ) ccp |= 4;
00251 if( (block[index + 1] = (block[index + 1]*a->q_intra_matrix[index + 1] + (1<<15))>>16) ) ccp |= 2;
00252 if( (block[index + 9] = (block[index + 9]*a->q_intra_matrix[index + 9] + (1<<15))>>16) ) ccp |= 1;
00253
00254 if(ccp){
00255 for(;nc_count; nc_count--)
00256 put_bits(&a->pb, ccp_tab[0][1], ccp_tab[0][0]);
00257
00258 put_bits(&a->pb, ccp_tab[ccp][1], ccp_tab[ccp][0]);
00259
00260 if(ccp&8) asv1_put_level(&a->pb, block[index + 0]);
00261 if(ccp&4) asv1_put_level(&a->pb, block[index + 8]);
00262 if(ccp&2) asv1_put_level(&a->pb, block[index + 1]);
00263 if(ccp&1) asv1_put_level(&a->pb, block[index + 9]);
00264 }else{
00265 nc_count++;
00266 }
00267 }
00268 put_bits(&a->pb, ccp_tab[16][1], ccp_tab[16][0]);
00269 }
00270
00271 static inline void asv2_encode_block(ASV1Context *a, DCTELEM block[64]){
00272 int i;
00273 int count=0;
00274
00275 for(count=63; count>3; count--){
00276 const int index= scantab[count];
00277
00278 if( (block[index]*a->q_intra_matrix[index] + (1<<15))>>16 )
00279 break;
00280 }
00281
00282 count >>= 2;
00283
00284 asv2_put_bits(&a->pb, 4, count);
00285 asv2_put_bits(&a->pb, 8, (block[0] + 32)>>6);
00286 block[0]= 0;
00287
00288 for(i=0; i<=count; i++){
00289 const int index= scantab[4*i];
00290 int ccp=0;
00291
00292 if( (block[index + 0] = (block[index + 0]*a->q_intra_matrix[index + 0] + (1<<15))>>16) ) ccp |= 8;
00293 if( (block[index + 8] = (block[index + 8]*a->q_intra_matrix[index + 8] + (1<<15))>>16) ) ccp |= 4;
00294 if( (block[index + 1] = (block[index + 1]*a->q_intra_matrix[index + 1] + (1<<15))>>16) ) ccp |= 2;
00295 if( (block[index + 9] = (block[index + 9]*a->q_intra_matrix[index + 9] + (1<<15))>>16) ) ccp |= 1;
00296
00297 assert(i || ccp<8);
00298 if(i) put_bits(&a->pb, ac_ccp_tab[ccp][1], ac_ccp_tab[ccp][0]);
00299 else put_bits(&a->pb, dc_ccp_tab[ccp][1], dc_ccp_tab[ccp][0]);
00300
00301 if(ccp){
00302 if(ccp&8) asv2_put_level(&a->pb, block[index + 0]);
00303 if(ccp&4) asv2_put_level(&a->pb, block[index + 8]);
00304 if(ccp&2) asv2_put_level(&a->pb, block[index + 1]);
00305 if(ccp&1) asv2_put_level(&a->pb, block[index + 9]);
00306 }
00307 }
00308 }
00309
00310 static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64]){
00311 int i;
00312
00313 a->dsp.clear_blocks(block[0]);
00314
00315 if(a->avctx->codec_id == CODEC_ID_ASV1){
00316 for(i=0; i<6; i++){
00317 if( asv1_decode_block(a, block[i]) < 0)
00318 return -1;
00319 }
00320 }else{
00321 for(i=0; i<6; i++){
00322 if( asv2_decode_block(a, block[i]) < 0)
00323 return -1;
00324 }
00325 }
00326 return 0;
00327 }
00328
00329 #define MAX_MB_SIZE (30*16*16*3/2/8)
00330
00331 static inline int encode_mb(ASV1Context *a, DCTELEM block[6][64]){
00332 int i;
00333
00334 if (a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb)>>3) < MAX_MB_SIZE) {
00335 av_log(a->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00336 return -1;
00337 }
00338
00339 if(a->avctx->codec_id == CODEC_ID_ASV1){
00340 for(i=0; i<6; i++)
00341 asv1_encode_block(a, block[i]);
00342 }else{
00343 for(i=0; i<6; i++)
00344 asv2_encode_block(a, block[i]);
00345 }
00346 return 0;
00347 }
00348
00349 static inline void idct_put(ASV1Context *a, int mb_x, int mb_y){
00350 DCTELEM (*block)[64]= a->block;
00351 int linesize= a->picture.linesize[0];
00352
00353 uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
00354 uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
00355 uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
00356
00357 a->dsp.idct_put(dest_y , linesize, block[0]);
00358 a->dsp.idct_put(dest_y + 8, linesize, block[1]);
00359 a->dsp.idct_put(dest_y + 8*linesize , linesize, block[2]);
00360 a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
00361
00362 if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
00363 a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
00364 a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
00365 }
00366 }
00367
00368 static inline void dct_get(ASV1Context *a, int mb_x, int mb_y){
00369 DCTELEM (*block)[64]= a->block;
00370 int linesize= a->picture.linesize[0];
00371 int i;
00372
00373 uint8_t *ptr_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16;
00374 uint8_t *ptr_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
00375 uint8_t *ptr_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
00376
00377 a->dsp.get_pixels(block[0], ptr_y , linesize);
00378 a->dsp.get_pixels(block[1], ptr_y + 8, linesize);
00379 a->dsp.get_pixels(block[2], ptr_y + 8*linesize , linesize);
00380 a->dsp.get_pixels(block[3], ptr_y + 8*linesize + 8, linesize);
00381 for(i=0; i<4; i++)
00382 a->dsp.fdct(block[i]);
00383
00384 if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
00385 a->dsp.get_pixels(block[4], ptr_cb, a->picture.linesize[1]);
00386 a->dsp.get_pixels(block[5], ptr_cr, a->picture.linesize[2]);
00387 for(i=4; i<6; i++)
00388 a->dsp.fdct(block[i]);
00389 }
00390 }
00391
00392 static int decode_frame(AVCodecContext *avctx,
00393 void *data, int *data_size,
00394 AVPacket *avpkt)
00395 {
00396 const uint8_t *buf = avpkt->data;
00397 int buf_size = avpkt->size;
00398 ASV1Context * const a = avctx->priv_data;
00399 AVFrame *picture = data;
00400 AVFrame * const p= &a->picture;
00401 int mb_x, mb_y;
00402
00403 if(p->data[0])
00404 avctx->release_buffer(avctx, p);
00405
00406 p->reference= 0;
00407 if(avctx->get_buffer(avctx, p) < 0){
00408 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00409 return -1;
00410 }
00411 p->pict_type= AV_PICTURE_TYPE_I;
00412 p->key_frame= 1;
00413
00414 av_fast_padded_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size,
00415 buf_size);
00416 if (!a->bitstream_buffer)
00417 return AVERROR(ENOMEM);
00418
00419 if(avctx->codec_id == CODEC_ID_ASV1)
00420 a->dsp.bswap_buf((uint32_t*)a->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
00421 else{
00422 int i;
00423 for(i=0; i<buf_size; i++)
00424 a->bitstream_buffer[i]= av_reverse[ buf[i] ];
00425 }
00426
00427 init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
00428
00429 for(mb_y=0; mb_y<a->mb_height2; mb_y++){
00430 for(mb_x=0; mb_x<a->mb_width2; mb_x++){
00431 if( decode_mb(a, a->block) <0)
00432 return -1;
00433
00434 idct_put(a, mb_x, mb_y);
00435 }
00436 }
00437
00438 if(a->mb_width2 != a->mb_width){
00439 mb_x= a->mb_width2;
00440 for(mb_y=0; mb_y<a->mb_height2; mb_y++){
00441 if( decode_mb(a, a->block) <0)
00442 return -1;
00443
00444 idct_put(a, mb_x, mb_y);
00445 }
00446 }
00447
00448 if(a->mb_height2 != a->mb_height){
00449 mb_y= a->mb_height2;
00450 for(mb_x=0; mb_x<a->mb_width; mb_x++){
00451 if( decode_mb(a, a->block) <0)
00452 return -1;
00453
00454 idct_put(a, mb_x, mb_y);
00455 }
00456 }
00457
00458 *picture = a->picture;
00459 *data_size = sizeof(AVPicture);
00460
00461 emms_c();
00462
00463 return (get_bits_count(&a->gb)+31)/32*4;
00464 }
00465
00466 #if CONFIG_ASV1_ENCODER || CONFIG_ASV2_ENCODER
00467 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00468 const AVFrame *pict, int *got_packet)
00469 {
00470 ASV1Context * const a = avctx->priv_data;
00471 AVFrame * const p= &a->picture;
00472 int size, ret;
00473 int mb_x, mb_y;
00474
00475 if ((ret = ff_alloc_packet2(avctx, pkt, a->mb_height*a->mb_width*MAX_MB_SIZE +
00476 FF_MIN_BUFFER_SIZE)) < 0)
00477 return ret;
00478
00479 init_put_bits(&a->pb, pkt->data, pkt->size);
00480
00481 *p = *pict;
00482 p->pict_type= AV_PICTURE_TYPE_I;
00483 p->key_frame= 1;
00484
00485 for(mb_y=0; mb_y<a->mb_height2; mb_y++){
00486 for(mb_x=0; mb_x<a->mb_width2; mb_x++){
00487 dct_get(a, mb_x, mb_y);
00488 encode_mb(a, a->block);
00489 }
00490 }
00491
00492 if(a->mb_width2 != a->mb_width){
00493 mb_x= a->mb_width2;
00494 for(mb_y=0; mb_y<a->mb_height2; mb_y++){
00495 dct_get(a, mb_x, mb_y);
00496 encode_mb(a, a->block);
00497 }
00498 }
00499
00500 if(a->mb_height2 != a->mb_height){
00501 mb_y= a->mb_height2;
00502 for(mb_x=0; mb_x<a->mb_width; mb_x++){
00503 dct_get(a, mb_x, mb_y);
00504 encode_mb(a, a->block);
00505 }
00506 }
00507 emms_c();
00508
00509 avpriv_align_put_bits(&a->pb);
00510 while(put_bits_count(&a->pb)&31)
00511 put_bits(&a->pb, 8, 0);
00512
00513 size= put_bits_count(&a->pb)/32;
00514
00515 if(avctx->codec_id == CODEC_ID_ASV1)
00516 a->dsp.bswap_buf((uint32_t*)pkt->data, (uint32_t*)pkt->data, size);
00517 else{
00518 int i;
00519 for(i=0; i<4*size; i++)
00520 pkt->data[i] = av_reverse[pkt->data[i]];
00521 }
00522
00523 pkt->size = size*4;
00524 pkt->flags |= AV_PKT_FLAG_KEY;
00525 *got_packet = 1;
00526
00527 return 0;
00528 }
00529 #endif
00530
00531 static av_cold void common_init(AVCodecContext *avctx){
00532 ASV1Context * const a = avctx->priv_data;
00533
00534 ff_dsputil_init(&a->dsp, avctx);
00535
00536 a->mb_width = (avctx->width + 15) / 16;
00537 a->mb_height = (avctx->height + 15) / 16;
00538 a->mb_width2 = (avctx->width + 0) / 16;
00539 a->mb_height2 = (avctx->height + 0) / 16;
00540
00541 avctx->coded_frame= &a->picture;
00542 a->avctx= avctx;
00543 }
00544
00545 static av_cold int decode_init(AVCodecContext *avctx){
00546 ASV1Context * const a = avctx->priv_data;
00547 AVFrame *p= &a->picture;
00548 int i;
00549 const int scale= avctx->codec_id == CODEC_ID_ASV1 ? 1 : 2;
00550
00551 common_init(avctx);
00552 init_vlcs(a);
00553 ff_init_scantable(a->dsp.idct_permutation, &a->scantable, scantab);
00554 avctx->pix_fmt= PIX_FMT_YUV420P;
00555
00556 if(avctx->extradata_size < 1 || (a->inv_qscale= avctx->extradata[0]) == 0){
00557 av_log(avctx, AV_LOG_ERROR, "illegal qscale 0\n");
00558 if(avctx->codec_id == CODEC_ID_ASV1)
00559 a->inv_qscale= 6;
00560 else
00561 a->inv_qscale= 10;
00562 }
00563
00564 for(i=0; i<64; i++){
00565 int index= scantab[i];
00566
00567 a->intra_matrix[i]= 64*scale*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
00568 }
00569
00570 p->qstride= a->mb_width;
00571 p->qscale_table= av_malloc( p->qstride * a->mb_height);
00572 p->quality= (32*scale + a->inv_qscale/2)/a->inv_qscale;
00573 memset(p->qscale_table, p->quality, p->qstride*a->mb_height);
00574
00575 return 0;
00576 }
00577
00578 #if CONFIG_ASV1_ENCODER || CONFIG_ASV2_ENCODER
00579 static av_cold int encode_init(AVCodecContext *avctx){
00580 ASV1Context * const a = avctx->priv_data;
00581 int i;
00582 const int scale= avctx->codec_id == CODEC_ID_ASV1 ? 1 : 2;
00583
00584 common_init(avctx);
00585
00586 if(avctx->global_quality == 0) avctx->global_quality= 4*FF_QUALITY_SCALE;
00587
00588 a->inv_qscale= (32*scale*FF_QUALITY_SCALE + avctx->global_quality/2) / avctx->global_quality;
00589
00590 avctx->extradata= av_mallocz(8);
00591 avctx->extradata_size=8;
00592 ((uint32_t*)avctx->extradata)[0]= av_le2ne32(a->inv_qscale);
00593 ((uint32_t*)avctx->extradata)[1]= av_le2ne32(AV_RL32("ASUS"));
00594
00595 for(i=0; i<64; i++){
00596 int q= 32*scale*ff_mpeg1_default_intra_matrix[i];
00597 a->q_intra_matrix[i]= ((a->inv_qscale<<16) + q/2) / q;
00598 }
00599
00600 return 0;
00601 }
00602 #endif
00603
00604 static av_cold int decode_end(AVCodecContext *avctx){
00605 ASV1Context * const a = avctx->priv_data;
00606
00607 av_freep(&a->bitstream_buffer);
00608 av_freep(&a->picture.qscale_table);
00609 a->bitstream_buffer_size=0;
00610
00611 if(a->picture.data[0])
00612 avctx->release_buffer(avctx, &a->picture);
00613
00614 return 0;
00615 }
00616
00617 AVCodec ff_asv1_decoder = {
00618 .name = "asv1",
00619 .type = AVMEDIA_TYPE_VIDEO,
00620 .id = CODEC_ID_ASV1,
00621 .priv_data_size = sizeof(ASV1Context),
00622 .init = decode_init,
00623 .close = decode_end,
00624 .decode = decode_frame,
00625 .capabilities = CODEC_CAP_DR1,
00626 .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
00627 };
00628
00629 AVCodec ff_asv2_decoder = {
00630 .name = "asv2",
00631 .type = AVMEDIA_TYPE_VIDEO,
00632 .id = CODEC_ID_ASV2,
00633 .priv_data_size = sizeof(ASV1Context),
00634 .init = decode_init,
00635 .close = decode_end,
00636 .decode = decode_frame,
00637 .capabilities = CODEC_CAP_DR1,
00638 .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
00639 };
00640
00641 #if CONFIG_ASV1_ENCODER
00642 AVCodec ff_asv1_encoder = {
00643 .name = "asv1",
00644 .type = AVMEDIA_TYPE_VIDEO,
00645 .id = CODEC_ID_ASV1,
00646 .priv_data_size = sizeof(ASV1Context),
00647 .init = encode_init,
00648 .encode2 = encode_frame,
00649 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
00650 .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
00651 };
00652 #endif
00653
00654 #if CONFIG_ASV2_ENCODER
00655 AVCodec ff_asv2_encoder = {
00656 .name = "asv2",
00657 .type = AVMEDIA_TYPE_VIDEO,
00658 .id = CODEC_ID_ASV2,
00659 .priv_data_size = sizeof(ASV1Context),
00660 .init = encode_init,
00661 .encode2 = encode_frame,
00662 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
00663 .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
00664 };
00665 #endif