]> git.sesse.net Git - vlc/blob - modules/codec/x264.c
* compilation fix.
[vlc] / modules / codec / x264.c
1 /*****************************************************************************
2  * x264.c: h264 video encoder
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/sout.h>
30 #include <vlc/decoder.h>
31
32 #include <x264.h>
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 static int  Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
39
40 #define SOUT_CFG_PREFIX "sout-x264-"
41 static char *enc_analyse_list[] = {
42     "all", "normal", "fast", "none"
43 };
44
45 static char *enc_analyse_list_text[] = {
46     N_("all"), N_("normal"), N_("fast"), N_("none")
47 };
48
49 vlc_module_begin();
50     set_description( _("h264 video encoder using x264 library"));
51     set_capability( "encoder", 200 );
52
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 );
56
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 );
60 vlc_module_end();
61
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static const char *ppsz_sout_options[] = {
67     "qp", "cabac", "loopfilter", "analyse", NULL
68 };
69
70 static block_t *Encode( encoder_t *, picture_t * );
71
72 struct encoder_sys_t
73 {
74     x264_t          *h;
75     x264_param_t    param;
76
77     int             i_buffer;
78     uint8_t         *p_buffer;
79 };
80
81 /*****************************************************************************
82  * Open: probe the encoder
83  *****************************************************************************/
84 static int  Open ( vlc_object_t *p_this )
85 {
86     encoder_t     *p_enc = (encoder_t *)p_this;
87     encoder_sys_t *p_sys;
88     vlc_value_t    val;
89
90     if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) && !p_enc->b_force )
91     {
92         return VLC_EGENERIC;
93     }
94     if( p_enc->fmt_in.video.i_width % 16 != 0 ||
95         p_enc->fmt_in.video.i_height % 16!= 0 )
96     {
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 );
100         return VLC_EGENERIC;
101     }
102
103     sout_CfgParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
104
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');
107
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 ) );
111
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 )
117     {
118         p_sys->param.i_iframe = p_enc->i_iframes;
119     }
120     var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
121     if( val.i_int >= 1 && val.i_int <= 51 )
122     {
123         p_sys->param.i_qp_constant = val.i_int;
124     }
125
126     var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
127     p_sys->param.b_cabac = val.b_bool;
128
129     var_Get( p_enc, SOUT_CFG_PREFIX "loopfilter", &val );
130     p_sys->param.b_deblocking_filter = val.b_bool;
131
132     if( p_enc->fmt_in.video.i_aspect > 0 )
133     {
134         p_sys->param.vui.i_sar_width = p_enc->fmt_in.video.i_aspect *
135                                        p_enc->fmt_in.video.i_height *
136                                        p_enc->fmt_in.video.i_height /
137                                        p_enc->fmt_in.video.i_width;
138         p_sys->param.vui.i_sar_height = p_enc->fmt_in.video.i_height;
139     }
140     if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
141     {
142         p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
143         p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
144     }
145     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
146     {
147         p_sys->param.cpu &= ~X264_CPU_MMX;
148     }
149     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
150     {
151         p_sys->param.cpu &= ~X264_CPU_MMXEXT;
152     }
153     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
154     {
155         p_sys->param.cpu &= ~(X264_CPU_SSE|X264_CPU_SSE2);
156     }
157
158     var_Get( p_enc, SOUT_CFG_PREFIX "analyse", &val );
159     if( !strcmp( val.psz_string, "none" ) )
160     {
161         p_sys->param.analyse.inter = 0;
162     }
163     else if( !strcmp( val.psz_string, "fast" ) )
164     {
165         p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
166     }
167     else if( !strcmp( val.psz_string, "normal" ) )
168     {
169         p_sys->param.analyse.inter = X264_ANALYSE_I4x4  | X264_ANALYSE_PSUB16x16;
170     }
171     else if( !strcmp( val.psz_string, "all" ) )
172     {
173         p_sys->param.analyse.inter = X264_ANALYSE_I4x4  |
174                                      X264_ANALYSE_PSUB16x16 | X264_ANALYSE_PSUB8x8;
175     }
176     /* Open the encoder */
177     p_sys->h = x264_encoder_open( &p_sys->param );
178
179     /* alloc mem */
180     p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width * p_enc->fmt_in.video.i_height + 1000;
181     p_sys->p_buffer = malloc( p_sys->i_buffer );
182
183     /* get the globals headers */
184     p_enc->fmt_out.i_extra = 0;
185     p_enc->fmt_out.p_extra = NULL;
186
187 #if 0
188     x264_encoder_headers( p_sys->h, &nal, &i_nal );
189     for( i = 0; i < i_nal; i++ )
190     {
191         int i_size = p_sys->i_buffer;
192
193         x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
194
195         p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
196
197         memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
198                 p_sys->p_buffer, i_size );
199
200         p_enc->fmt_out.i_extra += i_size;
201     }
202 #endif
203
204     return VLC_SUCCESS;
205 }
206
207 /****************************************************************************
208  * Encode:
209  ****************************************************************************/
210 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
211 {
212     encoder_sys_t *p_sys = p_enc->p_sys;
213     x264_picture_t  pic;
214     int        i_nal;
215     x264_nal_t *nal;
216     block_t *p_block;
217     int i_out;
218     int i;
219
220     /* init pic */
221     memset( &pic, 0, sizeof( x264_picture_t ) );
222     pic.img.i_csp = X264_CSP_I420;
223     pic.img.i_plane = p_pict->i_planes;
224     for( i = 0; i < p_pict->i_planes; i++ )
225     {
226         pic.img.plane[i] = p_pict->p[i].p_pixels;
227         pic.img.i_stride[i] = p_pict->p[i].i_pitch;
228     }
229
230     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
231     for( i = 0, i_out = 0; i < i_nal; i++ )
232     {
233         int i_size = p_sys->i_buffer - i_out;
234         x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
235
236         i_out += i_size;
237     }
238
239     p_block = block_New( p_enc, i_out );
240     p_block->i_dts = p_pict->date;
241     p_block->i_pts = p_pict->date;
242     memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
243
244     if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
245         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
246     else if( pic.i_type == X264_TYPE_P )
247         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
248     else if( pic.i_type == X264_TYPE_B )
249         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
250
251     return p_block;
252 }
253
254 /*****************************************************************************
255  * CloseEncoder: ffmpeg encoder destruction
256  *****************************************************************************/
257 static void Close( vlc_object_t *p_this )
258 {
259     encoder_t     *p_enc = (encoder_t *)p_this;
260     encoder_sys_t *p_sys = p_enc->p_sys;
261
262
263     x264_encoder_close( p_sys->h );
264     free( p_sys->p_buffer );
265     free( p_sys );
266 }