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.

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