frame_thread_encoder.c 11 KB

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