+ // 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 )