typo fixes
[mplayer/greg.git] / libmpcodecs / ve_x264.c
blob16e3c9862798e91e35fb9940a15744f5a4fbb6b2
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
15 * Foundation.
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 *****************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
33 #include "config.h"
34 #include "mp_msg.h"
36 #include "m_option.h"
37 #include "codec-cfg.h"
38 #include "stream.h"
39 #include "demuxer.h"
40 #include "stheader.h"
42 #include "muxer.h"
44 #include "img_format.h"
45 #include "mp_image.h"
46 #include "vf.h"
48 #include <x264.h>
50 typedef struct _h264_module_t {
51 muxer_stream_t *mux;
52 x264_param_t param;
53 x264_t * x264;
54 x264_picture_t pic;
55 } 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;
73 static int cabac = 1;
74 static int p4x4mv = 0;
75 static int p8x8mv = 1;
76 static int b8x8mv = 1;
77 static int i8x8 = 1;
78 static int i4x4 = 1;
79 static int dct8 = 0;
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;
94 static int pass = 0;
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;
100 static int subq = 5;
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;
111 static int psnr = 0;
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) {
212 int i;
213 if (!str) return 0;
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 );
218 return -1;
220 cqm[i] = coef;
221 str = &str[1];
223 mod->param.i_cqm_preset = X264_CQM_CUSTOM;
224 return 0;
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");
270 return 0;
272 if(bitrate > 0) {
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");
276 return 0;
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;
288 switch(me_method) {
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;
325 if(cqm != NULL)
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;
331 else
333 FILE *cqm_test;
334 cqm_test = fopen( cqm, "rb" );
335 if( cqm_test )
337 mod->param.i_cqm_preset = X264_CQM_CUSTOM;
338 mod->param.psz_cqm_file = cqm;
339 fclose( cqm_test );
341 else
343 mp_msg( MSGT_MENCODER, MSGL_ERR, "x264: CQM file failed to open.\n" );
344 return 0;
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) )
355 return 0;
357 switch(pass) {
358 case 0:
359 mod->param.rc.b_stat_write = 0;
360 mod->param.rc.b_stat_read = 0;
361 break;
362 case 1:
363 /* Adjust or disable some flags to gain speed in the first pass */
364 if(turbo == 1)
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;
372 else if(turbo == 2)
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;
384 break;
385 case 2:
386 mod->param.rc.b_stat_write = 0;
387 mod->param.rc.b_stat_read = 1;
388 break;
389 case 3:
390 mod->param.rc.b_stat_write = 1;
391 mod->param.rc.b_stat_read = 1;
392 break;
395 if(me_method >= 3)
396 mod->param.analyse.i_me_range = me_range;
398 switch(outfmt) {
399 case IMGFMT_I420:
400 mod->param.i_csp = X264_CSP_I420;
401 mod->mux->bih->biSizeImage = width * height * 3;
402 break;
403 case IMGFMT_YV12:
404 mod->param.i_csp = X264_CSP_YV12;
405 mod->mux->bih->biSizeImage = width * height * 3;
406 break;
407 case IMGFMT_422P:
408 mod->param.i_csp = X264_CSP_I422;
409 mod->mux->bih->biSizeImage = width * height * 3;
410 break;
411 case IMGFMT_444P:
412 mod->param.i_csp = X264_CSP_I444;
413 mod->mux->bih->biSizeImage = width * height * 3;
414 break;
415 case IMGFMT_YVYU:
416 mod->param.i_csp = X264_CSP_YUYV;
417 mod->mux->bih->biSizeImage = width * height * 3;
418 break;
419 case IMGFMT_RGB:
420 mod->param.i_csp = X264_CSP_RGB;
421 mod->mux->bih->biSizeImage = width * height * 3;
422 break;
423 case IMGFMT_BGR:
424 mod->param.i_csp = X264_CSP_BGR;
425 mod->mux->bih->biSizeImage = width * height * 3;
426 break;
427 case IMGFMT_BGR32:
428 mod->param.i_csp = X264_CSP_BGRA;
429 mod->mux->bih->biSizeImage = width * height * 4;
430 break;
431 default:
432 mp_msg(MSGT_MENCODER, MSGL_ERR, "Wrong colorspace.\n");
433 return 0;
436 mod->x264 = x264_encoder_open(&mod->param);
437 if(!mod->x264) {
438 mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_open failed.\n");
439 return 0;
442 if (mod->param.i_bframe > 1 && mod->param.b_bframe_pyramid)
443 mod->mux->decoder_delay = 2;
444 else
445 mod->mux->decoder_delay = mod->param.i_bframe ? 1 : 0;
447 return 1;
450 static int control(struct vf_instance_s* vf, int request, void *data)
452 switch(request){
453 case VFCTRL_FLUSH_FRAMES:
454 if(bframe)
455 while(encode_frame(vf, NULL) > 0);
456 return CONTROL_TRUE;
457 default:
458 return CONTROL_UNKNOWN;
462 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
464 switch(fmt) {
465 case IMGFMT_I420:
466 return (VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW);
467 case IMGFMT_YV12:
468 case IMGFMT_422P:
469 case IMGFMT_444P:
470 case IMGFMT_YVYU:
471 case IMGFMT_RGB:
472 case IMGFMT_BGR:
473 case IMGFMT_BGR32:
474 /* These colorspaces are supported, but they'll just have
475 * to be converted to I420 internally */
476 return 0; /* VFCAP_CSP_SUPPORTED */
478 return 0;
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;
484 int i;
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;
489 for(i=0; i<4; i++) {
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;
503 x264_nal_t *nal;
504 int i_nal;
505 int i_size = 0;
506 int i;
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");
510 return -1;
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]);
517 if(i_size>0) {
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);
523 else
524 ++mod->mux->encoder_delay;
526 return i_size;
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) {
536 h264_module_t *mod;
538 vf->config = config;
539 vf->default_caps = VFCAP_CONSTANT;
540 vf->control = control;
541 vf->query_format = query_format;
542 vf->put_image = put_image;
543 vf->uninit = uninit;
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');
555 return 1;
558 vf_info_t ve_info_x264 = {
559 "H.264 encoder",
560 "x264",
561 "Bernhard Rosenkraenzer <bero@arklinux.org>",
562 "(C) 2004 LINUX4MEDIA GmbH; (C) 2004 Ark Linux",
563 vf_open