]> git.sesse.net Git - ffmpeg/blob - libavcodec/libkvazaar.c
tests/fate/aac: Add bitexact flags to fate-aac-pns-encode
[ffmpeg] / libavcodec / libkvazaar.c
1 /*
2  * libkvazaar encoder
3  *
4  * Copyright (c) 2015 Tampere University of Technology
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <kvazaar.h>
24 #include <string.h>
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avcodec.h"
32 #include "internal.h"
33
34 typedef struct LibkvazaarContext {
35     const AVClass *class;
36
37     const kvz_api *api;
38     kvz_encoder *encoder;
39     kvz_config *config;
40
41     char *kvz_params;
42 } LibkvazaarContext;
43
44 static av_cold int libkvazaar_init(AVCodecContext *avctx)
45 {
46     int retval = 0;
47     kvz_config *cfg = NULL;
48     kvz_encoder *enc = NULL;
49     const kvz_api *const api = kvz_api_get(8);
50
51     LibkvazaarContext *const ctx = avctx->priv_data;
52
53     // Kvazaar requires width and height to be multiples of eight.
54     if (avctx->width % 8 || avctx->height % 8) {
55         av_log(avctx, AV_LOG_ERROR, "Video dimensions are not a multiple of 8.\n");
56         retval = AVERROR_INVALIDDATA;
57         goto done;
58     }
59
60     cfg = api->config_alloc();
61     if (!cfg) {
62         av_log(avctx, AV_LOG_ERROR, "Could not allocate kvazaar config structure.\n");
63         retval = AVERROR(ENOMEM);
64         goto done;
65     }
66
67     if (!api->config_init(cfg)) {
68         av_log(avctx, AV_LOG_ERROR, "Could not initialize kvazaar config structure.\n");
69         retval = AVERROR_EXTERNAL;
70         goto done;
71     }
72
73     cfg->width = avctx->width;
74     cfg->height = avctx->height;
75     cfg->framerate =
76       avctx->time_base.den / (double)(avctx->time_base.num * avctx->ticks_per_frame);
77     cfg->target_bitrate = avctx->bit_rate;
78     cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
79     cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
80
81     if (ctx->kvz_params) {
82         AVDictionary *dict = NULL;
83         if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
84             AVDictionaryEntry *entry = NULL;
85             while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
86                 if (!api->config_parse(cfg, entry->key, entry->value)) {
87                     av_log(avctx, AV_LOG_WARNING,
88                            "Invalid option: %s=%s.\n",
89                            entry->key, entry->value);
90                 }
91             }
92             av_dict_free(&dict);
93         }
94     }
95
96     enc = api->encoder_open(cfg);
97     if (!enc) {
98         av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
99         retval = AVERROR_EXTERNAL;
100         goto done;
101     }
102
103     ctx->api = api;
104     ctx->encoder = enc;
105     ctx->config = cfg;
106     enc = NULL;
107     cfg = NULL;
108
109 done:
110     api->config_destroy(cfg);
111     api->encoder_close(enc);
112
113     return retval;
114 }
115
116 static av_cold int libkvazaar_close(AVCodecContext *avctx)
117 {
118     LibkvazaarContext *ctx = avctx->priv_data;
119     if (!ctx->api) return 0;
120
121     if (ctx->encoder) {
122         ctx->api->encoder_close(ctx->encoder);
123         ctx->encoder = NULL;
124     }
125
126     if (ctx->config) {
127         ctx->api->config_destroy(ctx->config);
128         ctx->config = NULL;
129     }
130
131     return 0;
132 }
133
134 static int libkvazaar_encode(AVCodecContext *avctx,
135                              AVPacket *avpkt,
136                              const AVFrame *frame,
137                              int *got_packet_ptr)
138 {
139     int retval = 0;
140     kvz_picture *img_in = NULL;
141
142     kvz_data_chunk *data_out = NULL;
143     uint32_t len_out = 0;
144     kvz_picture *recon_pic = NULL;
145     kvz_frame_info frame_info;
146
147     LibkvazaarContext *ctx = avctx->priv_data;
148
149     *got_packet_ptr = 0;
150
151     if (frame) {
152         if (frame->width != ctx->config->width ||
153                 frame->height != ctx->config->height) {
154             av_log(avctx, AV_LOG_ERROR,
155                    "Changing video dimensions during encoding is not supported. "
156                    "(changed from %dx%d to %dx%d)\n",
157                    ctx->config->width, ctx->config->height,
158                    frame->width, frame->height);
159             retval = AVERROR_INVALIDDATA;
160             goto done;
161         }
162
163         if (frame->format != avctx->pix_fmt) {
164             av_log(avctx, AV_LOG_ERROR,
165                    "Changing pixel format during encoding is not supported. "
166                    "(changed from %s to %s)\n",
167                    av_get_pix_fmt_name(avctx->pix_fmt),
168                    av_get_pix_fmt_name(frame->format));
169             retval = AVERROR_INVALIDDATA;
170             goto done;
171         }
172
173         // Allocate input picture for kvazaar.
174         img_in = ctx->api->picture_alloc(frame->width, frame->height);
175         if (!img_in) {
176             av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
177             retval = AVERROR(ENOMEM);
178             goto done;
179         }
180
181         // Copy pixels from frame to img_in.
182         {
183             int dst_linesizes[4] = {
184               frame->width,
185               frame->width / 2,
186               frame->width / 2,
187               0
188             };
189             av_image_copy(img_in->data, dst_linesizes,
190                           frame->data, frame->linesize,
191                           frame->format, frame->width, frame->height);
192         }
193
194         img_in->pts = frame->pts;
195     }
196
197     if (!ctx->api->encoder_encode(ctx->encoder, img_in,
198                                   &data_out, &len_out,
199                                   &recon_pic, NULL,
200                                   &frame_info)) {
201         av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
202         retval = AVERROR_EXTERNAL;
203         goto done;
204     }
205
206     if (data_out) {
207         kvz_data_chunk *chunk = NULL;
208         uint64_t written = 0;
209
210         retval = ff_alloc_packet(avpkt, len_out);
211         if (retval < 0) {
212             av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
213             goto done;
214         }
215
216         for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
217             av_assert0(written + chunk->len <= len_out);
218             memcpy(avpkt->data + written, chunk->data, chunk->len);
219             written += chunk->len;
220         }
221         *got_packet_ptr = 1;
222
223         ctx->api->chunk_free(data_out);
224         data_out = NULL;
225
226         avpkt->pts = recon_pic->pts;
227         avpkt->dts = recon_pic->dts;
228
229         avpkt->flags = 0;
230         // IRAP VCL NAL unit types span the range
231         // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
232         if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
233                 frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
234             avpkt->flags |= AV_PKT_FLAG_KEY;
235         }
236     }
237
238 done:
239     ctx->api->picture_free(img_in);
240     ctx->api->picture_free(recon_pic);
241     ctx->api->chunk_free(data_out);
242     return retval;
243 }
244
245 static const enum AVPixelFormat pix_fmts[] = {
246     AV_PIX_FMT_YUV420P,
247     AV_PIX_FMT_NONE
248 };
249
250 static const AVOption options[] = {
251     { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of name=value pairs.",
252       offsetof(LibkvazaarContext, kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0,
253       AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
254     { NULL },
255 };
256
257 static const AVClass class = {
258     .class_name = "libkvazaar",
259     .item_name  = av_default_item_name,
260     .option     = options,
261     .version    = LIBAVUTIL_VERSION_INT,
262 };
263
264 static const AVCodecDefault defaults[] = {
265     { "b", "0" },
266     { NULL },
267 };
268
269 AVCodec ff_libkvazaar_encoder = {
270     .name             = "libkvazaar",
271     .long_name        = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
272     .type             = AVMEDIA_TYPE_VIDEO,
273     .id               = AV_CODEC_ID_HEVC,
274     .capabilities     = AV_CODEC_CAP_DELAY,
275     .pix_fmts         = pix_fmts,
276
277     .priv_class       = &class,
278     .priv_data_size   = sizeof(LibkvazaarContext),
279     .defaults         = defaults,
280
281     .init             = libkvazaar_init,
282     .encode2          = libkvazaar_encode,
283     .close            = libkvazaar_close,
284 };