Browse code

Add libx265 encoder

Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>

Derek Buitenhuis authored on 2013/10/02 23:46:26
Showing 8 changed files
... ...
@@ -26,6 +26,7 @@ version <next>
26 26
   MP3, AIFF, and OMA files), FLAC header, and the AVI "junk" block.
27 27
 - Mirillis FIC video decoder
28 28
 - Support DNx444
29
+- libx265 encoder
29 30
 
30 31
 
31 32
 version 2.1:
... ...
@@ -79,6 +79,7 @@ The following libraries are under GPL:
79 79
     - libutvideo
80 80
     - libvidstab
81 81
     - libx264
82
+    - libx265
82 83
     - libxavs
83 84
     - libxvid
84 85
 When combining them with FFmpeg, FFmpeg needs to be licensed as GPL as well by
... ...
@@ -244,6 +244,7 @@ External library support:
244 244
   --enable-libwavpack      enable wavpack encoding via libwavpack [no]
245 245
   --enable-libwebp         enable WebP encoding via libwebp [no]
246 246
   --enable-libx264         enable H.264 encoding via x264 [no]
247
+  --enable-libx265         enable HEVC encoding via x265 [no]
247 248
   --enable-libxavs         enable AVS encoding via xavs [no]
248 249
   --enable-libxvid         enable Xvid encoding via xvidcore,
249 250
                            native MPEG-4/Xvid encoder exists [no]
... ...
@@ -1310,6 +1311,7 @@ EXTERNAL_LIBRARY_LIST="
1310 1310
     libwavpack
1311 1311
     libwebp
1312 1312
     libx264
1313
+    libx265
1313 1314
     libxavs
1314 1315
     libxvid
1315 1316
     libzmq
... ...
@@ -2189,6 +2191,7 @@ libwavpack_encoder_deps="libwavpack"
2189 2189
 libwebp_encoder_deps="libwebp"
2190 2190
 libx264_encoder_deps="libx264"
2191 2191
 libx264rgb_encoder_deps="libx264"
2192
+libx265_encoder_deps="libx265"
2192 2193
 libxavs_encoder_deps="libxavs"
2193 2194
 libxvid_encoder_deps="libxvid"
2194 2195
 libutvideo_decoder_deps="libutvideo"
... ...
@@ -4011,6 +4014,7 @@ die_license_disabled gpl libcdio
4011 4011
 die_license_disabled gpl libutvideo
4012 4012
 die_license_disabled gpl libvidstab
4013 4013
 die_license_disabled gpl libx264
4014
+die_license_disabled gpl libx265
4014 4015
 die_license_disabled gpl libxavs
4015 4016
 die_license_disabled gpl libxvid
4016 4017
 die_license_disabled gpl libzvbi
... ...
@@ -4500,6 +4504,9 @@ enabled libwebp           && require_pkg_config libwebp webp/encode.h WebPGetEnc
4500 4500
 enabled libx264           && require libx264 x264.h x264_encoder_encode -lx264 &&
4501 4501
                              { check_cpp_condition x264.h "X264_BUILD >= 118" ||
4502 4502
                                die "ERROR: libx264 must be installed and version must be >= 0.118."; }
4503
+enabled libx265           && require_pkg_config x265 x265.h x265_encoder_encode &&
4504
+                             { check_cpp_condition x265.h "X265_BUILD >= 5" ||
4505
+                               die "ERROR: libx265 version must be >= 5."; }
4503 4506
 enabled libxavs           && require libxavs xavs.h xavs_encoder_encode -lxavs
4504 4507
 enabled libxvid           && require libxvid xvid.h xvid_global -lxvidcore
4505 4508
 enabled libzmq            && require_pkg_config libzmq zmq.h zmq_ctx_new
... ...
@@ -122,6 +122,20 @@ x264 is under the GNU Public License Version 2 or later
122 122
 details), you must upgrade FFmpeg's license to GPL in order to use it.
123 123
 @end float
124 124
 
125
+@section x265
126
+
127
+FFmpeg can make use of the x265 library for HEVC encoding.
128
+
129
+Go to @url{http://x265.org/developers.html} and follow the instructions
130
+for installing the library. Then pass @code{--enable-libx265} to configure
131
+to enable it.
132
+
133
+@float note
134
+x265 is under the GNU Public License Version 2 or later
135
+(see @url{http://www.gnu.org/licenses/old-licenses/gpl-2.0.html} for
136
+details), you must upgrade FFmpeg's license to GPL in order to use it.
137
+@end float
138
+
125 139
 @section libilbc
126 140
 
127 141
 iLBC is a narrowband speech codec that has been made freely available
... ...
@@ -342,7 +356,7 @@ library:
342 342
 @item raw H.261                 @tab X @tab X
343 343
 @item raw H.263                 @tab X @tab X
344 344
 @item raw H.264                 @tab X @tab X
345
-@item raw HEVC                  @tab   @tab X
345
+@item raw HEVC                  @tab X @tab X
346 346
 @item raw Ingenient MJPEG       @tab   @tab X
347 347
 @item raw MJPEG                 @tab X @tab X
348 348
 @item raw MLP                   @tab   @tab X
... ...
@@ -612,7 +626,8 @@ following image formats are supported:
612 612
 @item H.263+ / H.263-1998 / H.263 version 2  @tab  X  @tab  X
613 613
 @item H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10  @tab  E  @tab  X
614 614
     @tab encoding supported through external library libx264
615
-@item HEVC                   @tab     @tab  X
615
+@item HEVC                   @tab  X  @tab  X
616
+    @tab encoding supported through the external library libx265
616 617
 @item HNM version 4          @tab     @tab  X
617 618
 @item HuffYUV                @tab  X  @tab  X
618 619
 @item HuffYUV FFmpeg variant @tab  X  @tab  X
... ...
@@ -742,6 +742,7 @@ OBJS-$(CONFIG_LIBVPX_VP9_ENCODER)         += libvpxenc.o libvpx.o
742 742
 OBJS-$(CONFIG_LIBWAVPACK_ENCODER)         += libwavpackenc.o
743 743
 OBJS-$(CONFIG_LIBWEBP_ENCODER)            += libwebpenc.o
744 744
 OBJS-$(CONFIG_LIBX264_ENCODER)            += libx264.o
745
+OBJS-$(CONFIG_LIBX265_ENCODER)            += libx265.o
745 746
 OBJS-$(CONFIG_LIBXAVS_ENCODER)            += libxavs.o
746 747
 OBJS-$(CONFIG_LIBXVID_ENCODER)            += libxvid.o
747 748
 OBJS-$(CONFIG_LIBZVBI_TELETEXT_DECODER)   += libzvbi-teletextdec.o
... ...
@@ -516,6 +516,7 @@ void avcodec_register_all(void)
516 516
     REGISTER_ENCODER(LIBWEBP,           libwebp);
517 517
     REGISTER_ENCODER(LIBX264,           libx264);
518 518
     REGISTER_ENCODER(LIBX264RGB,        libx264rgb);
519
+    REGISTER_ENCODER(LIBX265,           libx265);
519 520
     REGISTER_ENCODER(LIBXAVS,           libxavs);
520 521
     REGISTER_ENCODER(LIBXVID,           libxvid);
521 522
     REGISTER_DECODER(LIBZVBI_TELETEXT,  libzvbi_teletext);
522 523
new file mode 100644
... ...
@@ -0,0 +1,284 @@
0
+/*
1
+ * libx265 encoder
2
+ *
3
+ * Copyright (c) 2013-2014 Derek Buitenhuis
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include <x265.h>
23
+
24
+#include "libavutil/internal.h"
25
+#include "libavutil/common.h"
26
+#include "libavutil/opt.h"
27
+#include "libavutil/pixdesc.h"
28
+#include "avcodec.h"
29
+#include "internal.h"
30
+
31
+typedef struct libx265Context {
32
+    const AVClass *class;
33
+
34
+    x265_encoder *encoder;
35
+    x265_param   *params;
36
+    uint8_t      *header;
37
+    int           header_size;
38
+
39
+    char *preset;
40
+    char *tune;
41
+    char *x265_opts;
42
+} libx265Context;
43
+
44
+static int is_keyframe(NalUnitType naltype)
45
+{
46
+    switch (naltype) {
47
+    case NAL_UNIT_CODED_SLICE_BLA_W_LP:
48
+    case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
49
+    case NAL_UNIT_CODED_SLICE_BLA_N_LP:
50
+    case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
51
+    case NAL_UNIT_CODED_SLICE_IDR_N_LP:
52
+    case NAL_UNIT_CODED_SLICE_CRA:
53
+        return 1;
54
+    default:
55
+        return 0;
56
+    }
57
+}
58
+
59
+static av_cold int libx265_encode_close(AVCodecContext *avctx)
60
+{
61
+    libx265Context *ctx = avctx->priv_data;
62
+
63
+    av_frame_free(&avctx->coded_frame);
64
+    av_freep(&ctx->header);
65
+
66
+    x265_param_free(ctx->params);
67
+
68
+    if (ctx->encoder)
69
+        x265_encoder_close(ctx->encoder);
70
+
71
+    return 0;
72
+}
73
+
74
+static av_cold int libx265_encode_init(AVCodecContext *avctx)
75
+{
76
+    libx265Context *ctx = avctx->priv_data;
77
+    x265_nal *nal;
78
+    uint8_t *buf;
79
+    int nnal;
80
+    int ret;
81
+    int i;
82
+
83
+    avctx->coded_frame = av_frame_alloc();
84
+    if (!avctx->coded_frame) {
85
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
86
+        return AVERROR(ENOMEM);
87
+    }
88
+
89
+    ctx->params = x265_param_alloc();
90
+    if (!ctx->params) {
91
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
92
+        return AVERROR(ENOMEM);
93
+    }
94
+
95
+    x265_param_default(ctx->params);
96
+    if (x265_param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
97
+        av_log(avctx, AV_LOG_ERROR, "Invalid preset or tune.\n");
98
+        return AVERROR(EINVAL);
99
+    }
100
+
101
+    ctx->params->frameNumThreads = avctx->thread_count;
102
+    ctx->params->frameRate       = (int) (avctx->time_base.den / avctx->time_base.num);
103
+    ctx->params->sourceWidth     = avctx->width;
104
+    ctx->params->sourceHeight    = avctx->height;
105
+    ctx->params->inputBitDepth   = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1;
106
+
107
+    if (avctx->bit_rate > 0) {
108
+        ctx->params->rc.bitrate         = avctx->bit_rate / 1000;
109
+        ctx->params->rc.rateControlMode = X265_RC_ABR;
110
+    }
111
+
112
+    if (ctx->x265_opts) {
113
+        AVDictionary *dict    = NULL;
114
+        AVDictionaryEntry *en = NULL;
115
+
116
+        if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
117
+            while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
118
+                int parse_ret = x265_param_parse(ctx->params, en->key, en->value);
119
+
120
+                switch (parse_ret) {
121
+                case X265_PARAM_BAD_NAME:
122
+                    av_log(avctx, AV_LOG_WARNING,
123
+                          "Unknown option: %s.\n", en->key);
124
+                    break;
125
+                case X265_PARAM_BAD_VALUE:
126
+                    av_log(avctx, AV_LOG_WARNING,
127
+                          "Invalid value for %s: %s.\n", en->key, en->value);
128
+                    break;
129
+                default:
130
+                    break;
131
+                }
132
+            }
133
+            av_dict_free(&dict);
134
+        }
135
+    }
136
+
137
+    ctx->encoder = x265_encoder_open(ctx->params);
138
+    if (!ctx->encoder) {
139
+        av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
140
+        libx265_encode_close(avctx);
141
+        return AVERROR_EXTERNAL;
142
+    }
143
+
144
+    ret = x265_encoder_headers(ctx->encoder, &nal, &nnal);
145
+    if (ret < 0) {
146
+        av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
147
+        libx265_encode_close(avctx);
148
+        return AVERROR_INVALIDDATA;
149
+    }
150
+
151
+    for (i = 0; i < nnal; i++)
152
+        ctx->header_size += nal[i].sizeBytes;
153
+
154
+    ctx->header = av_malloc(ctx->header_size);
155
+    if (!ctx->header) {
156
+        av_log(avctx, AV_LOG_ERROR,
157
+               "Cannot allocate HEVC header of size %d.\n", ctx->header_size);
158
+        libx265_encode_close(avctx);
159
+        return AVERROR(ENOMEM);
160
+    }
161
+
162
+    buf = ctx->header;
163
+    for (i = 0; i < nnal; i++) {
164
+        memcpy(buf, nal[i].payload, nal[i].sizeBytes);
165
+        buf += nal[i].sizeBytes;
166
+    }
167
+
168
+    return 0;
169
+}
170
+
171
+static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
172
+                                const AVFrame *pic, int *got_packet)
173
+{
174
+    libx265Context *ctx = avctx->priv_data;
175
+    x265_picture x265pic;
176
+    x265_picture x265pic_out = { { 0 } };
177
+    x265_nal *nal;
178
+    uint8_t *dst;
179
+    int payload = 0;
180
+    int nnal;
181
+    int ret;
182
+    int i;
183
+
184
+    if (pic) {
185
+        for (i = 0; i < 3; i++) {
186
+           x265pic.planes[i] = pic->data[i];
187
+           x265pic.stride[i] = pic->linesize[i];
188
+        }
189
+
190
+        x265pic.pts = pic->pts;
191
+    }
192
+
193
+    ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
194
+                              pic ? &x265pic : NULL, &x265pic_out);
195
+    if (ret < 0)
196
+        return AVERROR_UNKNOWN;
197
+
198
+    if (!nnal)
199
+        return 0;
200
+
201
+    for (i = 0; i < nnal; i++)
202
+        payload += nal[i].sizeBytes;
203
+
204
+    payload += ctx->header_size;
205
+
206
+    ret = ff_alloc_packet(pkt, payload);
207
+    if (ret < 0) {
208
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
209
+        return ret;
210
+    }
211
+    dst = pkt->data;
212
+
213
+    if (ctx->header) {
214
+        memcpy(dst, ctx->header, ctx->header_size);
215
+        dst += ctx->header_size;
216
+
217
+        av_freep(&ctx->header);
218
+        ctx->header_size = 0;
219
+    }
220
+
221
+    for (i = 0; i < nnal; i++) {
222
+        memcpy(dst, nal[i].payload, nal[i].sizeBytes);
223
+        dst += nal[i].sizeBytes;
224
+
225
+        if (is_keyframe(nal[i].type))
226
+            pkt->flags |= AV_PKT_FLAG_KEY;
227
+    }
228
+
229
+    pkt->pts = x265pic_out.pts;
230
+    pkt->dts = x265pic_out.dts;
231
+
232
+    *got_packet = 1;
233
+    return 0;
234
+}
235
+
236
+static const enum AVPixelFormat x265_csp_eight[] = {
237
+    AV_PIX_FMT_YUV420P,
238
+    AV_PIX_FMT_NONE
239
+};
240
+
241
+static const enum AVPixelFormat x265_csp_twelve[] = {
242
+    AV_PIX_FMT_YUV420P,
243
+    AV_PIX_FMT_YUV420P10,
244
+    AV_PIX_FMT_NONE
245
+};
246
+
247
+static av_cold void libx265_encode_init_csp(AVCodec *codec)
248
+{
249
+    if (x265_max_bit_depth == 8)
250
+        codec->pix_fmts = x265_csp_eight;
251
+    else if (x265_max_bit_depth == 12)
252
+        codec->pix_fmts = x265_csp_twelve;
253
+}
254
+
255
+#define OFFSET(x) offsetof(libx265Context, x)
256
+#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
257
+static const AVOption options[] = {
258
+    { "preset",      "set the x265 preset",                                                         OFFSET(preset),    AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
259
+    { "tune",        "set the x265 tune parameter",                                                 OFFSET(tune),      AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
260
+    { "x265-params", "set the x265 configuration using a :-separated list of key=value parameters", OFFSET(x265_opts), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
261
+    { NULL }
262
+};
263
+
264
+static const AVClass class = {
265
+    .class_name = "libx265",
266
+    .item_name  = av_default_item_name,
267
+    .option     = options,
268
+    .version    = LIBAVUTIL_VERSION_INT,
269
+};
270
+
271
+AVCodec ff_libx265_encoder = {
272
+    .name             = "libx265",
273
+    .long_name        = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
274
+    .type             = AVMEDIA_TYPE_VIDEO,
275
+    .id               = AV_CODEC_ID_HEVC,
276
+    .init             = libx265_encode_init,
277
+    .init_static_data = libx265_encode_init_csp,
278
+    .encode2          = libx265_encode_frame,
279
+    .close            = libx265_encode_close,
280
+    .priv_data_size   = sizeof(libx265Context),
281
+    .priv_class       = &class,
282
+    .capabilities     = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
283
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 55
32
-#define LIBAVCODEC_VERSION_MINOR  51
32
+#define LIBAVCODEC_VERSION_MINOR  52
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \