OSDN Git Service

This patch adds mingw32 cross-compilation support to HandBrake trunk to
[handbrake-jp/handbrake-jp-git.git] / libhb / decavcodec.c
index 04b12f6..8b09afc 100644 (file)
@@ -4,10 +4,64 @@
    Homepage: <http://handbrake.fr/>.
    It may be used under the terms of the GNU General Public License. */
 
-#include "hb.h"
+/* This module is Handbrake's interface to the ffmpeg decoder library
+   (libavcodec & small parts of libavformat). It contains four Handbrake
+   "work objects":
+
+    decavcodec  connects HB to an ffmpeg audio decoder
+    decavcodecv connects HB to an ffmpeg video decoder
+
+        (Two different routines are needed because the ffmpeg library
+        has different decoder calling conventions for audio & video.
+        The audio decoder should have had its name changed to "decavcodeca"
+        but I got lazy.) These work objects are self-contained & follow all
+        of HB's conventions for a decoder module. They can be used like
+        any other HB decoder (deca52, decmpeg2, etc.).
+
+    decavcodecai "internal" (incestuous?) version of decavcodec
+    decavcodecvi "internal" (incestuous?) version of decavcodecv
+
+        These routine are functionally equivalent to the routines above but
+        can only be used by the ffmpeg-based stream reader in libhb/stream.c.
+        The reason they exist is because the ffmpeg library leaves some of
+        the information needed by the decoder in the AVStream (the data
+        structure used by the stream reader) and we need to retrieve it
+        to successfully decode frames. But in HB the reader and decoder
+        modules are in completely separate threads and nothing goes between
+        them but hb_buffers containing frames to be decoded. I.e., there's
+        no easy way for the ffmpeg stream reader to pass a pointer to its
+        AVStream over to the ffmpeg video or audio decoder. So the *i work
+        objects use a private back door to the stream reader to get access
+        to the AVStream (routines hb_ffmpeg_avstream and hb_ffmpeg_context)
+        and the codec_param passed to these work objects is the key to this
+        back door (it's basically an index that allows the correct AVStream
+        to be retrieved).
+
+    The normal & *i objects share a lot of code (the basic frame decoding
+    and bitstream info code is factored out into subroutines that can be
+    called by either) but the top level routines of the *i objects
+    (decavcodecviWork, decavcodecviInfo, etc.) are different because:
+     1) they *have* to use the AVCodecContext that's contained in the
+        reader's AVStream rather than just allocating & using their own,
+     2) the Info routines have access to stuff kept in the AVStream in addition
+        to stuff kept in the AVCodecContext. This shouldn't be necessary but
+        crucial information like video frame rate that should be in the
+        AVCodecContext is either missing or wrong in the version of ffmpeg
+        we're currently using.
+
+    A consequence of the above is that the non-i work objects *can't* use
+    information from the AVStream because there isn't one - they get their
+    data from either the dvd reader or the mpeg reader, not the ffmpeg stream
+    reader. That means that they have to make up for deficiencies in the
+    AVCodecContext info by using stuff kept in the HB "title" struct. It
+    also means that ffmpeg codecs that randomly scatter state needed by
+    the decoder across both the AVCodecContext & the AVStream (e.g., the
+    VC1 decoder) can't easily be used by the HB mpeg stream reader.
+ */
 
-#include "libavcodec/avcodec.h"
-#include "libavformat/avformat.h"
+#include "hb.h"
+#include "hbffmpeg.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 ** );
@@ -26,21 +80,94 @@ hb_work_object_t hb_decavcodec =
     decavcodecBSInfo
 };
 
+#define HEAP_SIZE 8
+typedef struct {
+    // there are nheap items on the heap indexed 1..nheap (i.e., top of
+    // heap is 1). The 0th slot is unused - a marker is put there to check
+    // for overwrite errs.
+    int64_t h[HEAP_SIZE+1];
+    int     nheap;
+} pts_heap_t;
+
 struct hb_work_private_s
 {
-    hb_job_t             *job;
-    AVCodecContext       *context;
+    hb_job_t        *job;
+    AVCodecContext  *context;
     AVCodecParserContext *parser;
-    hb_list_t            *list;
-    double               pts_next;  // next pts we expect to generate
-    int64_t              pts;       // (video) pts passing from parser to decoder
-    int64_t              chap_time; // time of next chap mark (if new_chap != 0)
-    int                  new_chap;
-    int                  ignore_pts; // workaround M$ bugs
-    int                  nframes;
-    double               duration;  // frame duration (for video)
+    hb_list_t       *list;
+    double          duration;   // frame duration (for video)
+    double          pts_next;   // next pts we expect to generate
+    int64_t         pts;        // (video) pts passing from parser to decoder
+    int64_t         chap_time;  // time of next chap mark (if new_chap != 0)
+    int             new_chap;   // output chapter mark pending
+    uint32_t        nframes;
+    uint32_t        ndrops;
+    uint32_t        decode_errors;
+    int             brokenByMicrosoft; // video stream may contain packed b-frames
+    hb_buffer_t*    delayq[HEAP_SIZE];
+    pts_heap_t      pts_heap;
+    void*           buffer;
+    struct SwsContext *sws_context; // if we have to rescale or convert color space
 };
 
+static int64_t heap_pop( pts_heap_t *heap )
+{
+    int64_t result;
+
+    if ( heap->nheap <= 0 )
+    {
+        return -1;
+    }
+
+    // return the top of the heap then put the bottom element on top,
+    // decrease the heap size by one & rebalence the heap.
+    result = heap->h[1];
+
+    int64_t v = heap->h[heap->nheap--];
+    int parent = 1;
+    int child = parent << 1;
+    while ( child <= heap->nheap )
+    {
+        // find the smallest of the two children of parent
+        if (child < heap->nheap && heap->h[child] > heap->h[child+1] )
+            ++child;
+
+        if (v <= heap->h[child])
+            // new item is smaller than either child so it's the new parent.
+            break;
+
+        // smallest child is smaller than new item so move it up then
+        // check its children.
+        int64_t hp = heap->h[child];
+        heap->h[parent] = hp;
+        parent = child;
+        child = parent << 1;
+    }
+    heap->h[parent] = v;
+    return result;
+}
+
+static void heap_push( pts_heap_t *heap, int64_t v )
+{
+    if ( heap->nheap < HEAP_SIZE )
+    {
+        ++heap->nheap;
+    }
+
+    // stick the new value on the bottom of the heap then bubble it
+    // up to its correct spot.
+       int child = heap->nheap;
+       while (child > 1) {
+               int parent = child >> 1;
+               if (heap->h[parent] <= v)
+                       break;
+               // move parent down
+               int64_t hp = heap->h[parent];
+               heap->h[child] = hp;
+               child = parent;
+       }
+       heap->h[child] = v;
+}
 
 
 /***********************************************************************
@@ -61,11 +188,12 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
     /*XXX*/
     if ( codec_id == 0 )
         codec_id = CODEC_ID_MP2;
+
     codec = avcodec_find_decoder( codec_id );
     pv->parser = av_parser_init( codec_id );
 
     pv->context = avcodec_alloc_context();
-    avcodec_open( pv->context, codec );
+    hb_avcodec_open( pv->context, codec );
 
     return 0;
 }
@@ -78,17 +206,38 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
 static void decavcodecClose( hb_work_object_t * w )
 {
     hb_work_private_t * pv = w->private_data;
-    if ( pv->parser )
-       {
-               av_parser_close(pv->parser);
-       }
-    if ( pv->context && pv->context->codec )
-    {
-        avcodec_close( pv->context );
-    }
-    if ( pv->list )
+
+    if ( pv )
     {
-        hb_list_close( &pv->list );
+        if ( pv->job && pv->context && pv->context->codec )
+        {
+            hb_log( "%s-decoder done: %u frames, %u decoder errors, %u drops",
+                    pv->context->codec->name, pv->nframes, pv->decode_errors,
+                    pv->ndrops );
+        }
+        if ( pv->sws_context )
+        {
+            sws_freeContext( pv->sws_context );
+        }
+        if ( pv->parser )
+        {
+            av_parser_close(pv->parser);
+        }
+        if ( pv->context && pv->context->codec )
+        {
+            hb_avcodec_close( pv->context );
+        }
+        if ( pv->list )
+        {
+            hb_list_close( &pv->list );
+        }
+        if ( pv->buffer )
+        {
+            free( pv->buffer );
+            pv->buffer = NULL;
+        }
+        free( pv );
+        w->private_data = NULL;
     }
 }
 
@@ -108,8 +257,22 @@ static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     unsigned char *parser_output_buffer;
     int parser_output_buffer_len;
 
+    if ( (*buf_in)->size <= 0 )
+    {
+        /* EOF on input stream - send it downstream & say that we're done */
+        *buf_out = *buf_in;
+        *buf_in = NULL;
+        return HB_WORK_DONE;
+    }
+
     *buf_out = NULL;
 
+    if ( in->start < -1 && pv->pts_next <= 0 )
+    {
+        // discard buffers that start before video time 0
+        return HB_WORK_OK;
+    }
+
     cur = ( in->start < 0 )? pv->pts_next : in->start;
 
     pos = 0;
@@ -197,10 +360,23 @@ static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
     return 0;
 }
 
+static const int chan2layout[] = {
+    HB_INPUT_CH_LAYOUT_MONO,  // We should allow no audio really.
+    HB_INPUT_CH_LAYOUT_MONO,   
+    HB_INPUT_CH_LAYOUT_STEREO,
+    HB_INPUT_CH_LAYOUT_2F1R,   
+    HB_INPUT_CH_LAYOUT_2F2R,
+    HB_INPUT_CH_LAYOUT_3F2R,   
+    HB_INPUT_CH_LAYOUT_4F2R,
+    HB_INPUT_CH_LAYOUT_STEREO, 
+    HB_INPUT_CH_LAYOUT_STEREO,
+};
+
 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
                              hb_work_info_t *info )
 {
     hb_work_private_t *pv = w->private_data;
+    int ret = 0;
 
     memset( info, 0, sizeof(*info) );
 
@@ -213,18 +389,52 @@ static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
     // now we just return dummy values if there's a codec that will handle it.
     AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
                                                            CODEC_ID_MP2 );
-    if ( codec )
+    if ( codec )
     {
-        static char codec_name[64];
+        // there's no ffmpeg codec for this audio type - give up
+        return -1;
+    }
 
-        info->name =  strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
-        info->bitrate = 384000;
-        info->rate = 48000;
-        info->rate_base = 1;
-        info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
-        return 1;
+    static char codec_name[64];
+    info->name =  strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
+
+    AVCodecParserContext *parser = av_parser_init( codec->id );
+    AVCodecContext *context = avcodec_alloc_context();
+    hb_avcodec_open( context, codec );
+#if defined( SYS_CYGWIN )
+    uint8_t *buffer = memalign(16, AVCODEC_MAX_AUDIO_FRAME_SIZE);
+#else
+    uint8_t *buffer = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
+#endif
+    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 );
+        pos += len;
+        if ( pbuffer_size > 0 )
+        {
+            len = avcodec_decode_audio2( context, (int16_t*)buffer, &out_size,
+                                         pbuffer, pbuffer_size );
+            if ( len > 0 && context->sample_rate > 0 )
+            {
+                info->bitrate = context->bit_rate;
+                info->rate = context->sample_rate;
+                info->rate_base = 1;
+                info->channel_layout = chan2layout[context->channels & 7];
+                ret = 1;
+                break;
+            }
+        }
     }
-    return -1;
+    free( buffer );
+    av_parser_close( parser );
+    hb_avcodec_close( context );
+    return ret;
 }
 
 /* -------------------------------------------------------------
@@ -249,18 +459,52 @@ static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride
     return dst;
 }
 
-/* Note: assumes frame format is PIX_FMT_YUV420P */
-static hb_buffer_t *copy_frame( AVCodecContext *context, AVFrame *frame )
+// copy one video frame into an HB buf. If the frame isn't in our color space
+// or at least one of its dimensions is odd, use sws_scale to convert/rescale it.
+// Otherwise just copy the bits.
+static hb_buffer_t *copy_frame( hb_work_private_t *pv, AVFrame *frame )
 {
-    int w = context->width, h = context->height;
-    hb_buffer_t *buf = hb_buffer_init( w * h * 3 / 2 );
+    AVCodecContext *context = pv->context;
+    int w, h;
+    if ( ! pv->job )
+    {
+        // if the dimensions are odd, drop the lsb since h264 requires that
+        // both width and height be even.
+        w = ( context->width >> 1 ) << 1;
+        h = ( context->height >> 1 ) << 1;
+    }
+    else
+    {
+        w =  pv->job->title->width;
+        h =  pv->job->title->height;
+    }
+    hb_buffer_t *buf = hb_video_buffer_init( w, h );
     uint8_t *dst = buf->data;
 
-    dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
-    w >>= 1; h >>= 1;
-    dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
-    dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
+    if ( context->pix_fmt != PIX_FMT_YUV420P || w != context->width ||
+         h != context->height )
+    {
+        // have to convert to our internal color space and/or rescale
+        AVPicture dstpic;
+        avpicture_fill( &dstpic, dst, PIX_FMT_YUV420P, w, h );
 
+        if ( ! pv->sws_context )
+        {
+            pv->sws_context = sws_getContext( context->width, context->height, context->pix_fmt,
+                                              w, h, PIX_FMT_YUV420P,
+                                              SWS_LANCZOS|SWS_ACCURATE_RND,
+                                              NULL, NULL, NULL );
+        }
+        sws_scale( pv->sws_context, frame->data, frame->linesize, 0, h,
+                   dstpic.data, dstpic.linesize );
+    }
+    else
+    {
+        dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
+        w = (w + 1) >> 1; h = (h + 1) >> 1;
+        dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
+        dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
+    }
     return buf;
 }
 
@@ -269,23 +513,57 @@ static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
     hb_work_private_t *pv = context->opaque;
     frame->pts = pv->pts;
     pv->pts = -1;
-
     return avcodec_default_get_buffer( context, frame );
 }
 
 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 );
-    hb_log( "%s: \"%s\" (%d) at frame %u time %lld", pv->context->codec->name,
-            c->title, chap_num, pv->nframes, pts );
+    if ( c && c->title )
+    {
+        hb_log( "%s: \"%s\" (%d) at frame %u time %lld",
+                pv->context->codec->name, c->title, chap_num, pv->nframes, pts );
+    }
+    else
+    {
+        hb_log( "%s: Chapter %d at frame %u time %lld",
+                pv->context->codec->name, chap_num, pv->nframes, pts );
+    }
+}
+
+static void flushDelayQueue( hb_work_private_t *pv )
+{
+    hb_buffer_t *buf;
+    int slot = pv->nframes & (HEAP_SIZE-1);
+
+    // flush all the video packets left on our timestamp-reordering delay q
+    while ( ( buf = pv->delayq[slot] ) != NULL )
+    {
+        buf->start = heap_pop( &pv->pts_heap );
+        hb_list_add( pv->list, buf );
+        pv->delayq[slot] = NULL;
+        slot = ( slot + 1 ) & (HEAP_SIZE-1);
+    }
 }
 
 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
 {
-    int got_picture;
+    int got_picture, oldlevel = 0;
     AVFrame frame;
 
-    avcodec_decode_video( pv->context, &frame, &got_picture, data, size );
+    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 )
+    {
+        ++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
@@ -300,40 +578,87 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
         // worked at this point frame.pts should hold the frame's pts from the
         // original data stream or -1 if it didn't have one. in the latter case
         // we generate the next pts in sequence for it.
+        double frame_dur = pv->duration;
+        if ( frame_dur <= 0 )
+        {
+            frame_dur = 90000. * (double)pv->context->time_base.num /
+                        (double)pv->context->time_base.den;
+            pv->duration = frame_dur;
+        }
+        if ( frame.repeat_pict )
+        {
+            frame_dur += frame.repeat_pict * frame_dur * 0.5;
+        }
+        // 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;
         if ( pts < 0 )
         {
             pts = pv->pts_next;
         }
-        if ( pv->duration == 0 )
-        {
-            pv->duration = 90000. * pv->context->time_base.num /
-                           pv->context->time_base.den;
-        }
-        double frame_dur = pv->duration;
-        frame_dur += frame.repeat_pict * frame_dur * 0.5;
         pv->pts_next = pts + frame_dur;
 
-        hb_buffer_t *buf = copy_frame( pv->context, &frame );
-        buf->start = pts;
+        hb_buffer_t *buf;
 
-        if ( pv->new_chap && buf->start >= pv->chap_time )
+        // if we're doing a scan or this content couldn't have been broken
+        // by Microsoft we don't worry about timestamp reordering
+        if ( ! pv->job || ! pv->brokenByMicrosoft )
         {
-            buf->new_chap = pv->new_chap;
-            pv->new_chap = 0;
-            pv->chap_time = 0;
-            if ( pv->job )
+            buf = copy_frame( pv, &frame );
+            buf->start = pts;
+            hb_list_add( pv->list, buf );
+            ++pv->nframes;
+            return got_picture;
+        }
+
+        // XXX This following probably addresses a libavcodec bug but I don't
+        //     see an easy fix so we workaround it here.
+        //
+        // The M$ 'packed B-frames' atrocity results in decoded frames with
+        // the wrong timestamp. E.g., if there are 2 b-frames the timestamps
+        // we see here will be "2 3 1 5 6 4 ..." instead of "1 2 3 4 5 6".
+        // The frames are actually delivered in the right order but with
+        // the wrong timestamp. To get the correct timestamp attached to
+        // each frame we have a delay queue (longer than the max number of
+        // b-frames) & a sorting heap for the timestamps. As each frame
+        // comes out of the decoder the oldest frame in the queue is removed
+        // and associated with the smallest timestamp. Then the new frame is
+        // added to the queue & its timestamp is pushed on the heap.
+        // This does nothing if the timestamps are correct (i.e., the video
+        // uses a codec that Micro$oft hasn't broken yet) but the frames
+        // get timestamped correctly even when M$ has munged them.
+
+        // remove the oldest picture from the frame queue (if any) &
+        // give it the smallest timestamp from our heap. The queue size
+        // is a power of two so we get the slot of the oldest by masking
+        // the frame count & this will become the slot of the newest
+        // once we've removed & processed the oldest.
+        int slot = pv->nframes & (HEAP_SIZE-1);
+        if ( ( buf = pv->delayq[slot] ) != NULL )
+        {
+            buf->start = heap_pop( &pv->pts_heap );
+
+            if ( pv->new_chap && buf->start >= pv->chap_time )
             {
+                buf->new_chap = pv->new_chap;
+                pv->new_chap = 0;
+                pv->chap_time = 0;
                 log_chapter( pv, buf->new_chap, buf->start );
             }
+            else if ( pv->nframes == 0 )
+            {
+                log_chapter( pv, pv->job->chapter_start, buf->start );
+            }
+            hb_list_add( pv->list, buf );
         }
-        else if ( pv->job && pv->nframes == 0 )
-        {
-            log_chapter( pv, pv->job->chapter_start, buf->start );
-        }
-        hb_list_add( pv->list, buf );
+
+        // add the new frame to the delayq & push its timestamp on the heap
+        pv->delayq[slot] = copy_frame( pv, &frame );
+        heap_push( &pv->pts_heap, pts );
+
         ++pv->nframes;
     }
+
     return got_picture;
 }
 
@@ -362,8 +687,12 @@ static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
     } while ( pos < size );
 
     /* the stuff above flushed the parser, now flush the decoder */
-    while ( size == 0 && decodeFrame( pv, NULL, 0 ) )
+    if ( size <= 0 )
     {
+        while ( decodeFrame( pv, NULL, 0 ) )
+        {
+        }
+        flushDelayQueue( pv );
     }
 }
 
@@ -404,17 +733,91 @@ static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
     pv->context->opaque = pv;
     pv->context->get_buffer = get_frame_buf;
 
-    AVCodec *codec = avcodec_find_decoder( codec_id );
+    return 0;
+}
+
+static int next_hdr( hb_buffer_t *in, int offset )
+{
+    uint8_t *dat = in->data;
+    uint16_t last2 = 0xffff;
+    for ( ; in->size - offset > 1; ++offset )
+    {
+        if ( last2 == 0 && dat[offset] == 0x01 )
+            // found an mpeg start code
+            return offset - 2;
+
+        last2 = ( last2 << 8 ) | dat[offset];
+    }
+
+    return -1;
+}
+
+static int find_hdr( hb_buffer_t *in, int offset, uint8_t hdr_type )
+{
+    if ( in->size - offset < 4 )
+        // not enough room for an mpeg start code
+        return -1;
+
+    for ( ; ( offset = next_hdr( in, offset ) ) >= 0; ++offset )
+    {
+        if ( in->data[offset+3] == hdr_type )
+            // found it
+            break;
+    }
+    return offset;
+}
+
+static int setup_extradata( hb_work_object_t *w, hb_buffer_t *in )
+{
+    hb_work_private_t *pv = w->private_data;
 
     // we can't call the avstream funcs but the read_header func in the
     // AVInputFormat may set up some state in the AVContext. In particular 
     // vc1t_read_header allocates 'extradata' to deal with header issues
     // related to Microsoft's bizarre engineering notions. We alloc a chunk
     // of space to make vc1 work then associate the codec with the context.
-    pv->context->extradata_size = 32;
-    pv->context->extradata = av_malloc(pv->context->extradata_size);
-    avcodec_open( pv->context, codec );
+    if ( w->codec_param != CODEC_ID_VC1 )
+    {
+        // we haven't been inflicted with M$ - allocate a little space as
+        // a marker and return success.
+        pv->context->extradata_size = 16;
+        pv->context->extradata = av_malloc(pv->context->extradata_size);
+        return 0;
+    }
 
+    // find the start and and of the sequence header
+    int shdr, shdr_end;
+    if ( ( shdr = find_hdr( in, 0, 0x0f ) ) < 0 )
+    {
+        // didn't find start of seq hdr
+        return 1;
+    }
+    if ( ( shdr_end = next_hdr( in, shdr + 4 ) ) < 0 )
+    {
+        shdr_end = in->size;
+    }
+    shdr_end -= shdr;
+
+    // find the start and and of the entry point header
+    int ehdr, ehdr_end;
+    if ( ( ehdr = find_hdr( in, 0, 0x0e ) ) < 0 )
+    {
+        // didn't find start of entry point hdr
+        return 1;
+    }
+    if ( ( ehdr_end = next_hdr( in, ehdr + 4 ) ) < 0 )
+    {
+        ehdr_end = in->size;
+    }
+    ehdr_end -= ehdr;
+
+    // found both headers - allocate an extradata big enough to hold both
+    // then copy them into it.
+    pv->context->extradata_size = shdr_end + ehdr_end;
+    pv->context->extradata = av_malloc(pv->context->extradata_size + 8);
+    memcpy( pv->context->extradata, in->data + shdr, shdr_end );
+    memcpy( pv->context->extradata + shdr_end, in->data + ehdr, ehdr_end );
+    memset( pv->context->extradata + shdr_end + ehdr_end, 0, 8);
     return 0;
 }
 
@@ -423,7 +826,7 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
 {
     hb_work_private_t *pv = w->private_data;
     hb_buffer_t *in = *buf_in;
-    int64_t pts = -1;
+    int64_t pts = AV_NOPTS_VALUE;
     int64_t dts = pts;
 
     *buf_in = NULL;
@@ -434,10 +837,30 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         decodeVideo( pv, in->data, in->size, pts, dts );
         hb_list_add( pv->list, in );
         *buf_out = link_buf_list( pv );
-        hb_log( "%s done: %d frames", pv->context->codec->name, pv->nframes );
         return HB_WORK_DONE;
     }
 
+    // if this is the first frame open the codec (we have to wait for the
+    // first frame because of M$ VC1 braindamage).
+    if ( pv->context->extradata_size == 0 )
+    {
+        if ( setup_extradata( w, in ) )
+        {
+            // we didn't find the headers needed to set up extradata.
+            // the codec will abort if we open it so just free the buf
+            // and hope we eventually get the info we need.
+            hb_buffer_close( &in );
+            return HB_WORK_OK;
+        }
+        AVCodec *codec = avcodec_find_decoder( w->codec_param );
+        // There's a mis-feature in ffmpeg that causes the context to be 
+        // incorrectly initialized the 1st time avcodec_open is called.
+        // If you close it and open a 2nd time, it finishes the job.
+        hb_avcodec_open( pv->context, codec );
+        hb_avcodec_close( pv->context );
+        hb_avcodec_open( pv->context, codec );
+    }
+
     if( in->start >= 0 )
     {
         pts = in->start;
@@ -472,21 +895,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;
 
         /* 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
-         * rounding right. We round in the second decimal digit because we
-         * scale the (integer) aspect by 9 to preserve the 1st digit.  */
-        info->aspect = ( (double)context->sample_aspect_ratio.num * 
-                         (double)context->width /
-                         (double)context->sample_aspect_ratio.den /
-                         (double)context->height + 0.05 ) * HB_ASPECT_BASE;
-
-               if( context->sample_aspect_ratio.num == 0 )
-               {
-                       info->aspect = (double)context->width / (double)context->height * HB_ASPECT_BASE;
-               }
+         * rounding right. */
+        info->aspect = (double)info->pixel_aspect_width * 
+                       (double)context->width /
+                       (double)info->pixel_aspect_height /
+                       (double)context->height;
+
         info->profile = context->profile;
         info->level = context->level;
         info->name = context->codec->name;
@@ -535,22 +966,58 @@ static void init_ffmpeg_context( hb_work_object_t *w )
     if ( ! pv->context->codec )
     {
         AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
-        avcodec_open( pv->context, codec );
+        hb_avcodec_open( pv->context, codec );
     }
     // set up our best guess at the frame duration.
-    // the frame rate in the codec seems to be bogus but it's ok in the stream.
+    // the frame rate in the codec is usually bogus but it's sometimes
+    // ok in the stream.
     AVStream *st = hb_ffmpeg_avstream( w->codec_param );
-    AVRational tb = st->time_base;
-    if ( st->r_frame_rate.den && st->r_frame_rate.num )
+
+    if ( st->nb_frames && st->duration )
     {
-        tb.num = st->r_frame_rate.den;
-        tb.den = st->r_frame_rate.num;
+        // compute the average frame duration from the total number
+        // of frames & the total duration.
+        pv->duration = ( (double)st->duration * (double)st->time_base.num ) /
+                       ( (double)st->nb_frames * (double)st->time_base.den );
     }
-    pv->duration = 90000. * tb.num / tb.den;
+    else
+    {
+        // XXX We don't have a frame count or duration so try to use the
+        // far less reliable time base info in the stream.
+        // Because the time bases are so screwed up, we only take values
+        // in the range 8fps - 64fps.
+        AVRational tb;
+        if ( st->time_base.num * 64 > st->time_base.den &&
+             st->time_base.den > st->time_base.num * 8 )
+        {
+            tb = st->time_base;
+        }
+        else if ( st->r_frame_rate.den * 64 > st->r_frame_rate.num &&
+                  st->r_frame_rate.num > st->r_frame_rate.den * 8 )
+        {
+            tb.num = st->r_frame_rate.den;
+            tb.den = st->r_frame_rate.num;
+        }
+        else
+        {
+            tb.num = 1001;  /*XXX*/
+            tb.den = 24000; /*XXX*/
+        }
+        pv->duration =  (double)tb.num / (double)tb.den;
+    }
+    pv->duration *= 90000.;
 
     // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
     pv->context->opaque = pv;
     pv->context->get_buffer = get_frame_buf;
+
+    // avi, mkv and possibly mp4 containers can contain the M$ VFW packed
+    // b-frames abortion that messes up frame ordering and timestamps.
+    // XXX ffmpeg knows which streams are broken but doesn't expose the
+    //     info externally. We should patch ffmpeg to add a flag to the
+    //     codec context for this but until then we mark all ffmpeg streams
+    //     as suspicious.
+    pv->brokenByMicrosoft = 1;
 }
 
 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
@@ -577,7 +1044,8 @@ static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
     w->private_data = pv;
     pv->job   = job;
     pv->list = hb_list_init();
-
+    pv->pts_next = -1;
+    pv->pts = -1;
     return 0;
 }
 
@@ -588,32 +1056,8 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     if ( ! pv->context )
     {
         init_ffmpeg_context( w );
-
-        switch ( pv->context->codec_id )
-        {
-            // These are the only formats whose timestamps we'll believe.
-            // All others are treated as CFR (i.e., we take the first timestamp
-            // then generate all the others from the frame rate). The reason for
-            // this is that the M$ encoders are so frigging buggy with garbage
-            // like packed b-frames (vfw divx mpeg4) that believing their timestamps
-            // results in discarding more than half the video frames because they'll
-            // be out of sequence (and attempting to reseqence them doesn't work
-            // because it's the timestamps that are wrong, not the decoded frame
-            // order). All hail Redmond, ancestral home of the rich & stupid.
-            case CODEC_ID_MPEG2VIDEO:
-            case CODEC_ID_RAWVIDEO:
-            case CODEC_ID_H264:
-            case CODEC_ID_VC1:
-                break;
-
-            default:
-                pv->ignore_pts = 1;
-                break;
-        }
     }
     hb_buffer_t *in = *buf_in;
-    int64_t pts = -1;
-
     *buf_in = NULL;
 
     /* if we got an empty buffer signaling end-of-stream send it downstream */
@@ -623,35 +1067,21 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         while ( decodeFrame( pv, NULL, 0 ) )
         {
         }
+        flushDelayQueue( pv );
         hb_list_add( pv->list, in );
         *buf_out = link_buf_list( pv );
-        hb_log( "%s done: %d frames", pv->context->codec->name, pv->nframes );
         return HB_WORK_DONE;
     }
 
-    if( in->start >= 0 )
+    int64_t pts = in->start;
+    if( pts >= 0 )
     {
         // use the first timestamp as our 'next expected' pts
-        if ( pv->pts_next <= 0 )
+        if ( pv->pts_next < 0 )
         {
-            pv->pts_next = in->start;
-        }
-
-        if ( ! pv->ignore_pts )
-        {
-            pts = in->start;
-            if ( pv->pts > 0 )
-            {
-                hb_log( "overwriting pts %lld with %lld (diff %d)",
-                        pv->pts, pts, pts - pv->pts );
-            }
-            if ( pv->pts_next - pts >= 10.)
-            {
-                hb_log( "time reversal next %.0f pts %lld (diff %g)",
-                        pv->pts_next, pts, pv->pts_next - pts );
-            }
-            pv->pts = pts;
+            pv->pts_next = pts;
         }
+        pv->pts = pts;
     }
 
     if ( in->new_chap )
@@ -670,23 +1100,15 @@ static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
 {
     if ( decavcodecvInfo( w, info ) )
     {
-        // the frame rate in the codec seems to be bogus but it's ok in the stream.
-        AVStream *st = hb_ffmpeg_avstream( w->codec_param );
-        AVRational tb;
-        if ( st->r_frame_rate.den && st->r_frame_rate.num )
+        hb_work_private_t *pv = w->private_data;
+        if ( ! pv->context )
         {
-            tb.num = st->r_frame_rate.den;
-            tb.den = st->r_frame_rate.num;
-        }
-        else
-        {
-            tb = st->time_base;
+            init_ffmpeg_context( w );
         }
-
-        // ffmpeg gives the frame rate in frames per second while HB wants
-        // it in units of the 27MHz MPEG clock. */
+        // we have the frame duration in units of the 90KHz pts clock but
+        // need it in units of the 27MHz MPEG clock. */
         info->rate = 27000000;
-        info->rate_base = (int64_t)tb.num * 27000000LL / tb.den;
+        info->rate_base = pv->duration * 300.;
         return 1;
     }
     return 0;
@@ -699,8 +1121,27 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
 
     while ( pos < size )
     {
-        int16_t buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
-        int out_size = sizeof(buffer);
+        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.
+
+            #if defined( 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
+
+            buffer = pv->buffer;
+        }
+        int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
         int len = avcodec_decode_audio2( context, buffer, &out_size,
                                          data + pos, size - pos );
         if ( len <= 0 )
@@ -710,11 +1151,42 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
         pos += len;
         if( out_size > 0 )
         {
+            // We require signed 16-bit ints for the output format. If
+            // we got something different convert it.
+            if ( context->sample_fmt != SAMPLE_FMT_S16 )
+            {
+                // Note: av_audio_convert seems to be a work-in-progress but
+                //       looks like it will eventually handle general audio
+                //       mixdowns which would allow us much more flexibility
+                //       in handling multichannel audio in HB. If we were doing
+                //       anything more complicated than a one-for-one format
+                //       conversion we'd probably want to cache the converter
+                //       context in the pv.
+                int isamp = av_get_bits_per_sample_format( context->sample_fmt ) / 8;
+                AVAudioConvert *ctx = av_audio_convert_alloc( SAMPLE_FMT_S16, 1,
+                                                              context->sample_fmt, 1,
+                                                              NULL, 0 );
+                // get output buffer size (in 2-byte samples) then malloc a buffer
+                out_size = ( out_size * 2 ) / isamp;
+                buffer = malloc( out_size );
+
+                // we're doing straight sample format conversion which behaves as if
+                // there were only one channel.
+                const void * const ibuf[6] = { pv->buffer };
+                void * const obuf[6] = { buffer };
+                const int istride[6] = { isamp };
+                const int ostride[6] = { 2 };
+
+                av_audio_convert( ctx, obuf, ostride, ibuf, istride, out_size >> 1 );
+                av_audio_convert_free( ctx );
+            }
             hb_buffer_t *buf = hb_buffer_init( 2 * out_size );
 
+            // convert from bytes to total samples
+            out_size >>= 1;
+
             double pts = pv->pts_next;
             buf->start = pts;
-            out_size >>= 1;
             pts += out_size * pv->duration;
             buf->stop  = pts;
             pv->pts_next = pts;
@@ -726,6 +1198,12 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
                 fl32[i] = buffer[i];
             }
             hb_list_add( pv->list, buf );
+
+            // if we allocated a buffer for sample format conversion, free it
+            if ( buffer != pv->buffer )
+            {
+                free( buffer );
+            }
         }
     }
 }
@@ -733,15 +1211,38 @@ static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
                     hb_buffer_t **buf_out )
 {
+    if ( (*buf_in)->size <= 0 )
+    {
+        /* EOF on input stream - send it downstream & say that we're done */
+        *buf_out = *buf_in;
+        *buf_in = NULL;
+        return HB_WORK_DONE;
+    }
+
     hb_work_private_t *pv = w->private_data;
+
+    if ( (*buf_in)->start < -1 && pv->pts_next <= 0 )
+    {
+        // discard buffers that start before video time 0
+        *buf_out = NULL;
+        return HB_WORK_OK;
+    }
+
     if ( ! pv->context )
     {
         init_ffmpeg_context( w );
+        // 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.
         pv->duration = 90000. /
                     (double)( pv->context->sample_rate * pv->context->channels );
     }
     hb_buffer_t *in = *buf_in;
 
+    // if the packet has a timestamp use it if we don't have a timestamp yet
+    // or if there's been a timing discontinuity of more than 100ms.
     if ( in->start >= 0 &&
          ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
     {