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.

307 lines
10.0KB

  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. #define BUFFER_SIZE (2*MAX_THREADS)
  32. typedef struct{
  33. void *indata;
  34. void *outdata;
  35. int64_t return_code;
  36. unsigned index;
  37. } Task;
  38. typedef struct{
  39. AVCodecContext *parent_avctx;
  40. pthread_mutex_t buffer_mutex;
  41. AVFifoBuffer *task_fifo;
  42. pthread_mutex_t task_fifo_mutex;
  43. pthread_cond_t task_fifo_cond;
  44. Task finished_tasks[BUFFER_SIZE];
  45. pthread_mutex_t finished_task_mutex;
  46. pthread_cond_t finished_task_cond;
  47. unsigned task_index;
  48. unsigned finished_task_index;
  49. pthread_t worker[MAX_THREADS];
  50. atomic_int exit;
  51. } ThreadContext;
  52. static void * attribute_align_arg worker(void *v){
  53. AVCodecContext *avctx = v;
  54. ThreadContext *c = avctx->internal->frame_thread_encoder;
  55. AVPacket *pkt = NULL;
  56. while (!atomic_load(&c->exit)) {
  57. int got_packet, ret;
  58. AVFrame *frame;
  59. Task task;
  60. if(!pkt) pkt= av_mallocz(sizeof(*pkt));
  61. if(!pkt) continue;
  62. av_init_packet(pkt);
  63. pthread_mutex_lock(&c->task_fifo_mutex);
  64. while (av_fifo_size(c->task_fifo) <= 0 || atomic_load(&c->exit)) {
  65. if (atomic_load(&c->exit)) {
  66. pthread_mutex_unlock(&c->task_fifo_mutex);
  67. goto end;
  68. }
  69. pthread_cond_wait(&c->task_fifo_cond, &c->task_fifo_mutex);
  70. }
  71. av_fifo_generic_read(c->task_fifo, &task, sizeof(task), NULL);
  72. pthread_mutex_unlock(&c->task_fifo_mutex);
  73. frame = task.indata;
  74. ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet);
  75. pthread_mutex_lock(&c->buffer_mutex);
  76. av_frame_unref(frame);
  77. pthread_mutex_unlock(&c->buffer_mutex);
  78. av_frame_free(&frame);
  79. if(got_packet) {
  80. int ret2 = av_dup_packet(pkt);
  81. if (ret >= 0 && ret2 < 0)
  82. ret = ret2;
  83. } else {
  84. pkt->data = NULL;
  85. pkt->size = 0;
  86. }
  87. pthread_mutex_lock(&c->finished_task_mutex);
  88. c->finished_tasks[task.index].outdata = pkt; pkt = NULL;
  89. c->finished_tasks[task.index].return_code = ret;
  90. pthread_cond_signal(&c->finished_task_cond);
  91. pthread_mutex_unlock(&c->finished_task_mutex);
  92. }
  93. end:
  94. av_free(pkt);
  95. pthread_mutex_lock(&c->buffer_mutex);
  96. avcodec_close(avctx);
  97. pthread_mutex_unlock(&c->buffer_mutex);
  98. av_freep(&avctx);
  99. return NULL;
  100. }
  101. int ff_frame_thread_encoder_init(AVCodecContext *avctx, AVDictionary *options){
  102. int i=0;
  103. ThreadContext *c;
  104. if( !(avctx->thread_type & FF_THREAD_FRAME)
  105. || !(avctx->codec->capabilities & AV_CODEC_CAP_INTRA_ONLY))
  106. return 0;
  107. if( !avctx->thread_count
  108. && avctx->codec_id == AV_CODEC_ID_MJPEG
  109. && !(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
  110. av_log(avctx, AV_LOG_DEBUG,
  111. "Forcing thread count to 1 for MJPEG encoding, use -thread_type slice "
  112. "or a constant quantizer if you want to use multiple cpu cores\n");
  113. avctx->thread_count = 1;
  114. }
  115. if( avctx->thread_count > 1
  116. && avctx->codec_id == AV_CODEC_ID_MJPEG
  117. && !(avctx->flags & AV_CODEC_FLAG_QSCALE))
  118. av_log(avctx, AV_LOG_WARNING,
  119. "MJPEG CBR encoding works badly with frame multi-threading, consider "
  120. "using -threads 1, -thread_type slice or a constant quantizer.\n");
  121. if (avctx->codec_id == AV_CODEC_ID_HUFFYUV ||
  122. avctx->codec_id == AV_CODEC_ID_FFVHUFF) {
  123. int warn = 0;
  124. int context_model = 0;
  125. AVDictionaryEntry *con = av_dict_get(options, "context", NULL, AV_DICT_MATCH_CASE);
  126. if (con && con->value)
  127. context_model = atoi(con->value);
  128. if (avctx->flags & AV_CODEC_FLAG_PASS1)
  129. warn = 1;
  130. else if(context_model > 0) {
  131. AVDictionaryEntry *t = av_dict_get(options, "non_deterministic",
  132. NULL, AV_DICT_MATCH_CASE);
  133. warn = !t || !t->value || !atoi(t->value) ? 1 : 0;
  134. }
  135. // huffyuv does not support these with multiple frame threads currently
  136. if (warn) {
  137. av_log(avctx, AV_LOG_WARNING,
  138. "Forcing thread count to 1 for huffyuv encoding with first pass or context 1\n");
  139. avctx->thread_count = 1;
  140. }
  141. }
  142. if(!avctx->thread_count) {
  143. avctx->thread_count = av_cpu_count();
  144. avctx->thread_count = FFMIN(avctx->thread_count, MAX_THREADS);
  145. }
  146. if(avctx->thread_count <= 1)
  147. return 0;
  148. if(avctx->thread_count > MAX_THREADS)
  149. return AVERROR(EINVAL);
  150. av_assert0(!avctx->internal->frame_thread_encoder);
  151. c = avctx->internal->frame_thread_encoder = av_mallocz(sizeof(ThreadContext));
  152. if(!c)
  153. return AVERROR(ENOMEM);
  154. c->parent_avctx = avctx;
  155. c->task_fifo = av_fifo_alloc_array(BUFFER_SIZE, sizeof(Task));
  156. if(!c->task_fifo)
  157. goto fail;
  158. pthread_mutex_init(&c->task_fifo_mutex, NULL);
  159. pthread_mutex_init(&c->finished_task_mutex, NULL);
  160. pthread_mutex_init(&c->buffer_mutex, NULL);
  161. pthread_cond_init(&c->task_fifo_cond, NULL);
  162. pthread_cond_init(&c->finished_task_cond, NULL);
  163. atomic_init(&c->exit, 0);
  164. for(i=0; i<avctx->thread_count ; i++){
  165. AVDictionary *tmp = NULL;
  166. void *tmpv;
  167. AVCodecContext *thread_avctx = avcodec_alloc_context3(avctx->codec);
  168. if(!thread_avctx)
  169. goto fail;
  170. tmpv = thread_avctx->priv_data;
  171. *thread_avctx = *avctx;
  172. thread_avctx->priv_data = tmpv;
  173. thread_avctx->internal = NULL;
  174. if (avctx->codec->priv_class) {
  175. int ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data);
  176. if (ret < 0)
  177. goto fail;
  178. } else
  179. memcpy(thread_avctx->priv_data, avctx->priv_data, avctx->codec->priv_data_size);
  180. thread_avctx->thread_count = 1;
  181. thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
  182. av_dict_copy(&tmp, options, 0);
  183. av_dict_set(&tmp, "threads", "1", 0);
  184. if(avcodec_open2(thread_avctx, avctx->codec, &tmp) < 0) {
  185. av_dict_free(&tmp);
  186. goto fail;
  187. }
  188. av_dict_free(&tmp);
  189. av_assert0(!thread_avctx->internal->frame_thread_encoder);
  190. thread_avctx->internal->frame_thread_encoder = c;
  191. if(pthread_create(&c->worker[i], NULL, worker, thread_avctx)) {
  192. goto fail;
  193. }
  194. }
  195. avctx->active_thread_type = FF_THREAD_FRAME;
  196. return 0;
  197. fail:
  198. avctx->thread_count = i;
  199. av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
  200. ff_frame_thread_encoder_free(avctx);
  201. return -1;
  202. }
  203. void ff_frame_thread_encoder_free(AVCodecContext *avctx){
  204. int i;
  205. ThreadContext *c= avctx->internal->frame_thread_encoder;
  206. pthread_mutex_lock(&c->task_fifo_mutex);
  207. atomic_store(&c->exit, 1);
  208. pthread_cond_broadcast(&c->task_fifo_cond);
  209. pthread_mutex_unlock(&c->task_fifo_mutex);
  210. for (i=0; i<avctx->thread_count; i++) {
  211. pthread_join(c->worker[i], NULL);
  212. }
  213. pthread_mutex_destroy(&c->task_fifo_mutex);
  214. pthread_mutex_destroy(&c->finished_task_mutex);
  215. pthread_mutex_destroy(&c->buffer_mutex);
  216. pthread_cond_destroy(&c->task_fifo_cond);
  217. pthread_cond_destroy(&c->finished_task_cond);
  218. av_fifo_freep(&c->task_fifo);
  219. av_freep(&avctx->internal->frame_thread_encoder);
  220. }
  221. int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr){
  222. ThreadContext *c = avctx->internal->frame_thread_encoder;
  223. Task task;
  224. int ret;
  225. av_assert1(!*got_packet_ptr);
  226. if(frame){
  227. AVFrame *new = av_frame_alloc();
  228. if(!new)
  229. return AVERROR(ENOMEM);
  230. ret = av_frame_ref(new, frame);
  231. if(ret < 0) {
  232. av_frame_free(&new);
  233. return ret;
  234. }
  235. task.index = c->task_index;
  236. task.indata = (void*)new;
  237. pthread_mutex_lock(&c->task_fifo_mutex);
  238. av_fifo_generic_write(c->task_fifo, &task, sizeof(task), NULL);
  239. pthread_cond_signal(&c->task_fifo_cond);
  240. pthread_mutex_unlock(&c->task_fifo_mutex);
  241. c->task_index = (c->task_index+1) % BUFFER_SIZE;
  242. }
  243. pthread_mutex_lock(&c->finished_task_mutex);
  244. if (c->task_index == c->finished_task_index ||
  245. (frame && !c->finished_tasks[c->finished_task_index].outdata &&
  246. (c->task_index - c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)) {
  247. pthread_mutex_unlock(&c->finished_task_mutex);
  248. return 0;
  249. }
  250. while (!c->finished_tasks[c->finished_task_index].outdata) {
  251. pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
  252. }
  253. task = c->finished_tasks[c->finished_task_index];
  254. *pkt = *(AVPacket*)(task.outdata);
  255. if(pkt->data)
  256. *got_packet_ptr = 1;
  257. av_freep(&c->finished_tasks[c->finished_task_index].outdata);
  258. c->finished_task_index = (c->finished_task_index+1) % BUFFER_SIZE;
  259. pthread_mutex_unlock(&c->finished_task_mutex);
  260. return task.return_code;
  261. }