Libav
libwebpenc.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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include <webp/encode.h>
28 
29 #include "libavutil/common.h"
30 #include "libavutil/frame.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/opt.h"
33 #include "avcodec.h"
34 #include "internal.h"
35 
36 typedef struct LibWebPContext {
37  AVClass *class; // class for AVOptions
38  float quality; // lossy quality 0 - 100
39  int lossless; // use lossless encoding
40  int preset; // configuration preset
41  int chroma_warning; // chroma linesize mismatch warning has been printed
42  int conversion_warning; // pixel format conversion warning has been printed
43  WebPConfig config; // libwebp configuration
45 
46 static int libwebp_error_to_averror(int err)
47 {
48  switch (err) {
49  case VP8_ENC_ERROR_OUT_OF_MEMORY:
50  case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
51  return AVERROR(ENOMEM);
52  case VP8_ENC_ERROR_NULL_PARAMETER:
53  case VP8_ENC_ERROR_INVALID_CONFIGURATION:
54  case VP8_ENC_ERROR_BAD_DIMENSION:
55  return AVERROR(EINVAL);
56  }
57  return AVERROR_UNKNOWN;
58 }
59 
61 {
62  LibWebPContext *s = avctx->priv_data;
63  int ret;
64 
65  if (avctx->global_quality < 0)
66  avctx->global_quality = 75 * FF_QP2LAMBDA;
67  s->quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
68  0.0f, 100.0f);
69 
70  if (avctx->compression_level < 0 || avctx->compression_level > 6) {
71  av_log(avctx, AV_LOG_WARNING, "invalid compression level: %d\n",
72  avctx->compression_level);
73  avctx->compression_level = av_clip(avctx->compression_level, 0, 6);
74  }
75 
76  if (s->preset >= WEBP_PRESET_DEFAULT) {
77  ret = WebPConfigPreset(&s->config, s->preset, s->quality);
78  if (!ret)
79  return AVERROR_UNKNOWN;
80  s->lossless = s->config.lossless;
81  s->quality = s->config.quality;
82  avctx->compression_level = s->config.method;
83  } else {
84  ret = WebPConfigInit(&s->config);
85  if (!ret)
86  return AVERROR_UNKNOWN;
87 
88  s->config.lossless = s->lossless;
89  s->config.quality = s->quality;
90  s->config.method = avctx->compression_level;
91 
92  ret = WebPValidateConfig(&s->config);
93  if (!ret)
94  return AVERROR(EINVAL);
95  }
96 
97  av_log(avctx, AV_LOG_DEBUG, "%s - quality=%.1f method=%d\n",
98  s->lossless ? "Lossless" : "Lossy", s->quality,
99  avctx->compression_level);
100 
101  return 0;
102 }
103 
105  const AVFrame *frame, int *got_packet)
106 {
107  LibWebPContext *s = avctx->priv_data;
108  AVFrame *alt_frame = NULL;
109  WebPPicture *pic = NULL;
110  WebPMemoryWriter mw = { 0 };
111  int ret;
112 
113  if (avctx->width > WEBP_MAX_DIMENSION || avctx->height > WEBP_MAX_DIMENSION) {
114  av_log(avctx, AV_LOG_ERROR, "Picture size is too large. Max is %dx%d.\n",
115  WEBP_MAX_DIMENSION, WEBP_MAX_DIMENSION);
116  return AVERROR(EINVAL);
117  }
118 
119  pic = av_malloc(sizeof(*pic));
120  if (!pic)
121  return AVERROR(ENOMEM);
122 
123  ret = WebPPictureInit(pic);
124  if (!ret) {
125  ret = AVERROR_UNKNOWN;
126  goto end;
127  }
128  pic->width = avctx->width;
129  pic->height = avctx->height;
130 
131  if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
132  if (!s->lossless) {
133  /* libwebp will automatically convert RGB input to YUV when
134  encoding lossy. */
135  if (!s->conversion_warning) {
136  av_log(avctx, AV_LOG_WARNING,
137  "Using libwebp for RGB-to-YUV conversion. You may want "
138  "to consider passing in YUV instead for lossy "
139  "encoding.\n");
140  s->conversion_warning = 1;
141  }
142  }
143  pic->use_argb = 1;
144  pic->argb = (uint32_t *)frame->data[0];
145  pic->argb_stride = frame->linesize[0] / 4;
146  } else {
147  if (frame->linesize[1] != frame->linesize[2]) {
148  if (!s->chroma_warning) {
149  av_log(avctx, AV_LOG_WARNING,
150  "Copying frame due to differing chroma linesizes.\n");
151  s->chroma_warning = 1;
152  }
153  alt_frame = av_frame_alloc();
154  if (!alt_frame) {
155  ret = AVERROR(ENOMEM);
156  goto end;
157  }
158  alt_frame->width = frame->width;
159  alt_frame->height = frame->height;
160  alt_frame->format = frame->format;
161  ret = av_frame_get_buffer(alt_frame, 32);
162  if (ret < 0)
163  goto end;
164  av_image_copy(alt_frame->data, alt_frame->linesize,
165  frame->data, frame->linesize,
166  avctx->pix_fmt, frame->width, frame->height);
167  frame = alt_frame;
168  }
169  pic->use_argb = 0;
170  pic->y = frame->data[0];
171  pic->u = frame->data[1];
172  pic->v = frame->data[2];
173  pic->y_stride = frame->linesize[0];
174  pic->uv_stride = frame->linesize[1];
175  if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
176  pic->colorspace = WEBP_YUV420A;
177  pic->a = frame->data[3];
178  pic->a_stride = frame->linesize[3];
179  } else {
180  pic->colorspace = WEBP_YUV420;
181  }
182 
183  if (s->lossless) {
184  /* We do not have a way to automatically prioritize RGB over YUV
185  in automatic pixel format conversion based on whether we're
186  encoding lossless or lossy, so we do conversion with libwebp as
187  a convenience. */
188  if (!s->conversion_warning) {
189  av_log(avctx, AV_LOG_WARNING,
190  "Using libwebp for YUV-to-RGB conversion. You may want "
191  "to consider passing in RGB instead for lossless "
192  "encoding.\n");
193  s->conversion_warning = 1;
194  }
195 
196 #if (WEBP_ENCODER_ABI_VERSION <= 0x201)
197  /* libwebp should do the conversion automatically, but there is a
198  bug that causes it to return an error instead, so a work-around
199  is required.
200  See https://code.google.com/p/webp/issues/detail?id=178 */
201  pic->memory_ = (void*)1; /* something non-null */
202  ret = WebPPictureYUVAToARGB(pic);
203  if (!ret) {
204  av_log(avctx, AV_LOG_ERROR,
205  "WebPPictureYUVAToARGB() failed with error: %d\n",
206  pic->error_code);
207  ret = libwebp_error_to_averror(pic->error_code);
208  goto end;
209  }
210  pic->memory_ = NULL; /* restore pointer */
211 #endif
212  }
213  }
214 
215  WebPMemoryWriterInit(&mw);
216  pic->custom_ptr = &mw;
217  pic->writer = WebPMemoryWrite;
218 
219  ret = WebPEncode(&s->config, pic);
220  if (!ret) {
221  av_log(avctx, AV_LOG_ERROR, "WebPEncode() failed with error: %d\n",
222  pic->error_code);
223  ret = libwebp_error_to_averror(pic->error_code);
224  goto end;
225  }
226 
227  ret = ff_alloc_packet(pkt, mw.size);
228  if (ret < 0)
229  goto end;
230  memcpy(pkt->data, mw.mem, mw.size);
231 
232  pkt->flags |= AV_PKT_FLAG_KEY;
233  *got_packet = 1;
234 
235 end:
236  free(mw.mem); /* must use free() according to libwebp documentation */
237  WebPPictureFree(pic);
238  av_freep(&pic);
239  av_frame_free(&alt_frame);
240 
241  return ret;
242 }
243 
244 #define OFFSET(x) offsetof(LibWebPContext, x)
245 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
246 static const AVOption options[] = {
247  { "lossless", "Use lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
248  { "preset", "Configuration preset", OFFSET(preset), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, WEBP_PRESET_TEXT, VE, "preset" },
249  { "none", "do not use a preset", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0, VE, "preset" },
250  { "default", "default preset", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DEFAULT }, 0, 0, VE, "preset" },
251  { "picture", "digital picture, like portrait, inner shot", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PICTURE }, 0, 0, VE, "preset" },
252  { "photo", "outdoor photograph, with natural lighting", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PHOTO }, 0, 0, VE, "preset" },
253  { "drawing", "hand or line drawing, with high-contrast details", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DRAWING }, 0, 0, VE, "preset" },
254  { "icon", "small-sized colorful images", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_ICON }, 0, 0, VE, "preset" },
255  { "text", "text-like", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_TEXT }, 0, 0, VE, "preset" },
256  { NULL },
257 };
258 
259 static const AVClass class = {
260  .class_name = "libwebp",
261  .item_name = av_default_item_name,
262  .option = options,
264 };
265 
267  { "compression_level", "4" },
268  { "global_quality", "-1" },
269  { NULL },
270 };
271 
273  .name = "libwebp",
274  .long_name = NULL_IF_CONFIG_SMALL("libwebp WebP image"),
275  .type = AVMEDIA_TYPE_VIDEO,
276  .id = AV_CODEC_ID_WEBP,
277  .priv_data_size = sizeof(LibWebPContext),
279  .encode2 = libwebp_encode_frame,
280  .pix_fmts = (const enum AVPixelFormat[]) {
284  },
285  .priv_class = &class,
286  .defaults = libwebp_defaults,
287 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
AVOption.
Definition: opt.h:233
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static int libwebp_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libwebpenc.c:104
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
#define OFFSET(x)
Definition: libwebpenc.c:244
AVCodec.
Definition: avcodec.h:2755
#define VE
Definition: libwebpenc.c:245
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
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:38
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:104
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
AVOptions.
uint8_t * data
Definition: avcodec.h:973
static const AVCodecDefault libwebp_defaults[]
Definition: libwebpenc.c:266
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
int width
width and height of the video frame
Definition: frame.h:146
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:244
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
reference-counted frame API
int chroma_warning
Definition: libwebpenc.c:41
int width
picture width / height.
Definition: avcodec.h:1217
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1125
static av_cold int libwebp_encode_init(AVCodecContext *avctx)
Definition: libwebpenc.c:60
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
WebPConfig config
Definition: libwebpenc.c:43
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:158
NULL
Definition: eval.c:55
Libavcodec external API header.
int compression_level
Definition: avcodec.h:1134
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
static const AVOption options[]
Definition: libwebpenc.c:246
av_default_item_name
Definition: dnxhdenc.c:45
main external API structure.
Definition: avcodec.h:1054
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:206
Describe the class of an AVClass context structure.
Definition: log.h:33
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:161
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1128
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
static int libwebp_error_to_averror(int err)
Definition: libwebpenc.c:46
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal api header.
common internal and external API header
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
void * priv_data
Definition: avcodec.h:1090
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:207
int height
Definition: frame.h:146
int conversion_warning
Definition: libwebpenc.c:42
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
AVCodec ff_libwebp_encoder
Definition: libwebpenc.c:272