Commit 63b15e55 authored by Michael Niedermayer's avatar Michael Niedermayer

mpeg1 bframe encoding patch by (Raphaël LEGRAND) with some modifications by me

Originally committed as revision 1551 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 27efd846
...@@ -2234,10 +2234,6 @@ void opt_output_file(const char *filename) ...@@ -2234,10 +2234,6 @@ void opt_output_file(const char *filename)
if (b_frames) { if (b_frames) {
if (codec_id != CODEC_ID_MPEG4) {
fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
exit(1);
}
video_enc->max_b_frames = b_frames; video_enc->max_b_frames = b_frames;
video_enc->b_frame_strategy = 0; video_enc->b_frame_strategy = 0;
video_enc->b_quant_factor = 2.0; video_enc->b_quant_factor = 2.0;
......
...@@ -1493,12 +1493,13 @@ void ff_estimate_b_frame_motion(MpegEncContext * s, ...@@ -1493,12 +1493,13 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor; fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
//printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin); //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
{ {
int score= dmin; int score= fmin;
type=MB_TYPE_DIRECT; type = MB_TYPE_FORWARD;
if(fmin<score){ // RAL: No MB_TYPE_DIRECT in MPEG-1 video (only MPEG-4)
score=fmin; if (s->codec_id != CODEC_ID_MPEG1VIDEO && dmin <= score){
type= MB_TYPE_FORWARD; score = dmin;
type = MB_TYPE_DIRECT;
} }
if(bmin<score){ if(bmin<score){
score=bmin; score=bmin;
...@@ -1643,21 +1644,32 @@ void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, i ...@@ -1643,21 +1644,32 @@ void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, i
int y; int y;
UINT8 * fcode_tab= s->fcode_tab; UINT8 * fcode_tab= s->fcode_tab;
// RAL: 8 in MPEG-1, 16 in MPEG-4
int range = (((s->codec_id == CODEC_ID_MPEG1VIDEO) ? 8 : 16) << f_code);
/* clip / convert to intra 16x16 type MVs */ /* clip / convert to intra 16x16 type MVs */
for(y=0; y<s->mb_height; y++){ for(y=0; y<s->mb_height; y++){
int x; int x;
int xy= (y+1)* (s->mb_width+2)+1; int xy= (y+1)* (s->mb_width+2)+1;
int i= y*s->mb_width; int i= y*s->mb_width;
for(x=0; x<s->mb_width; x++){ for(x=0; x<s->mb_width; x++)
if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code {
|| fcode_tab[mv_table[xy][0] + MAX_MV] == 0){ if (s->mb_type[i] & type) // RAL: "type" test added...
if(mv_table[xy][0]>0) mv_table[xy][0]= (16<<f_code)-1; {
else mv_table[xy][0]= -(16<<f_code); if (fcode_tab[mv_table[xy][0] + MAX_MV] > f_code || fcode_tab[mv_table[xy][0] + MAX_MV] == 0)
} {
if( fcode_tab[mv_table[xy][1] + MAX_MV] > f_code if(mv_table[xy][0]>0)
|| fcode_tab[mv_table[xy][1] + MAX_MV] == 0){ mv_table[xy][0]= range-1;
if(mv_table[xy][1]>0) mv_table[xy][1]= (16<<f_code)-1; else
else mv_table[xy][1]= -(16<<f_code); mv_table[xy][0]= -range;
}
if (fcode_tab[mv_table[xy][1] + MAX_MV] > f_code || fcode_tab[mv_table[xy][1] + MAX_MV] == 0)
{
if(mv_table[xy][1]>0)
mv_table[xy][1]= range-1;
else
mv_table[xy][1]= -range;
}
} }
xy++; xy++;
i++; i++;
......
This diff is collapsed.
...@@ -2872,7 +2872,8 @@ static void encode_picture(MpegEncContext *s, int picture_number) ...@@ -2872,7 +2872,8 @@ static void encode_picture(MpegEncContext *s, int picture_number)
#ifdef CONFIG_RISKY #ifdef CONFIG_RISKY
/* we need to initialize some time vars before we can encode b-frames */ /* we need to initialize some time vars before we can encode b-frames */
if (s->h263_pred && !s->h263_msmpeg4) // RAL: Condition added for MPEG1VIDEO
if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
ff_set_mpeg4_time(s, s->picture_number); ff_set_mpeg4_time(s, s->picture_number);
#endif #endif
...@@ -2965,12 +2966,24 @@ static void encode_picture(MpegEncContext *s, int picture_number) ...@@ -2965,12 +2966,24 @@ static void encode_picture(MpegEncContext *s, int picture_number)
//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum); //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
} }
if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER); s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
// RAL: Next call moved into that bloc
ff_fix_long_p_mvs(s); ff_fix_long_p_mvs(s);
}
// RAL: All this bloc changed
if(s->pict_type==B_TYPE){ if(s->pict_type==B_TYPE){
s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD); int a, b;
s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
s->f_code = FFMAX(a, b);
a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
s->b_code = FFMAX(a, b);
ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD); ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD); ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
...@@ -3064,6 +3077,10 @@ static void encode_picture(MpegEncContext *s, int picture_number) ...@@ -3064,6 +3077,10 @@ static void encode_picture(MpegEncContext *s, int picture_number)
s->mb_incr = 1; s->mb_incr = 1;
s->last_mv[0][0][0] = 0; s->last_mv[0][0][0] = 0;
s->last_mv[0][0][1] = 0; s->last_mv[0][0][1] = 0;
s->last_mv[1][0][0] = 0;
s->last_mv[1][0][1] = 0;
s->last_mv_dir = 0;
#ifdef CONFIG_RISKY #ifdef CONFIG_RISKY
if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P) if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
...@@ -3227,7 +3244,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) ...@@ -3227,7 +3244,7 @@ static void encode_picture(MpegEncContext *s, int picture_number)
&dmin, &next_block, mx, my); &dmin, &next_block, mx, my);
} }
if(mb_type&MB_TYPE_INTRA){ if(mb_type&MB_TYPE_INTRA){
s->mv_dir = MV_DIR_FORWARD; s->mv_dir = 0;
s->mv_type = MV_TYPE_16X16; s->mv_type = MV_TYPE_16X16;
s->mb_intra= 1; s->mb_intra= 1;
s->mv[0][0][0] = 0; s->mv[0][0][0] = 0;
...@@ -3348,7 +3365,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) ...@@ -3348,7 +3365,7 @@ static void encode_picture(MpegEncContext *s, int picture_number)
switch(mb_type){ switch(mb_type){
case MB_TYPE_INTRA: case MB_TYPE_INTRA:
s->mv_dir = MV_DIR_FORWARD; s->mv_dir = 0;
s->mb_intra= 1; s->mb_intra= 1;
motion_x= s->mv[0][0][0] = 0; motion_x= s->mv[0][0][0] = 0;
motion_y= s->mv[0][0][1] = 0; motion_y= s->mv[0][0][1] = 0;
...@@ -3405,8 +3422,13 @@ static void encode_picture(MpegEncContext *s, int picture_number) ...@@ -3405,8 +3422,13 @@ static void encode_picture(MpegEncContext *s, int picture_number)
motion_x=motion_y=0; //gcc warning fix motion_x=motion_y=0; //gcc warning fix
printf("illegal MB type\n"); printf("illegal MB type\n");
} }
encode_mb(s, motion_x, motion_y); encode_mb(s, motion_x, motion_y);
// RAL: Update last macrobloc type
s->last_mv_dir = s->mv_dir;
} }
/* clean the MV table in IPS frames for direct mode in B frames */ /* clean the MV table in IPS frames for direct mode in B frames */
if(s->mb_intra /* && I,P,S_TYPE */){ if(s->mb_intra /* && I,P,S_TYPE */){
s->p_mv_table[xy][0]=0; s->p_mv_table[xy][0]=0;
......
...@@ -508,6 +508,7 @@ typedef struct MpegEncContext { ...@@ -508,6 +508,7 @@ typedef struct MpegEncContext {
/* Mpeg1 specific */ /* Mpeg1 specific */
int fake_picture_number; /* picture number at the bitstream frame rate */ int fake_picture_number; /* picture number at the bitstream frame rate */
int gop_picture_number; /* index of the first picture of a GOP based on fake_pic_num & mpeg1 specific */ int gop_picture_number; /* index of the first picture of a GOP based on fake_pic_num & mpeg1 specific */
int last_mv_dir; /* last mv_dir, used for b frame encoding */
/* MPEG2 specific - I wish I had not to support this mess. */ /* MPEG2 specific - I wish I had not to support this mess. */
int progressive_sequence; int progressive_sequence;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment