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.

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