]> git.sesse.net Git - vlc/blob - modules/codec/x264.c
* modules/codec/x264.c: added bpyramid and subpel options + support for X264_ANALYSE_...
[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 IDR-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 KEYINT_MIN_TEXT N_("Sets minimum interval between IDR-frames")
70 #define KEYINT_MIN_LONGTEXT N_("In H.264, I-Frames do not necessarily bound " \
71     "a 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. \n" \
76     "If scenecuts appear within this interval, they are still encoded as " \
77     "I-frames, but do not start a new GOP. Default value is keyint * 0.4." )
78
79 #define BFRAMES_TEXT N_("B frames")
80 #define BFRAMES_LONGTEXT N_( "Number of consecutive B-Frames between I and " \
81     "P-frames." )
82
83 #define BPYRAMID_TEXT N_("B pyramid")
84 #define BPYRAMID_LONGTEXT N_( "Allows B-frames to be used as references for " \
85     "predicting other frames." )
86
87 #define FRAMEREF_TEXT N_("Number of previous frames used as predictors.")
88 #define FRAMEREF_LONGTEXT N_( "This is effective in Anime, but seems to " \
89     "make little difference in live-action source material. Some decoders " \
90     "are unable to deal with large frameref values." )
91
92 #define SCENE_TEXT N_("Scene-cut detection.")
93 #define SCENE_LONGTEXT N_( "Controls how aggressively to insert extra " \
94     "I-frames. With small values of scenecut, the codec often has to force " \
95     "an I-frame when it would exceed keyint. " \
96     "Good values of scenecut may find a better location for the I-frame. " \
97     "Large values use more I-frames than necessary, thus wasting bits. " \
98     "-1 disables scene-cut detection, so I-frames are be inserted only every "\
99     "other keyint frames, which probably leads to ugly encoding artifacts." )
100
101 #define SUBPEL_TEXT N_("Sub-pixel refinement quality.")
102 #define SUBPEL_LONGTEXT N_( "This parameter controls quality versus speed " \
103     "tradeoffs involved in the motion estimation decision process " \
104     "(lower = quicker and higher = better quality)." )
105
106 static char *enc_analyse_list[] =
107   { "", "all", "normal", "fast", "none" };
108 static char *enc_analyse_list_text[] =
109   { N_("default"), N_("all"), N_("normal"), N_("fast"), N_("none") };
110
111 vlc_module_begin();
112     set_description( _("h264 video encoder using x264 library"));
113     set_capability( "encoder", 200 );
114     set_callbacks( Open, Close );
115     set_category( CAT_INPUT );
116     set_subcategory( SUBCAT_INPUT_VCODEC );
117
118     add_integer( SOUT_CFG_PREFIX "qp", 0, NULL, QP_TEXT, QP_LONGTEXT,
119                  VLC_FALSE );
120         change_integer_range( 0, 51 );
121     add_integer( SOUT_CFG_PREFIX "qp-min", 10, NULL, QPMIN_TEXT,
122                  QPMIN_LONGTEXT, VLC_FALSE );
123         change_integer_range( 0, 51 );
124     add_integer( SOUT_CFG_PREFIX "qp-max", 51, NULL, QPMAX_TEXT,
125                  QPMAX_LONGTEXT, VLC_FALSE );
126         change_integer_range( 0, 51 );
127
128     add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
129               VLC_FALSE );
130
131     add_bool( SOUT_CFG_PREFIX "loopfilter", 1, NULL, LOOPF_TEXT,
132               LOOPF_LONGTEXT, VLC_FALSE );
133
134     add_string( SOUT_CFG_PREFIX "analyse", "", NULL, ANALYSE_TEXT,
135                 ANALYSE_LONGTEXT, VLC_FALSE );
136         change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
137
138     add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
139                  KEYINT_LONGTEXT, VLC_FALSE );
140
141     add_integer( SOUT_CFG_PREFIX "keyint-min", 0, NULL, KEYINT_MIN_TEXT,
142                  KEYINT_MIN_LONGTEXT, VLC_FALSE );
143
144     add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
145                  BFRAMES_LONGTEXT, VLC_FALSE );
146         change_integer_range( 0, 16 );
147
148     add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
149               BPYRAMID_LONGTEXT, VLC_FALSE );
150
151     add_integer( SOUT_CFG_PREFIX "frameref", 1, NULL, FRAMEREF_TEXT,
152                  FRAMEREF_LONGTEXT, VLC_FALSE );
153         change_integer_range( 1, 15 );
154
155     add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
156                  SCENE_LONGTEXT, VLC_FALSE );
157         change_integer_range( -1, 100 );
158
159     add_integer( SOUT_CFG_PREFIX "subpel", 40, NULL, SUBPEL_TEXT,
160                  SUBPEL_LONGTEXT, VLC_FALSE );
161         change_integer_range( 1, 5 );
162
163 vlc_module_end();
164
165 /*****************************************************************************
166  * Local prototypes
167  *****************************************************************************/
168 static const char *ppsz_sout_options[] = {
169     "qp", "qp-min", "qp-max", "cabac", "loopfilter", "analyse",
170     "keyint", "keyint-min", "bframes", "bpyramid", "frameref", "scenecut",
171     "subpel", NULL
172 };
173
174 static block_t *Encode( encoder_t *, picture_t * );
175
176 struct encoder_sys_t
177 {
178     x264_t          *h;
179     x264_param_t    param;
180
181     int             i_buffer;
182     uint8_t         *p_buffer;
183 };
184
185 /*****************************************************************************
186  * Open: probe the encoder
187  *****************************************************************************/
188 static int  Open ( vlc_object_t *p_this )
189 {
190     encoder_t     *p_enc = (encoder_t *)p_this;
191     encoder_sys_t *p_sys;
192     vlc_value_t    val;
193     int i_qmin = 0, i_qmax = 0;
194
195     if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
196         !p_enc->b_force )
197     {
198         return VLC_EGENERIC;
199     }
200
201     if( p_enc->fmt_in.video.i_width % 16 != 0 ||
202         p_enc->fmt_in.video.i_height % 16!= 0 )
203     {
204         msg_Warn( p_enc, "invalid size %ix%i",
205                   p_enc->fmt_in.video.i_width,
206                   p_enc->fmt_in.video.i_height );
207         return VLC_EGENERIC;
208     }
209
210     sout_CfgParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
211
212     p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
213     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
214
215     p_enc->pf_encode_video = Encode;
216     p_enc->pf_encode_audio = NULL;
217     p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
218
219     x264_param_default( &p_sys->param );
220     p_sys->param.i_width  = p_enc->fmt_in.video.i_width;
221     p_sys->param.i_height = p_enc->fmt_in.video.i_height;
222
223     var_Get( p_enc, SOUT_CFG_PREFIX "qp-min", &val );
224     if( val.i_int >= 1 && val.i_int <= 51 ) i_qmin = val.i_int;
225     var_Get( p_enc, SOUT_CFG_PREFIX "qp-max", &val );
226     if( val.i_int >= 1 && val.i_int <= 51 ) i_qmax = val.i_int;
227
228     var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
229     if( val.i_int >= 1 && val.i_int <= 51 )
230     {
231         if( i_qmin > val.i_int ) i_qmin = val.i_int;
232         if( i_qmax < val.i_int ) i_qmax = val.i_int;
233
234 #if X264_BUILD >= 0x000a
235         p_sys->param.rc.i_qp_constant = val.i_int;
236         p_sys->param.rc.i_qp_min = i_qmin;
237         p_sys->param.rc.i_qp_max = i_qmax;
238 #else
239         p_sys->param.i_qp_constant = val.i_int;
240 #endif
241     }
242     else
243     {
244         /* No QP -> constant bitrate */
245 #if X264_BUILD >= 0x000a
246         p_sys->param.rc.b_cbr = 1;
247         p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
248         p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
249         p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
250 #endif
251     }
252
253     var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
254     p_sys->param.b_cabac = val.b_bool;
255
256     var_Get( p_enc, SOUT_CFG_PREFIX "loopfilter", &val );
257     p_sys->param.b_deblocking_filter = val.b_bool;
258
259     var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
260 #if X264_BUILD >= 0x000e
261     if( val.i_int > 0 ) p_sys->param.i_keyint_max = val.i_int;
262     if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int * 0.4;
263 #else
264     if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
265 #endif
266
267     var_Get( p_enc, SOUT_CFG_PREFIX "keyint-min", &val );
268 #if X264_BUILD >= 0x000e
269     if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int;
270 #else
271     if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
272 #endif
273
274     var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
275     if( val.i_int >= 0 && val.i_int <= 16 ) p_sys->param.i_bframe = val.i_int;
276
277 #if X264_BUILD >= 23
278     var_Get( p_enc, SOUT_CFG_PREFIX "bpyramid", &val );
279     p_sys->param.b_bframe_pyramid = val.b_bool;
280 #endif
281
282     var_Get( p_enc, SOUT_CFG_PREFIX "frameref", &val );
283     if( val.i_int > 0 && val.i_int <= 15 )
284         p_sys->param.i_frame_reference = val.i_int;
285
286     var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
287 #if X264_BUILD >= 0x000b
288     if( val.i_int >= -1 && val.i_int <= 100 )
289         p_sys->param.i_scenecut_threshold = val.i_int;
290 #endif
291
292 #if X264_BUILD >= 23
293     var_Get( p_enc, SOUT_CFG_PREFIX "subpel", &val );
294     if( val.i_int >= 1 && val.i_int <= 5 )
295         p_sys->param.analyse.i_subpel_refine = val.i_int;
296 #endif
297
298     var_Get( p_enc, SOUT_CFG_PREFIX "analyse", &val );
299     if( !strcmp( val.psz_string, "none" ) )
300     {
301         p_sys->param.analyse.inter = 0;
302     }
303     else if( !strcmp( val.psz_string, "fast" ) )
304     {
305         p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
306     }
307     else if( !strcmp( val.psz_string, "normal" ) )
308     {
309         p_sys->param.analyse.inter =
310             X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16;
311     }
312     else if( !strcmp( val.psz_string, "all" ) )
313     {
314 #ifndef X264_ANALYSE_BSUB16x16
315 #   define X264_ANALYSE_BSUB16x16 0
316 #endif
317         p_sys->param.analyse.inter =
318             X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_PSUB8x8 |
319             X264_ANALYSE_BSUB16x16;
320     }
321     if( val.psz_string ) free( val.psz_string );
322
323     if( p_enc->fmt_in.video.i_aspect > 0 )
324     {
325         int64_t i_num, i_den;
326         int i_dst_num, i_dst_den;
327
328         i_num = p_enc->fmt_in.video.i_aspect *
329             (int64_t)p_enc->fmt_in.video.i_height;
330         i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
331         vlc_reduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
332
333         p_sys->param.vui.i_sar_width = i_dst_num;
334         p_sys->param.vui.i_sar_height = i_dst_den;
335     }
336     if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
337     {
338         p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
339         p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
340     }
341     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
342     {
343         p_sys->param.cpu &= ~X264_CPU_MMX;
344     }
345     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
346     {
347         p_sys->param.cpu &= ~X264_CPU_MMXEXT;
348     }
349     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
350     {
351         p_sys->param.cpu &= ~X264_CPU_SSE;
352     }
353     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2) )
354     {
355         p_sys->param.cpu &= ~X264_CPU_SSE2;
356     }
357
358     /* Open the encoder */
359     p_sys->h = x264_encoder_open( &p_sys->param );
360
361     /* alloc mem */
362     p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
363         p_enc->fmt_in.video.i_height + 1000;
364     p_sys->p_buffer = malloc( p_sys->i_buffer );
365
366     /* get the globals headers */
367     p_enc->fmt_out.i_extra = 0;
368     p_enc->fmt_out.p_extra = NULL;
369
370 #if 0
371     x264_encoder_headers( p_sys->h, &nal, &i_nal );
372     for( i = 0; i < i_nal; i++ )
373     {
374         int i_size = p_sys->i_buffer;
375
376         x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
377
378         p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
379
380         memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
381                 p_sys->p_buffer, i_size );
382
383         p_enc->fmt_out.i_extra += i_size;
384     }
385 #endif
386
387     return VLC_SUCCESS;
388 }
389
390 /****************************************************************************
391  * Encode:
392  ****************************************************************************/
393 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
394 {
395     encoder_sys_t *p_sys = p_enc->p_sys;
396     x264_picture_t pic;
397     x264_nal_t *nal;
398     block_t *p_block;
399     int i_nal, i_out, i;
400
401     /* init pic */
402     memset( &pic, 0, sizeof( x264_picture_t ) );
403     pic.img.i_csp = X264_CSP_I420;
404     pic.img.i_plane = p_pict->i_planes;
405     for( i = 0; i < p_pict->i_planes; i++ )
406     {
407         pic.img.plane[i] = p_pict->p[i].p_pixels;
408         pic.img.i_stride[i] = p_pict->p[i].i_pitch;
409     }
410
411 #if X264_BUILD >= 0x0013
412     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic, &pic );
413 #else
414     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
415 #endif
416     for( i = 0, i_out = 0; i < i_nal; i++ )
417     {
418         int i_size = p_sys->i_buffer - i_out;
419         x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
420
421         i_out += i_size;
422     }
423
424     p_block = block_New( p_enc, i_out );
425     p_block->i_dts = p_pict->date;
426     p_block->i_pts = p_pict->date;
427     memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
428
429     if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
430         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
431     else if( pic.i_type == X264_TYPE_P )
432         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
433     else if( pic.i_type == X264_TYPE_B )
434         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
435
436     return p_block;
437 }
438
439 /*****************************************************************************
440  * CloseEncoder: ffmpeg encoder destruction
441  *****************************************************************************/
442 static void Close( vlc_object_t *p_this )
443 {
444     encoder_t     *p_enc = (encoder_t *)p_this;
445     encoder_sys_t *p_sys = p_enc->p_sys;
446
447     x264_encoder_close( p_sys->h );
448     free( p_sys->p_buffer );
449     free( p_sys );
450 }