changeset 880:1c32039e7215 libavcodec

aspect ratio cleanup
author michaelni
date Wed, 20 Nov 2002 13:08:04 +0000
parents af969e91f422
children d326091dae9f
files avcodec.h common.c common.h h263.c h263dec.c mpegvideo.c
diffstat 6 files changed, 76 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/avcodec.h	Wed Nov 20 07:32:18 2002 +0000
+++ b/avcodec.h	Wed Nov 20 13:08:04 2002 +0000
@@ -5,8 +5,8 @@
 
 #define LIBAVCODEC_VERSION_INT 0x000406
 #define LIBAVCODEC_VERSION     "0.4.6"
-#define LIBAVCODEC_BUILD       4639
-#define LIBAVCODEC_BUILD_STR   "4639"
+#define LIBAVCODEC_BUILD       4640
+#define LIBAVCODEC_BUILD_STR   "4640"
 
 enum CodecID {
     CODEC_ID_NONE, 
@@ -145,6 +145,7 @@
 #define CODEC_FLAG_NORMALIZE_AQP  0x00020000 /* normalize adaptive quantization */
 #define CODEC_FLAG_INTERLACED_DCT 0x00040000 /* use interlaced dct */
 #define CODEC_FLAG_LOW_DELAY      0x00080000 /* force low delay / will fail on b frames */
+#define CODEC_FLAG_ALT_SCAN       0x00100000 /* use alternate scan */
 
 /* codec capabilities */
 
@@ -222,8 +223,7 @@
     int width, height;
     
     /**
-     * encoding: set by user. 0 if not known
-     * decoding: set by lavc. 0 if not known
+     * Obsolete, will be removed
      */
     int aspect_ratio_info;
 #define FF_ASPECT_SQUARE 1
@@ -646,9 +646,7 @@
     float rc_initial_cplx;
 
     /**
-     * custom aspect ratio, used if aspect_info==FF_ASPECT_EXTENDED
-     * encoding: set by user.
-     * decoding: set by lavc.
+     * Obsolete, will be removed
      */
     int aspected_width;
     int aspected_height;
@@ -795,6 +793,13 @@
 #define FF_PRED_LEFT   0
 #define FF_PRED_PLANE  1
 #define FF_PRED_MEDIAN 2
+    
+    /**
+     * aspect ratio. (0 if unknown)
+     * encoding: set by user.
+     * decoding: set by lavc.
+     */
+    float aspect_ratio;
 } AVCodecContext;
 
 typedef struct AVCodec {
--- a/common.c	Wed Nov 20 07:32:18 2002 +0000
+++ b/common.c	Wed Nov 20 13:08:04 2002 +0000
@@ -326,3 +326,29 @@
     if(b) return ff_gcd(b, a%b);
     else  return a;
 }
+
+void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max){
+    double best_diff=1E10, diff;
+    int best_denom=1, best_nom=1;
+    int nom, denom, gcd;
+    
+    //brute force here, perhaps we should try continued fractions if we need large max ...
+    for(denom=1; denom<=max; denom++){
+        nom= (int)(f*denom + 0.5);
+        if(nom<=0 || nom>max) continue;
+        
+        diff= ABS( f - (double)nom / (double)denom );
+        if(diff < best_diff){
+            best_diff= diff;
+            best_nom= nom;
+            best_denom= denom;
+        }
+    }
+    
+    gcd= ff_gcd(best_nom, best_denom);
+    best_nom   /= gcd;
+    best_denom /= gcd;
+
+    *nom_arg= best_nom;
+    *denom_arg= best_denom;
+}
--- a/common.h	Wed Nov 20 07:32:18 2002 +0000
+++ b/common.h	Wed Nov 20 13:08:04 2002 +0000
@@ -842,6 +842,8 @@
     return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
 }
 
+void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
+
 
 #ifdef ARCH_X86
 #define MASK_ABS(mask, level)\
--- a/h263.c	Wed Nov 20 07:32:18 2002 +0000
+++ b/h263.c	Wed Nov 20 13:08:04 2002 +0000
@@ -120,6 +120,26 @@
     return format;
 }
 
+static void init_aspect_info(MpegEncContext * s){
+    double aspect;
+    
+    emms_c(); //paranoia ;)
+    
+    if(s->avctx->aspect_ratio==0) aspect= 1.0;
+    aspect= s->avctx->aspect_ratio;
+    
+    ff_float2fraction(&s->aspected_width, &s->aspected_height, aspect, 255);
+    
+    if(s->aspected_width == 4 && s->aspected_height == 3)
+        s->aspect_ratio_info= FF_ASPECT_4_3_625;
+    else if(s->aspected_width == 16 && s->aspected_height == 9)
+        s->aspect_ratio_info= FF_ASPECT_16_9_625;
+    else if(s->aspected_width == 1 && s->aspected_height == 1)
+        s->aspect_ratio_info= FF_ASPECT_SQUARE;
+    else
+        s->aspect_ratio_info= FF_ASPECT_EXTENDED;
+}
+
 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
 {
     int format;
@@ -196,11 +216,9 @@
 		
 		if (format == 7) {
             /* Custom Picture Format (CPFMT) */
-		
-	    if (s->aspect_ratio_info)
-        	put_bits(&s->pb,4,s->aspect_ratio_info);
-	    else
-        	put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
+            init_aspect_info(s);
+
+            put_bits(&s->pb,4,s->aspect_ratio_info);
             put_bits(&s->pb,9,(s->width >> 2) - 1);
             put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
             put_bits(&s->pb,9,(s->height >> 2));
@@ -1508,10 +1526,10 @@
     put_bits(&s->pb, 1, 1);		/* is obj layer id= yes */
       put_bits(&s->pb, 4, vo_ver_id);	/* is obj layer ver id */
       put_bits(&s->pb, 3, 1);		/* is obj layer priority */
-    if(s->aspect_ratio_info) 
-        put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
-    else
-        put_bits(&s->pb, 4, 1);		/* aspect ratio info= sqare pixel */
+    
+    init_aspect_info(s);
+
+    put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
     if (s->aspect_ratio_info == FF_ASPECT_EXTENDED)
     {
 	put_bits(&s->pb, 8, s->aspected_width);
--- a/h263dec.c	Wed Nov 20 07:32:18 2002 +0000
+++ b/h263dec.c	Wed Nov 20 13:08:04 2002 +0000
@@ -348,6 +348,8 @@
     MpegEncContext *s = avctx->priv_data;
     int ret,i;
     AVPicture *pict = data; 
+    float new_aspect;
+    
 #ifdef PRINT_FRAME_TIME
 uint64_t time= rdtsc();
 #endif
@@ -495,23 +497,19 @@
         /* and other parameters. So then we could init the picture   */
         /* FIXME: By the way H263 decoder is evolving it should have */
         /* an H263EncContext                                         */
+    if(s->aspected_height)
+        new_aspect= (float)s->aspected_width / (float)s->aspected_height;
+    else
+        new_aspect=0;
+
     if (   s->width != avctx->width || s->height != avctx->height 
-        || avctx->aspect_ratio_info != s->aspect_ratio_info
-        || avctx->aspected_width != s->aspected_width
-        || avctx->aspected_height != s->aspected_height) {
+        || ABS(new_aspect - avctx->aspect_ratio) > 0.001) {
         /* H.263 could change picture size any time */
         MPV_common_end(s);
         s->context_initialized=0;
     }
     if (!s->context_initialized) {
-        avctx->width = s->width;
-        avctx->height = s->height;
-        avctx->aspect_ratio_info= s->aspect_ratio_info;
-	if (s->aspect_ratio_info == FF_ASPECT_EXTENDED)
-	{
-	    avctx->aspected_width = s->aspected_width;
-	    avctx->aspected_height = s->aspected_height;
-	}
+        avctx->aspect_ratio= new_aspect;
 
         goto retry;
     }
--- a/mpegvideo.c	Wed Nov 20 07:32:18 2002 +0000
+++ b/mpegvideo.c	Wed Nov 20 13:08:04 2002 +0000
@@ -557,12 +557,6 @@
     s->qcompress= avctx->qcompress;
     s->qblur= avctx->qblur;
     s->avctx = avctx;
-    s->aspect_ratio_info= avctx->aspect_ratio_info;
-    if (avctx->aspect_ratio_info == FF_ASPECT_EXTENDED)
-    {
-	s->aspected_width = avctx->aspected_width;
-	s->aspected_height = avctx->aspected_height;
-    }
     s->flags= avctx->flags;
     s->max_b_frames= avctx->max_b_frames;
     s->b_frame_strategy= avctx->b_frame_strategy;