sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / libmpcodecs / ve_x264.c
bloba3e51a08f1374d7469da35b0eefa47264e11895a
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 file is part of MPlayer.
12 * MPlayer is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * MPlayer is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY 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 along
23 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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/stream.h"
39 #include "libmpdemux/demuxer.h"
40 #include "libmpdemux/stheader.h"
42 #include "stream/stream.h"
43 #include "libmpdemux/muxer.h"
45 #include "img_format.h"
46 #include "mp_image.h"
47 #include "vf.h"
49 #include <x264.h>
51 typedef struct h264_module_t {
52 muxer_stream_t *mux;
53 x264_t * x264;
54 x264_picture_t pic;
55 } h264_module_t;
57 extern char* passtmpfile;
58 static int turbo = 0;
59 static x264_param_t param;
60 static int parse_error = 0;
62 static int put_image(struct vf_instance_s *vf, mp_image_t *mpi, double pts);
63 static int encode_frame(struct vf_instance_s *vf, x264_picture_t *pic_in);
65 void x264enc_set_param(const m_option_t* opt, char* arg)
67 static int initted = 0;
68 if(!initted) {
69 x264_param_default(&param);
70 x264_param_parse(&param, "psnr", "no");
71 x264_param_parse(&param, "ssim", "no");
72 initted = 1;
75 if(!arg) {
76 parse_error = 1;
77 return;
80 while(*arg) {
81 char *name = arg;
82 char *value;
83 int ret;
85 arg += strcspn(arg, ":");
86 if(*arg) {
87 *arg = 0;
88 arg++;
91 value = strchr( name, '=' );
92 if(value) {
93 *value = 0;
94 value++;
97 if(!strcmp(name, "turbo")) {
98 turbo = value ? atoi(value) : 1;
99 continue;
102 ret = x264_param_parse(&param, name, value);
103 if(ret == X264_PARAM_BAD_NAME)
104 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option x264encopts: Unknown suboption %s\n", name);
105 if(ret == X264_PARAM_BAD_VALUE)
106 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option x264encopts: Bad argument %s=%s\n", name, value ? value : "(null)");
108 /* mark this option as done, so it's not reparsed if there's another -x264encopts */
109 *name = 0;
111 parse_error |= ret;
114 if(param.rc.b_stat_write && !param.rc.b_stat_read) {
115 /* Adjust or disable some flags to gain speed in the first pass */
116 if(turbo == 1)
118 param.i_frame_reference = ( param.i_frame_reference + 1 ) >> 1;
119 param.analyse.i_subpel_refine = FFMAX( FFMIN( 3, param.analyse.i_subpel_refine - 1 ), 1 );
120 param.analyse.inter &= ( ~X264_ANALYSE_PSUB8x8 );
121 param.analyse.inter &= ( ~X264_ANALYSE_BSUB16x16 );
122 param.analyse.i_trellis = 0;
124 else if(turbo >= 2)
126 param.i_frame_reference = 1;
127 param.analyse.i_subpel_refine = 1;
128 param.analyse.i_me_method = X264_ME_DIA;
129 param.analyse.inter = 0;
130 param.analyse.b_transform_8x8 = 0;
131 param.analyse.b_weighted_bipred = 0;
132 param.analyse.i_trellis = 0;
137 static int config(struct vf_instance_s* vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt) {
138 h264_module_t *mod=(h264_module_t*)vf->priv;
140 if(parse_error)
141 return 0;
143 mod->mux->bih->biWidth = width;
144 mod->mux->bih->biHeight = height;
145 mod->mux->bih->biSizeImage = width * height * 3;
146 mod->mux->aspect = (float)d_width/d_height;
148 // make sure param is initialized
149 x264enc_set_param(NULL, "");
150 param.i_width = width;
151 param.i_height = height;
152 param.i_fps_num = mod->mux->h.dwRate;
153 param.i_fps_den = mod->mux->h.dwScale;
154 param.vui.i_sar_width = d_width*height;
155 param.vui.i_sar_height = d_height*width;
157 x264_param_parse(&param, "stats", passtmpfile);
159 switch(outfmt) {
160 case IMGFMT_I420:
161 param.i_csp = X264_CSP_I420;
162 break;
163 case IMGFMT_YV12:
164 param.i_csp = X264_CSP_YV12;
165 break;
166 default:
167 mp_msg(MSGT_MENCODER, MSGL_ERR, "Wrong colorspace.\n");
168 return 0;
171 mod->x264 = x264_encoder_open(&param);
172 if(!mod->x264) {
173 mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_open failed.\n");
174 return 0;
177 if(!param.b_repeat_headers){
178 x264_nal_t *nal;
179 int extradata_size, nnal;
181 extradata_size = x264_encoder_headers(mod->x264, &nal, &nnal);
183 mod->mux->bih= realloc(mod->mux->bih, sizeof(BITMAPINFOHEADER) + extradata_size);
184 memcpy(mod->mux->bih + 1, nal->p_payload, extradata_size);
185 mod->mux->bih->biSize= sizeof(BITMAPINFOHEADER) + extradata_size;
188 if (param.i_bframe > 1 && param.i_bframe_pyramid)
189 mod->mux->decoder_delay = 2;
190 else
191 mod->mux->decoder_delay = param.i_bframe ? 1 : 0;
193 return 1;
196 static int control(struct vf_instance_s* vf, int request, void *data)
198 h264_module_t *mod=(h264_module_t*)vf->priv;
199 switch(request){
200 case VFCTRL_FLUSH_FRAMES:
201 while (x264_encoder_delayed_frames(mod->x264) > 0)
202 encode_frame(vf, NULL);
203 return CONTROL_TRUE;
204 default:
205 return CONTROL_UNKNOWN;
209 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
211 switch(fmt) {
212 case IMGFMT_I420:
213 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
214 case IMGFMT_YV12:
215 case IMGFMT_422P:
216 case IMGFMT_444P:
217 case IMGFMT_YVYU:
218 case IMGFMT_RGB:
219 case IMGFMT_BGR:
220 case IMGFMT_BGR32:
221 /* These colorspaces are supported, but they'll just have
222 * to be converted to I420 internally */
223 return 0; /* VFCAP_CSP_SUPPORTED */
225 return 0;
228 static int put_image(struct vf_instance_s *vf, mp_image_t *mpi, double pts)
230 h264_module_t *mod=(h264_module_t*)vf->priv;
231 int i;
233 memset(&mod->pic, 0, sizeof(x264_picture_t));
234 mod->pic.img.i_csp=param.i_csp;
235 mod->pic.img.i_plane=3;
236 for(i=0; i<4; i++) {
237 mod->pic.img.plane[i] = mpi->planes[i];
238 mod->pic.img.i_stride[i] = mpi->stride[i];
241 mod->pic.i_type = X264_TYPE_AUTO;
243 return encode_frame(vf, &mod->pic) >= 0;
246 static int encode_frame(struct vf_instance_s *vf, x264_picture_t *pic_in)
248 h264_module_t *mod=(h264_module_t*)vf->priv;
249 x264_picture_t pic_out;
250 x264_nal_t *nal;
251 int i_nal;
252 int i_size;
254 i_size = x264_encoder_encode(mod->x264, &nal, &i_nal, pic_in, &pic_out);
256 if(i_size<0) {
257 mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_encode failed\n");
258 return -1;
260 if(i_size>0) {
261 int keyframe = (pic_out.i_type == X264_TYPE_IDR) ||
262 (pic_out.i_type == X264_TYPE_I
263 && param.i_frame_reference == 1
264 && !param.i_bframe);
265 memcpy(mod->mux->buffer, nal->p_payload, i_size);
266 muxer_write_chunk(mod->mux, i_size, keyframe?0x10:0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
268 else
269 ++mod->mux->encoder_delay;
271 return i_size;
274 static void uninit(struct vf_instance_s *vf)
276 h264_module_t *mod=(h264_module_t*)vf->priv;
277 if (mod->x264)
278 x264_encoder_close(mod->x264);
281 static int vf_open(vf_instance_t *vf, char *args) {
282 h264_module_t *mod;
284 vf->config = config;
285 vf->default_caps = VFCAP_CONSTANT;
286 vf->control = control;
287 vf->query_format = query_format;
288 vf->put_image = put_image;
289 vf->uninit = uninit;
290 vf->priv = malloc(sizeof(h264_module_t));
292 mod=(h264_module_t*)vf->priv;
293 mod->mux = (muxer_stream_t*)args;
294 mod->mux->bih = malloc(sizeof(BITMAPINFOHEADER));
295 memset(mod->mux->bih, 0, sizeof(BITMAPINFOHEADER));
296 mod->mux->bih->biSize = sizeof(BITMAPINFOHEADER);
297 mod->mux->bih->biPlanes = 1;
298 mod->mux->bih->biBitCount = 24;
299 mod->mux->bih->biCompression = mmioFOURCC('h', '2', '6', '4');
301 return 1;
304 vf_info_t ve_info_x264 = {
305 "H.264 encoder",
306 "x264",
307 "Bernhard Rosenkraenzer <bero@arklinux.org>",
308 "(C) 2004 LINUX4MEDIA GmbH; (C) 2004 Ark Linux",
309 vf_open