x265.c 7 KB
Newer Older
Rafaël Carré's avatar
Rafaël Carré committed
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
/*****************************************************************************
 * x265.c: HEVC/H.265 video encoder
 *****************************************************************************
 * Copyright (C) 2013 Rafaël Carré
 *
 * Authors: Rafaël Carré <funman@videolanorg>
 *
 * This program 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

30
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
Rafaël Carré's avatar
Rafaël Carré committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_threads.h>
#include <vlc_sout.h>
#include <vlc_codec.h>

#include <x265.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

vlc_module_begin ()
    set_description(N_("H.265/HEVC encoder (x265)"))
    set_capability("encoder", 200)
    set_callbacks(Open, Close)
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_VCODEC)
vlc_module_end ()

struct encoder_sys_t
{
Rafaël Carré's avatar
Rafaël Carré committed
55 56
    x265_encoder    *h;
    x265_param      param;
Rafaël Carré's avatar
Rafaël Carré committed
57 58 59 60 61 62 63 64 65 66 67 68 69

    mtime_t         i_initial_delay;

    mtime_t         dts;
    mtime_t         initial_date;
#ifndef NDEBUG
    mtime_t         start;
#endif
};

static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
{
    encoder_sys_t *p_sys = p_enc->p_sys;
Rafaël Carré's avatar
Rafaël Carré committed
70
    x265_picture pic;
Rafaël Carré's avatar
Rafaël Carré committed
71

72 73
    x265_picture_init(&p_sys->param, &pic);

Rafaël Carré's avatar
Rafaël Carré committed
74
    if (likely(p_pict)) {
75
        pic.pts = p_pict->date;
Rafaël Carré's avatar
Rafaël Carré committed
76 77 78 79 80 81 82 83 84 85 86 87 88
        if (unlikely(p_sys->initial_date == 0)) {
            p_sys->initial_date = p_pict->date;
#ifndef NDEBUG
            p_sys->start = mdate();
#endif
        }

        for (int i = 0; i < p_pict->i_planes; i++) {
            pic.planes[i] = p_pict->p[i].p_pixels;
            pic.stride[i] = p_pict->p[i].i_pitch;
        }
    }

Rafaël Carré's avatar
Rafaël Carré committed
89 90
    x265_nal *nal;
    uint32_t i_nal = 0;
Rafaël Carré's avatar
Rafaël Carré committed
91 92 93 94 95 96 97
    x265_encoder_encode(p_sys->h, &nal, &i_nal,
            likely(p_pict) ? &pic : NULL, &pic);

    if (!i_nal)
        return NULL;

    int i_out = 0;
Rafaël Carré's avatar
Rafaël Carré committed
98 99
    for (uint32_t i = 0; i < i_nal; i++)
        i_out += nal[i].sizeBytes;
Rafaël Carré's avatar
Rafaël Carré committed
100

101
    block_t *p_block = block_Alloc(i_out);
Rafaël Carré's avatar
Rafaël Carré committed
102 103 104 105
    if (!p_block)
        return NULL;

    /* all payloads are sequentially laid out in memory */
106
    memcpy(p_block->p_buffer, nal[0].payload, i_out);
Rafaël Carré's avatar
Rafaël Carré committed
107 108 109 110 111 112 113 114 115

    /* This isn't really valid for streams with B-frames */
    p_block->i_length = CLOCK_FREQ *
        p_enc->fmt_in.video.i_frame_rate_base /
            p_enc->fmt_in.video.i_frame_rate;

    p_block->i_pts = p_sys->initial_date + pic.poc * p_block->i_length;
    p_block->i_dts = p_sys->initial_date + p_sys->dts++ * p_block->i_length;

116 117 118
    switch (pic.sliceType)
    {
    case X265_TYPE_I:
119
    case X265_TYPE_IDR:
120 121 122 123 124 125
        p_block->i_flags |= BLOCK_FLAG_TYPE_I;
        break;
    case X265_TYPE_P:
        p_block->i_flags |= BLOCK_FLAG_TYPE_P;
        break;
    case X265_TYPE_B:
126
    case X265_TYPE_BREF:
127 128 129 130
        p_block->i_flags |= BLOCK_FLAG_TYPE_B;
        break;
    }

Rafaël Carré's avatar
Rafaël Carré committed
131 132 133 134 135 136 137 138 139 140 141 142 143
#ifndef NDEBUG
    msg_Dbg(p_enc, "%zu bytes (frame %"PRId64", %.2ffps)", p_block->i_buffer,
        p_sys->dts, (float)p_sys->dts * CLOCK_FREQ / (mdate() - p_sys->start));
#endif

    return p_block;
}

static int  Open (vlc_object_t *p_this)
{
    encoder_t     *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys;

144
    if (p_enc->fmt_out.i_codec != VLC_CODEC_HEVC && !p_enc->obj.force)
Rafaël Carré's avatar
Rafaël Carré committed
145 146 147 148 149 150 151 152 153 154
        return VLC_EGENERIC;

    p_enc->fmt_out.i_cat = VIDEO_ES;
    p_enc->fmt_out.i_codec = VLC_CODEC_HEVC;
    p_enc->p_sys = p_sys = malloc(sizeof(encoder_sys_t));
    if (!p_sys)
        return VLC_ENOMEM;

    p_enc->fmt_in.i_codec = VLC_CODEC_I420;

Rafaël Carré's avatar
Rafaël Carré committed
155
    x265_param *param = &p_sys->param;
Rafaël Carré's avatar
Rafaël Carré committed
156 157 158 159 160 161
    x265_param_default(param);

    param->frameNumThreads = vlc_GetCPUCount();
    param->bEnableWavefront = 0; // buggy in x265, use frame threading for now
    param->maxCUSize = 16; /* use smaller macroblock */

Rafaël Carré's avatar
Rafaël Carré committed
162 163 164
#if X265_BUILD >= 6
    param->fpsNum = p_enc->fmt_in.video.i_frame_rate;
    param->fpsDenom = p_enc->fmt_in.video.i_frame_rate_base;
165 166 167 168
    if (!param->fpsNum) {
        param->fpsNum = 25;
        param->fpsDenom = 1;
    }
Rafaël Carré's avatar
Rafaël Carré committed
169
#else
170 171
    if (p_enc->fmt_in.video.i_frame_rate_base) {
        param->frameRate = p_enc->fmt_in.video.i_frame_rate /
Rafaël Carré's avatar
Rafaël Carré committed
172
            p_enc->fmt_in.video.i_frame_rate_base;
173 174 175
    } else {
        param->frameRate = 25;
    }
Rafaël Carré's avatar
Rafaël Carré committed
176
#endif
Rafaël Carré's avatar
Rafaël Carré committed
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
    param->sourceWidth = p_enc->fmt_in.video.i_visible_width;
    param->sourceHeight = p_enc->fmt_in.video.i_visible_height;

    if (param->sourceWidth & (param->maxCUSize - 1)) {
        msg_Err(p_enc, "Width (%d) must be a multiple of %d",
            param->sourceWidth, param->maxCUSize);
        free(p_sys);
        return VLC_EGENERIC;
    }
    if (param->sourceHeight & 7) {
        msg_Err(p_enc, "Height (%d) must be a multiple of 8", param->sourceHeight);
        free(p_sys);
        return VLC_EGENERIC;
    }

    if (p_enc->fmt_out.i_bitrate > 0) {
        param->rc.bitrate = p_enc->fmt_out.i_bitrate / 1000;
        param->rc.rateControlMode = X265_RC_ABR;
    }

    p_sys->h = x265_encoder_open(param);
    if (p_sys->h == NULL) {
        msg_Err(p_enc, "cannot open x265 encoder");
        free(p_sys);
        return VLC_EGENERIC;
    }

Rafaël Carré's avatar
Rafaël Carré committed
204 205
    x265_nal *nal;
    uint32_t i_nal;
206
    if (x265_encoder_headers(p_sys->h, &nal, &i_nal) < 0) {
Rafaël Carré's avatar
Rafaël Carré committed
207 208 209 210 211 212
        msg_Err(p_enc, "cannot get x265 headers");
        Close(VLC_OBJECT(p_enc));
        return VLC_EGENERIC;
    }

    size_t i_extra = 0;
Rafaël Carré's avatar
Rafaël Carré committed
213 214
    for (uint32_t i = 0; i < i_nal; i++)
        i_extra += nal[i].sizeBytes;
Rafaël Carré's avatar
Rafaël Carré committed
215 216 217 218 219 220 221 222 223

    p_enc->fmt_out.i_extra = i_extra;

    uint8_t *p_extra = p_enc->fmt_out.p_extra = malloc(i_extra);
    if (!p_extra) {
        Close(VLC_OBJECT(p_enc));
        return VLC_ENOMEM;
    }

Rafaël Carré's avatar
Rafaël Carré committed
224 225 226
    for (uint32_t i = 0; i < i_nal; i++) {
        memcpy(p_extra, nal[i].payload, nal[i].sizeBytes);
        p_extra += nal[i].sizeBytes;
Rafaël Carré's avatar
Rafaël Carré committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    }

    p_sys->dts = 0;
    p_sys->initial_date = 0;
    p_sys->i_initial_delay = 0;

    p_enc->pf_encode_video = Encode;
    p_enc->pf_encode_audio = NULL;

    return VLC_SUCCESS;
}

static void Close(vlc_object_t *p_this)
{
    encoder_t     *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys = p_enc->p_sys;

Rafaël Carré's avatar
Rafaël Carré committed
244
    x265_encoder_close(p_sys->h);
Rafaël Carré's avatar
Rafaël Carré committed
245 246 247

    free(p_sys);
}