FFmpeg
Loading...
Searching...
No Matches
libwebpenc_common.c
Go to the documentation of this file.
1/*
2 * WebP encoding support via libwebp
3 * Copyright (c) 2013 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * WebP encoder using libwebp: common structs and methods.
25 */
26
27#include "libavutil/mem.h"
28#include "libavutil/opt.h"
29#include "libwebpenc_common.h"
30
32 { "compression_level", "4" },
33 { "global_quality", "-1" },
34 { NULL },
35};
36
37#define OFFSET(x) offsetof(LibWebPContextCommon, x)
38#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
39static const AVOption options[] = {
40 { "lossless", "Use lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
41 { "preset", "Configuration preset", OFFSET(preset), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, WEBP_PRESET_TEXT, VE, .unit = "preset" },
42 { "none", "do not use a preset", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0, VE, .unit = "preset" },
43 { "default", "default preset", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DEFAULT }, 0, 0, VE, .unit = "preset" },
44 { "picture", "digital picture, like portrait, inner shot", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PICTURE }, 0, 0, VE, .unit = "preset" },
45 { "photo", "outdoor photograph, with natural lighting", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PHOTO }, 0, 0, VE, .unit = "preset" },
46 { "drawing", "hand or line drawing, with high-contrast details", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DRAWING }, 0, 0, VE, .unit = "preset" },
47 { "icon", "small-sized colorful images", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_ICON }, 0, 0, VE, .unit = "preset" },
48 { "text", "text-like", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_TEXT }, 0, 0, VE, .unit = "preset" },
49 { "cr_threshold","Conditional replenishment threshold", OFFSET(cr_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
50 { "cr_size" ,"Conditional replenishment block size", OFFSET(cr_size) , AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 256, VE },
51 { "quality" ,"Quality", OFFSET(quality), AV_OPT_TYPE_FLOAT, { .dbl = 75 }, 0, 100, VE },
52 { NULL },
53};
54
56 .class_name = "libwebp encoder",
57 .item_name = av_default_item_name,
58 .option = options,
59 .version = LIBAVUTIL_VERSION_INT,
60};
61
67
69{
70 switch (err) {
71 case VP8_ENC_ERROR_OUT_OF_MEMORY:
72 case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
73 return AVERROR(ENOMEM);
74 case VP8_ENC_ERROR_NULL_PARAMETER:
75 case VP8_ENC_ERROR_INVALID_CONFIGURATION:
76 case VP8_ENC_ERROR_BAD_DIMENSION:
77 return AVERROR(EINVAL);
78 }
79 return AVERROR_UNKNOWN;
80}
81
83{
85 int ret;
86
87 if (avctx->global_quality >= 0)
88 s->quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
89 0.0f, 100.0f);
90
91 if (avctx->compression_level < 0 || avctx->compression_level > 6) {
92 av_log(avctx, AV_LOG_WARNING, "invalid compression level: %d\n",
93 avctx->compression_level);
94 avctx->compression_level = av_clip(avctx->compression_level, 0, 6);
95 }
96
97 if (s->preset >= WEBP_PRESET_DEFAULT) {
98 ret = WebPConfigPreset(&s->config, s->preset, s->quality);
99 if (!ret)
100 return AVERROR_UNKNOWN;
101 s->lossless = s->config.lossless;
102 s->quality = s->config.quality;
103 avctx->compression_level = s->config.method;
104 } else {
105 ret = WebPConfigInit(&s->config);
106 if (!ret)
107 return AVERROR_UNKNOWN;
108
109 s->config.lossless = s->lossless;
110 s->config.quality = s->quality;
111 s->config.method = avctx->compression_level;
112
113 ret = WebPValidateConfig(&s->config);
114 if (!ret)
115 return AVERROR(EINVAL);
116 }
117
118 av_log(avctx, AV_LOG_DEBUG, "%s - quality=%.1f method=%d\n",
119 s->lossless ? "Lossless" : "Lossy", s->quality,
120 avctx->compression_level);
121
122 return 0;
123}
124
126 const AVFrame *frame, AVFrame **alt_frame_ptr,
127 WebPPicture **pic_ptr) {
128 int ret;
129 WebPPicture *pic = NULL;
130 AVFrame *alt_frame = NULL;
131
132 if (avctx->width > WEBP_MAX_DIMENSION || avctx->height > WEBP_MAX_DIMENSION) {
133 av_log(avctx, AV_LOG_ERROR, "Picture size is too large. Max is %dx%d.\n",
134 WEBP_MAX_DIMENSION, WEBP_MAX_DIMENSION);
135 return AVERROR(EINVAL);
136 }
137
138 *pic_ptr = av_malloc(sizeof(*pic));
139 pic = *pic_ptr;
140 if (!pic)
141 return AVERROR(ENOMEM);
142
143 ret = WebPPictureInit(pic);
144 if (!ret) {
145 ret = AVERROR_UNKNOWN;
146 goto end;
147 }
148 pic->width = avctx->width;
149 pic->height = avctx->height;
150
151 if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
152 if (!s->lossless) {
153 /* libwebp will automatically convert RGB input to YUV when
154 encoding lossy. */
155 if (!s->conversion_warning) {
156 av_log(avctx, AV_LOG_WARNING,
157 "Using libwebp for RGB-to-YUV conversion. You may want "
158 "to consider passing in YUV instead for lossy "
159 "encoding.\n");
160 s->conversion_warning = 1;
161 }
162 }
163 pic->use_argb = 1;
164 pic->argb = (uint32_t *)frame->data[0];
165 pic->argb_stride = frame->linesize[0] / 4;
166 } else {
167 if (frame->linesize[1] != frame->linesize[2] || s->cr_threshold) {
168 if (!s->chroma_warning && !s->cr_threshold) {
169 av_log(avctx, AV_LOG_WARNING,
170 "Copying frame due to differing chroma linesizes.\n");
171 s->chroma_warning = 1;
172 }
173 *alt_frame_ptr = av_frame_alloc();
174 alt_frame = *alt_frame_ptr;
175 if (!alt_frame) {
176 ret = AVERROR(ENOMEM);
177 goto end;
178 }
179 alt_frame->width = frame->width;
180 alt_frame->height = frame->height;
181 alt_frame->format = frame->format;
182 if (s->cr_threshold)
183 alt_frame->format = AV_PIX_FMT_YUVA420P;
184 ret = av_frame_get_buffer(alt_frame, 0);
185 if (ret < 0)
186 goto end;
187 alt_frame->format = frame->format;
188 av_frame_copy(alt_frame, frame);
189 frame = alt_frame;
190 if (s->cr_threshold) {
191 int x,y, x2, y2, p;
192 int bs = s->cr_size;
193
194 if (!s->ref) {
195 s->ref = av_frame_clone(frame);
196 if (!s->ref) {
197 ret = AVERROR(ENOMEM);
198 goto end;
199 }
200 }
201
202 alt_frame->format = AV_PIX_FMT_YUVA420P;
203 for (y = 0; y < frame->height; y+= bs) {
204 for (x = 0; x < frame->width; x+= bs) {
205 int skip;
206 int sse = 0;
207 for (p = 0; p < 3; p++) {
208 int bs2 = bs >> !!p;
209 int w = AV_CEIL_RSHIFT(frame->width , !!p);
210 int h = AV_CEIL_RSHIFT(frame->height, !!p);
211 int xs = x >> !!p;
212 int ys = y >> !!p;
213 for (y2 = ys; y2 < FFMIN(ys + bs2, h); y2++) {
214 for (x2 = xs; x2 < FFMIN(xs + bs2, w); x2++) {
215 int diff = frame->data[p][frame->linesize[p] * y2 + x2]
216 -s->ref->data[p][frame->linesize[p] * y2 + x2];
217 sse += diff*diff;
218 }
219 }
220 }
221 skip = sse < s->cr_threshold && frame->data[3] != s->ref->data[3];
222 if (!skip)
223 for (p = 0; p < 3; p++) {
224 int bs2 = bs >> !!p;
225 int w = AV_CEIL_RSHIFT(frame->width , !!p);
226 int h = AV_CEIL_RSHIFT(frame->height, !!p);
227 int xs = x >> !!p;
228 int ys = y >> !!p;
229 for (y2 = ys; y2 < FFMIN(ys + bs2, h); y2++) {
230 memcpy(&s->ref->data[p][frame->linesize[p] * y2 + xs],
231 & frame->data[p][frame->linesize[p] * y2 + xs], FFMIN(bs2, w-xs));
232 }
233 }
234 for (y2 = y; y2 < FFMIN(y+bs, frame->height); y2++) {
235 memset(&frame->data[3][frame->linesize[3] * y2 + x],
236 skip ? 0 : 255,
237 FFMIN(bs, frame->width-x));
238 }
239 }
240 }
241 }
242 }
243
244 pic->use_argb = 0;
245 pic->y = frame->data[0];
246 pic->u = frame->data[1];
247 pic->v = frame->data[2];
248 pic->y_stride = frame->linesize[0];
249 pic->uv_stride = frame->linesize[1];
250 if (frame->format == AV_PIX_FMT_YUVA420P) {
251 pic->colorspace = WEBP_YUV420A;
252 pic->a = frame->data[3];
253 pic->a_stride = frame->linesize[3];
254 if (alt_frame)
255 WebPCleanupTransparentArea(pic);
256 } else {
257 pic->colorspace = WEBP_YUV420;
258 }
259
260 if (s->lossless) {
261 /* We do not have a way to automatically prioritize RGB over YUV
262 in automatic pixel format conversion based on whether we're
263 encoding lossless or lossy, so we do conversion with libwebp as
264 a convenience. */
265 if (!s->conversion_warning) {
266 av_log(avctx, AV_LOG_WARNING,
267 "Using libwebp for YUV-to-RGB conversion. You may want "
268 "to consider passing in RGB instead for lossless "
269 "encoding.\n");
270 s->conversion_warning = 1;
271 }
272
273#if (WEBP_ENCODER_ABI_VERSION <= 0x201)
274 /* libwebp should do the conversion automatically, but there is a
275 bug that causes it to return an error instead, so a work-around
276 is required.
277 See https://code.google.com/p/webp/issues/detail?id=178 */
278 pic->memory_ = (void*)1; /* something non-null */
279 ret = WebPPictureYUVAToARGB(pic);
280 if (!ret) {
281 av_log(avctx, AV_LOG_ERROR,
282 "WebPPictureYUVAToARGB() failed with error: %d\n",
283 pic->error_code);
284 ret = libwebp_error_to_averror(pic->error_code);
285 goto end;
286 }
287 pic->memory_ = NULL; /* restore pointer */
288#endif
289 }
290 }
291end:
292 return ret;
293}
#define VE
Definition amfenc_av1.c:30
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define s(width, name)
Definition cbs_vp9.c:198
#define xs(width, name, var, subs,...)
Definition cbs_vp9.c:222
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip
Definition common.h:100
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
static AVFrame * frame
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition avutil.h:226
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR(e)
Definition error.h:45
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:206
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition frame.c:711
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_cold
Definition attributes.h:117
int ff_libwebp_get_frame(AVCodecContext *avctx, LibWebPContextCommon *s, const AVFrame *frame, AVFrame **alt_frame_ptr, WebPPicture **pic_ptr)
const AVClass ff_libwebpenc_class
enum AVPixelFormat ff_libwebpenc_pix_fmts[]
av_cold int ff_libwebp_encode_init_common(AVCodecContext *avctx)
int ff_libwebp_error_to_averror(int err)
const FFCodecDefault ff_libwebp_defaults[]
#define OFFSET(x)
WebP encoder using libwebp: common structs and methods.
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
static int sse(const MPVEncContext *const s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
#define AV_PIX_FMT_RGB32
Definition pixfmt.h:517
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int global_quality
Global quality for codecs which cannot change it per frame.
Definition avcodec.h:1235
int compression_level
Definition avcodec.h:1241
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int width
Definition frame.h:544
int height
Definition frame.h:544
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
AVOption.
Definition opt.h:428
#define av_log(a,...)
preset
Definition vf_curves.c:47
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static const uint8_t quality[]
Definition vmixdec.c:58