00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033
00034 #include "svq1.h"
00035 #include "svq1enc_cb.h"
00036
00037 #undef NDEBUG
00038 #include <assert.h>
00039
00040
00041 typedef struct SVQ1Context {
00042 MpegEncContext m;
00043 AVCodecContext *avctx;
00044 DSPContext dsp;
00045 AVFrame picture;
00046 AVFrame current_picture;
00047 AVFrame last_picture;
00048 PutBitContext pb;
00049 GetBitContext gb;
00050
00051 PutBitContext reorder_pb[6];
00052
00053 int frame_width;
00054 int frame_height;
00055
00056
00057 int y_block_width;
00058 int y_block_height;
00059
00060
00061 int c_block_width;
00062 int c_block_height;
00063
00064 uint16_t *mb_type;
00065 uint32_t *dummy;
00066 int16_t (*motion_val8[3])[2];
00067 int16_t (*motion_val16[3])[2];
00068
00069 int64_t rd_total;
00070 } SVQ1Context;
00071
00072 static void svq1_write_header(SVQ1Context *s, int frame_type)
00073 {
00074 int i;
00075
00076
00077 put_bits(&s->pb, 22, 0x20);
00078
00079
00080 put_bits(&s->pb, 8, 0x00);
00081
00082
00083 put_bits(&s->pb, 2, frame_type - 1);
00084
00085 if (frame_type == I_TYPE) {
00086
00087
00088
00089
00090
00091
00092 put_bits(&s->pb, 5, 2);
00093
00094 for (i = 0; i < 7; i++)
00095 {
00096 if ((ff_svq1_frame_size_table[i].width == s->frame_width) &&
00097 (ff_svq1_frame_size_table[i].height == s->frame_height))
00098 {
00099 put_bits(&s->pb, 3, i);
00100 break;
00101 }
00102 }
00103
00104 if (i == 7)
00105 {
00106 put_bits(&s->pb, 3, 7);
00107 put_bits(&s->pb, 12, s->frame_width);
00108 put_bits(&s->pb, 12, s->frame_height);
00109 }
00110 }
00111
00112
00113 put_bits(&s->pb, 2, 0);
00114 }
00115
00116
00117 #define QUALITY_THRESHOLD 100
00118 #define THRESHOLD_MULTIPLIER 0.6
00119
00120 #if defined(HAVE_ALTIVEC)
00121 #undef vector
00122 #endif
00123
00124 static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref, uint8_t *decoded, int stride, int level, int threshold, int lambda, int intra){
00125 int count, y, x, i, j, split, best_mean, best_score, best_count;
00126 int best_vector[6];
00127 int block_sum[7]= {0, 0, 0, 0, 0, 0};
00128 int w= 2<<((level+2)>>1);
00129 int h= 2<<((level+1)>>1);
00130 int size=w*h;
00131 int16_t block[7][256];
00132 const int8_t *codebook_sum, *codebook;
00133 const uint16_t (*mean_vlc)[2];
00134 const uint8_t (*multistage_vlc)[2];
00135
00136 best_score=0;
00137
00138 if(intra){
00139 codebook_sum= svq1_intra_codebook_sum[level];
00140 codebook= ff_svq1_intra_codebooks[level];
00141 mean_vlc= ff_svq1_intra_mean_vlc;
00142 multistage_vlc= ff_svq1_intra_multistage_vlc[level];
00143 for(y=0; y<h; y++){
00144 for(x=0; x<w; x++){
00145 int v= src[x + y*stride];
00146 block[0][x + w*y]= v;
00147 best_score += v*v;
00148 block_sum[0] += v;
00149 }
00150 }
00151 }else{
00152 codebook_sum= svq1_inter_codebook_sum[level];
00153 codebook= ff_svq1_inter_codebooks[level];
00154 mean_vlc= ff_svq1_inter_mean_vlc + 256;
00155 multistage_vlc= ff_svq1_inter_multistage_vlc[level];
00156 for(y=0; y<h; y++){
00157 for(x=0; x<w; x++){
00158 int v= src[x + y*stride] - ref[x + y*stride];
00159 block[0][x + w*y]= v;
00160 best_score += v*v;
00161 block_sum[0] += v;
00162 }
00163 }
00164 }
00165
00166 best_count=0;
00167 best_score -= ((block_sum[0]*block_sum[0])>>(level+3));
00168 best_mean= (block_sum[0] + (size>>1)) >> (level+3);
00169
00170 if(level<4){
00171 for(count=1; count<7; count++){
00172 int best_vector_score= INT_MAX;
00173 int best_vector_sum=-999, best_vector_mean=-999;
00174 const int stage= count-1;
00175 const int8_t *vector;
00176
00177 for(i=0; i<16; i++){
00178 int sum= codebook_sum[stage*16 + i];
00179 int sqr, diff, score;
00180
00181 vector = codebook + stage*size*16 + i*size;
00182 sqr = s->dsp.ssd_int8_vs_int16(vector, block[stage], size);
00183 diff= block_sum[stage] - sum;
00184 score= sqr - ((diff*(int64_t)diff)>>(level+3));
00185 if(score < best_vector_score){
00186 int mean= (diff + (size>>1)) >> (level+3);
00187 assert(mean >-300 && mean<300);
00188 mean= av_clip(mean, intra?0:-256, 255);
00189 best_vector_score= score;
00190 best_vector[stage]= i;
00191 best_vector_sum= sum;
00192 best_vector_mean= mean;
00193 }
00194 }
00195 assert(best_vector_mean != -999);
00196 vector= codebook + stage*size*16 + best_vector[stage]*size;
00197 for(j=0; j<size; j++){
00198 block[stage+1][j] = block[stage][j] - vector[j];
00199 }
00200 block_sum[stage+1]= block_sum[stage] - best_vector_sum;
00201 best_vector_score +=
00202 lambda*(+ 1 + 4*count
00203 + multistage_vlc[1+count][1]
00204 + mean_vlc[best_vector_mean][1]);
00205
00206 if(best_vector_score < best_score){
00207 best_score= best_vector_score;
00208 best_count= count;
00209 best_mean= best_vector_mean;
00210 }
00211 }
00212 }
00213
00214 split=0;
00215 if(best_score > threshold && level){
00216 int score=0;
00217 int offset= (level&1) ? stride*h/2 : w/2;
00218 PutBitContext backup[6];
00219
00220 for(i=level-1; i>=0; i--){
00221 backup[i]= s->reorder_pb[i];
00222 }
00223 score += encode_block(s, src , ref , decoded , stride, level-1, threshold>>1, lambda, intra);
00224 score += encode_block(s, src + offset, ref + offset, decoded + offset, stride, level-1, threshold>>1, lambda, intra);
00225 score += lambda;
00226
00227 if(score < best_score){
00228 best_score= score;
00229 split=1;
00230 }else{
00231 for(i=level-1; i>=0; i--){
00232 s->reorder_pb[i]= backup[i];
00233 }
00234 }
00235 }
00236 if (level > 0)
00237 put_bits(&s->reorder_pb[level], 1, split);
00238
00239 if(!split){
00240 assert((best_mean >= 0 && best_mean<256) || !intra);
00241 assert(best_mean >= -256 && best_mean<256);
00242 assert(best_count >=0 && best_count<7);
00243 assert(level<4 || best_count==0);
00244
00245
00246 put_bits(&s->reorder_pb[level],
00247 multistage_vlc[1 + best_count][1],
00248 multistage_vlc[1 + best_count][0]);
00249 put_bits(&s->reorder_pb[level], mean_vlc[best_mean][1],
00250 mean_vlc[best_mean][0]);
00251
00252 for (i = 0; i < best_count; i++){
00253 assert(best_vector[i]>=0 && best_vector[i]<16);
00254 put_bits(&s->reorder_pb[level], 4, best_vector[i]);
00255 }
00256
00257 for(y=0; y<h; y++){
00258 for(x=0; x<w; x++){
00259 decoded[x + y*stride]= src[x + y*stride] - block[best_count][x + w*y] + best_mean;
00260 }
00261 }
00262 }
00263
00264 return best_score;
00265 }
00266
00267
00268 static int svq1_encode_plane(SVQ1Context *s, int plane, unsigned char *src_plane, unsigned char *ref_plane, unsigned char *decoded_plane,
00269 int width, int height, int src_stride, int stride)
00270 {
00271 int x, y;
00272 int i;
00273 int block_width, block_height;
00274 int level;
00275 int threshold[6];
00276 const int lambda= (s->picture.quality*s->picture.quality) >> (2*FF_LAMBDA_SHIFT);
00277
00278
00279 threshold[5] = QUALITY_THRESHOLD;
00280 for (level = 4; level >= 0; level--)
00281 threshold[level] = threshold[level + 1] * THRESHOLD_MULTIPLIER;
00282
00283 block_width = (width + 15) / 16;
00284 block_height = (height + 15) / 16;
00285
00286 if(s->picture.pict_type == P_TYPE){
00287 s->m.avctx= s->avctx;
00288 s->m.current_picture_ptr= &s->m.current_picture;
00289 s->m.last_picture_ptr = &s->m.last_picture;
00290 s->m.last_picture.data[0]= ref_plane;
00291 s->m.linesize=
00292 s->m.last_picture.linesize[0]=
00293 s->m.new_picture.linesize[0]=
00294 s->m.current_picture.linesize[0]= stride;
00295 s->m.width= width;
00296 s->m.height= height;
00297 s->m.mb_width= block_width;
00298 s->m.mb_height= block_height;
00299 s->m.mb_stride= s->m.mb_width+1;
00300 s->m.b8_stride= 2*s->m.mb_width+1;
00301 s->m.f_code=1;
00302 s->m.pict_type= s->picture.pict_type;
00303 s->m.me_method= s->avctx->me_method;
00304 s->m.me.scene_change_score=0;
00305 s->m.flags= s->avctx->flags;
00306
00307
00308
00309 s->m.lambda= s->picture.quality;
00310 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
00311 s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
00312
00313 if(!s->motion_val8[plane]){
00314 s->motion_val8 [plane]= av_mallocz((s->m.b8_stride*block_height*2 + 2)*2*sizeof(int16_t));
00315 s->motion_val16[plane]= av_mallocz((s->m.mb_stride*(block_height + 2) + 1)*2*sizeof(int16_t));
00316 }
00317
00318 s->m.mb_type= s->mb_type;
00319
00320
00321 s->m.current_picture.mb_mean= (uint8_t *)s->dummy;
00322 s->m.current_picture.mb_var= (uint16_t*)s->dummy;
00323 s->m.current_picture.mc_mb_var= (uint16_t*)s->dummy;
00324 s->m.current_picture.mb_type= s->dummy;
00325
00326 s->m.current_picture.motion_val[0]= s->motion_val8[plane] + 2;
00327 s->m.p_mv_table= s->motion_val16[plane] + s->m.mb_stride + 1;
00328 s->m.dsp= s->dsp;
00329 ff_init_me(&s->m);
00330
00331 s->m.me.dia_size= s->avctx->dia_size;
00332 s->m.first_slice_line=1;
00333 for (y = 0; y < block_height; y++) {
00334 uint8_t src[stride*16];
00335
00336 s->m.new_picture.data[0]= src - y*16*stride;
00337 s->m.mb_y= y;
00338
00339 for(i=0; i<16 && i + 16*y<height; i++){
00340 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
00341 for(x=width; x<16*block_width; x++)
00342 src[i*stride+x]= src[i*stride+x-1];
00343 }
00344 for(; i<16 && i + 16*y<16*block_height; i++)
00345 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
00346
00347 for (x = 0; x < block_width; x++) {
00348 s->m.mb_x= x;
00349 ff_init_block_index(&s->m);
00350 ff_update_block_index(&s->m);
00351
00352 ff_estimate_p_frame_motion(&s->m, x, y);
00353 }
00354 s->m.first_slice_line=0;
00355 }
00356
00357 ff_fix_long_p_mvs(&s->m);
00358 ff_fix_long_mvs(&s->m, NULL, 0, s->m.p_mv_table, s->m.f_code, CANDIDATE_MB_TYPE_INTER, 0);
00359 }
00360
00361 s->m.first_slice_line=1;
00362 for (y = 0; y < block_height; y++) {
00363 uint8_t src[stride*16];
00364
00365 for(i=0; i<16 && i + 16*y<height; i++){
00366 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
00367 for(x=width; x<16*block_width; x++)
00368 src[i*stride+x]= src[i*stride+x-1];
00369 }
00370 for(; i<16 && i + 16*y<16*block_height; i++)
00371 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
00372
00373 s->m.mb_y= y;
00374 for (x = 0; x < block_width; x++) {
00375 uint8_t reorder_buffer[3][6][7*32];
00376 int count[3][6];
00377 int offset = y * 16 * stride + x * 16;
00378 uint8_t *decoded= decoded_plane + offset;
00379 uint8_t *ref= ref_plane + offset;
00380 int score[4]={0,0,0,0}, best;
00381 uint8_t temp[16*stride];
00382
00383 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 3000){
00384 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00385 return -1;
00386 }
00387
00388 s->m.mb_x= x;
00389 ff_init_block_index(&s->m);
00390 ff_update_block_index(&s->m);
00391
00392 if(s->picture.pict_type == I_TYPE || (s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTRA)){
00393 for(i=0; i<6; i++){
00394 init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i], 7*32);
00395 }
00396 if(s->picture.pict_type == P_TYPE){
00397 const uint8_t *vlc= ff_svq1_block_type_vlc[SVQ1_BLOCK_INTRA];
00398 put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
00399 score[0]= vlc[1]*lambda;
00400 }
00401 score[0]+= encode_block(s, src+16*x, NULL, temp, stride, 5, 64, lambda, 1);
00402 for(i=0; i<6; i++){
00403 count[0][i]= put_bits_count(&s->reorder_pb[i]);
00404 flush_put_bits(&s->reorder_pb[i]);
00405 }
00406 }else
00407 score[0]= INT_MAX;
00408
00409 best=0;
00410
00411 if(s->picture.pict_type == P_TYPE){
00412 const uint8_t *vlc= ff_svq1_block_type_vlc[SVQ1_BLOCK_INTER];
00413 int mx, my, pred_x, pred_y, dxy;
00414 int16_t *motion_ptr;
00415
00416 motion_ptr= h263_pred_motion(&s->m, 0, 0, &pred_x, &pred_y);
00417 if(s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER){
00418 for(i=0; i<6; i++)
00419 init_put_bits(&s->reorder_pb[i], reorder_buffer[1][i], 7*32);
00420
00421 put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
00422
00423 s->m.pb= s->reorder_pb[5];
00424 mx= motion_ptr[0];
00425 my= motion_ptr[1];
00426 assert(mx>=-32 && mx<=31);
00427 assert(my>=-32 && my<=31);
00428 assert(pred_x>=-32 && pred_x<=31);
00429 assert(pred_y>=-32 && pred_y<=31);
00430 ff_h263_encode_motion(&s->m, mx - pred_x, 1);
00431 ff_h263_encode_motion(&s->m, my - pred_y, 1);
00432 s->reorder_pb[5]= s->m.pb;
00433 score[1] += lambda*put_bits_count(&s->reorder_pb[5]);
00434
00435 dxy= (mx&1) + 2*(my&1);
00436
00437 s->dsp.put_pixels_tab[0][dxy](temp+16, ref + (mx>>1) + stride*(my>>1), stride, 16);
00438
00439 score[1]+= encode_block(s, src+16*x, temp+16, decoded, stride, 5, 64, lambda, 0);
00440 best= score[1] <= score[0];
00441
00442 vlc= ff_svq1_block_type_vlc[SVQ1_BLOCK_SKIP];
00443 score[2]= s->dsp.sse[0](NULL, src+16*x, ref, stride, 16);
00444 score[2]+= vlc[1]*lambda;
00445 if(score[2] < score[best] && mx==0 && my==0){
00446 best=2;
00447 s->dsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
00448 for(i=0; i<6; i++){
00449 count[2][i]=0;
00450 }
00451 put_bits(&s->pb, vlc[1], vlc[0]);
00452 }
00453 }
00454
00455 if(best==1){
00456 for(i=0; i<6; i++){
00457 count[1][i]= put_bits_count(&s->reorder_pb[i]);
00458 flush_put_bits(&s->reorder_pb[i]);
00459 }
00460 }else{
00461 motion_ptr[0 ] = motion_ptr[1 ]=
00462 motion_ptr[2 ] = motion_ptr[3 ]=
00463 motion_ptr[0+2*s->m.b8_stride] = motion_ptr[1+2*s->m.b8_stride]=
00464 motion_ptr[2+2*s->m.b8_stride] = motion_ptr[3+2*s->m.b8_stride]=0;
00465 }
00466 }
00467
00468 s->rd_total += score[best];
00469
00470 for(i=5; i>=0; i--){
00471 ff_copy_bits(&s->pb, reorder_buffer[best][i], count[best][i]);
00472 }
00473 if(best==0){
00474 s->dsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
00475 }
00476 }
00477 s->m.first_slice_line=0;
00478 }
00479 return 0;
00480 }
00481
00482 static int svq1_encode_init(AVCodecContext *avctx)
00483 {
00484 SVQ1Context * const s = avctx->priv_data;
00485
00486 dsputil_init(&s->dsp, avctx);
00487 avctx->coded_frame= (AVFrame*)&s->picture;
00488
00489 s->frame_width = avctx->width;
00490 s->frame_height = avctx->height;
00491
00492 s->y_block_width = (s->frame_width + 15) / 16;
00493 s->y_block_height = (s->frame_height + 15) / 16;
00494
00495 s->c_block_width = (s->frame_width / 4 + 15) / 16;
00496 s->c_block_height = (s->frame_height / 4 + 15) / 16;
00497
00498 s->avctx= avctx;
00499 s->m.avctx= avctx;
00500 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
00501 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
00502 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
00503 s->mb_type = av_mallocz((s->y_block_width+1)*s->y_block_height*sizeof(int16_t));
00504 s->dummy = av_mallocz((s->y_block_width+1)*s->y_block_height*sizeof(int32_t));
00505 h263_encode_init(&s->m);
00506
00507 return 0;
00508 }
00509
00510 static int svq1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
00511 int buf_size, void *data)
00512 {
00513 SVQ1Context * const s = avctx->priv_data;
00514 AVFrame *pict = data;
00515 AVFrame * const p= (AVFrame*)&s->picture;
00516 AVFrame temp;
00517 int i;
00518
00519 if(avctx->pix_fmt != PIX_FMT_YUV410P){
00520 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
00521 return -1;
00522 }
00523
00524 if(!s->current_picture.data[0]){
00525 avctx->get_buffer(avctx, &s->current_picture);
00526 avctx->get_buffer(avctx, &s->last_picture);
00527 }
00528
00529 temp= s->current_picture;
00530 s->current_picture= s->last_picture;
00531 s->last_picture= temp;
00532
00533 init_put_bits(&s->pb, buf, buf_size);
00534
00535 *p = *pict;
00536 p->pict_type = avctx->gop_size && avctx->frame_number % avctx->gop_size ? P_TYPE : I_TYPE;
00537 p->key_frame = p->pict_type == I_TYPE;
00538
00539 svq1_write_header(s, p->pict_type);
00540 for(i=0; i<3; i++){
00541 if(svq1_encode_plane(s, i,
00542 s->picture.data[i], s->last_picture.data[i], s->current_picture.data[i],
00543 s->frame_width / (i?4:1), s->frame_height / (i?4:1),
00544 s->picture.linesize[i], s->current_picture.linesize[i]) < 0)
00545 return -1;
00546 }
00547
00548
00549 while(put_bits_count(&s->pb) & 31)
00550 put_bits(&s->pb, 1, 0);
00551
00552 flush_put_bits(&s->pb);
00553
00554 return (put_bits_count(&s->pb) / 8);
00555 }
00556
00557 static int svq1_encode_end(AVCodecContext *avctx)
00558 {
00559 SVQ1Context * const s = avctx->priv_data;
00560 int i;
00561
00562 av_log(avctx, AV_LOG_DEBUG, "RD: %f\n", s->rd_total/(double)(avctx->width*avctx->height*avctx->frame_number));
00563
00564 av_freep(&s->m.me.scratchpad);
00565 av_freep(&s->m.me.map);
00566 av_freep(&s->m.me.score_map);
00567 av_freep(&s->mb_type);
00568 av_freep(&s->dummy);
00569
00570 for(i=0; i<3; i++){
00571 av_freep(&s->motion_val8[i]);
00572 av_freep(&s->motion_val16[i]);
00573 }
00574
00575 return 0;
00576 }
00577
00578
00579 AVCodec svq1_encoder = {
00580 "svq1",
00581 CODEC_TYPE_VIDEO,
00582 CODEC_ID_SVQ1,
00583 sizeof(SVQ1Context),
00584 svq1_encode_init,
00585 svq1_encode_frame,
00586 svq1_encode_end,
00587 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV410P, -1},
00588 };