1 /*****************************************************************************
3 * - H.264 encoder for mencoder using x264 -
5 * Copyright (C) 2004 LINUX4MEDIA GmbH
6 * Copyright (C) 2004 Ark Linux
8 * Written by Bernhard Rosenkraenzer <bero@arklinux.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation, or if, and only if,
13 * version 2 is ruled invalid in a court of law, any later version
14 * of the GNU General Public License published by the Free Software
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *****************************************************************************/
37 #include "codec-cfg.h"
44 #include "img_format.h"
50 typedef struct _h264_module_t
{
57 extern char* passtmpfile
;
59 static int bitrate
= -1;
60 static int qp_constant
= 26;
61 static int rf_constant
= 0;
62 static int frame_ref
= 1;
63 static int keyint_max
= 250;
64 static int keyint_min
= 25;
65 static int scenecut_threshold
= 40;
66 static int bframe
= 0;
67 static int bframe_adaptive
= 1;
68 static int bframe_bias
= 0;
69 static int bframe_pyramid
= 0;
70 static int deblock
= 1;
71 static int deblockalpha
= 0;
72 static int deblockbeta
= 0;
74 static int p4x4mv
= 0;
75 static int p8x8mv
= 1;
76 static int b8x8mv
= 1;
80 static int direct_pred
= X264_DIRECT_PRED_SPATIAL
;
81 static int weight_b
= 0;
82 static int chroma_me
= 1;
83 static int mixed_references
= 0;
84 static int chroma_qp_offset
= 0;
85 static float ip_factor
= 1.4;
86 static float pb_factor
= 1.3;
87 static float ratetol
= 1.0;
88 static int vbv_maxrate
= 0;
89 static int vbv_bufsize
= 0;
90 static float vbv_init
= 0.9;
91 static int qp_min
= 10;
92 static int qp_max
= 51;
93 static int qp_step
= 2;
95 static float qcomp
= 0.6;
96 static float qblur
= 0.5;
97 static float complexity_blur
= 20;
98 static char *rc_eq
= "blurCplx^(1-qComp)";
99 static char *zones
= NULL
;
101 static int bframe_rdo
= 0;
102 static int bidir_me
= 0;
103 static int me_method
= 2;
104 static int me_range
= 16;
105 static int trellis
= 1;
106 static int fast_pskip
= 1;
107 static int dct_decimate
= 1;
108 static int noise_reduction
= 0;
109 static int threads
= 1;
110 static int level_idc
= 51;
112 static int log_level
= 2;
113 static int turbo
= 0;
114 static int visualize
= 0;
115 static char *cqm
= NULL
;
116 static char *cqm4iy
= NULL
;
117 static char *cqm4ic
= NULL
;
118 static char *cqm4py
= NULL
;
119 static char *cqm4pc
= NULL
;
120 static char *cqm8iy
= NULL
;
121 static char *cqm8py
= NULL
;
123 m_option_t x264encopts_conf
[] = {
124 {"bitrate", &bitrate
, CONF_TYPE_INT
, CONF_RANGE
, 0, 24000000, NULL
},
125 {"qp_constant", &qp_constant
, CONF_TYPE_INT
, CONF_RANGE
, 0, 51, NULL
},
126 {"qp", &qp_constant
, CONF_TYPE_INT
, CONF_RANGE
, 0, 51, NULL
},
127 {"crf", &rf_constant
, CONF_TYPE_INT
, CONF_RANGE
, 1, 50, NULL
},
128 {"frameref", &frame_ref
, CONF_TYPE_INT
, CONF_RANGE
, 1, 16, NULL
},
129 {"keyint", &keyint_max
, CONF_TYPE_INT
, CONF_RANGE
, 1, 24000000, NULL
},
130 {"keyint_min", &keyint_min
, CONF_TYPE_INT
, CONF_RANGE
, 1, 24000000, NULL
},
131 {"scenecut", &scenecut_threshold
, CONF_TYPE_INT
, CONF_RANGE
, -1, 100, NULL
},
132 {"bframes", &bframe
, CONF_TYPE_INT
, CONF_RANGE
, 0, 16, NULL
},
133 {"b_adapt", &bframe_adaptive
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
134 {"nob_adapt", &bframe_adaptive
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
135 {"b_bias", &bframe_bias
, CONF_TYPE_INT
, CONF_RANGE
, -100, 100, NULL
},
136 {"b_pyramid", &bframe_pyramid
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
137 {"nob_pyramid", &bframe_pyramid
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
138 {"deblock", &deblock
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
139 {"nodeblock", &deblock
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
140 {"deblockalpha", &deblockalpha
, CONF_TYPE_INT
, CONF_RANGE
, -6, 6, NULL
},
141 {"deblockbeta", &deblockbeta
, CONF_TYPE_INT
, CONF_RANGE
, -6, 6, NULL
},
142 {"cabac", &cabac
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
143 {"nocabac", &cabac
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
144 {"4x4mv", &p4x4mv
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
145 {"no4x4mv", &p4x4mv
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
146 {"8x8mv", &p8x8mv
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
147 {"no8x8mv", &p8x8mv
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
148 {"b8x8mv", &b8x8mv
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
149 {"nob8x8mv", &b8x8mv
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
150 {"i4x4", &i4x4
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
151 {"noi4x4", &i4x4
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
152 {"i8x8", &i8x8
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
153 {"noi8x8", &i8x8
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
154 {"8x8dct", &dct8
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
155 {"no8x8dct", &dct8
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
156 {"direct_pred", &direct_pred
, CONF_TYPE_INT
, CONF_RANGE
, 0, 3, NULL
},
157 {"weight_b", &weight_b
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
158 {"noweight_b", &weight_b
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
159 {"bime", &bidir_me
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
160 {"nobime", &bidir_me
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
161 {"chroma_me", &chroma_me
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
162 {"nochroma_me", &chroma_me
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
163 {"mixed_refs", &mixed_references
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
164 {"nomixed_refs", &mixed_references
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
165 {"chroma_qp_offset", &chroma_qp_offset
, CONF_TYPE_INT
, CONF_RANGE
, -12, 12, NULL
},
166 {"ip_factor", &ip_factor
, CONF_TYPE_FLOAT
, CONF_RANGE
, -10.0, 10.0, NULL
},
167 {"pb_factor", &pb_factor
, CONF_TYPE_FLOAT
, CONF_RANGE
, -10.0, 10.0, NULL
},
168 {"ratetol", &ratetol
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.1, 100.0, NULL
},
169 {"vbv_maxrate", &vbv_maxrate
, CONF_TYPE_INT
, CONF_RANGE
, 0, 24000000, NULL
},
170 {"vbv_bufsize", &vbv_bufsize
, CONF_TYPE_INT
, CONF_RANGE
, 0, 24000000, NULL
},
171 {"vbv_init", &vbv_init
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 1.0, NULL
},
172 {"qp_min", &qp_min
, CONF_TYPE_INT
, CONF_RANGE
, 1, 51, NULL
},
173 {"qp_max", &qp_max
, CONF_TYPE_INT
, CONF_RANGE
, 1, 51, NULL
},
174 {"qp_step", &qp_step
, CONF_TYPE_INT
, CONF_RANGE
, 1, 50, NULL
},
175 {"pass", &pass
, CONF_TYPE_INT
, CONF_RANGE
, 1, 3, NULL
},
176 {"rc_eq", &rc_eq
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
177 {"cqm", &cqm
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
178 {"cqm4iy", &cqm4iy
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
179 {"cqm4ic", &cqm4ic
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
180 {"cqm4py", &cqm4py
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
181 {"cqm4pc", &cqm4pc
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
182 {"cqm8iy", &cqm8iy
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
183 {"cqm8py", &cqm8py
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
184 {"qcomp", &qcomp
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0, 1, NULL
},
185 {"qblur", &qblur
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0, 99, NULL
},
186 {"cplx_blur", &complexity_blur
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0, 999, NULL
},
187 {"zones", &zones
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
188 {"subq", &subq
, CONF_TYPE_INT
, CONF_RANGE
, 1, 7, NULL
},
189 {"brdo", &bframe_rdo
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
190 {"nobrdo", &bframe_rdo
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
191 {"me", &me_method
, CONF_TYPE_INT
, CONF_RANGE
, 1, 4, NULL
},
192 {"me_range", &me_range
, CONF_TYPE_INT
, CONF_RANGE
, 4, 64, NULL
},
193 {"trellis", &trellis
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2, NULL
},
194 {"fast_pskip", &fast_pskip
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
195 {"nofast_pskip", &fast_pskip
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
196 {"dct_decimate", &dct_decimate
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
197 {"nodct_decimate", &dct_decimate
, CONF_TYPE_FLAG
, 0, 0, 0, NULL
},
198 {"nr", &noise_reduction
, CONF_TYPE_INT
, CONF_RANGE
, 0, 100000, NULL
},
199 {"level_idc", &level_idc
, CONF_TYPE_INT
, CONF_RANGE
, 10, 51, NULL
},
200 {"threads", &threads
, CONF_TYPE_INT
, CONF_RANGE
, 1, 4, NULL
},
201 {"psnr", &psnr
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
202 {"nopsnr", &psnr
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
203 {"log", &log_level
, CONF_TYPE_INT
, CONF_RANGE
, -1, 3, NULL
},
204 {"turbo", &turbo
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2, NULL
},
205 {"visualize", &visualize
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
206 {"novisualize", &visualize
, CONF_TYPE_FLAG
, 0, 1, 0, NULL
},
207 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
210 static int parse_cqm(const char *str
, uint8_t *cqm
, int length
,
211 h264_module_t
*mod
, char *matrix_name
) {
214 for (i
= 0; i
< length
; i
++) {
215 long coef
= strtol(str
, &str
, 0);
216 if (coef
< 1 || coef
> 255 || str
[0] != ((i
+ 1 == length
)?0:',')) {
217 mp_msg( MSGT_MENCODER
, MSGL_ERR
, "x264: Invalid entry in cqm%s at position %d.\n", matrix_name
, i
+1 );
223 mod
->param
.i_cqm_preset
= X264_CQM_CUSTOM
;
227 static int put_image(struct vf_instance_s
*vf
, mp_image_t
*mpi
, double pts
);
228 static int encode_frame(struct vf_instance_s
*vf
, x264_picture_t
*pic_in
);
230 static int config(struct vf_instance_s
* vf
, int width
, int height
, int d_width
, int d_height
, unsigned int flags
, unsigned int outfmt
) {
231 h264_module_t
*mod
=(h264_module_t
*)vf
->priv
;
232 mod
->mux
->bih
->biWidth
= width
;
233 mod
->mux
->bih
->biHeight
= height
;
234 mod
->mux
->aspect
= (float)d_width
/d_height
;
236 x264_param_default(&mod
->param
);
237 mod
->param
.i_frame_reference
= frame_ref
;
238 mod
->param
.i_keyint_max
= keyint_max
;
239 mod
->param
.i_keyint_min
= keyint_min
;
240 mod
->param
.i_scenecut_threshold
= scenecut_threshold
;
241 mod
->param
.i_bframe
= bframe
;
242 mod
->param
.b_bframe_adaptive
= bframe_adaptive
;
243 mod
->param
.i_bframe_bias
= bframe_bias
;
244 mod
->param
.b_bframe_pyramid
= bframe_pyramid
;
245 mod
->param
.b_deblocking_filter
= deblock
;
246 mod
->param
.i_deblocking_filter_alphac0
= deblockalpha
;
247 mod
->param
.i_deblocking_filter_beta
= deblockbeta
;
248 mod
->param
.b_cabac
= cabac
;
250 mod
->param
.rc
.i_qp_constant
= qp_constant
;
251 mod
->param
.rc
.i_rf_constant
= rf_constant
;
252 if(qp_min
> qp_constant
)
253 qp_min
= qp_constant
;
254 if(qp_max
< qp_constant
)
255 qp_max
= qp_constant
;
256 mod
->param
.rc
.i_qp_min
= qp_min
;
257 mod
->param
.rc
.i_qp_max
= qp_max
;
258 mod
->param
.rc
.i_qp_step
= qp_step
;
259 mod
->param
.rc
.psz_rc_eq
= rc_eq
;
260 mod
->param
.rc
.f_qcompress
= qcomp
;
261 mod
->param
.rc
.f_qblur
= qblur
;
262 mod
->param
.rc
.f_complexity_blur
= complexity_blur
;
263 mod
->param
.analyse
.i_subpel_refine
= subq
;
264 mod
->param
.rc
.psz_stat_out
= passtmpfile
;
265 mod
->param
.rc
.psz_stat_in
= passtmpfile
;
266 if((pass
& 2) && bitrate
<= 0)
268 mp_msg(MSGT_MENCODER
, MSGL_ERR
,
269 "2 pass encoding enabled, but no bitrate specified.\n");
273 if((vbv_maxrate
> 0) != (vbv_bufsize
> 0)) {
274 mp_msg(MSGT_MENCODER
, MSGL_ERR
,
275 "VBV requires both vbv_maxrate and vbv_bufsize.\n");
278 mod
->param
.rc
.b_cbr
= 1;
279 mod
->param
.rc
.i_bitrate
= bitrate
;
280 mod
->param
.rc
.f_rate_tolerance
= ratetol
;
281 mod
->param
.rc
.i_vbv_max_bitrate
= vbv_maxrate
;
282 mod
->param
.rc
.i_vbv_buffer_size
= vbv_bufsize
;
283 mod
->param
.rc
.f_vbv_buffer_init
= vbv_init
;
285 mod
->param
.rc
.f_ip_factor
= ip_factor
;
286 mod
->param
.rc
.f_pb_factor
= pb_factor
;
287 mod
->param
.rc
.psz_zones
= zones
;
289 case 1: mod
->param
.analyse
.i_me_method
= X264_ME_DIA
; break;
290 case 2: mod
->param
.analyse
.i_me_method
= X264_ME_HEX
; break;
291 case 3: mod
->param
.analyse
.i_me_method
= X264_ME_UMH
; break;
292 case 4: mod
->param
.analyse
.i_me_method
= X264_ME_ESA
; break;
294 mod
->param
.analyse
.inter
= 0;
295 if(p4x4mv
) mod
->param
.analyse
.inter
|= X264_ANALYSE_PSUB8x8
;
296 if(p8x8mv
) mod
->param
.analyse
.inter
|= X264_ANALYSE_PSUB16x16
;
297 if(b8x8mv
) mod
->param
.analyse
.inter
|= X264_ANALYSE_BSUB16x16
;
298 if(i4x4
) mod
->param
.analyse
.inter
|= X264_ANALYSE_I4x4
;
299 if(i8x8
) mod
->param
.analyse
.inter
|= X264_ANALYSE_I8x8
;
300 mod
->param
.analyse
.b_transform_8x8
= dct8
;
301 mod
->param
.analyse
.i_direct_mv_pred
= direct_pred
;
302 mod
->param
.analyse
.b_weighted_bipred
= weight_b
;
303 mod
->param
.analyse
.i_chroma_qp_offset
= chroma_qp_offset
;
304 mod
->param
.analyse
.b_bidir_me
= bidir_me
;
305 mod
->param
.analyse
.b_chroma_me
= chroma_me
;
306 mod
->param
.analyse
.b_mixed_references
= mixed_references
;
307 mod
->param
.analyse
.i_trellis
= trellis
;
308 mod
->param
.analyse
.b_fast_pskip
= fast_pskip
;
309 mod
->param
.analyse
.b_dct_decimate
= dct_decimate
;
310 mod
->param
.analyse
.i_noise_reduction
= noise_reduction
;
311 mod
->param
.analyse
.b_bframe_rdo
= bframe_rdo
;
313 mod
->param
.i_width
= width
;
314 mod
->param
.i_height
= height
;
315 mod
->param
.i_fps_num
= mod
->mux
->h
.dwRate
;
316 mod
->param
.i_fps_den
= mod
->mux
->h
.dwScale
;
317 mod
->param
.i_level_idc
= level_idc
;
318 mod
->param
.analyse
.b_psnr
= psnr
;
319 mod
->param
.i_log_level
= log_level
;
320 mod
->param
.b_visualize
= visualize
;
321 mod
->param
.vui
.i_sar_width
= d_width
*height
;
322 mod
->param
.vui
.i_sar_height
= d_height
*width
;
323 mod
->param
.i_threads
= threads
;
327 if( !strcmp(cqm
, "flat") )
328 mod
->param
.i_cqm_preset
= X264_CQM_FLAT
;
329 else if( !strcmp(cqm
, "jvt") )
330 mod
->param
.i_cqm_preset
= X264_CQM_JVT
;
334 cqm_test
= fopen( cqm
, "rb" );
337 mod
->param
.i_cqm_preset
= X264_CQM_CUSTOM
;
338 mod
->param
.psz_cqm_file
= cqm
;
343 mp_msg( MSGT_MENCODER
, MSGL_ERR
, "x264: CQM file failed to open.\n" );
349 if( (parse_cqm(cqm4iy
, mod
->param
.cqm_4iy
, 16, mod
, "4iy") < 0) ||
350 (parse_cqm(cqm4ic
, mod
->param
.cqm_4ic
, 16, mod
, "4ic") < 0) ||
351 (parse_cqm(cqm4py
, mod
->param
.cqm_4py
, 16, mod
, "4py") < 0) ||
352 (parse_cqm(cqm4pc
, mod
->param
.cqm_4pc
, 16, mod
, "4pc") < 0) ||
353 (parse_cqm(cqm8iy
, mod
->param
.cqm_8iy
, 64, mod
, "8iy") < 0) ||
354 (parse_cqm(cqm8py
, mod
->param
.cqm_8py
, 64, mod
, "8py") < 0) )
359 mod
->param
.rc
.b_stat_write
= 0;
360 mod
->param
.rc
.b_stat_read
= 0;
363 /* Adjust or disable some flags to gain speed in the first pass */
366 mod
->param
.i_frame_reference
= ( frame_ref
+ 1 ) >> 1;
367 mod
->param
.analyse
.i_subpel_refine
= max( min( 3, subq
- 1 ), 1 );
368 mod
->param
.analyse
.inter
&= ( ~X264_ANALYSE_PSUB8x8
);
369 mod
->param
.analyse
.inter
&= ( ~X264_ANALYSE_BSUB16x16
);
370 mod
->param
.analyse
.i_trellis
= 0;
374 mod
->param
.i_frame_reference
= 1;
375 mod
->param
.analyse
.i_subpel_refine
= 1;
376 mod
->param
.analyse
.i_me_method
= X264_ME_DIA
;
377 mod
->param
.analyse
.inter
= 0;
378 mod
->param
.analyse
.b_transform_8x8
= 0;
379 mod
->param
.analyse
.b_weighted_bipred
= 0;
380 mod
->param
.analyse
.i_trellis
= 0;
382 mod
->param
.rc
.b_stat_write
= 1;
383 mod
->param
.rc
.b_stat_read
= 0;
386 mod
->param
.rc
.b_stat_write
= 0;
387 mod
->param
.rc
.b_stat_read
= 1;
390 mod
->param
.rc
.b_stat_write
= 1;
391 mod
->param
.rc
.b_stat_read
= 1;
396 mod
->param
.analyse
.i_me_range
= me_range
;
400 mod
->param
.i_csp
= X264_CSP_I420
;
401 mod
->mux
->bih
->biSizeImage
= width
* height
* 3;
404 mod
->param
.i_csp
= X264_CSP_YV12
;
405 mod
->mux
->bih
->biSizeImage
= width
* height
* 3;
408 mod
->param
.i_csp
= X264_CSP_I422
;
409 mod
->mux
->bih
->biSizeImage
= width
* height
* 3;
412 mod
->param
.i_csp
= X264_CSP_I444
;
413 mod
->mux
->bih
->biSizeImage
= width
* height
* 3;
416 mod
->param
.i_csp
= X264_CSP_YUYV
;
417 mod
->mux
->bih
->biSizeImage
= width
* height
* 3;
420 mod
->param
.i_csp
= X264_CSP_RGB
;
421 mod
->mux
->bih
->biSizeImage
= width
* height
* 3;
424 mod
->param
.i_csp
= X264_CSP_BGR
;
425 mod
->mux
->bih
->biSizeImage
= width
* height
* 3;
428 mod
->param
.i_csp
= X264_CSP_BGRA
;
429 mod
->mux
->bih
->biSizeImage
= width
* height
* 4;
432 mp_msg(MSGT_MENCODER
, MSGL_ERR
, "Wrong colorspace.\n");
436 mod
->x264
= x264_encoder_open(&mod
->param
);
438 mp_msg(MSGT_MENCODER
, MSGL_ERR
, "x264_encoder_open failed.\n");
442 if (mod
->param
.i_bframe
> 1 && mod
->param
.b_bframe_pyramid
)
443 mod
->mux
->decoder_delay
= 2;
445 mod
->mux
->decoder_delay
= mod
->param
.i_bframe
? 1 : 0;
450 static int control(struct vf_instance_s
* vf
, int request
, void *data
)
453 case VFCTRL_FLUSH_FRAMES
:
455 while(encode_frame(vf
, NULL
) > 0);
458 return CONTROL_UNKNOWN
;
462 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
)
466 return (VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
);
474 /* These colorspaces are supported, but they'll just have
475 * to be converted to I420 internally */
476 return 0; /* VFCAP_CSP_SUPPORTED */
481 static int put_image(struct vf_instance_s
*vf
, mp_image_t
*mpi
, double pts
)
483 h264_module_t
*mod
=(h264_module_t
*)vf
->priv
;
486 memset(&mod
->pic
, 0, sizeof(x264_picture_t
));
487 mod
->pic
.img
.i_csp
=mod
->param
.i_csp
;
488 mod
->pic
.img
.i_plane
=3;
490 mod
->pic
.img
.plane
[i
] = mpi
->planes
[i
];
491 mod
->pic
.img
.i_stride
[i
] = mpi
->stride
[i
];
494 mod
->pic
.i_type
= X264_TYPE_AUTO
;
496 return encode_frame(vf
, &mod
->pic
) >= 0;
499 static int encode_frame(struct vf_instance_s
*vf
, x264_picture_t
*pic_in
)
501 h264_module_t
*mod
=(h264_module_t
*)vf
->priv
;
502 x264_picture_t pic_out
;
508 if(x264_encoder_encode(mod
->x264
, &nal
, &i_nal
, pic_in
, &pic_out
) < 0) {
509 mp_msg(MSGT_MENCODER
, MSGL_ERR
, "x264_encoder_encode failed\n");
513 for(i
=0; i
< i_nal
; i
++) {
514 int i_data
= mod
->mux
->buffer_size
- i_size
;
515 i_size
+= x264_nal_encode(mod
->mux
->buffer
+ i_size
, &i_data
, 1, &nal
[i
]);
518 int keyframe
= (pic_out
.i_type
== X264_TYPE_IDR
) ||
519 (pic_out
.i_type
== X264_TYPE_I
520 && frame_ref
== 1 && !bframe
);
521 muxer_write_chunk(mod
->mux
, i_size
, keyframe
?0x10:0, MP_NOPTS_VALUE
, MP_NOPTS_VALUE
);
524 ++mod
->mux
->encoder_delay
;
529 static void uninit(struct vf_instance_s
*vf
)
531 h264_module_t
*mod
=(h264_module_t
*)vf
->priv
;
532 x264_encoder_close(mod
->x264
);
535 static int vf_open(vf_instance_t
*vf
, char *args
) {
539 vf
->default_caps
= VFCAP_CONSTANT
;
540 vf
->control
= control
;
541 vf
->query_format
= query_format
;
542 vf
->put_image
= put_image
;
544 vf
->priv
= malloc(sizeof(h264_module_t
));
546 mod
=(h264_module_t
*)vf
->priv
;
547 mod
->mux
= (muxer_stream_t
*)args
;
548 mod
->mux
->bih
= malloc(sizeof(BITMAPINFOHEADER
));
549 memset(mod
->mux
->bih
, 0, sizeof(BITMAPINFOHEADER
));
550 mod
->mux
->bih
->biSize
= sizeof(BITMAPINFOHEADER
);
551 mod
->mux
->bih
->biPlanes
= 1;
552 mod
->mux
->bih
->biBitCount
= 24;
553 mod
->mux
->bih
->biCompression
= mmioFOURCC('h', '2', '6', '4');
558 vf_info_t ve_info_x264
= {
561 "Bernhard Rosenkraenzer <bero@arklinux.org>",
562 "(C) 2004 LINUX4MEDIA GmbH; (C) 2004 Ark Linux",