OSDN Git Service

avcodec/vp9block: fix runtime error: signed integer overflow: 196675 * 20670 cannot...
[android-x86/external-ffmpeg.git] / 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 <stdint.h>
25 #include <string.h>
26
27 #include "libavutil/attributes.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/error.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/log.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/opt.h"
37
38 #include "avcodec.h"
39 #include "internal.h"
40
41 typedef struct LibkvazaarContext {
42     const AVClass *class;
43
44     const kvz_api *api;
45     kvz_encoder *encoder;
46     kvz_config *config;
47
48     char *kvz_params;
49 } LibkvazaarContext;
50
51 static av_cold int libkvazaar_init(AVCodecContext *avctx)
52 {
53     LibkvazaarContext *const ctx = avctx->priv_data;
54     const kvz_api *const api = ctx->api = kvz_api_get(8);
55     kvz_config *cfg = NULL;
56     kvz_encoder *enc = NULL;
57
58     /* Kvazaar requires width and height to be multiples of eight. */
59     if (avctx->width % 8 || avctx->height % 8) {
60         av_log(avctx, AV_LOG_ERROR,
61                "Video dimensions are not a multiple of 8 (%dx%d).\n",
62                avctx->width, avctx->height);
63         return AVERROR(ENOSYS);
64     }
65
66     ctx->config = cfg = api->config_alloc();
67     if (!cfg) {
68         av_log(avctx, AV_LOG_ERROR,
69                "Could not allocate kvazaar config structure.\n");
70         return AVERROR(ENOMEM);
71     }
72
73     if (!api->config_init(cfg)) {
74         av_log(avctx, AV_LOG_ERROR,
75                "Could not initialize kvazaar config structure.\n");
76         return AVERROR_BUG;
77     }
78
79     cfg->width  = avctx->width;
80     cfg->height = avctx->height;
81
82     if (avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
83         av_log(avctx, AV_LOG_ERROR,
84                "Could not set framerate for kvazaar: integer overflow\n");
85         return AVERROR(EINVAL);
86     }
87     cfg->framerate_num   = avctx->time_base.den;
88     cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
89     cfg->target_bitrate = avctx->bit_rate;
90     cfg->vui.sar_width  = avctx->sample_aspect_ratio.num;
91     cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
92
93     if (ctx->kvz_params) {
94         AVDictionary *dict = NULL;
95         if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
96             AVDictionaryEntry *entry = NULL;
97             while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
98                 if (!api->config_parse(cfg, entry->key, entry->value)) {
99                     av_log(avctx, AV_LOG_WARNING, "Invalid option: %s=%s.\n",
100                            entry->key, entry->value);
101                 }
102             }
103             av_dict_free(&dict);
104         }
105     }
106
107     ctx->encoder = enc = api->encoder_open(cfg);
108     if (!enc) {
109         av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
110         return AVERROR_BUG;
111     }
112
113     if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
114         kvz_data_chunk *data_out = NULL;
115         kvz_data_chunk *chunk = NULL;
116         uint32_t len_out;
117         uint8_t *p;
118
119         if (!api->encoder_headers(enc, &data_out, &len_out))
120             return AVERROR(ENOMEM);
121
122         avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
123         if (!p) {
124             ctx->api->chunk_free(data_out);
125             return AVERROR(ENOMEM);
126         }
127
128         avctx->extradata_size = len_out;
129
130         for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
131             memcpy(p, chunk->data, chunk->len);
132             p += chunk->len;
133         }
134
135         ctx->api->chunk_free(data_out);
136     }
137
138     return 0;
139 }
140
141 static av_cold int libkvazaar_close(AVCodecContext *avctx)
142 {
143     LibkvazaarContext *ctx = avctx->priv_data;
144
145     if (ctx->api) {
146       ctx->api->encoder_close(ctx->encoder);
147       ctx->api->config_destroy(ctx->config);
148     }
149
150     if (avctx->extradata)
151         av_freep(&avctx->extradata);
152
153     return 0;
154 }
155
156 static int libkvazaar_encode(AVCodecContext *avctx,
157                              AVPacket *avpkt,
158                              const AVFrame *frame,
159                              int *got_packet_ptr)
160 {
161     LibkvazaarContext *ctx = avctx->priv_data;
162     kvz_picture *input_pic = NULL;
163     kvz_picture *recon_pic = NULL;
164     kvz_frame_info frame_info;
165     kvz_data_chunk *data_out = NULL;
166     uint32_t len_out = 0;
167     int retval = 0;
168
169     *got_packet_ptr = 0;
170
171     if (frame) {
172         if (frame->width != ctx->config->width ||
173                 frame->height != ctx->config->height) {
174             av_log(avctx, AV_LOG_ERROR,
175                    "Changing video dimensions during encoding is not supported. "
176                    "(changed from %dx%d to %dx%d)\n",
177                    ctx->config->width, ctx->config->height,
178                    frame->width, frame->height);
179             retval = AVERROR_INVALIDDATA;
180             goto done;
181         }
182
183         if (frame->format != avctx->pix_fmt) {
184             av_log(avctx, AV_LOG_ERROR,
185                    "Changing pixel format during encoding is not supported. "
186                    "(changed from %s to %s)\n",
187                    av_get_pix_fmt_name(avctx->pix_fmt),
188                    av_get_pix_fmt_name(frame->format));
189             retval = AVERROR_INVALIDDATA;
190             goto done;
191         }
192
193         // Allocate input picture for kvazaar.
194         input_pic = ctx->api->picture_alloc(frame->width, frame->height);
195         if (!input_pic) {
196             av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
197             retval = AVERROR(ENOMEM);
198             goto done;
199         }
200
201         // Copy pixels from frame to input_pic.
202         {
203             int dst_linesizes[4] = {
204               frame->width,
205               frame->width / 2,
206               frame->width / 2,
207               0
208             };
209             av_image_copy(input_pic->data, dst_linesizes,
210                           frame->data, frame->linesize,
211                           frame->format, frame->width, frame->height);
212         }
213
214         input_pic->pts = frame->pts;
215     }
216
217     retval = ctx->api->encoder_encode(ctx->encoder,
218                                       input_pic,
219                                       &data_out, &len_out,
220                                       &recon_pic, NULL,
221                                       &frame_info);
222     if (!retval) {
223         av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
224         retval = AVERROR_INVALIDDATA;
225         goto done;
226     }
227     else
228         retval = 0; /* kvazaar returns 1 on success */
229
230     if (data_out) {
231         kvz_data_chunk *chunk = NULL;
232         uint64_t written = 0;
233
234         retval = ff_alloc_packet(avpkt, len_out);
235         if (retval < 0) {
236             av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
237             goto done;
238         }
239
240         for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
241             av_assert0(written + chunk->len <= len_out);
242             memcpy(avpkt->data + written, chunk->data, chunk->len);
243             written += chunk->len;
244         }
245
246         avpkt->pts = recon_pic->pts;
247         avpkt->dts = recon_pic->dts;
248         avpkt->flags = 0;
249         // IRAP VCL NAL unit types span the range
250         // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
251         if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
252                 frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
253             avpkt->flags |= AV_PKT_FLAG_KEY;
254         }
255
256         *got_packet_ptr = 1;
257     }
258
259 done:
260     ctx->api->picture_free(input_pic);
261     ctx->api->picture_free(recon_pic);
262     ctx->api->chunk_free(data_out);
263     return retval;
264 }
265
266 static const enum AVPixelFormat pix_fmts[] = {
267     AV_PIX_FMT_YUV420P,
268     AV_PIX_FMT_NONE
269 };
270
271 #define OFFSET(x) offsetof(LibkvazaarContext, x)
272 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
273 static const AVOption options[] = {
274     { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
275         OFFSET(kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
276     { NULL },
277 };
278
279 static const AVClass class = {
280     .class_name = "libkvazaar",
281     .item_name  = av_default_item_name,
282     .option     = options,
283     .version    = LIBAVUTIL_VERSION_INT,
284 };
285
286 static const AVCodecDefault defaults[] = {
287     { "b", "0" },
288     { NULL },
289 };
290
291 AVCodec ff_libkvazaar_encoder = {
292     .name             = "libkvazaar",
293     .long_name        = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
294     .type             = AVMEDIA_TYPE_VIDEO,
295     .id               = AV_CODEC_ID_HEVC,
296     .capabilities     = AV_CODEC_CAP_DELAY,
297     .pix_fmts         = pix_fmts,
298
299     .priv_class       = &class,
300     .priv_data_size   = sizeof(LibkvazaarContext),
301     .defaults         = defaults,
302
303     .init             = libkvazaar_init,
304     .encode2          = libkvazaar_encode,
305     .close            = libkvazaar_close,
306
307     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
308 };