forked from CasparCG/server
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverted TBB multithreading for FFmpeg
- Loading branch information
Showing
11 changed files
with
196 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2013 Sveriges Television AB http://casparcg.com/ | ||
* | ||
* This file is part of CasparCG (www.casparcg.com). | ||
* | ||
* CasparCG is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* CasparCG is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with CasparCG. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Author: Robert Nagy, [email protected] | ||
*/ | ||
|
||
#include "StdAfx.h" | ||
|
||
#include "tbb_avcodec.h" | ||
|
||
#include <common/log/log.h> | ||
#include <common/env.h> | ||
#include <common/utility/assert.h> | ||
|
||
#include <tbb/task.h> | ||
#include <tbb/atomic.h> | ||
#include <tbb/parallel_for.h> | ||
#include <tbb/tbb_thread.h> | ||
#include <boost/thread.hpp> | ||
|
||
#if defined(_MSC_VER) | ||
#pragma warning (push) | ||
#pragma warning (disable : 4244) | ||
#endif | ||
extern "C" | ||
{ | ||
#define __STDC_CONSTANT_MACROS | ||
#define __STDC_LIMIT_MACROS | ||
#include <libavformat/avformat.h> | ||
} | ||
#if defined(_MSC_VER) | ||
#pragma warning (pop) | ||
#endif | ||
|
||
namespace caspar { | ||
|
||
static const size_t MAX_THREADS = 16; // See mpegvideo.h | ||
|
||
int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size) | ||
{ | ||
tbb::parallel_for(0, count, 1, [&](int i) | ||
{ | ||
int r = func(s, (char*)arg + i*size); | ||
if(ret) | ||
ret[i] = r; | ||
}); | ||
|
||
return 0; | ||
} | ||
|
||
int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count) | ||
{ | ||
tbb::atomic<int> counter; | ||
counter = 0; | ||
|
||
//CASPAR_VERIFY(tbb::tbb_thread::hardware_concurrency() < MAX_THREADS); | ||
// Note: this will probably only work when tbb::task_scheduler_init::num_threads() < 16. | ||
tbb::parallel_for(tbb::blocked_range<int>(0, count, 2), [&](const tbb::blocked_range<int> &r) | ||
{ | ||
int threadnr = counter++; | ||
for(int jobnr = r.begin(); jobnr != r.end(); ++jobnr) | ||
{ | ||
int r = func(s, arg, jobnr, threadnr); | ||
if (ret) | ||
ret[jobnr] = r; | ||
} | ||
--counter; | ||
}); | ||
|
||
return 0; | ||
} | ||
|
||
void thread_init(AVCodecContext* s, bool execute2enable) | ||
{ | ||
s->execute = thread_execute; | ||
if (execute2enable) | ||
s->execute2 = thread_execute2; | ||
if (s->thread_type & FF_THREAD_SLICE) | ||
s->slice_count = std::min(tbb::tbb_thread::hardware_concurrency(), MAX_THREADS); | ||
if (s->thread_type & FF_THREAD_FRAME) | ||
s->thread_count = std::min(tbb::tbb_thread::hardware_concurrency(), MAX_THREADS); // We are using a task-scheduler, so use as many "threads/tasks" as possible. | ||
|
||
CASPAR_LOG(info) << "Initialized ffmpeg tbb context."; | ||
} | ||
|
||
int tbb_avcodec_open(AVCodecContext* avctx, const AVCodec* codec, AVDictionary** options) | ||
{ | ||
if(((codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) && (avctx->thread_type & FF_THREAD_SLICE) | ||
|| (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)) && (avctx->thread_type & FF_THREAD_FRAME)) | ||
{ | ||
thread_init(avctx, codec->id != AV_CODEC_ID_PRORES); // do not enable execute2 for prores codec as it cause crash | ||
} | ||
avctx->refcounted_frames = 0; | ||
return avcodec_open2(avctx, codec, options); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2013 Sveriges Television AB http://casparcg.com/ | ||
* | ||
* This file is part of CasparCG (www.casparcg.com). | ||
* | ||
* CasparCG is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* CasparCG is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with CasparCG. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Author: Robert Nagy, [email protected] | ||
*/ | ||
|
||
#pragma once | ||
|
||
struct AVCodecContext; | ||
struct AVCodec; | ||
struct AVDictionary; | ||
|
||
namespace caspar { | ||
|
||
int tbb_avcodec_open(AVCodecContext *avctx, const AVCodec *codec, AVDictionary ** options); | ||
|
||
} |