thread.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2008 Alexander Strange <astrange@ithinksw.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Multithreading support functions
  23. * @author Alexander Strange <astrange@ithinksw.com>
  24. */
  25. #ifndef AVCODEC_THREAD_H
  26. #define AVCODEC_THREAD_H
  27. #include "libavutil/buffer.h"
  28. #include "avcodec.h"
  29. typedef struct ThreadFrame {
  30. AVFrame *f;
  31. AVCodecContext *owner[2];
  32. // progress->data is an array of 2 ints holding progress for top/bottom
  33. // fields
  34. AVBufferRef *progress;
  35. } ThreadFrame;
  36. /**
  37. * Wait for decoding threads to finish and reset internal state.
  38. * Called by avcodec_flush_buffers().
  39. *
  40. * @param avctx The context.
  41. */
  42. void ff_thread_flush(AVCodecContext *avctx);
  43. /**
  44. * Submit a new frame to a decoding thread.
  45. * Returns the next available frame in picture. *got_picture_ptr
  46. * will be 0 if none is available.
  47. * The return value on success is the size of the consumed packet for
  48. * compatibility with avcodec_decode_video2(). This means the decoder
  49. * has to consume the full packet.
  50. *
  51. * Parameters are the same as avcodec_decode_video2().
  52. */
  53. int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture,
  54. int *got_picture_ptr, AVPacket *avpkt);
  55. /**
  56. * If the codec defines update_thread_context(), call this
  57. * when they are ready for the next thread to start decoding
  58. * the next frame. After calling it, do not change any variables
  59. * read by the update_thread_context() method, or call ff_thread_get_buffer().
  60. *
  61. * @param avctx The context.
  62. */
  63. void ff_thread_finish_setup(AVCodecContext *avctx);
  64. /**
  65. * Notify later decoding threads when part of their reference picture is ready.
  66. * Call this when some part of the picture is finished decoding.
  67. * Later calls with lower values of progress have no effect.
  68. *
  69. * @param f The picture being decoded.
  70. * @param progress Value, in arbitrary units, of how much of the picture has decoded.
  71. * @param field The field being decoded, for field-picture codecs.
  72. * 0 for top field or frame pictures, 1 for bottom field.
  73. */
  74. void ff_thread_report_progress(ThreadFrame *f, int progress, int field);
  75. /**
  76. * Wait for earlier decoding threads to finish reference pictures.
  77. * Call this before accessing some part of a picture, with a given
  78. * value for progress, and it will return after the responsible decoding
  79. * thread calls ff_thread_report_progress() with the same or
  80. * higher value for progress.
  81. *
  82. * @param f The picture being referenced.
  83. * @param progress Value, in arbitrary units, to wait for.
  84. * @param field The field being referenced, for field-picture codecs.
  85. * 0 for top field or frame pictures, 1 for bottom field.
  86. */
  87. void ff_thread_await_progress(ThreadFrame *f, int progress, int field);
  88. /**
  89. * Wrapper around get_format() for frame-multithreaded codecs.
  90. * Call this function instead of avctx->get_format().
  91. * Cannot be called after the codec has called ff_thread_finish_setup().
  92. *
  93. * @param avctx The current context.
  94. * @param fmt The list of available formats.
  95. */
  96. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt);
  97. /**
  98. * Wrapper around get_buffer() for frame-multithreaded codecs.
  99. * Call this function instead of ff_get_buffer(f).
  100. * Cannot be called after the codec has called ff_thread_finish_setup().
  101. *
  102. * @param avctx The current context.
  103. * @param f The frame to write into.
  104. */
  105. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags);
  106. /**
  107. * Wrapper around release_buffer() frame-for multithreaded codecs.
  108. * Call this function instead of avctx->release_buffer(f).
  109. * The AVFrame will be copied and the actual release_buffer() call
  110. * will be performed later. The contents of data pointed to by the
  111. * AVFrame should not be changed until ff_thread_get_buffer() is called
  112. * on it.
  113. *
  114. * @param avctx The current context.
  115. * @param f The picture being released.
  116. */
  117. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f);
  118. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src);
  119. int ff_thread_init(AVCodecContext *s);
  120. int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx,
  121. int (*action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr),
  122. int (*main_func)(AVCodecContext *c), void *arg, int *ret, int job_count);
  123. void ff_thread_free(AVCodecContext *s);
  124. int ff_alloc_entries(AVCodecContext *avctx, int count);
  125. void ff_reset_entries(AVCodecContext *avctx);
  126. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n);
  127. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift);
  128. #endif /* AVCODEC_THREAD_H */