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