2 * WebP encoding support via libwebp
3 * Copyright (c) 2013 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
24 * WebP encoder using libwebp: common structs and methods.
27 #include "libwebpenc_common.h"
29 int ff_libwebp_error_to_averror(int err)
32 case VP8_ENC_ERROR_OUT_OF_MEMORY:
33 case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
34 return AVERROR(ENOMEM);
35 case VP8_ENC_ERROR_NULL_PARAMETER:
36 case VP8_ENC_ERROR_INVALID_CONFIGURATION:
37 case VP8_ENC_ERROR_BAD_DIMENSION:
38 return AVERROR(EINVAL);
40 return AVERROR_UNKNOWN;
43 av_cold int ff_libwebp_encode_init_common(AVCodecContext *avctx)
45 LibWebPContextCommon *s = avctx->priv_data;
48 if (avctx->global_quality >= 0)
49 s->quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
52 if (avctx->compression_level < 0 || avctx->compression_level > 6) {
53 av_log(avctx, AV_LOG_WARNING, "invalid compression level: %d\n",
54 avctx->compression_level);
55 avctx->compression_level = av_clip(avctx->compression_level, 0, 6);
58 if (s->preset >= WEBP_PRESET_DEFAULT) {
59 ret = WebPConfigPreset(&s->config, s->preset, s->quality);
61 return AVERROR_UNKNOWN;
62 s->lossless = s->config.lossless;
63 s->quality = s->config.quality;
64 avctx->compression_level = s->config.method;
66 ret = WebPConfigInit(&s->config);
68 return AVERROR_UNKNOWN;
70 s->config.lossless = s->lossless;
71 s->config.quality = s->quality;
72 s->config.method = avctx->compression_level;
74 ret = WebPValidateConfig(&s->config);
76 return AVERROR(EINVAL);
79 av_log(avctx, AV_LOG_DEBUG, "%s - quality=%.1f method=%d\n",
80 s->lossless ? "Lossless" : "Lossy", s->quality,
81 avctx->compression_level);
86 int ff_libwebp_get_frame(AVCodecContext *avctx, LibWebPContextCommon *s,
87 const AVFrame *frame, AVFrame **alt_frame_ptr,
88 WebPPicture **pic_ptr) {
90 WebPPicture *pic = NULL;
91 AVFrame *alt_frame = NULL;
93 if (avctx->width > WEBP_MAX_DIMENSION || avctx->height > WEBP_MAX_DIMENSION) {
94 av_log(avctx, AV_LOG_ERROR, "Picture size is too large. Max is %dx%d.\n",
95 WEBP_MAX_DIMENSION, WEBP_MAX_DIMENSION);
96 return AVERROR(EINVAL);
99 *pic_ptr = av_malloc(sizeof(*pic));
102 return AVERROR(ENOMEM);
104 ret = WebPPictureInit(pic);
106 ret = AVERROR_UNKNOWN;
109 pic->width = avctx->width;
110 pic->height = avctx->height;
112 if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
114 /* libwebp will automatically convert RGB input to YUV when
116 if (!s->conversion_warning) {
117 av_log(avctx, AV_LOG_WARNING,
118 "Using libwebp for RGB-to-YUV conversion. You may want "
119 "to consider passing in YUV instead for lossy "
121 s->conversion_warning = 1;
125 pic->argb = (uint32_t *)frame->data[0];
126 pic->argb_stride = frame->linesize[0] / 4;
128 if (frame->linesize[1] != frame->linesize[2] || s->cr_threshold) {
129 if (!s->chroma_warning && !s->cr_threshold) {
130 av_log(avctx, AV_LOG_WARNING,
131 "Copying frame due to differing chroma linesizes.\n");
132 s->chroma_warning = 1;
134 *alt_frame_ptr = av_frame_alloc();
135 alt_frame = *alt_frame_ptr;
137 ret = AVERROR(ENOMEM);
140 alt_frame->width = frame->width;
141 alt_frame->height = frame->height;
142 alt_frame->format = frame->format;
144 alt_frame->format = AV_PIX_FMT_YUVA420P;
145 ret = av_frame_get_buffer(alt_frame, 32);
148 alt_frame->format = frame->format;
149 av_frame_copy(alt_frame, frame);
151 if (s->cr_threshold) {
156 s->ref = av_frame_clone(frame);
158 ret = AVERROR(ENOMEM);
163 alt_frame->format = AV_PIX_FMT_YUVA420P;
164 for (y = 0; y < frame->height; y+= bs) {
165 for (x = 0; x < frame->width; x+= bs) {
168 for (p = 0; p < 3; p++) {
170 int w = AV_CEIL_RSHIFT(frame->width , !!p);
171 int h = AV_CEIL_RSHIFT(frame->height, !!p);
174 for (y2 = ys; y2 < FFMIN(ys + bs2, h); y2++) {
175 for (x2 = xs; x2 < FFMIN(xs + bs2, w); x2++) {
176 int diff = frame->data[p][frame->linesize[p] * y2 + x2]
177 -s->ref->data[p][frame->linesize[p] * y2 + x2];
182 skip = sse < s->cr_threshold && frame->data[3] != s->ref->data[3];
184 for (p = 0; p < 3; p++) {
186 int w = AV_CEIL_RSHIFT(frame->width , !!p);
187 int h = AV_CEIL_RSHIFT(frame->height, !!p);
190 for (y2 = ys; y2 < FFMIN(ys + bs2, h); y2++) {
191 memcpy(&s->ref->data[p][frame->linesize[p] * y2 + xs],
192 & frame->data[p][frame->linesize[p] * y2 + xs], FFMIN(bs2, w-xs));
195 for (y2 = y; y2 < FFMIN(y+bs, frame->height); y2++) {
196 memset(&frame->data[3][frame->linesize[3] * y2 + x],
198 FFMIN(bs, frame->width-x));
206 pic->y = frame->data[0];
207 pic->u = frame->data[1];
208 pic->v = frame->data[2];
209 pic->y_stride = frame->linesize[0];
210 pic->uv_stride = frame->linesize[1];
211 if (frame->format == AV_PIX_FMT_YUVA420P) {
212 pic->colorspace = WEBP_YUV420A;
213 pic->a = frame->data[3];
214 pic->a_stride = frame->linesize[3];
216 WebPCleanupTransparentArea(pic);
218 pic->colorspace = WEBP_YUV420;
222 /* We do not have a way to automatically prioritize RGB over YUV
223 in automatic pixel format conversion based on whether we're
224 encoding lossless or lossy, so we do conversion with libwebp as
226 if (!s->conversion_warning) {
227 av_log(avctx, AV_LOG_WARNING,
228 "Using libwebp for YUV-to-RGB conversion. You may want "
229 "to consider passing in RGB instead for lossless "
231 s->conversion_warning = 1;
234 #if (WEBP_ENCODER_ABI_VERSION <= 0x201)
235 /* libwebp should do the conversion automatically, but there is a
236 bug that causes it to return an error instead, so a work-around
238 See https://code.google.com/p/webp/issues/detail?id=178 */
239 pic->memory_ = (void*)1; /* something non-null */
240 ret = WebPPictureYUVAToARGB(pic);
242 av_log(avctx, AV_LOG_ERROR,
243 "WebPPictureYUVAToARGB() failed with error: %d\n",
245 ret = libwebp_error_to_averror(pic->error_code);
248 pic->memory_ = NULL; /* restore pointer */