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.

802 lines
25KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Frame multithreading support functions
  21. * @see doc/multithreading.txt
  22. */
  23. #include "config.h"
  24. #include <stdint.h>
  25. #if HAVE_PTHREADS
  26. #include <pthread.h>
  27. #elif HAVE_W32THREADS
  28. #include "compat/w32pthreads.h"
  29. #endif
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. #include "pthread_internal.h"
  33. #include "thread.h"
  34. #include "version.h"
  35. #include "libavutil/avassert.h"
  36. #include "libavutil/buffer.h"
  37. #include "libavutil/common.h"
  38. #include "libavutil/cpu.h"
  39. #include "libavutil/frame.h"
  40. #include "libavutil/internal.h"
  41. #include "libavutil/log.h"
  42. #include "libavutil/mem.h"
  43. /**
  44. * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
  45. */
  46. typedef struct PerThreadContext {
  47. struct FrameThreadContext *parent;
  48. pthread_t thread;
  49. int thread_init;
  50. pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
  51. pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
  52. pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
  53. pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
  54. pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
  55. AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
  56. AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
  57. AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
  58. int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
  59. int result; ///< The result of the last codec decode/encode() call.
  60. enum {
  61. STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
  62. STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
  63. STATE_GET_BUFFER, /**<
  64. * Set when the codec calls get_buffer().
  65. * State is returned to STATE_SETTING_UP afterwards.
  66. */
  67. STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
  68. } state;
  69. /**
  70. * Array of frames passed to ff_thread_release_buffer().
  71. * Frames are released after all threads referencing them are finished.
  72. */
  73. AVFrame *released_buffers;
  74. int num_released_buffers;
  75. int released_buffers_allocated;
  76. AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
  77. int requested_flags; ///< flags passed to get_buffer() for requested_frame
  78. } PerThreadContext;
  79. /**
  80. * Context stored in the client AVCodecInternal thread_ctx.
  81. */
  82. typedef struct FrameThreadContext {
  83. PerThreadContext *threads; ///< The contexts for each thread.
  84. PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
  85. pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
  86. int next_decoding; ///< The next context to submit a packet to.
  87. int next_finished; ///< The next context to return output from.
  88. int delaying; /**<
  89. * Set for the first N packets, where N is the number of threads.
  90. * While it is set, ff_thread_en/decode_frame won't return any results.
  91. */
  92. int die; ///< Set when threads should exit.
  93. } FrameThreadContext;
  94. /**
  95. * Codec worker thread.
  96. *
  97. * Automatically calls ff_thread_finish_setup() if the codec does
  98. * not provide an update_thread_context method, or if the codec returns
  99. * before calling it.
  100. */
  101. static attribute_align_arg void *frame_worker_thread(void *arg)
  102. {
  103. PerThreadContext *p = arg;
  104. FrameThreadContext *fctx = p->parent;
  105. AVCodecContext *avctx = p->avctx;
  106. const AVCodec *codec = avctx->codec;
  107. while (1) {
  108. if (p->state == STATE_INPUT_READY && !fctx->die) {
  109. pthread_mutex_lock(&p->mutex);
  110. while (p->state == STATE_INPUT_READY && !fctx->die)
  111. pthread_cond_wait(&p->input_cond, &p->mutex);
  112. pthread_mutex_unlock(&p->mutex);
  113. }
  114. if (fctx->die) break;
  115. if (!codec->update_thread_context && avctx->thread_safe_callbacks)
  116. ff_thread_finish_setup(avctx);
  117. pthread_mutex_lock(&p->mutex);
  118. av_frame_unref(p->frame);
  119. p->got_frame = 0;
  120. p->result = codec->decode(avctx, p->frame, &p->got_frame, &p->avpkt);
  121. if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
  122. if (avctx->internal->allocate_progress)
  123. av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
  124. "free the frame on failure. This is a bug, please report it.\n");
  125. av_frame_unref(p->frame);
  126. }
  127. if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
  128. p->state = STATE_INPUT_READY;
  129. pthread_mutex_lock(&p->progress_mutex);
  130. pthread_cond_signal(&p->output_cond);
  131. pthread_mutex_unlock(&p->progress_mutex);
  132. pthread_mutex_unlock(&p->mutex);
  133. }
  134. return NULL;
  135. }
  136. /**
  137. * Update the next thread's AVCodecContext with values from the reference thread's context.
  138. *
  139. * @param dst The destination context.
  140. * @param src The source context.
  141. * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
  142. */
  143. static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
  144. {
  145. int err = 0;
  146. if (dst != src) {
  147. dst->time_base = src->time_base;
  148. dst->framerate = src->framerate;
  149. dst->width = src->width;
  150. dst->height = src->height;
  151. dst->pix_fmt = src->pix_fmt;
  152. dst->coded_width = src->coded_width;
  153. dst->coded_height = src->coded_height;
  154. dst->has_b_frames = src->has_b_frames;
  155. dst->idct_algo = src->idct_algo;
  156. dst->bits_per_coded_sample = src->bits_per_coded_sample;
  157. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  158. #if FF_API_AFD
  159. FF_DISABLE_DEPRECATION_WARNINGS
  160. dst->dtg_active_format = src->dtg_active_format;
  161. FF_ENABLE_DEPRECATION_WARNINGS
  162. #endif /* FF_API_AFD */
  163. dst->profile = src->profile;
  164. dst->level = src->level;
  165. dst->bits_per_raw_sample = src->bits_per_raw_sample;
  166. dst->ticks_per_frame = src->ticks_per_frame;
  167. dst->color_primaries = src->color_primaries;
  168. dst->color_trc = src->color_trc;
  169. dst->colorspace = src->colorspace;
  170. dst->color_range = src->color_range;
  171. dst->chroma_sample_location = src->chroma_sample_location;
  172. dst->hwaccel = src->hwaccel;
  173. dst->hwaccel_context = src->hwaccel_context;
  174. dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
  175. }
  176. if (for_user) {
  177. dst->coded_frame = src->coded_frame;
  178. } else {
  179. if (dst->codec->update_thread_context)
  180. err = dst->codec->update_thread_context(dst, src);
  181. }
  182. return err;
  183. }
  184. /**
  185. * Update the next thread's AVCodecContext with values set by the user.
  186. *
  187. * @param dst The destination context.
  188. * @param src The source context.
  189. * @return 0 on success, negative error code on failure
  190. */
  191. static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
  192. {
  193. #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
  194. dst->flags = src->flags;
  195. dst->draw_horiz_band= src->draw_horiz_band;
  196. dst->get_buffer2 = src->get_buffer2;
  197. #if FF_API_GET_BUFFER
  198. FF_DISABLE_DEPRECATION_WARNINGS
  199. dst->get_buffer = src->get_buffer;
  200. dst->release_buffer = src->release_buffer;
  201. FF_ENABLE_DEPRECATION_WARNINGS
  202. #endif
  203. dst->opaque = src->opaque;
  204. dst->debug = src->debug;
  205. dst->slice_flags = src->slice_flags;
  206. dst->flags2 = src->flags2;
  207. copy_fields(skip_loop_filter, subtitle_header);
  208. dst->frame_number = src->frame_number;
  209. dst->reordered_opaque = src->reordered_opaque;
  210. if (src->slice_count && src->slice_offset) {
  211. if (dst->slice_count < src->slice_count) {
  212. int *tmp = av_realloc(dst->slice_offset, src->slice_count *
  213. sizeof(*dst->slice_offset));
  214. if (!tmp) {
  215. av_free(dst->slice_offset);
  216. return AVERROR(ENOMEM);
  217. }
  218. dst->slice_offset = tmp;
  219. }
  220. memcpy(dst->slice_offset, src->slice_offset,
  221. src->slice_count * sizeof(*dst->slice_offset));
  222. }
  223. dst->slice_count = src->slice_count;
  224. return 0;
  225. #undef copy_fields
  226. }
  227. /// Releases the buffers that this decoding thread was the last user of.
  228. static void release_delayed_buffers(PerThreadContext *p)
  229. {
  230. FrameThreadContext *fctx = p->parent;
  231. while (p->num_released_buffers > 0) {
  232. AVFrame *f;
  233. pthread_mutex_lock(&fctx->buffer_mutex);
  234. // fix extended data in case the caller screwed it up
  235. av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  236. f = &p->released_buffers[--p->num_released_buffers];
  237. f->extended_data = f->data;
  238. av_frame_unref(f);
  239. pthread_mutex_unlock(&fctx->buffer_mutex);
  240. }
  241. }
  242. static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
  243. {
  244. FrameThreadContext *fctx = p->parent;
  245. PerThreadContext *prev_thread = fctx->prev_thread;
  246. const AVCodec *codec = p->avctx->codec;
  247. if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
  248. pthread_mutex_lock(&p->mutex);
  249. release_delayed_buffers(p);
  250. if (prev_thread) {
  251. int err;
  252. if (prev_thread->state == STATE_SETTING_UP) {
  253. pthread_mutex_lock(&prev_thread->progress_mutex);
  254. while (prev_thread->state == STATE_SETTING_UP)
  255. pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
  256. pthread_mutex_unlock(&prev_thread->progress_mutex);
  257. }
  258. err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
  259. if (err) {
  260. pthread_mutex_unlock(&p->mutex);
  261. return err;
  262. }
  263. }
  264. av_packet_unref(&p->avpkt);
  265. av_packet_ref(&p->avpkt, avpkt);
  266. p->state = STATE_SETTING_UP;
  267. pthread_cond_signal(&p->input_cond);
  268. pthread_mutex_unlock(&p->mutex);
  269. /*
  270. * If the client doesn't have a thread-safe get_buffer(),
  271. * then decoding threads call back to the main thread,
  272. * and it calls back to the client here.
  273. */
  274. FF_DISABLE_DEPRECATION_WARNINGS
  275. if (!p->avctx->thread_safe_callbacks && (
  276. #if FF_API_GET_BUFFER
  277. p->avctx->get_buffer ||
  278. #endif
  279. p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
  280. FF_ENABLE_DEPRECATION_WARNINGS
  281. while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
  282. pthread_mutex_lock(&p->progress_mutex);
  283. while (p->state == STATE_SETTING_UP)
  284. pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
  285. if (p->state == STATE_GET_BUFFER) {
  286. p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
  287. p->state = STATE_SETTING_UP;
  288. pthread_cond_signal(&p->progress_cond);
  289. }
  290. pthread_mutex_unlock(&p->progress_mutex);
  291. }
  292. }
  293. fctx->prev_thread = p;
  294. fctx->next_decoding++;
  295. return 0;
  296. }
  297. int ff_thread_decode_frame(AVCodecContext *avctx,
  298. AVFrame *picture, int *got_picture_ptr,
  299. AVPacket *avpkt)
  300. {
  301. FrameThreadContext *fctx = avctx->internal->thread_ctx;
  302. int finished = fctx->next_finished;
  303. PerThreadContext *p;
  304. int err;
  305. /*
  306. * Submit a packet to the next decoding thread.
  307. */
  308. p = &fctx->threads[fctx->next_decoding];
  309. err = update_context_from_user(p->avctx, avctx);
  310. if (err) return err;
  311. err = submit_packet(p, avpkt);
  312. if (err) return err;
  313. /*
  314. * If we're still receiving the initial packets, don't return a frame.
  315. */
  316. if (fctx->delaying) {
  317. if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
  318. *got_picture_ptr=0;
  319. if (avpkt->size)
  320. return avpkt->size;
  321. }
  322. /*
  323. * Return the next available frame from the oldest thread.
  324. * If we're at the end of the stream, then we have to skip threads that
  325. * didn't output a frame, because we don't want to accidentally signal
  326. * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
  327. */
  328. do {
  329. p = &fctx->threads[finished++];
  330. if (p->state != STATE_INPUT_READY) {
  331. pthread_mutex_lock(&p->progress_mutex);
  332. while (p->state != STATE_INPUT_READY)
  333. pthread_cond_wait(&p->output_cond, &p->progress_mutex);
  334. pthread_mutex_unlock(&p->progress_mutex);
  335. }
  336. av_frame_move_ref(picture, p->frame);
  337. *got_picture_ptr = p->got_frame;
  338. picture->pkt_dts = p->avpkt.dts;
  339. /*
  340. * A later call with avkpt->size == 0 may loop over all threads,
  341. * including this one, searching for a frame to return before being
  342. * stopped by the "finished != fctx->next_finished" condition.
  343. * Make sure we don't mistakenly return the same frame again.
  344. */
  345. p->got_frame = 0;
  346. if (finished >= avctx->thread_count) finished = 0;
  347. } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
  348. update_context_from_thread(avctx, p->avctx, 1);
  349. if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
  350. fctx->next_finished = finished;
  351. /* return the size of the consumed packet if no error occurred */
  352. return (p->result >= 0) ? avpkt->size : p->result;
  353. }
  354. void ff_thread_report_progress(ThreadFrame *f, int n, int field)
  355. {
  356. PerThreadContext *p;
  357. int *progress = f->progress ? (int*)f->progress->data : NULL;
  358. if (!progress || progress[field] >= n) return;
  359. p = f->owner->internal->thread_ctx;
  360. if (f->owner->debug&FF_DEBUG_THREADS)
  361. av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
  362. pthread_mutex_lock(&p->progress_mutex);
  363. progress[field] = n;
  364. pthread_cond_broadcast(&p->progress_cond);
  365. pthread_mutex_unlock(&p->progress_mutex);
  366. }
  367. void ff_thread_await_progress(ThreadFrame *f, int n, int field)
  368. {
  369. PerThreadContext *p;
  370. int *progress = f->progress ? (int*)f->progress->data : NULL;
  371. if (!progress || progress[field] >= n) return;
  372. p = f->owner->internal->thread_ctx;
  373. if (f->owner->debug&FF_DEBUG_THREADS)
  374. av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
  375. pthread_mutex_lock(&p->progress_mutex);
  376. while (progress[field] < n)
  377. pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
  378. pthread_mutex_unlock(&p->progress_mutex);
  379. }
  380. void ff_thread_finish_setup(AVCodecContext *avctx) {
  381. PerThreadContext *p = avctx->internal->thread_ctx;
  382. if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
  383. pthread_mutex_lock(&p->progress_mutex);
  384. p->state = STATE_SETUP_FINISHED;
  385. pthread_cond_broadcast(&p->progress_cond);
  386. pthread_mutex_unlock(&p->progress_mutex);
  387. }
  388. /// Waits for all threads to finish.
  389. static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
  390. {
  391. int i;
  392. for (i = 0; i < thread_count; i++) {
  393. PerThreadContext *p = &fctx->threads[i];
  394. if (p->state != STATE_INPUT_READY) {
  395. pthread_mutex_lock(&p->progress_mutex);
  396. while (p->state != STATE_INPUT_READY)
  397. pthread_cond_wait(&p->output_cond, &p->progress_mutex);
  398. pthread_mutex_unlock(&p->progress_mutex);
  399. }
  400. }
  401. }
  402. void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
  403. {
  404. FrameThreadContext *fctx = avctx->internal->thread_ctx;
  405. const AVCodec *codec = avctx->codec;
  406. int i;
  407. park_frame_worker_threads(fctx, thread_count);
  408. if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
  409. update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
  410. fctx->die = 1;
  411. for (i = 0; i < thread_count; i++) {
  412. PerThreadContext *p = &fctx->threads[i];
  413. pthread_mutex_lock(&p->mutex);
  414. pthread_cond_signal(&p->input_cond);
  415. pthread_mutex_unlock(&p->mutex);
  416. if (p->thread_init)
  417. pthread_join(p->thread, NULL);
  418. if (codec->close)
  419. codec->close(p->avctx);
  420. avctx->codec = NULL;
  421. release_delayed_buffers(p);
  422. av_frame_free(&p->frame);
  423. }
  424. for (i = 0; i < thread_count; i++) {
  425. PerThreadContext *p = &fctx->threads[i];
  426. pthread_mutex_destroy(&p->mutex);
  427. pthread_mutex_destroy(&p->progress_mutex);
  428. pthread_cond_destroy(&p->input_cond);
  429. pthread_cond_destroy(&p->progress_cond);
  430. pthread_cond_destroy(&p->output_cond);
  431. av_packet_unref(&p->avpkt);
  432. av_freep(&p->released_buffers);
  433. if (i) {
  434. av_freep(&p->avctx->priv_data);
  435. av_freep(&p->avctx->slice_offset);
  436. }
  437. av_freep(&p->avctx->internal);
  438. av_freep(&p->avctx);
  439. }
  440. av_freep(&fctx->threads);
  441. pthread_mutex_destroy(&fctx->buffer_mutex);
  442. av_freep(&avctx->internal->thread_ctx);
  443. }
  444. int ff_frame_thread_init(AVCodecContext *avctx)
  445. {
  446. int thread_count = avctx->thread_count;
  447. const AVCodec *codec = avctx->codec;
  448. AVCodecContext *src = avctx;
  449. FrameThreadContext *fctx;
  450. int i, err = 0;
  451. #if HAVE_W32THREADS
  452. w32thread_init();
  453. #endif
  454. if (!thread_count) {
  455. int nb_cpus = av_cpu_count();
  456. av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
  457. // use number of cores + 1 as thread count if there is more than one
  458. if (nb_cpus > 1)
  459. thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
  460. else
  461. thread_count = avctx->thread_count = 1;
  462. }
  463. if (thread_count <= 1) {
  464. avctx->active_thread_type = 0;
  465. return 0;
  466. }
  467. avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
  468. fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
  469. pthread_mutex_init(&fctx->buffer_mutex, NULL);
  470. fctx->delaying = 1;
  471. for (i = 0; i < thread_count; i++) {
  472. AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
  473. PerThreadContext *p = &fctx->threads[i];
  474. pthread_mutex_init(&p->mutex, NULL);
  475. pthread_mutex_init(&p->progress_mutex, NULL);
  476. pthread_cond_init(&p->input_cond, NULL);
  477. pthread_cond_init(&p->progress_cond, NULL);
  478. pthread_cond_init(&p->output_cond, NULL);
  479. p->frame = av_frame_alloc();
  480. if (!p->frame) {
  481. av_freep(&copy);
  482. err = AVERROR(ENOMEM);
  483. goto error;
  484. }
  485. p->parent = fctx;
  486. p->avctx = copy;
  487. if (!copy) {
  488. err = AVERROR(ENOMEM);
  489. goto error;
  490. }
  491. *copy = *src;
  492. copy->internal = av_malloc(sizeof(AVCodecInternal));
  493. if (!copy->internal) {
  494. err = AVERROR(ENOMEM);
  495. goto error;
  496. }
  497. *copy->internal = *src->internal;
  498. copy->internal->thread_ctx = p;
  499. copy->internal->pkt = &p->avpkt;
  500. if (!i) {
  501. src = copy;
  502. if (codec->init)
  503. err = codec->init(copy);
  504. update_context_from_thread(avctx, copy, 1);
  505. } else {
  506. copy->priv_data = av_malloc(codec->priv_data_size);
  507. if (!copy->priv_data) {
  508. err = AVERROR(ENOMEM);
  509. goto error;
  510. }
  511. memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
  512. copy->internal->is_copy = 1;
  513. if (codec->init_thread_copy)
  514. err = codec->init_thread_copy(copy);
  515. }
  516. if (err) goto error;
  517. if (!pthread_create(&p->thread, NULL, frame_worker_thread, p))
  518. p->thread_init = 1;
  519. }
  520. return 0;
  521. error:
  522. ff_frame_thread_free(avctx, i+1);
  523. return err;
  524. }
  525. void ff_thread_flush(AVCodecContext *avctx)
  526. {
  527. int i;
  528. FrameThreadContext *fctx = avctx->internal->thread_ctx;
  529. if (!fctx) return;
  530. park_frame_worker_threads(fctx, avctx->thread_count);
  531. if (fctx->prev_thread) {
  532. if (fctx->prev_thread != &fctx->threads[0])
  533. update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
  534. }
  535. fctx->next_decoding = fctx->next_finished = 0;
  536. fctx->delaying = 1;
  537. fctx->prev_thread = NULL;
  538. for (i = 0; i < avctx->thread_count; i++) {
  539. PerThreadContext *p = &fctx->threads[i];
  540. // Make sure decode flush calls with size=0 won't return old frames
  541. p->got_frame = 0;
  542. av_frame_unref(p->frame);
  543. release_delayed_buffers(p);
  544. if (avctx->codec->flush)
  545. avctx->codec->flush(p->avctx);
  546. }
  547. }
  548. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  549. {
  550. PerThreadContext *p = avctx->internal->thread_ctx;
  551. int err;
  552. f->owner = avctx;
  553. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  554. return ff_get_buffer(avctx, f->f, flags);
  555. if (p->state != STATE_SETTING_UP &&
  556. (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
  557. av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
  558. return -1;
  559. }
  560. if (avctx->internal->allocate_progress) {
  561. int *progress;
  562. f->progress = av_buffer_alloc(2 * sizeof(int));
  563. if (!f->progress) {
  564. return AVERROR(ENOMEM);
  565. }
  566. progress = (int*)f->progress->data;
  567. progress[0] = progress[1] = -1;
  568. }
  569. pthread_mutex_lock(&p->parent->buffer_mutex);
  570. FF_DISABLE_DEPRECATION_WARNINGS
  571. if (avctx->thread_safe_callbacks || (
  572. #if FF_API_GET_BUFFER
  573. !avctx->get_buffer &&
  574. #endif
  575. avctx->get_buffer2 == avcodec_default_get_buffer2)) {
  576. FF_ENABLE_DEPRECATION_WARNINGS
  577. err = ff_get_buffer(avctx, f->f, flags);
  578. } else {
  579. p->requested_frame = f->f;
  580. p->requested_flags = flags;
  581. p->state = STATE_GET_BUFFER;
  582. pthread_mutex_lock(&p->progress_mutex);
  583. pthread_cond_signal(&p->progress_cond);
  584. while (p->state != STATE_SETTING_UP)
  585. pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
  586. err = p->result;
  587. pthread_mutex_unlock(&p->progress_mutex);
  588. }
  589. if (!avctx->thread_safe_callbacks && !avctx->codec->update_thread_context)
  590. ff_thread_finish_setup(avctx);
  591. if (err)
  592. av_buffer_unref(&f->progress);
  593. pthread_mutex_unlock(&p->parent->buffer_mutex);
  594. return err;
  595. }
  596. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  597. {
  598. PerThreadContext *p = avctx->internal->thread_ctx;
  599. FrameThreadContext *fctx;
  600. AVFrame *dst, *tmp;
  601. FF_DISABLE_DEPRECATION_WARNINGS
  602. int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
  603. avctx->thread_safe_callbacks ||
  604. (
  605. #if FF_API_GET_BUFFER
  606. !avctx->get_buffer &&
  607. #endif
  608. avctx->get_buffer2 == avcodec_default_get_buffer2);
  609. FF_ENABLE_DEPRECATION_WARNINGS
  610. if (!f->f || !f->f->buf[0])
  611. return;
  612. if (avctx->debug & FF_DEBUG_BUFFERS)
  613. av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
  614. av_buffer_unref(&f->progress);
  615. f->owner = NULL;
  616. if (can_direct_free) {
  617. av_frame_unref(f->f);
  618. return;
  619. }
  620. fctx = p->parent;
  621. pthread_mutex_lock(&fctx->buffer_mutex);
  622. if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
  623. goto fail;
  624. tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
  625. (p->num_released_buffers + 1) *
  626. sizeof(*p->released_buffers));
  627. if (!tmp)
  628. goto fail;
  629. p->released_buffers = tmp;
  630. dst = &p->released_buffers[p->num_released_buffers];
  631. av_frame_move_ref(dst, f->f);
  632. p->num_released_buffers++;
  633. fail:
  634. pthread_mutex_unlock(&fctx->buffer_mutex);
  635. }