27 #include <webp/encode.h>
52 case VP8_ENC_ERROR_OUT_OF_MEMORY:
53 case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
55 case VP8_ENC_ERROR_NULL_PARAMETER:
56 case VP8_ENC_ERROR_INVALID_CONFIGURATION:
57 case VP8_ENC_ERROR_BAD_DIMENSION:
78 if (s->
preset >= WEBP_PRESET_DEFAULT) {
86 ret = WebPConfigInit(&s->
config);
94 ret = WebPValidateConfig(&s->
config);
111 WebPPicture *pic =
NULL;
112 WebPMemoryWriter mw = { 0 };
115 if (avctx->
width > WEBP_MAX_DIMENSION || avctx->
height > WEBP_MAX_DIMENSION) {
117 WEBP_MAX_DIMENSION, WEBP_MAX_DIMENSION);
125 ret = WebPPictureInit(pic);
130 pic->width = avctx->
width;
131 pic->height = avctx->
height;
139 "Using libwebp for RGB-to-YUV conversion. You may want "
140 "to consider passing in YUV instead for lossy "
146 pic->argb = (uint32_t *)frame->
data[0];
147 pic->argb_stride = frame->
linesize[0] / 4;
152 "Copying frame due to differing chroma linesizes.\n");
184 for (y = 0; y < frame->
height; y+= bs) {
185 for (x = 0; x < frame->
width; x+= bs) {
188 for (p = 0; p < 3; p++) {
194 for (y2 = ys; y2 <
FFMIN(ys + bs2, h); y2++) {
195 for (x2 = xs; x2 <
FFMIN(xs + bs2, w); x2++) {
204 for (p = 0; p < 3; p++) {
210 for (y2 = ys; y2 <
FFMIN(ys + bs2, h); y2++) {
212 & frame->
data[p][frame->
linesize[p] * y2 + xs], FFMIN(bs2, w-xs));
215 for (y2 = y; y2 <
FFMIN(y+bs, frame->
height); y2++) {
218 FFMIN(bs, frame->
width-x));
226 pic->y = frame->
data[0];
227 pic->u = frame->
data[1];
228 pic->v = frame->
data[2];
230 pic->uv_stride = frame->
linesize[1];
232 pic->colorspace = WEBP_YUV420A;
233 pic->a = frame->
data[3];
236 WebPCleanupTransparentArea(pic);
238 pic->colorspace = WEBP_YUV420;
248 "Using libwebp for YUV-to-RGB conversion. You may want "
249 "to consider passing in RGB instead for lossless "
254 #if (WEBP_ENCODER_ABI_VERSION <= 0x201)
259 pic->memory_ = (
void*)1;
260 ret = WebPPictureYUVAToARGB(pic);
263 "WebPPictureYUVAToARGB() failed with error: %d\n",
273 WebPMemoryWriterInit(&mw);
274 pic->custom_ptr = &mw;
275 pic->writer = WebPMemoryWrite;
277 ret = WebPEncode(&s->
config, pic);
288 memcpy(pkt->
data, mw.mem, mw.size);
294 #if (WEBP_ENCODER_ABI_VERSION > 0x0203)
295 WebPMemoryWriterClear(&mw);
299 WebPPictureFree(pic);
315 #define OFFSET(x) offsetof(LibWebPContext, x)
316 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
320 {
"none",
"do not use a preset", 0,
AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0,
VE,
"preset" },
321 {
"default",
"default preset", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DEFAULT }, 0, 0,
VE,
"preset" },
322 {
"picture",
"digital picture, like portrait, inner shot", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PICTURE }, 0, 0,
VE,
"preset" },
323 {
"photo",
"outdoor photograph, with natural lighting", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PHOTO }, 0, 0,
VE,
"preset" },
324 {
"drawing",
"hand or line drawing, with high-contrast details", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DRAWING }, 0, 0,
VE,
"preset" },
325 {
"icon",
"small-sized colorful images", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_ICON }, 0, 0,
VE,
"preset" },
326 {
"text",
"text-like", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_TEXT }, 0, 0,
VE,
"preset" },
327 {
"cr_threshold",
"Conditional replenishment threshold",
OFFSET(cr_threshold),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
VE },
328 {
"cr_size" ,
"Conditional replenishment block size",
OFFSET(cr_size) ,
AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 256,
VE },
342 {
"compression_level",
"4" },
343 {
"global_quality",
"-1" },
361 .priv_class = &
class,
This structure describes decoded (raw) audio or video data.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
static int libwebp_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
static int libwebp_encode_close(AVCodecContext *avctx)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static av_cold int end(AVCodecContext *avctx)
static const AVCodecDefault libwebp_defaults[]
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
const char * name
Name of the codec implementation.
Libavcodec external API header.
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
int flags
A combination of AV_PKT_FLAG values.
reference-counted frame API
int width
picture width / height.
#define FF_CEIL_RSHIFT(a, b)
int ff_alloc_packet(AVPacket *avpkt, int size)
static av_cold int libwebp_encode_init(AVCodecContext *avctx)
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static const AVOption options[]
main external API structure.
Describe the class of an AVClass context structure.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
int global_quality
Global quality for codecs which cannot change it per frame.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int libwebp_error_to_averror(int err)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
common internal and external API header
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
AVPixelFormat
Pixel format.
This structure stores compressed data.
AVCodec ff_libwebp_encoder