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.

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