+/** Locate the first unprocessed frame in the queue.
+ *
+ * When playing with realtime behavior, we do not use the true head, but
+ * rather an adjusted process_head. The process_head is adjusted based on
+ * the rate of frame-dropping or recovery from frame-dropping. The idea is
+ * that as the level of frame-dropping increases to move the process_head
+ * closer to the tail because the frames are not completing processing prior
+ * to their playout! Then, as frames are not dropped the process_head moves
+ * back closer to the head of the queue so that worker threads can work
+ * ahead of the playout point (queue head).
+ *
+ * \private \memberof mlt_consumer_s
+ * \param self a consumer
+ * \return an index into the queue
+ */
+
+static inline int first_unprocessed_frame( mlt_consumer self )
+{
+ int index = self->real_time <= 0 ? 0 : self->process_head;
+ while ( index < mlt_deque_count( self->queue ) && MLT_FRAME( mlt_deque_peek( self->queue, index ) )->is_processing )
+ index++;
+ return index;
+}
+
+/** The worker thread procedure for parallel processing frames.
+ *
+ * \private \memberof mlt_consumer_s
+ * \param arg a consumer
+ */
+
+static void *consumer_worker_thread( void *arg )
+{
+ // The argument is the consumer
+ mlt_consumer self = arg;
+
+ // Get the properties of the consumer
+ mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
+
+ // Get the width and height
+ int width = mlt_properties_get_int( properties, "width" );
+ int height = mlt_properties_get_int( properties, "height" );
+ mlt_image_format format = self->format;
+
+ // See if video is turned off
+ int video_off = mlt_properties_get_int( properties, "video_off" );
+ int preview_off = mlt_properties_get_int( properties, "preview_off" );
+ int preview_format = mlt_properties_get_int( properties, "preview_format" );
+
+ // General frame variable
+ mlt_frame frame = NULL;
+ uint8_t *image = NULL;
+
+ if ( preview_off && preview_format != 0 )
+ format = preview_format;
+
+ // Continue to read ahead
+ while ( self->ahead )
+ {
+ // Get the next unprocessed frame from the work queue
+ pthread_mutex_lock( &self->queue_mutex );
+ int index = first_unprocessed_frame( self );
+ while ( self->ahead && index >= mlt_deque_count( self->queue ) )
+ {
+ mlt_log_debug( MLT_CONSUMER_SERVICE(self), "waiting in worker index = %d queue count = %d\n",
+ index, mlt_deque_count( self->queue ) );
+ pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
+ index = first_unprocessed_frame( self );
+ }
+
+ // Mark the frame for processing
+ frame = mlt_deque_peek( self->queue, index );
+ if ( frame )
+ {
+ mlt_log_debug( MLT_CONSUMER_SERVICE(self), "worker processing index = %d frame %d queue count = %d\n",
+ index, mlt_frame_get_position(frame), mlt_deque_count( self->queue ) );
+ frame->is_processing = 1;
+ mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
+ }
+ pthread_mutex_unlock( &self->queue_mutex );
+
+ // If there's no frame, we're probably stopped...
+ if ( frame == NULL )
+ continue;
+
+#ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
+ // All non normal playback frames should be shown
+ if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
+ mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
+#endif
+
+ // Get the image
+ if ( !video_off )
+ {
+ // Fetch width/height again
+ width = mlt_properties_get_int( properties, "width" );
+ height = mlt_properties_get_int( properties, "height" );
+ mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
+ mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
+ }
+ mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
+ mlt_frame_close( frame );
+
+ // Tell a waiting thread (non-realtime main consumer thread) that we are done.
+ pthread_mutex_lock( &self->done_mutex );
+ pthread_cond_broadcast( &self->done_cond );
+ pthread_mutex_unlock( &self->done_mutex );
+ }
+
+ return NULL;
+}
+
+/** Start the read/render thread.
+ *
+ * \private \memberof mlt_consumer_s
+ * \param self a consumer
+ */
+
+static void consumer_read_ahead_start( mlt_consumer self )