lookahead.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*****************************************************************************
  2. * lookahead.c: high-level lookahead functions
  3. *****************************************************************************
  4. * Copyright (C) 2010-2018 Avail Media and x264 project
  5. *
  6. * Authors: Michael Kazmier <mkazmier@availmedia.com>
  7. * Alex Giladi <agiladi@availmedia.com>
  8. * Steven Walters <kemuri9@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  23. *
  24. * This program is also available under a commercial proprietary license.
  25. * For more information, contact us at licensing@x264.com.
  26. *****************************************************************************/
  27. /* LOOKAHEAD (threaded and non-threaded mode)
  28. *
  29. * Lookahead types:
  30. * [1] Slice type / scene cut;
  31. *
  32. * In non-threaded mode, we run the existing slicetype decision code as it was.
  33. * In threaded mode, we run in a separate thread, that lives between the calls
  34. * to x264_encoder_open() and x264_encoder_close(), and performs lookahead for
  35. * the number of frames specified in rc_lookahead. Recommended setting is
  36. * # of bframes + # of threads.
  37. */
  38. #include "common/common.h"
  39. #include "analyse.h"
  40. static void lookahead_shift( x264_sync_frame_list_t *dst, x264_sync_frame_list_t *src, int count )
  41. {
  42. int i = count;
  43. while( i-- )
  44. {
  45. assert( dst->i_size < dst->i_max_size );
  46. assert( src->i_size );
  47. dst->list[ dst->i_size++ ] = x264_frame_shift( src->list );
  48. src->i_size--;
  49. }
  50. if( count )
  51. {
  52. x264_pthread_cond_broadcast( &dst->cv_fill );
  53. x264_pthread_cond_broadcast( &src->cv_empty );
  54. }
  55. }
  56. static void lookahead_update_last_nonb( x264_t *h, x264_frame_t *new_nonb )
  57. {
  58. if( h->lookahead->last_nonb )
  59. x264_frame_push_unused( h, h->lookahead->last_nonb );
  60. h->lookahead->last_nonb = new_nonb;
  61. new_nonb->i_reference_count++;
  62. }
  63. #if HAVE_THREAD
  64. static void lookahead_slicetype_decide( x264_t *h )
  65. {
  66. x264_slicetype_decide( h );
  67. lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
  68. int shift_frames = h->lookahead->next.list[0]->i_bframes + 1;
  69. x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  70. while( h->lookahead->ofbuf.i_size == h->lookahead->ofbuf.i_max_size )
  71. x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_empty, &h->lookahead->ofbuf.mutex );
  72. x264_pthread_mutex_lock( &h->lookahead->next.mutex );
  73. lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, shift_frames );
  74. x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  75. /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
  76. if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
  77. x264_slicetype_analyse( h, shift_frames );
  78. x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
  79. }
  80. static void *lookahead_thread_internal( x264_t *h )
  81. {
  82. while( !h->lookahead->b_exit_thread )
  83. {
  84. x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
  85. x264_pthread_mutex_lock( &h->lookahead->next.mutex );
  86. int shift = X264_MIN( h->lookahead->next.i_max_size - h->lookahead->next.i_size, h->lookahead->ifbuf.i_size );
  87. lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, shift );
  88. x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  89. if( h->lookahead->next.i_size <= h->lookahead->i_slicetype_length + h->param.b_vfr_input )
  90. {
  91. while( !h->lookahead->ifbuf.i_size && !h->lookahead->b_exit_thread )
  92. x264_pthread_cond_wait( &h->lookahead->ifbuf.cv_fill, &h->lookahead->ifbuf.mutex );
  93. x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
  94. }
  95. else
  96. {
  97. x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
  98. lookahead_slicetype_decide( h );
  99. }
  100. } /* end of input frames */
  101. x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
  102. x264_pthread_mutex_lock( &h->lookahead->next.mutex );
  103. lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, h->lookahead->ifbuf.i_size );
  104. x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  105. x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
  106. while( h->lookahead->next.i_size )
  107. lookahead_slicetype_decide( h );
  108. x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  109. h->lookahead->b_thread_active = 0;
  110. x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_fill );
  111. x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
  112. return NULL;
  113. }
  114. static void *lookahead_thread( x264_t *h )
  115. {
  116. return (void*)x264_stack_align( lookahead_thread_internal, h );
  117. }
  118. #endif
  119. int x264_lookahead_init( x264_t *h, int i_slicetype_length )
  120. {
  121. x264_lookahead_t *look;
  122. CHECKED_MALLOCZERO( look, sizeof(x264_lookahead_t) );
  123. for( int i = 0; i < h->param.i_threads; i++ )
  124. h->thread[i]->lookahead = look;
  125. look->i_last_keyframe = - h->param.i_keyint_max;
  126. look->b_analyse_keyframe = (h->param.rc.b_mb_tree || (h->param.rc.i_vbv_buffer_size && h->param.rc.i_lookahead))
  127. && !h->param.rc.b_stat_read;
  128. look->i_slicetype_length = i_slicetype_length;
  129. /* init frame lists */
  130. if( x264_sync_frame_list_init( &look->ifbuf, h->param.i_sync_lookahead+3 ) ||
  131. x264_sync_frame_list_init( &look->next, h->frames.i_delay+3 ) ||
  132. x264_sync_frame_list_init( &look->ofbuf, h->frames.i_delay+3 ) )
  133. goto fail;
  134. if( !h->param.i_sync_lookahead )
  135. return 0;
  136. x264_t *look_h = h->thread[h->param.i_threads];
  137. *look_h = *h;
  138. if( x264_macroblock_cache_allocate( look_h ) )
  139. goto fail;
  140. if( x264_macroblock_thread_allocate( look_h, 1 ) < 0 )
  141. goto fail;
  142. if( x264_pthread_create( &look->thread_handle, NULL, (void*)lookahead_thread, look_h ) )
  143. goto fail;
  144. look->b_thread_active = 1;
  145. return 0;
  146. fail:
  147. x264_free( look );
  148. return -1;
  149. }
  150. void x264_lookahead_delete( x264_t *h )
  151. {
  152. if( h->param.i_sync_lookahead )
  153. {
  154. x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
  155. h->lookahead->b_exit_thread = 1;
  156. x264_pthread_cond_broadcast( &h->lookahead->ifbuf.cv_fill );
  157. x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
  158. x264_pthread_join( h->lookahead->thread_handle, NULL );
  159. x264_macroblock_cache_free( h->thread[h->param.i_threads] );
  160. x264_macroblock_thread_free( h->thread[h->param.i_threads], 1 );
  161. x264_free( h->thread[h->param.i_threads] );
  162. }
  163. x264_sync_frame_list_delete( &h->lookahead->ifbuf );
  164. x264_sync_frame_list_delete( &h->lookahead->next );
  165. if( h->lookahead->last_nonb )
  166. x264_frame_push_unused( h, h->lookahead->last_nonb );
  167. x264_sync_frame_list_delete( &h->lookahead->ofbuf );
  168. x264_free( h->lookahead );
  169. }
  170. void x264_lookahead_put_frame( x264_t *h, x264_frame_t *frame )
  171. {
  172. if( h->param.i_sync_lookahead )
  173. x264_sync_frame_list_push( &h->lookahead->ifbuf, frame );
  174. else
  175. x264_sync_frame_list_push( &h->lookahead->next, frame );
  176. }
  177. int x264_lookahead_is_empty( x264_t *h )
  178. {
  179. x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  180. x264_pthread_mutex_lock( &h->lookahead->next.mutex );
  181. int b_empty = !h->lookahead->next.i_size && !h->lookahead->ofbuf.i_size;
  182. x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  183. x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
  184. return b_empty;
  185. }
  186. static void lookahead_encoder_shift( x264_t *h )
  187. {
  188. if( !h->lookahead->ofbuf.i_size )
  189. return;
  190. int i_frames = h->lookahead->ofbuf.list[0]->i_bframes + 1;
  191. while( i_frames-- )
  192. {
  193. x264_frame_push( h->frames.current, x264_frame_shift( h->lookahead->ofbuf.list ) );
  194. h->lookahead->ofbuf.i_size--;
  195. }
  196. x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_empty );
  197. }
  198. void x264_lookahead_get_frames( x264_t *h )
  199. {
  200. if( h->param.i_sync_lookahead )
  201. { /* We have a lookahead thread, so get frames from there */
  202. x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  203. while( !h->lookahead->ofbuf.i_size && h->lookahead->b_thread_active )
  204. x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_fill, &h->lookahead->ofbuf.mutex );
  205. lookahead_encoder_shift( h );
  206. x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
  207. }
  208. else
  209. { /* We are not running a lookahead thread, so perform all the slicetype decide on the fly */
  210. if( h->frames.current[0] || !h->lookahead->next.i_size )
  211. return;
  212. x264_slicetype_decide( h );
  213. lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
  214. int shift_frames = h->lookahead->next.list[0]->i_bframes + 1;
  215. lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, shift_frames );
  216. /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
  217. if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
  218. x264_slicetype_analyse( h, shift_frames );
  219. lookahead_encoder_shift( h );
  220. }
  221. }