1 /*****************************************************************************
2  *
3  * - H.264 encoder for mencoder using x264 -
4  *
5  * Copyright (C) 2004 LINUX4MEDIA GmbH
6  * Copyright (C) 2004 Ark Linux
7  *
8  * Written by Bernhard Rosenkraenzer <bero@arklinux.org>
9  *
10  * This file is part of MPlayer.
11  *
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.
16  *
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.
21  *
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.
25  *
26  *****************************************************************************/
27 
28 #define _BSD_SOURCE
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 
35 #include "config.h"
36 #include "libavutil/avstring.h"
37 #include "mp_msg.h"
38 #include "mencoder.h"
39 #include "m_option.h"
40 #include "codec-cfg.h"
41 #include "stream/stream.h"
42 #include "libmpdemux/demuxer.h"
43 #include "libmpdemux/stheader.h"
44 #include "osdep/strsep.h"
45 #include "stream/stream.h"
46 #include "libmpdemux/muxer.h"
47 
48 #include "img_format.h"
49 #include "mp_image.h"
50 #include "vf.h"
51 #include "ve.h"
52 #include "ve_x264.h"
53 
54 #include <x264.h>
55 
56 typedef struct h264_module_t {
57     muxer_stream_t *mux;
58     x264_t *    x264;
59     x264_picture_t  pic;
60 } h264_module_t;
61 
62 static x264_param_t param;
63 static int parse_error = 0;
64 
65 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts);
66 static int encode_frame(struct vf_instance *vf, x264_picture_t *pic_in);
67 
x264enc_set_param(const m_option_t * opt,char * arg)68 void x264enc_set_param(const m_option_t* opt, char* arg)
69 {
70     static int initialized = 0;
71     int slow_firstpass = 0;
72     char *preset = NULL, *tune = NULL, *profile = NULL;
73     char *p, *copy, *name;
74 
75     if (!initialized) {
76         x264_param_default(&param);
77         initialized = 1;
78     }
79     if (!arg) {
80         mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option x264encopts: no options provided\n");
81         parse_error = 1;
82         return;
83     } else if (!*arg)
84         /* Empty arguments, just doing initialization of default parameters. */
85         return;
86 
87     /* Step 1: look for initial preset/tune. */
88     copy = p = strdup(arg);
89     while ((name = strsep(&copy, ":"))) {
90         char *value = strpbrk(name, "=:");
91         if (!value)
92             continue;
93         *value++ = 0;
94         if (!av_strcasecmp(name, "preset"))
95             preset = value;
96         else if (!av_strcasecmp(name, "tune"))
97             tune = value;
98     }
99     if (x264_param_default_preset(&param, preset, tune) < 0) {
100         mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option x264encopts: Invalid preset or tune.\n");
101         parse_error = 1;
102     }
103     free(p);
104 
105     /* Step 2: explicit user overrides */
106     while ((name = strsep(&arg, ":")) && *name) {
107         int ret = 0;
108         char *value = strpbrk(name, "=:");
109 
110         if (value)
111             *value++ = 0;
112         if (!av_strcasecmp(name, "profile"))
113             profile = value;
114         else if (!av_strcasecmp(name, "turbo")) {
115             mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Option x264encopts: turbo option is deprecated; "
116                                               "use slow_firstpass to disable turbo\n");
117             if (value && *value == '0')
118                 slow_firstpass = 1;
119         } else if (!av_strcasecmp(name, "slow_firstpass"))
120             slow_firstpass = 1;
121         else if (av_strcasecmp(name, "preset") && av_strcasecmp(name, "tune")) {
122             ret = x264_param_parse(&param, name, value);
123             if (ret == X264_PARAM_BAD_NAME)
124                 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option x264encopts: Unknown suboption %s\n", name);
125             if (ret == X264_PARAM_BAD_VALUE)
126                 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option x264encopts: Bad argument %s=%s\n",
127                        name, value ? value : "(null)");
128 
129         }
130         /* mark this option as done, so it's not reparsed if there's another -x264encopts */
131         *name = 0;
132 
133         parse_error |= ret;
134     }
135 
136     /* Step 3: Apply fast first pass (turbo) options. */
137     if (!slow_firstpass)
138         x264_param_apply_fastfirstpass(&param);
139 
140     /* Step 4: enforce profile */
141     if (profile && x264_param_apply_profile(&param, profile) < 0)
142         parse_error = 1;
143 }
144 
config(struct vf_instance * vf,int width,int height,int d_width,int d_height,unsigned int flags,unsigned int outfmt)145 static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt) {
146     h264_module_t *mod=(h264_module_t*)vf->priv;
147 
148     if(parse_error)
149         return 0;
150 
151     mod->mux->bih->biWidth = width;
152     mod->mux->bih->biHeight = height;
153     mod->mux->bih->biSizeImage = width * height * 3;
154     mod->mux->aspect = (float)d_width/d_height;
155 
156     // make sure param is initialized
157     x264enc_set_param(NULL, "");
158     param.i_width = width;
159     param.i_height = height;
160     param.i_fps_num = mod->mux->h.dwRate;
161     param.i_fps_den = mod->mux->h.dwScale;
162     param.b_vfr_input = 0;
163     param.vui.i_sar_width = d_width*height;
164     param.vui.i_sar_height = d_height*width;
165 
166     x264_param_parse(&param, "stats", passtmpfile);
167 
168     switch(outfmt) {
169     case IMGFMT_I420:
170         param.i_csp = X264_CSP_I420;
171         break;
172     case IMGFMT_YV12:
173         param.i_csp = X264_CSP_YV12;
174         break;
175     default:
176         mp_msg(MSGT_MENCODER, MSGL_ERR, "Wrong colorspace.\n");
177         return 0;
178     }
179 
180     mod->x264 = x264_encoder_open(&param);
181     if(!mod->x264) {
182         mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_open failed.\n");
183         return 0;
184     }
185 
186     if(!param.b_repeat_headers){
187         x264_nal_t *nal;
188         int extradata_size, nnal;
189 
190         extradata_size = x264_encoder_headers(mod->x264, &nal, &nnal);
191 
192         mod->mux->bih= realloc(mod->mux->bih, sizeof(*mod->mux->bih) + extradata_size);
193         memcpy(mod->mux->bih + 1, nal->p_payload, extradata_size);
194         mod->mux->bih->biSize= sizeof(*mod->mux->bih) + extradata_size;
195     }
196 
197     if (param.i_bframe > 1 && param.i_bframe_pyramid)
198         mod->mux->decoder_delay = 2;
199     else
200         mod->mux->decoder_delay = param.i_bframe ? 1 : 0;
201 
202     return 1;
203 }
204 
control(struct vf_instance * vf,int request,void * data)205 static int control(struct vf_instance *vf, int request, void *data)
206 {
207     h264_module_t *mod=(h264_module_t*)vf->priv;
208     switch(request){
209         case VFCTRL_FLUSH_FRAMES:
210             while (x264_encoder_delayed_frames(mod->x264) > 0)
211                 encode_frame(vf, NULL);
212             return CONTROL_TRUE;
213         default:
214             return CONTROL_UNKNOWN;
215     }
216 }
217 
query_format(struct vf_instance * vf,unsigned int fmt)218 static int query_format(struct vf_instance *vf, unsigned int fmt)
219 {
220     switch(fmt) {
221     case IMGFMT_I420:
222         return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
223     case IMGFMT_YV12:
224     case IMGFMT_422P:
225     case IMGFMT_444P:
226     case IMGFMT_YVYU:
227     case IMGFMT_RGB:
228     case IMGFMT_BGR:
229     case IMGFMT_BGR32:
230         /* These colorspaces are supported, but they'll just have
231          * to be converted to I420 internally */
232         return 0; /* VFCAP_CSP_SUPPORTED */
233     }
234     return 0;
235 }
236 
put_image(struct vf_instance * vf,mp_image_t * mpi,double pts)237 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
238 {
239     h264_module_t *mod=(h264_module_t*)vf->priv;
240     int i;
241 
242     x264_picture_init(&mod->pic);
243     mod->pic.img.i_csp=param.i_csp;
244     mod->pic.img.i_plane=3;
245     for(i=0; i<4; i++) {
246         mod->pic.img.plane[i] = mpi->planes[i];
247         mod->pic.img.i_stride[i] = mpi->stride[i];
248     }
249 
250     mod->pic.i_type = X264_TYPE_AUTO;
251     if (is_forced_key_frame(pts))
252         mod->pic.i_type = X264_TYPE_KEYFRAME;
253 
254     return encode_frame(vf, &mod->pic) >= 0;
255 }
256 
encode_frame(struct vf_instance * vf,x264_picture_t * pic_in)257 static int encode_frame(struct vf_instance *vf, x264_picture_t *pic_in)
258 {
259     h264_module_t *mod=(h264_module_t*)vf->priv;
260     x264_picture_t pic_out;
261     x264_nal_t *nal;
262     int i_nal;
263     int i_size;
264 
265     i_size = x264_encoder_encode(mod->x264, &nal, &i_nal, pic_in, &pic_out);
266 
267     if(i_size<0) {
268         mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_encode failed\n");
269         return -1;
270     }
271     if(i_size>0) {
272         int keyframe = pic_out.b_keyframe;
273         memcpy(mod->mux->buffer, nal->p_payload, i_size);
274         muxer_write_chunk(mod->mux, i_size, keyframe?AVIIF_KEYFRAME:0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
275     }
276     else
277         ++mod->mux->encoder_delay;
278 
279     return i_size;
280 }
281 
uninit(struct vf_instance * vf)282 static void uninit(struct vf_instance *vf)
283 {
284     h264_module_t *mod=(h264_module_t*)vf->priv;
285     if (mod->x264)
286     x264_encoder_close(mod->x264);
287 }
288 
vf_open(vf_instance_t * vf,char * args)289 static int vf_open(vf_instance_t *vf, char *args) {
290     h264_module_t *mod;
291 
292     vf->config = config;
293     vf->default_caps = VFCAP_CONSTANT;
294     vf->control = control;
295     vf->query_format = query_format;
296     vf->put_image = put_image;
297     vf->uninit = uninit;
298     vf->priv = malloc(sizeof(h264_module_t));
299 
300     mod=(h264_module_t*)vf->priv;
301     mod->mux = (muxer_stream_t*)args;
302     mod->mux->bih = calloc(1, sizeof(*mod->mux->bih));
303     mod->mux->bih->biSize = sizeof(*mod->mux->bih);
304     mod->mux->bih->biPlanes = 1;
305     mod->mux->bih->biBitCount = 24;
306     mod->mux->bih->biCompression = mmioFOURCC('h', '2', '6', '4');
307 
308     return 1;
309 }
310 
311 const vf_info_t ve_info_x264 = {
312     "H.264 encoder",
313     "x264",
314     "Bernhard Rosenkraenzer <bero@arklinux.org>",
315     "(C) 2004 LINUX4MEDIA GmbH; (C) 2004 Ark Linux",
316     vf_open
317 };
318