OSDN Git Service

x264 bump to r1339-82b80ef
[handbrake-jp/handbrake-jp-git.git] / libhb / decavcodec.c
index 2870991..9d1474b 100644 (file)
@@ -61,9 +61,7 @@
 
 #include "hb.h"
 #include "hbffmpeg.h"
-
-//#include "libavcodec/audioconvert.h"
-#include "../contrib/ffmpeg/libavcodec/audioconvert.h"
+#include "libavcodec/audioconvert.h"
 
 static int  decavcodecInit( hb_work_object_t *, hb_job_t * );
 static int  decavcodecWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
@@ -235,7 +233,7 @@ static void decavcodecClose( hb_work_object_t * w )
         }
         if ( pv->buffer )
         {
-            free( pv->buffer );
+            av_free( pv->buffer );
             pv->buffer = NULL;
         }
         free( pv );
@@ -254,7 +252,7 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     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 buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
+    short* bufaligned;
     uint64_t cur;
     unsigned char *parser_output_buffer;
     int parser_output_buffer_len;
@@ -277,21 +275,24 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
 
     cur = ( in->start < 0 )? pv->pts_next : in->start;
 
+    bufaligned = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
     pos = 0;
     while( pos < in->size )
     {
-        len = av_parser_parse( pv->parser, pv->context,
-                               &parser_output_buffer, &parser_output_buffer_len,
-                               in->data + pos, in->size - pos, cur, cur );
+        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 );
         out_size = 0;
         uncompressed_len = 0;
         if (parser_output_buffer_len)
         {
-            out_size = sizeof(buffer);
-            uncompressed_len = avcodec_decode_audio2( pv->context, buffer,
-                                                      &out_size,
-                                                      parser_output_buffer,
-                                                      parser_output_buffer_len );
+            AVPacket avp;
+            av_init_packet( &avp );
+            avp.data = parser_output_buffer;
+            avp.size = parser_output_buffer_len;
+
+            out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
+            uncompressed_len = avcodec_decode_audio3( pv->context, bufaligned, &out_size, &avp );
         }
         if( out_size )
         {
@@ -318,7 +319,7 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                          pv->context->sample_rate;
             cur = buf->stop;
 
-            s16  = buffer;
+            s16  = bufaligned;
             fl32 = (float *) buf->data;
             for( i = 0; i < out_size / 2; i++ )
             {
@@ -340,6 +341,7 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
 
     pv->pts_next = cur;
 
+    av_free( bufaligned );
     return HB_WORK_OK;
 }
 
@@ -403,25 +405,25 @@ static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
     AVCodecParserContext *parser = av_parser_init( codec->id );
     AVCodecContext *context = avcodec_alloc_context();
     hb_avcodec_open( context, codec );
-#ifdef SYS_CYGWIN
-    uint8_t *buffer = memalign(16, AVCODEC_MAX_AUDIO_FRAME_SIZE);
-#else
-    uint8_t *buffer = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
-#endif
+    uint8_t *buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
     int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
     unsigned char *pbuffer;
     int pos = 0, pbuffer_size;
 
     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;
@@ -433,7 +435,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;
@@ -523,12 +525,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 );
     }
 }
@@ -550,13 +552,27 @@ static void flushDelayQueue( hb_work_private_t *pv )
 
 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
 {
-    int got_picture;
+    int got_picture, oldlevel = 0;
     AVFrame frame;
+    AVPacket avp;
 
-    if ( avcodec_decode_video( pv->context, &frame, &got_picture, data, size ) < 0 )
+    if ( global_verbosity_level <= 1 )
+    {
+        oldlevel = av_log_get_level();
+        av_log_set_level( AV_LOG_QUIET );
+    }
+
+    av_init_packet( &avp );
+    avp.data = data;
+    avp.size = size;
+    if ( avcodec_decode_video2( pv->context, &frame, &got_picture, &avp ) < 0 )
     {
         ++pv->decode_errors;     
     }
+    if ( global_verbosity_level <= 1 )
+    {
+        av_log_set_level( oldlevel );
+    }
     if( got_picture )
     {
         // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
@@ -582,6 +598,14 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
         {
             frame_dur += frame.repeat_pict * frame_dur * 0.5;
         }
+        // XXX Unlike every other video decoder, the Raw decoder doesn't
+        //     use the standard buffer allocation routines so we never
+        //     get to put a PTS in the frame. Do it now.
+        if ( pv->context->codec_id == CODEC_ID_RAWVIDEO )
+        {
+            frame.pts = pv->pts;
+            pv->pts = -1;
+        }
         // If there was no pts for this frame, assume constant frame rate
         // video & estimate the next frame time from the last & duration.
         double pts = frame.pts;
@@ -668,8 +692,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 )
@@ -888,14 +912,29 @@ static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
         info->rate = 27000000;
         info->rate_base = (int64_t)context->time_base.num * 27000000LL /
                           context->time_base.den;
+        if ( context->ticks_per_frame > 1 )
+        {
+            // for ffmpeg 0.5 & later, the H.264 & MPEG-2 time base is
+            // field rate rather than frame rate so convert back to frames.
+            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
@@ -1111,26 +1150,17 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
         int16_t *buffer = pv->buffer;
         if ( buffer == NULL )
         {
-            // XXX ffmpeg bug workaround
-            // malloc a buffer for the audio decode. On an x86, ffmpeg
-            // uses mmx/sse instructions on this buffer without checking
-            // that it's 16 byte aligned and this will cause an abort if
-            // the buffer is allocated on our stack. Rather than doing
-            // complicated, machine dependent alignment here we use the
-            // fact that malloc returns an aligned pointer on most architectures.
-
-            #ifdef SYS_CYGWIN
-                // Cygwin's malloc doesn't appear to return 16-byte aligned memory so use memalign instead.
-               pv->buffer = memalign(16, AVCODEC_MAX_AUDIO_FRAME_SIZE);
-            #else
-                pv->buffer = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
-            #endif
-
+            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;
@@ -1155,7 +1185,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.
@@ -1189,7 +1219,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 );
             }
         }
     }