2013-11-22 19:05:13 +03:00
|
|
|
/*
|
|
|
|
* WebP encoding support via libwebp
|
|
|
|
* Copyright (c) 2013 Justin Ruggles <justin.ruggles@gmail.com>
|
|
|
|
*
|
2013-12-22 13:53:41 +03:00
|
|
|
* This file is part of FFmpeg.
|
2013-11-22 19:05:13 +03:00
|
|
|
*
|
2013-12-22 13:53:41 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2013-11-22 19:05:13 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2013-12-22 13:53:41 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2013-11-22 19:05:13 +03:00
|
|
|
* 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
|
2013-12-22 13:53:41 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2013-11-22 19:05:13 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2015-05-20 02:39:54 +02:00
|
|
|
* WebP encoder using libwebp (WebPEncode API)
|
2013-11-22 19:05:13 +03:00
|
|
|
*/
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
#include "codec_internal.h"
|
2021-04-25 01:43:26 +02:00
|
|
|
#include "encode.h"
|
2015-05-20 02:39:54 +02:00
|
|
|
#include "libwebpenc_common.h"
|
2013-11-22 19:05:13 +03:00
|
|
|
|
2015-05-20 02:39:54 +02:00
|
|
|
typedef LibWebPContextCommon LibWebPContext;
|
2013-11-22 19:05:13 +03:00
|
|
|
|
|
|
|
static av_cold int libwebp_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
2015-05-20 02:39:54 +02:00
|
|
|
return ff_libwebp_encode_init_common(avctx);
|
2013-11-22 19:05:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int libwebp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *frame, int *got_packet)
|
|
|
|
{
|
|
|
|
LibWebPContext *s = avctx->priv_data;
|
|
|
|
WebPPicture *pic = NULL;
|
2015-05-20 02:39:54 +02:00
|
|
|
AVFrame *alt_frame = NULL;
|
2013-11-22 19:05:13 +03:00
|
|
|
WebPMemoryWriter mw = { 0 };
|
|
|
|
|
2015-05-20 02:39:54 +02:00
|
|
|
int ret = ff_libwebp_get_frame(avctx, s, frame, &alt_frame, &pic);
|
|
|
|
if (ret < 0)
|
2013-11-22 19:05:13 +03:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
WebPMemoryWriterInit(&mw);
|
|
|
|
pic->custom_ptr = &mw;
|
|
|
|
pic->writer = WebPMemoryWrite;
|
|
|
|
|
|
|
|
ret = WebPEncode(&s->config, pic);
|
|
|
|
if (!ret) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "WebPEncode() failed with error: %d\n",
|
|
|
|
pic->error_code);
|
2015-05-20 02:39:54 +02:00
|
|
|
ret = ff_libwebp_error_to_averror(pic->error_code);
|
2013-11-22 19:05:13 +03:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2021-04-25 01:43:26 +02:00
|
|
|
ret = ff_get_encode_buffer(avctx, pkt, mw.size, 0);
|
2013-11-22 19:05:13 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
memcpy(pkt->data, mw.mem, mw.size);
|
|
|
|
|
|
|
|
*got_packet = 1;
|
|
|
|
|
|
|
|
end:
|
2014-08-27 21:39:50 +03:00
|
|
|
#if (WEBP_ENCODER_ABI_VERSION > 0x0203)
|
|
|
|
WebPMemoryWriterClear(&mw);
|
|
|
|
#else
|
2013-11-22 19:05:13 +03:00
|
|
|
free(mw.mem); /* must use free() according to libwebp documentation */
|
2014-08-27 21:39:50 +03:00
|
|
|
#endif
|
2013-11-22 19:05:13 +03:00
|
|
|
WebPPictureFree(pic);
|
|
|
|
av_freep(&pic);
|
|
|
|
av_frame_free(&alt_frame);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-31 17:39:56 +02:00
|
|
|
static int libwebp_encode_close(AVCodecContext *avctx)
|
|
|
|
{
|
2015-05-23 19:24:34 +02:00
|
|
|
LibWebPContextCommon *s = avctx->priv_data;
|
|
|
|
av_frame_free(&s->ref);
|
|
|
|
|
|
|
|
return 0;
|
2014-10-31 17:39:56 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_libwebp_encoder = {
|
|
|
|
.p.name = "libwebp",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("libwebp WebP image"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_WEBP,
|
2022-11-27 14:37:10 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.pix_fmts = ff_libwebpenc_pix_fmts,
|
|
|
|
.p.priv_class = &ff_libwebpenc_class,
|
|
|
|
.p.wrapper_name = "libwebp",
|
2022-07-09 22:25:41 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
|
2013-11-22 19:05:13 +03:00
|
|
|
.priv_data_size = sizeof(LibWebPContext),
|
2021-06-09 01:33:39 +02:00
|
|
|
.defaults = ff_libwebp_defaults,
|
2013-11-22 19:05:13 +03:00
|
|
|
.init = libwebp_encode_init,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_ENCODE_CB(libwebp_encode_frame),
|
2014-10-31 17:39:56 +02:00
|
|
|
.close = libwebp_encode_close,
|
2013-11-22 19:05:13 +03:00
|
|
|
};
|