1 /*****************************************************************************
2 * x264.c: h264 video encoder
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/decoder.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
40 #define SOUT_CFG_PREFIX "sout-x264-"
41 static char *enc_analyse_list[] = {
42 "all", "normal", "fast", "none"
45 static char *enc_analyse_list_text[] = {
46 N_("all"), N_("normal"), N_("fast"), N_("none")
50 set_description( _("h264 video encoder using x264 library"));
51 set_capability( "encoder", 200 );
53 add_integer( SOUT_CFG_PREFIX "qp", 0, NULL, "Set fixed QP (1-51)", "", VLC_FALSE );
54 add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, "Enable CABAC", "", VLC_FALSE );
55 add_bool( SOUT_CFG_PREFIX "loopfilter", 1, NULL, "Enable loop filter", "", VLC_FALSE );
57 add_string( SOUT_CFG_PREFIX "analyse", "", NULL, "Analyse mode", "", VLC_FALSE );
58 change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
59 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
66 static const char *ppsz_sout_options[] = {
67 "qp", "cabac", "loopfilter", "analyse", NULL
70 static block_t *Encode( encoder_t *, picture_t * );
81 /*****************************************************************************
82 * Open: probe the encoder
83 *****************************************************************************/
84 static int Open ( vlc_object_t *p_this )
86 encoder_t *p_enc = (encoder_t *)p_this;
90 if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) && !p_enc->b_force )
94 if( p_enc->fmt_in.video.i_width % 16 != 0 ||
95 p_enc->fmt_in.video.i_height % 16!= 0 )
97 msg_Warn( p_enc, "invalid size %ix%i",
98 p_enc->fmt_in.video.i_width,
99 p_enc->fmt_in.video.i_height );
103 sout_CfgParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
105 p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
106 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
108 p_enc->pf_encode_video = Encode;
109 p_enc->pf_encode_audio = NULL;
110 p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
112 x264_param_default( &p_sys->param );
113 p_sys->param.i_width = p_enc->fmt_in.video.i_width;
114 p_sys->param.i_height = p_enc->fmt_in.video.i_height;
115 p_sys->param.i_idrframe = 1;
116 if( p_enc->i_iframes > 0 )
118 p_sys->param.i_iframe = p_enc->i_iframes;
120 var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
121 if( val.i_int >= 1 && val.i_int <= 51 )
123 #if X264_BUILD >= 0x000a
124 p_sys->param.rc.i_qp_constant = val.i_int;
126 p_sys->param.i_qp_constant = val.i_int;
130 var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
131 p_sys->param.b_cabac = val.b_bool;
133 var_Get( p_enc, SOUT_CFG_PREFIX "loopfilter", &val );
134 p_sys->param.b_deblocking_filter = val.b_bool;
136 if( p_enc->fmt_in.video.i_aspect > 0 )
138 int64_t i_num, i_den;
139 int i_dst_num, i_dst_den;
141 i_num = p_enc->fmt_in.video.i_aspect *
142 (int64_t)p_enc->fmt_in.video.i_height;
143 i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
144 vlc_reduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
146 p_sys->param.vui.i_sar_width = i_dst_num;
147 p_sys->param.vui.i_sar_height = i_dst_den;
149 if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
151 p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
152 p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
154 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
156 p_sys->param.cpu &= ~X264_CPU_MMX;
158 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
160 p_sys->param.cpu &= ~X264_CPU_MMXEXT;
162 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
164 p_sys->param.cpu &= ~(X264_CPU_SSE|X264_CPU_SSE2);
167 var_Get( p_enc, SOUT_CFG_PREFIX "analyse", &val );
168 if( !strcmp( val.psz_string, "none" ) )
170 p_sys->param.analyse.inter = 0;
172 else if( !strcmp( val.psz_string, "fast" ) )
174 p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
176 else if( !strcmp( val.psz_string, "normal" ) )
178 p_sys->param.analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16;
180 else if( !strcmp( val.psz_string, "all" ) )
182 p_sys->param.analyse.inter = X264_ANALYSE_I4x4 |
183 X264_ANALYSE_PSUB16x16 | X264_ANALYSE_PSUB8x8;
185 /* Open the encoder */
186 p_sys->h = x264_encoder_open( &p_sys->param );
189 p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width * p_enc->fmt_in.video.i_height + 1000;
190 p_sys->p_buffer = malloc( p_sys->i_buffer );
192 /* get the globals headers */
193 p_enc->fmt_out.i_extra = 0;
194 p_enc->fmt_out.p_extra = NULL;
197 x264_encoder_headers( p_sys->h, &nal, &i_nal );
198 for( i = 0; i < i_nal; i++ )
200 int i_size = p_sys->i_buffer;
202 x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
204 p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
206 memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
207 p_sys->p_buffer, i_size );
209 p_enc->fmt_out.i_extra += i_size;
216 /****************************************************************************
218 ****************************************************************************/
219 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
221 encoder_sys_t *p_sys = p_enc->p_sys;
230 memset( &pic, 0, sizeof( x264_picture_t ) );
231 pic.img.i_csp = X264_CSP_I420;
232 pic.img.i_plane = p_pict->i_planes;
233 for( i = 0; i < p_pict->i_planes; i++ )
235 pic.img.plane[i] = p_pict->p[i].p_pixels;
236 pic.img.i_stride[i] = p_pict->p[i].i_pitch;
239 x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
240 for( i = 0, i_out = 0; i < i_nal; i++ )
242 int i_size = p_sys->i_buffer - i_out;
243 x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
248 p_block = block_New( p_enc, i_out );
249 p_block->i_dts = p_pict->date;
250 p_block->i_pts = p_pict->date;
251 memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
253 if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
254 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
255 else if( pic.i_type == X264_TYPE_P )
256 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
257 else if( pic.i_type == X264_TYPE_B )
258 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
263 /*****************************************************************************
264 * CloseEncoder: ffmpeg encoder destruction
265 *****************************************************************************/
266 static void Close( vlc_object_t *p_this )
268 encoder_t *p_enc = (encoder_t *)p_this;
269 encoder_sys_t *p_sys = p_enc->p_sys;
272 x264_encoder_close( p_sys->h );
273 free( p_sys->p_buffer );