thread.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*****************************************************************************
  2. * thread.c: threaded input
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Loren Merritt <lorenm@u.washington.edu>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  22. *
  23. * This program is also available under a commercial proprietary license.
  24. * For more information, contact us at licensing@x264.com.
  25. *****************************************************************************/
  26. #include "input.h"
  27. #include "common/common.h"
  28. #define thread_input x264_glue3(thread, BIT_DEPTH, input)
  29. typedef struct
  30. {
  31. cli_input_t input;
  32. hnd_t p_handle;
  33. cli_pic_t pic;
  34. x264_threadpool_t *pool;
  35. int next_frame;
  36. int frame_total;
  37. struct thread_input_arg_t *next_args;
  38. } thread_hnd_t;
  39. typedef struct thread_input_arg_t
  40. {
  41. thread_hnd_t *h;
  42. cli_pic_t *pic;
  43. int i_frame;
  44. int status;
  45. } thread_input_arg_t;
  46. static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
  47. {
  48. thread_hnd_t *h = malloc( sizeof(thread_hnd_t) );
  49. FAIL_IF_ERR( !h || cli_input.picture_alloc( &h->pic, *p_handle, info->csp, info->width, info->height ),
  50. "x264", "malloc failed\n" );
  51. h->input = cli_input;
  52. h->p_handle = *p_handle;
  53. h->next_frame = -1;
  54. h->next_args = malloc( sizeof(thread_input_arg_t) );
  55. if( !h->next_args )
  56. return -1;
  57. h->next_args->h = h;
  58. h->next_args->status = 0;
  59. h->frame_total = info->num_frames;
  60. if( x264_threadpool_init( &h->pool, 1, NULL, NULL ) )
  61. return -1;
  62. *p_handle = h;
  63. return 0;
  64. }
  65. static void read_frame_thread_int( thread_input_arg_t *i )
  66. {
  67. i->status = i->h->input.read_frame( i->pic, i->h->p_handle, i->i_frame );
  68. }
  69. static int read_frame( cli_pic_t *p_pic, hnd_t handle, int i_frame )
  70. {
  71. thread_hnd_t *h = handle;
  72. int ret = 0;
  73. if( h->next_frame >= 0 )
  74. {
  75. x264_threadpool_wait( h->pool, h->next_args );
  76. ret |= h->next_args->status;
  77. }
  78. if( h->next_frame == i_frame )
  79. XCHG( cli_pic_t, *p_pic, h->pic );
  80. else
  81. {
  82. if( h->next_frame >= 0 )
  83. thread_input.release_frame( &h->pic, handle );
  84. ret |= h->input.read_frame( p_pic, h->p_handle, i_frame );
  85. }
  86. if( !h->frame_total || i_frame+1 < h->frame_total )
  87. {
  88. h->next_frame =
  89. h->next_args->i_frame = i_frame+1;
  90. h->next_args->pic = &h->pic;
  91. x264_threadpool_run( h->pool, (void*)read_frame_thread_int, h->next_args );
  92. }
  93. else
  94. h->next_frame = -1;
  95. return ret;
  96. }
  97. static int release_frame( cli_pic_t *pic, hnd_t handle )
  98. {
  99. thread_hnd_t *h = handle;
  100. if( h->input.release_frame )
  101. return h->input.release_frame( pic, h->p_handle );
  102. return 0;
  103. }
  104. static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
  105. {
  106. thread_hnd_t *h = handle;
  107. return h->input.picture_alloc( pic, h->p_handle, csp, width, height );
  108. }
  109. static void picture_clean( cli_pic_t *pic, hnd_t handle )
  110. {
  111. thread_hnd_t *h = handle;
  112. h->input.picture_clean( pic, h->p_handle );
  113. }
  114. static int close_file( hnd_t handle )
  115. {
  116. thread_hnd_t *h = handle;
  117. x264_threadpool_delete( h->pool );
  118. h->input.picture_clean( &h->pic, h->p_handle );
  119. h->input.close_file( h->p_handle );
  120. free( h->next_args );
  121. free( h );
  122. return 0;
  123. }
  124. const cli_input_t thread_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };