view libmpcodecs/ve_x264.c @ 20105:bcb586a0800c

Avoid crash with fontconfig 2.3.9x (as shipped with SuSE 10.1, FcDirScan is broken) and warn that these are beta versions and not supported.
author reimar
date Sun, 08 Oct 2006 13:01:14 +0000
parents 8760c8681eac
children eaaf13b39595
line wrap: on
line source

/*****************************************************************************
 *
 * - 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 *****************************************************************************/

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

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

#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 turbo = 0;
char *x264encopts = "";

static int encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal){
    uint8_t *p = buf;
    int i;

    for(i = 0; i < nnal; i++){
        int s = x264_nal_encode(p, &size, 1, nals + i);
        if(s < 0)
            return -1;
        p += s;
    }

    return p - buf;
}

static int put_image(struct vf_instance_s *vf, mp_image_t *mpi, double pts);
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_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.vui.i_sar_width = d_width*height;
    mod->param.vui.i_sar_height = d_height*width;
    x264_param_parse(&mod->param, "psnr", "no");
    x264_param_parse(&mod->param, "ssim", "no");

    while(*x264encopts) {
        char *name = x264encopts;
        char *value;
        int ret;

        x264encopts += strcspn(x264encopts, ":");
        if(*x264encopts) {
            *x264encopts = 0;
            x264encopts++;
        }

        value = strchr( name, '=' );
        if(value) {
            *value = 0;
            value++;
        }

        if(!strcmp(name, "turbo")) {
            turbo = value ? atoi(value) : 1;
            continue;
        }

        ret = x264_param_parse(&mod->param, name, value);
        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)");
        if(ret)
            return 0;
    }

    if(mod->param.rc.b_stat_write) {
        /* Adjust or disable some flags to gain speed in the first pass */
        if(turbo == 1)
        {
            mod->param.i_frame_reference = ( mod->param.i_frame_reference + 1 ) >> 1;
            mod->param.analyse.i_subpel_refine = max( min( 3, mod->param.analyse.i_subpel_refine - 1 ), 1 );
            mod->param.analyse.inter &= ( ~X264_ANALYSE_PSUB8x8 );
            mod->param.analyse.inter &= ( ~X264_ANALYSE_BSUB16x16 );
            mod->param.analyse.i_trellis = 0;
        }
        else if(turbo >= 2)
        {
            mod->param.i_frame_reference = 1;
            mod->param.analyse.i_subpel_refine = 1;
            mod->param.analyse.i_me_method = X264_ME_DIA;
            mod->param.analyse.inter = 0;
            mod->param.analyse.b_transform_8x8 = 0;
            mod->param.analyse.b_weighted_bipred = 0;
            mod->param.analyse.i_trellis = 0;
        }
    }

    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;
    }

    if(!mod->param.b_repeat_headers){
        uint8_t *extradata;
        x264_nal_t *nal;
        int extradata_size, nnal, i, s = 0;

        x264_encoder_headers(mod->x264, &nal, &nnal);

        /* 5 bytes NAL header + worst case escaping */
        for(i = 0; i < nnal; i++)
            s += 5 + nal[i].i_payload * 4 / 3;

        extradata = malloc(s);
        extradata_size = encode_nals(extradata, s, nal, nnal);

        mod->mux->bih= realloc(mod->mux->bih, sizeof(BITMAPINFOHEADER) + extradata_size);
        memcpy(mod->mux->bih + 1, extradata, extradata_size);
        mod->mux->bih->biSize= sizeof(BITMAPINFOHEADER) + extradata_size;
    }
    
    if (mod->param.i_bframe > 1 && mod->param.b_bframe_pyramid)
        mod->mux->decoder_delay = 2;
    else
        mod->mux->decoder_delay = mod->param.i_bframe ? 1 : 0;
    
    return 1;
}

static int control(struct vf_instance_s* vf, int request, void *data)
{
    h264_module_t *mod=(h264_module_t*)vf->priv;
    switch(request){
        case VFCTRL_FLUSH_FRAMES:
            if(mod->param.i_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, double pts)
{
    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
                        && mod->param.i_frame_reference == 1
                        && !mod->param.i_bframe);
        muxer_write_chunk(mod->mux, i_size, keyframe?0x10:0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
    }
    else
        ++mod->mux->encoder_delay;

    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
};