Browse code

avcodec: add libkvazaar HECV encoder

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

Arttu Ylä-Outinen authored on 2015/07/14 14:53:35
Showing 8 changed files
... ...
@@ -15,6 +15,7 @@ version <next>:
15 15
 - adrawgraph audio and drawgraph video filter
16 16
 - removegrain video filter
17 17
 - Intel QSV-accelerated MPEG-2 video and HEVC encoding
18
+- libkvazaar HEVC encoder
18 19
 
19 20
 
20 21
 version 2.7:
... ...
@@ -222,6 +222,7 @@ External library support:
222 222
   --enable-libgsm          enable GSM de/encoding via libgsm [no]
223 223
   --enable-libiec61883     enable iec61883 via libiec61883 [no]
224 224
   --enable-libilbc         enable iLBC de/encoding via libilbc [no]
225
+  --enable-libkvazaar      enable HEVC encoding via libkvazaar [no]
225 226
   --enable-libmfx          enable HW acceleration through libmfx
226 227
   --enable-libmodplug      enable ModPlug via libmodplug [no]
227 228
   --enable-libmp3lame      enable MP3 encoding via libmp3lame [no]
... ...
@@ -1386,6 +1387,7 @@ EXTERNAL_LIBRARY_LIST="
1386 1386
     libgsm
1387 1387
     libiec61883
1388 1388
     libilbc
1389
+    libkvazaar
1389 1390
     libmfx
1390 1391
     libmodplug
1391 1392
     libmp3lame
... ...
@@ -2464,6 +2466,7 @@ libgsm_ms_decoder_deps="libgsm"
2464 2464
 libgsm_ms_encoder_deps="libgsm"
2465 2465
 libilbc_decoder_deps="libilbc"
2466 2466
 libilbc_encoder_deps="libilbc"
2467
+libkvazaar_encoder_deps="libkvazaar"
2467 2468
 libmodplug_demuxer_deps="libmodplug"
2468 2469
 libmp3lame_encoder_deps="libmp3lame"
2469 2470
 libmp3lame_encoder_select="audio_frame_queue"
... ...
@@ -5152,6 +5155,7 @@ enabled libgsm            && { for gsm_hdr in "gsm.h" "gsm/gsm.h"; do
5152 5152
                                    check_lib "${gsm_hdr}" gsm_create -lgsm && break;
5153 5153
                                done || die "ERROR: libgsm not found"; }
5154 5154
 enabled libilbc           && require libilbc ilbc.h WebRtcIlbcfix_InitDecode -lilbc
5155
+enabled libkvazaar        && require2 libkvazaar kvazaar.h kvz_api_get -lkvazaar
5155 5156
 enabled libmfx            && require_pkg_config libmfx "mfx/mfxvideo.h" MFXInit
5156 5157
 enabled libmodplug        && require_pkg_config libmodplug libmodplug/modplug.h ModPlug_Load
5157 5158
 enabled libmp3lame        && require "libmp3lame >= 3.98.3" lame/lame.h lame_set_VBR_quality -lmp3lame
... ...
@@ -2315,6 +2315,30 @@ Setting a higher @option{bits_per_mb} limit will improve the speed.
2315 2315
 For the fastest encoding speed set the @option{qscale} parameter (4 is the
2316 2316
 recommended value) and do not set a size constraint.
2317 2317
 
2318
+@section libkvazaar
2319
+
2320
+Kvazaar H.265/HEVC encoder.
2321
+
2322
+Requires the presence of the libkvazaar headers and library during
2323
+configuration. You need to explicitly configure the build with
2324
+@option{--enable-libkvazaar}.
2325
+
2326
+@subsection Options
2327
+
2328
+@table @option
2329
+
2330
+@item b
2331
+Set target video bitrate in bit/s and enable rate control.
2332
+
2333
+@item threads
2334
+Set number of encoding threads.
2335
+
2336
+@item kvazaar-params
2337
+Set kvazaar parameters as a list of @var{name}=@var{value} pairs separated
2338
+by commas (,). See kvazaar documentation for a list of options.
2339
+
2340
+@end table
2341
+
2318 2342
 @c man end VIDEO ENCODERS
2319 2343
 
2320 2344
 @chapter Subtitles Encoders
... ...
@@ -145,6 +145,14 @@ x265 is under the GNU Public License Version 2 or later
145 145
 details), you must upgrade FFmpeg's license to GPL in order to use it.
146 146
 @end float
147 147
 
148
+@section kvazaar
149
+
150
+FFmpeg can make use of the kvazaar library for HEVC encoding.
151
+
152
+Go to @url{https://github.com/ultravideo/kvazaar} and follow the
153
+instructions for installing the library. Then pass
154
+@code{--enable-libkvazaar} to configure to enable it.
155
+
148 156
 @section libilbc
149 157
 
150 158
 iLBC is a narrowband speech codec that has been made freely available
... ...
@@ -688,7 +696,7 @@ following image formats are supported:
688 688
 @item H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10  @tab  E  @tab  X
689 689
     @tab encoding supported through external library libx264 and OpenH264
690 690
 @item HEVC                   @tab  X  @tab  X
691
-    @tab encoding supported through the external library libx265
691
+    @tab encoding supported through external library libx265 and libkvazaar
692 692
 @item HNM version 4          @tab     @tab  X
693 693
 @item HuffYUV                @tab  X  @tab  X
694 694
 @item HuffYUV FFmpeg variant @tab  X  @tab  X
... ...
@@ -768,6 +768,7 @@ OBJS-$(CONFIG_LIBGSM_MS_DECODER)          += libgsmdec.o
768 768
 OBJS-$(CONFIG_LIBGSM_MS_ENCODER)          += libgsmenc.o
769 769
 OBJS-$(CONFIG_LIBILBC_DECODER)            += libilbc.o
770 770
 OBJS-$(CONFIG_LIBILBC_ENCODER)            += libilbc.o
771
+OBJS-$(CONFIG_LIBKVAZAAR_ENCODER)         += libkvazaar.o
771 772
 OBJS-$(CONFIG_LIBMP3LAME_ENCODER)         += libmp3lame.o mpegaudiodecheader.o
772 773
 OBJS-$(CONFIG_LIBOPENCORE_AMRNB_DECODER)  += libopencore-amr.o
773 774
 OBJS-$(CONFIG_LIBOPENCORE_AMRNB_ENCODER)  += libopencore-amr.o
... ...
@@ -571,6 +571,7 @@ void avcodec_register_all(void)
571 571
     REGISTER_ENCODER(NVENC_H264,        nvenc_h264);
572 572
     REGISTER_ENCODER(NVENC_HEVC,        nvenc_hevc);
573 573
     REGISTER_ENCODER(HEVC_QSV,          hevc_qsv);
574
+    REGISTER_ENCODER(LIBKVAZAAR,        libkvazaar);
574 575
     REGISTER_ENCODER(MPEG2_QSV,         mpeg2_qsv);
575 576
 
576 577
     /* parsers */
577 578
new file mode 100644
... ...
@@ -0,0 +1,248 @@
0
+/*
1
+ * libkvazaar encoder
2
+ *
3
+ * Copyright (c) 2015 Tampere University of Technology
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 <kvazaar.h>
23
+#include <string.h>
24
+
25
+#include "libavutil/avassert.h"
26
+#include "libavutil/dict.h"
27
+#include "libavutil/opt.h"
28
+#include "avcodec.h"
29
+#include "internal.h"
30
+
31
+typedef struct LibkvazaarContext {
32
+    const AVClass *class;
33
+
34
+    const kvz_api *api;
35
+    kvz_encoder *encoder;
36
+    kvz_config *config;
37
+
38
+    char *kvz_params;
39
+} LibkvazaarContext;
40
+
41
+static av_cold int libkvazaar_init(AVCodecContext *avctx)
42
+{
43
+    int retval = 0;
44
+    kvz_config *cfg = NULL;
45
+    kvz_encoder *enc = NULL;
46
+    const kvz_api *const api = kvz_api_get(8);
47
+
48
+    LibkvazaarContext *const ctx = avctx->priv_data;
49
+
50
+    // Kvazaar requires width and height to be multiples of eight.
51
+    if (avctx->width % 8 || avctx->height % 8) {
52
+        av_log(avctx, AV_LOG_ERROR, "Video dimensions are not a multiple of 8.\n");
53
+        retval = AVERROR_INVALIDDATA;
54
+        goto done;
55
+    }
56
+
57
+    cfg = api->config_alloc();
58
+    if (!cfg) {
59
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate kvazaar config structure.\n");
60
+        retval = AVERROR(ENOMEM);
61
+        goto done;
62
+    }
63
+
64
+    if (!api->config_init(cfg)) {
65
+        av_log(avctx, AV_LOG_ERROR, "Could not initialize kvazaar config structure.\n");
66
+        retval = AVERROR_EXTERNAL;
67
+        goto done;
68
+    }
69
+
70
+    cfg->width = avctx->width;
71
+    cfg->height = avctx->height;
72
+    cfg->framerate =
73
+      (double)(avctx->time_base.num * avctx->ticks_per_frame) / avctx->time_base.den;
74
+    cfg->threads = avctx->thread_count;
75
+    cfg->target_bitrate = avctx->bit_rate;
76
+    cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
77
+    cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
78
+
79
+    if (ctx->kvz_params) {
80
+        AVDictionary *dict = NULL;
81
+        if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
82
+            AVDictionaryEntry *entry = NULL;
83
+            while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
84
+                if (!api->config_parse(cfg, entry->key, entry->value)) {
85
+                    av_log(avctx, AV_LOG_WARNING,
86
+                           "Invalid option: %s=%s.\n",
87
+                           entry->key, entry->value);
88
+                }
89
+            }
90
+            av_dict_free(&dict);
91
+        }
92
+    }
93
+
94
+    enc = api->encoder_open(cfg);
95
+    if (!enc) {
96
+        av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
97
+        retval = AVERROR_EXTERNAL;
98
+        goto done;
99
+    }
100
+
101
+    ctx->api = api;
102
+    ctx->encoder = enc;
103
+    ctx->config = cfg;
104
+    enc = NULL;
105
+    cfg = NULL;
106
+
107
+done:
108
+    if (cfg) api->config_destroy(cfg);
109
+    if (enc) api->encoder_close(enc);
110
+
111
+    return retval;
112
+}
113
+
114
+static av_cold int libkvazaar_close(AVCodecContext *avctx)
115
+{
116
+    LibkvazaarContext *ctx = avctx->priv_data;
117
+    if (!ctx->api) return 0;
118
+
119
+    if (ctx->encoder) {
120
+        ctx->api->encoder_close(ctx->encoder);
121
+        ctx->encoder = NULL;
122
+    }
123
+
124
+    if (ctx->config) {
125
+        ctx->api->config_destroy(ctx->config);
126
+        ctx->config = NULL;
127
+    }
128
+
129
+    return 0;
130
+}
131
+
132
+static int libkvazaar_encode(AVCodecContext *avctx,
133
+                             AVPacket *avpkt,
134
+                             const AVFrame *frame,
135
+                             int *got_packet_ptr)
136
+{
137
+    int retval = 0;
138
+    kvz_picture *img_in = NULL;
139
+    kvz_data_chunk *data_out = NULL;
140
+    uint32_t len_out = 0;
141
+    LibkvazaarContext *ctx = avctx->priv_data;
142
+
143
+    *got_packet_ptr = 0;
144
+
145
+    if (frame) {
146
+        int i = 0;
147
+
148
+        av_assert0(frame->width == ctx->config->width);
149
+        av_assert0(frame->height == ctx->config->height);
150
+        av_assert0(frame->format == avctx->pix_fmt);
151
+
152
+        // Allocate input picture for kvazaar.
153
+        img_in = ctx->api->picture_alloc(frame->width, frame->height);
154
+        if (!img_in) {
155
+            av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
156
+            retval = AVERROR(ENOMEM);
157
+            goto done;
158
+        }
159
+
160
+        // Copy pixels from frame to img_in.
161
+        for (i = 0; i < 3; ++i) {
162
+            uint8_t *dst = img_in->data[i];
163
+            uint8_t *src = frame->data[i];
164
+            int width = (i == 0) ? frame->width : (frame->width / 2);
165
+            int height = (i == 0) ? frame->height : (frame->height / 2);
166
+            int y = 0;
167
+            for (y = 0; y < height; ++y) {
168
+                memcpy(dst, src, width);
169
+                src += frame->linesize[i];
170
+                dst += width;
171
+            }
172
+        }
173
+    }
174
+
175
+    if (!ctx->api->encoder_encode(ctx->encoder, img_in, &data_out, &len_out, NULL)) {
176
+        av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
177
+        retval = AVERROR_EXTERNAL;
178
+        goto done;
179
+    }
180
+
181
+    if (data_out) {
182
+        kvz_data_chunk *chunk = NULL;
183
+        uint64_t written = 0;
184
+
185
+        retval = ff_alloc_packet(avpkt, len_out);
186
+        if (retval < 0) {
187
+            av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
188
+            goto done;
189
+        }
190
+
191
+        for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
192
+            av_assert0(written + chunk->len <= len_out);
193
+            memcpy(avpkt->data + written, chunk->data, chunk->len);
194
+            written += chunk->len;
195
+        }
196
+        *got_packet_ptr = 1;
197
+
198
+        ctx->api->chunk_free(data_out);
199
+        data_out = NULL;
200
+    }
201
+
202
+done:
203
+    if (img_in) ctx->api->picture_free(img_in);
204
+    if (data_out) ctx->api->chunk_free(data_out);
205
+    return retval;
206
+}
207
+
208
+static const enum AVPixelFormat pix_fmts[] = {
209
+    AV_PIX_FMT_YUV420P,
210
+    AV_PIX_FMT_NONE
211
+};
212
+
213
+static const AVOption options[] = {
214
+    { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of name=value pairs.",
215
+      offsetof(LibkvazaarContext, kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0,
216
+      AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
217
+    { NULL },
218
+};
219
+
220
+static const AVClass class = {
221
+    .class_name = "libkvazaar",
222
+    .item_name  = av_default_item_name,
223
+    .option     = options,
224
+    .version    = LIBAVUTIL_VERSION_INT,
225
+};
226
+
227
+static const AVCodecDefault defaults[] = {
228
+    { "b", "0" },
229
+    { NULL },
230
+};
231
+
232
+AVCodec ff_libkvazaar_encoder = {
233
+    .name             = "libkvazaar",
234
+    .long_name        = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
235
+    .type             = AVMEDIA_TYPE_VIDEO,
236
+    .id               = AV_CODEC_ID_HEVC,
237
+    .capabilities     = CODEC_CAP_DELAY,
238
+    .pix_fmts         = pix_fmts,
239
+
240
+    .priv_class       = &class,
241
+    .priv_data_size   = sizeof(LibkvazaarContext),
242
+    .defaults         = defaults,
243
+
244
+    .init             = libkvazaar_init,
245
+    .encode2          = libkvazaar_encode,
246
+    .close            = libkvazaar_close,
247
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 56
32
-#define LIBAVCODEC_VERSION_MINOR  47
32
+#define LIBAVCODEC_VERSION_MINOR  48
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \