Mercurial > libavcodec.hg
comparison mpegvideo.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 | 997aac7d4e3e |
children | ae5c33165d5c |
comparison
equal
deleted
inserted
replaced
276:1e2f9ef286d4 | 277:5cb2978e701f |
---|---|
64 }; | 64 }; |
65 | 65 |
66 static UINT8 h263_chroma_roundtab[16] = { | 66 static UINT8 h263_chroma_roundtab[16] = { |
67 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | 67 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, |
68 }; | 68 }; |
69 | |
70 static UINT16 default_mv_penalty[MAX_FCODE][MAX_MV*2+1]; | |
71 static UINT8 default_fcode_tab[MAX_MV*2+1]; | |
69 | 72 |
70 /* default motion estimation */ | 73 /* default motion estimation */ |
71 int motion_estimation_method = ME_LOG; | 74 int motion_estimation_method = ME_LOG; |
72 | 75 |
73 extern UINT8 zigzag_end[64]; | 76 extern UINT8 zigzag_end[64]; |
354 break; | 357 break; |
355 default: | 358 default: |
356 return -1; | 359 return -1; |
357 } | 360 } |
358 | 361 |
362 { /* set up some save defaults, some codecs might override them later */ | |
363 static int done=0; | |
364 if(!done){ | |
365 int i; | |
366 done=1; | |
367 memset(default_mv_penalty, 0, sizeof(UINT16)*MAX_FCODE*(2*MAX_MV+1)); | |
368 memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1)); | |
369 | |
370 for(i=-16; i<16; i++){ | |
371 default_fcode_tab[i + MAX_MV]= 1; | |
372 } | |
373 } | |
374 } | |
375 s->mv_penalty= default_mv_penalty; | |
376 s->fcode_tab= default_fcode_tab; | |
377 | |
359 if (s->out_format == FMT_H263) | 378 if (s->out_format == FMT_H263) |
360 h263_encode_init_vlc(s); | 379 h263_encode_init(s); |
361 | 380 |
362 s->encoding = 1; | 381 s->encoding = 1; |
363 | 382 |
364 /* init */ | 383 /* init */ |
365 if (MPV_common_init(s) < 0) | 384 if (MPV_common_init(s) < 0) |
373 | 392 |
374 /* rate control init */ | 393 /* rate control init */ |
375 rate_control_init(s); | 394 rate_control_init(s); |
376 | 395 |
377 s->picture_number = 0; | 396 s->picture_number = 0; |
397 s->picture_in_gop_number = 0; | |
378 s->fake_picture_number = 0; | 398 s->fake_picture_number = 0; |
379 /* motion detector init */ | 399 /* motion detector init */ |
380 s->f_code = 1; | 400 s->f_code = 1; |
381 | 401 |
382 return 0; | 402 return 0; |
478 | 498 |
479 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | 499 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); |
480 | 500 |
481 if (!s->intra_only) { | 501 if (!s->intra_only) { |
482 /* first picture of GOP is intra */ | 502 /* first picture of GOP is intra */ |
483 if ((s->picture_number % s->gop_size) == 0) | 503 if (s->picture_in_gop_number >= s->gop_size){ |
504 s->picture_in_gop_number=0; | |
484 s->pict_type = I_TYPE; | 505 s->pict_type = I_TYPE; |
485 else | 506 }else |
486 s->pict_type = P_TYPE; | 507 s->pict_type = P_TYPE; |
487 } else { | 508 } else { |
488 s->pict_type = I_TYPE; | 509 s->pict_type = I_TYPE; |
489 } | 510 } |
490 | 511 |
519 encode_picture(s, s->picture_number); | 540 encode_picture(s, s->picture_number); |
520 avctx->key_frame = (s->pict_type == I_TYPE); | 541 avctx->key_frame = (s->pict_type == I_TYPE); |
521 | 542 |
522 MPV_frame_end(s); | 543 MPV_frame_end(s); |
523 s->picture_number++; | 544 s->picture_number++; |
545 s->picture_in_gop_number++; | |
524 | 546 |
525 if (s->out_format == FMT_MJPEG) | 547 if (s->out_format == FMT_MJPEG) |
526 mjpeg_picture_trailer(s); | 548 mjpeg_picture_trailer(s); |
527 | 549 |
528 flush_put_bits(&s->pb); | 550 flush_put_bits(&s->pb); |
1075 s->mb_type[xy] = s->mb_intra; | 1097 s->mb_type[xy] = s->mb_intra; |
1076 s->mv_table[0][xy] = motion_x; | 1098 s->mv_table[0][xy] = motion_x; |
1077 s->mv_table[1][xy] = motion_y; | 1099 s->mv_table[1][xy] = motion_y; |
1078 } | 1100 } |
1079 } | 1101 } |
1102 emms_c(); | |
1080 | 1103 |
1081 if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE){ //FIXME subtract MV bits | 1104 if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE){ //FIXME subtract MV bits |
1082 int i; | 1105 int i; |
1083 s->pict_type= I_TYPE; | 1106 s->pict_type= I_TYPE; |
1084 for(i=0; i<s->mb_height*s->mb_width; i++){ | 1107 s->picture_in_gop_number=0; |
1085 s->mb_type[i] = I_TYPE; | 1108 for(i=0; i<s->mb_num; i++){ |
1109 s->mb_type[i] = 1; | |
1086 s->mv_table[0][i] = 0; | 1110 s->mv_table[0][i] = 0; |
1087 s->mv_table[1][i] = 0; | 1111 s->mv_table[1][i] = 0; |
1088 } | 1112 } |
1089 } | 1113 } |
1090 | 1114 |
1115 /* find best f_code */ | |
1116 if(s->pict_type==P_TYPE){ | |
1117 int mv_num[8]; | |
1118 int i; | |
1119 int loose=0; | |
1120 UINT8 * fcode_tab= s->fcode_tab; | |
1121 | |
1122 for(i=0; i<8; i++) mv_num[i]=0; | |
1123 | |
1124 for(i=0; i<s->mb_num; i++){ | |
1125 if(s->mb_type[i] == 0){ | |
1126 mv_num[ fcode_tab[s->mv_table[0][i] + MAX_MV] ]++; | |
1127 mv_num[ fcode_tab[s->mv_table[1][i] + MAX_MV] ]++; | |
1128 //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i); | |
1129 } | |
1130 //else printf("I"); | |
1131 } | |
1132 | |
1133 for(i=MAX_FCODE; i>1; i--){ | |
1134 loose+= mv_num[i]; | |
1135 if(loose > 4) break; | |
1136 } | |
1137 s->f_code= i; | |
1138 }else{ | |
1139 s->f_code= 1; | |
1140 } | |
1141 //printf("f_code %d ///\n", s->f_code); | |
1142 /* convert MBs with too long MVs to I-Blocks */ | |
1143 if(s->pict_type==P_TYPE){ | |
1144 int i; | |
1145 const int f_code= s->f_code; | |
1146 UINT8 * fcode_tab= s->fcode_tab; | |
1147 | |
1148 for(i=0; i<s->mb_num; i++){ | |
1149 if(s->mb_type[i] == 0){ | |
1150 if( fcode_tab[s->mv_table[0][i] + MAX_MV] > f_code | |
1151 || fcode_tab[s->mv_table[0][i] + MAX_MV] == 0 | |
1152 || fcode_tab[s->mv_table[1][i] + MAX_MV] > f_code | |
1153 || fcode_tab[s->mv_table[1][i] + MAX_MV] == 0 ){ | |
1154 s->mb_type[i] = 1; | |
1155 s->mv_table[0][i] = 0; | |
1156 s->mv_table[1][i] = 0; | |
1157 } | |
1158 } | |
1159 } | |
1160 } | |
1161 | |
1091 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); | 1162 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); |
1092 | 1163 |
1093 if (!s->fixed_qscale) | 1164 if (!s->fixed_qscale) |
1094 s->qscale = rate_estimate_qscale(s); | 1165 s->qscale = rate_estimate_qscale(s); |
1095 | 1166 |