]> git.sesse.net Git - vlc/blob - modules/codec/x264.c
Improvements to preferences
[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 #define SOUT_CFG_PREFIX "sout-x264-"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 #define QP_TEXT N_("Quantizer parameter")
43 #define QP_LONGTEXT N_( \
44     "This selects the quantizer to use (1 to 51). Lower values result in " \
45     "better fidelity, but higher bitrates. 26 is a good default value." )
46
47 #define QPMIN_TEXT N_("Minimum quantizer parameter")
48 #define QPMIN_LONGTEXT N_( "Minimum quantizer, 15/35 seems to be a useful " \
49     "range." )
50
51 #define QPMAX_TEXT N_("Maximum quantizer parameter")
52 #define QPMAX_LONGTEXT N_( "Maximum quantizer parameter." )
53
54 #define CABAC_TEXT N_("Enable CABAC")
55 #define CABAC_LONGTEXT N_( "Enable CABAC (Context-Adaptive Binary Arithmetic "\
56     "Coding). Slightly slows down encoding and decoding, but should save " \
57     "10-15% bitrate." )
58
59 #define LOOPF_TEXT N_("Enable loop filter")
60 #define LOOPF_LONGTEXT N_( "Use deblocking loop filter (increases quality).")
61
62 #define ANALYSE_TEXT N_("Analyse mode")
63 #define ANALYSE_LONGTEXT N_( "This selects the analysing mode.")
64
65 #define KEYINT_TEXT N_("Sets maximum interval between I frames")
66 #define KEYINT_LONGTEXT N_( "Larger values save bits, thus improve quality "\
67     "for a given bitrate, at the cost of seeking precision." )
68
69 #define IDRINT_TEXT N_("IDR frames")
70 #define IDRINT_LONGTEXT N_("In H.264, I-Frames do not necessarily bound a " \
71     "closed GOP because it is allowable for a P-frame to be predicted from " \
72     "more frames than just the one frame before it (also see frameref). " \
73     "Therefore, I-frames are not necessarily seekable. " \
74     "IDR-Frames restrict subsequent P-frames from referring to any frame " \
75     "prior to the IDR-Frame." )
76
77 #define BFRAMES_TEXT N_("B frames")
78 #define BFRAMES_LONGTEXT N_( "Number of consecutive B-Frames between I and " \
79     "P-frames." )
80
81 #define FRAMEREF_TEXT N_("Number of previous frames used as predictors.")
82 #define FRAMEREF_LONGTEXT N_( "This is effective in Anime, but seems to " \
83     "make little difference in live-action source material. Some decoders " \
84     "are unable to deal with large frameref values." )
85
86 #define SCENE_TEXT N_("Scene-cut detection.")
87 #define SCENE_LONGTEXT N_( "Controls how aggressively to insert extra " \
88     "I-frames. With small values of scenecut, the codec often has to force " \
89     "an I-frame when it would exceed keyint. " \
90     "Good values of scenecut may find a better location for the I-frame. " \
91     "Large values use more I-frames than necessary, thus wasting bits. " \
92     "-1 disables scene-cut detection, so I-frames are be inserted only every "\
93     "other keyint frames, which probably leads to ugly encoding artifacts." )
94
95 static char *enc_analyse_list[] =
96   { "", "all", "normal", "fast", "none" };
97 static char *enc_analyse_list_text[] =
98   { N_("default"), N_("all"), N_("normal"), N_("fast"), N_("none") };
99
100 vlc_module_begin();
101     set_description( _("h264 video encoder using x264 library"));
102     set_capability( "encoder", 200 );
103     set_callbacks( Open, Close );
104     set_category( CAT_INPUT );
105     set_subcategory( SUBCAT_INPUT_VCODEC );
106
107     add_integer( SOUT_CFG_PREFIX "qp", 0, NULL, QP_TEXT, QP_LONGTEXT,
108                  VLC_FALSE );
109         change_integer_range( 0, 51 );
110     add_integer( SOUT_CFG_PREFIX "qp-min", 10, NULL, QPMIN_TEXT,
111                  QPMIN_LONGTEXT, VLC_FALSE );
112         change_integer_range( 0, 51 );
113     add_integer( SOUT_CFG_PREFIX "qp-max", 51, NULL, QPMAX_TEXT,
114                  QPMAX_LONGTEXT, VLC_FALSE );
115         change_integer_range( 0, 51 );
116
117     add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
118               VLC_FALSE );
119
120     add_bool( SOUT_CFG_PREFIX "loopfilter", 1, NULL, LOOPF_TEXT,
121               LOOPF_LONGTEXT, VLC_FALSE );
122
123     add_string( SOUT_CFG_PREFIX "analyse", "", NULL, ANALYSE_TEXT,
124                 ANALYSE_LONGTEXT, VLC_FALSE );
125         change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
126
127     add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
128                  KEYINT_LONGTEXT, VLC_FALSE );
129
130     add_integer( SOUT_CFG_PREFIX "idrint", 2, NULL, IDRINT_TEXT,
131                  IDRINT_LONGTEXT, VLC_FALSE );
132
133     add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
134                  BFRAMES_LONGTEXT, VLC_FALSE );
135         change_integer_range( 0, 16 );
136
137     add_integer( SOUT_CFG_PREFIX "frameref", 1, NULL, FRAMEREF_TEXT,
138                  FRAMEREF_LONGTEXT, VLC_FALSE );
139         change_integer_range( 1, 15 );
140
141     add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
142                  SCENE_LONGTEXT, VLC_FALSE );
143         change_integer_range( -1, 100 );
144
145 vlc_module_end();
146
147 /*****************************************************************************
148  * Local prototypes
149  *****************************************************************************/
150 static const char *ppsz_sout_options[] = {
151     "qp", "qp-min", "qp-max", "cabac", "loopfilter", "analyse",
152     "keyint", "idrint", "bframes", "frameref", "scenecut", NULL
153 };
154
155 static block_t *Encode( encoder_t *, picture_t * );
156
157 struct encoder_sys_t
158 {
159     x264_t          *h;
160     x264_param_t    param;
161
162     int             i_buffer;
163     uint8_t         *p_buffer;
164 };
165
166 /*****************************************************************************
167  * Open: probe the encoder
168  *****************************************************************************/
169 static int  Open ( vlc_object_t *p_this )
170 {
171     encoder_t     *p_enc = (encoder_t *)p_this;
172     encoder_sys_t *p_sys;
173     vlc_value_t    val;
174     int i_qmin = 0, i_qmax = 0;
175
176     if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
177         !p_enc->b_force )
178     {
179         return VLC_EGENERIC;
180     }
181
182     if( p_enc->fmt_in.video.i_width % 16 != 0 ||
183         p_enc->fmt_in.video.i_height % 16!= 0 )
184     {
185         msg_Warn( p_enc, "invalid size %ix%i",
186                   p_enc->fmt_in.video.i_width,
187                   p_enc->fmt_in.video.i_height );
188         return VLC_EGENERIC;
189     }
190
191     sout_CfgParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
192
193     p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
194     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
195
196     p_enc->pf_encode_video = Encode;
197     p_enc->pf_encode_audio = NULL;
198     p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
199
200     x264_param_default( &p_sys->param );
201     p_sys->param.i_width  = p_enc->fmt_in.video.i_width;
202     p_sys->param.i_height = p_enc->fmt_in.video.i_height;
203
204     var_Get( p_enc, SOUT_CFG_PREFIX "qp-min", &val );
205     if( val.i_int >= 1 && val.i_int <= 51 ) i_qmin = val.i_int;
206     var_Get( p_enc, SOUT_CFG_PREFIX "qp-max", &val );
207     if( val.i_int >= 1 && val.i_int <= 51 ) i_qmax = val.i_int;
208
209     var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
210     if( val.i_int >= 1 && val.i_int <= 51 )
211     {
212         if( i_qmin > val.i_int ) i_qmin = val.i_int;
213         if( i_qmax < val.i_int ) i_qmax = val.i_int;
214
215 #if X264_BUILD >= 0x000a
216         p_sys->param.rc.i_qp_constant = val.i_int;
217         p_sys->param.rc.i_qp_min = i_qmin;
218         p_sys->param.rc.i_qp_max = i_qmax;
219 #else
220         p_sys->param.i_qp_constant = val.i_int;
221 #endif
222     }
223     else
224     {
225         /* No QP -> constant bitrate */
226 #if X264_BUILD >= 0x000a
227         p_sys->param.rc.b_cbr = 1;
228         p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
229         p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
230         p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
231 #endif
232     }
233
234     var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
235     p_sys->param.b_cabac = val.b_bool;
236
237     var_Get( p_enc, SOUT_CFG_PREFIX "loopfilter", &val );
238     p_sys->param.b_deblocking_filter = val.b_bool;
239
240     var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
241     if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
242
243     var_Get( p_enc, SOUT_CFG_PREFIX "idrint", &val );
244     if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
245
246     var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
247     if( val.i_int >= 0 && val.i_int <= 16 ) p_sys->param.i_bframe = val.i_int;
248
249     var_Get( p_enc, SOUT_CFG_PREFIX "frameref", &val );
250     if( val.i_int > 0 && val.i_int <= 15 )
251         p_sys->param.i_frame_reference = val.i_int;
252
253     var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
254 #if X264_BUILD >= 0x000b
255     if( val.i_int >= -1 && val.i_int <= 100 )
256         p_sys->param.i_scenecut_threshold = val.i_int;
257 #endif
258
259     var_Get( p_enc, SOUT_CFG_PREFIX "analyse", &val );
260     if( !strcmp( val.psz_string, "none" ) )
261     {
262         p_sys->param.analyse.inter = 0;
263     }
264     else if( !strcmp( val.psz_string, "fast" ) )
265     {
266         p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
267     }
268     else if( !strcmp( val.psz_string, "normal" ) )
269     {
270         p_sys->param.analyse.inter =
271             X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16;
272     }
273     else if( !strcmp( val.psz_string, "all" ) )
274     {
275         p_sys->param.analyse.inter =
276             X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_PSUB8x8;
277     }
278
279     if( p_enc->fmt_in.video.i_aspect > 0 )
280     {
281         int64_t i_num, i_den;
282         int i_dst_num, i_dst_den;
283
284         i_num = p_enc->fmt_in.video.i_aspect *
285             (int64_t)p_enc->fmt_in.video.i_height;
286         i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
287         vlc_reduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
288
289         p_sys->param.vui.i_sar_width = i_dst_num;
290         p_sys->param.vui.i_sar_height = i_dst_den;
291     }
292     if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
293     {
294         p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
295         p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
296     }
297     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
298     {
299         p_sys->param.cpu &= ~X264_CPU_MMX;
300     }
301     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
302     {
303         p_sys->param.cpu &= ~X264_CPU_MMXEXT;
304     }
305     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
306     {
307         p_sys->param.cpu &= ~X264_CPU_SSE;
308     }
309     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2) )
310     {
311         p_sys->param.cpu &= ~X264_CPU_SSE2;
312     }
313
314     /* Open the encoder */
315     p_sys->h = x264_encoder_open( &p_sys->param );
316
317     /* alloc mem */
318     p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
319         p_enc->fmt_in.video.i_height + 1000;
320     p_sys->p_buffer = malloc( p_sys->i_buffer );
321
322     /* get the globals headers */
323     p_enc->fmt_out.i_extra = 0;
324     p_enc->fmt_out.p_extra = NULL;
325
326 #if 0
327     x264_encoder_headers( p_sys->h, &nal, &i_nal );
328     for( i = 0; i < i_nal; i++ )
329     {
330         int i_size = p_sys->i_buffer;
331
332         x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
333
334         p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
335
336         memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
337                 p_sys->p_buffer, i_size );
338
339         p_enc->fmt_out.i_extra += i_size;
340     }
341 #endif
342
343     return VLC_SUCCESS;
344 }
345
346 /****************************************************************************
347  * Encode:
348  ****************************************************************************/
349 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
350 {
351     encoder_sys_t *p_sys = p_enc->p_sys;
352     x264_picture_t  pic;
353     int        i_nal;
354     x264_nal_t *nal;
355     block_t *p_block;
356     int i_out;
357     int i;
358
359     /* init pic */
360     memset( &pic, 0, sizeof( x264_picture_t ) );
361     pic.img.i_csp = X264_CSP_I420;
362     pic.img.i_plane = p_pict->i_planes;
363     for( i = 0; i < p_pict->i_planes; i++ )
364     {
365         pic.img.plane[i] = p_pict->p[i].p_pixels;
366         pic.img.i_stride[i] = p_pict->p[i].i_pitch;
367     }
368
369     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
370     for( i = 0, i_out = 0; i < i_nal; i++ )
371     {
372         int i_size = p_sys->i_buffer - i_out;
373         x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
374
375         i_out += i_size;
376     }
377
378     p_block = block_New( p_enc, i_out );
379     p_block->i_dts = p_pict->date;
380     p_block->i_pts = p_pict->date;
381     memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
382
383     if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
384         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
385     else if( pic.i_type == X264_TYPE_P )
386         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
387     else if( pic.i_type == X264_TYPE_B )
388         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
389
390     return p_block;
391 }
392
393 /*****************************************************************************
394  * CloseEncoder: ffmpeg encoder destruction
395  *****************************************************************************/
396 static void Close( vlc_object_t *p_this )
397 {
398     encoder_t     *p_enc = (encoder_t *)p_this;
399     encoder_sys_t *p_sys = p_enc->p_sys;
400
401     x264_encoder_close( p_sys->h );
402     free( p_sys->p_buffer );
403     free( p_sys );
404 }