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