aboutsummaryrefslogtreecommitdiffhomepage
path: root/libmpcodecs/ve_x264.c
blob: 88de7c88bf59f842692c1665b2b86ee7cd9025a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*****************************************************************************
 *
 * - H.264 encoder for mencoder using x264 -
 *
 * Copyright (C) 2004 LINUX4MEDIA GmbH
 * Copyright (C) 2004 Ark Linux
 *
 * Written by Bernhard Rosenkraenzer <bero@arklinux.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation, or if, and only if,
 * version 2 is ruled invalid in a court of law, any later version
 * of the GNU General Public License published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "../config.h"
#include "../mp_msg.h"

#ifdef HAVE_X264

#include "m_option.h"
#include "codec-cfg.h"
#include "stream.h"
#include "demuxer.h"
#include "stheader.h"

#include "muxer.h"

#include "img_format.h"
#include "mp_image.h"
#include "vf.h"

#include <x264.h>

typedef struct _h264_module_t {
    muxer_stream_t *mux;
    x264_param_t    param;
    x264_t *    x264;
    x264_picture_t  pic;
} h264_module_t;

extern char* passtmpfile;

static int bitrate = -1;
static int qp_constant = 26;
static int frame_ref = 1;
static int keyint_max = 250;
static int keyint_min = -1;
static int scenecut_threshold = 40;
static int bframe = 0;
static int bframe_adaptive = 1;
static int bframe_bias = 0;
static int bframe_pyramid = 0;
static int deblock = 1;
static int deblockalpha = 0;
static int deblockbeta = 0;
static int cabac = 1;
static int p4x4mv = 0;
static int p8x8mv = 1;
static int b8x8mv = 1;
static int direct_pred = X264_DIRECT_PRED_TEMPORAL;
static int weight_b = 0;
static int chroma_me = 1;
static int chroma_qp_offset = 0;
static float ip_factor = 1.4;
static float pb_factor = 1.3;
static int rc_buffer_size = -1;
static float rc_init_buffer = 0.25;
static int rc_sens = 4;
static int qp_min = 10;
static int qp_max = 51;
static int qp_step = 1;
static int pass = 0;
static float qcomp = 0.6;
static float qblur = 0.5;
static float complexity_blur = 20;
static char *rc_eq = "blurCplx^(1-qComp)";
static int subq = 5;
static int level_idc = 40;
static int psnr = 0;
static int log_level = 2;

m_option_t x264encopts_conf[] = {
    {"bitrate", &bitrate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
    {"qp_constant", &qp_constant, CONF_TYPE_INT, CONF_RANGE, 1, 51, NULL},
    {"frameref", &frame_ref, CONF_TYPE_INT, CONF_RANGE, 1, 16, NULL},
    {"keyint", &keyint_max, CONF_TYPE_INT, CONF_RANGE, 1, 24000000, NULL},
    {"keyint_min", &keyint_min, CONF_TYPE_INT, CONF_RANGE, 1, 24000000, NULL},
    {"scenecut", &scenecut_threshold, CONF_TYPE_INT, CONF_RANGE, -1, 100, NULL},
    {"bframes", &bframe, CONF_TYPE_INT, CONF_RANGE, 0, 16, NULL},
    {"b_adapt", &bframe_adaptive, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"nob_adapt", &bframe_adaptive, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"b_bias", &bframe_bias, CONF_TYPE_INT, CONF_RANGE, -100, 100, NULL},
    {"b_pyramid", &bframe_pyramid, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"nob_pyramid", &bframe_pyramid, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"deblock", &deblock, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"nodeblock", &deblock, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"deblockalpha", &deblockalpha, CONF_TYPE_INT, CONF_RANGE, -6, 6, NULL},
    {"deblockbeta", &deblockbeta, CONF_TYPE_INT, CONF_RANGE, -6, 6, NULL},
    {"cabac", &cabac, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"nocabac", &cabac, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"4x4mv", &p4x4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"no4x4mv", &p4x4mv, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"8x8mv", &p8x8mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"no8x8mv", &p8x8mv, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"b8x8mv", &b8x8mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"nob8x8mv", &b8x8mv, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"direct_pred", &direct_pred, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL},
    {"weight_b", &weight_b, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"noweight_b", &weight_b, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"chroma_me", &chroma_me, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"nochroma_me", &chroma_me, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"chroma_qp_offset", &chroma_qp_offset, CONF_TYPE_INT, CONF_RANGE, -12, 12, NULL},
    {"ip_factor", &ip_factor, CONF_TYPE_FLOAT, CONF_RANGE, -10.0, 10.0, NULL},
    {"pb_factor", &pb_factor, CONF_TYPE_FLOAT, CONF_RANGE, -10.0, 10.0, NULL},
    {"rc_buffer_size", &rc_buffer_size, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
    {"rc_init_buffer", &rc_init_buffer, CONF_TYPE_FLOAT, CONF_RANGE, 0, 24000000, NULL},
    {"rc_sens", &rc_sens, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL},
    {"qp_min", &qp_min, CONF_TYPE_INT, CONF_RANGE, 1, 51, NULL},
    {"qp_max", &qp_max, CONF_TYPE_INT, CONF_RANGE, 1, 51, NULL},
    {"qp_step", &qp_step, CONF_TYPE_INT, CONF_RANGE, 1, 50, NULL},
    {"pass", &pass, CONF_TYPE_INT, CONF_RANGE, 1, 3, NULL},
    {"rc_eq", &rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL},
    {"qcomp", &qcomp, CONF_TYPE_FLOAT, CONF_RANGE, 0, 1, NULL},
    {"qblur", &qblur, CONF_TYPE_FLOAT, CONF_RANGE, 0, 99, NULL},
    {"cplx_blur", &complexity_blur, CONF_TYPE_FLOAT, CONF_RANGE, 0, 999, NULL},
    {"subq", &subq, CONF_TYPE_INT, CONF_RANGE, 1, 5, NULL},
    {"level_idc", &level_idc, CONF_TYPE_INT, CONF_RANGE, 10, 51, NULL},
    {"psnr", &psnr, CONF_TYPE_FLAG, 0, 0, 1, NULL},
    {"nopsnr", &psnr, CONF_TYPE_FLAG, 0, 1, 0, NULL},
    {"log", &log_level, CONF_TYPE_INT, CONF_RANGE, -1, 3, NULL},
    {NULL, NULL, 0, 0, 0, 0, NULL}
};

static int put_image(struct vf_instance_s *vf, mp_image_t *mpi);
static int encode_frame(struct vf_instance_s *vf, x264_picture_t *pic_in);

static int config(struct vf_instance_s* vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt) {
    h264_module_t *mod=(h264_module_t*)vf->priv;
    mod->mux->bih->biWidth = width;
    mod->mux->bih->biHeight = height;
    mod->mux->aspect = (float)d_width/d_height;
    
    x264_param_default(&mod->param);
    mod->param.i_frame_reference = frame_ref;
    mod->param.i_keyint_max = keyint_max;
    mod->param.i_keyint_min = keyint_min > 0 ? keyint_min : keyint_max * 2 / 5;
    mod->param.i_scenecut_threshold = scenecut_threshold;
    mod->param.i_bframe = bframe;
    mod->param.b_bframe_adaptive = bframe_adaptive;
    mod->param.i_bframe_bias = bframe_bias;
    mod->param.b_bframe_pyramid = bframe_pyramid;
    mod->param.b_deblocking_filter = deblock;
    mod->param.i_deblocking_filter_alphac0 = deblockalpha;
    mod->param.i_deblocking_filter_beta = deblockbeta;
    mod->param.b_cabac = cabac;

    mod->param.rc.i_qp_constant = qp_constant;
    if(qp_min > qp_constant)
        qp_min = qp_constant;
    if(qp_max < qp_constant)
        qp_max = qp_constant;
    mod->param.rc.i_qp_min = qp_min;
    mod->param.rc.i_qp_max = qp_max;
    mod->param.rc.i_qp_step = qp_step;
    mod->param.rc.psz_rc_eq = rc_eq;
    mod->param.rc.f_qcompress = qcomp;
    mod->param.rc.f_qblur = qblur;
    mod->param.rc.f_complexity_blur = complexity_blur;
    mod->param.analyse.i_subpel_refine = subq;
    mod->param.rc.psz_stat_out = passtmpfile;
    mod->param.rc.psz_stat_in = passtmpfile;
    if((pass & 2) && bitrate <= 0)
    {
        mp_msg(MSGT_MENCODER, MSGL_ERR,
               "2 pass encoding enabled, but no bitrate specified.\n");
        return 0;
    }
    switch(pass) {
    case 0:
        mod->param.rc.b_stat_write = 0;
        mod->param.rc.b_stat_read = 0;
        break;
    case 1:
        mod->param.rc.b_stat_write = 1;
        mod->param.rc.b_stat_read = 0;
        break;
    case 2:
        mod->param.rc.b_stat_write = 0;
        mod->param.rc.b_stat_read = 1;
        break;
    case 3:
        mod->param.rc.b_stat_write = 1;
        mod->param.rc.b_stat_read = 1;
        break;
    }
    if(bitrate > 0) {
        if(rc_buffer_size <= 0)
            rc_buffer_size = bitrate;
        mod->param.rc.b_cbr = 1;
        mod->param.rc.i_bitrate = bitrate;
        mod->param.rc.i_rc_buffer_size = rc_buffer_size;
        if(rc_init_buffer > 1)
            mod->param.rc.i_rc_init_buffer = rc_init_buffer;
        else
            mod->param.rc.i_rc_init_buffer = rc_buffer_size * rc_init_buffer;
        mod->param.rc.i_rc_sens = rc_sens;
    }
    mod->param.rc.f_ip_factor = ip_factor;
    mod->param.rc.f_pb_factor = pb_factor;
    mod->param.analyse.inter = X264_ANALYSE_I4x4;
    if(p4x4mv)
        mod->param.analyse.inter |= X264_ANALYSE_PSUB8x8;
    if(p8x8mv)
        mod->param.analyse.inter |= X264_ANALYSE_PSUB16x16;
    if(b8x8mv)
        mod->param.analyse.inter |= X264_ANALYSE_BSUB16x16;
    mod->param.analyse.i_direct_mv_pred = direct_pred;
    mod->param.analyse.b_weighted_bipred = weight_b;
    mod->param.analyse.i_chroma_qp_offset = chroma_qp_offset;
    mod->param.analyse.b_chroma_me = chroma_me;

    mod->param.i_width = width;
    mod->param.i_height = height;
    mod->param.i_fps_num = mod->mux->h.dwRate;
    mod->param.i_fps_den = mod->mux->h.dwScale;
    mod->param.i_level_idc = level_idc;
    mod->param.analyse.b_psnr = psnr;
    mod->param.i_log_level = log_level;
    mod->param.vui.i_sar_width = d_width*height;
    mod->param.vui.i_sar_height = d_height*width;

    switch(outfmt) {
    case IMGFMT_I420:
        mod->param.i_csp = X264_CSP_I420;
        mod->mux->bih->biSizeImage = width * height * 3;
        break;
    case IMGFMT_YV12:
        mod->param.i_csp = X264_CSP_YV12;
        mod->mux->bih->biSizeImage = width * height * 3;
        break;
    case IMGFMT_422P:
        mod->param.i_csp = X264_CSP_I422;
        mod->mux->bih->biSizeImage = width * height * 3;
        break;
    case IMGFMT_444P:
        mod->param.i_csp = X264_CSP_I444;
        mod->mux->bih->biSizeImage = width * height * 3;
        break;
    case IMGFMT_YVYU:
        mod->param.i_csp = X264_CSP_YUYV;
        mod->mux->bih->biSizeImage = width * height * 3;
        break;
    case IMGFMT_RGB:
        mod->param.i_csp = X264_CSP_RGB;
        mod->mux->bih->biSizeImage = width * height * 3;
        break;
    case IMGFMT_BGR:
        mod->param.i_csp = X264_CSP_BGR;
        mod->mux->bih->biSizeImage = width * height * 3;
        break;
    case IMGFMT_BGR32:
        mod->param.i_csp = X264_CSP_BGRA;
        mod->mux->bih->biSizeImage = width * height * 4;
        break;
    default:
        mp_msg(MSGT_MENCODER, MSGL_ERR, "Wrong colorspace.\n");
        return 0;
    }
    
    mod->x264 = x264_encoder_open(&mod->param);
    if(!mod->x264) {
        mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_open failed.\n");
        return 0;
    }
    
    return 1;
}

static int control(struct vf_instance_s* vf, int request, void *data)
{
    switch(request){
        case VFCTRL_FLUSH_FRAMES:
            if(bframe)
                while(encode_frame(vf, NULL) > 0);
            return CONTROL_TRUE;
        default:
            return CONTROL_UNKNOWN;
    }
}

static int query_format(struct vf_instance_s* vf, unsigned int fmt)
{
    switch(fmt) {
    case IMGFMT_I420:
        return (VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW);
    case IMGFMT_YV12:
    case IMGFMT_422P:
    case IMGFMT_444P:
    case IMGFMT_YVYU:
    case IMGFMT_RGB:
    case IMGFMT_BGR:
    case IMGFMT_BGR32:
        /* These colorspaces are supported, but they'll just have
         * to be converted to I420 internally */
        return 0; /* VFCAP_CSP_SUPPORTED */
    }
    return 0;
}

static int put_image(struct vf_instance_s *vf, mp_image_t *mpi)
{
    h264_module_t *mod=(h264_module_t*)vf->priv;
    int i;
    
    memset(&mod->pic, 0, sizeof(x264_picture_t));
    mod->pic.img.i_csp=mod->param.i_csp;
    mod->pic.img.i_plane=3;
    for(i=0; i<4; i++) {
        mod->pic.img.plane[i] = mpi->planes[i];
        mod->pic.img.i_stride[i] = mpi->stride[i];
    }

    mod->pic.i_type = X264_TYPE_AUTO;

    return encode_frame(vf, &mod->pic) >= 0;
}

static int encode_frame(struct vf_instance_s *vf, x264_picture_t *pic_in)
{
    h264_module_t *mod=(h264_module_t*)vf->priv;
    x264_picture_t pic_out;
    x264_nal_t *nal;
    int i_nal;
    int i_size = 0;
    int i;

    if(x264_encoder_encode(mod->x264, &nal, &i_nal, pic_in, &pic_out) < 0) {
        mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_encode failed\n");
        return -1;
    }
    
    for(i=0; i < i_nal; i++) {
        int i_data = mod->mux->buffer_size - i_size;
        i_size += x264_nal_encode(mod->mux->buffer + i_size, &i_data, 1, &nal[i]);
    }
    if(i_size>0) {
        int keyframe = (pic_out.i_type == X264_TYPE_IDR) ||
                       (pic_out.i_type == X264_TYPE_I
                        && frame_ref == 1 && !bframe);
        muxer_write_chunk(mod->mux, i_size, keyframe?0x10:0);
    }

    return i_size;
}

static void uninit(struct vf_instance_s *vf)
{
    h264_module_t *mod=(h264_module_t*)vf->priv;
    x264_encoder_close(mod->x264);
}

static int vf_open(vf_instance_t *vf, char *args) {
    h264_module_t *mod;

    vf->config = config;
    vf->default_caps = VFCAP_CONSTANT;
    vf->control = control;
    vf->query_format = query_format;
    vf->put_image = put_image;
    vf->uninit = uninit;
    vf->priv = malloc(sizeof(h264_module_t));

    mod=(h264_module_t*)vf->priv;
    mod->mux = (muxer_stream_t*)args;
    mod->mux->bih = malloc(sizeof(BITMAPINFOHEADER));
    memset(mod->mux->bih, 0, sizeof(BITMAPINFOHEADER));
    mod->mux->bih->biSize = sizeof(BITMAPINFOHEADER);
    mod->mux->bih->biPlanes = 1;
    mod->mux->bih->biBitCount = 24;
    mod->mux->bih->biCompression = mmioFOURCC('h', '2', '6', '4');

    return 1;
}

vf_info_t ve_info_x264 = {
    "H.264 encoder",
    "x264",
    "Bernhard Rosenkraenzer <bero@arklinux.org>",
    "(C) 2004 LINUX4MEDIA GmbH; (C) 2004 Ark Linux",
    vf_open
};
#endif