Signed-off-by: Derek Buitenhuis <[email protected]>
---
 configure              |    5 +
 libavcodec/Makefile    |    1 +
 libavcodec/allcodecs.c |    1 +
 libavcodec/libx265.c   |  195 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 202 insertions(+), 0 deletions(-)
 create mode 100644 libavcodec/libx265.c

diff --git a/configure b/configure
index 258dd73..fdfb9fb 100755
--- a/configure
+++ b/configure
@@ -203,6 +203,7 @@ External library support:
   --enable-libvpx          enable VP8 and VP9 de/encoding via libvpx [no]
   --enable-libwavpack      enable wavpack encoding via libwavpack [no]
   --enable-libx264         enable H.264 encoding via x264 [no]
+  --enable-libx265         enable HEVC encoding via x265 [no]
   --enable-libxavs         enable AVS encoding via xavs [no]
   --enable-libxvid         enable Xvid encoding via xvidcore,
                            native MPEG-4/Xvid encoder exists [no]
@@ -1064,6 +1065,7 @@ EXTERNAL_LIBRARY_LIST="
     libvpx
     libwavpack
     libx264
+    libx265
     libxavs
     libxvid
     openssl
@@ -1799,6 +1801,7 @@ libvpx_vp9_decoder_deps="libvpx"
 libvpx_vp9_encoder_deps="libvpx"
 libwavpack_encoder_deps="libwavpack"
 libx264_encoder_deps="libx264"
+libx265_encoder_deps="libx265"
 libxavs_encoder_deps="libxavs"
 libxvid_encoder_deps="libxvid"
 
@@ -3391,6 +3394,7 @@ die_license_disabled() {
 
 die_license_disabled gpl libcdio
 die_license_disabled gpl libx264
+die_license_disabled gpl libx265
 die_license_disabled gpl libxavs
 die_license_disabled gpl libxvid
 die_license_disabled gpl x11grab
@@ -3808,6 +3812,7 @@ enabled libwavpack        && require libwavpack 
wavpack/wavpack.h WavpackOpenFil
 enabled libx264           && require libx264 x264.h x264_encoder_encode -lx264 
&&
                              { check_cpp_condition x264.h "X264_BUILD >= 118" 
||
                                die "ERROR: libx264 version must be >= 0.118."; 
}
+enabled libx265           && require libx265 x265.h x265_encoder_encode -lx265 
-lstdc++
 enabled libxavs           && require libxavs xavs.h xavs_encoder_encode -lxavs
 enabled libxvid           && require libxvid xvid.h xvid_global -lxvidcore
 enabled openssl           && { check_lib openssl/ssl.h SSL_library_init -lssl 
-lcrypto ||
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 04eb2a5..f6478b5 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -613,6 +613,7 @@ OBJS-$(CONFIG_LIBVPX_VP9_DECODER)         += libvpxdec.o
 OBJS-$(CONFIG_LIBVPX_VP9_ENCODER)         += libvpxenc.o
 OBJS-$(CONFIG_LIBWAVPACK_ENCODER)         += libwavpackenc.o
 OBJS-$(CONFIG_LIBX264_ENCODER)            += libx264.o
+OBJS-$(CONFIG_LIBX265_ENCODER)            += libx265.o
 OBJS-$(CONFIG_LIBXAVS_ENCODER)            += libxavs.o
 OBJS-$(CONFIG_LIBXVID_ENCODER)            += libxvid.o
 
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 24b459e..a3729cc 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -433,6 +433,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC (LIBVPX_VP9,        libvpx_vp9);
     REGISTER_ENCODER(LIBWAVPACK,        libwavpack);
     REGISTER_ENCODER(LIBX264,           libx264);
+    REGISTER_ENCODER(LIBX265,           libx265);
     REGISTER_ENCODER(LIBXAVS,           libxavs);
     REGISTER_ENCODER(LIBXVID,           libxvid);
 
diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c
new file mode 100644
index 0000000..58d4a9a
--- /dev/null
+++ b/libavcodec/libx265.c
@@ -0,0 +1,195 @@
+/*
+ * libx265 encoder
+ *
+ * Copyright (c) 2013 Derek Buitenhuis
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/internal.h"
+#include "avcodec.h"
+#include "internal.h"
+
+#include <x265.h>
+
+typedef struct libx265Context {
+    x265_t       *encoder;
+    x265_param_t  params;
+    uint8_t      *header;
+    int           header_size;
+} libx265Context;
+
+static av_cold int libx265_encode_close(AVCodecContext *avctx)
+{
+    libx265Context *ctx = avctx->priv_data;
+
+    av_freep(&avctx->coded_frame);
+    av_freep(&ctx->header);
+
+    if (ctx->encoder)
+        x265_encoder_close(ctx->encoder, NULL);
+
+    return 0;
+}
+
+static av_cold int libx265_encode_init(AVCodecContext *avctx)
+{
+    libx265Context *ctx = avctx->priv_data;
+    x265_nal_t *nal;
+    uint8_t *buf;
+    int nnal;
+    int ret;
+    int i;
+
+    avctx->coded_frame = avcodec_alloc_frame();
+    if (!avctx->coded_frame) {
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
+        return AVERROR(ENOMEM);
+    }
+
+    x265_param_default(&ctx->params);
+
+    ctx->params.frameNumThreads = avctx->thread_count;
+    ctx->params.maxCUSize       = 16; /* Only require mod16 width. */
+    ctx->params.frameRate       = (int) (avctx->time_base.den / 
avctx->time_base.num); /* No way this is right... */
+    ctx->params.sourceWidth     = avctx->width; /* TODO: Fix mid-stream res 
changes? */
+    ctx->params.sourceHeight    = avctx->height;
+
+    if (avctx->width % ctx->params.maxCUSize) {
+        av_log(avctx, AV_LOG_ERROR,
+               "libx265 requires a width that is a multiple of %d.\n",
+               ctx->params.maxCUSize);
+        libx265_encode_close(avctx);
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (avctx->height % 8) {
+        av_log(avctx, AV_LOG_ERROR,
+               "libx265 requires a height that is a multiple of 8.\n");
+        libx265_encode_close(avctx);
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (avctx->bit_rate > 0) {
+        ctx->params.rc.bitrate         = avctx->bit_rate / 1000;
+        ctx->params.rc.rateControlMode = X265_RC_ABR;
+    }
+
+    ctx->encoder = x265_encoder_open(&ctx->params);
+    if (!ctx->encoder) {
+        av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
+        libx265_encode_close(avctx);
+        return AVERROR_INVALIDDATA;
+    }
+
+    ret = x265_encoder_headers(ctx->encoder, &nal, &nnal);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
+        libx265_encode_close(avctx);
+        return AVERROR_INVALIDDATA;
+    }
+
+    for (i = 0; i < nnal; i++)
+        ctx->header_size += nal[i].i_payload;
+
+    ctx->header = av_malloc(ctx->header_size);
+    if (!ctx->header) {
+        av_log(avctx, AV_LOG_ERROR, "Cannot allocate HEVC header.\n");
+        libx265_encode_close(avctx);
+        return AVERROR(ENOMEM);
+    }
+
+    buf = ctx->header;
+    for (i = 0; i < nnal; i++) {
+        memcpy(buf, nal[i].p_payload, nal[i].i_payload);
+        buf += nal[i].i_payload;
+    }
+
+    return 0;
+}
+
+static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
+                                const AVFrame *pic, int *got_packet)
+{
+    libx265Context *ctx = avctx->priv_data;
+    x265_picture_t x265pic;
+    x265_picture_t x265pic_out;
+    x265_nal_t *nal;
+    uint8_t *dst;
+    int payload = 0;
+    int nnal;
+    int ret;
+    int i;
+
+    memset(&x265pic_out, 0, sizeof(x265pic_out));
+
+    if (pic) {
+        for (i = 0; i < 3; i++) {
+           x265pic.planes[i] = pic->data[i];
+           x265pic.stride[i] = pic->linesize[i];
+        }
+    }
+
+    ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
+                              pic ? &x265pic : NULL, &x265pic_out);
+    if (ret < 0)
+        return AVERROR_UNKNOWN;
+
+    if (!nnal)
+        return 0;
+
+    for (i = 0; i < nnal; i++)
+        payload += nal[i].i_payload;
+
+    payload += ctx->header_size;
+
+    ret = ff_alloc_packet(pkt, payload);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
+        return ret;
+    }
+    dst = pkt->data;
+
+    if (ctx->header) {
+        memcpy(dst, ctx->header, ctx->header_size);
+        dst += ctx->header_size;
+
+        av_freep(&ctx->header);
+        ctx->header_size = 0;
+    }
+
+    for (i = 0; i < nnal; i++) {
+        memcpy(dst, nal[i].p_payload, nal[i].i_payload);
+        dst += nal[i].i_payload;
+    }
+
+    *got_packet = 1;
+    return 0;
+}
+
+AVCodec ff_libx265_encoder = {
+    .name           = "libx265",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_HEVC,
+    .init           = libx265_encode_init,
+    .encode2        = libx265_encode_frame,
+    .close          = libx265_encode_close,
+    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, 
AV_PIX_FMT_NONE },
+    .priv_data_size = sizeof(libx265Context),
+    .long_name      = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
+};
-- 
1.7.1

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to