Mercurial > libavcodec.hg
comparison motion_est.c @ 455:7f2d2be2aee9 libavcodec
dont double check vectors
more correct EPZS implementation
much faster halfpel ME
simplifications / reducing duplicate code
4MV in LQ mode
author | michaelni |
---|---|
date | Sun, 02 Jun 2002 12:18:18 +0000 |
parents | 718a22dc121f |
children | f74798aca30e |
comparison
equal
deleted
inserted
replaced
454:eda22d628b2d | 455:7f2d2be2aee9 |
---|---|
1 /* | 1 /* |
2 * Motion estimation | 2 * Motion estimation |
3 * Copyright (c) 2000,2001 Fabrice Bellard. | 3 * Copyright (c) 2000,2001 Fabrice Bellard. |
4 * Copyright (c) 2002 Michael Niedermayer | |
4 * | 5 * |
5 * | 6 * |
6 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Lesser General Public | 8 * modify it under the terms of the GNU Lesser General Public |
8 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
23 #include <stdio.h> | 24 #include <stdio.h> |
24 #include "avcodec.h" | 25 #include "avcodec.h" |
25 #include "dsputil.h" | 26 #include "dsputil.h" |
26 #include "mpegvideo.h" | 27 #include "mpegvideo.h" |
27 | 28 |
28 //#define ABS(a) ((a)>0 ? (a) : -(a)) | 29 #define SQ(a) ((a)*(a)) |
29 #define MAX(a,b) ((a) > (b) ? (a) : (b)) | |
30 #define INTER_BIAS 257 | 30 #define INTER_BIAS 257 |
31 | 31 |
32 static int halfpel_motion_search(MpegEncContext * s, | 32 #define P_LAST P[0] |
33 int *mx_ptr, int *my_ptr, int dmin, | 33 #define P_LEFT P[1] |
34 int xmin, int ymin, int xmax, int ymax, | 34 #define P_TOP P[2] |
35 int pred_x, int pred_y, uint8_t *ref_picture); | 35 #define P_TOPRIGHT P[3] |
36 #define P_MEDIAN P[4] | |
37 #define P_LAST_LEFT P[5] | |
38 #define P_LAST_RIGHT P[6] | |
39 #define P_LAST_TOP P[7] | |
40 #define P_LAST_BOTTOM P[8] | |
41 #define P_MV1 P[9] | |
42 | |
36 | 43 |
37 static int pix_sum(UINT8 * pix, int line_size) | 44 static int pix_sum(UINT8 * pix, int line_size) |
38 { | 45 { |
39 int s, i, j; | 46 int s, i, j; |
40 | 47 |
357 | 364 |
358 #define Z_THRESHOLD 256 | 365 #define Z_THRESHOLD 256 |
359 | 366 |
360 #define CHECK_MV(x,y)\ | 367 #define CHECK_MV(x,y)\ |
361 {\ | 368 {\ |
362 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ | 369 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\ |
363 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ | 370 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\ |
364 if(d<dmin){\ | 371 if(map[index]!=key){\ |
365 best[0]=x;\ | 372 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ |
366 best[1]=y;\ | 373 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ |
367 dmin=d;\ | 374 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\ |
375 map[index]= key;\ | |
376 score_map[index]= d;\ | |
368 }\ | 377 }\ |
369 } | 378 } |
370 | 379 |
371 #define CHECK_MV_DIR(x,y,new_dir)\ | 380 #define CHECK_MV_DIR(x,y,new_dir)\ |
372 {\ | 381 {\ |
373 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ | 382 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\ |
374 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ | 383 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\ |
375 if(d<dmin){\ | 384 if(map[index]!=key){\ |
376 best[0]=x;\ | 385 d = pix_abs(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ |
377 best[1]=y;\ | 386 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ |
378 dmin=d;\ | 387 if(d<dmin){\ |
379 next_dir= new_dir;\ | 388 best[0]=x;\ |
389 best[1]=y;\ | |
390 dmin=d;\ | |
391 next_dir= new_dir;\ | |
392 }\ | |
393 map[index]= key;\ | |
394 score_map[index]= d;\ | |
380 }\ | 395 }\ |
381 } | 396 } |
382 | 397 |
383 #define CHECK_MV4(x,y)\ | 398 #define CHECK_MV4(x,y)\ |
384 {\ | 399 {\ |
385 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ | 400 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\ |
386 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ | 401 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\ |
387 if(d<dmin){\ | 402 if(map[index]!=key){\ |
388 best[0]=x;\ | 403 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ |
389 best[1]=y;\ | 404 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ |
390 dmin=d;\ | 405 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\ |
406 map[index]= key;\ | |
407 score_map[index]= d;\ | |
391 }\ | 408 }\ |
392 } | 409 } |
393 | 410 |
394 #define CHECK_MV4_DIR(x,y,new_dir)\ | |
395 {\ | |
396 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\ | |
397 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\ | |
398 if(d<dmin){\ | |
399 best[0]=x;\ | |
400 best[1]=y;\ | |
401 dmin=d;\ | |
402 next_dir= new_dir;\ | |
403 }\ | |
404 } | |
405 | |
406 | |
407 #define check(x,y,S,v)\ | 411 #define check(x,y,S,v)\ |
408 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d xmin" #v, (x), (y), s->mb_x, s->mb_y);\ | 412 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\ |
409 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d xmax" #v, (x), (y), s->mb_x, s->mb_y);\ | 413 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\ |
410 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d ymin" #v, (x), (y), s->mb_x, s->mb_y);\ | 414 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\ |
411 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d ymax" #v, (x), (y), s->mb_x, s->mb_y);\ | 415 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\ |
412 | 416 |
413 | 417 |
414 static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin, | 418 static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin, |
415 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, | 419 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, |
416 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, | 420 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, |
417 int xmin, int ymin, int xmax, int ymax, int shift) | 421 int xmin, int ymin, int xmax, int ymax, int shift, |
422 uint32_t *map, uint16_t *score_map, int map_generation, | |
423 op_pixels_abs_func pix_abs) | |
418 { | 424 { |
419 int next_dir=-1; | 425 int next_dir=-1; |
420 | 426 |
421 for(;;){ | 427 for(;;){ |
422 int d; | 428 int d; |
460 if(dmin==last_min) return dmin; | 466 if(dmin==last_min) return dmin; |
461 } | 467 } |
462 */ | 468 */ |
463 } | 469 } |
464 | 470 |
465 static inline int small_diamond_search4MV(MpegEncContext * s, int *best, int dmin, | 471 #if 1 |
466 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, | 472 #define SNAKE_1 3 |
467 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, | 473 #define SNAKE_2 2 |
468 int xmin, int ymin, int xmax, int ymax, int shift) | 474 #else |
469 { | 475 #define SNAKE_1 7 |
470 int next_dir=-1; | 476 #define SNAKE_2 3 |
471 | 477 #endif |
472 for(;;){ | |
473 int d; | |
474 const int dir= next_dir; | |
475 const int x= best[0]; | |
476 const int y= best[1]; | |
477 next_dir=-1; | |
478 | |
479 //printf("%d", dir); | |
480 if(dir!=2 && x>xmin) CHECK_MV4_DIR(x-1, y , 0) | |
481 if(dir!=3 && y>ymin) CHECK_MV4_DIR(x , y-1, 1) | |
482 if(dir!=0 && x<xmax) CHECK_MV4_DIR(x+1, y , 2) | |
483 if(dir!=1 && y<ymax) CHECK_MV4_DIR(x , y+1, 3) | |
484 | |
485 if(next_dir==-1){ | |
486 return dmin; | |
487 } | |
488 } | |
489 } | |
490 | |
491 static inline int snake_search(MpegEncContext * s, int *best, int dmin, | 478 static inline int snake_search(MpegEncContext * s, int *best, int dmin, |
492 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, | 479 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, |
493 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, | 480 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, |
494 int xmin, int ymin, int xmax, int ymax, int shift) | 481 int xmin, int ymin, int xmax, int ymax, int shift, |
482 uint32_t *map, uint16_t *score_map,int map_generation, | |
483 op_pixels_abs_func pix_abs) | |
495 { | 484 { |
496 int dir=0; | 485 int dir=0; |
497 int c=1; | 486 int c=1; |
498 static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1}; | 487 static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1}; |
499 static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1}; | 488 static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1}; |
515 int y= best[1]; | 504 int y= best[1]; |
516 int d; | 505 int d; |
517 x+=x_dir[dir]; | 506 x+=x_dir[dir]; |
518 y+=y_dir[dir]; | 507 y+=y_dir[dir]; |
519 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){ | 508 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){ |
520 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride); | 509 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation; |
521 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant; | 510 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1); |
511 if(map[index]!=key){ | |
512 d = pix_abs(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride); | |
513 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant; | |
514 map[index]=key; | |
515 score_map[index]=d; | |
516 }else | |
517 d= dmin+1; | |
522 }else{ | 518 }else{ |
523 d = dmin + 10000; //FIXME smarter boundary handling | 519 d = dmin + 10000; //FIXME smarter boundary handling |
524 } | 520 } |
525 if(d<dmin){ | 521 if(d<dmin){ |
526 best[0]=x; | 522 best[0]=x; |
535 last_d[1]=last_d[0]; | 531 last_d[1]=last_d[0]; |
536 last_d[0]=d; | 532 last_d[0]=d; |
537 }else{ | 533 }else{ |
538 //bad++; | 534 //bad++; |
539 if(fails){ | 535 if(fails){ |
540 if(fails>=3) return dmin; | 536 if(fails>=SNAKE_1+1) return dmin; |
541 }else{ | 537 }else{ |
542 c= -c; | 538 if(dir&1) dir-= c*3; |
539 else c= -c; | |
540 // c= -c; | |
543 } | 541 } |
544 dir+=c*2; | 542 dir+=c*SNAKE_2; |
545 fails++; | 543 fails++; |
546 } | 544 } |
547 dir&=7; | 545 dir&=7; |
548 } | 546 } |
547 } | |
548 | |
549 static inline int cross_search(MpegEncContext * s, int *best, int dmin, | |
550 UINT8 *new_pic, UINT8 *old_pic, int pic_stride, | |
551 int pred_x, int pred_y, UINT16 *mv_penalty, int quant, | |
552 int xmin, int ymin, int xmax, int ymax, int shift, | |
553 uint32_t *map, uint16_t *score_map,int map_generation, | |
554 op_pixels_abs_func pix_abs) | |
555 { | |
556 static int x_dir[4]= {-1, 0, 1, 0}; | |
557 static int y_dir[4]= { 0,-1, 0, 1}; | |
558 int improvement[2]={100000, 100000}; | |
559 int dirs[2]={2, 3}; | |
560 int dir; | |
561 int last_dir= -1; | |
562 | |
563 for(;;){ | |
564 dir= dirs[ improvement[0] > improvement[1] ? 0 : 1 ]; | |
565 if(improvement[dir&1]==-1) return dmin; | |
566 | |
567 { | |
568 const int x= best[0] + x_dir[dir]; | |
569 const int y= best[1] + y_dir[dir]; | |
570 const int key= (y<<ME_MAP_MV_BITS) + x + map_generation; | |
571 const int index= ((y<<ME_MAP_SHIFT) + x)&(ME_MAP_SIZE-1); | |
572 int d; | |
573 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){ | |
574 if(map[index]!=key){ | |
575 d = pix_abs(new_pic, old_pic + x + y*pic_stride, pic_stride); | |
576 d += (mv_penalty[(x<<shift)-pred_x] + mv_penalty[(y<<shift)-pred_y])*quant; | |
577 map[index]=key; | |
578 score_map[index]=d; | |
579 if(d<dmin){ | |
580 improvement[dir&1]= dmin-d; | |
581 improvement[(dir&1)^1]++; | |
582 dmin=d; | |
583 best[0]= x; | |
584 best[1]= y; | |
585 last_dir=dir; | |
586 continue; | |
587 } | |
588 }else{ | |
589 d= score_map[index]; | |
590 } | |
591 }else{ | |
592 d= dmin + 1000; //FIXME is this a good idea? | |
593 } | |
594 /* evaluated point was cached or checked and worse */ | |
595 | |
596 if(last_dir==dir){ | |
597 improvement[dir&1]= -1; | |
598 }else{ | |
599 improvement[dir&1]= d-dmin; | |
600 last_dir= dirs[dir&1]= dir^2; | |
601 } | |
602 } | |
603 } | |
604 } | |
605 | |
606 static inline int update_map_generation(MpegEncContext * s) | |
607 { | |
608 s->me_map_generation+= 1<<(ME_MAP_MV_BITS*2); | |
609 if(s->me_map_generation==0){ | |
610 s->me_map_generation= 1<<(ME_MAP_MV_BITS*2); | |
611 memset(s->me_map, 0, sizeof(uint32_t)*ME_MAP_SIZE); | |
612 } | |
613 return s->me_map_generation; | |
549 } | 614 } |
550 | 615 |
551 static int epzs_motion_search(MpegEncContext * s, | 616 static int epzs_motion_search(MpegEncContext * s, |
552 int *mx_ptr, int *my_ptr, | 617 int *mx_ptr, int *my_ptr, |
553 int P[5][2], int pred_x, int pred_y, | 618 int P[10][2], int pred_x, int pred_y, |
554 int xmin, int ymin, int xmax, int ymax, uint8_t * ref_picture) | 619 int xmin, int ymin, int xmax, int ymax, uint8_t * ref_picture) |
555 { | 620 { |
556 int best[2]={0, 0}; | 621 int best[2]={0, 0}; |
557 int d, dmin; | 622 int d, dmin; |
558 UINT8 *new_pic, *old_pic; | 623 UINT8 *new_pic, *old_pic; |
559 const int pic_stride= s->linesize; | 624 const int pic_stride= s->linesize; |
560 const int pic_xy= (s->mb_y*pic_stride + s->mb_x)*16; | 625 const int pic_xy= (s->mb_y*pic_stride + s->mb_x)*16; |
561 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | 626 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame |
562 int quant= s->qscale; // qscale of the prev frame | 627 int quant= s->qscale; // qscale of the prev frame |
563 const int shift= 1+s->quarter_sample; | 628 const int shift= 1+s->quarter_sample; |
629 uint32_t *map= s->me_map; | |
630 uint16_t *score_map= s->me_score_map; | |
631 int map_generation; | |
564 | 632 |
565 new_pic = s->new_picture[0] + pic_xy; | 633 new_pic = s->new_picture[0] + pic_xy; |
566 old_pic = ref_picture + pic_xy; | 634 old_pic = ref_picture + pic_xy; |
567 | 635 |
636 map_generation= update_map_generation(s); | |
637 | |
568 dmin = pix_abs16x16(new_pic, old_pic, pic_stride); | 638 dmin = pix_abs16x16(new_pic, old_pic, pic_stride); |
569 if(dmin<Z_THRESHOLD){ | 639 map[0]= map_generation; |
570 *mx_ptr= 0; | 640 score_map[0]= dmin; |
571 *my_ptr= 0; | |
572 //printf("Z"); | |
573 return dmin; | |
574 } | |
575 | 641 |
576 /* first line */ | 642 /* first line */ |
577 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) { | 643 if ((s->mb_y == 0 || s->first_slice_line)) { |
578 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift) | 644 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
645 CHECK_MV(P_LAST[0]>>shift, P_LAST[1]>>shift) | |
579 }else{ | 646 }else{ |
580 CHECK_MV(P[4][0]>>shift, P[4][1]>>shift) | 647 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1] |
581 if(dmin<Z_THRESHOLD){ | 648 |P_TOP[0] |P_TOP[1] |
582 *mx_ptr= P[4][0]>>shift; | 649 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){ |
583 *my_ptr= P[4][1]>>shift; | 650 *mx_ptr= 0; |
584 //printf("M\n"); | 651 *my_ptr= 0; |
652 s->skip_me=1; | |
585 return dmin; | 653 return dmin; |
586 } | 654 } |
587 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift) | 655 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) |
588 CHECK_MV(P[2][0]>>shift, P[2][1]>>shift) | 656 if(dmin>256*2){ |
589 CHECK_MV(P[3][0]>>shift, P[3][1]>>shift) | 657 CHECK_MV(P_LAST[0] >>shift, P_LAST[1] >>shift) |
590 } | 658 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift) |
591 CHECK_MV(P[0][0]>>shift, P[0][1]>>shift) | 659 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift) |
592 | 660 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) |
661 } | |
662 } | |
663 if(dmin>256*4){ | |
664 CHECK_MV(P_LAST_RIGHT[0] >>shift, P_LAST_RIGHT[1] >>shift) | |
665 CHECK_MV(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift) | |
666 } | |
667 #if 0 //doest only slow things down | |
668 if(dmin>512*3){ | |
669 int step; | |
670 dmin= score_map[0]; | |
671 best[0]= best[1]=0; | |
672 for(step=128; step>0; step>>=1){ | |
673 const int step2= step; | |
674 int y; | |
675 for(y=-step2+best[1]; y<=step2+best[1]; y+=step){ | |
676 int x; | |
677 if(y<ymin || y>ymax) continue; | |
678 | |
679 for(x=-step2+best[0]; x<=step2+best[0]; x+=step){ | |
680 if(x<xmin || x>xmax) continue; | |
681 if(x==best[0] && y==best[1]) continue; | |
682 CHECK_MV(x,y) | |
683 } | |
684 } | |
685 } | |
686 } | |
687 #endif | |
593 //check(best[0],best[1],0, b0) | 688 //check(best[0],best[1],0, b0) |
594 if(s->me_method==ME_EPZS) | 689 if(s->me_method==ME_EPZS) |
595 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride, | 690 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride, |
596 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift); | 691 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, |
692 shift, map, score_map, map_generation, pix_abs16x16); | |
597 else | 693 else |
598 dmin= snake_search(s, best, dmin, new_pic, old_pic, pic_stride, | 694 dmin= cross_search(s, best, dmin, new_pic, old_pic, pic_stride, |
599 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift); | 695 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, |
696 shift, map, score_map, map_generation, pix_abs16x16); | |
600 //check(best[0],best[1],0, b1) | 697 //check(best[0],best[1],0, b1) |
601 *mx_ptr= best[0]; | 698 *mx_ptr= best[0]; |
602 *my_ptr= best[1]; | 699 *my_ptr= best[1]; |
603 | 700 |
604 // printf("%d %d %d \n", best[0], best[1], dmin); | 701 // printf("%d %d %d \n", best[0], best[1], dmin); |
605 return dmin; | 702 return dmin; |
606 } | 703 } |
607 | 704 |
608 static int epzs_motion_search4(MpegEncContext * s, int block, | 705 static int epzs_motion_search4(MpegEncContext * s, int block, |
609 int *mx_ptr, int *my_ptr, | 706 int *mx_ptr, int *my_ptr, |
610 int P[6][2], int pred_x, int pred_y, | 707 int P[10][2], int pred_x, int pred_y, |
611 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) | 708 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) |
612 { | 709 { |
613 int best[2]={0, 0}; | 710 int best[2]={0, 0}; |
614 int d, dmin; | 711 int d, dmin; |
615 UINT8 *new_pic, *old_pic; | 712 UINT8 *new_pic, *old_pic; |
616 const int pic_stride= s->linesize; | 713 const int pic_stride= s->linesize; |
617 const int pic_xy= ((s->mb_y*2 + (block>>1))*pic_stride + s->mb_x*2 + (block&1))*8; | 714 const int pic_xy= ((s->mb_y*2 + (block>>1))*pic_stride + s->mb_x*2 + (block&1))*8; |
618 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | 715 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame |
619 int quant= s->qscale; // qscale of the prev frame | 716 int quant= s->qscale; // qscale of the prev frame |
620 const int shift= 1+s->quarter_sample; | 717 const int shift= 1+s->quarter_sample; |
718 uint32_t *map= s->me_map; | |
719 uint16_t *score_map= s->me_score_map; | |
720 int map_generation; | |
621 | 721 |
622 new_pic = s->new_picture[0] + pic_xy; | 722 new_pic = s->new_picture[0] + pic_xy; |
623 old_pic = ref_picture + pic_xy; | 723 old_pic = ref_picture + pic_xy; |
624 | 724 |
625 dmin = pix_abs8x8(new_pic, old_pic, pic_stride); | 725 map_generation= update_map_generation(s); |
626 | 726 |
727 dmin = 1000000; | |
728 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); | |
627 /* first line */ | 729 /* first line */ |
628 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) { | 730 if ((s->mb_y == 0 || s->first_slice_line) && block<2) { |
629 CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift) | 731 CHECK_MV4(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
732 CHECK_MV4(P_LAST[0]>>shift, P_LAST[1]>>shift) | |
733 CHECK_MV4(P_MV1[0]>>shift, P_MV1[1]>>shift) | |
630 }else{ | 734 }else{ |
631 CHECK_MV4(P[4][0]>>shift, P[4][1]>>shift) | 735 CHECK_MV4(P_MV1[0]>>shift, P_MV1[1]>>shift) |
632 if(dmin<Z_THRESHOLD){ | 736 //FIXME try some early stop |
633 *mx_ptr= P[4][0]>>shift; | 737 if(dmin>64*2){ |
634 *my_ptr= P[4][1]>>shift; | 738 CHECK_MV4(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) |
635 //printf("M\n"); | 739 CHECK_MV4(P_LEFT[0]>>shift, P_LEFT[1]>>shift) |
636 return dmin; | 740 CHECK_MV4(P_TOP[0]>>shift, P_TOP[1]>>shift) |
637 } | 741 CHECK_MV4(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) |
638 CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift) | 742 CHECK_MV4(P_LAST[0]>>shift, P_LAST[1]>>shift) |
639 CHECK_MV4(P[2][0]>>shift, P[2][1]>>shift) | 743 } |
640 CHECK_MV4(P[3][0]>>shift, P[3][1]>>shift) | 744 } |
641 } | 745 if(dmin>64*4){ |
642 CHECK_MV4(P[0][0]>>shift, P[0][1]>>shift) | 746 CHECK_MV4(P_LAST_RIGHT[0]>>shift, P_LAST_RIGHT[1]>>shift) |
643 CHECK_MV4(P[5][0]>>shift, P[5][1]>>shift) | 747 CHECK_MV4(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift) |
644 | 748 } |
645 //check(best[0],best[1],0, b0) | 749 |
646 dmin= small_diamond_search4MV(s, best, dmin, new_pic, old_pic, pic_stride, | 750 if(s->me_method==ME_EPZS) |
647 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift); | 751 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride, |
648 //check(best[0],best[1],0, b1) | 752 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, |
753 shift, map, score_map, map_generation, pix_abs8x8); | |
754 else | |
755 dmin= cross_search(s, best, dmin, new_pic, old_pic, pic_stride, | |
756 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, | |
757 shift, map, score_map, map_generation, pix_abs8x8); | |
758 | |
649 *mx_ptr= best[0]; | 759 *mx_ptr= best[0]; |
650 *my_ptr= best[1]; | 760 *my_ptr= best[1]; |
651 | 761 |
652 // printf("%d %d %d \n", best[0], best[1], dmin); | 762 // printf("%d %d %d \n", best[0], best[1], dmin); |
653 return dmin; | 763 return dmin; |
654 } | 764 } |
655 | 765 |
656 #define CHECK_HALF_MV(suffix, x, y) \ | 766 #define CHECK_HALF_MV(suffix, x, y) \ |
657 d= pix_abs16x16_ ## suffix(pix, ptr+((x)>>1), s->linesize);\ | 767 {\ |
768 d= pix_abs_ ## suffix(pix, ptr+((x)>>1), s->linesize);\ | |
658 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\ | 769 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\ |
659 if(d<dminh){\ | 770 COPY3_IF_LT(dminh, d, dx, x, dy, y)\ |
660 dminh= d;\ | 771 } |
661 mx= mx1 + x;\ | 772 |
662 my= my1 + y;\ | |
663 } | |
664 | |
665 #define CHECK_HALF_MV4(suffix, x, y) \ | |
666 d= pix_abs8x8_ ## suffix(pix, ptr+((x)>>1), s->linesize);\ | |
667 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\ | |
668 if(d<dminh){\ | |
669 dminh= d;\ | |
670 mx= mx1 + x;\ | |
671 my= my1 + y;\ | |
672 } | |
673 | 773 |
674 /* The idea would be to make half pel ME after Inter/Intra decision to | 774 /* The idea would be to make half pel ME after Inter/Intra decision to |
675 save time. */ | 775 save time. */ |
676 static inline int halfpel_motion_search(MpegEncContext * s, | 776 static inline int halfpel_motion_search(MpegEncContext * s, |
677 int *mx_ptr, int *my_ptr, int dmin, | 777 int *mx_ptr, int *my_ptr, int dmin, |
678 int xmin, int ymin, int xmax, int ymax, | 778 int xmin, int ymin, int xmax, int ymax, |
679 int pred_x, int pred_y, uint8_t *ref_picture) | 779 int pred_x, int pred_y, uint8_t *ref_picture, |
780 op_pixels_abs_func pix_abs_x2, | |
781 op_pixels_abs_func pix_abs_y2, op_pixels_abs_func pix_abs_xy2, int n) | |
680 { | 782 { |
681 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | 783 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame |
682 const int quant= s->qscale; | 784 const int quant= s->qscale; |
683 int pen_x, pen_y; | 785 int mx, my, xx, yy, dminh; |
684 int mx, my, mx1, my1, d, xx, yy, dminh; | |
685 UINT8 *pix, *ptr; | 786 UINT8 *pix, *ptr; |
787 | |
788 if(s->skip_me){ | |
789 *mx_ptr = 0; | |
790 *my_ptr = 0; | |
791 return dmin; | |
792 }else | |
793 | |
794 xx = 16 * s->mb_x + 8*(n&1); | |
795 yy = 16 * s->mb_y + 8*(n>>1); | |
796 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
686 | 797 |
687 mx = *mx_ptr; | 798 mx = *mx_ptr; |
688 my = *my_ptr; | 799 my = *my_ptr; |
689 ptr = ref_picture + (my * s->linesize) + mx; | 800 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx); |
690 | |
691 xx = 16 * s->mb_x; | |
692 yy = 16 * s->mb_y; | |
693 pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
694 | 801 |
695 dminh = dmin; | 802 dminh = dmin; |
696 | 803 |
697 if (mx > xmin && mx < xmax && | 804 if (mx > xmin && mx < xmax && |
698 my > ymin && my < ymax) { | 805 my > ymin && my < ymax) { |
699 | 806 int dx=0, dy=0; |
700 mx= mx1= 2*(mx - xx); | 807 int d, pen_x, pen_y; |
701 my= my1= 2*(my - yy); | 808 |
702 if(dmin < Z_THRESHOLD && mx==0 && my==0){ | 809 mx<<=1; |
703 *mx_ptr = 0; | 810 my<<=1; |
704 *my_ptr = 0; | |
705 return dmin; | |
706 } | |
707 | 811 |
708 pen_x= pred_x + mx; | 812 pen_x= pred_x + mx; |
709 pen_y= pred_y + my; | 813 pen_y= pred_y + my; |
710 | 814 |
711 ptr-= s->linesize; | 815 ptr-= s->linesize; |
718 CHECK_HALF_MV(x2 , +1, 0) | 822 CHECK_HALF_MV(x2 , +1, 0) |
719 CHECK_HALF_MV(xy2, -1, +1) | 823 CHECK_HALF_MV(xy2, -1, +1) |
720 CHECK_HALF_MV(y2 , 0, +1) | 824 CHECK_HALF_MV(y2 , 0, +1) |
721 CHECK_HALF_MV(xy2, +1, +1) | 825 CHECK_HALF_MV(xy2, +1, +1) |
722 | 826 |
827 mx+=dx; | |
828 my+=dy; | |
723 }else{ | 829 }else{ |
724 mx= 2*(mx - xx); | 830 mx<<=1; |
725 my= 2*(my - yy); | 831 my<<=1; |
726 } | 832 } |
727 | 833 |
728 *mx_ptr = mx; | 834 *mx_ptr = mx; |
729 *my_ptr = my; | 835 *my_ptr = my; |
730 return dminh; | 836 return dminh; |
731 } | 837 } |
732 | 838 |
733 static inline void halfpel_motion_search4(MpegEncContext * s, | 839 static inline int fast_halfpel_motion_search(MpegEncContext * s, |
734 int *mx_ptr, int *my_ptr, int dmin, | 840 int *mx_ptr, int *my_ptr, int dmin, |
735 int xmin, int ymin, int xmax, int ymax, | 841 int xmin, int ymin, int xmax, int ymax, |
736 int pred_x, int pred_y, int block_x, int block_y, | 842 int pred_x, int pred_y, uint8_t *ref_picture, |
737 uint8_t *ref_picture) | 843 op_pixels_abs_func pix_abs_x2, |
844 op_pixels_abs_func pix_abs_y2, op_pixels_abs_func pix_abs_xy2, int n) | |
738 { | 845 { |
739 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | 846 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame |
847 uint16_t *score_map= s->me_score_map; | |
740 const int quant= s->qscale; | 848 const int quant= s->qscale; |
741 int pen_x, pen_y; | 849 int mx, my, xx, yy, dminh; |
742 int mx, my, mx1, my1, d, xx, yy, dminh; | |
743 UINT8 *pix, *ptr; | 850 UINT8 *pix, *ptr; |
744 | 851 |
745 xx = 8 * block_x; | 852 if(s->skip_me){ |
746 yy = 8 * block_y; | 853 // printf("S"); |
854 *mx_ptr = 0; | |
855 *my_ptr = 0; | |
856 return dmin; | |
857 } | |
858 // printf("N"); | |
859 | |
860 xx = 16 * s->mb_x + 8*(n&1); | |
861 yy = 16 * s->mb_y + 8*(n>>1); | |
747 pix = s->new_picture[0] + (yy * s->linesize) + xx; | 862 pix = s->new_picture[0] + (yy * s->linesize) + xx; |
748 | 863 |
749 mx = *mx_ptr; | 864 mx = *mx_ptr; |
750 my = *my_ptr; | 865 my = *my_ptr; |
751 ptr = ref_picture + ((yy+my) * s->linesize) + xx + mx; | 866 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx); |
752 | 867 |
753 dminh = dmin; | 868 dminh = dmin; |
754 | 869 |
755 if (mx > xmin && mx < xmax && | 870 if (mx > xmin && mx < xmax && |
756 my > ymin && my < ymax) { | 871 my > ymin && my < ymax) { |
757 | 872 int dx=0, dy=0; |
758 mx= mx1= 2*mx; | 873 int d, pen_x, pen_y; |
759 my= my1= 2*my; | 874 const int index= (my<<ME_MAP_SHIFT) + mx; |
760 if(dmin < Z_THRESHOLD && mx==0 && my==0){ | 875 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; |
761 *mx_ptr = 0; | 876 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]; |
762 *my_ptr = 0; | 877 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]; |
763 return; | 878 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; |
764 } | 879 mx<<=1; |
880 my<<=1; | |
881 | |
765 | 882 |
766 pen_x= pred_x + mx; | 883 pen_x= pred_x + mx; |
767 pen_y= pred_y + my; | 884 pen_y= pred_y + my; |
768 | 885 |
769 ptr-= s->linesize; | 886 ptr-= s->linesize; |
770 CHECK_HALF_MV4(xy2, -1, -1) | 887 if(t<=b){ |
771 CHECK_HALF_MV4(y2 , 0, -1) | 888 CHECK_HALF_MV(y2 , 0, -1) |
772 CHECK_HALF_MV4(xy2, +1, -1) | 889 if(l<=r){ |
773 | 890 CHECK_HALF_MV(xy2, -1, -1) |
774 ptr+= s->linesize; | 891 if(t+r<=b+l){ |
775 CHECK_HALF_MV4(x2 , -1, 0) | 892 CHECK_HALF_MV(xy2, +1, -1) |
776 CHECK_HALF_MV4(x2 , +1, 0) | 893 ptr+= s->linesize; |
777 CHECK_HALF_MV4(xy2, -1, +1) | 894 }else{ |
778 CHECK_HALF_MV4(y2 , 0, +1) | 895 ptr+= s->linesize; |
779 CHECK_HALF_MV4(xy2, +1, +1) | 896 CHECK_HALF_MV(xy2, -1, +1) |
897 } | |
898 CHECK_HALF_MV(x2 , -1, 0) | |
899 }else{ | |
900 CHECK_HALF_MV(xy2, +1, -1) | |
901 if(t+l<=b+r){ | |
902 CHECK_HALF_MV(xy2, -1, -1) | |
903 ptr+= s->linesize; | |
904 }else{ | |
905 ptr+= s->linesize; | |
906 CHECK_HALF_MV(xy2, +1, +1) | |
907 } | |
908 CHECK_HALF_MV(x2 , +1, 0) | |
909 } | |
910 }else{ | |
911 if(l<=r){ | |
912 if(t+l<=b+r){ | |
913 CHECK_HALF_MV(xy2, -1, -1) | |
914 ptr+= s->linesize; | |
915 }else{ | |
916 ptr+= s->linesize; | |
917 CHECK_HALF_MV(xy2, +1, +1) | |
918 } | |
919 CHECK_HALF_MV(x2 , -1, 0) | |
920 CHECK_HALF_MV(xy2, -1, +1) | |
921 }else{ | |
922 if(t+r<=b+l){ | |
923 CHECK_HALF_MV(xy2, +1, -1) | |
924 ptr+= s->linesize; | |
925 }else{ | |
926 ptr+= s->linesize; | |
927 CHECK_HALF_MV(xy2, -1, +1) | |
928 } | |
929 CHECK_HALF_MV(x2 , +1, 0) | |
930 CHECK_HALF_MV(xy2, +1, +1) | |
931 } | |
932 CHECK_HALF_MV(y2 , 0, +1) | |
933 } | |
934 mx+=dx; | |
935 my+=dy; | |
780 | 936 |
781 }else{ | 937 }else{ |
782 mx*=2; | 938 mx<<=1; |
783 my*=2; | 939 my<<=1; |
784 } | 940 } |
785 | 941 |
786 *mx_ptr = mx; | 942 *mx_ptr = mx; |
787 *my_ptr = my; | 943 *my_ptr = my; |
788 } | 944 return dminh; |
789 | 945 } |
790 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my) | 946 |
947 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4) | |
791 { | 948 { |
792 const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2); | 949 const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2); |
793 | 950 |
794 s->p_mv_table[xy][0] = mx; | 951 s->p_mv_table[xy][0] = mx; |
795 s->p_mv_table[xy][1] = my; | 952 s->p_mv_table[xy][1] = my; |
796 | 953 |
797 /* has allready been set to the 4 MV if 4MV is done */ | 954 /* has allready been set to the 4 MV if 4MV is done */ |
798 if(!(s->flags&CODEC_FLAG_4MV)){ | 955 if(mv4){ |
799 int mot_xy= s->block_index[0]; | 956 int mot_xy= s->block_index[0]; |
800 | 957 |
801 s->motion_val[mot_xy ][0]= mx; | 958 s->motion_val[mot_xy ][0]= mx; |
802 s->motion_val[mot_xy ][1]= my; | 959 s->motion_val[mot_xy ][1]= my; |
803 s->motion_val[mot_xy+1][0]= mx; | 960 s->motion_val[mot_xy+1][0]= mx; |
838 *xmax = s->mb_width*16 - 16; | 995 *xmax = s->mb_width*16 - 16; |
839 *ymax = s->mb_height*16 - 16; | 996 *ymax = s->mb_height*16 - 16; |
840 } | 997 } |
841 } | 998 } |
842 | 999 |
1000 static inline int mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift) | |
1001 { | |
1002 int block; | |
1003 int P[10][2]; | |
1004 uint8_t *ref_picture= s->last_picture[0]; | |
1005 int dmin_sum=0; | |
1006 | |
1007 for(block=0; block<4; block++){ | |
1008 int mx4, my4; | |
1009 int pred_x4, pred_y4; | |
1010 int dmin4; | |
1011 static const int off[4]= {2, 1, 1, -1}; | |
1012 const int mot_stride = s->block_wrap[0]; | |
1013 const int mot_xy = s->block_index[block]; | |
1014 // const int block_x= (block&1); | |
1015 // const int block_y= (block>>1); | |
1016 #if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way | |
1017 const int rel_xmin4= xmin; | |
1018 const int rel_xmax4= xmax; | |
1019 const int rel_ymin4= ymin; | |
1020 const int rel_ymax4= ymax; | |
1021 #else | |
1022 const int rel_xmin4= xmin - block_x*8; | |
1023 const int rel_xmax4= xmax - block_x*8 + 8; | |
1024 const int rel_ymin4= ymin - block_y*8; | |
1025 const int rel_ymax4= ymax - block_y*8 + 8; | |
1026 #endif | |
1027 P_LAST[0] = s->motion_val[mot_xy ][0]; | |
1028 P_LAST[1] = s->motion_val[mot_xy ][1]; | |
1029 P_LEFT[0] = s->motion_val[mot_xy - 1][0]; | |
1030 P_LEFT[1] = s->motion_val[mot_xy - 1][1]; | |
1031 P_LAST_RIGHT[0] = s->motion_val[mot_xy + 1][0]; | |
1032 P_LAST_RIGHT[1] = s->motion_val[mot_xy + 1][1]; | |
1033 P_LAST_BOTTOM[0]= s->motion_val[mot_xy + 1*mot_stride][0]; | |
1034 P_LAST_BOTTOM[1]= s->motion_val[mot_xy + 1*mot_stride][1]; | |
1035 | |
1036 if(P_LEFT[0] > (rel_xmax4<<shift)) P_LEFT[0] = (rel_xmax4<<shift); | |
1037 if(P_LAST_RIGHT[0] < (rel_xmin4<<shift)) P_LAST_RIGHT[0] = (rel_xmin4<<shift); | |
1038 if(P_LAST_BOTTOM[1]< (rel_ymin4<<shift)) P_LAST_BOTTOM[1]= (rel_ymin4<<shift); | |
1039 | |
1040 /* special case for first line */ | |
1041 if ((s->mb_y == 0 || s->first_slice_line) && block<2) { | |
1042 pred_x4= P_LEFT[0]; | |
1043 pred_y4= P_LEFT[1]; | |
1044 } else { | |
1045 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0]; | |
1046 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1]; | |
1047 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + off[block]][0]; | |
1048 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + off[block]][1]; | |
1049 if(P_TOP[1] > (rel_ymax4<<shift)) P_TOP[1] = (rel_ymax4<<shift); | |
1050 if(P_TOPRIGHT[0] < (rel_xmin4<<shift)) P_TOPRIGHT[0]= (rel_xmin4<<shift); | |
1051 if(P_TOPRIGHT[0] > (rel_xmax4<<shift)) P_TOPRIGHT[0]= (rel_xmax4<<shift); | |
1052 if(P_TOPRIGHT[1] > (rel_ymax4<<shift)) P_TOPRIGHT[1]= (rel_ymax4<<shift); | |
1053 | |
1054 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); | |
1055 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); | |
1056 | |
1057 if(s->out_format == FMT_H263){ | |
1058 pred_x4 = P_MEDIAN[0]; | |
1059 pred_y4 = P_MEDIAN[1]; | |
1060 }else { /* mpeg1 at least */ | |
1061 pred_x4= P_LEFT[0]; | |
1062 pred_y4= P_LEFT[1]; | |
1063 } | |
1064 } | |
1065 P_MV1[0]= mx; | |
1066 P_MV1[1]= my; | |
1067 | |
1068 dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture); | |
1069 | |
1070 dmin4= fast_halfpel_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, | |
1071 pred_x4, pred_y4, ref_picture, pix_abs8x8_x2, | |
1072 pix_abs8x8_y2, pix_abs8x8_xy2, block); | |
1073 | |
1074 s->motion_val[ s->block_index[block] ][0]= mx4; | |
1075 s->motion_val[ s->block_index[block] ][1]= my4; | |
1076 dmin_sum+= dmin4; | |
1077 } | |
1078 return dmin_sum; | |
1079 } | |
1080 | |
843 void ff_estimate_p_frame_motion(MpegEncContext * s, | 1081 void ff_estimate_p_frame_motion(MpegEncContext * s, |
844 int mb_x, int mb_y) | 1082 int mb_x, int mb_y) |
845 { | 1083 { |
846 UINT8 *pix, *ppix; | 1084 UINT8 *pix, *ppix; |
847 int sum, varc, vard, mx, my, range, dmin, xx, yy; | 1085 int sum, varc, vard, mx, my, range, dmin, xx, yy; |
848 int xmin, ymin, xmax, ymax; | 1086 int xmin, ymin, xmax, ymax; |
849 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; | 1087 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; |
850 int pred_x=0, pred_y=0; | 1088 int pred_x=0, pred_y=0; |
851 int P[6][2]; | 1089 int P[10][2]; |
852 const int shift= 1+s->quarter_sample; | 1090 const int shift= 1+s->quarter_sample; |
853 int mb_type=0; | 1091 int mb_type=0; |
854 uint8_t *ref_picture= s->last_picture[0]; | 1092 uint8_t *ref_picture= s->last_picture[0]; |
855 | 1093 |
856 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code); | 1094 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code); |
1095 rel_xmin= xmin - mb_x*16; | |
1096 rel_xmax= xmax - mb_x*16; | |
1097 rel_ymin= ymin - mb_y*16; | |
1098 rel_ymax= ymax - mb_y*16; | |
1099 s->skip_me=0; | |
857 | 1100 |
858 switch(s->me_method) { | 1101 switch(s->me_method) { |
859 case ME_ZERO: | 1102 case ME_ZERO: |
860 default: | 1103 default: |
861 no_motion_search(s, &mx, &my); | 1104 no_motion_search(s, &mx, &my); |
1105 mx-= mb_x*16; | |
1106 my-= mb_y*16; | |
862 dmin = 0; | 1107 dmin = 0; |
863 break; | 1108 break; |
864 case ME_FULL: | 1109 case ME_FULL: |
865 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); | 1110 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); |
1111 mx-= mb_x*16; | |
1112 my-= mb_y*16; | |
866 break; | 1113 break; |
867 case ME_LOG: | 1114 case ME_LOG: |
868 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | 1115 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
1116 mx-= mb_x*16; | |
1117 my-= mb_y*16; | |
869 break; | 1118 break; |
870 case ME_PHODS: | 1119 case ME_PHODS: |
871 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | 1120 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
1121 mx-= mb_x*16; | |
1122 my-= mb_y*16; | |
872 break; | 1123 break; |
873 case ME_X1: | 1124 case ME_X1: |
874 case ME_EPZS: | 1125 case ME_EPZS: |
875 { | 1126 { |
876 const int mot_stride = s->block_wrap[0]; | 1127 const int mot_stride = s->block_wrap[0]; |
877 const int mot_xy = s->block_index[0]; | 1128 const int mot_xy = s->block_index[0]; |
878 | 1129 |
879 rel_xmin= xmin - mb_x*16; | 1130 P_LAST[0] = s->motion_val[mot_xy ][0]; |
880 rel_xmax= xmax - mb_x*16; | 1131 P_LAST[1] = s->motion_val[mot_xy ][1]; |
881 rel_ymin= ymin - mb_y*16; | 1132 P_LEFT[0] = s->motion_val[mot_xy - 1][0]; |
882 rel_ymax= ymax - mb_y*16; | 1133 P_LEFT[1] = s->motion_val[mot_xy - 1][1]; |
883 | 1134 P_LAST_RIGHT[0] = s->motion_val[mot_xy + 2][0]; |
884 P[0][0] = s->motion_val[mot_xy ][0]; | 1135 P_LAST_RIGHT[1] = s->motion_val[mot_xy + 2][1]; |
885 P[0][1] = s->motion_val[mot_xy ][1]; | 1136 P_LAST_BOTTOM[0]= s->motion_val[mot_xy + 2*mot_stride][0]; |
886 P[1][0] = s->motion_val[mot_xy - 1][0]; | 1137 P_LAST_BOTTOM[1]= s->motion_val[mot_xy + 2*mot_stride][1]; |
887 P[1][1] = s->motion_val[mot_xy - 1][1]; | 1138 |
888 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift); | 1139 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift); |
1140 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift); | |
1141 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift); | |
889 | 1142 |
890 /* special case for first line */ | 1143 /* special case for first line */ |
891 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) { | 1144 if ((mb_y == 0 || s->first_slice_line)) { |
892 P[4][0] = P[1][0]; | 1145 pred_x= P_LEFT[0]; |
893 P[4][1] = P[1][1]; | 1146 pred_y= P_LEFT[1]; |
894 } else { | 1147 } else { |
895 P[2][0] = s->motion_val[mot_xy - mot_stride ][0]; | 1148 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0]; |
896 P[2][1] = s->motion_val[mot_xy - mot_stride ][1]; | 1149 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1]; |
897 P[3][0] = s->motion_val[mot_xy - mot_stride + 2 ][0]; | 1150 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + 2][0]; |
898 P[3][1] = s->motion_val[mot_xy - mot_stride + 2 ][1]; | 1151 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + 2][1]; |
899 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift); | 1152 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1] = (rel_ymax<<shift); |
900 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift); | 1153 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift); |
901 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift); | 1154 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift); |
902 | 1155 |
903 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]); | 1156 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); |
904 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]); | 1157 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); |
905 } | 1158 |
906 if(s->out_format == FMT_H263){ | 1159 if(s->out_format == FMT_H263){ |
907 pred_x = P[4][0]; | 1160 pred_x = P_MEDIAN[0]; |
908 pred_y = P[4][1]; | 1161 pred_y = P_MEDIAN[1]; |
909 }else { /* mpeg1 at least */ | 1162 }else { /* mpeg1 at least */ |
910 pred_x= P[1][0]; | 1163 pred_x= P_LEFT[0]; |
911 pred_y= P[1][1]; | 1164 pred_y= P_LEFT[1]; |
1165 } | |
912 } | 1166 } |
913 } | 1167 } |
914 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture); | 1168 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture); |
915 | 1169 |
916 mx+= mb_x*16; | |
917 my+= mb_y*16; | |
918 break; | 1170 break; |
919 } | |
920 | |
921 if(s->flags&CODEC_FLAG_4MV){ | |
922 int block; | |
923 | |
924 mb_type|= MB_TYPE_INTER4V; | |
925 | |
926 for(block=0; block<4; block++){ | |
927 int mx4, my4; | |
928 int pred_x4, pred_y4; | |
929 int dmin4; | |
930 static const int off[4]= {2, 1, 1, -1}; | |
931 const int mot_stride = s->block_wrap[0]; | |
932 const int mot_xy = s->block_index[block]; | |
933 const int block_x= mb_x*2 + (block&1); | |
934 const int block_y= mb_y*2 + (block>>1); | |
935 | |
936 const int rel_xmin4= xmin - block_x*8; | |
937 const int rel_xmax4= xmax - block_x*8 + 8; | |
938 const int rel_ymin4= ymin - block_y*8; | |
939 const int rel_ymax4= ymax - block_y*8 + 8; | |
940 | |
941 P[0][0] = s->motion_val[mot_xy ][0]; | |
942 P[0][1] = s->motion_val[mot_xy ][1]; | |
943 P[1][0] = s->motion_val[mot_xy - 1][0]; | |
944 P[1][1] = s->motion_val[mot_xy - 1][1]; | |
945 if(P[1][0] > (rel_xmax4<<shift)) P[1][0]= (rel_xmax4<<shift); | |
946 | |
947 /* special case for first line */ | |
948 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) { | |
949 P[4][0] = P[1][0]; | |
950 P[4][1] = P[1][1]; | |
951 } else { | |
952 P[2][0] = s->motion_val[mot_xy - mot_stride ][0]; | |
953 P[2][1] = s->motion_val[mot_xy - mot_stride ][1]; | |
954 P[3][0] = s->motion_val[mot_xy - mot_stride + off[block]][0]; | |
955 P[3][1] = s->motion_val[mot_xy - mot_stride + off[block]][1]; | |
956 if(P[2][1] > (rel_ymax4<<shift)) P[2][1]= (rel_ymax4<<shift); | |
957 if(P[3][0] < (rel_xmin4<<shift)) P[3][0]= (rel_xmin4<<shift); | |
958 if(P[3][0] > (rel_xmax4<<shift)) P[3][0]= (rel_xmax4<<shift); | |
959 if(P[3][1] > (rel_ymax4<<shift)) P[3][1]= (rel_ymax4<<shift); | |
960 | |
961 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]); | |
962 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]); | |
963 } | |
964 if(s->out_format == FMT_H263){ | |
965 pred_x4 = P[4][0]; | |
966 pred_y4 = P[4][1]; | |
967 }else { /* mpeg1 at least */ | |
968 pred_x4= P[1][0]; | |
969 pred_y4= P[1][1]; | |
970 } | |
971 P[5][0]= mx - mb_x*16; | |
972 P[5][1]= my - mb_y*16; | |
973 | |
974 dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture); | |
975 | |
976 halfpel_motion_search4(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, | |
977 pred_x4, pred_y4, block_x, block_y, ref_picture); | |
978 | |
979 s->motion_val[ s->block_index[block] ][0]= mx4; | |
980 s->motion_val[ s->block_index[block] ][1]= my4; | |
981 } | |
982 } | 1171 } |
983 | 1172 |
984 /* intra / predictive decision */ | 1173 /* intra / predictive decision */ |
985 xx = mb_x * 16; | 1174 xx = mb_x * 16; |
986 yy = mb_y * 16; | 1175 yy = mb_y * 16; |
987 | 1176 |
988 pix = s->new_picture[0] + (yy * s->linesize) + xx; | 1177 pix = s->new_picture[0] + (yy * s->linesize) + xx; |
989 /* At this point (mx,my) are full-pell and the absolute displacement */ | 1178 /* At this point (mx,my) are full-pell and the relative displacement */ |
990 ppix = ref_picture + (my * s->linesize) + mx; | 1179 ppix = ref_picture + ((yy+my) * s->linesize) + (xx+mx); |
991 | 1180 |
992 sum = pix_sum(pix, s->linesize); | 1181 sum = pix_sum(pix, s->linesize); |
993 #if 0 | 1182 |
994 varc = pix_dev(pix, s->linesize, (sum+128)>>8) + INTER_BIAS; | |
995 vard = pix_abs16x16(pix, ppix, s->linesize); | |
996 #else | |
997 sum= (sum+8)>>4; | 1183 sum= (sum+8)>>4; |
998 varc = ((pix_norm1(pix, s->linesize) - sum*sum + 128 + 500)>>8); | 1184 varc = (pix_norm1(pix, s->linesize) - sum*sum + 500 + 128)>>8; |
999 vard = (pix_norm(pix, ppix, s->linesize)+128)>>8; | 1185 vard = (pix_norm(pix, ppix, s->linesize)+128)>>8; |
1000 #endif | 1186 //printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout); |
1001 | 1187 s->mb_var [s->mb_width * mb_y + mb_x] = varc; |
1002 s->mb_var[s->mb_width * mb_y + mb_x] = varc; | 1188 s->mc_mb_var[s->mb_width * mb_y + mb_x] = vard; |
1003 s->avg_mb_var+= varc; | 1189 s->mb_var_sum += varc; |
1004 s->mc_mb_var += vard; | 1190 s->mc_mb_var_sum += vard; |
1005 | 1191 //printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout); |
1006 | 1192 |
1007 #if 0 | 1193 #if 0 |
1008 printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n", | 1194 printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n", |
1009 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy); | 1195 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy); |
1010 #endif | 1196 #endif |
1011 if(s->flags&CODEC_FLAG_HQ){ | 1197 if(s->flags&CODEC_FLAG_HQ){ |
1012 if (vard*2 + 200 > varc) | 1198 if (vard*2 + 200 > varc) |
1013 mb_type|= MB_TYPE_INTRA; | 1199 mb_type|= MB_TYPE_INTRA; |
1014 if (varc*2 + 200 > vard){ | 1200 if (varc*2 + 200 > vard){ |
1015 mb_type|= MB_TYPE_INTER; | 1201 mb_type|= MB_TYPE_INTER; |
1016 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture); | 1202 if(s->me_method >= ME_EPZS) |
1203 fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, | |
1204 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2, | |
1205 pix_abs16x16_xy2, 0); | |
1206 else | |
1207 halfpel_motion_search( s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, | |
1208 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2, | |
1209 pix_abs16x16_xy2, 0); | |
1017 }else{ | 1210 }else{ |
1018 mx = mx*2 - mb_x*32; | 1211 mx <<=1; |
1019 my = my*2 - mb_y*32; | 1212 my <<=1; |
1020 } | 1213 } |
1214 if((s->flags&CODEC_FLAG_4MV) | |
1215 && !s->skip_me && varc>50 && vard>10){ | |
1216 mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift); | |
1217 mb_type|=MB_TYPE_INTER4V; | |
1218 | |
1219 set_p_mv_tables(s, mx, my, 0); | |
1220 }else | |
1221 set_p_mv_tables(s, mx, my, 1); | |
1021 }else{ | 1222 }else{ |
1022 if (vard <= 64 || vard < varc) { | 1223 if (vard <= 64 || vard < varc) { |
1023 mb_type|= MB_TYPE_INTER; | 1224 mb_type|= MB_TYPE_INTER; |
1024 if (s->me_method != ME_ZERO) { | 1225 if (s->me_method != ME_ZERO) { |
1025 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture); | 1226 if(s->me_method >= ME_EPZS) |
1227 dmin= fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, | |
1228 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2, | |
1229 pix_abs16x16_xy2, 0); | |
1230 else | |
1231 dmin= halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, | |
1232 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2, | |
1233 pix_abs16x16_xy2, 0); | |
1234 if((s->flags&CODEC_FLAG_4MV) | |
1235 && !s->skip_me && varc>50 && vard>10){ | |
1236 int dmin4= mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift); | |
1237 if(dmin4 + 128 <dmin) | |
1238 mb_type= MB_TYPE_INTER4V; | |
1239 } | |
1240 set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V); | |
1241 | |
1026 } else { | 1242 } else { |
1027 mx -= 16 * mb_x; | 1243 mx <<=1; |
1028 my -= 16 * mb_y; | 1244 my <<=1; |
1029 } | 1245 } |
1030 #if 0 | 1246 #if 0 |
1031 if (vard < 10) { | 1247 if (vard < 10) { |
1032 skip++; | 1248 skip++; |
1033 fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d", | 1249 fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d", |
1034 skip, vard, varc, dmin); | 1250 skip, vard, varc, dmin); |
1035 } | 1251 } |
1036 #endif | 1252 #endif |
1037 }else{ | 1253 }else{ |
1038 mb_type|= MB_TYPE_INTRA; | 1254 mb_type|= MB_TYPE_INTRA; |
1039 mx = 0;//mx*2 - 32 * mb_x; | 1255 mx = 0; |
1040 my = 0;//my*2 - 32 * mb_y; | 1256 my = 0; |
1041 } | 1257 } |
1042 } | 1258 } |
1043 | 1259 |
1044 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; | 1260 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; |
1045 set_p_mv_tables(s, mx, my); | |
1046 } | 1261 } |
1047 | 1262 |
1048 int ff_estimate_motion_b(MpegEncContext * s, | 1263 int ff_estimate_motion_b(MpegEncContext * s, |
1049 int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *ref_picture, int f_code) | 1264 int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *ref_picture, int f_code) |
1050 { | 1265 { |
1051 int mx, my, range, dmin; | 1266 int mx, my, range, dmin; |
1052 int xmin, ymin, xmax, ymax; | 1267 int xmin, ymin, xmax, ymax; |
1053 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; | 1268 int rel_xmin, rel_ymin, rel_xmax, rel_ymax; |
1054 int pred_x=0, pred_y=0; | 1269 int pred_x=0, pred_y=0; |
1055 int P[6][2]; | 1270 int P[10][2]; |
1056 const int shift= 1+s->quarter_sample; | 1271 const int shift= 1+s->quarter_sample; |
1057 const int mot_stride = s->mb_width + 2; | 1272 const int mot_stride = s->mb_width + 2; |
1058 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; | 1273 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; |
1059 | 1274 |
1060 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code); | 1275 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code); |
1276 rel_xmin= xmin - mb_x*16; | |
1277 rel_xmax= xmax - mb_x*16; | |
1278 rel_ymin= ymin - mb_y*16; | |
1279 rel_ymax= ymax - mb_y*16; | |
1061 | 1280 |
1062 switch(s->me_method) { | 1281 switch(s->me_method) { |
1063 case ME_ZERO: | 1282 case ME_ZERO: |
1064 default: | 1283 default: |
1065 no_motion_search(s, &mx, &my); | 1284 no_motion_search(s, &mx, &my); |
1066 dmin = 0; | 1285 dmin = 0; |
1286 mx-= mb_x*16; | |
1287 my-= mb_y*16; | |
1067 break; | 1288 break; |
1068 case ME_FULL: | 1289 case ME_FULL: |
1069 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); | 1290 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); |
1291 mx-= mb_x*16; | |
1292 my-= mb_y*16; | |
1070 break; | 1293 break; |
1071 case ME_LOG: | 1294 case ME_LOG: |
1072 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | 1295 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
1296 mx-= mb_x*16; | |
1297 my-= mb_y*16; | |
1073 break; | 1298 break; |
1074 case ME_PHODS: | 1299 case ME_PHODS: |
1075 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); | 1300 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); |
1301 mx-= mb_x*16; | |
1302 my-= mb_y*16; | |
1076 break; | 1303 break; |
1077 case ME_X1: | 1304 case ME_X1: |
1078 case ME_EPZS: | 1305 case ME_EPZS: |
1079 { | 1306 { |
1080 | 1307 |
1081 rel_xmin= xmin - mb_x*16; | 1308 P_LAST[0] = mv_table[mot_xy ][0]; |
1082 rel_xmax= xmax - mb_x*16; | 1309 P_LAST[1] = mv_table[mot_xy ][1]; |
1083 rel_ymin= ymin - mb_y*16; | 1310 P_LEFT[0] = mv_table[mot_xy - 1][0]; |
1084 rel_ymax= ymax - mb_y*16; | 1311 P_LEFT[1] = mv_table[mot_xy - 1][1]; |
1085 | 1312 P_LAST_RIGHT[0] = mv_table[mot_xy + 1][0]; |
1086 P[0][0] = mv_table[mot_xy ][0]; | 1313 P_LAST_RIGHT[1] = mv_table[mot_xy + 1][1]; |
1087 P[0][1] = mv_table[mot_xy ][1]; | 1314 P_LAST_BOTTOM[0] = mv_table[mot_xy + mot_stride][0]; |
1088 P[1][0] = mv_table[mot_xy - 1][0]; | 1315 P_LAST_BOTTOM[1] = mv_table[mot_xy + mot_stride][1]; |
1089 P[1][1] = mv_table[mot_xy - 1][1]; | 1316 |
1090 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift); | 1317 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift); |
1318 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift); | |
1319 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift); | |
1091 | 1320 |
1092 /* special case for first line */ | 1321 /* special case for first line */ |
1093 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) { | 1322 if ((mb_y == 0 || s->first_slice_line)) { |
1094 P[4][0] = P[1][0]; | |
1095 P[4][1] = P[1][1]; | |
1096 } else { | 1323 } else { |
1097 P[2][0] = mv_table[mot_xy - mot_stride ][0]; | 1324 P_TOP[0] = mv_table[mot_xy - mot_stride ][0]; |
1098 P[2][1] = mv_table[mot_xy - mot_stride ][1]; | 1325 P_TOP[1] = mv_table[mot_xy - mot_stride ][1]; |
1099 P[3][0] = mv_table[mot_xy - mot_stride + 1 ][0]; | 1326 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0]; |
1100 P[3][1] = mv_table[mot_xy - mot_stride + 1 ][1]; | 1327 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1]; |
1101 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift); | 1328 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1]= (rel_ymax<<shift); |
1102 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift); | 1329 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift); |
1103 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift); | 1330 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift); |
1104 | 1331 |
1105 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]); | 1332 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); |
1106 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]); | 1333 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); |
1107 } | 1334 } |
1108 pred_x= P[1][0]; | 1335 pred_x= P_LEFT[0]; |
1109 pred_y= P[1][1]; | 1336 pred_y= P_LEFT[1]; |
1110 } | 1337 } |
1111 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture); | 1338 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture); |
1112 | 1339 |
1113 mx+= mb_x*16; | |
1114 my+= mb_y*16; | |
1115 break; | 1340 break; |
1116 } | 1341 } |
1117 | 1342 |
1118 /* intra / predictive decision */ | 1343 dmin= fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, |
1119 // xx = mb_x * 16; | 1344 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2, |
1120 // yy = mb_y * 16; | 1345 pix_abs16x16_xy2, 0); |
1121 | 1346 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my); |
1122 // pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
1123 /* At this point (mx,my) are full-pell and the absolute displacement */ | |
1124 // ppix = ref_picture + (my * s->linesize) + mx; | |
1125 | |
1126 dmin= halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture); | |
1127 | |
1128 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; | 1347 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; |
1129 mv_table[mot_xy][0]= mx; | 1348 mv_table[mot_xy][0]= mx; |
1130 mv_table[mot_xy][1]= my; | 1349 mv_table[mot_xy][1]= my; |
1131 return dmin; | 1350 return dmin; |
1132 } | 1351 } |
1138 int motion_bx, int motion_by, | 1357 int motion_bx, int motion_by, |
1139 int pred_fx, int pred_fy, | 1358 int pred_fx, int pred_fy, |
1140 int pred_bx, int pred_by) | 1359 int pred_bx, int pred_by) |
1141 { | 1360 { |
1142 //FIXME optimize? | 1361 //FIXME optimize? |
1362 //FIXME direct mode penalty | |
1143 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame | 1363 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame |
1144 uint8_t *dest_y = s->me_scratchpad; | 1364 uint8_t *dest_y = s->me_scratchpad; |
1145 uint8_t *ptr; | 1365 uint8_t *ptr; |
1146 int dxy; | 1366 int dxy; |
1147 int src_x, src_y; | 1367 int src_x, src_y; |
1199 } | 1419 } |
1200 | 1420 |
1201 static inline int direct_search(MpegEncContext * s, | 1421 static inline int direct_search(MpegEncContext * s, |
1202 int mb_x, int mb_y) | 1422 int mb_x, int mb_y) |
1203 { | 1423 { |
1204 int P[6][2]; | 1424 int P[10][2]; |
1205 const int mot_stride = s->mb_width + 2; | 1425 const int mot_stride = s->mb_width + 2; |
1206 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; | 1426 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1; |
1207 int dmin, dmin2; | 1427 int dmin, dmin2; |
1208 int motion_fx, motion_fy, motion_bx, motion_by, motion_bx0, motion_by0; | 1428 int motion_fx, motion_fy, motion_bx, motion_by, motion_bx0, motion_by0; |
1209 int motion_dx, motion_dy; | 1429 int motion_dx, motion_dy; |
1264 avg_pixels_tab[dxy](dest_y , ptr , s->linesize, 16); | 1484 avg_pixels_tab[dxy](dest_y , ptr , s->linesize, 16); |
1265 avg_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16); | 1485 avg_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16); |
1266 } | 1486 } |
1267 } | 1487 } |
1268 | 1488 |
1269 P[0][0] = mv_table[mot_xy ][0]; | 1489 P_LAST[0] = mv_table[mot_xy ][0]; |
1270 P[0][1] = mv_table[mot_xy ][1]; | 1490 P_LAST[1] = mv_table[mot_xy ][1]; |
1271 P[1][0] = mv_table[mot_xy - 1][0]; | 1491 P_LEFT[0] = mv_table[mot_xy - 1][0]; |
1272 P[1][1] = mv_table[mot_xy - 1][1]; | 1492 P_LEFT[1] = mv_table[mot_xy - 1][1]; |
1273 | 1493 P_LAST_RIGHT[0] = mv_table[mot_xy + 1][0]; |
1494 P_LAST_RIGHT[1] = mv_table[mot_xy + 1][1]; | |
1495 P_LAST_BOTTOM[0] = mv_table[mot_xy + mot_stride][0]; | |
1496 P_LAST_BOTTOM[1] = mv_table[mot_xy + mot_stride][1]; | |
1497 /* | |
1498 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift); | |
1499 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift); | |
1500 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift); | |
1501 */ | |
1274 /* special case for first line */ | 1502 /* special case for first line */ |
1275 if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) { | 1503 if ((mb_y == 0 || s->first_slice_line)) { |
1276 P[4][0] = P[1][0]; | |
1277 P[4][1] = P[1][1]; | |
1278 } else { | 1504 } else { |
1279 P[2][0] = mv_table[mot_xy - mot_stride ][0]; | 1505 P_TOP[0] = mv_table[mot_xy - mot_stride ][0]; |
1280 P[2][1] = mv_table[mot_xy - mot_stride ][1]; | 1506 P_TOP[1] = mv_table[mot_xy - mot_stride ][1]; |
1281 P[3][0] = mv_table[mot_xy - mot_stride + 1 ][0]; | 1507 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0]; |
1282 P[3][1] = mv_table[mot_xy - mot_stride + 1 ][1]; | 1508 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1]; |
1283 | 1509 |
1284 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]); | 1510 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); |
1285 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]); | 1511 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); |
1286 } | 1512 } |
1287 dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, -16, -16, 15, 15, ref_picture); | 1513 dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, -16, -16, 15, 15, ref_picture); |
1288 if(mx==0 && my==0) dmin=99999999; // not representable, due to rounding stuff | 1514 if(mx==0 && my==0) dmin=99999999; // not representable, due to rounding stuff |
1289 if(dmin2<dmin){ | 1515 if(dmin2<dmin){ |
1290 dmin= dmin2; | 1516 dmin= dmin2; |
1330 } | 1556 } |
1331 | 1557 |
1332 void ff_estimate_b_frame_motion(MpegEncContext * s, | 1558 void ff_estimate_b_frame_motion(MpegEncContext * s, |
1333 int mb_x, int mb_y) | 1559 int mb_x, int mb_y) |
1334 { | 1560 { |
1335 const int mot_stride = s->mb_width + 2; | |
1336 const int xy = (mb_y + 1)*mot_stride + mb_x + 1; | |
1337 const int quant= s->qscale; | 1561 const int quant= s->qscale; |
1338 int fmin, bmin, dmin, fbmin; | 1562 int fmin, bmin, dmin, fbmin; |
1339 int type=0; | 1563 int type=0; |
1340 int motion_fx, motion_fy, motion_bx, motion_by; | |
1341 | 1564 |
1342 dmin= direct_search(s, mb_x, mb_y); | 1565 dmin= direct_search(s, mb_x, mb_y); |
1343 | 1566 |
1344 fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, s->last_picture[0], s->f_code); | 1567 fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, s->last_picture[0], s->f_code); |
1345 bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, s->next_picture[0], s->b_code) - quant; | 1568 bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, s->next_picture[0], s->b_code) - quant; |
1363 } | 1586 } |
1364 if(fbmin<score){ | 1587 if(fbmin<score){ |
1365 score=fbmin; | 1588 score=fbmin; |
1366 type= MB_TYPE_BIDIR; | 1589 type= MB_TYPE_BIDIR; |
1367 } | 1590 } |
1368 s->mc_mb_var += score; | 1591 s->mc_mb_var_sum += score; |
1369 } | 1592 s->mc_mb_var[mb_y*s->mb_width + mb_x] = score; |
1593 } | |
1594 /* | |
1595 { | |
1596 static int count=0; | |
1597 static int sum=0; | |
1598 if(type==MB_TYPE_DIRECT){ | |
1599 int diff= ABS(s->b_forw_mv_table) | |
1600 } | |
1601 }*/ | |
1370 | 1602 |
1371 s->mb_type[mb_y*s->mb_width + mb_x]= type; | 1603 s->mb_type[mb_y*s->mb_width + mb_x]= type; |
1604 /* if(mb_y==0 && mb_x==0) printf("\n"); | |
1605 if(mb_x==0) printf("\n"); | |
1606 printf("%d", av_log2(type)); | |
1607 */ | |
1372 } | 1608 } |
1373 | 1609 |
1374 /* find best f_code for ME which do unlimited searches */ | 1610 /* find best f_code for ME which do unlimited searches */ |
1375 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) | 1611 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) |
1376 { | 1612 { |
1377 int f_code; | |
1378 | |
1379 if(s->me_method>=ME_EPZS){ | 1613 if(s->me_method>=ME_EPZS){ |
1380 int mv_num[8]; | 1614 int score[8]; |
1381 int i, y; | 1615 int i, y; |
1382 int loose=0; | |
1383 UINT8 * fcode_tab= s->fcode_tab; | 1616 UINT8 * fcode_tab= s->fcode_tab; |
1384 | 1617 int best_fcode=-1; |
1385 for(i=0; i<8; i++) mv_num[i]=0; | 1618 int best_score=-10000000; |
1619 | |
1620 for(i=0; i<8; i++) score[i]= s->mb_num*(8-i); //FIXME *2 and all other too so its the same but nicer | |
1386 | 1621 |
1387 for(y=0; y<s->mb_height; y++){ | 1622 for(y=0; y<s->mb_height; y++){ |
1388 int x; | 1623 int x; |
1389 int xy= (y+1)* (s->mb_width+2) + 1; | 1624 int xy= (y+1)* (s->mb_width+2) + 1; |
1390 i= y*s->mb_width; | 1625 i= y*s->mb_width; |
1391 for(x=0; x<s->mb_width; x++){ | 1626 for(x=0; x<s->mb_width; x++){ |
1392 if(s->mb_type[i] & type){ | 1627 if(s->mb_type[i] & type){ |
1393 mv_num[ fcode_tab[mv_table[xy][0] + MAX_MV] ]++; | 1628 int fcode= MAX(fcode_tab[mv_table[xy][0] + MAX_MV], |
1394 mv_num[ fcode_tab[mv_table[xy][1] + MAX_MV] ]++; | 1629 fcode_tab[mv_table[xy][1] + MAX_MV]); |
1395 //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i); | 1630 int j; |
1631 | |
1632 for(j=0; j<fcode && j<8; j++){ | |
1633 if(s->pict_type==B_TYPE || s->mc_mb_var[i] < s->mb_var[i]) | |
1634 score[j]-= 170; | |
1635 } | |
1396 } | 1636 } |
1397 i++; | 1637 i++; |
1398 xy++; | 1638 xy++; |
1399 } | 1639 } |
1400 } | 1640 } |
1401 | 1641 |
1402 for(i=MAX_FCODE; i>1; i--){ | 1642 for(i=1; i<8; i++){ |
1403 int threshold; | 1643 if(score[i] > best_score){ |
1404 loose+= mv_num[i]; | 1644 best_score= score[i]; |
1405 | 1645 best_fcode= i; |
1406 if(s->pict_type==B_TYPE) threshold= 0; | 1646 } |
1407 else threshold= s->mb_num/20; //FIXME | 1647 // printf("%d %d\n", i, score[i]); |
1408 if(loose > threshold) break; | 1648 } |
1409 } | 1649 |
1410 // printf("fcode: %d type: %d\n", i, s->pict_type); | 1650 // printf("fcode: %d type: %d\n", i, s->pict_type); |
1411 return i; | 1651 return best_fcode; |
1412 /* for(i=0; i<=MAX_FCODE; i++){ | 1652 /* for(i=0; i<=MAX_FCODE; i++){ |
1413 printf("%d ", mv_num[i]); | 1653 printf("%d ", mv_num[i]); |
1414 } | 1654 } |
1415 printf("\n");*/ | 1655 printf("\n");*/ |
1416 }else{ | 1656 }else{ |
1421 void ff_fix_long_p_mvs(MpegEncContext * s) | 1661 void ff_fix_long_p_mvs(MpegEncContext * s) |
1422 { | 1662 { |
1423 const int f_code= s->f_code; | 1663 const int f_code= s->f_code; |
1424 int y; | 1664 int y; |
1425 UINT8 * fcode_tab= s->fcode_tab; | 1665 UINT8 * fcode_tab= s->fcode_tab; |
1426 | 1666 //int clip=0; |
1667 //int noclip=0; | |
1427 /* clip / convert to intra 16x16 type MVs */ | 1668 /* clip / convert to intra 16x16 type MVs */ |
1428 for(y=0; y<s->mb_height; y++){ | 1669 for(y=0; y<s->mb_height; y++){ |
1429 int x; | 1670 int x; |
1430 int xy= (y+1)* (s->mb_width+2)+1; | 1671 int xy= (y+1)* (s->mb_width+2)+1; |
1431 int i= y*s->mb_width; | 1672 int i= y*s->mb_width; |
1437 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){ | 1678 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){ |
1438 s->mb_type[i] &= ~MB_TYPE_INTER; | 1679 s->mb_type[i] &= ~MB_TYPE_INTER; |
1439 s->mb_type[i] |= MB_TYPE_INTRA; | 1680 s->mb_type[i] |= MB_TYPE_INTRA; |
1440 s->p_mv_table[xy][0] = 0; | 1681 s->p_mv_table[xy][0] = 0; |
1441 s->p_mv_table[xy][1] = 0; | 1682 s->p_mv_table[xy][1] = 0; |
1683 //clip++; | |
1442 } | 1684 } |
1685 //else | |
1686 // noclip++; | |
1443 } | 1687 } |
1444 xy++; | 1688 xy++; |
1445 i++; | 1689 i++; |
1446 } | 1690 } |
1447 } | 1691 } |
1448 | 1692 //printf("%d no:%d %d//\n", clip, noclip, f_code); |
1449 if(s->flags&CODEC_FLAG_4MV){ | 1693 if(s->flags&CODEC_FLAG_4MV){ |
1450 const int wrap= 2+ s->mb_width*2; | 1694 const int wrap= 2+ s->mb_width*2; |
1451 | 1695 |
1452 /* clip / convert to intra 8x8 type MVs */ | 1696 /* clip / convert to intra 8x8 type MVs */ |
1453 for(y=0; y<s->mb_height; y++){ | 1697 for(y=0; y<s->mb_height; y++){ |