]> git.sesse.net Git - vlc/blob - modules/codec/x264.c
ALL: backport of 13058,13059,13070,13088,13090,13091,13099 from trunk.
[vlc] / modules / codec / x264.c
1 /*****************************************************************************
2  * x264.c: h264 video encoder
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
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 TOLERANCE_TEXT N_("Bitrate tolerance")
66 #define TOLERANCE_LONGTEXT N_( "Sets the allowed variance in average " \
67     "bitrate.")
68
69 #define VBV_MAXRATE_TEXT N_("Maximum local bitrate")
70 #define VBV_MAXRATE_LONGTEXT N_( "Sets a maximum local bitrate in kbits/s.")
71
72 #define VBV_BUFSIZE_TEXT N_("Averaging period for the maximum local bitrate")
73 #define VBV_BUFSIZE_LONGTEXT N_( "Sets an averaging period for the maximum " \
74     "local bitrate, in kbits/s.")
75
76 #define VBV_INIT_TEXT N_("Initial buffer occupancy")
77 #define VBV_INIT_LONGTEXT N_( "Sets the initial buffer occupancy as a " \
78     "fraction of the buffer size.")
79
80 #define KEYINT_TEXT N_("Sets maximum interval between IDR-frames")
81 #define KEYINT_LONGTEXT N_( "Larger values save bits, thus improve quality "\
82     "for a given bitrate, at the cost of seeking precision." )
83
84 #define KEYINT_MIN_TEXT N_("Sets minimum interval between IDR-frames")
85 #define KEYINT_MIN_LONGTEXT N_("In H.264, I-Frames do not necessarily bound " \
86     "a closed GOP because it is allowable for a P-frame to be predicted from "\
87     "more frames than just the one frame before it (also see frameref). " \
88     "Therefore, I-frames are not necessarily seekable. " \
89     "IDR-Frames restrict subsequent P-frames from referring to any frame " \
90     "prior to the IDR-Frame. \n" \
91     "If scenecuts appear within this interval, they are still encoded as " \
92     "I-frames, but do not start a new GOP. Default value is keyint * 0.4." )
93
94 #define BFRAMES_TEXT N_("B frames")
95 #define BFRAMES_LONGTEXT N_( "Number of consecutive B-Frames between I and " \
96     "P-frames." )
97
98 #define BPYRAMID_TEXT N_("B pyramid")
99 #define BPYRAMID_LONGTEXT N_( "Allows B-frames to be used as references for " \
100     "predicting other frames." )
101
102 #define FRAMEREF_TEXT N_("Number of previous frames used as predictors.")
103 #define FRAMEREF_LONGTEXT N_( "This is effective in Anime, but seems to " \
104     "make little difference in live-action source material. Some decoders " \
105     "are unable to deal with large frameref values." )
106
107 #define SCENE_TEXT N_("Scene-cut detection.")
108 #define SCENE_LONGTEXT N_( "Controls how aggressively to insert extra " \
109     "I-frames. With small values of scenecut, the codec often has to force " \
110     "an I-frame when it would exceed keyint. " \
111     "Good values of scenecut may find a better location for the I-frame. " \
112     "Large values use more I-frames than necessary, thus wasting bits. " \
113     "-1 disables scene-cut detection, so I-frames are be inserted only every "\
114     "other keyint frames, which probably leads to ugly encoding artifacts." )
115
116 #define SUBPEL_TEXT N_("Sub-pixel refinement quality.")
117 #define SUBPEL_LONGTEXT N_( "This parameter controls quality versus speed " \
118     "tradeoffs involved in the motion estimation decision process " \
119     "(lower = quicker and higher = better quality)." )
120
121 static char *enc_analyse_list[] =
122   { "", "all", "normal", "fast", "none" };
123 static char *enc_analyse_list_text[] =
124   { N_("default"), N_("all"), N_("slow"), N_("normal"),
125     N_("fast"), N_("none") };
126
127 vlc_module_begin();
128     set_description( _("H264 encoder (using x264 library)"));
129     set_capability( "encoder", 200 );
130     set_callbacks( Open, Close );
131     set_category( CAT_INPUT );
132     set_subcategory( SUBCAT_INPUT_VCODEC );
133
134     add_integer( SOUT_CFG_PREFIX "qp", 0, NULL, QP_TEXT, QP_LONGTEXT,
135                  VLC_FALSE );
136         change_integer_range( 0, 51 );
137     add_integer( SOUT_CFG_PREFIX "qp-min", 10, NULL, QPMIN_TEXT,
138                  QPMIN_LONGTEXT, VLC_FALSE );
139         change_integer_range( 0, 51 );
140     add_integer( SOUT_CFG_PREFIX "qp-max", 51, NULL, QPMAX_TEXT,
141                  QPMAX_LONGTEXT, VLC_FALSE );
142         change_integer_range( 0, 51 );
143
144     add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
145               VLC_FALSE );
146
147     add_bool( SOUT_CFG_PREFIX "loopfilter", 1, NULL, LOOPF_TEXT,
148               LOOPF_LONGTEXT, VLC_FALSE );
149
150     add_string( SOUT_CFG_PREFIX "analyse", "", NULL, ANALYSE_TEXT,
151                 ANALYSE_LONGTEXT, VLC_FALSE );
152         change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
153
154     add_float( SOUT_CFG_PREFIX "tolerance", 1.0, NULL, TOLERANCE_TEXT,
155                TOLERANCE_LONGTEXT, VLC_FALSE );
156         change_float_range( 0, 100 );
157
158     add_integer( SOUT_CFG_PREFIX "vbv-maxrate", 0, NULL, VBV_MAXRATE_TEXT,
159                  VBV_MAXRATE_LONGTEXT, VLC_FALSE );
160
161     add_integer( SOUT_CFG_PREFIX "vbv-bufsize", 0, NULL, VBV_BUFSIZE_TEXT,
162                  VBV_BUFSIZE_LONGTEXT, VLC_FALSE );
163
164     add_float( SOUT_CFG_PREFIX "vbv-init", 0.9, NULL, VBV_INIT_TEXT,
165                VBV_INIT_LONGTEXT, VLC_FALSE );
166         change_float_range( 0, 1 );
167
168     add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
169                  KEYINT_LONGTEXT, VLC_FALSE );
170
171     add_integer( SOUT_CFG_PREFIX "keyint-min", 0, NULL, KEYINT_MIN_TEXT,
172                  KEYINT_MIN_LONGTEXT, VLC_FALSE );
173
174     add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
175                  BFRAMES_LONGTEXT, VLC_FALSE );
176         change_integer_range( 0, 16 );
177
178     add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
179               BPYRAMID_LONGTEXT, VLC_FALSE );
180
181     add_integer( SOUT_CFG_PREFIX "frameref", 1, NULL, FRAMEREF_TEXT,
182                  FRAMEREF_LONGTEXT, VLC_FALSE );
183         change_integer_range( 1, 15 );
184
185     add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
186                  SCENE_LONGTEXT, VLC_FALSE );
187         change_integer_range( -1, 100 );
188
189 #if X264_BUILD >= 30
190     add_integer( SOUT_CFG_PREFIX "subpel", 6, NULL, SUBPEL_TEXT,
191                  SUBPEL_LONGTEXT, VLC_FALSE );
192         change_integer_range( 1, 6 );
193 #else
194     add_integer( SOUT_CFG_PREFIX "subpel", 5, NULL, SUBPEL_TEXT,
195                  SUBPEL_LONGTEXT, VLC_FALSE );
196         change_integer_range( 1, 5 );
197 #endif
198
199 vlc_module_end();
200
201 /*****************************************************************************
202  * Local prototypes
203  *****************************************************************************/
204 static const char *ppsz_sout_options[] = {
205     "qp", "qp-min", "qp-max", "cabac", "loopfilter", "analyse",
206     "keyint", "keyint-min", "bframes", "bpyramid", "frameref", "scenecut",
207     "subpel", "tolerance", "vbv-maxrate", "vbv-bufsize", "vbv-init", NULL
208 };
209
210 static block_t *Encode( encoder_t *, picture_t * );
211
212 struct encoder_sys_t
213 {
214     x264_t          *h;
215     x264_param_t    param;
216
217     int             i_buffer;
218     uint8_t         *p_buffer;
219
220     mtime_t         i_last_ref_pts;
221 };
222
223 /*****************************************************************************
224  * Open: probe the encoder
225  *****************************************************************************/
226 static int  Open ( vlc_object_t *p_this )
227 {
228     encoder_t     *p_enc = (encoder_t *)p_this;
229     encoder_sys_t *p_sys;
230     vlc_value_t    val;
231     int i_qmin = 0, i_qmax = 0;
232
233     if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
234         !p_enc->b_force )
235     {
236         return VLC_EGENERIC;
237     }
238
239 #if X264_BUILD < 37
240     if( p_enc->fmt_in.video.i_width % 16 != 0 ||
241         p_enc->fmt_in.video.i_height % 16!= 0 )
242     {
243         msg_Warn( p_enc, "size is not a multiple of 16 (%ix%i)",
244                   p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
245
246         if( p_enc->fmt_in.video.i_width < 16 ||
247             p_enc->fmt_in.video.i_height < 16 )
248         {
249             msg_Err( p_enc, "video is too small to be cropped" );
250             return VLC_EGENERIC;
251         }
252
253         msg_Warn( p_enc, "cropping video to %ix%i",
254                   p_enc->fmt_in.video.i_width >> 4 << 4,
255                   p_enc->fmt_in.video.i_height >> 4 << 4 );
256     }
257 #endif
258
259     sout_CfgParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
260
261     p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
262     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
263
264     p_enc->pf_encode_video = Encode;
265     p_enc->pf_encode_audio = NULL;
266     p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
267     p_sys->i_last_ref_pts = 0;
268
269     x264_param_default( &p_sys->param );
270     p_sys->param.i_width  = p_enc->fmt_in.video.i_width;
271     p_sys->param.i_height = p_enc->fmt_in.video.i_height;
272 #if X264_BUILD < 37
273     p_sys->param.i_width  = p_sys->param.i_width >> 4 << 4;
274     p_sys->param.i_height = p_sys->param.i_height >> 4 << 4;
275 #endif
276
277     var_Get( p_enc, SOUT_CFG_PREFIX "qp-min", &val );
278     if( val.i_int >= 1 && val.i_int <= 51 ) i_qmin = val.i_int;
279     var_Get( p_enc, SOUT_CFG_PREFIX "qp-max", &val );
280     if( val.i_int >= 1 && val.i_int <= 51 ) i_qmax = val.i_int;
281
282     var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
283     if( val.i_int >= 1 && val.i_int <= 51 )
284     {
285         if( i_qmin > val.i_int ) i_qmin = val.i_int;
286         if( i_qmax < val.i_int ) i_qmax = val.i_int;
287
288 #if X264_BUILD >= 0x000a
289         p_sys->param.rc.i_qp_constant = val.i_int;
290         p_sys->param.rc.i_qp_min = i_qmin;
291         p_sys->param.rc.i_qp_max = i_qmax;
292 #else
293         p_sys->param.i_qp_constant = val.i_int;
294 #endif
295     }
296     else
297     {
298         /* No QP -> constant bitrate */
299 #if X264_BUILD >= 0x000a
300         p_sys->param.rc.b_cbr = 1;
301         p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
302
303 #if X264_BUILD >= 24
304         var_Get( p_enc, SOUT_CFG_PREFIX "tolerance", &val );
305         p_sys->param.rc.f_rate_tolerance = val.f_float;
306
307         var_Get( p_enc, SOUT_CFG_PREFIX "vbv-maxrate", &val );
308         p_sys->param.rc.i_vbv_max_bitrate = val.i_int;
309
310         var_Get( p_enc, SOUT_CFG_PREFIX "vbv-bufsize", &val );
311         p_sys->param.rc.i_vbv_buffer_size = val.i_int;
312         if( !val.i_int )
313             p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate;
314
315         var_Get( p_enc, SOUT_CFG_PREFIX "vbv-init", &val );
316         p_sys->param.rc.f_vbv_buffer_init = val.f_float;
317 #else
318         p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
319         p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
320 #endif
321 #endif
322     }
323
324     var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
325     p_sys->param.b_cabac = val.b_bool;
326
327     var_Get( p_enc, SOUT_CFG_PREFIX "loopfilter", &val );
328     p_sys->param.b_deblocking_filter = val.b_bool;
329
330     var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
331 #if X264_BUILD >= 0x000e
332     if( val.i_int > 0 ) p_sys->param.i_keyint_max = val.i_int;
333     if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int * 0.4;
334 #else
335     if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
336 #endif
337
338     var_Get( p_enc, SOUT_CFG_PREFIX "keyint-min", &val );
339 #if X264_BUILD >= 0x000e
340     if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int;
341 #else
342     if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
343 #endif
344
345     var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
346     if( val.i_int >= 0 && val.i_int <= 16 ) p_sys->param.i_bframe = val.i_int;
347
348 #if X264_BUILD >= 22
349     var_Get( p_enc, SOUT_CFG_PREFIX "bpyramid", &val );
350     p_sys->param.b_bframe_pyramid = val.b_bool;
351 #endif
352
353     var_Get( p_enc, SOUT_CFG_PREFIX "frameref", &val );
354     if( val.i_int > 0 && val.i_int <= 15 )
355         p_sys->param.i_frame_reference = val.i_int;
356
357     var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
358 #if X264_BUILD >= 0x000b
359     if( val.i_int >= -1 && val.i_int <= 100 )
360         p_sys->param.i_scenecut_threshold = val.i_int;
361 #endif
362
363 #if X264_BUILD >= 22
364     var_Get( p_enc, SOUT_CFG_PREFIX "subpel", &val );
365 #if X264_BUILD >= 30
366     if( val.i_int >= 1 && val.i_int <= 6 )
367 #else
368     if( val.i_int >= 1 && val.i_int <= 5 )
369 #endif
370         p_sys->param.analyse.i_subpel_refine = val.i_int;
371 #endif
372
373 #ifndef X264_ANALYSE_BSUB16x16
374 #   define X264_ANALYSE_BSUB16x16 0
375 #endif
376     var_Get( p_enc, SOUT_CFG_PREFIX "analyse", &val );
377     if( !strcmp( val.psz_string, "none" ) )
378     {
379         p_sys->param.analyse.inter = 0;
380     }
381     else if( !strcmp( val.psz_string, "fast" ) )
382     {
383         p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
384     }
385     else if( !strcmp( val.psz_string, "normal" ) )
386     {
387         p_sys->param.analyse.inter =
388             X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16;
389     }
390     else if( !strcmp( val.psz_string, "slow" ) )
391     {
392         p_sys->param.analyse.inter =
393             X264_ANALYSE_I4x4 |
394             X264_ANALYSE_PSUB16x16 | X264_ANALYSE_PSUB8x8 |
395             X264_ANALYSE_BSUB16x16;
396     }
397     else if( !strcmp( val.psz_string, "all" ) )
398     {
399         p_sys->param.analyse.inter =
400             X264_ANALYSE_I4x4 |
401             X264_ANALYSE_PSUB16x16 | X264_ANALYSE_PSUB8x8 |
402             X264_ANALYSE_BSUB16x16;
403 #ifdef X264_ANALYSE_I8x8
404         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
405         p_sys->param.analyse.b_transform_8x8 = 1;
406 #endif
407     }
408     if( val.psz_string ) free( val.psz_string );
409
410     if( p_enc->fmt_in.video.i_aspect > 0 )
411     {
412         int64_t i_num, i_den;
413         int i_dst_num, i_dst_den;
414
415         i_num = p_enc->fmt_in.video.i_aspect *
416             (int64_t)p_enc->fmt_in.video.i_height;
417         i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
418         vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
419
420         p_sys->param.vui.i_sar_width = i_dst_num;
421         p_sys->param.vui.i_sar_height = i_dst_den;
422     }
423     if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
424     {
425         p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
426         p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
427     }
428     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
429     {
430         p_sys->param.cpu &= ~X264_CPU_MMX;
431     }
432     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
433     {
434         p_sys->param.cpu &= ~X264_CPU_MMXEXT;
435     }
436     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
437     {
438         p_sys->param.cpu &= ~X264_CPU_SSE;
439     }
440     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2) )
441     {
442         p_sys->param.cpu &= ~X264_CPU_SSE2;
443     }
444
445 #if X264_BUILD >= 29
446     if( p_enc->i_threads >= 1 )
447         p_sys->param.i_threads = p_enc->i_threads;
448 #endif
449
450     /* Open the encoder */
451     p_sys->h = x264_encoder_open( &p_sys->param );
452
453     /* alloc mem */
454     p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
455         p_enc->fmt_in.video.i_height + 1000;
456     p_sys->p_buffer = malloc( p_sys->i_buffer );
457
458     /* get the globals headers */
459     p_enc->fmt_out.i_extra = 0;
460     p_enc->fmt_out.p_extra = NULL;
461
462 #if 0
463     x264_encoder_headers( p_sys->h, &nal, &i_nal );
464     for( i = 0; i < i_nal; i++ )
465     {
466         int i_size = p_sys->i_buffer;
467
468         x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
469
470         p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
471
472         memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
473                 p_sys->p_buffer, i_size );
474
475         p_enc->fmt_out.i_extra += i_size;
476     }
477 #endif
478
479     return VLC_SUCCESS;
480 }
481
482 /****************************************************************************
483  * Encode:
484  ****************************************************************************/
485 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
486 {
487     encoder_sys_t *p_sys = p_enc->p_sys;
488     x264_picture_t pic;
489     x264_nal_t *nal;
490     block_t *p_block;
491     int i_nal, i_out, i;
492
493     /* init pic */
494     memset( &pic, 0, sizeof( x264_picture_t ) );
495     pic.i_pts = p_pict->date;
496     pic.img.i_csp = X264_CSP_I420;
497     pic.img.i_plane = p_pict->i_planes;
498     for( i = 0; i < p_pict->i_planes; i++ )
499     {
500         pic.img.plane[i] = p_pict->p[i].p_pixels;
501         pic.img.i_stride[i] = p_pict->p[i].i_pitch;
502     }
503
504 #if X264_BUILD >= 0x0013
505     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic, &pic );
506 #else
507     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
508 #endif
509
510     if( !i_nal ) return NULL;
511
512     for( i = 0, i_out = 0; i < i_nal; i++ )
513     {
514         int i_size = p_sys->i_buffer - i_out;
515         x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
516
517         i_out += i_size;
518     }
519
520     p_block = block_New( p_enc, i_out );
521     memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
522
523     if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
524         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
525     else if( pic.i_type == X264_TYPE_P )
526         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
527     else if( pic.i_type == X264_TYPE_B )
528         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
529
530     /* This isn't really valid for streams with B-frames */
531     p_block->i_length = I64C(1000000) *
532         p_enc->fmt_in.video.i_frame_rate_base /
533             p_enc->fmt_in.video.i_frame_rate;
534
535     p_block->i_dts = p_block->i_pts = pic.i_pts;
536
537     if( p_sys->param.i_bframe > 0 )
538     {
539         if( p_block->i_flags & BLOCK_FLAG_TYPE_B )
540         {
541             p_block->i_dts = p_block->i_pts;
542         }
543         else
544         {
545             if( p_sys->i_last_ref_pts )
546             {
547                 p_block->i_dts = p_sys->i_last_ref_pts;
548             }
549             else
550             {
551                 /* Let's put something sensible */
552                 p_block->i_dts = p_block->i_pts;
553             }
554
555             p_sys->i_last_ref_pts = p_block->i_pts;
556         }
557     }
558
559     return p_block;
560 }
561
562 /*****************************************************************************
563  * CloseEncoder: x264 encoder destruction
564  *****************************************************************************/
565 static void Close( vlc_object_t *p_this )
566 {
567     encoder_t     *p_enc = (encoder_t *)p_this;
568     encoder_sys_t *p_sys = p_enc->p_sys;
569
570     x264_encoder_close( p_sys->h );
571     free( p_sys->p_buffer );
572     free( p_sys );
573 }