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.

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