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