OSDN Git Service

BuildSystem:
[handbrake-jp/handbrake-jp-git.git] / libhb / decmpeg2.c
index e5fcdc5..4e21aa1 100644 (file)
@@ -5,7 +5,7 @@
    It may be used under the terms of the GNU General Public License. */
 
 #include "hb.h"
-
+#include "hbffmpeg.h"
 #include "mpeg2dec/mpeg2.h"
 
 /* Cadence tracking */
@@ -25,8 +25,6 @@
 #define BTB_PROG 64
 #define TB_PROG 128
 #define TBT_PROG 256
-static int cadence[12];
-static int flag = 0;
 
 /**********************************************************************
  * hb_libmpeg2_t
@@ -39,12 +37,14 @@ typedef struct hb_libmpeg2_s
     int                  width;
     int                  height;
     int                  rate;
-    int                  aspect_ratio;
+    double               aspect_ratio;
     int                  got_iframe;        /* set when we get our first iframe */
     int                  look_for_iframe;   /* need an iframe to add chap break */
     int                  look_for_break;    /* need gop start to add chap break */
     uint32_t             nframes;           /* number of frames we've decoded */
     int64_t              last_pts;
+    int cadence[12];
+    int flag;
 } hb_libmpeg2_t;
 
 /**********************************************************************
@@ -63,6 +63,65 @@ static hb_libmpeg2_t * hb_libmpeg2_init()
     return m;
 }
 
+static hb_buffer_t *hb_copy_frame( hb_job_t *job, int width, int height,
+                                   uint8_t* y, uint8_t *u, uint8_t *v )
+{
+    int dst_w = width, dst_h = height;
+    if ( job )
+    {
+        dst_w = job->title->width;
+        dst_h = job->title->height;
+    }
+    int dst_wh = dst_w * dst_h;
+    hb_buffer_t *buf  = hb_video_buffer_init( dst_w, dst_h );
+
+    if ( dst_w != width || dst_h != height )
+    {
+        // we're encoding and the frame dimensions don't match the title dimensions -
+        // rescale & matte Y, U, V into our output buf.
+        AVPicture in, out;
+        avpicture_alloc(&in,  PIX_FMT_YUV420P, width, height );
+        avpicture_alloc(&out, PIX_FMT_YUV420P, dst_w, dst_h );
+
+        int src_wh = width * height;
+        memcpy( in.data[0], y, src_wh );
+        memcpy( in.data[1], u, src_wh >> 2 );
+        memcpy( in.data[2], v, src_wh >> 2 );
+        struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUV420P,
+                                                     dst_w, dst_h, PIX_FMT_YUV420P,
+                                                     SWS_LANCZOS|SWS_ACCURATE_RND,
+                                                     NULL, NULL, NULL );
+        sws_scale( context, in.data, in.linesize, 0, height, out.data, out.linesize );
+        sws_freeContext( context );
+
+        u_int8_t *data = buf->data;
+        memcpy( data, out.data[0], dst_wh );
+        data += dst_wh;
+        // U & V planes are 1/4 the size of Y plane.
+        dst_wh >>= 2;
+        memcpy( data, out.data[1], dst_wh );
+        data += dst_wh;
+        memcpy( data, out.data[2], dst_wh );
+
+        avpicture_free( &out );
+        avpicture_free( &in );
+    }
+    else
+    {
+        // we're scanning or the frame dimensions match the title's dimensions
+        // so we can do a straight copy.
+        u_int8_t *data = buf->data;
+        memcpy( data, y, dst_wh );
+        data += dst_wh;
+        // U & V planes are 1/4 the size of Y plane.
+        dst_wh >>= 2;
+        memcpy( data, u, dst_wh );
+        data += dst_wh;
+        memcpy( data, v, dst_wh );
+    }
+    return buf;
+}
+
 /**********************************************************************
  * hb_libmpeg2_decode
  **********************************************************************
@@ -73,7 +132,6 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
 {
     mpeg2_state_t   state;
     hb_buffer_t   * buf;
-    uint8_t       * data;
 
     if ( buf_es->size )
     {
@@ -109,13 +167,10 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
                      * it keeps the pixel width & height that would cause
                      * the storage width & height to come out in the correct
                      * aspect ratio. Convert these back to aspect ratio.
-                     * We do the calc in floating point to get the rounding right.
-                     * We round in the second decimal digit because we scale
-                     * the (integer) aspect by 9 to preserve the 1st digit.
                      */
                     double ar_numer = m->width * m->info->sequence->pixel_width;
                     double ar_denom = m->height * m->info->sequence->pixel_height;
-                    m->aspect_ratio = ( ar_numer / ar_denom + .05 ) * HB_ASPECT_BASE;
+                    m->aspect_ratio = ar_numer / ar_denom;
                 }
             }
         }
@@ -138,29 +193,18 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
 
             if( m->got_iframe )
             {
-                buf  = hb_buffer_init( m->width * m->height * 3 / 2 );
-                data = buf->data;
-
+                buf  = hb_copy_frame( m->job, m->info->sequence->width,
+                                      m->info->sequence->height,
+                                      m->info->display_fbuf->buf[0],
+                                      m->info->display_fbuf->buf[1],
+                                      m->info->display_fbuf->buf[2] );
                 buf->sequence = buf_es->sequence;
 
-                memcpy( data, m->info->display_fbuf->buf[0],
-                        m->width * m->height );
-                data += m->width * m->height;
-                memcpy( data, m->info->display_fbuf->buf[1],
-                        m->width * m->height / 4 );
-                data += m->width * m->height / 4;
-                memcpy( data, m->info->display_fbuf->buf[2],
-                        m->width * m->height / 4 );
-
                 if( m->info->display_picture->flags & PIC_FLAG_TAGS )
                 {
                     buf->start =
                         ( (uint64_t) m->info->display_picture->tag << 32 ) |
                         ( (uint64_t) m->info->display_picture->tag2 );
-                   /*
-                     * Add back in again to track PTS of MPEG2 frames
-                     * hb_log("MPEG2: Normal buf->start = %lld", buf->start);
-                   */
                 }
                 else if( m->last_pts > -1 )
                 {
@@ -205,23 +249,23 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
                 }
                 ++m->nframes;
 
-                flag = m->info->display_picture->flags;
+                m->flag = m->info->display_picture->flags;
 
 /*  Uncomment this block to see frame-by-frame picture flags, as the video encodes.
                hb_log("***** MPEG 2 Picture Info for PTS %lld *****", buf->start);
-                if( flag & TOP_FIRST )
+                if( m->flag & TOP_FIRST )
                     hb_log("MPEG2 Flag: Top field first");
-                if( flag & PROGRESSIVE )
+                if( m->flag & PROGRESSIVE )
                     hb_log("MPEG2 Flag: Progressive");
-                if( flag & COMPOSITE )
+                if( m->flag & COMPOSITE )
                     hb_log("MPEG2 Flag: Composite");
-                if( flag & SKIP )
+                if( m->flag & SKIP )
                     hb_log("MPEG2 Flag: Skip!");
-                if( flag & TAGS )
+                if( m->flag & TAGS )
                     hb_log("MPEG2 Flag: TAGS");
-                if(flag & REPEAT_FIRST )
+                if(fm->lag & REPEAT_FIRST )
                     hb_log("MPEG2 Flag: Repeat first field");
-                if( flag & COMPOSITE_MASK )
+                if( m->flag & COMPOSITE_MASK )
                     hb_log("MPEG2 Flag: Composite mask");
                 hb_log("fields: %d", m->info->display_picture->nb_fields);
 */
@@ -229,66 +273,66 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
                 int i = 0;
                 for(i=11; i > 0; i--)
                 {
-                    cadence[i] = cadence[i-1];
+                    m->cadence[i] = m->cadence[i-1];
                 }
 
-                if ( !(flag & PROGRESSIVE) && !(flag & TOP_FIRST) )
+                if ( !(m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) )
                 {
                     /* Not progressive, not top first...
                        That means it's probably bottom
                        first, 2 fields displayed.
                     */
                     //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
-                    cadence[0] = BT;
+                    m->cadence[0] = BT;
                 }
-                else if ( !(flag & PROGRESSIVE) && (flag & TOP_FIRST) )
+                else if ( !(m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) )
                 {
                     /* Not progressive, top is first,
                        Two fields displayed.
                     */
                     //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
-                    cadence[0] = TB;
+                    m->cadence[0] = TB;
                 }
-                else if ( (flag & PROGRESSIVE) && !(flag & TOP_FIRST) && !( flag & REPEAT_FIRST )  )
+                else if ( (m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) && !( m->flag & REPEAT_FIRST )  )
                 {
                     /* Progressive, but noting else.
                        That means Bottom first,
                        2 fields displayed.
                     */
                     //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
-                    cadence[0] = BT_PROG;
+                    m->cadence[0] = BT_PROG;
                 }
-                else if ( (flag & PROGRESSIVE) && !(flag & TOP_FIRST) && ( flag & REPEAT_FIRST )  )
+                else if ( (m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) && ( m->flag & REPEAT_FIRST )  )
                 {
                     /* Progressive, and repeat. .
                        That means Bottom first,
                        3 fields displayed.
                     */
                     //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
-                    cadence[0] = BTB_PROG;
+                    m->cadence[0] = BTB_PROG;
                 }
-                else if ( (flag & PROGRESSIVE) && (flag & TOP_FIRST) && !( flag & REPEAT_FIRST )  )
+                else if ( (m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) && !( m->flag & REPEAT_FIRST )  )
                 {
                     /* Progressive, top first.
                        That means top first,
                        2 fields displayed.
                     */
                     //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
-                    cadence[0] = TB_PROG;
+                    m->cadence[0] = TB_PROG;
                 }
-                else if ( (flag & PROGRESSIVE) && (flag & TOP_FIRST) && ( flag & REPEAT_FIRST )  )
+                else if ( (m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) && ( m->flag & REPEAT_FIRST )  )
                 {
                     /* Progressive, top, repeat.
                        That means top first,
                        3 fields displayed.
                     */
                     //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
-                    cadence[0] = TBT_PROG;
+                    m->cadence[0] = TBT_PROG;
                 }
 
-                if ( (cadence[2] <= TB) && (cadence[1] <= TB) && (cadence[0] > TB) && (cadence[11]) )
+                if ( (m->cadence[2] <= TB) && (m->cadence[1] <= TB) && (m->cadence[0] > TB) && (m->cadence[11]) )
                     hb_log("%fs: Video -> Film", (float)buf->start / 90000);
-                if ( (cadence[2] > TB) && (cadence[1] <= TB) && (cadence[0] <= TB) && (cadence[11]) )
+                if ( (m->cadence[2] > TB) && (m->cadence[1] <= TB) && (m->cadence[0] <= TB) && (m->cadence[11]) )
                     hb_log("%fs: Film -> Video", (float)buf->start / 90000);
 
                 /* Store picture flags for later use by filters */
@@ -306,33 +350,6 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
 }
 
 /**********************************************************************
- * hb_libmpeg2_info
- **********************************************************************
- *
- *********************************************************************/
-static void hb_libmpeg2_info( hb_libmpeg2_t * m, int * width, int * height,
-                        int * rate, int *aspect_ratio )
-{
-    *width  = m->width;
-    *height = m->height;
-    if (m->info->display_fbuf)
-    {
-        if( (m->info->display_picture->flags & PROGRESSIVE) && (m->height == 480) )
-        {
-            /* The frame is progressive and it's NTSC DVD height, so change its FPS to 23.976.
-               This might not be correct for the title. It's really just for scan.c's benefit.
-               Scan.c will reset the fps to 29.97, until a simple majority of the preview
-               frames report at 23.976.
-            */
-            //hb_log("Detecting NTSC Progressive Frame");
-            m->rate = 1126125;
-        }
-    }
-    *rate   = m->rate;
-    *aspect_ratio = m->aspect_ratio;
-}
-
-/**********************************************************************
  * hb_libmpeg2_close
  **********************************************************************
  *
@@ -450,16 +467,24 @@ static int decmpeg2Info( hb_work_object_t *w, hb_work_info_t *info )
 {
     hb_work_private_t *pv = w->private_data;
 
-    if ( pv && pv->libmpeg2 )
+    memset( info, 0, sizeof(*info) );
+
+    if ( pv && pv->libmpeg2 && pv->libmpeg2->info && pv->libmpeg2->info->sequence )
     {
-        int aspect;
         hb_libmpeg2_t *m = pv->libmpeg2;
 
-        hb_libmpeg2_info( m, &info->width, &info->height, &info->rate_base,
-                          &aspect );
+        info->width = m->width;
+        info->height = m->height;
+        info->pixel_aspect_width = m->info->sequence->pixel_width;
+        info->pixel_aspect_height = m->info->sequence->pixel_height;
+        info->aspect = m->aspect_ratio;
 
-        info->aspect = (double)aspect;
+        // if the frame is progressive & NTSC DVD height report it as 23.976 FPS
+        // so that scan can autodetect NTSC film
         info->rate = 27000000;
+        info->rate_base = ( m->info->display_fbuf && m->info->display_picture &&
+                            (m->info->display_picture->flags & PROGRESSIVE) &&
+                            (m->height == 480 ) ) ?  1126125 : m->rate;
 
         info->bitrate = m->info->sequence->byte_rate * 8;
         info->profile = m->info->sequence->profile_level_id >> 4;