- frame = task.indata;
-
- ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet);
- pthread_mutex_lock(&c->buffer_mutex);
- av_frame_unref(frame);
- pthread_mutex_unlock(&c->buffer_mutex);
- av_frame_free(&frame);
+ /* The main thread ensures that any two outstanding tasks have
+ * different indices, ergo each worker thread owns its element
+ * of c->tasks with the exception of finished, which is shared
+ * with the main thread and guarded by finished_task_mutex. */
+ task = &c->tasks[task_index];
+ frame = task->indata;
+ pkt = task->outdata;
+
+ ret = avctx->codec->encode2(avctx, pkt, frame, &got_packet);