• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * Frame multithreading support functions
22  * @see doc/multithreading.txt
23  */
24 
25 #include "config.h"
26 
27 #include <stdatomic.h>
28 #include <stdint.h>
29 
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "hwconfig.h"
33 #include "internal.h"
34 #include "pthread_internal.h"
35 #include "thread.h"
36 #include "threadframe.h"
37 #include "version_major.h"
38 
39 #include "libavutil/avassert.h"
40 #include "libavutil/buffer.h"
41 #include "libavutil/common.h"
42 #include "libavutil/cpu.h"
43 #include "libavutil/frame.h"
44 #include "libavutil/internal.h"
45 #include "libavutil/log.h"
46 #include "libavutil/mem.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/thread.h"
49 
50 enum {
51     ///< Set when the thread is awaiting a packet.
52     STATE_INPUT_READY,
53     ///< Set before the codec has called ff_thread_finish_setup().
54     STATE_SETTING_UP,
55     /**
56      * Set when the codec calls get_buffer().
57      * State is returned to STATE_SETTING_UP afterwards.
58      */
59     STATE_GET_BUFFER,
60      /**
61       * Set when the codec calls get_format().
62       * State is returned to STATE_SETTING_UP afterwards.
63       */
64     STATE_GET_FORMAT,
65     ///< Set after the codec has called ff_thread_finish_setup().
66     STATE_SETUP_FINISHED,
67 };
68 
69 enum {
70     UNINITIALIZED,  ///< Thread has not been created, AVCodec->close mustn't be called
71     NEEDS_CLOSE,    ///< FFCodec->close needs to be called
72     INITIALIZED,    ///< Thread has been properly set up
73 };
74 
75 /**
76  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
77  */
78 typedef struct PerThreadContext {
79     struct FrameThreadContext *parent;
80 
81     pthread_t      thread;
82     int            thread_init;
83     unsigned       pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
84     pthread_cond_t input_cond;      ///< Used to wait for a new packet from the main thread.
85     pthread_cond_t progress_cond;   ///< Used by child threads to wait for progress to change.
86     pthread_cond_t output_cond;     ///< Used by the main thread to wait for frames to finish.
87 
88     pthread_mutex_t mutex;          ///< Mutex used to protect the contents of the PerThreadContext.
89     pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
90 
91     AVCodecContext *avctx;          ///< Context used to decode packets passed to this thread.
92 
93     AVPacket       *avpkt;          ///< Input packet (for decoding) or output (for encoding).
94 
95     AVFrame *frame;                 ///< Output frame (for decoding) or input (for encoding).
96     int     got_frame;              ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
97     int     result;                 ///< The result of the last codec decode/encode() call.
98 
99     atomic_int state;
100 
101 #if FF_API_THREAD_SAFE_CALLBACKS
102     /**
103      * Array of frames passed to ff_thread_release_buffer().
104      * Frames are released after all threads referencing them are finished.
105      */
106     AVFrame **released_buffers;
107     int   num_released_buffers;
108     int       released_buffers_allocated;
109 
110     AVFrame *requested_frame;       ///< AVFrame the codec passed to get_buffer()
111     int      requested_flags;       ///< flags passed to get_buffer() for requested_frame
112 
113     const enum AVPixelFormat *available_formats; ///< Format array for get_format()
114     enum AVPixelFormat result_format;            ///< get_format() result
115 #endif
116 
117     int die;                        ///< Set when the thread should exit.
118 
119     int hwaccel_serializing;
120     int async_serializing;
121 
122     atomic_int debug_threads;       ///< Set if the FF_DEBUG_THREADS option is set.
123 } PerThreadContext;
124 
125 /**
126  * Context stored in the client AVCodecInternal thread_ctx.
127  */
128 typedef struct FrameThreadContext {
129     PerThreadContext *threads;     ///< The contexts for each thread.
130     PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
131 
132     unsigned    pthread_init_cnt;  ///< Number of successfully initialized mutexes/conditions
133     pthread_mutex_t buffer_mutex;  ///< Mutex used to protect get/release_buffer().
134     /**
135      * This lock is used for ensuring threads run in serial when hwaccel
136      * is used.
137      */
138     pthread_mutex_t hwaccel_mutex;
139     pthread_mutex_t async_mutex;
140     pthread_cond_t async_cond;
141     int async_lock;
142 
143     int next_decoding;             ///< The next context to submit a packet to.
144     int next_finished;             ///< The next context to return output from.
145 
146     int delaying;                  /**<
147                                     * Set for the first N packets, where N is the number of threads.
148                                     * While it is set, ff_thread_en/decode_frame won't return any results.
149                                     */
150 
151     /* hwaccel state is temporarily stored here in order to transfer its ownership
152      * to the next decoding thread without the need for extra synchronization */
153     const AVHWAccel *stash_hwaccel;
154     void            *stash_hwaccel_context;
155     void            *stash_hwaccel_priv;
156 } FrameThreadContext;
157 
158 #if FF_API_THREAD_SAFE_CALLBACKS
159 #define THREAD_SAFE_CALLBACKS(avctx) \
160 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
161 #endif
162 
async_lock(FrameThreadContext * fctx)163 static void async_lock(FrameThreadContext *fctx)
164 {
165     pthread_mutex_lock(&fctx->async_mutex);
166     while (fctx->async_lock)
167         pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
168     fctx->async_lock = 1;
169     pthread_mutex_unlock(&fctx->async_mutex);
170 }
171 
async_unlock(FrameThreadContext * fctx)172 static void async_unlock(FrameThreadContext *fctx)
173 {
174     pthread_mutex_lock(&fctx->async_mutex);
175     av_assert0(fctx->async_lock);
176     fctx->async_lock = 0;
177     pthread_cond_broadcast(&fctx->async_cond);
178     pthread_mutex_unlock(&fctx->async_mutex);
179 }
180 
181 /**
182  * Codec worker thread.
183  *
184  * Automatically calls ff_thread_finish_setup() if the codec does
185  * not provide an update_thread_context method, or if the codec returns
186  * before calling it.
187  */
frame_worker_thread(void * arg)188 static attribute_align_arg void *frame_worker_thread(void *arg)
189 {
190     PerThreadContext *p = arg;
191     AVCodecContext *avctx = p->avctx;
192     const FFCodec *codec = ffcodec(avctx->codec);
193 
194     pthread_mutex_lock(&p->mutex);
195     while (1) {
196         while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
197             pthread_cond_wait(&p->input_cond, &p->mutex);
198 
199         if (p->die) break;
200 
201 FF_DISABLE_DEPRECATION_WARNINGS
202         if (!codec->update_thread_context
203 #if FF_API_THREAD_SAFE_CALLBACKS
204             && THREAD_SAFE_CALLBACKS(avctx)
205 #endif
206             )
207             ff_thread_finish_setup(avctx);
208 FF_ENABLE_DEPRECATION_WARNINGS
209 
210         /* If a decoder supports hwaccel, then it must call ff_get_format().
211          * Since that call must happen before ff_thread_finish_setup(), the
212          * decoder is required to implement update_thread_context() and call
213          * ff_thread_finish_setup() manually. Therefore the above
214          * ff_thread_finish_setup() call did not happen and hwaccel_serializing
215          * cannot be true here. */
216         av_assert0(!p->hwaccel_serializing);
217 
218         /* if the previous thread uses hwaccel then we take the lock to ensure
219          * the threads don't run concurrently */
220         if (avctx->hwaccel) {
221             pthread_mutex_lock(&p->parent->hwaccel_mutex);
222             p->hwaccel_serializing = 1;
223         }
224 
225         av_frame_unref(p->frame);
226         p->got_frame = 0;
227         p->result = codec->cb.decode(avctx, p->frame, &p->got_frame, p->avpkt);
228 
229         if ((p->result < 0 || !p->got_frame) && p->frame->buf[0])
230             ff_thread_release_buffer(avctx, p->frame);
231 
232         if (atomic_load(&p->state) == STATE_SETTING_UP)
233             ff_thread_finish_setup(avctx);
234 
235         if (p->hwaccel_serializing) {
236             /* wipe hwaccel state to avoid stale pointers lying around;
237              * the state was transferred to FrameThreadContext in
238              * ff_thread_finish_setup(), so nothing is leaked */
239             avctx->hwaccel                     = NULL;
240             avctx->hwaccel_context             = NULL;
241             avctx->internal->hwaccel_priv_data = NULL;
242 
243             p->hwaccel_serializing = 0;
244             pthread_mutex_unlock(&p->parent->hwaccel_mutex);
245         }
246         av_assert0(!avctx->hwaccel);
247 
248         if (p->async_serializing) {
249             p->async_serializing = 0;
250 
251             async_unlock(p->parent);
252         }
253 
254         pthread_mutex_lock(&p->progress_mutex);
255 
256         atomic_store(&p->state, STATE_INPUT_READY);
257 
258         pthread_cond_broadcast(&p->progress_cond);
259         pthread_cond_signal(&p->output_cond);
260         pthread_mutex_unlock(&p->progress_mutex);
261     }
262     pthread_mutex_unlock(&p->mutex);
263 
264     return NULL;
265 }
266 
267 /**
268  * Update the next thread's AVCodecContext with values from the reference thread's context.
269  *
270  * @param dst The destination context.
271  * @param src The source context.
272  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
273  * @return 0 on success, negative error code on failure
274  */
update_context_from_thread(AVCodecContext * dst,AVCodecContext * src,int for_user)275 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
276 {
277     const FFCodec *const codec = ffcodec(dst->codec);
278     int err = 0;
279 
280     if (dst != src && (for_user || codec->update_thread_context)) {
281         dst->time_base = src->time_base;
282         dst->framerate = src->framerate;
283         dst->width     = src->width;
284         dst->height    = src->height;
285         dst->pix_fmt   = src->pix_fmt;
286         dst->sw_pix_fmt = src->sw_pix_fmt;
287 
288         dst->coded_width  = src->coded_width;
289         dst->coded_height = src->coded_height;
290 
291         dst->has_b_frames = src->has_b_frames;
292         dst->idct_algo    = src->idct_algo;
293         dst->properties   = src->properties;
294 
295         dst->bits_per_coded_sample = src->bits_per_coded_sample;
296         dst->sample_aspect_ratio   = src->sample_aspect_ratio;
297 
298         dst->profile = src->profile;
299         dst->level   = src->level;
300 
301         dst->bits_per_raw_sample = src->bits_per_raw_sample;
302         dst->ticks_per_frame     = src->ticks_per_frame;
303         dst->color_primaries     = src->color_primaries;
304 
305         dst->color_trc   = src->color_trc;
306         dst->colorspace  = src->colorspace;
307         dst->color_range = src->color_range;
308         dst->chroma_sample_location = src->chroma_sample_location;
309 
310         dst->sample_rate    = src->sample_rate;
311         dst->sample_fmt     = src->sample_fmt;
312 #if FF_API_OLD_CHANNEL_LAYOUT
313 FF_DISABLE_DEPRECATION_WARNINGS
314         dst->channels       = src->channels;
315         dst->channel_layout = src->channel_layout;
316 FF_ENABLE_DEPRECATION_WARNINGS
317 #endif
318         err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
319         if (err < 0)
320             return err;
321 
322         if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
323             (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
324             av_buffer_unref(&dst->hw_frames_ctx);
325 
326             if (src->hw_frames_ctx) {
327                 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
328                 if (!dst->hw_frames_ctx)
329                     return AVERROR(ENOMEM);
330             }
331         }
332 
333         dst->hwaccel_flags = src->hwaccel_flags;
334 
335         err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
336         if (err < 0)
337             return err;
338     }
339 
340     if (for_user) {
341         if (codec->update_thread_context_for_user)
342             err = codec->update_thread_context_for_user(dst, src);
343     } else {
344         if (codec->update_thread_context)
345             err = codec->update_thread_context(dst, src);
346     }
347 
348     return err;
349 }
350 
351 /**
352  * Update the next thread's AVCodecContext with values set by the user.
353  *
354  * @param dst The destination context.
355  * @param src The source context.
356  * @return 0 on success, negative error code on failure
357  */
update_context_from_user(AVCodecContext * dst,AVCodecContext * src)358 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
359 {
360     dst->flags          = src->flags;
361 
362     dst->draw_horiz_band= src->draw_horiz_band;
363     dst->get_buffer2    = src->get_buffer2;
364 
365     dst->opaque   = src->opaque;
366     dst->debug    = src->debug;
367 
368     dst->slice_flags = src->slice_flags;
369     dst->flags2      = src->flags2;
370     dst->export_side_data = src->export_side_data;
371 
372     dst->skip_loop_filter = src->skip_loop_filter;
373     dst->skip_idct        = src->skip_idct;
374     dst->skip_frame       = src->skip_frame;
375 
376     dst->frame_number     = src->frame_number;
377     dst->reordered_opaque = src->reordered_opaque;
378 #if FF_API_THREAD_SAFE_CALLBACKS
379 FF_DISABLE_DEPRECATION_WARNINGS
380     dst->thread_safe_callbacks = src->thread_safe_callbacks;
381 FF_ENABLE_DEPRECATION_WARNINGS
382 #endif
383 
384     if (src->slice_count && src->slice_offset) {
385         if (dst->slice_count < src->slice_count) {
386             int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
387                                         sizeof(*dst->slice_offset));
388             if (err < 0)
389                 return err;
390         }
391         memcpy(dst->slice_offset, src->slice_offset,
392                src->slice_count * sizeof(*dst->slice_offset));
393     }
394     dst->slice_count = src->slice_count;
395     return 0;
396 }
397 
398 #if FF_API_THREAD_SAFE_CALLBACKS
399 /// Releases the buffers that this decoding thread was the last user of.
release_delayed_buffers(PerThreadContext * p)400 static void release_delayed_buffers(PerThreadContext *p)
401 {
402     FrameThreadContext *fctx = p->parent;
403 
404     while (p->num_released_buffers > 0) {
405         AVFrame *f;
406 
407         pthread_mutex_lock(&fctx->buffer_mutex);
408 
409         // fix extended data in case the caller screwed it up
410         av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
411                    p->avctx->codec_type == AVMEDIA_TYPE_AUDIO);
412         f = p->released_buffers[--p->num_released_buffers];
413         f->extended_data = f->data;
414         av_frame_unref(f);
415 
416         pthread_mutex_unlock(&fctx->buffer_mutex);
417     }
418 }
419 #endif
420 
submit_packet(PerThreadContext * p,AVCodecContext * user_avctx,AVPacket * avpkt)421 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
422                          AVPacket *avpkt)
423 {
424     FrameThreadContext *fctx = p->parent;
425     PerThreadContext *prev_thread = fctx->prev_thread;
426     const AVCodec *codec = p->avctx->codec;
427     int ret;
428 
429     if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
430         return 0;
431 
432     pthread_mutex_lock(&p->mutex);
433 
434     ret = update_context_from_user(p->avctx, user_avctx);
435     if (ret) {
436         pthread_mutex_unlock(&p->mutex);
437         return ret;
438     }
439     atomic_store_explicit(&p->debug_threads,
440                           (p->avctx->debug & FF_DEBUG_THREADS) != 0,
441                           memory_order_relaxed);
442 
443 #if FF_API_THREAD_SAFE_CALLBACKS
444     release_delayed_buffers(p);
445 #endif
446 
447     if (prev_thread) {
448         int err;
449         if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
450             pthread_mutex_lock(&prev_thread->progress_mutex);
451             while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
452                 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
453             pthread_mutex_unlock(&prev_thread->progress_mutex);
454         }
455 
456         err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
457         if (err) {
458             pthread_mutex_unlock(&p->mutex);
459             return err;
460         }
461     }
462 
463     /* transfer the stashed hwaccel state, if any */
464     av_assert0(!p->avctx->hwaccel);
465     FFSWAP(const AVHWAccel*, p->avctx->hwaccel,                     fctx->stash_hwaccel);
466     FFSWAP(void*,            p->avctx->hwaccel_context,             fctx->stash_hwaccel_context);
467     FFSWAP(void*,            p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
468 
469     av_packet_unref(p->avpkt);
470     ret = av_packet_ref(p->avpkt, avpkt);
471     if (ret < 0) {
472         pthread_mutex_unlock(&p->mutex);
473         av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
474         return ret;
475     }
476 
477     atomic_store(&p->state, STATE_SETTING_UP);
478     pthread_cond_signal(&p->input_cond);
479     pthread_mutex_unlock(&p->mutex);
480 
481 #if FF_API_THREAD_SAFE_CALLBACKS
482 FF_DISABLE_DEPRECATION_WARNINGS
483     /*
484      * If the client doesn't have a thread-safe get_buffer(),
485      * then decoding threads call back to the main thread,
486      * and it calls back to the client here.
487      */
488 
489     if (!p->avctx->thread_safe_callbacks && (
490          p->avctx->get_format != avcodec_default_get_format ||
491          p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
492         while (atomic_load(&p->state) != STATE_SETUP_FINISHED && atomic_load(&p->state) != STATE_INPUT_READY) {
493             int call_done = 1;
494             pthread_mutex_lock(&p->progress_mutex);
495             while (atomic_load(&p->state) == STATE_SETTING_UP)
496                 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
497 
498             switch (atomic_load_explicit(&p->state, memory_order_acquire)) {
499             case STATE_GET_BUFFER:
500                 p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
501                 break;
502             case STATE_GET_FORMAT:
503                 p->result_format = ff_get_format(p->avctx, p->available_formats);
504                 break;
505             default:
506                 call_done = 0;
507                 break;
508             }
509             if (call_done) {
510                 atomic_store(&p->state, STATE_SETTING_UP);
511                 pthread_cond_signal(&p->progress_cond);
512             }
513             pthread_mutex_unlock(&p->progress_mutex);
514         }
515     }
516 FF_ENABLE_DEPRECATION_WARNINGS
517 #endif
518 
519     fctx->prev_thread = p;
520     fctx->next_decoding++;
521 
522     return 0;
523 }
524 
ff_thread_decode_frame(AVCodecContext * avctx,AVFrame * picture,int * got_picture_ptr,AVPacket * avpkt)525 int ff_thread_decode_frame(AVCodecContext *avctx,
526                            AVFrame *picture, int *got_picture_ptr,
527                            AVPacket *avpkt)
528 {
529     FrameThreadContext *fctx = avctx->internal->thread_ctx;
530     int finished = fctx->next_finished;
531     PerThreadContext *p;
532     int err;
533 
534     /* release the async lock, permitting blocked hwaccel threads to
535      * go forward while we are in this function */
536     async_unlock(fctx);
537 
538     /*
539      * Submit a packet to the next decoding thread.
540      */
541 
542     p = &fctx->threads[fctx->next_decoding];
543     err = submit_packet(p, avctx, avpkt);
544     if (err)
545         goto finish;
546 
547     /*
548      * If we're still receiving the initial packets, don't return a frame.
549      */
550 
551     if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
552         fctx->delaying = 0;
553 
554     if (fctx->delaying) {
555         *got_picture_ptr=0;
556         if (avpkt->size) {
557             err = avpkt->size;
558             goto finish;
559         }
560     }
561 
562     /*
563      * Return the next available frame from the oldest thread.
564      * If we're at the end of the stream, then we have to skip threads that
565      * didn't output a frame/error, because we don't want to accidentally signal
566      * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
567      */
568 
569     do {
570         p = &fctx->threads[finished++];
571 
572         if (atomic_load(&p->state) != STATE_INPUT_READY) {
573             pthread_mutex_lock(&p->progress_mutex);
574             while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
575                 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
576             pthread_mutex_unlock(&p->progress_mutex);
577         }
578 
579         av_frame_move_ref(picture, p->frame);
580         *got_picture_ptr = p->got_frame;
581         picture->pkt_dts = p->avpkt->dts;
582         err = p->result;
583 
584         /*
585          * A later call with avkpt->size == 0 may loop over all threads,
586          * including this one, searching for a frame/error to return before being
587          * stopped by the "finished != fctx->next_finished" condition.
588          * Make sure we don't mistakenly return the same frame/error again.
589          */
590         p->got_frame = 0;
591         p->result = 0;
592 
593         if (finished >= avctx->thread_count) finished = 0;
594     } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
595 
596     update_context_from_thread(avctx, p->avctx, 1);
597 
598     if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
599 
600     fctx->next_finished = finished;
601 
602     /* return the size of the consumed packet if no error occurred */
603     if (err >= 0)
604         err = avpkt->size;
605 finish:
606     async_lock(fctx);
607     return err;
608 }
609 
ff_thread_report_progress(ThreadFrame * f,int n,int field)610 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
611 {
612     PerThreadContext *p;
613     atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
614 
615     if (!progress ||
616         atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
617         return;
618 
619     p = f->owner[field]->internal->thread_ctx;
620 
621     if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
622         av_log(f->owner[field], AV_LOG_DEBUG,
623                "%p finished %d field %d\n", progress, n, field);
624 
625     pthread_mutex_lock(&p->progress_mutex);
626 
627     atomic_store_explicit(&progress[field], n, memory_order_release);
628 
629     pthread_cond_broadcast(&p->progress_cond);
630     pthread_mutex_unlock(&p->progress_mutex);
631 }
632 
ff_thread_await_progress(ThreadFrame * f,int n,int field)633 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
634 {
635     PerThreadContext *p;
636     atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
637 
638     if (!progress ||
639         atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
640         return;
641 
642     p = f->owner[field]->internal->thread_ctx;
643 
644     if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
645         av_log(f->owner[field], AV_LOG_DEBUG,
646                "thread awaiting %d field %d from %p\n", n, field, progress);
647 
648     pthread_mutex_lock(&p->progress_mutex);
649     while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
650         pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
651     pthread_mutex_unlock(&p->progress_mutex);
652 }
653 
ff_thread_finish_setup(AVCodecContext * avctx)654 void ff_thread_finish_setup(AVCodecContext *avctx) {
655     PerThreadContext *p = avctx->internal->thread_ctx;
656 
657     if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
658 
659     if (avctx->hwaccel && !p->hwaccel_serializing) {
660         pthread_mutex_lock(&p->parent->hwaccel_mutex);
661         p->hwaccel_serializing = 1;
662     }
663 
664     /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
665     if (avctx->hwaccel &&
666         !(avctx->hwaccel->caps_internal & HWACCEL_CAP_ASYNC_SAFE)) {
667         p->async_serializing = 1;
668 
669         async_lock(p->parent);
670     }
671 
672     /* save hwaccel state for passing to the next thread;
673      * this is done here so that this worker thread can wipe its own hwaccel
674      * state after decoding, without requiring synchronization */
675     av_assert0(!p->parent->stash_hwaccel);
676     p->parent->stash_hwaccel         = avctx->hwaccel;
677     p->parent->stash_hwaccel_context = avctx->hwaccel_context;
678     p->parent->stash_hwaccel_priv    = avctx->internal->hwaccel_priv_data;
679 
680     pthread_mutex_lock(&p->progress_mutex);
681     if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
682         av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
683     }
684 
685     atomic_store(&p->state, STATE_SETUP_FINISHED);
686 
687     pthread_cond_broadcast(&p->progress_cond);
688     pthread_mutex_unlock(&p->progress_mutex);
689 }
690 
691 /// Waits for all threads to finish.
park_frame_worker_threads(FrameThreadContext * fctx,int thread_count)692 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
693 {
694     int i;
695 
696     async_unlock(fctx);
697 
698     for (i = 0; i < thread_count; i++) {
699         PerThreadContext *p = &fctx->threads[i];
700 
701         if (atomic_load(&p->state) != STATE_INPUT_READY) {
702             pthread_mutex_lock(&p->progress_mutex);
703             while (atomic_load(&p->state) != STATE_INPUT_READY)
704                 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
705             pthread_mutex_unlock(&p->progress_mutex);
706         }
707         p->got_frame = 0;
708     }
709 
710     async_lock(fctx);
711 }
712 
713 #define OFF(member) offsetof(FrameThreadContext, member)
714 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
715                     (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
716                     (OFF(async_cond)));
717 #undef OFF
718 
719 #define OFF(member) offsetof(PerThreadContext, member)
720 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
721                     (OFF(progress_mutex), OFF(mutex)),
722                     (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
723 #undef OFF
724 
ff_frame_thread_free(AVCodecContext * avctx,int thread_count)725 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
726 {
727     FrameThreadContext *fctx = avctx->internal->thread_ctx;
728     const FFCodec *codec = ffcodec(avctx->codec);
729     int i;
730 
731     park_frame_worker_threads(fctx, thread_count);
732 
733     for (i = 0; i < thread_count; i++) {
734         PerThreadContext *p = &fctx->threads[i];
735         AVCodecContext *ctx = p->avctx;
736 
737         if (ctx->internal) {
738             if (p->thread_init == INITIALIZED) {
739                 pthread_mutex_lock(&p->mutex);
740                 p->die = 1;
741                 pthread_cond_signal(&p->input_cond);
742                 pthread_mutex_unlock(&p->mutex);
743 
744                 pthread_join(p->thread, NULL);
745             }
746             if (codec->close && p->thread_init != UNINITIALIZED)
747                 codec->close(ctx);
748 
749 #if FF_API_THREAD_SAFE_CALLBACKS
750             release_delayed_buffers(p);
751             for (int j = 0; j < p->released_buffers_allocated; j++)
752                 av_frame_free(&p->released_buffers[j]);
753             av_freep(&p->released_buffers);
754 #endif
755             if (ctx->priv_data) {
756                 if (codec->p.priv_class)
757                     av_opt_free(ctx->priv_data);
758                 av_freep(&ctx->priv_data);
759             }
760 
761             av_freep(&ctx->slice_offset);
762 
763             av_buffer_unref(&ctx->internal->pool);
764             av_freep(&ctx->internal);
765             av_buffer_unref(&ctx->hw_frames_ctx);
766         }
767 
768         av_frame_free(&p->frame);
769 
770         ff_pthread_free(p, per_thread_offsets);
771         av_packet_free(&p->avpkt);
772 
773         av_freep(&p->avctx);
774     }
775 
776     av_freep(&fctx->threads);
777     ff_pthread_free(fctx, thread_ctx_offsets);
778 
779     /* if we have stashed hwaccel state, move it to the user-facing context,
780      * so it will be freed in avcodec_close() */
781     av_assert0(!avctx->hwaccel);
782     FFSWAP(const AVHWAccel*, avctx->hwaccel,                     fctx->stash_hwaccel);
783     FFSWAP(void*,            avctx->hwaccel_context,             fctx->stash_hwaccel_context);
784     FFSWAP(void*,            avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
785 
786     av_freep(&avctx->internal->thread_ctx);
787 }
788 
init_thread(PerThreadContext * p,int * threads_to_free,FrameThreadContext * fctx,AVCodecContext * avctx,const FFCodec * codec,int first)789 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
790                                FrameThreadContext *fctx, AVCodecContext *avctx,
791                                const FFCodec *codec, int first)
792 {
793     AVCodecContext *copy;
794     int err;
795 
796     atomic_init(&p->state, STATE_INPUT_READY);
797 
798     copy = av_memdup(avctx, sizeof(*avctx));
799     if (!copy)
800         return AVERROR(ENOMEM);
801     copy->priv_data = NULL;
802 
803     /* From now on, this PerThreadContext will be cleaned up by
804      * ff_frame_thread_free in case of errors. */
805     (*threads_to_free)++;
806 
807     p->parent = fctx;
808     p->avctx  = copy;
809 
810     copy->internal = av_mallocz(sizeof(*copy->internal));
811     if (!copy->internal)
812         return AVERROR(ENOMEM);
813     copy->internal->thread_ctx = p;
814 
815     copy->delay = avctx->delay;
816 
817     if (codec->priv_data_size) {
818         copy->priv_data = av_mallocz(codec->priv_data_size);
819         if (!copy->priv_data)
820             return AVERROR(ENOMEM);
821 
822         if (codec->p.priv_class) {
823             *(const AVClass **)copy->priv_data = codec->p.priv_class;
824             err = av_opt_copy(copy->priv_data, avctx->priv_data);
825             if (err < 0)
826                 return err;
827         }
828     }
829 
830     err = ff_pthread_init(p, per_thread_offsets);
831     if (err < 0)
832         return err;
833 
834     if (!(p->frame = av_frame_alloc()) ||
835         !(p->avpkt = av_packet_alloc()))
836         return AVERROR(ENOMEM);
837     copy->internal->last_pkt_props = p->avpkt;
838 
839     if (!first)
840         copy->internal->is_copy = 1;
841 
842     if (codec->init) {
843         err = codec->init(copy);
844         if (err < 0) {
845             if (codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)
846                 p->thread_init = NEEDS_CLOSE;
847             return err;
848         }
849     }
850     p->thread_init = NEEDS_CLOSE;
851 
852     if (first)
853         update_context_from_thread(avctx, copy, 1);
854 
855     atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
856 
857     err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
858     if (err < 0)
859         return err;
860     p->thread_init = INITIALIZED;
861 
862     return 0;
863 }
864 
ff_frame_thread_init(AVCodecContext * avctx)865 int ff_frame_thread_init(AVCodecContext *avctx)
866 {
867     int thread_count = avctx->thread_count;
868     const FFCodec *codec = ffcodec(avctx->codec);
869     FrameThreadContext *fctx;
870     int err, i = 0;
871 
872     if (!thread_count) {
873         int nb_cpus = av_cpu_count();
874         // use number of cores + 1 as thread count if there is more than one
875         if (nb_cpus > 1)
876             thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
877         else
878             thread_count = avctx->thread_count = 1;
879     }
880 
881     if (thread_count <= 1) {
882         avctx->active_thread_type = 0;
883         return 0;
884     }
885 
886     avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
887     if (!fctx)
888         return AVERROR(ENOMEM);
889 
890     err = ff_pthread_init(fctx, thread_ctx_offsets);
891     if (err < 0) {
892         ff_pthread_free(fctx, thread_ctx_offsets);
893         av_freep(&avctx->internal->thread_ctx);
894         return err;
895     }
896 
897     fctx->async_lock = 1;
898     fctx->delaying = 1;
899 
900     if (codec->p.type == AVMEDIA_TYPE_VIDEO)
901         avctx->delay = avctx->thread_count - 1;
902 
903     fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads));
904     if (!fctx->threads) {
905         err = AVERROR(ENOMEM);
906         goto error;
907     }
908 
909     for (; i < thread_count; ) {
910         PerThreadContext *p  = &fctx->threads[i];
911         int first = !i;
912 
913         err = init_thread(p, &i, fctx, avctx, codec, first);
914         if (err < 0)
915             goto error;
916     }
917 
918     return 0;
919 
920 error:
921     ff_frame_thread_free(avctx, i);
922     return err;
923 }
924 
ff_thread_flush(AVCodecContext * avctx)925 void ff_thread_flush(AVCodecContext *avctx)
926 {
927     int i;
928     FrameThreadContext *fctx = avctx->internal->thread_ctx;
929 
930     if (!fctx) return;
931 
932     park_frame_worker_threads(fctx, avctx->thread_count);
933     if (fctx->prev_thread) {
934         if (fctx->prev_thread != &fctx->threads[0])
935             update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
936     }
937 
938     fctx->next_decoding = fctx->next_finished = 0;
939     fctx->delaying = 1;
940     fctx->prev_thread = NULL;
941     for (i = 0; i < avctx->thread_count; i++) {
942         PerThreadContext *p = &fctx->threads[i];
943         // Make sure decode flush calls with size=0 won't return old frames
944         p->got_frame = 0;
945         av_frame_unref(p->frame);
946         p->result = 0;
947 
948 #if FF_API_THREAD_SAFE_CALLBACKS
949         release_delayed_buffers(p);
950 #endif
951 
952         if (ffcodec(avctx->codec)->flush)
953             ffcodec(avctx->codec)->flush(p->avctx);
954     }
955 }
956 
ff_thread_can_start_frame(AVCodecContext * avctx)957 int ff_thread_can_start_frame(AVCodecContext *avctx)
958 {
959     PerThreadContext *p = avctx->internal->thread_ctx;
960 FF_DISABLE_DEPRECATION_WARNINGS
961     if ((avctx->active_thread_type&FF_THREAD_FRAME) && atomic_load(&p->state) != STATE_SETTING_UP &&
962         (ffcodec(avctx->codec)->update_thread_context
963 #if FF_API_THREAD_SAFE_CALLBACKS
964          || !THREAD_SAFE_CALLBACKS(avctx)
965 #endif
966          )) {
967         return 0;
968     }
969 FF_ENABLE_DEPRECATION_WARNINGS
970     return 1;
971 }
972 
thread_get_buffer_internal(AVCodecContext * avctx,AVFrame * f,int flags)973 static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
974 {
975     PerThreadContext *p;
976     int err;
977 
978     if (!(avctx->active_thread_type & FF_THREAD_FRAME))
979         return ff_get_buffer(avctx, f, flags);
980 
981     p = avctx->internal->thread_ctx;
982 FF_DISABLE_DEPRECATION_WARNINGS
983     if (atomic_load(&p->state) != STATE_SETTING_UP &&
984         (ffcodec(avctx->codec)->update_thread_context
985 #if FF_API_THREAD_SAFE_CALLBACKS
986          || !THREAD_SAFE_CALLBACKS(avctx)
987 #endif
988          )) {
989 FF_ENABLE_DEPRECATION_WARNINGS
990         av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
991         return -1;
992     }
993 
994     pthread_mutex_lock(&p->parent->buffer_mutex);
995 #if !FF_API_THREAD_SAFE_CALLBACKS
996     err = ff_get_buffer(avctx, f->f, flags);
997 #else
998 FF_DISABLE_DEPRECATION_WARNINGS
999     if (THREAD_SAFE_CALLBACKS(avctx)) {
1000         err = ff_get_buffer(avctx, f, flags);
1001     } else {
1002         pthread_mutex_lock(&p->progress_mutex);
1003         p->requested_frame = f;
1004         p->requested_flags = flags;
1005         atomic_store_explicit(&p->state, STATE_GET_BUFFER, memory_order_release);
1006         pthread_cond_broadcast(&p->progress_cond);
1007 
1008         while (atomic_load(&p->state) != STATE_SETTING_UP)
1009             pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
1010 
1011         err = p->result;
1012 
1013         pthread_mutex_unlock(&p->progress_mutex);
1014 
1015     }
1016     if (!THREAD_SAFE_CALLBACKS(avctx) && !ffcodec(avctx->codec)->update_thread_context)
1017         ff_thread_finish_setup(avctx);
1018 FF_ENABLE_DEPRECATION_WARNINGS
1019 #endif
1020 
1021     pthread_mutex_unlock(&p->parent->buffer_mutex);
1022 
1023     return err;
1024 }
1025 
1026 #if FF_API_THREAD_SAFE_CALLBACKS
1027 FF_DISABLE_DEPRECATION_WARNINGS
ff_thread_get_format(AVCodecContext * avctx,const enum AVPixelFormat * fmt)1028 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1029 {
1030     enum AVPixelFormat res;
1031     PerThreadContext *p;
1032     if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
1033         avctx->get_format == avcodec_default_get_format)
1034         return ff_get_format(avctx, fmt);
1035 
1036     p = avctx->internal->thread_ctx;
1037     if (atomic_load(&p->state) != STATE_SETTING_UP) {
1038         av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
1039         return -1;
1040     }
1041     pthread_mutex_lock(&p->progress_mutex);
1042     p->available_formats = fmt;
1043     atomic_store(&p->state, STATE_GET_FORMAT);
1044     pthread_cond_broadcast(&p->progress_cond);
1045 
1046     while (atomic_load(&p->state) != STATE_SETTING_UP)
1047         pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
1048 
1049     res = p->result_format;
1050 
1051     pthread_mutex_unlock(&p->progress_mutex);
1052 
1053     return res;
1054 }
1055 FF_ENABLE_DEPRECATION_WARNINGS
1056 #endif
1057 
ff_thread_get_buffer(AVCodecContext * avctx,AVFrame * f,int flags)1058 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
1059 {
1060     int ret = thread_get_buffer_internal(avctx, f, flags);
1061     if (ret < 0)
1062         av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1063     return ret;
1064 }
1065 
ff_thread_get_ext_buffer(AVCodecContext * avctx,ThreadFrame * f,int flags)1066 int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
1067 {
1068     int ret;
1069 
1070     f->owner[0] = f->owner[1] = avctx;
1071     /* Hint: It is possible for this function to be called with codecs
1072      * that don't support frame threading at all, namely in case
1073      * a frame-threaded decoder shares code with codecs that are not.
1074      * This currently affects non-MPEG-4 mpegvideo codecs and and VP7.
1075      * The following check will always be true for them. */
1076     if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1077         return ff_get_buffer(avctx, f->f, flags);
1078 
1079     if (ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_ALLOCATE_PROGRESS) {
1080         atomic_int *progress;
1081         f->progress = av_buffer_alloc(2 * sizeof(*progress));
1082         if (!f->progress) {
1083             return AVERROR(ENOMEM);
1084         }
1085         progress = (atomic_int*)f->progress->data;
1086 
1087         atomic_init(&progress[0], -1);
1088         atomic_init(&progress[1], -1);
1089     }
1090 
1091     ret = ff_thread_get_buffer(avctx, f->f, flags);
1092     if (ret)
1093         av_buffer_unref(&f->progress);
1094     return ret;
1095 }
1096 
ff_thread_release_buffer(AVCodecContext * avctx,AVFrame * f)1097 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1098 {
1099 #if FF_API_THREAD_SAFE_CALLBACKS
1100 FF_DISABLE_DEPRECATION_WARNINGS
1101     PerThreadContext *p;
1102     FrameThreadContext *fctx;
1103     AVFrame *dst;
1104     int ret = 0;
1105     int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
1106                           THREAD_SAFE_CALLBACKS(avctx);
1107 FF_ENABLE_DEPRECATION_WARNINGS
1108 #endif
1109 
1110     if (!f)
1111         return;
1112 
1113     if (avctx->debug & FF_DEBUG_BUFFERS)
1114         av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1115 
1116 #if !FF_API_THREAD_SAFE_CALLBACKS
1117     av_frame_unref(f->f);
1118 #else
1119     // when the frame buffers are not allocated, just reset it to clean state
1120     if (can_direct_free || !f->buf[0]) {
1121         av_frame_unref(f);
1122         return;
1123     }
1124 
1125     p    = avctx->internal->thread_ctx;
1126     fctx = p->parent;
1127     pthread_mutex_lock(&fctx->buffer_mutex);
1128 
1129     if (p->num_released_buffers == p->released_buffers_allocated) {
1130         AVFrame **tmp = av_realloc_array(p->released_buffers, p->released_buffers_allocated + 1,
1131                                          sizeof(*p->released_buffers));
1132         if (tmp) {
1133             tmp[p->released_buffers_allocated] = av_frame_alloc();
1134             p->released_buffers = tmp;
1135         }
1136 
1137         if (!tmp || !tmp[p->released_buffers_allocated]) {
1138             ret = AVERROR(ENOMEM);
1139             goto fail;
1140         }
1141         p->released_buffers_allocated++;
1142     }
1143 
1144     dst = p->released_buffers[p->num_released_buffers];
1145     av_frame_move_ref(dst, f);
1146 
1147     p->num_released_buffers++;
1148 
1149 fail:
1150     pthread_mutex_unlock(&fctx->buffer_mutex);
1151 
1152     // make sure the frame is clean even if we fail to free it
1153     // this leaks, but it is better than crashing
1154     if (ret < 0) {
1155         av_log(avctx, AV_LOG_ERROR, "Could not queue a frame for freeing, this will leak\n");
1156         memset(f->buf, 0, sizeof(f->buf));
1157         if (f->extended_buf)
1158             memset(f->extended_buf, 0, f->nb_extended_buf * sizeof(*f->extended_buf));
1159         av_frame_unref(f);
1160     }
1161 #endif
1162 }
1163 
ff_thread_release_ext_buffer(AVCodecContext * avctx,ThreadFrame * f)1164 void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
1165 {
1166     av_buffer_unref(&f->progress);
1167     f->owner[0] = f->owner[1] = NULL;
1168     ff_thread_release_buffer(avctx, f->f);
1169 }
1170