+ /* Point x264 at our current buffers Y(UV) data. */
+ pv->pic_in.img.plane[0] = in->data;
+
+ int uvsize = ( (job->width + 1) >> 1 ) * ( (job->height + 1) >> 1 );
+ if( job->grayscale )
+ {
+ /* XXX x264 has currently no option for grayscale encoding */
+ memset( pv->pic_in.img.plane[1], 0x80, uvsize );
+ memset( pv->pic_in.img.plane[2], 0x80, uvsize );
+ }
+ else
+ {
+ /* Point x264 at our buffers (Y)UV data */
+ pv->pic_in.img.plane[1] = in->data + job->width * job->height;
+ pv->pic_in.img.plane[2] = pv->pic_in.img.plane[1] + uvsize;
+ }
+ if( in->new_chap && job->chapter_markers )
+ {
+ /* chapters have to start with an IDR frame so request that this
+ frame be coded as IDR. Since there may be up to 16 frames
+ currently buffered in the encoder remember the timestamp so
+ when this frame finally pops out of the encoder we'll mark
+ its buffer as the start of a chapter. */
+ pv->pic_in.i_type = X264_TYPE_IDR;
+ if( pv->next_chap == 0 )
+ {
+ pv->next_chap = in->start;
+ pv->chap_mark = in->new_chap;
+ }
+ /* don't let 'work_loop' put a chapter mark on the wrong buffer */
+ in->new_chap = 0;
+ }
+ else
+ {
+ pv->pic_in.i_type = X264_TYPE_AUTO;
+ }
+ pv->pic_in.i_qpplus1 = 0;
+
+ /* XXX this is temporary debugging code to check that the upstream
+ * modules (render & sync) have generated a continuous, self-consistent
+ * frame stream with the current frame's start time equal to the
+ * previous frame's stop time.
+ */
+ if( pv->last_stop != in->start )
+ {
+ hb_log("encx264 input continuity err: last stop %"PRId64" start %"PRId64,
+ pv->last_stop, in->start);
+ }
+ pv->last_stop = in->stop;
+
+ // Remember info about this frame that we need to pass across
+ // the x264_encoder_encode call (since it reorders frames).
+ save_frame_info( pv, in );
+
+ /* Feed the input PTS to x264 so it can figure out proper output PTS */
+ pv->pic_in.i_pts = in->start;
+
+ x264_picture_t pic_out;
+ int i_nal;
+ x264_nal_t *nal;
+
+ x264_encoder_encode( pv->x264, &nal, &i_nal, &pv->pic_in, &pic_out );
+ if ( i_nal > 0 )
+ {
+ return nal_encode( w, &pic_out, i_nal, nal );
+ }
+ return NULL;