OSDN Git Service

Repeat after me, eddyg is a wally.
[handbrake-jp/handbrake-jp-git.git] / libhb / deca52.c
index b18acf6..94bf8a5 100644 (file)
@@ -25,9 +25,16 @@ struct hb_work_private_s
     int           sync;
     int           size;
 
+    int64_t       next_expected_pts;
+
+    int64_t       sequence;
+
     uint8_t       frame[3840];
 
     hb_list_t   * list;
+       
+       int           out_discrete_channels;
+       
 };
 
 int  deca52Init( hb_work_object_t *, hb_job_t * );
@@ -62,9 +69,20 @@ int deca52Init( hb_work_object_t * w, hb_job_t * job )
 
     pv->list      = hb_list_init();
     pv->state     = a52_init( 0 );
-    pv->flags_out = A52_STEREO;
-    pv->level     = 32768.0;
 
+       /* Decide what format we want out of a52dec
+       work.c has already done some of this deduction for us in do_job() */
+       
+       pv->flags_out = HB_AMIXDOWN_GET_A52_FORMAT(w->amixdown);
+
+       /* pass the number of channels used into the private work data */
+       /* will only be actually used if we're not doing AC3 passthru */
+       pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(w->amixdown);
+
+    pv->level     = 32768.0;
+    pv->next_expected_pts = 0;
+    pv->sequence = 0;
+    
     return 0;
 }
 
@@ -77,6 +95,9 @@ void deca52Close( hb_work_object_t * w )
 {
     hb_work_private_t * pv = w->private_data;
     a52_free( pv->state );
+    hb_list_empty( &pv->list );
+    free( pv );
+    w->private_data = NULL;
 }
 
 /***********************************************************************
@@ -91,13 +112,19 @@ int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
     hb_work_private_t * pv = w->private_data;
     hb_buffer_t * buf;
 
+    if( buf_in && *buf_in )
+    {
+        pv->sequence = (*buf_in)->sequence;
+    }
+
     hb_list_add( pv->list, *buf_in );
     *buf_in = NULL;
 
     /* If we got more than a frame, chain raw buffers */
     *buf_out = buf = Decode( w );
     while( buf )
-    {
+    { 
+        buf->sequence = pv->sequence;
         buf->next = Decode( w );
         buf       = buf->next;
     }
@@ -114,9 +141,8 @@ static hb_buffer_t * Decode( hb_work_object_t * w )
 {
     hb_work_private_t * pv = w->private_data;
     hb_buffer_t * buf;
-    int           i, j;
-    uint64_t      pts;
-    int           pos;
+    int           i, j, k;
+    uint64_t      pts, pos;
 
     /* Get a frame header if don't have one yet */
     if( !pv->sync )
@@ -175,11 +201,22 @@ static hb_buffer_t * Decode( hb_work_object_t * w )
     /* Feed liba52 */
     a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
 
-    /* 6 blocks per frame, 256 samples per block, 2 channels */
-    buf        = hb_buffer_init( 3072 * sizeof( float ) );
+    /* 6 blocks per frame, 256 samples per block, channelsused channels */
+    buf        = hb_buffer_init( 6 * 256 * pv->out_discrete_channels * sizeof( float ) );
+    if (pts == -1)
+    {
+      pts = pv->next_expected_pts;
+    }
     buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
     buf->stop  = buf->start + 6 * 256 * 90000 / pv->rate;
 
+    /*
+       * To track AC3 PTS add this back in again.
+        *hb_log("AC3: pts is %lld, buf->start %lld buf->stop %lld", pts, buf->start, buf->stop); 
+        */
+    
+    pv->next_expected_pts = buf->stop;
+    
     for( i = 0; i < 6; i++ )
     {
         sample_t * samples_in;
@@ -187,14 +224,17 @@ static hb_buffer_t * Decode( hb_work_object_t * w )
 
         a52_block( pv->state );
         samples_in  = a52_samples( pv->state );
-        samples_out = ((float *) buf->data) + 512 * i;
+        samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
 
         /* Interleave */
         for( j = 0; j < 256; j++ )
         {
-            samples_out[2*j]   = samples_in[j];
-            samples_out[2*j+1] = samples_in[256+j];
+                       for ( k = 0; k < pv->out_discrete_channels; k++ )
+                       {
+                               samples_out[(pv->out_discrete_channels*j)+k]   = samples_in[(256*k)+j];
+                       }
         }
+
     }
 
     pv->sync = 0;