[MPlayer-dev-eng] [PATCH] fix aspect ratio bug for MPEG1 PAL 16/9

Giacomo Comes comes at naic.edu
Wed Aug 23 17:19:30 CEST 2006


I have found a small bug of libavcodec during the creation of mpeg1 video.
Here is the description of the problem.

The following command is a simple one for creating MPEG1 PAL video with 16/9
aspect ratio:

mencoder -of mpeg -mpegopts format=xvcd -ovc lavc -lavcopts vcodec=mpeg1video:vrc_buf_size=327:vrc_minrate=1152:vrc_maxrate=1152:keyint=15:vmax_b_frames=2:aspect=16/9:vbitrate=1152 -o test.mpg source.avi -ofps 25 -vf scale=352:288,harddup -oac lavc -lavcopts acodec=mp2:abitrate=224

The MPEG1 standard says that the values for the 4 bit pel_aspect_ratio field are:

pel  w/h  
 3  0.7031   16:9 625 lines
 6  0.8437   16:9 525 lines
 8  0.9375    4:3 625 lines
12  1.1250    4:3 525 lines

libavcodec set the correct value for the aspect ratio in all cases except the first
one. MPEG1 PAL 16/9 instead of having aspect 3 has aspect 2.

The code that set the aspect ratio in the function mpeg1_encode_sequence_header
in the file mpeg12.c is:

    for(i=1; i<15; i++){
        float error= aspect_ratio;
        if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
            error-= 1.0/mpeg1_aspect[i];
        else
            error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
        error= ABS(error);
        if(error < best_aspect_error){
            best_aspect_error= error;
            s->aspect_ratio_info= i;
        }
    }

The code itself is not wrong: for PAL 16/9, the aspect ratio that gives the smallest
best_aspect_error is really 2 (2 -> 0.030235, 3 -> 0.032272), but the standard
says that in this case the aspect has to be 3. Other tools, like mjpegtools,
follow the standard and set 3 as aspect ratio for MPEG1 PAL 16/9.

The attached patch check for the specific case (MPEG1 PAL 16/9) and set aspect 3,
leaving all the other cases to the for loop.

If you think the patch is correct and should be committed, please forward to the 
appropriate list.

Giacomo
-------------- next part --------------
diff -Nraub mplayer.ori/libavcodec/mpeg12.c mplayer/libavcodec/mpeg12.c
--- mplayer.ori/libavcodec/mpeg12.c	2006-07-21 12:41:41.000000000 -0400
+++ mplayer/libavcodec/mpeg12.c	2006-08-23 10:59:50.000000000 -0400
@@ -270,6 +270,9 @@
             put_bits(&s->pb, 12, s->width);
             put_bits(&s->pb, 12, s->height);
 
+            if(s->codec_id == CODEC_ID_MPEG1VIDEO && ABS(aspect_ratio-16.0/11) < 1E-5 ){
+                s->aspect_ratio_info = 3;
+            } else {
             for(i=1; i<15; i++){
                 float error= aspect_ratio;
                 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
@@ -284,6 +287,7 @@
                     s->aspect_ratio_info= i;
                 }
             }
+            }
 
             put_bits(&s->pb, 4, s->aspect_ratio_info);
             put_bits(&s->pb, 4, s->frame_rate_index);


More information about the MPlayer-dev-eng mailing list