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.

804 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 "libavutil/avassert.h"
  35. #include "libavutil/buffer.h"
  36. #include "libavutil/common.h"
  37. #include "libavutil/cpu.h"
  38. #include "libavutil/frame.h"
  39. #include "libavutil/log.h"
  40. #include "libavutil/mem.h"
  41. /**
  42. * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
  43. */
  44. typedef struct PerThreadContext {
  45. struct FrameThreadContext *parent;
  46. pthread_t thread;
  47. int thread_init;
  48. pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
  49. pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
  50. pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
  51. pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
  52. pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
  53. AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
  54. AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
  55. uint8_t *buf; ///< backup storage for packet data when the input packet is not refcounted
  56. int allocated_buf_size; ///< Size allocated for buf
  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->width = src->width;
  149. dst->height = src->height;
  150. dst->pix_fmt = src->pix_fmt;
  151. dst->coded_width = src->coded_width;
  152. dst->coded_height = src->coded_height;
  153. dst->has_b_frames = src->has_b_frames;
  154. dst->idct_algo = src->idct_algo;
  155. dst->bits_per_coded_sample = src->bits_per_coded_sample;
  156. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  157. dst->dtg_active_format = src->dtg_active_format;
  158. dst->profile = src->profile;
  159. dst->level = src->level;
  160. dst->bits_per_raw_sample = src->bits_per_raw_sample;
  161. dst->ticks_per_frame = src->ticks_per_frame;
  162. dst->color_primaries = src->color_primaries;
  163. dst->color_trc = src->color_trc;
  164. dst->colorspace = src->colorspace;
  165. dst->color_range = src->color_range;
  166. dst->chroma_sample_location = src->chroma_sample_location;
  167. dst->hwaccel = src->hwaccel;
  168. dst->hwaccel_context = src->hwaccel_context;
  169. }
  170. if (for_user) {
  171. dst->coded_frame = src->coded_frame;
  172. } else {
  173. if (dst->codec->update_thread_context)
  174. err = dst->codec->update_thread_context(dst, src);
  175. }
  176. return err;
  177. }
  178. /**
  179. * Update the next thread's AVCodecContext with values set by the user.
  180. *
  181. * @param dst The destination context.
  182. * @param src The source context.
  183. * @return 0 on success, negative error code on failure
  184. */
  185. static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
  186. {
  187. #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
  188. dst->flags = src->flags;
  189. dst->draw_horiz_band= src->draw_horiz_band;
  190. dst->get_buffer2 = src->get_buffer2;
  191. #if FF_API_GET_BUFFER
  192. FF_DISABLE_DEPRECATION_WARNINGS
  193. dst->get_buffer = src->get_buffer;
  194. dst->release_buffer = src->release_buffer;
  195. FF_ENABLE_DEPRECATION_WARNINGS
  196. #endif
  197. dst->opaque = src->opaque;
  198. dst->debug = src->debug;
  199. dst->slice_flags = src->slice_flags;
  200. dst->flags2 = src->flags2;
  201. copy_fields(skip_loop_filter, subtitle_header);
  202. dst->frame_number = src->frame_number;
  203. dst->reordered_opaque = src->reordered_opaque;
  204. if (src->slice_count && src->slice_offset) {
  205. if (dst->slice_count < src->slice_count) {
  206. int *tmp = av_realloc(dst->slice_offset, src->slice_count *
  207. sizeof(*dst->slice_offset));
  208. if (!tmp) {
  209. av_free(dst->slice_offset);
  210. return AVERROR(ENOMEM);
  211. }
  212. dst->slice_offset = tmp;
  213. }
  214. memcpy(dst->slice_offset, src->slice_offset,
  215. src->slice_count * sizeof(*dst->slice_offset));
  216. }
  217. dst->slice_count = src->slice_count;
  218. return 0;
  219. #undef copy_fields
  220. }
  221. /// Releases the buffers that this decoding thread was the last user of.
  222. static void release_delayed_buffers(PerThreadContext *p)
  223. {
  224. FrameThreadContext *fctx = p->parent;
  225. while (p->num_released_buffers > 0) {
  226. AVFrame *f;
  227. pthread_mutex_lock(&fctx->buffer_mutex);
  228. // fix extended data in case the caller screwed it up
  229. av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  230. f = &p->released_buffers[--p->num_released_buffers];
  231. f->extended_data = f->data;
  232. av_frame_unref(f);
  233. pthread_mutex_unlock(&fctx->buffer_mutex);
  234. }
  235. }
  236. static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
  237. {
  238. FrameThreadContext *fctx = p->parent;
  239. PerThreadContext *prev_thread = fctx->prev_thread;
  240. const AVCodec *codec = p->avctx->codec;
  241. if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
  242. pthread_mutex_lock(&p->mutex);
  243. release_delayed_buffers(p);
  244. if (prev_thread) {
  245. int err;
  246. if (prev_thread->state == STATE_SETTING_UP) {
  247. pthread_mutex_lock(&prev_thread->progress_mutex);
  248. while (prev_thread->state == STATE_SETTING_UP)
  249. pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
  250. pthread_mutex_unlock(&prev_thread->progress_mutex);
  251. }
  252. err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
  253. if (err) {
  254. pthread_mutex_unlock(&p->mutex);
  255. return err;
  256. }
  257. }
  258. av_buffer_unref(&p->avpkt.buf);
  259. p->avpkt = *avpkt;
  260. if (avpkt->buf)
  261. p->avpkt.buf = av_buffer_ref(avpkt->buf);
  262. else {
  263. av_fast_malloc(&p->buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  264. p->avpkt.data = p->buf;
  265. memcpy(p->buf, avpkt->data, avpkt->size);
  266. memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  267. }
  268. p->state = STATE_SETTING_UP;
  269. pthread_cond_signal(&p->input_cond);
  270. pthread_mutex_unlock(&p->mutex);
  271. /*
  272. * If the client doesn't have a thread-safe get_buffer(),
  273. * then decoding threads call back to the main thread,
  274. * and it calls back to the client here.
  275. */
  276. FF_DISABLE_DEPRECATION_WARNINGS
  277. if (!p->avctx->thread_safe_callbacks && (
  278. #if FF_API_GET_BUFFER
  279. p->avctx->get_buffer ||
  280. #endif
  281. p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
  282. FF_ENABLE_DEPRECATION_WARNINGS
  283. while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
  284. pthread_mutex_lock(&p->progress_mutex);
  285. while (p->state == STATE_SETTING_UP)
  286. pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
  287. if (p->state == STATE_GET_BUFFER) {
  288. p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
  289. p->state = STATE_SETTING_UP;
  290. pthread_cond_signal(&p->progress_cond);
  291. }
  292. pthread_mutex_unlock(&p->progress_mutex);
  293. }
  294. }
  295. fctx->prev_thread = p;
  296. fctx->next_decoding++;
  297. return 0;
  298. }
  299. int ff_thread_decode_frame(AVCodecContext *avctx,
  300. AVFrame *picture, int *got_picture_ptr,
  301. AVPacket *avpkt)
  302. {
  303. FrameThreadContext *fctx = avctx->internal->thread_ctx;
  304. int finished = fctx->next_finished;
  305. PerThreadContext *p;
  306. int err;
  307. /*
  308. * Submit a packet to the next decoding thread.
  309. */
  310. p = &fctx->threads[fctx->next_decoding];
  311. err = update_context_from_user(p->avctx, avctx);
  312. if (err) return err;
  313. err = submit_packet(p, avpkt);
  314. if (err) return err;
  315. /*
  316. * If we're still receiving the initial packets, don't return a frame.
  317. */
  318. if (fctx->delaying) {
  319. if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
  320. *got_picture_ptr=0;
  321. if (avpkt->size)
  322. return avpkt->size;
  323. }
  324. /*
  325. * Return the next available frame from the oldest thread.
  326. * If we're at the end of the stream, then we have to skip threads that
  327. * didn't output a frame, because we don't want to accidentally signal
  328. * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
  329. */
  330. do {
  331. p = &fctx->threads[finished++];
  332. if (p->state != STATE_INPUT_READY) {
  333. pthread_mutex_lock(&p->progress_mutex);
  334. while (p->state != STATE_INPUT_READY)
  335. pthread_cond_wait(&p->output_cond, &p->progress_mutex);
  336. pthread_mutex_unlock(&p->progress_mutex);
  337. }
  338. av_frame_move_ref(picture, p->frame);
  339. *got_picture_ptr = p->got_frame;
  340. picture->pkt_dts = p->avpkt.dts;
  341. /*
  342. * A later call with avkpt->size == 0 may loop over all threads,
  343. * including this one, searching for a frame to return before being
  344. * stopped by the "finished != fctx->next_finished" condition.
  345. * Make sure we don't mistakenly return the same frame again.
  346. */
  347. p->got_frame = 0;
  348. if (finished >= avctx->thread_count) finished = 0;
  349. } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
  350. update_context_from_thread(avctx, p->avctx, 1);
  351. if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
  352. fctx->next_finished = finished;
  353. /* return the size of the consumed packet if no error occurred */
  354. return (p->result >= 0) ? avpkt->size : p->result;
  355. }
  356. void ff_thread_report_progress(ThreadFrame *f, int n, int field)
  357. {
  358. PerThreadContext *p;
  359. int *progress = f->progress ? (int*)f->progress->data : NULL;
  360. if (!progress || progress[field] >= n) return;
  361. p = f->owner->internal->thread_ctx;
  362. if (f->owner->debug&FF_DEBUG_THREADS)
  363. av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
  364. pthread_mutex_lock(&p->progress_mutex);
  365. progress[field] = n;
  366. pthread_cond_broadcast(&p->progress_cond);
  367. pthread_mutex_unlock(&p->progress_mutex);
  368. }
  369. void ff_thread_await_progress(ThreadFrame *f, int n, int field)
  370. {
  371. PerThreadContext *p;
  372. int *progress = f->progress ? (int*)f->progress->data : NULL;
  373. if (!progress || progress[field] >= n) return;
  374. p = f->owner->internal->thread_ctx;
  375. if (f->owner->debug&FF_DEBUG_THREADS)
  376. av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
  377. pthread_mutex_lock(&p->progress_mutex);
  378. while (progress[field] < n)
  379. pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
  380. pthread_mutex_unlock(&p->progress_mutex);
  381. }
  382. void ff_thread_finish_setup(AVCodecContext *avctx) {
  383. PerThreadContext *p = avctx->internal->thread_ctx;
  384. if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
  385. pthread_mutex_lock(&p->progress_mutex);
  386. p->state = STATE_SETUP_FINISHED;
  387. pthread_cond_broadcast(&p->progress_cond);
  388. pthread_mutex_unlock(&p->progress_mutex);
  389. }
  390. /// Waits for all threads to finish.
  391. static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
  392. {
  393. int i;
  394. for (i = 0; i < thread_count; i++) {
  395. PerThreadContext *p = &fctx->threads[i];
  396. if (p->state != STATE_INPUT_READY) {
  397. pthread_mutex_lock(&p->progress_mutex);
  398. while (p->state != STATE_INPUT_READY)
  399. pthread_cond_wait(&p->output_cond, &p->progress_mutex);
  400. pthread_mutex_unlock(&p->progress_mutex);
  401. }
  402. }
  403. }
  404. void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
  405. {
  406. FrameThreadContext *fctx = avctx->internal->thread_ctx;
  407. const AVCodec *codec = avctx->codec;
  408. int i;
  409. park_frame_worker_threads(fctx, thread_count);
  410. if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
  411. update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
  412. fctx->die = 1;
  413. for (i = 0; i < thread_count; i++) {
  414. PerThreadContext *p = &fctx->threads[i];
  415. pthread_mutex_lock(&p->mutex);
  416. pthread_cond_signal(&p->input_cond);
  417. pthread_mutex_unlock(&p->mutex);
  418. if (p->thread_init)
  419. pthread_join(p->thread, NULL);
  420. if (codec->close)
  421. codec->close(p->avctx);
  422. avctx->codec = NULL;
  423. release_delayed_buffers(p);
  424. av_frame_free(&p->frame);
  425. }
  426. for (i = 0; i < thread_count; i++) {
  427. PerThreadContext *p = &fctx->threads[i];
  428. pthread_mutex_destroy(&p->mutex);
  429. pthread_mutex_destroy(&p->progress_mutex);
  430. pthread_cond_destroy(&p->input_cond);
  431. pthread_cond_destroy(&p->progress_cond);
  432. pthread_cond_destroy(&p->output_cond);
  433. av_buffer_unref(&p->avpkt.buf);
  434. av_freep(&p->buf);
  435. av_freep(&p->released_buffers);
  436. if (i) {
  437. av_freep(&p->avctx->priv_data);
  438. av_freep(&p->avctx->slice_offset);
  439. }
  440. av_freep(&p->avctx->internal);
  441. av_freep(&p->avctx);
  442. }
  443. av_freep(&fctx->threads);
  444. pthread_mutex_destroy(&fctx->buffer_mutex);
  445. av_freep(&avctx->internal->thread_ctx);
  446. }
  447. int ff_frame_thread_init(AVCodecContext *avctx)
  448. {
  449. int thread_count = avctx->thread_count;
  450. const AVCodec *codec = avctx->codec;
  451. AVCodecContext *src = avctx;
  452. FrameThreadContext *fctx;
  453. int i, err = 0;
  454. #if HAVE_W32THREADS
  455. w32thread_init();
  456. #endif
  457. if (!thread_count) {
  458. int nb_cpus = av_cpu_count();
  459. av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
  460. // use number of cores + 1 as thread count if there is more than one
  461. if (nb_cpus > 1)
  462. thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
  463. else
  464. thread_count = avctx->thread_count = 1;
  465. }
  466. if (thread_count <= 1) {
  467. avctx->active_thread_type = 0;
  468. return 0;
  469. }
  470. avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
  471. fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
  472. pthread_mutex_init(&fctx->buffer_mutex, NULL);
  473. fctx->delaying = 1;
  474. for (i = 0; i < thread_count; i++) {
  475. AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
  476. PerThreadContext *p = &fctx->threads[i];
  477. pthread_mutex_init(&p->mutex, NULL);
  478. pthread_mutex_init(&p->progress_mutex, NULL);
  479. pthread_cond_init(&p->input_cond, NULL);
  480. pthread_cond_init(&p->progress_cond, NULL);
  481. pthread_cond_init(&p->output_cond, NULL);
  482. p->frame = av_frame_alloc();
  483. if (!p->frame) {
  484. err = AVERROR(ENOMEM);
  485. goto error;
  486. }
  487. p->parent = fctx;
  488. p->avctx = copy;
  489. if (!copy) {
  490. err = AVERROR(ENOMEM);
  491. goto error;
  492. }
  493. *copy = *src;
  494. copy->internal = av_malloc(sizeof(AVCodecInternal));
  495. if (!copy->internal) {
  496. err = AVERROR(ENOMEM);
  497. goto error;
  498. }
  499. *copy->internal = *src->internal;
  500. copy->internal->thread_ctx = p;
  501. copy->internal->pkt = &p->avpkt;
  502. if (!i) {
  503. src = copy;
  504. if (codec->init)
  505. err = codec->init(copy);
  506. update_context_from_thread(avctx, copy, 1);
  507. } else {
  508. copy->priv_data = av_malloc(codec->priv_data_size);
  509. if (!copy->priv_data) {
  510. err = AVERROR(ENOMEM);
  511. goto error;
  512. }
  513. memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
  514. copy->internal->is_copy = 1;
  515. if (codec->init_thread_copy)
  516. err = codec->init_thread_copy(copy);
  517. }
  518. if (err) goto error;
  519. if (!pthread_create(&p->thread, NULL, frame_worker_thread, p))
  520. p->thread_init = 1;
  521. }
  522. return 0;
  523. error:
  524. ff_frame_thread_free(avctx, i+1);
  525. return err;
  526. }
  527. void ff_thread_flush(AVCodecContext *avctx)
  528. {
  529. int i;
  530. FrameThreadContext *fctx = avctx->internal->thread_ctx;
  531. if (!fctx) return;
  532. park_frame_worker_threads(fctx, avctx->thread_count);
  533. if (fctx->prev_thread) {
  534. if (fctx->prev_thread != &fctx->threads[0])
  535. update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
  536. }
  537. fctx->next_decoding = fctx->next_finished = 0;
  538. fctx->delaying = 1;
  539. fctx->prev_thread = NULL;
  540. for (i = 0; i < avctx->thread_count; i++) {
  541. PerThreadContext *p = &fctx->threads[i];
  542. // Make sure decode flush calls with size=0 won't return old frames
  543. p->got_frame = 0;
  544. av_frame_unref(p->frame);
  545. release_delayed_buffers(p);
  546. if (avctx->codec->flush)
  547. avctx->codec->flush(p->avctx);
  548. }
  549. }
  550. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  551. {
  552. PerThreadContext *p = avctx->internal->thread_ctx;
  553. int err;
  554. f->owner = avctx;
  555. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  556. return ff_get_buffer(avctx, f->f, flags);
  557. if (p->state != STATE_SETTING_UP &&
  558. (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
  559. av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
  560. return -1;
  561. }
  562. if (avctx->internal->allocate_progress) {
  563. int *progress;
  564. f->progress = av_buffer_alloc(2 * sizeof(int));
  565. if (!f->progress) {
  566. return AVERROR(ENOMEM);
  567. }
  568. progress = (int*)f->progress->data;
  569. progress[0] = progress[1] = -1;
  570. }
  571. pthread_mutex_lock(&p->parent->buffer_mutex);
  572. FF_DISABLE_DEPRECATION_WARNINGS
  573. if (avctx->thread_safe_callbacks || (
  574. #if FF_API_GET_BUFFER
  575. !avctx->get_buffer &&
  576. #endif
  577. avctx->get_buffer2 == avcodec_default_get_buffer2)) {
  578. FF_ENABLE_DEPRECATION_WARNINGS
  579. err = ff_get_buffer(avctx, f->f, flags);
  580. } else {
  581. p->requested_frame = f->f;
  582. p->requested_flags = flags;
  583. p->state = STATE_GET_BUFFER;
  584. pthread_mutex_lock(&p->progress_mutex);
  585. pthread_cond_signal(&p->progress_cond);
  586. while (p->state != STATE_SETTING_UP)
  587. pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
  588. err = p->result;
  589. pthread_mutex_unlock(&p->progress_mutex);
  590. }
  591. if (!avctx->thread_safe_callbacks && !avctx->codec->update_thread_context)
  592. ff_thread_finish_setup(avctx);
  593. if (err)
  594. av_buffer_unref(&f->progress);
  595. pthread_mutex_unlock(&p->parent->buffer_mutex);
  596. return err;
  597. }
  598. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  599. {
  600. PerThreadContext *p = avctx->internal->thread_ctx;
  601. FrameThreadContext *fctx;
  602. AVFrame *dst, *tmp;
  603. FF_DISABLE_DEPRECATION_WARNINGS
  604. int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
  605. avctx->thread_safe_callbacks ||
  606. (
  607. #if FF_API_GET_BUFFER
  608. !avctx->get_buffer &&
  609. #endif
  610. avctx->get_buffer2 == avcodec_default_get_buffer2);
  611. FF_ENABLE_DEPRECATION_WARNINGS
  612. if (!f->f->buf[0])
  613. return;
  614. if (avctx->debug & FF_DEBUG_BUFFERS)
  615. av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
  616. av_buffer_unref(&f->progress);
  617. f->owner = NULL;
  618. if (can_direct_free) {
  619. av_frame_unref(f->f);
  620. return;
  621. }
  622. fctx = p->parent;
  623. pthread_mutex_lock(&fctx->buffer_mutex);
  624. if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
  625. goto fail;
  626. tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
  627. (p->num_released_buffers + 1) *
  628. sizeof(*p->released_buffers));
  629. if (!tmp)
  630. goto fail;
  631. p->released_buffers = tmp;
  632. dst = &p->released_buffers[p->num_released_buffers];
  633. av_frame_move_ref(dst, f->f);
  634. p->num_released_buffers++;
  635. fail:
  636. pthread_mutex_unlock(&fctx->buffer_mutex);
  637. }