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.

310 lines
10KB

  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. int ret = av_opt_copy(thread_avctx, avctx);
  173. if (ret < 0)
  174. goto fail;
  175. thread_avctx->priv_data = tmpv;
  176. thread_avctx->internal = NULL;
  177. if (avctx->codec->priv_class) {
  178. int ret = av_opt_copy(thread_avctx->priv_data, avctx->priv_data);
  179. if (ret < 0)
  180. goto fail;
  181. } else
  182. memcpy(thread_avctx->priv_data, avctx->priv_data, avctx->codec->priv_data_size);
  183. thread_avctx->thread_count = 1;
  184. thread_avctx->active_thread_type &= ~FF_THREAD_FRAME;
  185. av_dict_copy(&tmp, options, 0);
  186. av_dict_set(&tmp, "threads", "1", 0);
  187. if(avcodec_open2(thread_avctx, avctx->codec, &tmp) < 0) {
  188. av_dict_free(&tmp);
  189. goto fail;
  190. }
  191. av_dict_free(&tmp);
  192. av_assert0(!thread_avctx->internal->frame_thread_encoder);
  193. thread_avctx->internal->frame_thread_encoder = c;
  194. if(pthread_create(&c->worker[i], NULL, worker, thread_avctx)) {
  195. goto fail;
  196. }
  197. }
  198. avctx->active_thread_type = FF_THREAD_FRAME;
  199. return 0;
  200. fail:
  201. avctx->thread_count = i;
  202. av_log(avctx, AV_LOG_ERROR, "ff_frame_thread_encoder_init failed\n");
  203. ff_frame_thread_encoder_free(avctx);
  204. return -1;
  205. }
  206. void ff_frame_thread_encoder_free(AVCodecContext *avctx){
  207. int i;
  208. ThreadContext *c= avctx->internal->frame_thread_encoder;
  209. pthread_mutex_lock(&c->task_fifo_mutex);
  210. atomic_store(&c->exit, 1);
  211. pthread_cond_broadcast(&c->task_fifo_cond);
  212. pthread_mutex_unlock(&c->task_fifo_mutex);
  213. for (i=0; i<avctx->thread_count; i++) {
  214. pthread_join(c->worker[i], NULL);
  215. }
  216. pthread_mutex_destroy(&c->task_fifo_mutex);
  217. pthread_mutex_destroy(&c->finished_task_mutex);
  218. pthread_mutex_destroy(&c->buffer_mutex);
  219. pthread_cond_destroy(&c->task_fifo_cond);
  220. pthread_cond_destroy(&c->finished_task_cond);
  221. av_fifo_freep(&c->task_fifo);
  222. av_freep(&avctx->internal->frame_thread_encoder);
  223. }
  224. int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet_ptr){
  225. ThreadContext *c = avctx->internal->frame_thread_encoder;
  226. Task task;
  227. int ret;
  228. av_assert1(!*got_packet_ptr);
  229. if(frame){
  230. AVFrame *new = av_frame_alloc();
  231. if(!new)
  232. return AVERROR(ENOMEM);
  233. ret = av_frame_ref(new, frame);
  234. if(ret < 0) {
  235. av_frame_free(&new);
  236. return ret;
  237. }
  238. task.index = c->task_index;
  239. task.indata = (void*)new;
  240. pthread_mutex_lock(&c->task_fifo_mutex);
  241. av_fifo_generic_write(c->task_fifo, &task, sizeof(task), NULL);
  242. pthread_cond_signal(&c->task_fifo_cond);
  243. pthread_mutex_unlock(&c->task_fifo_mutex);
  244. c->task_index = (c->task_index+1) % BUFFER_SIZE;
  245. }
  246. pthread_mutex_lock(&c->finished_task_mutex);
  247. if (c->task_index == c->finished_task_index ||
  248. (frame && !c->finished_tasks[c->finished_task_index].outdata &&
  249. (c->task_index - c->finished_task_index) % BUFFER_SIZE <= avctx->thread_count)) {
  250. pthread_mutex_unlock(&c->finished_task_mutex);
  251. return 0;
  252. }
  253. while (!c->finished_tasks[c->finished_task_index].outdata) {
  254. pthread_cond_wait(&c->finished_task_cond, &c->finished_task_mutex);
  255. }
  256. task = c->finished_tasks[c->finished_task_index];
  257. *pkt = *(AVPacket*)(task.outdata);
  258. if(pkt->data)
  259. *got_packet_ptr = 1;
  260. av_freep(&c->finished_tasks[c->finished_task_index].outdata);
  261. c->finished_task_index = (c->finished_task_index+1) % BUFFER_SIZE;
  262. pthread_mutex_unlock(&c->finished_task_mutex);
  263. return task.return_code;
  264. }