comparison mpegvideo.c @ 327:d359db02fc90 libavcodec

much better ME for b frames (a bit slow though) fixed MC rounding for b frames fixed hq mode with b-frames
author michaelni
date Fri, 19 Apr 2002 03:25:20 +0000
parents 6ebb8680885d
children 5cc47d0ba53e
comparison
equal deleted inserted replaced
326:6ebb8680885d 327:d359db02fc90
223 if (s->b_direct_mv_table == NULL) { 223 if (s->b_direct_mv_table == NULL) {
224 perror("malloc"); 224 perror("malloc");
225 goto fail; 225 goto fail;
226 } 226 }
227 227
228 s->me_scratchpad = av_mallocz( s->linesize*16*3*sizeof(uint8_t));
229 if (s->me_scratchpad == NULL) {
230 perror("malloc");
231 goto fail;
232 }
233
228 if(s->max_b_frames){ 234 if(s->max_b_frames){
229 for(j=0; j<REORDER_BUFFER_SIZE; j++){ 235 for(j=0; j<REORDER_BUFFER_SIZE; j++){
230 int i; 236 int i;
231 for(i=0;i<3;i++) { 237 for(i=0;i<3;i++) {
232 int w, h, shift; 238 int w, h, shift;
295 /* init macroblock skip table */ 301 /* init macroblock skip table */
296 s->mbskip_table = av_mallocz(s->mb_num); 302 s->mbskip_table = av_mallocz(s->mb_num);
297 if (!s->mbskip_table) 303 if (!s->mbskip_table)
298 goto fail; 304 goto fail;
299 305
300 s->block= s->intra_block; 306 s->block= s->blocks[0];
301 307
302 s->context_initialized = 1; 308 s->context_initialized = 1;
303 return 0; 309 return 0;
304 fail: 310 fail:
305 MPV_common_end(s); 311 MPV_common_end(s);
331 CHECK_FREE(s->motion_val); 337 CHECK_FREE(s->motion_val);
332 CHECK_FREE(s->dc_val[0]); 338 CHECK_FREE(s->dc_val[0]);
333 CHECK_FREE(s->ac_val[0]); 339 CHECK_FREE(s->ac_val[0]);
334 CHECK_FREE(s->coded_block); 340 CHECK_FREE(s->coded_block);
335 CHECK_FREE(s->mbintra_table); 341 CHECK_FREE(s->mbintra_table);
342 CHECK_FREE(s->me_scratchpad);
336 343
337 CHECK_FREE(s->mbskip_table); 344 CHECK_FREE(s->mbskip_table);
338 for(i=0;i<3;i++) { 345 for(i=0;i<3;i++) {
339 int j; 346 int j;
340 CHECK_FREE(s->last_picture_base[i]); 347 CHECK_FREE(s->last_picture_base[i]);
759 // printf("%f\n", avctx->psnr_y); 766 // printf("%f\n", avctx->psnr_y);
760 } 767 }
761 return pbBufPtr(&s->pb) - s->pb.buf; 768 return pbBufPtr(&s->pb) - s->pb.buf;
762 } 769 }
763 770
764 static inline int clip(int a, int amin, int amax)
765 {
766 if (a < amin)
767 return amin;
768 else if (a > amax)
769 return amax;
770 else
771 return a;
772 }
773
774 static inline void gmc1_motion(MpegEncContext *s, 771 static inline void gmc1_motion(MpegEncContext *s,
775 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, 772 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
776 int dest_offset, 773 int dest_offset,
777 UINT8 **ref_picture, int src_offset, 774 UINT8 **ref_picture, int src_offset,
778 int h) 775 int h)
1223 } 1220 }
1224 1221
1225 if (!s->mb_intra) { 1222 if (!s->mb_intra) {
1226 /* motion handling */ 1223 /* motion handling */
1227 if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){ 1224 if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){
1228 if (!s->no_rounding){ 1225 if ((!s->no_rounding) || s->pict_type==B_TYPE){
1229 op_pix = put_pixels_tab; 1226 op_pix = put_pixels_tab;
1230 op_qpix= qpel_mc_rnd_tab; 1227 op_qpix= qpel_mc_rnd_tab;
1231 }else{ 1228 }else{
1232 op_pix = put_no_rnd_pixels_tab; 1229 op_pix = put_no_rnd_pixels_tab;
1233 op_qpix= qpel_mc_no_rnd_tab; 1230 op_qpix= qpel_mc_no_rnd_tab;
1234 } 1231 }
1235 1232
1236 if (s->mv_dir & MV_DIR_FORWARD) { 1233 if (s->mv_dir & MV_DIR_FORWARD) {
1237 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); 1234 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1238 if (!s->no_rounding) 1235 if ((!s->no_rounding) || s->pict_type==B_TYPE)
1239 op_pix = avg_pixels_tab; 1236 op_pix = avg_pixels_tab;
1240 else 1237 else
1241 op_pix = avg_no_rnd_pixels_tab; 1238 op_pix = avg_no_rnd_pixels_tab;
1242 } 1239 }
1243 if (s->mv_dir & MV_DIR_BACKWARD) { 1240 if (s->mv_dir & MV_DIR_BACKWARD) {
1310 1307
1311 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16; 1308 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
1312 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; 1309 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1313 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; 1310 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1314 1311
1315 if (!s->no_rounding){ 1312 if ((!s->no_rounding) || s->pict_type==B_TYPE){
1316 op_pix = put_pixels_tab; 1313 op_pix = put_pixels_tab;
1317 op_qpix= qpel_mc_rnd_tab; 1314 op_qpix= qpel_mc_rnd_tab;
1318 }else{ 1315 }else{
1319 op_pix = put_no_rnd_pixels_tab; 1316 op_pix = put_no_rnd_pixels_tab;
1320 op_qpix= qpel_mc_no_rnd_tab; 1317 op_qpix= qpel_mc_no_rnd_tab;
1321 } 1318 }
1322 1319
1323 if (s->mv_dir & MV_DIR_FORWARD) { 1320 if (s->mv_dir & MV_DIR_FORWARD) {
1324 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); 1321 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1325 if (!s->no_rounding) 1322 if ((!s->no_rounding) || s->pict_type==B_TYPE)
1326 op_pix = avg_pixels_tab; 1323 op_pix = avg_pixels_tab;
1327 else 1324 else
1328 op_pix = avg_no_rnd_pixels_tab; 1325 op_pix = avg_no_rnd_pixels_tab;
1329 } 1326 }
1330 if (s->mv_dir & MV_DIR_BACKWARD) { 1327 if (s->mv_dir & MV_DIR_BACKWARD) {
1427 d->i_count= s->i_count; 1424 d->i_count= s->i_count;
1428 d->p_count= s->p_count; 1425 d->p_count= s->p_count;
1429 d->skip_count= s->skip_count; 1426 d->skip_count= s->skip_count;
1430 d->misc_bits= s->misc_bits; 1427 d->misc_bits= s->misc_bits;
1431 d->last_bits= s->last_bits; 1428 d->last_bits= s->last_bits;
1429
1430 d->mb_skiped= s->mb_skiped;
1432 } 1431 }
1433 1432
1434 static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ 1433 static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1435 int i; 1434 int i;
1436 1435
1451 d->skip_count= s->skip_count; 1450 d->skip_count= s->skip_count;
1452 d->misc_bits= s->misc_bits; 1451 d->misc_bits= s->misc_bits;
1453 d->last_bits= s->last_bits; 1452 d->last_bits= s->last_bits;
1454 1453
1455 d->mb_intra= s->mb_intra; 1454 d->mb_intra= s->mb_intra;
1455 d->mb_skiped= s->mb_skiped;
1456 d->mv_type= s->mv_type; 1456 d->mv_type= s->mv_type;
1457 d->mv_dir= s->mv_dir; 1457 d->mv_dir= s->mv_dir;
1458 d->pb= s->pb; 1458 d->pb= s->pb;
1459 d->block= s->block; 1459 d->block= s->block;
1460 for(i=0; i<6; i++) 1460 for(i=0; i<6; i++)
1466 { 1466 {
1467 int mb_x, mb_y, last_gob, pdif = 0; 1467 int mb_x, mb_y, last_gob, pdif = 0;
1468 int i; 1468 int i;
1469 int bits; 1469 int bits;
1470 MpegEncContext best_s, backup_s; 1470 MpegEncContext best_s, backup_s;
1471 UINT8 bit_buf[4][3000]; //FIXME check that this is ALLWAYS large enogh for a MB 1471 UINT8 bit_buf[7][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
1472 1472
1473 s->picture_number = picture_number; 1473 s->picture_number = picture_number;
1474 1474
1475 s->block_wrap[0]= 1475 s->block_wrap[0]=
1476 s->block_wrap[1]= 1476 s->block_wrap[1]=
1481 1481
1482 s->last_mc_mb_var = s->mc_mb_var; 1482 s->last_mc_mb_var = s->mc_mb_var;
1483 /* Reset the average MB variance */ 1483 /* Reset the average MB variance */
1484 s->avg_mb_var = 0; 1484 s->avg_mb_var = 0;
1485 s->mc_mb_var = 0; 1485 s->mc_mb_var = 0;
1486 1486
1487 /* we need to initialize some time vars before we can encode b-frames */
1488 if (s->h263_pred && !s->h263_msmpeg4)
1489 ff_set_mpeg4_time(s, s->picture_number);
1490
1487 /* Estimate motion for every MB */ 1491 /* Estimate motion for every MB */
1488 if(s->pict_type != I_TYPE){ 1492 if(s->pict_type != I_TYPE){
1489 // int16_t (*tmp)[2]= s->p_mv_table; 1493 // int16_t (*tmp)[2]= s->p_mv_table;
1490 // s->p_mv_table= s->last_mv_table; 1494 // s->p_mv_table= s->last_mv_table;
1491 // s->last_mv_table= s->mv_table; 1495 // s->last_mv_table= s->mv_table;
1533 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER); 1537 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
1534 ff_fix_long_p_mvs(s); 1538 ff_fix_long_p_mvs(s);
1535 if(s->pict_type==B_TYPE){ 1539 if(s->pict_type==B_TYPE){
1536 s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD); 1540 s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
1537 s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD); 1541 s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
1538 //FIXME if BIDIR != for&back 1542
1539 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD |MB_TYPE_BIDIR); 1543 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
1540 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD|MB_TYPE_BIDIR); 1544 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
1545 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
1546 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
1541 } 1547 }
1542 1548
1543 //printf("f_code %d ///\n", s->f_code); 1549 //printf("f_code %d ///\n", s->f_code);
1544 1550
1545 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); 1551 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
1630 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; 1636 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1631 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); 1637 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1632 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); 1638 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
1633 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2); 1639 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
1634 for(mb_x=0; mb_x < s->mb_width; mb_x++) { 1640 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1635 /*const */int mb_type= s->mb_type[mb_y * s->mb_width + mb_x]; 1641 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
1642 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1636 PutBitContext pb; 1643 PutBitContext pb;
1637 int d; 1644 int d;
1638 int dmin=10000000; 1645 int dmin=10000000;
1639 int best=0; 1646 int best=0;
1640 1647
1645 s->block_index[2]+=2; 1652 s->block_index[2]+=2;
1646 s->block_index[3]+=2; 1653 s->block_index[3]+=2;
1647 s->block_index[4]++; 1654 s->block_index[4]++;
1648 s->block_index[5]++; 1655 s->block_index[5]++;
1649 if(mb_type & (mb_type-1)){ // more than 1 MB type possible 1656 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
1657 int next_block=0;
1650 pb= s->pb; 1658 pb= s->pb;
1651 s->mv_dir = MV_DIR_FORWARD;
1652 1659
1653 copy_context_before_encode(&backup_s, s, -1); 1660 copy_context_before_encode(&backup_s, s, -1);
1654 1661
1655 if(mb_type&MB_TYPE_INTER){ 1662 if(mb_type&MB_TYPE_INTER){
1656 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; 1663 s->mv_dir = MV_DIR_FORWARD;
1657 s->mv_type = MV_TYPE_16X16; 1664 s->mv_type = MV_TYPE_16X16;
1658 s->mb_intra= 0; 1665 s->mb_intra= 0;
1659 s->mv[0][0][0] = s->p_mv_table[xy][0]; 1666 s->mv[0][0][0] = s->p_mv_table[xy][0];
1660 s->mv[0][0][1] = s->p_mv_table[xy][1]; 1667 s->mv[0][0][1] = s->p_mv_table[xy][1];
1661 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL); 1668 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
1662 s->block= s->inter_block; 1669 s->block= s->blocks[next_block];
1663 1670
1664 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]); 1671 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1665 d= get_bit_count(&s->pb); 1672 d= get_bit_count(&s->pb);
1666 if(d<dmin){ 1673 if(d<dmin){
1667 flush_put_bits(&s->pb); 1674 flush_put_bits(&s->pb);
1668 dmin=d; 1675 dmin=d;
1669 copy_context_after_encode(&best_s, s, MB_TYPE_INTER); 1676 copy_context_after_encode(&best_s, s, MB_TYPE_INTER);
1670 best=1; 1677 best=1;
1678 next_block^=1;
1671 } 1679 }
1672 } 1680 }
1673 if(mb_type&MB_TYPE_INTER4V){ 1681 if(mb_type&MB_TYPE_INTER4V){
1674 copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V); 1682 copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);
1683 s->mv_dir = MV_DIR_FORWARD;
1675 s->mv_type = MV_TYPE_8X8; 1684 s->mv_type = MV_TYPE_8X8;
1676 s->mb_intra= 0; 1685 s->mb_intra= 0;
1677 for(i=0; i<4; i++){ 1686 for(i=0; i<4; i++){
1678 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; 1687 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
1679 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; 1688 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
1680 } 1689 }
1681 init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL); 1690 init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
1682 s->block= s->inter4v_block; 1691 s->block= s->blocks[next_block];
1683 1692
1684 encode_mb(s, 0, 0); 1693 encode_mb(s, 0, 0);
1685 d= get_bit_count(&s->pb); 1694 d= get_bit_count(&s->pb);
1686 if(d<dmin && 0){ 1695 if(d<dmin){
1687 flush_put_bits(&s->pb); 1696 flush_put_bits(&s->pb);
1688 dmin=d; 1697 dmin=d;
1689 copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V); 1698 copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);
1690 best=2; 1699 best=2;
1700 next_block^=1;
1701 }
1702 }
1703 if(mb_type&MB_TYPE_FORWARD){
1704 copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD);
1705 s->mv_dir = MV_DIR_FORWARD;
1706 s->mv_type = MV_TYPE_16X16;
1707 s->mb_intra= 0;
1708 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1709 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1710 init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL);
1711 s->block= s->blocks[next_block];
1712
1713 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1714 d= get_bit_count(&s->pb);
1715 if(d<dmin){
1716 flush_put_bits(&s->pb);
1717 dmin=d;
1718 copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD);
1719 best=3;
1720 next_block^=1;
1721 }
1722 }
1723 if(mb_type&MB_TYPE_BACKWARD){
1724 copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD);
1725 s->mv_dir = MV_DIR_BACKWARD;
1726 s->mv_type = MV_TYPE_16X16;
1727 s->mb_intra= 0;
1728 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1729 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1730 init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL);
1731 s->block= s->blocks[next_block];
1732
1733 encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]);
1734 d= get_bit_count(&s->pb);
1735 if(d<dmin){
1736 flush_put_bits(&s->pb);
1737 dmin=d;
1738 copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD);
1739 best=4;
1740 next_block^=1;
1741 }
1742 }
1743 if(mb_type&MB_TYPE_BIDIR){
1744 copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR);
1745 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1746 s->mv_type = MV_TYPE_16X16;
1747 s->mb_intra= 0;
1748 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1749 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1750 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1751 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1752 init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL);
1753 s->block= s->blocks[next_block];
1754
1755 encode_mb(s, 0, 0);
1756 d= get_bit_count(&s->pb);
1757 if(d<dmin){
1758 flush_put_bits(&s->pb);
1759 dmin=d;
1760 copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR);
1761 best=5;
1762 next_block^=1;
1763 }
1764 }
1765 if(mb_type&MB_TYPE_DIRECT){
1766 copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT);
1767 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1768 s->mv_type = MV_TYPE_16X16; //FIXME
1769 s->mb_intra= 0;
1770 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1771 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1772 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1773 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1774 init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL);
1775 s->block= s->blocks[next_block];
1776
1777 encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
1778 d= get_bit_count(&s->pb);
1779 if(d<dmin){
1780 flush_put_bits(&s->pb);
1781 dmin=d;
1782 copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT);
1783 best=6;
1784 next_block^=1;
1691 } 1785 }
1692 } 1786 }
1693 if(mb_type&MB_TYPE_INTRA){ 1787 if(mb_type&MB_TYPE_INTRA){
1694 copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA); 1788 copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);
1789 s->mv_dir = MV_DIR_FORWARD;
1695 s->mv_type = MV_TYPE_16X16; 1790 s->mv_type = MV_TYPE_16X16;
1696 s->mb_intra= 1; 1791 s->mb_intra= 1;
1697 s->mv[0][0][0] = 0; 1792 s->mv[0][0][0] = 0;
1698 s->mv[0][0][1] = 0; 1793 s->mv[0][0][1] = 0;
1699 init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL); 1794 init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
1700 s->block= s->intra_block; 1795 s->block= s->blocks[next_block];
1701 1796
1702 encode_mb(s, 0, 0); 1797 encode_mb(s, 0, 0);
1703 d= get_bit_count(&s->pb); 1798 d= get_bit_count(&s->pb);
1704 if(d<dmin){ 1799 if(d<dmin){
1705 flush_put_bits(&s->pb); 1800 flush_put_bits(&s->pb);
1706 dmin=d; 1801 dmin=d;
1707 copy_context_after_encode(&best_s, s, MB_TYPE_INTRA); 1802 copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);
1708 best=0; 1803 best=0;
1804 next_block^=1;
1709 } 1805 }
1710 /* force cleaning of ac/dc pred stuff if needed ... */ 1806 /* force cleaning of ac/dc pred stuff if needed ... */
1711 if(s->h263_pred || s->h263_aic) 1807 if(s->h263_pred || s->h263_aic)
1712 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; 1808 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1713 } 1809 }
1716 s->pb= pb; 1812 s->pb= pb;
1717 } else { 1813 } else {
1718 int motion_x, motion_y; 1814 int motion_x, motion_y;
1719 s->mv_type=MV_TYPE_16X16; 1815 s->mv_type=MV_TYPE_16X16;
1720 // only one MB-Type possible 1816 // only one MB-Type possible
1721 //FIXME convert to swicth() 1817 switch(mb_type){
1722 if(mb_type&MB_TYPE_INTRA){ 1818 case MB_TYPE_INTRA:
1723 s->mv_dir = MV_DIR_FORWARD; 1819 s->mv_dir = MV_DIR_FORWARD;
1724 s->mb_intra= 1; 1820 s->mb_intra= 1;
1725 motion_x= s->mv[0][0][0] = 0; 1821 motion_x= s->mv[0][0][0] = 0;
1726 motion_y= s->mv[0][0][1] = 0; 1822 motion_y= s->mv[0][0][1] = 0;
1727 }else if(mb_type&MB_TYPE_INTER){ 1823 break;
1728 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; 1824 case MB_TYPE_INTER:
1729 s->mv_dir = MV_DIR_FORWARD; 1825 s->mv_dir = MV_DIR_FORWARD;
1730 s->mb_intra= 0; 1826 s->mb_intra= 0;
1731 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0]; 1827 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
1732 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1]; 1828 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
1733 }else if(mb_type&MB_TYPE_DIRECT){ 1829 break;
1734 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; 1830 case MB_TYPE_DIRECT:
1735 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; 1831 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1736 s->mb_intra= 0; 1832 s->mb_intra= 0;
1737 motion_x=0; 1833 motion_x=s->b_direct_mv_table[xy][0];
1738 motion_y=0; 1834 motion_y=s->b_direct_mv_table[xy][1];
1739 s->mv[0][0][0] = 0; 1835 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1740 s->mv[0][0][1] = 0; 1836 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1741 s->mv[1][0][0] = 0; 1837 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1742 s->mv[1][0][1] = 0; 1838 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1743 }else if(mb_type&MB_TYPE_BIDIR){ 1839 break;
1744 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; 1840 case MB_TYPE_BIDIR:
1745 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; 1841 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1746 s->mb_intra= 0; 1842 s->mb_intra= 0;
1747 motion_x=0; 1843 motion_x=0;
1748 motion_y=0; 1844 motion_y=0;
1749 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; 1845 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1750 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; 1846 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1751 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; 1847 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1752 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; 1848 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1753 }else if(mb_type&MB_TYPE_BACKWARD){ 1849 break;
1754 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; 1850 case MB_TYPE_BACKWARD:
1755 s->mv_dir = MV_DIR_BACKWARD; 1851 s->mv_dir = MV_DIR_BACKWARD;
1756 s->mb_intra= 0; 1852 s->mb_intra= 0;
1757 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0]; 1853 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1758 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1]; 1854 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1759 }else if(mb_type&MB_TYPE_FORWARD){ 1855 break;
1760 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; 1856 case MB_TYPE_FORWARD:
1761 s->mv_dir = MV_DIR_FORWARD; 1857 s->mv_dir = MV_DIR_FORWARD;
1762 s->mb_intra= 0; 1858 s->mb_intra= 0;
1763 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; 1859 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1764 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; 1860 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1765 // printf(" %d %d ", motion_x, motion_y); 1861 // printf(" %d %d ", motion_x, motion_y);
1766 }else{ 1862 break;
1863 default:
1767 motion_x=motion_y=0; //gcc warning fix 1864 motion_x=motion_y=0; //gcc warning fix
1768 printf("illegal MB type\n"); 1865 printf("illegal MB type\n");
1769 } 1866 }
1770 encode_mb(s, motion_x, motion_y); 1867 encode_mb(s, motion_x, motion_y);
1868 }
1869 /* clean the MV table in IPS frames for direct mode in B frames */
1870 if(s->mb_intra /* && I,P,S_TYPE */){
1871 s->p_mv_table[xy][0]=0;
1872 s->p_mv_table[xy][1]=0;
1771 } 1873 }
1772 1874
1773 MPV_decode_mb(s, s->block); 1875 MPV_decode_mb(s, s->block);
1774 } 1876 }
1775 1877