2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../stdafx.h"
\r
24 #include "tbb_avcodec.h"
\r
26 #include <common/assert.h>
\r
27 #include <common/log.h>
\r
28 #include <common/env.h>
\r
30 #include <tbb/atomic.h>
\r
31 #include <tbb/parallel_for.h>
\r
32 #include <tbb/tbb_thread.h>
\r
34 #include <boost/foreach.hpp>
\r
36 #if defined(_MSC_VER)
\r
37 #pragma warning (push)
\r
38 #pragma warning (disable : 4244)
\r
42 #define __STDC_CONSTANT_MACROS
\r
43 #define __STDC_LIMIT_MACROS
\r
44 #include <libavformat/avformat.h>
\r
46 #if defined(_MSC_VER)
\r
47 #pragma warning (pop)
\r
52 static const int MAX_THREADS = 16; // See mpegvideo.h
\r
54 int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size)
\r
56 tbb::parallel_for(0, count, 1, [&](int i)
\r
58 int r = func(s, (char*)arg + i*size);
\r
66 int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count)
\r
68 std::array<std::vector<int>, 16> jobs;
\r
70 for(int n = 0; n < count; ++n)
\r
71 jobs[(n*MAX_THREADS) / count].push_back(n);
\r
73 tbb::parallel_for(0, MAX_THREADS, [&](int n)
\r
75 BOOST_FOREACH(auto k, jobs[n])
77 int r = func(s, arg, k, n);
86 void thread_init(AVCodecContext* s)
\r
88 static int dummy_opaque;
\r
90 s->active_thread_type = FF_THREAD_SLICE;
\r
91 s->thread_opaque = &dummy_opaque;
\r
92 s->execute = thread_execute;
\r
93 s->execute2 = thread_execute2;
\r
94 s->thread_count = MAX_THREADS; // We are using a task-scheduler, so use as many "threads/tasks" as possible.
\r
97 void thread_free(AVCodecContext* s)
\r
99 if(!s->thread_opaque)
\r
102 s->thread_opaque = nullptr;
\r
105 int tbb_avcodec_open(AVCodecContext* avctx, AVCodec* codec)
\r
107 avctx->thread_count = 1;
\r
108 // Some codecs don't like to have multiple multithreaded decoding instances. Only enable for those we know work.
\r
109 if((codec->capabilities & CODEC_CAP_SLICE_THREADS) && (avctx->thread_type & FF_THREAD_SLICE))
\r
110 thread_init(avctx);
\r
112 // ff_thread_init will not be executed since thread_opaque != nullptr || thread_count == 1.
\r
113 return avcodec_open(avctx, codec);
\r
116 int tbb_avcodec_close(AVCodecContext* avctx)
\r
118 thread_free(avctx);
\r
119 // ff_thread_free will not be executed since thread_opaque == nullptr.
\r
120 return avcodec_close(avctx);
\r