comparison h263.c @ 277:5cb2978e701f libavcodec

new motion estimation (epzs) not complete yet but allready pretty good :) unlimited mv search range minor bugfix in the mpeg4 header parser reset picture in gop counter if scene change is detected
author michaelni
date Fri, 22 Mar 2002 02:21:17 +0000
parents d0c186bcf075
children 6a5293bc8089
comparison
equal deleted inserted replaced
276:1e2f9ef286d4 277:5cb2978e701f
15 * GNU General Public License for more details. 15 * GNU General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * ac prediction encoding by Michael Niedermayer <michaelni@gmx.at>
20 */ 22 */
21 #include "common.h" 23 #include "common.h"
22 #include "dsputil.h" 24 #include "dsputil.h"
23 #include "avcodec.h" 25 #include "avcodec.h"
24 #include "mpegvideo.h" 26 #include "mpegvideo.h"
43 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, 45 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
44 int n, int coded); 46 int n, int coded);
45 static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr); 47 static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr);
46 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n, 48 static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
47 int dir); 49 int dir);
48
49 extern UINT32 inverse[256]; 50 extern UINT32 inverse[256];
51
52 static UINT16 mv_penalty[MAX_FCODE][MAX_MV*2+1];
53 static UINT8 fcode_tab[MAX_MV*2+1];
50 54
51 int h263_get_picture_format(int width, int height) 55 int h263_get_picture_format(int width, int height)
52 { 56 {
53 int format; 57 int format;
54 58
522 /* top copy */ 526 /* top copy */
523 for(i=1;i<8;i++) 527 for(i=1;i<8;i++)
524 ac_val1[8 + i] = block[block_permute_op(i)]; 528 ac_val1[8 + i] = block[block_permute_op(i)];
525 } 529 }
526 530
527
528 static inline int mid_pred(int a, int b, int c)
529 {
530 int vmin, vmax;
531 vmax = vmin = a;
532 if (b < vmin)
533 vmin = b;
534 else
535 vmax = b;
536
537 if (c < vmin)
538 vmin = c;
539 else if (c > vmax)
540 vmax = c;
541
542 return a + b + c - vmin - vmax;
543 }
544
545 INT16 *h263_pred_motion(MpegEncContext * s, int block, 531 INT16 *h263_pred_motion(MpegEncContext * s, int block,
546 int *px, int *py) 532 int *px, int *py)
547 { 533 {
548 int xy, wrap; 534 int xy, wrap;
549 INT16 *A, *B, *C, *mot_val; 535 INT16 *A, *B, *C, *mot_val;
646 put_bits(&s->pb, (2*n_bits)+1, code); 632 put_bits(&s->pb, (2*n_bits)+1, code);
647 //printf("\nVal = %d\tCode = %d", sval, code); 633 //printf("\nVal = %d\tCode = %d", sval, code);
648 } 634 }
649 } 635 }
650 636
651 void h263_encode_init_vlc(MpegEncContext *s) 637 static void init_mv_penalty_and_fcode(MpegEncContext *s)
638 {
639 int f_code;
640 int mv;
641 for(f_code=1; f_code<=MAX_FCODE; f_code++){
642 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
643 int len;
644
645 if(mv==0) len= mvtab[0][1];
646 else{
647 int val, bit_size, range, code;
648
649 bit_size = s->f_code - 1;
650 range = 1 << bit_size;
651
652 val=mv;
653 if (val < 0)
654 val = -val;
655 val--;
656 code = (val >> bit_size) + 1;
657 if(code<33){
658 len= mvtab[code][1] + 1 + bit_size;
659 }else{
660 len= mvtab[32][1] + 2 + bit_size;
661 }
662 }
663
664 mv_penalty[f_code][mv+MAX_MV]= len;
665 }
666 }
667
668
669 for(f_code=MAX_FCODE; f_code>0; f_code--){
670 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
671 fcode_tab[mv+MAX_MV]= f_code;
672 }
673 }
674 }
675
676 void h263_encode_init(MpegEncContext *s)
652 { 677 {
653 static int done = 0; 678 static int done = 0;
654 679
655 if (!done) { 680 if (!done) {
656 done = 1; 681 done = 1;
657 init_rl(&rl_inter); 682 init_rl(&rl_inter);
658 init_rl(&rl_intra); 683 init_rl(&rl_intra);
659 } 684
685 init_mv_penalty_and_fcode(s);
686 }
687 s->mv_penalty= mv_penalty;
688
689 // use fcodes >1 only for mpeg4 FIXME
690 if(!s->h263_msmpeg4 && s->h263_pred) s->fcode_tab= fcode_tab;
660 } 691 }
661 692
662 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) 693 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
663 { 694 {
664 int level, run, last, i, j, last_index, last_non_zero, sign, slevel; 695 int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
2092 state = ((state << 8) | v) & 0xffffff; 2123 state = ((state << 8) | v) & 0xffffff;
2093 startcode = state; 2124 startcode = state;
2094 break; 2125 break;
2095 } 2126 }
2096 state = ((state << 8) | v) & 0xffffff; 2127 state = ((state << 8) | v) & 0xffffff;
2097 /* XXX: really detect end of frame */ 2128 if( get_bits_count(&s->gb) > s->gb.size*8){
2098 if (state == 0){ 2129 printf("no VOP startcode found\n");
2099 printf("illegal zero code found\n");
2100 return -1; 2130 return -1;
2101 } 2131 }
2102 } 2132 }
2103 //printf("startcode %X %d\n", startcode, get_bits_count(&s->gb)); 2133 //printf("startcode %X %d\n", startcode, get_bits_count(&s->gb));
2104 if (startcode == 0x120) { // Video Object Layer 2134 if (startcode == 0x120) { // Video Object Layer
2150 height = get_bits(&s->gb, 13); 2180 height = get_bits(&s->gb, 13);
2151 skip_bits1(&s->gb); /* marker */ 2181 skip_bits1(&s->gb); /* marker */
2152 if(width && height){ /* they should be non zero but who knows ... */ 2182 if(width && height){ /* they should be non zero but who knows ... */
2153 s->width = width; 2183 s->width = width;
2154 s->height = height; 2184 s->height = height;
2185 // printf("%d %d\n", width, height);
2155 } 2186 }
2156 } 2187 }
2157 2188
2158 if(get_bits1(&s->gb)) printf("interlaced not supported\n"); /* interlaced */ 2189 if(get_bits1(&s->gb)) printf("interlaced not supported\n"); /* interlaced */
2159 if(!get_bits1(&s->gb)) printf("OBMC not supported\n"); /* OBMC Disable */ 2190 if(!get_bits1(&s->gb)) printf("OBMC not supported\n"); /* OBMC Disable */