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.

319 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/avassert.h"
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/thread.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. #include "thread.h"
  29. #define MAX_THREADS 64
  30. /* There can be as many as MAX_THREADS + 1 outstanding tasks.
  31. * An additional + 1 is needed so that one can distinguish
  32. * the case of zero and MAX_THREADS + 1 outstanding tasks modulo
  33. * the number of buffers. */
  34. #define BUFFER_SIZE (MAX_THREADS + 2)
  35. typedef struct{
  36. AVFrame *indata;
  37. AVPacket *outdata;
  38. int return_code;
  39. int finished;
  40. } Task;
  41. typedef struct{
  42. AVCodecContext *parent_avctx;
  43. pthread_mutex_t buffer_mutex;
  44. pthread_mutex_t task_fifo_mutex; /* Used to guard (next_)task_index */
  45. pthread_cond_t task_fifo_cond;
  46. unsigned max_tasks;
  47. Task tasks[BUFFER_SIZE];
  48. pthread_mutex_t finished_task_mutex; /* Guards tasks[i].finished */
  49. pthread_cond_t finished_task_cond;
  50. unsigned next_task_index;
  51. unsigned task_index;
  52. unsigned finished_task_index;
  53. pthread_t worker[MAX_THREADS];
  54. atomic_int exit;
  55. } ThreadContext;
  56. static void * attribute_align_arg worker(void *v){
  57. AVCodecContext *avctx = v;
  58. ThreadContext *c = avctx->internal->frame_thread_encoder;
  59. while (!atomic_load(&c->exit)) {
  60. int got_packet = 0, ret;
  61. AVPacket *pkt;
  62. AVFrame *frame;
  63. Task *task;
  64. unsigned task_index;
  65. pthread_mutex_lock(&c->task_fifo_mutex);
  66. while (c->next_task_index == c->task_index || atomic_load(&c->exit)) {
  67. if (atomic_load(&c->exit)) {
  68. pthread_mutex_unlock(&c->task_fifo_mutex);
  69. goto end;
  70. }
  71. pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
  72. }
  73. task_index = c->next_task_index;
  74. c->next_task_index = (c->next_task_index + 1) % c->max_tasks;
  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. pthread_mutex_init(&c->task_fifo_mutex, NULL);
  164. pthread_mutex_init(&c->finished_task_mutex, NULL);
  165. pthread_mutex_init(&c->buffer_mutex, NULL);
  166. pthread_cond_init(&c->task_fifo_cond, NULL);
  167. pthread_cond_init(&c->finished_task_cond, NULL);
  168. atomic_init(&c->exit, 0);
  169. c->max_tasks = avctx->thread_count + 2;
  170. for (unsigned i = 0; i < c->max_tasks; i++) {
  171. if (!(c->tasks[i].indata = av_frame_alloc()) ||
  172. !(c->tasks[i].outdata = av_packet_alloc()))
  173. goto fail;
  174. }
  175. for(i=0; i<avctx->thread_count ; i++){
  176. AVDictionary *tmp = NULL;
  177. int ret;
  178. void *tmpv;
  179. AVCodecContext *thread_avctx = avcodec_alloc_context3(avctx->codec);
  180. if(!thread_avctx)
  181. goto fail;
  182. tmpv = thread_avctx->priv_data;
  183. *thread_avctx = *avctx;
  184. ret = av_opt_copy(thread_avctx, avctx);
  185. if (ret < 0)
  186. goto fail;
  187. thread_avctx->priv_data = tmpv;
  188. thread_avctx->internal = NULL;
  189. if (avctx->codec->priv_class) {
  190. int ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data);
  191. if (ret < 0)
  192. goto fail;
  193. } else if (avctx->codec->priv_data_size) {
  194. memcpy(thread_avctx->priv_data, avctx->priv_data, avctx->codec->priv_data_size);
  195. }
  196. thread_avctx->thread_count = 1;
  197. thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
  198. av_dict_copy(&tmp, options, 0);
  199. av_dict_set(&tmp, "threads", "1", 0);
  200. if(avcodec_open2(thread_avctx, avctx->codec, &tmp) < 0) {
  201. av_dict_free(&tmp);
  202. goto fail;
  203. }
  204. av_dict_free(&tmp);
  205. av_assert0(!thread_avctx->internal->frame_thread_encoder);
  206. thread_avctx->internal->frame_thread_encoder = c;
  207. if(pthread_create(&c->worker[i], NULL, worker, thread_avctx)) {
  208. goto fail;
  209. }
  210. }
  211. avctx->active_thread_type = FF_THREAD_FRAME;
  212. return 0;
  213. fail:
  214. avctx->thread_count = i;
  215. av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
  216. ff_frame_thread_encoder_free(avctx);
  217. return -1;
  218. }
  219. void ff_frame_thread_encoder_free(AVCodecContext *avctx){
  220. int i;
  221. ThreadContext *c= avctx->internal->frame_thread_encoder;
  222. pthread_mutex_lock(&c->task_fifo_mutex);
  223. atomic_store(&c->exit, 1);
  224. pthread_cond_broadcast(&c->task_fifo_cond);
  225. pthread_mutex_unlock(&c->task_fifo_mutex);
  226. for (i=0; i<avctx->thread_count; i++) {
  227. pthread_join(c->worker[i], NULL);
  228. }
  229. for (unsigned i = 0; i < c->max_tasks; i++) {
  230. av_frame_free(&c->tasks[i].indata);
  231. av_packet_free(&c->tasks[i].outdata);
  232. }
  233. pthread_mutex_destroy(&c->task_fifo_mutex);
  234. pthread_mutex_destroy(&c->finished_task_mutex);
  235. pthread_mutex_destroy(&c->buffer_mutex);
  236. pthread_cond_destroy(&c->task_fifo_cond);
  237. pthread_cond_destroy(&c->finished_task_cond);
  238. av_freep(&avctx->internal->frame_thread_encoder);
  239. }
  240. int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  241. AVFrame *frame, int *got_packet_ptr)
  242. {
  243. ThreadContext *c = avctx->internal->frame_thread_encoder;
  244. Task *outtask;
  245. av_assert1(!*got_packet_ptr);
  246. if(frame){
  247. av_frame_move_ref(c->tasks[c->task_index].indata, frame);
  248. pthread_mutex_lock(&c->task_fifo_mutex);
  249. c->task_index = (c->task_index + 1) % c->max_tasks;
  250. pthread_cond_signal(&c->task_fifo_cond);
  251. pthread_mutex_unlock(&c->task_fifo_mutex);
  252. }
  253. outtask = &c->tasks[c->finished_task_index];
  254. pthread_mutex_lock(&c->finished_task_mutex);
  255. /* The access to task_index in the following code is ok,
  256. * because it is only ever changed by the main thread. */
  257. if (c->task_index == c->finished_task_index ||
  258. (frame && !outtask->finished &&
  259. (c->task_index - c->finished_task_index + c->max_tasks) % c->max_tasks <= avctx->thread_count)) {
  260. pthread_mutex_unlock(&c->finished_task_mutex);
  261. return 0;
  262. }
  263. while (!outtask->finished) {
  264. pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
  265. }
  266. pthread_mutex_unlock(&c->finished_task_mutex);
  267. /* We now own outtask completely: No worker thread touches it any more,
  268. * because there is no outstanding task with this index. */
  269. outtask->finished = 0;
  270. av_packet_move_ref(pkt, outtask->outdata);
  271. if(pkt->data)
  272. *got_packet_ptr = 1;
  273. c->finished_task_index = (c->finished_task_index + 1) % c->max_tasks;
  274. return outtask->return_code;
  275. }