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