lookahead.c
上传用户:lctgjx
上传日期:2022-06-04
资源大小:8887k
文件大小:10k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * lookahead.c: Lookahead slicetype decisions for x264
  3.  *****************************************************************************
  4.  * Lookahead.c and associated modifications:
  5.  *     Copyright (C) 2008 Avail Media
  6.  *
  7.  * Authors: Michael Kazmier <mkazmier@availmedia.com>
  8.  *          Alex Giladi <agiladi@availmedia.com>
  9.  *          Steven Walters <kemuri9@gmail.com>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program 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
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  24.  *****************************************************************************/
  25. /* LOOKAHEAD (threaded and non-threaded mode)
  26.  *
  27.  * Lookahead types:
  28.  *     [1] Slice type / scene cut;
  29.  *
  30.  * In non-threaded mode, we run the existing slicetype decision code as it was.
  31.  * In threaded mode, we run in a separate thread, that lives between the calls
  32.  * to x264_encoder_open() and x264_encoder_close(), and performs lookahead for
  33.  * the number of frames specified in rc_lookahead.  Recommended setting is
  34.  * # of bframes + # of threads.
  35.  */
  36. #include "common/common.h"
  37. #include "common/cpu.h"
  38. #include "analyse.h"
  39. static void x264_lookahead_shift( x264_synch_frame_list_t *dst, x264_synch_frame_list_t *src, int count )
  40. {
  41.     int i = count;
  42.     while( i-- )
  43.     {
  44.         assert( dst->i_size < dst->i_max_size );
  45.         assert( src->i_size );
  46.         dst->list[ dst->i_size++ ] = x264_frame_shift( src->list );
  47.         src->i_size--;
  48.     }
  49.     if( count )
  50.     {
  51.         x264_pthread_cond_broadcast( &dst->cv_fill );
  52.         x264_pthread_cond_broadcast( &src->cv_empty );
  53.     }
  54. }
  55. static void x264_lookahead_update_last_nonb( x264_t *h, x264_frame_t *new_nonb )
  56. {
  57.     if( h->lookahead->last_nonb )
  58.         x264_frame_push_unused( h, h->lookahead->last_nonb );
  59.     h->lookahead->last_nonb = new_nonb;
  60.     new_nonb->i_reference_count++;
  61. }
  62. #ifdef HAVE_PTHREAD
  63. static void x264_lookahead_slicetype_decide( x264_t *h )
  64. {
  65.     int bframes = 0;
  66.     x264_stack_align( x264_slicetype_decide, h );
  67.     while( IS_X264_TYPE_B( h->lookahead->next.list[bframes]->i_type ) )
  68.         bframes++;
  69.     x264_lookahead_update_last_nonb( h, h->lookahead->next.list[bframes] );
  70.     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  71.     while( h->lookahead->ofbuf.i_size == h->lookahead->ofbuf.i_max_size )
  72.         x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_empty, &h->lookahead->ofbuf.mutex );
  73.     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
  74.     x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, bframes + 1 );
  75.     x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  76.     /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
  77.     if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
  78.         x264_stack_align( x264_slicetype_analyse, h, 1 );
  79.     x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
  80. }
  81. static void x264_lookahead_thread( x264_t *h )
  82. {
  83.     int shift;
  84. #ifdef HAVE_MMX
  85.     if( h->param.cpu&X264_CPU_SSE_MISALIGN )
  86.         x264_cpu_mask_misalign_sse();
  87. #endif
  88.     while( !h->lookahead->b_exit_thread )
  89.     {
  90.         x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
  91.         x264_pthread_mutex_lock( &h->lookahead->next.mutex );
  92.         shift = X264_MIN( h->lookahead->next.i_max_size - h->lookahead->next.i_size, h->lookahead->ifbuf.i_size );
  93.         x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, shift );
  94.         x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  95.         if( h->lookahead->next.i_size <= h->lookahead->i_slicetype_length )
  96.         {
  97.             while( !h->lookahead->ifbuf.i_size && !h->lookahead->b_exit_thread )
  98.                 x264_pthread_cond_wait( &h->lookahead->ifbuf.cv_fill, &h->lookahead->ifbuf.mutex );
  99.             x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
  100.         }
  101.         else
  102.         {
  103.             x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
  104.             x264_lookahead_slicetype_decide( h );
  105.         }
  106.     }   /* end of input frames */
  107.     x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
  108.     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
  109.     x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, h->lookahead->ifbuf.i_size );
  110.     x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  111.     x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
  112.     while( h->lookahead->next.i_size )
  113.         x264_lookahead_slicetype_decide( h );
  114.     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  115.     h->lookahead->b_thread_active = 0;
  116.     x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_fill );
  117.     x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
  118. }
  119. #endif
  120. int x264_lookahead_init( x264_t *h, int i_slicetype_length )
  121. {
  122.     x264_lookahead_t *look;
  123. //C程序不能中间定义变量 --@lia
  124. int i;
  125. x264_t *look_h;
  126.     CHECKED_MALLOCZERO( look, sizeof(x264_lookahead_t) );
  127.     //C程序不能中间定义变量 --@lia
  128. //int i;
  129.     for( i = 0; i < h->param.i_threads; i++ )
  130.         h->thread[i]->lookahead = look;
  131.     look->i_last_idr = - h->param.i_keyint_max;
  132.     look->b_analyse_keyframe = (h->param.rc.b_mb_tree || (h->param.rc.i_vbv_buffer_size && h->param.rc.i_lookahead))
  133.                                && !h->param.rc.b_stat_read;
  134.     look->i_slicetype_length = i_slicetype_length;
  135.     /* init frame lists */
  136.     if( x264_synch_frame_list_init( &look->ifbuf, h->param.i_sync_lookahead+3 ) ||
  137.         x264_synch_frame_list_init( &look->next, h->frames.i_delay+3 ) ||
  138.         x264_synch_frame_list_init( &look->ofbuf, h->frames.i_delay+3 ) )
  139.         goto fail;
  140.     if( !h->param.i_sync_lookahead )
  141.         return 0;
  142.     //C程序不能中间定义变量 --@lia
  143. //x264_t *
  144. look_h = h->thread[h->param.i_threads];
  145.     *look_h = *h;
  146.     if( x264_macroblock_cache_init( look_h ) )
  147.         goto fail;
  148.     if( x264_pthread_create( &look_h->thread_handle, NULL, (void *)x264_lookahead_thread, look_h ) )
  149.         goto fail;
  150.     look->b_thread_active = 1;
  151.     return 0;
  152. fail:
  153.     x264_free( look );
  154.     return -1;
  155. }
  156. void x264_lookahead_delete( x264_t *h )
  157. {
  158.     if( h->param.i_sync_lookahead )
  159.     {
  160.         h->lookahead->b_exit_thread = 1;
  161.         x264_pthread_cond_broadcast( &h->lookahead->ifbuf.cv_fill );
  162.         x264_pthread_join( h->thread[h->param.i_threads]->thread_handle, NULL );
  163.         x264_macroblock_cache_end( h->thread[h->param.i_threads] );
  164.         x264_free( h->thread[h->param.i_threads] );
  165.     }
  166.     x264_synch_frame_list_delete( &h->lookahead->ifbuf );
  167.     x264_synch_frame_list_delete( &h->lookahead->next );
  168.     if( h->lookahead->last_nonb )
  169.         x264_frame_push_unused( h, h->lookahead->last_nonb );
  170.     x264_synch_frame_list_delete( &h->lookahead->ofbuf );
  171.     x264_free( h->lookahead );
  172. }
  173. void x264_lookahead_put_frame( x264_t *h, x264_frame_t *frame )
  174. {
  175.     if( h->param.i_sync_lookahead )
  176.         x264_synch_frame_list_push( &h->lookahead->ifbuf, frame );
  177.     else
  178.         x264_synch_frame_list_push( &h->lookahead->next, frame );
  179. }
  180. int x264_lookahead_is_empty( x264_t *h )
  181. {
  182.     int b_empty;
  183.     x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  184.     x264_pthread_mutex_lock( &h->lookahead->next.mutex );
  185.     b_empty = !h->lookahead->next.i_size && !h->lookahead->ofbuf.i_size;
  186.     x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
  187.     x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
  188.     return b_empty;
  189. }
  190. static void x264_lookahead_encoder_shift( x264_t *h )
  191. {
  192.     int bframes  = 0;
  193.     int i_frames = 0;
  194.     while( h->lookahead->ofbuf.list[i_frames] )
  195.     {
  196.         if( IS_X264_TYPE_B( h->lookahead->ofbuf.list[bframes]->i_type ) )
  197.             bframes++;
  198.         else
  199.             break;
  200.         i_frames++;
  201.     }
  202.     if( h->lookahead->ofbuf.list[i_frames] )
  203.     {
  204.         x264_frame_push( h->frames.current, x264_frame_shift( &h->lookahead->ofbuf.list[bframes] ) );
  205.         h->lookahead->ofbuf.i_size--;
  206.         if( h->param.b_bframe_pyramid && bframes > 1 )
  207.         {
  208.             x264_frame_t *mid = x264_frame_shift( &h->lookahead->ofbuf.list[bframes/2] );
  209.             h->lookahead->ofbuf.i_size--;
  210.             mid->i_type = X264_TYPE_BREF;
  211.             x264_frame_push( h->frames.current, mid );
  212.             bframes--;
  213.         }
  214.         while( bframes-- )
  215.         {
  216.             x264_frame_push( h->frames.current, x264_frame_shift( h->lookahead->ofbuf.list ) );
  217.             h->lookahead->ofbuf.i_size--;
  218.         }
  219.         x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_empty );
  220.     }
  221. }
  222. void x264_lookahead_get_frames( x264_t *h )
  223. {
  224.     if( h->param.i_sync_lookahead )
  225.     {   /* We have a lookahead thread, so get frames from there */
  226.         x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
  227.         while( !h->lookahead->ofbuf.i_size && h->lookahead->b_thread_active )
  228.             x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_fill, &h->lookahead->ofbuf.mutex );
  229.         x264_lookahead_encoder_shift( h );
  230.         x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
  231.     }
  232.     else
  233.     {   /* We are not running a lookahead thread, so perform all the slicetype decide on the fly */
  234.         //C程序不能中间定义变量 --@lia
  235. int bframes;
  236. if( h->frames.current[0] || !h->lookahead->next.i_size )
  237.             return;
  238.         x264_stack_align( x264_slicetype_decide, h );
  239.         //C程序不能中间定义变量 --@lia
  240. //int 
  241. bframes=0;
  242.         while( IS_X264_TYPE_B( h->lookahead->next.list[bframes]->i_type ) )
  243.             bframes++;
  244.         x264_lookahead_update_last_nonb( h, h->lookahead->next.list[bframes] );
  245.         x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, bframes + 1 );
  246.         /* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
  247.         if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
  248.             x264_stack_align( x264_slicetype_analyse, h, 1 );
  249.         x264_lookahead_encoder_shift( h );
  250.     }
  251. }