1 /*****************************************************************************
2 * x264.c: h264 video encoder
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: encoder.c 7342 2004-04-13 14:13:07Z gbazin $
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 *****************************************************************************/
29 #include <vlc/decoder.h>
33 /*****************************************************************************
35 *****************************************************************************/
36 static int Open ( vlc_object_t * );
37 static void Close( vlc_object_t * );
40 set_description( _("h264 video encoder using x264 library"));
41 set_capability( "encoder", 10 );
42 set_callbacks( Open, Close );
46 /*****************************************************************************
48 *****************************************************************************/
49 static block_t *Encode( encoder_t *, picture_t * );
61 /*****************************************************************************
62 * Open: probe the encoder
63 *****************************************************************************/
64 static int Open ( vlc_object_t *p_this )
66 encoder_t *p_enc = (encoder_t *)p_this;
73 if( p_enc->fmt_out.i_cat != VIDEO_ES ||
74 p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) )
78 if( p_enc->fmt_in.video.i_width % 16 != 0 ||
79 p_enc->fmt_in.video.i_height % 16!= 0 )
81 msg_Warn( p_enc, "invalid size %ix%i",
82 p_enc->fmt_in.video.i_width,
83 p_enc->fmt_in.video.i_height );
87 p_enc->pf_encode_video = Encode;
88 p_enc->pf_encode_audio = NULL;
89 p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
91 x264_param_default( &p_sys->param );
92 p_sys->param.i_width = p_enc->fmt_in.video.i_width;
93 p_sys->param.i_height = p_enc->fmt_in.video.i_height;
94 p_sys->param.analyse.inter = X264_ANALYSE_I16x16 | X264_ANALYSE_I4x4 |
96 X264_ANALYSE_P16x8 | X264_ANALYSE_P8x16 |
97 X264_ANALYSE_P8x8 | X264_ANALYSE_SMART_PSUB;
98 p_sys->param.i_idrframe = 1;
99 p_sys->param.b_cabac = 1;
101 if( p_enc->fmt_in.video.i_aspect > 0 )
104 p_sys->param.vui.i_sar_width = 0;
105 p_sys->param.vui.i_sar_height = 0;
107 if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
109 p_sys->param.f_fps = (float)p_enc->fmt_in.video.i_frame_rate /
110 (float)p_enc->fmt_in.video.i_frame_rate_base;
112 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
114 p_sys->param.cpu &= ~X264_CPU_MMX;
116 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
118 p_sys->param.cpu &= ~X264_CPU_MMXEXT;
120 if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
122 p_sys->param.cpu &= ~(X264_CPU_SSE|X264_CPU_SSE2);
124 if( p_enc->i_key_int > 0 )
126 p_sys->param.i_iframe = p_enc->i_key_int;
129 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
130 if( p_enc->i_qmin == p_enc->i_qmax )
132 p_sys->param.i_qp_constant = p_enc->i_qmin;
134 switch( p_enc->i_hq )
137 p_sys->param.analyse.inter = 0;
140 p_sys->param.analyse.inter = X264_ANALYSE_I16x16|X264_ANALYSE_I4x4|X264_ANALYSE_P16x16;
143 p_sys->param.analyse.inter = X264_ANALYSE_I16x16|X264_ANALYSE_I4x4|X264_ANALYSE_P16x16|
144 X264_ANALYSE_P16x8|X264_ANALYSE_P8x16|X264_ANALYSE_P8x8|X264_ANALYSE_SMART_PSUB;
147 p_sys->param.analyse.inter = X264_ANALYSE_I16x16|X264_ANALYSE_I4x4|X264_ANALYSE_P16x16|
148 X264_ANALYSE_P16x8|X264_ANALYSE_P8x16|X264_ANALYSE_P8x8|
149 X264_ANALYSE_P8x4|X264_ANALYSE_P4x8|X264_ANALYSE_P4x4|
150 X264_ANALYSE_SMART_PSUB;
154 /* Open the encoder */
155 p_sys->h = x264_encoder_open( &p_sys->param );
158 p_sys->pic = x264_picture_new( p_sys->h );
159 p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width * p_enc->fmt_in.video.i_height + 1000;
160 p_sys->p_buffer = malloc( p_sys->i_buffer );
162 /* get the globals headers */
163 p_enc->fmt_out.i_extra = 0;
164 p_enc->fmt_out.p_extra = NULL;
166 x264_encoder_headers( p_sys->h, &nal, &i_nal );
167 for( i = 0; i < i_nal; i++ )
169 int i_size = p_sys->i_buffer;
171 x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
173 p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
175 memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
176 p_sys->p_buffer, i_size );
178 p_enc->fmt_out.i_extra += i_size;
185 /****************************************************************************
187 ****************************************************************************/
188 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
190 encoder_sys_t *p_sys = p_enc->p_sys;
197 /* copy the picture */
198 for( i = 0; i < 3; i++ )
200 uint8_t *src = p_pict->p[i].p_pixels;
201 uint8_t *dst = p_sys->pic->plane[i];
204 for( j = 0;j < p_pict->p[i].i_lines; j++ )
206 memcpy( dst, src, __MIN( p_sys->pic->i_stride[i], p_pict->p[i].i_pitch ) );
208 src += p_pict->p[i].i_pitch;
209 dst += p_sys->pic->i_stride[i];
213 x264_encoder_encode( p_sys->h, &nal, &i_nal, p_sys->pic );
214 for( i = 0, i_out = 0; i < i_nal; i++ )
216 int i_size = p_sys->i_buffer - i_out;
217 x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
222 p_block = block_New( p_enc, i_out );
223 p_block->i_dts = p_pict->date;
224 p_block->i_pts = p_pict->date;
225 memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
228 /* p_block->i_flags |= BLOCK_FLAG_TYPE_I; */
233 /*****************************************************************************
234 * CloseEncoder: ffmpeg encoder destruction
235 *****************************************************************************/
236 static void Close( vlc_object_t *p_this )
238 encoder_t *p_enc = (encoder_t *)p_this;
239 encoder_sys_t *p_sys = p_enc->p_sys;
242 x264_picture_delete( p_sys->pic );
243 x264_encoder_close( p_sys->h );
244 free( p_sys->p_buffer );