FFmpeg
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/opt.h"
28 #include "libwebpenc_common.h"
29 
31  { "compression_level", "4" },
32  { "global_quality", "-1" },
33  { NULL },
34 };
35 
36 #define OFFSET(x) offsetof(LibWebPContextCommon, x)
37 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
38 static const AVOption options[] = {
39  { "lossless", "Use lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
40  { "preset", "Configuration preset", OFFSET(preset), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, WEBP_PRESET_TEXT, VE, "preset" },
41  { "none", "do not use a preset", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0, VE, "preset" },
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 },
50  { "quality" ,"Quality", OFFSET(quality), AV_OPT_TYPE_FLOAT, { .dbl = 75 }, 0, 100, VE },
51  { NULL },
52 };
53 
55  .class_name = "libwebp encoder",
56  .item_name = av_default_item_name,
57  .option = options,
58  .version = LIBAVUTIL_VERSION_INT,
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:
72  return AVERROR(ENOMEM);
73  case VP8_ENC_ERROR_NULL_PARAMETER:
74  case VP8_ENC_ERROR_INVALID_CONFIGURATION:
75  case VP8_ENC_ERROR_BAD_DIMENSION:
76  return AVERROR(EINVAL);
77  }
78  return AVERROR_UNKNOWN;
79 }
80 
82 {
84  int ret;
85 
86  if (avctx->global_quality >= 0)
87  s->quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
88  0.0f, 100.0f);
89 
90  if (avctx->compression_level < 0 || avctx->compression_level > 6) {
91  av_log(avctx, AV_LOG_WARNING, "invalid compression level: %d\n",
92  avctx->compression_level);
93  avctx->compression_level = av_clip(avctx->compression_level, 0, 6);
94  }
95 
96  if (s->preset >= WEBP_PRESET_DEFAULT) {
97  ret = WebPConfigPreset(&s->config, s->preset, s->quality);
98  if (!ret)
99  return AVERROR_UNKNOWN;
100  s->lossless = s->config.lossless;
101  s->quality = s->config.quality;
102  avctx->compression_level = s->config.method;
103  } else {
104  ret = WebPConfigInit(&s->config);
105  if (!ret)
106  return AVERROR_UNKNOWN;
107 
108  s->config.lossless = s->lossless;
109  s->config.quality = s->quality;
110  s->config.method = avctx->compression_level;
111 
112  ret = WebPValidateConfig(&s->config);
113  if (!ret)
114  return AVERROR(EINVAL);
115  }
116 
117  av_log(avctx, AV_LOG_DEBUG, "%s - quality=%.1f method=%d\n",
118  s->lossless ? "Lossless" : "Lossy", s->quality,
119  avctx->compression_level);
120 
121  return 0;
122 }
123 
125  const AVFrame *frame, AVFrame **alt_frame_ptr,
126  WebPPicture **pic_ptr) {
127  int ret;
128  WebPPicture *pic = NULL;
129  AVFrame *alt_frame = NULL;
130 
131  if (avctx->width > WEBP_MAX_DIMENSION || avctx->height > WEBP_MAX_DIMENSION) {
132  av_log(avctx, AV_LOG_ERROR, "Picture size is too large. Max is %dx%d.\n",
133  WEBP_MAX_DIMENSION, WEBP_MAX_DIMENSION);
134  return AVERROR(EINVAL);
135  }
136 
137  *pic_ptr = av_malloc(sizeof(*pic));
138  pic = *pic_ptr;
139  if (!pic)
140  return AVERROR(ENOMEM);
141 
142  ret = WebPPictureInit(pic);
143  if (!ret) {
145  goto end;
146  }
147  pic->width = avctx->width;
148  pic->height = avctx->height;
149 
150  if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
151  if (!s->lossless) {
152  /* libwebp will automatically convert RGB input to YUV when
153  encoding lossy. */
154  if (!s->conversion_warning) {
155  av_log(avctx, AV_LOG_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) {
168  av_log(avctx, AV_LOG_WARNING,
169  "Copying frame due to differing chroma linesizes.\n");
170  s->chroma_warning = 1;
171  }
172  *alt_frame_ptr = av_frame_alloc();
173  alt_frame = *alt_frame_ptr;
174  if (!alt_frame) {
175  ret = AVERROR(ENOMEM);
176  goto end;
177  }
178  alt_frame->width = frame->width;
179  alt_frame->height = frame->height;
180  alt_frame->format = frame->format;
181  if (s->cr_threshold)
182  alt_frame->format = AV_PIX_FMT_YUVA420P;
183  ret = av_frame_get_buffer(alt_frame, 0);
184  if (ret < 0)
185  goto end;
186  alt_frame->format = frame->format;
187  av_frame_copy(alt_frame, frame);
188  frame = alt_frame;
189  if (s->cr_threshold) {
190  int x,y, x2, y2, p;
191  int bs = s->cr_size;
192 
193  if (!s->ref) {
194  s->ref = av_frame_clone(frame);
195  if (!s->ref) {
196  ret = AVERROR(ENOMEM);
197  goto end;
198  }
199  }
200 
201  alt_frame->format = AV_PIX_FMT_YUVA420P;
202  for (y = 0; y < frame->height; y+= bs) {
203  for (x = 0; x < frame->width; x+= bs) {
204  int skip;
205  int sse = 0;
206  for (p = 0; p < 3; p++) {
207  int bs2 = bs >> !!p;
208  int w = AV_CEIL_RSHIFT(frame->width , !!p);
209  int h = AV_CEIL_RSHIFT(frame->height, !!p);
210  int xs = x >> !!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++) {
214  int diff = frame->data[p][frame->linesize[p] * y2 + x2]
215  -s->ref->data[p][frame->linesize[p] * y2 + x2];
216  sse += diff*diff;
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;
224  int w = AV_CEIL_RSHIFT(frame->width , !!p);
225  int h = AV_CEIL_RSHIFT(frame->height, !!p);
226  int xs = x >> !!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],
230  & frame->data[p][frame->linesize[p] * y2 + xs], FFMIN(bs2, w-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,
236  FFMIN(bs, frame->width-x));
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];
249  if (frame->format == AV_PIX_FMT_YUVA420P) {
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 
259  if (s->lossless) {
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) {
265  av_log(avctx, AV_LOG_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);
279  if (!ret) {
280  av_log(avctx, AV_LOG_ERROR,
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:
291  return ret;
292 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
av_clip
#define av_clip
Definition: common.h:96
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_libwebp_error_to_averror
int ff_libwebp_error_to_averror(int err)
Definition: libwebpenc_common.c:67
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:243
ff_libwebp_encode_init_common
av_cold int ff_libwebp_encode_init_common(AVCodecContext *avctx)
Definition: libwebpenc_common.c:81
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVFrame::width
int width
Definition: frame.h:412
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:251
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
quality
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
Definition: rate_distortion.txt:12
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
FFCodecDefault
Definition: codec_internal.h:97
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
preset
preset
Definition: vf_curves.c:46
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:507
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:51
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:609
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
frame
static AVFrame * frame
Definition: demux_decode.c:54
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
xs
#define xs(width, name, var, subs,...)
Definition: cbs_vp9.c:305
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
VE
#define VE
Definition: libwebpenc_common.c:37
NULL
#define NULL
Definition: coverity.c:32
ff_libwebp_defaults
const FFCodecDefault ff_libwebp_defaults[]
Definition: libwebpenc_common.c:30
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
av_clipf
av_clipf
Definition: af_crystalizer.c:121
OFFSET
#define OFFSET(x)
Definition: libwebpenc_common.c:36
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:899
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:427
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:164
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:441
LibWebPContextCommon
Definition: libwebpenc_common.h:39
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
AVCodecContext
main external API structure.
Definition: avcodec.h:441
AVFrame::height
int height
Definition: frame.h:412
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
ff_libwebp_get_frame
int ff_libwebp_get_frame(AVCodecContext *avctx, LibWebPContextCommon *s, const AVFrame *frame, AVFrame **alt_frame_ptr, WebPPicture **pic_ptr)
Definition: libwebpenc_common.c:124
options
static const AVOption options[]
Definition: libwebpenc_common.c:38
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
ff_libwebpenc_pix_fmts
enum AVPixelFormat ff_libwebpenc_pix_fmts[]
Definition: libwebpenc_common.c:61
libwebpenc_common.h
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
sse
static int sse(MpegEncContext *s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
Definition: mpegvideo_enc.c:2636
h
h
Definition: vp9dsp_template.c:2038
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:375
AVCodecContext::compression_level
int compression_level
Definition: avcodec.h:513
ff_libwebpenc_class
const AVClass ff_libwebpenc_class
Definition: libwebpenc_common.c:54