]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/frame_thread_encoder.c
avformat/mpegtsenc: Fix mpegts_write_pes() for private_stream_2 and other types
[ffmpeg] / libavcodec / frame_thread_encoder.c
index 5fe886aed9d68c71e0273c183a647f61809a7d11..778317d60bbb479b238a24b6b06b98759e0eabf8 100644 (file)
@@ -22,7 +22,6 @@
 
 #include "frame_thread_encoder.h"
 
-#include "libavutil/fifo.h"
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
@@ -41,7 +40,7 @@
 typedef struct{
     AVFrame  *indata;
     AVPacket *outdata;
-    int64_t return_code;
+    int       return_code;
     int       finished;
 } Task;
 
@@ -49,8 +48,7 @@ typedef struct{
     AVCodecContext *parent_avctx;
     pthread_mutex_t buffer_mutex;
 
-    AVFifoBuffer *task_fifo;
-    pthread_mutex_t task_fifo_mutex;
+    pthread_mutex_t task_fifo_mutex; /* Used to guard (next_)task_index */
     pthread_cond_t task_fifo_cond;
 
     unsigned max_tasks;
@@ -58,6 +56,7 @@ typedef struct{
     pthread_mutex_t finished_task_mutex; /* Guards tasks[i].finished */
     pthread_cond_t finished_task_cond;
 
+    unsigned next_task_index;
     unsigned task_index;
     unsigned finished_task_index;
 
@@ -77,14 +76,15 @@ static void * attribute_align_arg worker(void *v){
         unsigned task_index;
 
         pthread_mutex_lock(&c->task_fifo_mutex);
-        while (av_fifo_size(c->task_fifo) <= 0 || atomic_load(&c->exit)) {
+        while (c->next_task_index == c->task_index || atomic_load(&c->exit)) {
             if (atomic_load(&c->exit)) {
                 pthread_mutex_unlock(&c->task_fifo_mutex);
                 goto end;
             }
             pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
         }
-        av_fifo_generic_read(c->task_fifo, &task_index, sizeof(task_index), NULL);
+        task_index         = c->next_task_index;
+        c->next_task_index = (c->next_task_index + 1) % c->max_tasks;
         pthread_mutex_unlock(&c->task_fifo_mutex);
         /* The main thread ensures that any two outstanding tasks have
          * different indices, ergo each worker thread owns its element
@@ -187,12 +187,6 @@ int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){
 
     c->parent_avctx = avctx;
 
-    c->task_fifo = av_fifo_alloc_array(BUFFER_SIZE, sizeof(unsigned));
-    if (!c->task_fifo) {
-        av_freep(&avctx->internal->frame_thread_encoder);
-        return AVERROR(ENOMEM);
-    }
-
     pthread_mutex_init(&c->task_fifo_mutex, NULL);
     pthread_mutex_init(&c->finished_task_mutex, NULL);
     pthread_mutex_init(&c->buffer_mutex, NULL);
@@ -278,7 +272,6 @@ void ff_frame_thread_encoder_free(AVCodecContext *avctx){
     pthread_mutex_destroy(&c->buffer_mutex);
     pthread_cond_destroy(&c->task_fifo_cond);
     pthread_cond_destroy(&c->finished_task_cond);
-    av_fifo_freep(&c->task_fifo);
     av_freep(&avctx->internal->frame_thread_encoder);
 }
 
@@ -294,16 +287,15 @@ int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         av_frame_move_ref(c->tasks[c->task_index].indata, frame);
 
         pthread_mutex_lock(&c->task_fifo_mutex);
-        av_fifo_generic_write(c->task_fifo, &c->task_index,
-                              sizeof(c->task_index), NULL);
+        c->task_index = (c->task_index + 1) % c->max_tasks;
         pthread_cond_signal(&c->task_fifo_cond);
         pthread_mutex_unlock(&c->task_fifo_mutex);
-
-        c->task_index = (c->task_index + 1) % c->max_tasks;
     }
 
     outtask = &c->tasks[c->finished_task_index];
     pthread_mutex_lock(&c->finished_task_mutex);
+    /* The access to task_index in the following code is ok,
+     * because it is only ever changed by the main thread. */
     if (c->task_index == c->finished_task_index ||
         (frame && !outtask->finished &&
          (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) {
@@ -313,6 +305,7 @@ int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     while (!outtask->finished) {
         pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
     }
+    pthread_mutex_unlock(&c->finished_task_mutex);
     /* We now own outtask completely: No worker thread touches it any more,
      * because there is no outstanding task with this index. */
     outtask->finished = 0;
@@ -320,7 +313,6 @@ int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     if(pkt->data)
         *got_packet_ptr = 1;
     c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks;
-    pthread_mutex_unlock(&c->finished_task_mutex);
 
     return outtask->return_code;
 }