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.

296 lines
9.6KB

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