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