OSDN Git Service

cli: add vbv settings to appletv preset
[handbrake-jp/handbrake-jp-git.git] / libhb / decavcodec.c
index dae2544..19bd56c 100644 (file)
@@ -110,6 +110,10 @@ struct hb_work_private_s
     struct SwsContext *sws_context; // if we have to rescale or convert color space
 };
 
+static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size );
+static hb_buffer_t *link_buf_list( hb_work_private_t *pv );
+
+
 static int64_t heap_pop( pts_heap_t *heap )
 {
     int64_t result;
@@ -183,6 +187,7 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
     w->private_data = pv;
 
     pv->job   = job;
+    pv->list  = hb_list_init();
 
     int codec_id = w->codec_param;
     /*XXX*/
@@ -233,7 +238,7 @@ static void decavcodecClose( hb_work_object_t * w )
         }
         if ( pv->buffer )
         {
-            free( pv->buffer );
+            av_free( pv->buffer );
             pv->buffer = NULL;
         }
         free( pv );
@@ -250,17 +255,12 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                     hb_buffer_t ** buf_out )
 {
     hb_work_private_t * pv = w->private_data;
-    hb_buffer_t * in = *buf_in, * buf, * last = NULL;
-    int   pos, len, out_size, i, uncompressed_len;
-    short* bufaligned;
-    uint64_t cur;
-    unsigned char *parser_output_buffer;
-    int parser_output_buffer_len;
+    hb_buffer_t * in = *buf_in;
 
-    if ( (*buf_in)->size <= 0 )
+    if ( in->size <= 0 )
     {
         /* EOF on input stream - send it downstream & say that we're done */
-        *buf_out = *buf_in;
+        *buf_out = in;
         *buf_in = NULL;
         return HB_WORK_DONE;
     }
@@ -273,73 +273,40 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         return HB_WORK_OK;
     }
 
-    cur = ( in->start < 0 )? pv->pts_next : in->start;
+    // if the packet has a timestamp use it 
+    if ( in->start != -1 )
+    {
+        pv->pts_next = in->start;
+    }
 
-    bufaligned = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
-    pos = 0;
-    while( pos < in->size )
+    int pos, len;
+    for ( pos = 0; pos < in->size; pos += len )
     {
-        len = av_parser_parse( pv->parser, pv->context,
-                               &parser_output_buffer, &parser_output_buffer_len,
-                               in->data + pos, in->size - pos, cur, cur );
-        out_size = 0;
-        uncompressed_len = 0;
+        uint8_t *parser_output_buffer;
+        int parser_output_buffer_len;
+        int64_t cur = pv->pts_next;
+
+        len = av_parser_parse2( pv->parser, pv->context,
+                                &parser_output_buffer, &parser_output_buffer_len,
+                                in->data + pos, in->size - pos, cur, cur, AV_NOPTS_VALUE );
         if (parser_output_buffer_len)
         {
-            out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
-            uncompressed_len = avcodec_decode_audio2( pv->context, bufaligned,
-                                                      &out_size,
-                                                      parser_output_buffer,
-                                                      parser_output_buffer_len );
-        }
-        if( out_size )
-        {
-            short * s16;
-            float * fl32;
-
-            buf = hb_buffer_init( 2 * out_size );
-
-            int sample_size_in_bytes = 2;   // Default to 2 bytes
-            switch (pv->context->sample_fmt)
-            {
-              case SAMPLE_FMT_S16:
-                sample_size_in_bytes = 2;
-                break;
-              /* We should handle other formats here - but that needs additional format conversion work below */
-              /* For now we'll just report the error and try to carry on */
-              default:
-                hb_log("decavcodecWork - Unknown Sample Format from avcodec_decode_audio (%d) !", pv->context->sample_fmt);
-                break;
-            }
-
-            buf->start = cur;
-            buf->stop  = cur + 90000 * ( out_size / (sample_size_in_bytes * pv->context->channels) ) /
-                         pv->context->sample_rate;
-            cur = buf->stop;
-
-            s16  = bufaligned;
-            fl32 = (float *) buf->data;
-            for( i = 0; i < out_size / 2; i++ )
-            {
-                fl32[i] = s16[i];
-            }
-
-            if( last )
-            {
-                last = last->next = buf;
-            }
-            else
+            // set the duration on every frame since the stream format can
+            // change (it shouldn't but there's no way to guarantee it).
+            // duration is a scaling factor to go from #bytes in the decoded
+            // frame to frame time (in 90KHz mpeg ticks). 'channels' converts
+            // total samples to per-channel samples. 'sample_rate' converts
+            // per-channel samples to seconds per sample and the 90000
+            // is mpeg ticks per second.
+            if ( pv->context->sample_rate && pv->context->channels )
             {
-                *buf_out = last = buf;
+                pv->duration = 90000. /
+                            (double)( pv->context->sample_rate * pv->context->channels );
             }
+            decodeAudio( pv, parser_output_buffer, parser_output_buffer_len );
         }
-
-        pos += len;
     }
-
-    pv->pts_next = cur;
-
-    free( bufaligned );
+    *buf_out = link_buf_list( pv );
     return HB_WORK_OK;
 }
 
@@ -410,14 +377,18 @@ static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
 
     while ( pos < buf->size )
     {
-        int len = av_parser_parse( parser, context, &pbuffer, &pbuffer_size,
-                                   buf->data + pos, buf->size - pos,
-                                   buf->start, buf->start );
+        int len = av_parser_parse2( parser, context, &pbuffer, &pbuffer_size,
+                                    buf->data + pos, buf->size - pos,
+                                    buf->start, buf->start, AV_NOPTS_VALUE );
         pos += len;
         if ( pbuffer_size > 0 )
         {
-            len = avcodec_decode_audio2( context, (int16_t*)buffer, &out_size,
-                                         pbuffer, pbuffer_size );
+            AVPacket avp;
+            av_init_packet( &avp );
+            avp.data = pbuffer;
+            avp.size = pbuffer_size;
+
+            len = avcodec_decode_audio3( context, (int16_t*)buffer, &out_size, &avp );
             if ( len > 0 && context->sample_rate > 0 )
             {
                 info->bitrate = context->bit_rate;
@@ -429,7 +400,7 @@ static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
             }
         }
     }
-    free( buffer );
+    av_free( buffer );
     av_parser_close( parser );
     hb_avcodec_close( context );
     return ret;
@@ -519,12 +490,12 @@ static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
     hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
     if ( c && c->title )
     {
-        hb_log( "%s: \"%s\" (%d) at frame %u time %lld",
+        hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64,
                 pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
     }
     else
     {
-        hb_log( "%s: Chapter %d at frame %u time %lld",
+        hb_log( "%s: Chapter %d at frame %u time %"PRId64,
                 pv->context->codec->name, chap_num, pv->nframes, pts );
     }
 }
@@ -548,13 +519,18 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
 {
     int got_picture, oldlevel = 0;
     AVFrame frame;
+    AVPacket avp;
 
     if ( global_verbosity_level <= 1 )
     {
         oldlevel = av_log_get_level();
         av_log_set_level( AV_LOG_QUIET );
     }
-    if ( avcodec_decode_video( pv->context, &frame, &got_picture, data, size ) < 0 )
+
+    av_init_packet( &avp );
+    avp.data = data;
+    avp.size = size;
+    if ( avcodec_decode_video2( pv->context, &frame, &got_picture, &avp ) < 0 )
     {
         ++pv->decode_errors;     
     }
@@ -681,8 +657,8 @@ static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
     do {
         uint8_t *pout;
         int pout_len;
-        int len = av_parser_parse( pv->parser, pv->context, &pout, &pout_len,
-                                   data + pos, size - pos, pts, dts );
+        int len = av_parser_parse2( pv->parser, pv->context, &pout, &pout_len,
+                                    data + pos, size - pos, pts, dts, AV_NOPTS_VALUE );
         pos += len;
 
         if ( pout_len > 0 )
@@ -908,13 +884,22 @@ static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
             info->rate_base *= context->ticks_per_frame;
         }
         
-        /* Sometimes there's no pixel aspect set in the source. In that case,
-           assume a 1:1 PAR. Otherwise, preserve the source PAR.             */
-        info->pixel_aspect_width = context->sample_aspect_ratio.num ?
-                                        context->sample_aspect_ratio.num : 1;
-        info->pixel_aspect_height = context->sample_aspect_ratio.den ?
-                                        context->sample_aspect_ratio.den : 1;
-
+        info->pixel_aspect_width = context->sample_aspect_ratio.num;
+        info->pixel_aspect_height = context->sample_aspect_ratio.den;
+
+        /* Sometimes there's no pixel aspect set in the source ffmpeg context
+         * which appears to come from the video stream. In that case,
+         * try the pixel aspect in AVStream (which appears to come from
+         * the container). Else assume a 1:1 PAR. */
+        if ( info->pixel_aspect_width == 0 ||
+             info->pixel_aspect_height == 0 )
+        {
+            AVStream *st = hb_ffmpeg_avstream( w->codec_param );
+            info->pixel_aspect_width = st->sample_aspect_ratio.num ?
+                                        st->sample_aspect_ratio.num : 1;
+            info->pixel_aspect_height = st->sample_aspect_ratio.den ?
+                                        st->sample_aspect_ratio.den : 1;
+        }
         /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
          * Display Aspect Ratio so we convert by scaling by the Storage
          * Aspect Ratio (w/h). We do the calc in floating point to get the
@@ -1059,10 +1044,6 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                              hb_buffer_t ** buf_out )
 {
     hb_work_private_t *pv = w->private_data;
-    if ( ! pv->context )
-    {
-        init_ffmpeg_context( w );
-    }
     hb_buffer_t *in = *buf_in;
     *buf_in = NULL;
 
@@ -1070,7 +1051,7 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     if ( in->size == 0 )
     {
         /* flush any frames left in the decoder */
-        while ( decodeFrame( pv, NULL, 0 ) )
+        while ( pv->context && decodeFrame( pv, NULL, 0 ) )
         {
         }
         flushDelayQueue( pv );
@@ -1079,6 +1060,11 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         return HB_WORK_DONE;
     }
 
+    if ( ! pv->context )
+    {
+        init_ffmpeg_context( w );
+    }
+
     int64_t pts = in->start;
     if( pts >= 0 )
     {
@@ -1133,9 +1119,14 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
             pv->buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
             buffer = pv->buffer;
         }
+
+        AVPacket avp;
+        av_init_packet( &avp );
+        avp.data = data + pos;
+        avp.size = size - pos;
+
         int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
-        int len = avcodec_decode_audio2( context, buffer, &out_size,
-                                         data + pos, size - pos );
+        int len = avcodec_decode_audio3( context, buffer, &out_size, &avp );
         if ( len <= 0 )
         {
             return;
@@ -1160,7 +1151,7 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
                                                               NULL, 0 );
                 // get output buffer size (in 2-byte samples) then malloc a buffer
                 out_size = ( out_size * 2 ) / isamp;
-                buffer = malloc( out_size );
+                buffer = av_malloc( out_size );
 
                 // we're doing straight sample format conversion which behaves as if
                 // there were only one channel.
@@ -1194,7 +1185,7 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
             // if we allocated a buffer for sample format conversion, free it
             if ( buffer != pv->buffer )
             {
-                free( buffer );
+                av_free( buffer );
             }
         }
     }