]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/tbb_avcodec.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / modules / ffmpeg / producer / tbb_avcodec.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "tbb_avcodec.h"
25
26 #include <common/assert.h>
27 #include <common/except.h>
28 #include <common/log.h>
29 #include <common/env.h>
30
31 #include <tbb/atomic.h>
32 #include <tbb/parallel_for.h>
33 #include <tbb/tbb_thread.h>
34
35 #include <boost/foreach.hpp>
36
37 #if defined(_MSC_VER)
38 #pragma warning (push)
39 #pragma warning (disable : 4244)
40 #endif
41 extern "C" 
42 {
43         #define __STDC_CONSTANT_MACROS
44         #define __STDC_LIMIT_MACROS
45         #include <libavformat/avformat.h>
46 }
47 #if defined(_MSC_VER)
48 #pragma warning (pop)
49 #endif
50
51 namespace caspar {
52                 
53 static const int MAX_THREADS = 16; // See mpegvideo.h
54
55 int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size)
56 {
57         tbb::parallel_for(0, count, 1, [&](int i)
58         {
59         int r = func(s, (char*)arg + i*size);
60         if(ret) 
61                         ret[i] = r;
62     });
63
64         return 0;
65 }
66
67 int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count)
68 {          
69         // TODO: Micro-optimize...
70
71         std::array<std::vector<int>, 16> jobs;
72         
73         for(int n = 0; n < count; ++n)  
74                 jobs[(n*MAX_THREADS) / count].push_back(n);     
75         
76         tbb::parallel_for(0, MAX_THREADS, [&](int n)    
77     {   
78                 BOOST_FOREACH(auto k, jobs[n])
79                 {
80                         int r = func(s, arg, k, n);
81                         if(ret) 
82                                 ret[k]= r;
83                 }
84     });   
85
86         return 0; 
87 }
88
89 void thread_init(AVCodecContext* s)
90 {
91         static int dummy_opaque;
92
93     s->active_thread_type = FF_THREAD_SLICE;
94         s->thread_opaque          = &dummy_opaque; 
95     s->execute                    = thread_execute;
96     s->execute2                   = thread_execute2;
97     s->thread_count               = MAX_THREADS; // We are using a task-scheduler, so use as many "threads/tasks" as possible. 
98 }
99
100 void thread_free(AVCodecContext* s)
101 {
102         if(!s->thread_opaque)
103                 return;
104
105         s->thread_opaque = nullptr;
106 }
107
108 int tbb_avcodec_open(AVCodecContext* avctx, AVCodec* codec)
109 {
110         if(codec->capabilities & CODEC_CAP_EXPERIMENTAL)
111                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Experimental codecs are not supported."));
112
113         avctx->thread_count = 1;
114
115         if(codec->capabilities & CODEC_CAP_SLICE_THREADS)       
116                 thread_init(avctx);
117         
118         // ff_thread_init will not be executed since thread_opaque != nullptr || thread_count == 1.
119         return avcodec_open(avctx, codec); 
120 }
121
122 int tbb_avcodec_close(AVCodecContext* avctx)
123 {
124         thread_free(avctx);
125         // ff_thread_free will not be executed since thread_opaque == nullptr.
126         return avcodec_close(avctx); 
127 }
128
129 }