pthread.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2004 Roman Shaposhnik
  3. * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
  4. *
  5. * Many thanks to Steven M. Schultz for providing clever ideas and
  6. * to Michael Niedermayer <michaelni@gmx.at> for writing initial
  7. * implementation.
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /**
  26. * @file
  27. * Multithreading support functions
  28. * @see doc/multithreading.txt
  29. */
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. #include "pthread_internal.h"
  33. #include "thread.h"
  34. /**
  35. * Set the threading algorithms used.
  36. *
  37. * Threading requires more than one thread.
  38. * Frame threading requires entire frames to be passed to the codec,
  39. * and introduces extra decoding delay, so is incompatible with low_delay.
  40. *
  41. * @param avctx The context.
  42. */
  43. static void validate_thread_parameters(AVCodecContext *avctx)
  44. {
  45. int frame_threading_supported = (avctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS)
  46. && !(avctx->flags & AV_CODEC_FLAG_TRUNCATED)
  47. && !(avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  48. && !(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS);
  49. if (avctx->thread_count == 1) {
  50. avctx->active_thread_type = 0;
  51. } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
  52. avctx->active_thread_type = FF_THREAD_FRAME;
  53. } else if (avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS &&
  54. avctx->thread_type & FF_THREAD_SLICE) {
  55. avctx->active_thread_type = FF_THREAD_SLICE;
  56. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_AUTO_THREADS)) {
  57. avctx->thread_count = 1;
  58. avctx->active_thread_type = 0;
  59. }
  60. if (avctx->thread_count > MAX_AUTO_THREADS)
  61. av_log(avctx, AV_LOG_WARNING,
  62. "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
  63. avctx->thread_count, MAX_AUTO_THREADS);
  64. }
  65. int ff_thread_init(AVCodecContext *avctx)
  66. {
  67. validate_thread_parameters(avctx);
  68. if (avctx->active_thread_type&FF_THREAD_SLICE)
  69. return ff_slice_thread_init(avctx);
  70. else if (avctx->active_thread_type&FF_THREAD_FRAME)
  71. return ff_frame_thread_init(avctx);
  72. return 0;
  73. }
  74. void ff_thread_free(AVCodecContext *avctx)
  75. {
  76. if (avctx->active_thread_type&FF_THREAD_FRAME)
  77. ff_frame_thread_free(avctx, avctx->thread_count);
  78. else
  79. ff_slice_thread_free(avctx);
  80. }