+ for( i = 0; i < i_nal; i++ )
+ {
+ int data = buf->alloc - buf->size;
+ int size = x264_nal_encode( buf->data + buf->size, &data, 1, &nal[i] );
+ if( size < 1 )
+ {
+ continue;
+ }
+
+ if( job->mux & HB_MUX_AVI )
+ {
+ if( nal[i].i_ref_idc == NAL_PRIORITY_HIGHEST )
+ {
+ buf->frametype = HB_FRAME_KEY;
+ }
+ buf->size += size;
+ continue;
+ }
+
+ /* H.264 in .mp4 or .mkv */
+ int naltype = buf->data[buf->size+4] & 0x1f;
+ if ( naltype == 0x7 || naltype == 0x8 )
+ {
+ // Sequence Parameter Set & Program Parameter Set go in the
+ // mp4 header so skip them here
+ continue;
+ }
+
+ /* H.264 in mp4 (stolen from mp4creator) */
+ buf->data[buf->size+0] = ( ( size - 4 ) >> 24 ) & 0xFF;
+ buf->data[buf->size+1] = ( ( size - 4 ) >> 16 ) & 0xFF;
+ buf->data[buf->size+2] = ( ( size - 4 ) >> 8 ) & 0xFF;
+ buf->data[buf->size+3] = ( ( size - 4 ) >> 0 ) & 0xFF;
+
+ /* Decide what type of frame we have. */
+ switch( pic_out->i_type )
+ {
+ case X264_TYPE_IDR:
+ buf->frametype = HB_FRAME_IDR;
+ /* if we have a chapter marker pending and this
+ frame's presentation time stamp is at or after
+ the marker's time stamp, use this as the
+ chapter start. */
+ if( pv->next_chap != 0 && pv->next_chap <= pic_out->i_pts )
+ {
+ pv->next_chap = 0;
+ buf->new_chap = pv->chap_mark;
+ }
+ break;
+
+ case X264_TYPE_I:
+ buf->frametype = HB_FRAME_I;
+ break;
+
+ case X264_TYPE_P:
+ buf->frametype = HB_FRAME_P;
+ break;
+
+ case X264_TYPE_B:
+ buf->frametype = HB_FRAME_B;
+ break;
+
+ /* This is for b-pyramid, which has reference b-frames
+ However, it doesn't seem to ever be used... */
+ case X264_TYPE_BREF:
+ buf->frametype = HB_FRAME_BREF;
+ break;
+
+ // If it isn't the above, what type of frame is it??
+ default:
+ buf->frametype = 0;
+ break;
+ }
+
+ /* Since libx264 doesn't tell us when b-frames are
+ themselves reference frames, figure it out on our own. */
+ if( (buf->frametype == HB_FRAME_B) &&
+ (nal[i].i_ref_idc != NAL_PRIORITY_DISPOSABLE) )
+ buf->frametype = HB_FRAME_BREF;
+
+ buf->size += size;
+ }
+ // make sure we found at least one video frame
+ if ( buf->size <= 0 )
+ {
+ // no video - discard the buf
+ hb_buffer_close( &buf );
+ }
+ return buf;
+}
+
+static hb_buffer_t *x264_encode( hb_work_object_t *w, hb_buffer_t *in )
+{
+ hb_work_private_t *pv = w->private_data;
+ hb_job_t *job = pv->job;
+
+ /* 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 );