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.

794 lines
24KB

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