2004-08-27 20:43:05 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* - H.264 encoder for mencoder using x264 -
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 LINUX4MEDIA GmbH
|
|
|
|
* Copyright (C) 2004 Ark Linux
|
|
|
|
*
|
|
|
|
* Written by Bernhard Rosenkraenzer <bero@arklinux.org>
|
|
|
|
*
|
2008-05-07 07:49:35 +00:00
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
2004-08-27 20:43:05 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-05-07 07:49:35 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2004-08-27 20:43:05 +00:00
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2008-05-07 07:49:35 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-08-27 20:43:05 +00:00
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "mp_msg.h"
|
2004-08-27 20:43:05 +00:00
|
|
|
|
|
|
|
#include "m_option.h"
|
|
|
|
#include "codec-cfg.h"
|
2007-03-15 17:30:55 +00:00
|
|
|
#include "stream/stream.h"
|
2007-03-15 17:51:32 +00:00
|
|
|
#include "libmpdemux/demuxer.h"
|
|
|
|
#include "libmpdemux/stheader.h"
|
2004-08-27 20:43:05 +00:00
|
|
|
|
2007-03-15 17:30:55 +00:00
|
|
|
#include "stream/stream.h"
|
2007-03-15 17:51:32 +00:00
|
|
|
#include "libmpdemux/muxer.h"
|
2004-08-27 20:43:05 +00:00
|
|
|
|
|
|
|
#include "img_format.h"
|
|
|
|
#include "mp_image.h"
|
|
|
|
#include "vf.h"
|
|
|
|
|
|
|
|
#include <x264.h>
|
|
|
|
|
2008-01-06 23:20:15 +00:00
|
|
|
typedef struct h264_module_t {
|
2004-08-27 20:43:05 +00:00
|
|
|
muxer_stream_t *mux;
|
|
|
|
x264_t * x264;
|
|
|
|
x264_picture_t pic;
|
|
|
|
} h264_module_t;
|
|
|
|
|
|
|
|
extern char* passtmpfile;
|
2005-07-11 17:52:03 +00:00
|
|
|
static int turbo = 0;
|
2006-10-15 23:11:36 +00:00
|
|
|
static x264_param_t param;
|
2006-10-20 19:19:29 +00:00
|
|
|
static int parse_error = 0;
|
2005-09-02 21:54:17 +00:00
|
|
|
|
2006-03-21 21:26:42 +00:00
|
|
|
static int put_image(struct vf_instance_s *vf, mp_image_t *mpi, double pts);
|
2005-02-23 19:59:24 +00:00
|
|
|
static int encode_frame(struct vf_instance_s *vf, x264_picture_t *pic_in);
|
2004-08-27 20:43:05 +00:00
|
|
|
|
2007-12-02 16:40:20 +00:00
|
|
|
void x264enc_set_param(const m_option_t* opt, char* arg)
|
2006-10-15 23:11:36 +00:00
|
|
|
{
|
|
|
|
static int initted = 0;
|
|
|
|
if(!initted) {
|
|
|
|
x264_param_default(¶m);
|
|
|
|
x264_param_parse(¶m, "psnr", "no");
|
|
|
|
x264_param_parse(¶m, "ssim", "no");
|
|
|
|
initted = 1;
|
|
|
|
}
|
|
|
|
|
2007-01-28 20:22:21 +00:00
|
|
|
if(!arg) {
|
|
|
|
parse_error = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-15 23:11:36 +00:00
|
|
|
while(*arg) {
|
|
|
|
char *name = arg;
|
2006-10-05 02:03:10 +00:00
|
|
|
char *value;
|
|
|
|
int ret;
|
|
|
|
|
2006-10-15 23:11:36 +00:00
|
|
|
arg += strcspn(arg, ":");
|
|
|
|
if(*arg) {
|
|
|
|
*arg = 0;
|
|
|
|
arg++;
|
2005-09-02 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 02:03:10 +00:00
|
|
|
value = strchr( name, '=' );
|
|
|
|
if(value) {
|
|
|
|
*value = 0;
|
|
|
|
value++;
|
|
|
|
}
|
2005-09-02 21:54:17 +00:00
|
|
|
|
2006-10-05 02:03:10 +00:00
|
|
|
if(!strcmp(name, "turbo")) {
|
|
|
|
turbo = value ? atoi(value) : 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-10-15 23:11:36 +00:00
|
|
|
ret = x264_param_parse(¶m, name, value);
|
2006-10-05 02:03:10 +00:00
|
|
|
if(ret == X264_PARAM_BAD_NAME)
|
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option x264encopts: Unknown suboption %s\n", name);
|
|
|
|
if(ret == X264_PARAM_BAD_VALUE)
|
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option x264encopts: Bad argument %s=%s\n", name, value ? value : "(null)");
|
2006-10-15 23:11:36 +00:00
|
|
|
|
|
|
|
/* mark this option as done, so it's not reparsed if there's another -x264encopts */
|
|
|
|
*name = 0;
|
|
|
|
|
2006-10-20 19:19:29 +00:00
|
|
|
parse_error |= ret;
|
2006-10-05 02:03:10 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 21:57:15 +00:00
|
|
|
if(param.rc.b_stat_write && !param.rc.b_stat_read) {
|
2005-07-11 17:52:03 +00:00
|
|
|
/* Adjust or disable some flags to gain speed in the first pass */
|
|
|
|
if(turbo == 1)
|
|
|
|
{
|
2006-10-15 23:11:36 +00:00
|
|
|
param.i_frame_reference = ( param.i_frame_reference + 1 ) >> 1;
|
2006-12-09 12:24:11 +00:00
|
|
|
param.analyse.i_subpel_refine = FFMAX( FFMIN( 3, param.analyse.i_subpel_refine - 1 ), 1 );
|
2006-10-15 23:11:36 +00:00
|
|
|
param.analyse.inter &= ( ~X264_ANALYSE_PSUB8x8 );
|
|
|
|
param.analyse.inter &= ( ~X264_ANALYSE_BSUB16x16 );
|
|
|
|
param.analyse.i_trellis = 0;
|
2005-07-11 17:52:03 +00:00
|
|
|
}
|
2006-10-05 02:03:10 +00:00
|
|
|
else if(turbo >= 2)
|
2005-07-11 17:52:03 +00:00
|
|
|
{
|
2006-10-15 23:11:36 +00:00
|
|
|
param.i_frame_reference = 1;
|
|
|
|
param.analyse.i_subpel_refine = 1;
|
|
|
|
param.analyse.i_me_method = X264_ME_DIA;
|
|
|
|
param.analyse.inter = 0;
|
|
|
|
param.analyse.b_transform_8x8 = 0;
|
|
|
|
param.analyse.b_weighted_bipred = 0;
|
|
|
|
param.analyse.i_trellis = 0;
|
2005-07-11 17:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-15 23:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2006-10-20 19:19:29 +00:00
|
|
|
|
|
|
|
if(parse_error)
|
|
|
|
return 0;
|
|
|
|
|
2006-10-15 23:11:36 +00:00
|
|
|
mod->mux->bih->biWidth = width;
|
|
|
|
mod->mux->bih->biHeight = height;
|
2008-01-05 11:03:54 +00:00
|
|
|
mod->mux->bih->biSizeImage = width * height * 3;
|
2006-10-15 23:11:36 +00:00
|
|
|
mod->mux->aspect = (float)d_width/d_height;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-10-21 19:34:06 +00:00
|
|
|
// make sure param is initialized
|
|
|
|
x264enc_set_param(NULL, "");
|
2006-10-15 23:11:36 +00:00
|
|
|
param.i_width = width;
|
|
|
|
param.i_height = height;
|
|
|
|
param.i_fps_num = mod->mux->h.dwRate;
|
|
|
|
param.i_fps_den = mod->mux->h.dwScale;
|
|
|
|
param.vui.i_sar_width = d_width*height;
|
|
|
|
param.vui.i_sar_height = d_height*width;
|
2005-07-11 17:52:03 +00:00
|
|
|
|
2006-10-19 08:15:47 +00:00
|
|
|
x264_param_parse(¶m, "stats", passtmpfile);
|
|
|
|
|
2004-08-27 20:43:05 +00:00
|
|
|
switch(outfmt) {
|
|
|
|
case IMGFMT_I420:
|
2006-10-15 23:11:36 +00:00
|
|
|
param.i_csp = X264_CSP_I420;
|
2004-08-27 20:43:05 +00:00
|
|
|
break;
|
|
|
|
case IMGFMT_YV12:
|
2006-10-15 23:11:36 +00:00
|
|
|
param.i_csp = X264_CSP_YV12;
|
2004-08-27 20:43:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_ERR, "Wrong colorspace.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-10-15 23:11:36 +00:00
|
|
|
mod->x264 = x264_encoder_open(¶m);
|
2004-08-27 20:43:05 +00:00
|
|
|
if(!mod->x264) {
|
|
|
|
mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_open failed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-28 19:32:53 +00:00
|
|
|
|
2006-10-15 23:11:36 +00:00
|
|
|
if(!param.b_repeat_headers){
|
2006-08-28 19:32:53 +00:00
|
|
|
x264_nal_t *nal;
|
2009-09-23 12:01:04 +00:00
|
|
|
int extradata_size, nnal;
|
2006-08-28 19:32:53 +00:00
|
|
|
|
2009-09-23 12:01:04 +00:00
|
|
|
extradata_size = x264_encoder_headers(mod->x264, &nal, &nnal);
|
2006-08-28 19:32:53 +00:00
|
|
|
|
|
|
|
mod->mux->bih= realloc(mod->mux->bih, sizeof(BITMAPINFOHEADER) + extradata_size);
|
2009-09-23 12:01:04 +00:00
|
|
|
memcpy(mod->mux->bih + 1, nal->p_payload, extradata_size);
|
2006-08-28 19:32:53 +00:00
|
|
|
mod->mux->bih->biSize= sizeof(BITMAPINFOHEADER) + extradata_size;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-10-15 23:11:36 +00:00
|
|
|
if (param.i_bframe > 1 && param.b_bframe_pyramid)
|
2006-02-21 09:39:22 +00:00
|
|
|
mod->mux->decoder_delay = 2;
|
|
|
|
else
|
2006-10-15 23:11:36 +00:00
|
|
|
mod->mux->decoder_delay = param.i_bframe ? 1 : 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2004-08-27 20:43:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int control(struct vf_instance_s* vf, int request, void *data)
|
|
|
|
{
|
2006-10-05 02:03:10 +00:00
|
|
|
h264_module_t *mod=(h264_module_t*)vf->priv;
|
2009-08-07 09:07:02 +00:00
|
|
|
int count = 256; // giant HACK, x264_encoder_encode may incorrectly return 0
|
|
|
|
// when threads > 1 and delayed frames pending
|
2005-02-23 19:59:24 +00:00
|
|
|
switch(request){
|
|
|
|
case VFCTRL_FLUSH_FRAMES:
|
2009-08-07 09:07:02 +00:00
|
|
|
while(encode_frame(vf, NULL) == 0 && --count);
|
2005-02-23 19:59:24 +00:00
|
|
|
while(encode_frame(vf, NULL) > 0);
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
default:
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
2004-08-27 20:43:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_format(struct vf_instance_s* vf, unsigned int fmt)
|
|
|
|
{
|
|
|
|
switch(fmt) {
|
|
|
|
case IMGFMT_I420:
|
2008-05-16 00:23:02 +00:00
|
|
|
return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
|
2004-08-27 20:43:05 +00:00
|
|
|
case IMGFMT_YV12:
|
|
|
|
case IMGFMT_422P:
|
|
|
|
case IMGFMT_444P:
|
|
|
|
case IMGFMT_YVYU:
|
|
|
|
case IMGFMT_RGB:
|
|
|
|
case IMGFMT_BGR:
|
|
|
|
case IMGFMT_BGR32:
|
2005-03-14 23:26:11 +00:00
|
|
|
/* These colorspaces are supported, but they'll just have
|
|
|
|
* to be converted to I420 internally */
|
2004-08-27 20:43:05 +00:00
|
|
|
return 0; /* VFCAP_CSP_SUPPORTED */
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-21 21:26:42 +00:00
|
|
|
static int put_image(struct vf_instance_s *vf, mp_image_t *mpi, double pts)
|
2004-08-27 20:43:05 +00:00
|
|
|
{
|
|
|
|
h264_module_t *mod=(h264_module_t*)vf->priv;
|
|
|
|
int i;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2004-08-27 20:43:05 +00:00
|
|
|
memset(&mod->pic, 0, sizeof(x264_picture_t));
|
2006-10-15 23:11:36 +00:00
|
|
|
mod->pic.img.i_csp=param.i_csp;
|
2004-08-27 20:43:05 +00:00
|
|
|
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;
|
2005-02-23 19:59:24 +00:00
|
|
|
|
|
|
|
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;
|
2009-09-23 12:01:04 +00:00
|
|
|
int i_size;
|
2005-02-23 19:59:24 +00:00
|
|
|
|
2009-09-23 12:01:04 +00:00
|
|
|
i_size = x264_encoder_encode(mod->x264, &nal, &i_nal, pic_in, &pic_out);
|
|
|
|
|
|
|
|
if(i_size<0) {
|
2004-08-27 20:43:05 +00:00
|
|
|
mp_msg(MSGT_MENCODER, MSGL_ERR, "x264_encoder_encode failed\n");
|
2005-02-23 19:59:24 +00:00
|
|
|
return -1;
|
2004-08-27 20:43:05 +00:00
|
|
|
}
|
|
|
|
if(i_size>0) {
|
2005-02-23 19:59:24 +00:00
|
|
|
int keyframe = (pic_out.i_type == X264_TYPE_IDR) ||
|
|
|
|
(pic_out.i_type == X264_TYPE_I
|
2006-10-15 23:11:36 +00:00
|
|
|
&& param.i_frame_reference == 1
|
|
|
|
&& !param.i_bframe);
|
2009-09-23 12:01:04 +00:00
|
|
|
memcpy(mod->mux->buffer, nal->p_payload, i_size);
|
2006-01-26 19:32:07 +00:00
|
|
|
muxer_write_chunk(mod->mux, i_size, keyframe?0x10:0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
|
2004-08-27 20:43:05 +00:00
|
|
|
}
|
2006-02-21 09:39:22 +00:00
|
|
|
else
|
|
|
|
++mod->mux->encoder_delay;
|
2005-02-23 19:59:24 +00:00
|
|
|
|
|
|
|
return i_size;
|
2004-08-27 20:43:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(struct vf_instance_s *vf)
|
|
|
|
{
|
|
|
|
h264_module_t *mod=(h264_module_t*)vf->priv;
|
2007-02-08 15:42:53 +00:00
|
|
|
if (mod->x264)
|
2004-08-27 20:43:05 +00:00
|
|
|
x264_encoder_close(mod->x264);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vf_open(vf_instance_t *vf, char *args) {
|
2004-09-14 16:57:37 +00:00
|
|
|
h264_module_t *mod;
|
|
|
|
|
2004-08-27 20:43:05 +00:00
|
|
|
vf->config = config;
|
2005-03-01 20:21:58 +00:00
|
|
|
vf->default_caps = VFCAP_CONSTANT;
|
2004-08-27 20:43:05 +00:00
|
|
|
vf->control = control;
|
|
|
|
vf->query_format = query_format;
|
|
|
|
vf->put_image = put_image;
|
|
|
|
vf->uninit = uninit;
|
|
|
|
vf->priv = malloc(sizeof(h264_module_t));
|
|
|
|
|
2004-09-14 16:57:37 +00:00
|
|
|
mod=(h264_module_t*)vf->priv;
|
2004-08-27 20:43:05 +00:00
|
|
|
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
|
|
|
|
};
|