]> granicus.if.org Git - handbrake/commitdiff
Add PixelRatio support, when used, HB use the same pixel ratio than the source DVD...
authorprigaux <pri@nopapers.org>
Mon, 15 Jan 2007 08:56:54 +0000 (08:56 +0000)
committerprigaux <pri@nopapers.org>
Mon, 15 Jan 2007 08:56:54 +0000 (08:56 +0000)
git-svn-id: svn://svn.handbrake.fr/HandBrake/branches/0.7.3@116 b64f7644-9d1e-0410-96f1-a4d463321fa5

libhb/common.c
libhb/common.h
libhb/encavcodec.c
libhb/encx264.c
libhb/encxvid.c
libhb/scan.c

index e9d366f75a780d88b21307a5a7636900d866473f..7b41a93fb1be54bb23471e87f7196accaf2e3764 100644 (file)
@@ -36,6 +36,29 @@ int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) /
                               sizeof( hb_rate_t );
 int hb_audio_bitrates_default = 8; /* 128 kbps */
 
+/**********************************************************************
+ * hb_reduce
+ **********************************************************************
+ * Given a numerator (num) and a denominator (den), reduce them to an
+ * equivalent fraction and store the result in x and y.
+ *********************************************************************/
+void hb_reduce( int *x, int *y, int num, int den )
+{
+    int lower = MIN( num, den );
+    int i;
+    *x = num;
+    *y = den;
+    for( i = lower - 1; i > 1; --i )
+    {
+        if( ( num % i == 0 ) && ( den % i == 0 ) )
+        {
+            *x = num / i;
+            *y = den / i;
+            break;
+        }
+    }
+}
+
 /**********************************************************************
  * hb_fix_aspect
  **********************************************************************
index 859711f81e02df92d625e0584f4f3080a133b3dd..be56d1a3c6ee96d04521d743be89481279d6dddb 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef HB_COMMON_H
 #define HB_COMMON_H
 
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -55,6 +56,8 @@ void        hb_list_rem( hb_list_t *, void * );
 void      * hb_list_item( hb_list_t *, int );
 void        hb_list_close( hb_list_t ** );
 
+void hb_reduce( int *x, int *y, int num, int den );
+
 #define HB_KEEP_WIDTH  0
 #define HB_KEEP_HEIGHT 1
 void hb_fix_aspect( hb_job_t * job, int keep );
@@ -92,17 +95,24 @@ struct hb_job_s
     int             chapter_end;
 
     /* Picture settings:
-         crop:        must be multiples of 2 (top/bottom/left/right)
-         deinterlace: 0 or 1
-         width:       must be a multiple of 16
-         height:      must be a multiple of 16
-         keep_ratio:  used by UIs */
+         crop:                must be multiples of 2 (top/bottom/left/right)
+         deinterlace:         0 or 1
+         width:               must be a multiple of 16
+         height:              must be a multiple of 16
+         keep_ratio:          used by UIs 
+         pixel_ratio:         store pixel aspect ratio in the video
+         pixel_aspect_width:  numerator for pixel aspect ratio
+         pixel_aspect_height: denominator for pixel aspect ratio */
+
     int             crop[4];
     int             deinterlace;
     int             width;
     int             height;
     int             keep_ratio;
     int             grayscale;
+    int             pixel_ratio;
+    int             pixel_aspect_width;
+    int             pixel_aspect_height;
 
     /* Video settings:
          vcodec:            output codec
index f0460690698d022ef352f023ba0faa012279efc7..c71c8384e88df31f69b1e32d6d42da8682599db7 100644 (file)
@@ -65,6 +65,15 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
     context->gop_size  = 10 * job->vrate / job->vrate_base;
     context->pix_fmt   = PIX_FMT_YUV420P;
 
+    if( job->pixel_ratio )
+    {
+        context->sample_aspect_ratio.num = job->pixel_aspect_width;
+        context->sample_aspect_ratio.den = job->pixel_aspect_height;
+
+        hb_log( "encavcodec: encoding with stored aspect %d/%d", 
+                job->pixel_aspect_width, job->pixel_aspect_height );
+    }
+
     if( job->mux & ( HB_MUX_MP4 | HB_MUX_PSP ) )
     {
         context->flags |= CODEC_FLAG_GLOBAL_HEADER;
index 1bc24866c5144514441e438ae0b2b890b0945dd1..044b6fecd88e4b242854326621ece538d61129f9 100644 (file)
@@ -75,6 +75,16 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job )
     /* Slightly faster with minimal quality lost */
     param.analyse.i_subpel_refine = 4;
        
+    if( job->pixel_ratio )
+     {
+         param.vui.i_sar_width = job->pixel_aspect_width;
+         param.vui.i_sar_height = job->pixel_aspect_height;
+         hb_log( "encx264: encoding with stored aspect %d/%d",
+                 param.vui.i_sar_width, param.vui.i_sar_height );
+     }
+
     if( job->vquality >= 0.0 && job->vquality <= 1.0 )
     {
         switch(job->crf)
index 87c42b0a461162d4bad7f1c000762ce2e8f54b1f..7768f8e12a59d0af224c3bf6a5debc098dd5e1d8 100644 (file)
@@ -162,6 +162,13 @@ int encxvidWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     frame.vol_flags = 0;
     frame.vop_flags = XVID_VOP_HALFPEL | XVID_VOP_INTER4V |
                       XVID_VOP_TRELLISQUANT | XVID_VOP_HQACPRED;
+    if( job->pixel_ratio )
+    {
+        frame.par = XVID_PAR_EXT;
+        frame.par_width = job->pixel_aspect_width;
+        frame.par_height = job->pixel_aspect_height;
+    }
+
     if( job->grayscale )
     {
         frame.vop_flags |= XVID_VOP_GREYSCALE;
index 097f116381cf2bf294d89e7adc3752690ce0d08a..101b954ec0f569292da5db6582d75d5c75ce22db 100644 (file)
@@ -177,13 +177,20 @@ static void ScanFunc( void * _data )
         /* Autocrop by default. Gnark gnark */
         memcpy( job->crop, title->crop, 4 * sizeof( int ) );
 
-        job->width = title->width - job->crop[2] - job->crop[3];
-        hb_fix_aspect( job, HB_KEEP_WIDTH );
-        if( job->height > title->height - job->crop[0] - job->crop[1] )
+        if( title->aspect == 16 )
+        {
+            hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
+                       16 * title->height, 9 * title->width );
+        }
+        else
         {
-            job->height = title->height - job->crop[0] - job->crop[1];
-            hb_fix_aspect( job, HB_KEEP_HEIGHT );
+            hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
+                       4 * title->height, 3 * title->width );
         }
+
+        job->width = title->width - job->crop[2] - job->crop[3];
+        job->height = title->height - job->crop[0] - job->crop[1];
+
         job->keep_ratio = 1;
 
         job->vcodec     = HB_VCODEC_FFMPEG;