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
29
31 { "compression_level", "4" },
32 { "global_quality", "-1" },
34 };
35
36 #define OFFSET(x) offsetof(LibWebPContextCommon, x)
37 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
42 {
"default",
"default preset", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DEFAULT }, 0, 0,
VE,
"preset" },
43 {
"picture",
"digital picture, like portrait, inner shot", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PICTURE }, 0, 0,
VE,
"preset" },
44 {
"photo",
"outdoor photograph, with natural lighting", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PHOTO }, 0, 0,
VE,
"preset" },
45 {
"drawing",
"hand or line drawing, with high-contrast details", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DRAWING }, 0, 0,
VE,
"preset" },
46 {
"icon",
"small-sized colorful images", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_ICON }, 0, 0,
VE,
"preset" },
47 {
"text",
"text-like", 0,
AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_TEXT }, 0, 0,
VE,
"preset" },
48 {
"cr_threshold",
"Conditional replenishment threshold",
OFFSET(cr_threshold),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
VE },
49 {
"cr_size" ,
"Conditional replenishment block size",
OFFSET(cr_size) ,
AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 256,
VE },
52 };
53
59 };
60
65 };
66
68 {
69 switch (err) {
70 case VP8_ENC_ERROR_OUT_OF_MEMORY:
71 case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
73 case VP8_ENC_ERROR_NULL_PARAMETER:
74 case VP8_ENC_ERROR_INVALID_CONFIGURATION:
75 case VP8_ENC_ERROR_BAD_DIMENSION:
77 }
79 }
80
82 {
85
88 0.0f, 100.0f);
89
94 }
95
96 if (
s->preset >= WEBP_PRESET_DEFAULT) {
97 ret = WebPConfigPreset(&
s->config,
s->preset,
s->quality);
100 s->lossless =
s->config.lossless;
101 s->quality =
s->config.quality;
103 } else {
104 ret = WebPConfigInit(&
s->config);
107
108 s->config.lossless =
s->lossless;
109 s->config.quality =
s->quality;
111
112 ret = WebPValidateConfig(&
s->config);
115 }
116
118 s->lossless ?
"Lossless" :
"Lossy",
s->quality,
120
121 return 0;
122 }
123
126 WebPPicture **pic_ptr) {
128 WebPPicture *pic =
NULL;
130
131 if (avctx->
width > WEBP_MAX_DIMENSION || avctx->
height > WEBP_MAX_DIMENSION) {
133 WEBP_MAX_DIMENSION, WEBP_MAX_DIMENSION);
135 }
136
138 pic = *pic_ptr;
139 if (!pic)
141
142 ret = WebPPictureInit(pic);
145 goto end;
146 }
147 pic->width = avctx->
width;
148 pic->height = avctx->
height;
149
152 /* libwebp will automatically convert RGB input to YUV when
153 encoding lossy. */
154 if (!
s->conversion_warning) {
156 "Using libwebp for RGB-to-YUV conversion. You may want "
157 "to consider passing in YUV instead for lossy "
158 "encoding.\n");
159 s->conversion_warning = 1;
160 }
161 }
162 pic->use_argb = 1;
163 pic->argb = (uint32_t *)
frame->data[0];
164 pic->argb_stride =
frame->linesize[0] / 4;
165 } else {
166 if (
frame->linesize[1] !=
frame->linesize[2] ||
s->cr_threshold) {
167 if (!
s->chroma_warning && !
s->cr_threshold) {
169 "Copying frame due to differing chroma linesizes.\n");
170 s->chroma_warning = 1;
171 }
173 alt_frame = *alt_frame_ptr;
174 if (!alt_frame) {
176 goto end;
177 }
185 goto end;
189 if (
s->cr_threshold) {
190 int x,y, x2, y2, p;
192
197 goto end;
198 }
199 }
200
202 for (y = 0; y <
frame->height; y+= bs) {
203 for (x = 0; x <
frame->width; x+= bs) {
204 int skip;
206 for (p = 0; p < 3; p++) {
207 int bs2 = bs >> !!p;
211 int ys = y >> !!p;
212 for (y2 = ys; y2 <
FFMIN(ys + bs2,
h); y2++) {
213 for (x2 =
xs; x2 <
FFMIN(
xs + bs2,
w); x2++) {
215 -
s->ref->data[p][
frame->linesize[p] * y2 + x2];
217 }
218 }
219 }
220 skip =
sse <
s->cr_threshold &&
frame->data[3] !=
s->ref->data[3];
221 if (!skip)
222 for (p = 0; p < 3; p++) {
223 int bs2 = bs >> !!p;
227 int ys = y >> !!p;
228 for (y2 = ys; y2 <
FFMIN(ys + bs2,
h); y2++) {
229 memcpy(&
s->ref->data[p][
frame->linesize[p] * y2 +
xs],
231 }
232 }
233 for (y2 = y; y2 <
FFMIN(y+bs,
frame->height); y2++) {
234 memset(&
frame->data[3][
frame->linesize[3] * y2 + x],
235 skip ? 0 : 255,
237 }
238 }
239 }
240 }
241 }
242
243 pic->use_argb = 0;
244 pic->y =
frame->data[0];
245 pic->u =
frame->data[1];
246 pic->v =
frame->data[2];
247 pic->y_stride =
frame->linesize[0];
248 pic->uv_stride =
frame->linesize[1];
250 pic->colorspace = WEBP_YUV420A;
251 pic->a =
frame->data[3];
252 pic->a_stride =
frame->linesize[3];
253 if (alt_frame)
254 WebPCleanupTransparentArea(pic);
255 } else {
256 pic->colorspace = WEBP_YUV420;
257 }
258
260 /* We do not have a way to automatically prioritize RGB over YUV
261 in automatic pixel format conversion based on whether we're
262 encoding lossless or lossy, so we do conversion with libwebp as
263 a convenience. */
264 if (!
s->conversion_warning) {
266 "Using libwebp for YUV-to-RGB conversion. You may want "
267 "to consider passing in RGB instead for lossless "
268 "encoding.\n");
269 s->conversion_warning = 1;
270 }
271
272 #if (WEBP_ENCODER_ABI_VERSION <= 0x201)
273 /* libwebp should do the conversion automatically, but there is a
274 bug that causes it to return an error instead, so a work-around
275 is required.
276 See https://code.google.com/p/webp/issues/detail?id=178 */
277 pic->memory_ = (void*)1; /* something non-null */
278 ret = WebPPictureYUVAToARGB(pic);
281 "WebPPictureYUVAToARGB() failed with error: %d\n",
282 pic->error_code);
283 ret = libwebp_error_to_averror(pic->error_code);
284 goto end;
285 }
286 pic->memory_ =
NULL;
/* restore pointer */
287 #endif
288 }
289 }
290 end:
292 }