You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
11KB

  1. /*
  2. * Copyright (c) 2012 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdatomic.h>
  21. #include "frame_thread_encoder.h"
  22. #include "libavutil/fifo.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/thread.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "thread.h"
  30. #define MAX_THREADS 64
  31. /* There can be as many as MAX_THREADS + 1 outstanding tasks.
  32. * An additional + 1 is needed so that one can distinguish
  33. * the case of zero and MAX_THREADS + 1 outstanding tasks modulo
  34. * the number of buffers. */
  35. #define BUFFER_SIZE (MAX_THREADS + 2)
  36. typedef struct{
  37. AVFrame *indata;
  38. AVPacket *outdata;
  39. int64_t return_code;
  40. int finished;
  41. } Task;
  42. typedef struct{
  43. AVCodecContext *parent_avctx;
  44. pthread_mutex_t buffer_mutex;
  45. AVFifoBuffer *task_fifo;
  46. pthread_mutex_t task_fifo_mutex;
  47. pthread_cond_t task_fifo_cond;
  48. unsigned max_tasks;
  49. Task tasks[BUFFER_SIZE];
  50. pthread_mutex_t finished_task_mutex; /* Guards tasks[i].finished */
  51. pthread_cond_t finished_task_cond;
  52. unsigned task_index;
  53. unsigned finished_task_index;
  54. pthread_t worker[MAX_THREADS];
  55. atomic_int exit;
  56. } ThreadContext;
  57. static void * attribute_align_arg worker(void *v){
  58. AVCodecContext *avctx = v;
  59. ThreadContext *c = avctx->internal->frame_thread_encoder;
  60. while (!atomic_load(&c->exit)) {
  61. int got_packet = 0, ret;
  62. AVPacket *pkt;
  63. AVFrame *frame;
  64. Task *task;
  65. unsigned task_index;
  66. pthread_mutex_lock(&c->task_fifo_mutex);
  67. while (av_fifo_size(c->task_fifo) <= 0 || atomic_load(&c->exit)) {
  68. if (atomic_load(&c->exit)) {
  69. pthread_mutex_unlock(&c->task_fifo_mutex);
  70. goto end;
  71. }
  72. pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
  73. }
  74. av_fifo_generic_read(c->task_fifo, &task_index, sizeof(task_index), NULL);
  75. pthread_mutex_unlock(&c->task_fifo_mutex);
  76. /* The main thread ensures that any two outstanding tasks have
  77. * different indices, ergo each worker thread owns its element
  78. * of c->tasks with the exception of finished, which is shared
  79. * with the main thread and guarded by finished_task_mutex. */
  80. task = &c->tasks[task_index];
  81. frame = task->indata;
  82. pkt = task->outdata;
  83. ret = avctx->codec->encode2(avctx, pkt, frame, &got_packet);
  84. if(got_packet) {
  85. int ret2 = av_packet_make_refcounted(pkt);
  86. if (ret >= 0 && ret2 < 0)
  87. ret = ret2;
  88. pkt->pts = pkt->dts = frame->pts;
  89. } else {
  90. pkt->data = NULL;
  91. pkt->size = 0;
  92. }
  93. pthread_mutex_lock(&c->buffer_mutex);
  94. av_frame_unref(frame);
  95. pthread_mutex_unlock(&c->buffer_mutex);
  96. pthread_mutex_lock(&c->finished_task_mutex);
  97. task->return_code = ret;
  98. task->finished = 1;
  99. pthread_cond_signal(&c->finished_task_cond);
  100. pthread_mutex_unlock(&c->finished_task_mutex);
  101. }
  102. end:
  103. pthread_mutex_lock(&c->buffer_mutex);
  104. avcodec_close(avctx);
  105. pthread_mutex_unlock(&c->buffer_mutex);
  106. av_freep(&avctx);
  107. return NULL;
  108. }
  109. int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){
  110. int i=0;
  111. ThreadContext *c;
  112. if( !(avctx->thread_type & FF_THREAD_FRAME)
  113. || !(avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS))
  114. return 0;
  115. if( !avctx->thread_count
  116. && avctx->codec_id == AV_CODEC_ID_MJPEG
  117. && !(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
  118. av_log(avctx, AV_LOG_DEBUG,
  119. "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice "
  120. "or a constant quantizer if you want to use multiple cpu cores\n");
  121. avctx->thread_count = 1;
  122. }
  123. if( avctx->thread_count > 1
  124. && avctx->codec_id == AV_CODEC_ID_MJPEG
  125. && !(avctx->flags & AV_CODEC_FLAG_QSCALE))
  126. av_log(avctx, AV_LOG_WARNING,
  127. "MJPEG CBR encoding works badly with frame multi-threading, consider "
  128. "using -threads 1, -thread_type slice or a constant quantizer.\n");
  129. if (avctx->codec_id == AV_CODEC_ID_HUFFYUV ||
  130. avctx->codec_id == AV_CODEC_ID_FFVHUFF) {
  131. int warn = 0;
  132. int context_model = 0;
  133. AVDictionaryEntry *con = av_dict_get(options, "context", NULL, AV_DICT_MATCH_CASE);
  134. if (con && con->value)
  135. context_model = atoi(con->value);
  136. if (avctx->flags & AV_CODEC_FLAG_PASS1)
  137. warn = 1;
  138. else if(context_model > 0) {
  139. AVDictionaryEntry *t = av_dict_get(options, "non_deterministic",
  140. NULL, AV_DICT_MATCH_CASE);
  141. warn = !t || !t->value || !atoi(t->value) ? 1 : 0;
  142. }
  143. // huffyuv does not support these with multiple frame threads currently
  144. if (warn) {
  145. av_log(avctx, AV_LOG_WARNING,
  146. "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n");
  147. avctx->thread_count = 1;
  148. }
  149. }
  150. if(!avctx->thread_count) {
  151. avctx->thread_count = av_cpu_count();
  152. avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS);
  153. }
  154. if(avctx->thread_count <= 1)
  155. return 0;
  156. if(avctx->thread_count > MAX_THREADS)
  157. return AVERROR(EINVAL);
  158. av_assert0(!avctx->internal->frame_thread_encoder);
  159. c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext));
  160. if(!c)
  161. return AVERROR(ENOMEM);
  162. c->parent_avctx = avctx;
  163. c->task_fifo = av_fifo_alloc_array(BUFFER_SIZE, sizeof(unsigned));
  164. if (!c->task_fifo) {
  165. av_freep(&avctx->internal->frame_thread_encoder);
  166. return AVERROR(ENOMEM);
  167. }
  168. pthread_mutex_init(&c->task_fifo_mutex, NULL);
  169. pthread_mutex_init(&c->finished_task_mutex, NULL);
  170. pthread_mutex_init(&c->buffer_mutex, NULL);
  171. pthread_cond_init(&c->task_fifo_cond, NULL);
  172. pthread_cond_init(&c->finished_task_cond, NULL);
  173. atomic_init(&c->exit, 0);
  174. c->max_tasks = avctx->thread_count + 2;
  175. for (unsigned i = 0; i < c->max_tasks; i++) {
  176. if (!(c->tasks[i].indata = av_frame_alloc()) ||
  177. !(c->tasks[i].outdata = av_packet_alloc()))
  178. goto fail;
  179. }
  180. for(i=0; i<avctx->thread_count ; i++){
  181. AVDictionary *tmp = NULL;
  182. int ret;
  183. void *tmpv;
  184. AVCodecContext *thread_avctx = avcodec_alloc_context3(avctx->codec);
  185. if(!thread_avctx)
  186. goto fail;
  187. tmpv = thread_avctx->priv_data;
  188. *thread_avctx = *avctx;
  189. ret = av_opt_copy(thread_avctx, avctx);
  190. if (ret < 0)
  191. goto fail;
  192. thread_avctx->priv_data = tmpv;
  193. thread_avctx->internal = NULL;
  194. if (avctx->codec->priv_class) {
  195. int ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data);
  196. if (ret < 0)
  197. goto fail;
  198. } else if (avctx->codec->priv_data_size) {
  199. memcpy(thread_avctx->priv_data, avctx->priv_data, avctx->codec->priv_data_size);
  200. }
  201. thread_avctx->thread_count = 1;
  202. thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
  203. av_dict_copy(&tmp, options, 0);
  204. av_dict_set(&tmp, "threads", "1", 0);
  205. if(avcodec_open2(thread_avctx, avctx->codec, &tmp) < 0) {
  206. av_dict_free(&tmp);
  207. goto fail;
  208. }
  209. av_dict_free(&tmp);
  210. av_assert0(!thread_avctx->internal->frame_thread_encoder);
  211. thread_avctx->internal->frame_thread_encoder = c;
  212. if(pthread_create(&c->worker[i], NULL, worker, thread_avctx)) {
  213. goto fail;
  214. }
  215. }
  216. avctx->active_thread_type = FF_THREAD_FRAME;
  217. return 0;
  218. fail:
  219. avctx->thread_count = i;
  220. av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
  221. ff_frame_thread_encoder_free(avctx);
  222. return -1;
  223. }
  224. void ff_frame_thread_encoder_free(AVCodecContext *avctx){
  225. int i;
  226. ThreadContext *c= avctx->internal->frame_thread_encoder;
  227. pthread_mutex_lock(&c->task_fifo_mutex);
  228. atomic_store(&c->exit, 1);
  229. pthread_cond_broadcast(&c->task_fifo_cond);
  230. pthread_mutex_unlock(&c->task_fifo_mutex);
  231. for (i=0; i<avctx->thread_count; i++) {
  232. pthread_join(c->worker[i], NULL);
  233. }
  234. for (unsigned i = 0; i < c->max_tasks; i++) {
  235. av_frame_free(&c->tasks[i].indata);
  236. av_packet_free(&c->tasks[i].outdata);
  237. }
  238. pthread_mutex_destroy(&c->task_fifo_mutex);
  239. pthread_mutex_destroy(&c->finished_task_mutex);
  240. pthread_mutex_destroy(&c->buffer_mutex);
  241. pthread_cond_destroy(&c->task_fifo_cond);
  242. pthread_cond_destroy(&c->finished_task_cond);
  243. av_fifo_freep(&c->task_fifo);
  244. av_freep(&avctx->internal->frame_thread_encoder);
  245. }
  246. int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  247. AVFrame *frame, int *got_packet_ptr)
  248. {
  249. ThreadContext *c = avctx->internal->frame_thread_encoder;
  250. Task *outtask;
  251. av_assert1(!*got_packet_ptr);
  252. if(frame){
  253. av_frame_move_ref(c->tasks[c->task_index].indata, frame);
  254. pthread_mutex_lock(&c->task_fifo_mutex);
  255. av_fifo_generic_write(c->task_fifo, &c->task_index,
  256. sizeof(c->task_index), NULL);
  257. pthread_cond_signal(&c->task_fifo_cond);
  258. pthread_mutex_unlock(&c->task_fifo_mutex);
  259. c->task_index = (c->task_index + 1) % c->max_tasks;
  260. }
  261. outtask = &c->tasks[c->finished_task_index];
  262. pthread_mutex_lock(&c->finished_task_mutex);
  263. if (c->task_index == c->finished_task_index ||
  264. (frame && !outtask->finished &&
  265. (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) {
  266. pthread_mutex_unlock(&c->finished_task_mutex);
  267. return 0;
  268. }
  269. while (!outtask->finished) {
  270. pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
  271. }
  272. /* We now own outtask completely: No worker thread touches it any more,
  273. * because there is no outstanding task with this index. */
  274. outtask->finished = 0;
  275. av_packet_move_ref(pkt, outtask->outdata);
  276. if(pkt->data)
  277. *got_packet_ptr = 1;
  278. c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks;
  279. pthread_mutex_unlock(&c->finished_task_mutex);
  280. return outtask->return_code;
  281. }