OSDN Git Service

LinGui: SRT support
[handbrake-jp/handbrake-jp-git.git] / libhb / decmpeg2.c
index 83c36f6..03d906d 100644 (file)
@@ -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
@@ -36,6 +34,7 @@ typedef struct hb_libmpeg2_s
     mpeg2dec_t         * libmpeg2;
     const mpeg2_info_t * info;
     hb_job_t           * job;
+    hb_title_t         * title;
     int                  width;
     int                  height;
     int                  rate;
@@ -45,6 +44,9 @@ typedef struct hb_libmpeg2_s
     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_list_t          * list_subtitle;
 } hb_libmpeg2_t;
 
 /**********************************************************************
@@ -63,6 +65,67 @@ static hb_libmpeg2_t * hb_libmpeg2_init()
     return m;
 }
 
+static void hb_mpeg2_cc( hb_libmpeg2_t * m, uint8_t *cc_block )
+{
+    uint8_t cc_valid = (*cc_block & 4) >>2;
+    uint8_t cc_type = *cc_block & 3;
+    hb_buffer_t *cc_buf;
+    int i;
+    hb_subtitle_t * subtitle;
+    
+    if( !m->job )
+    {
+        /*
+         * Ignore CC decoding during scanning.
+         */
+        return;
+    }
+
+    if (cc_valid || cc_type==3)
+    {
+        switch (cc_type)
+        {
+        case 0:
+            // CC1 stream
+            for (i = 0; i < hb_list_count( m->list_subtitle ); i++)
+            {
+                subtitle = hb_list_item( m->list_subtitle, i );
+                cc_buf = hb_buffer_init( 2 );
+                if( cc_buf )
+                {
+                    cc_buf->start = m->last_pts;
+                    memcpy( cc_buf->data, cc_block+1, 2 );
+                    hb_fifo_push( subtitle->fifo_in, cc_buf );
+                }
+            }
+            break;
+        case 1:
+            // CC2 stream
+            //process608( cc_block+1, 2, &m->cc608 );
+            break;
+        case 2: //EIA-708
+            // DTVCC packet data
+            // Fall through
+        case 3: //EIA-708
+        {
+            uint8_t temp[4];
+            temp[0]=cc_valid;
+            temp[1]=cc_type;
+            temp[2]=cc_block[1];
+            temp[3]=cc_block[2];
+            //do_708 ((const unsigned char *) temp, 4);
+        }
+        break;
+        default:
+            break;
+        } 
+    } 
+    else
+    {
+        hb_log("Ignoring invalid CC block");
+    }
+}
+
 static hb_buffer_t *hb_copy_frame( hb_job_t *job, int width, int height,
                                    uint8_t* y, uint8_t *u, uint8_t *v )
 {
@@ -94,7 +157,7 @@ static hb_buffer_t *hb_copy_frame( hb_job_t *job, int width, int height,
         sws_scale( context, in.data, in.linesize, 0, height, out.data, out.linesize );
         sws_freeContext( context );
 
-        u_int8_t *data = buf->data;
+        uint8_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.
@@ -110,7 +173,7 @@ static hb_buffer_t *hb_copy_frame( hb_job_t *job, int width, int height,
     {
         // 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;
+        uint8_t *data = buf->data;
         memcpy( data, y, dst_wh );
         data += dst_wh;
         // U & V planes are 1/4 the size of Y plane.
@@ -128,7 +191,7 @@ static hb_buffer_t *hb_copy_frame( hb_job_t *job, int width, int height,
  *
  *********************************************************************/
 static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
-                        hb_list_t * list_raw )
+                               hb_list_t * list_raw )
 {
     mpeg2_state_t   state;
     hb_buffer_t   * buf;
@@ -236,7 +299,7 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
                                                          buf->new_chap - 1 );
                         chap_name = c->title;
                     }
-                    hb_log( "mpeg2: \"%s\" (%d) at frame %u time %lld",
+                    hb_log( "mpeg2: \"%s\" (%d) at frame %u time %"PRId64,
                             chap_name, buf->new_chap, m->nframes, buf->start );
                 } else if ( m->nframes == 0 && m->job &&
                             hb_list_item( m->job->title->list_chapter,
@@ -244,28 +307,28 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
                 {
                     hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
                                                      m->job->chapter_start - 1 );
-                    hb_log( "mpeg2: \"%s\" (%d) at frame %u time %lld", c->title,
+                    hb_log( "mpeg2: \"%s\" (%d) at frame %u time %"PRId64, c->title,
                             m->job->chapter_start, m->nframes, buf->start );
                 }
                 ++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);
 */
@@ -273,66 +336,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 */
@@ -345,6 +408,131 @@ static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
         {
             mpeg2_reset( m->libmpeg2, 0 );
         }
+
+        /*
+         * Look for Closed Captions if scanning (!job) or if closed captions have been requested.
+         *
+         * Send them on to the closed caption decoder if requested and found.
+         */
+        if( ( !m->job || hb_list_count( m->list_subtitle) ) &&
+            ( m->info->user_data_len != 0 &&
+              m->info->user_data[0] == 0x43 &&
+              m->info->user_data[1] == 0x43 ) ) 
+        {
+            int i, j;
+            const uint8_t *header = &m->info->user_data[4];
+            uint8_t pattern=header[0] & 0x80;
+            int field1packet = 0; /* expect Field 1 first */
+            if (pattern==0x00) 
+                field1packet=1; /* expect Field 1 second */
+            int capcount=(header[0] & 0x1e) / 2;
+            header++;
+            
+            /*
+             * Add closed captions to the title if we are scanning (no job).
+             *
+             * Just because we don't add this doesn't mean that there aren't any when 
+             * we scan, just that noone said anything. So you should be able to add
+             * closed captions some other way (See decmpeg2Init() for alternative
+             * approach of assuming that there are always CC, which is probably
+             * safer - however I want to leave the autodetect in here for now to
+             * see how it goes).
+             */
+            if( !m->job && m->title )
+            {
+                hb_subtitle_t * subtitle;
+                int found = 0;
+                int i;
+                
+                for( i = 0; i < hb_list_count( m->title->list_subtitle ); i++ )
+                {
+                    subtitle = hb_list_item( m->title->list_subtitle, i);
+                    /*
+                     * Let's call them 608 subs for now even if they aren't, since they 
+                     * are the only types we grok.
+                     */
+                    if( subtitle && subtitle->source == CC608SUB ) 
+                    {
+                        found = 1;
+                        break;
+                    }
+                }
+                
+                if( !found )
+                {
+                    subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
+                    subtitle->track = 0;
+                    subtitle->id = 0x0;
+                    snprintf( subtitle->lang, sizeof( subtitle->lang ), "Closed Captions");
+                    /*
+                     * The language of the subtitles will be the same as the first audio
+                     * track, i.e. the same as the video.
+                     */
+                    hb_audio_t *audio = hb_list_item( m->title->list_audio, 0 );
+                    if( audio )
+                    {
+                        snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), 
+                                  audio->config.lang.iso639_2);
+                    } else {
+                        snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "und");
+                    }
+                    subtitle->format = TEXTSUB;
+                    subtitle->source = CC608SUB;
+                    subtitle->config.dest   = PASSTHRUSUB;
+                    subtitle->type = 5; 
+                    
+                    hb_list_add( m->title->list_subtitle, subtitle );
+                }
+            }
+
+            for( i=0; i<capcount; i++ )
+            {
+                for( j=0; j<2; j++ )
+                {
+                    uint8_t data[3];
+                    data[0] = header[0];
+                    data[1] = header[1];
+                    data[2] = header[2];
+                    header += 3;
+                    /* Field 1 and 2 data can be in either order,
+                       with marker bytes of \xff and \xfe
+                       Since markers can be repeated, use pattern as well */
+                    if( data[0] == 0xff && j == field1packet )
+                    {
+                        data[0] = 0x04; // Field 1
+                    }   
+                    else
+                    {
+                        data[0] = 0x05; // Field 2
+                    }
+                    hb_mpeg2_cc( m, data );
+                }
+            }
+            // Deal with extra closed captions some DVD have.
+            while( header[0]==0xfe || header[0]==0xff )
+            {
+                for( j=0; j<2; j++ )
+                {
+                    uint8_t data[3];
+                    data[0] = header[0];
+                    data[1] = header[1];
+                    data[2] = header[2];
+                    header += 3;
+                    /* Field 1 and 2 data can be in either order,
+                       with marker bytes of \xff and \xfe
+                       Since markers can be repeated, use pattern as well */
+                    if( data[0] == 0xff && j == field1packet )
+                    {
+                        data[0] = 0x04; // Field 1
+                    }   
+                    else
+                    {
+                        data[0] = 0x05; // Field 2
+                    } 
+                    hb_mpeg2_cc( m, data );
+                }
+            }   
+        }
     }
     return 1;
 }
@@ -392,6 +580,31 @@ static int decmpeg2Init( hb_work_object_t * w, hb_job_t * job )
 
     pv->libmpeg2->job = job;
 
+    if( job && job->title ) {
+        pv->libmpeg2->title = job->title;
+    }
+
+    /*
+     * If not scanning, then are we supposed to extract Closed Captions
+     * and send them to the decoder? 
+     */
+    pv->libmpeg2->list_subtitle = hb_list_init();
+    if( job )
+    {
+        hb_subtitle_t * subtitle;
+        int i;
+        
+        for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
+        {
+            subtitle = hb_list_item( job->list_subtitle, i);
+            if( subtitle && subtitle->source == CC608SUB ) 
+            {
+                hb_list_add(pv->libmpeg2->list_subtitle, subtitle);
+            }
+        }
+
+    }
+
     return 0;
 }
 
@@ -401,17 +614,25 @@ static int decmpeg2Init( hb_work_object_t * w, hb_job_t * job )
  *
  *********************************************************************/
 static int decmpeg2Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
-                   hb_buffer_t ** buf_out )
+                         hb_buffer_t ** buf_out )
 {
     hb_work_private_t * pv = w->private_data;
     hb_buffer_t * buf, * last = NULL;
     int status = HB_WORK_OK;
 
-    // The reader found a chapter break, consume it completely, and remove it from the
-    // stream. We need to shift it.
+    if( w->title && pv && pv->libmpeg2 && !pv->libmpeg2->title ) {
+        pv->libmpeg2->title = w->title;
+    }
+
+    // The reader found a chapter break. Remove it from the input 
+    // stream. If we're reading (as opposed to scanning) start looking
+    // for the next GOP start since that's where the chapter begins.
     if( (*buf_in)->new_chap )
     {
-        pv->libmpeg2->look_for_break = (*buf_in)->new_chap;
+        if ( pv->libmpeg2->job )
+        {
+            pv->libmpeg2->look_for_break = (*buf_in)->new_chap;
+        }
         (*buf_in)->new_chap = 0;
     }
 
@@ -420,9 +641,25 @@ static int decmpeg2Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
     /* if we got an empty buffer signaling end-of-stream send it downstream */
     if ( (*buf_in)->size == 0 )
     {
+        int i;
+
         hb_list_add( pv->list, *buf_in );
         *buf_in = NULL;
         status = HB_WORK_DONE;
+        /*
+         * Let the Closed Captions know that it is the end of the data.
+         */
+        for (i = 0; i < hb_list_count( pv->libmpeg2->list_subtitle ); i++)
+        {
+            hb_subtitle_t * subtitle;
+            hb_buffer_t *buf_eof = hb_buffer_init( 0 );
+            
+            subtitle = hb_list_item( pv->libmpeg2->list_subtitle, i );
+            if( buf_eof )
+            {
+                hb_fifo_push( subtitle->fifo_in, buf_eof );
+            }
+        }
     }
 
     *buf_out = NULL;
@@ -459,6 +696,7 @@ static void decmpeg2Close( hb_work_object_t * w )
         hb_log( "mpeg2 done: %d frames", pv->libmpeg2->nframes );
     }
     hb_list_close( &pv->list );
+    hb_list_close( &pv->libmpeg2->list_subtitle );
     hb_libmpeg2_close( &pv->libmpeg2 );
     free( pv );
 }