LCOV - code coverage report
Current view: top level - libavfilter - pthread.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 37 45 82.2 %
Date: 2017-12-18 06:23:41 Functions: 6 6 100.0 %

          Line data    Source code
       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             :  * Libavfilter multithreading support
      22             :  */
      23             : 
      24             : #include "config.h"
      25             : 
      26             : #include "libavutil/common.h"
      27             : #include "libavutil/cpu.h"
      28             : #include "libavutil/mem.h"
      29             : #include "libavutil/thread.h"
      30             : #include "libavutil/slicethread.h"
      31             : 
      32             : #include "avfilter.h"
      33             : #include "internal.h"
      34             : #include "thread.h"
      35             : 
      36             : typedef struct ThreadContext {
      37             :     AVFilterGraph *graph;
      38             :     AVSliceThread *thread;
      39             :     avfilter_action_func *func;
      40             : 
      41             :     /* per-execute parameters */
      42             :     AVFilterContext *ctx;
      43             :     void *arg;
      44             :     int   *rets;
      45             : } ThreadContext;
      46             : 
      47       10881 : static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
      48             : {
      49       10881 :     ThreadContext *c = priv;
      50       10881 :     int ret = c->func(c->ctx, c->arg, jobnr, nb_jobs);
      51       10881 :     if (c->rets)
      52           0 :         c->rets[jobnr] = ret;
      53       10881 : }
      54             : 
      55        2508 : static void slice_thread_uninit(ThreadContext *c)
      56             : {
      57        2508 :     avpriv_slicethread_free(&c->thread);
      58        2508 : }
      59             : 
      60        1209 : static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
      61             :                           void *arg, int *ret, int nb_jobs)
      62             : {
      63        1209 :     ThreadContext *c = ctx->graph->internal->thread;
      64             : 
      65        1209 :     if (nb_jobs <= 0)
      66           0 :         return 0;
      67        1209 :     c->ctx         = ctx;
      68        1209 :     c->arg         = arg;
      69        1209 :     c->func        = func;
      70        1209 :     c->rets        = ret;
      71             : 
      72        1209 :     avpriv_slicethread_execute(c->thread, nb_jobs, 0);
      73        1209 :     return 0;
      74             : }
      75             : 
      76        2508 : static int thread_init_internal(ThreadContext *c, int nb_threads)
      77             : {
      78        2508 :     nb_threads = avpriv_slicethread_create(&c->thread, c, worker_func, NULL, nb_threads);
      79        2508 :     if (nb_threads <= 1)
      80           0 :         avpriv_slicethread_free(&c->thread);
      81        2508 :     return FFMAX(nb_threads, 1);
      82             : }
      83             : 
      84        5474 : int ff_graph_thread_init(AVFilterGraph *graph)
      85             : {
      86             :     int ret;
      87             : 
      88             : #if HAVE_W32THREADS
      89             :     w32thread_init();
      90             : #endif
      91             : 
      92        5474 :     if (graph->nb_threads == 1) {
      93        2966 :         graph->thread_type = 0;
      94        2966 :         return 0;
      95             :     }
      96             : 
      97        2508 :     graph->internal->thread = av_mallocz(sizeof(ThreadContext));
      98        2508 :     if (!graph->internal->thread)
      99           0 :         return AVERROR(ENOMEM);
     100             : 
     101        2508 :     ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
     102        2508 :     if (ret <= 1) {
     103           0 :         av_freep(&graph->internal->thread);
     104           0 :         graph->thread_type = 0;
     105           0 :         graph->nb_threads  = 1;
     106           0 :         return (ret < 0) ? ret : 0;
     107             :     }
     108        2508 :     graph->nb_threads = ret;
     109             : 
     110        2508 :     graph->internal->thread_execute = thread_execute;
     111             : 
     112        2508 :     return 0;
     113             : }
     114             : 
     115        5474 : void ff_graph_thread_free(AVFilterGraph *graph)
     116             : {
     117        5474 :     if (graph->internal->thread)
     118        2508 :         slice_thread_uninit(graph->internal->thread);
     119        5474 :     av_freep(&graph->internal->thread);
     120        5474 : }

Generated by: LCOV version 1.13