You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
10KB

  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. /**
  22. * @file
  23. * WebP encoder using libwebp
  24. */
  25. #include <webp/encode.h>
  26. #include "libavutil/common.h"
  27. #include "libavutil/frame.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/opt.h"
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. typedef struct LibWebPContext {
  33. AVClass *class; // class for AVOptions
  34. float quality; // lossy quality 0 - 100
  35. int lossless; // use lossless encoding
  36. int preset; // configuration preset
  37. int chroma_warning; // chroma linesize mismatch warning has been printed
  38. int conversion_warning; // pixel format conversion warning has been printed
  39. WebPConfig config; // libwebp configuration
  40. } LibWebPContext;
  41. static int libwebp_error_to_averror(int err)
  42. {
  43. switch (err) {
  44. case VP8_ENC_ERROR_OUT_OF_MEMORY:
  45. case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
  46. return AVERROR(ENOMEM);
  47. case VP8_ENC_ERROR_NULL_PARAMETER:
  48. case VP8_ENC_ERROR_INVALID_CONFIGURATION:
  49. case VP8_ENC_ERROR_BAD_DIMENSION:
  50. return AVERROR(EINVAL);
  51. }
  52. return AVERROR_UNKNOWN;
  53. }
  54. static av_cold int libwebp_encode_init(AVCodecContext *avctx)
  55. {
  56. LibWebPContext *s = avctx->priv_data;
  57. int ret;
  58. if (avctx->global_quality < 0)
  59. avctx->global_quality = 75 * FF_QP2LAMBDA;
  60. s->quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
  61. 0.0f, 100.0f);
  62. if (avctx->compression_level < 0 || avctx->compression_level > 6) {
  63. av_log(avctx, AV_LOG_WARNING, "invalid compression level: %d\n",
  64. avctx->compression_level);
  65. avctx->compression_level = av_clip(avctx->compression_level, 0, 6);
  66. }
  67. if (s->preset >= WEBP_PRESET_DEFAULT) {
  68. ret = WebPConfigPreset(&s->config, s->preset, s->quality);
  69. if (!ret)
  70. return AVERROR_UNKNOWN;
  71. s->lossless = s->config.lossless;
  72. s->quality = s->config.quality;
  73. avctx->compression_level = s->config.method;
  74. } else {
  75. ret = WebPConfigInit(&s->config);
  76. if (!ret)
  77. return AVERROR_UNKNOWN;
  78. s->config.lossless = s->lossless;
  79. s->config.quality = s->quality;
  80. s->config.method = avctx->compression_level;
  81. ret = WebPValidateConfig(&s->config);
  82. if (!ret)
  83. return AVERROR(EINVAL);
  84. }
  85. av_log(avctx, AV_LOG_DEBUG, "%s - quality=%.1f method=%d\n",
  86. s->lossless ? "Lossless" : "Lossy", s->quality,
  87. avctx->compression_level);
  88. return 0;
  89. }
  90. static int libwebp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  91. const AVFrame *frame, int *got_packet)
  92. {
  93. LibWebPContext *s = avctx->priv_data;
  94. AVFrame *alt_frame = NULL;
  95. WebPPicture *pic = NULL;
  96. WebPMemoryWriter mw = { 0 };
  97. int ret;
  98. if (avctx->width > WEBP_MAX_DIMENSION || avctx->height > WEBP_MAX_DIMENSION) {
  99. av_log(avctx, AV_LOG_ERROR, "Picture size is too large. Max is %dx%d.\n",
  100. WEBP_MAX_DIMENSION, WEBP_MAX_DIMENSION);
  101. return AVERROR(EINVAL);
  102. }
  103. pic = av_malloc(sizeof(*pic));
  104. if (!pic)
  105. return AVERROR(ENOMEM);
  106. ret = WebPPictureInit(pic);
  107. if (!ret) {
  108. ret = AVERROR_UNKNOWN;
  109. goto end;
  110. }
  111. pic->width = avctx->width;
  112. pic->height = avctx->height;
  113. if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  114. if (!s->lossless) {
  115. /* libwebp will automatically convert RGB input to YUV when
  116. encoding lossy. */
  117. if (!s->conversion_warning) {
  118. av_log(avctx, AV_LOG_WARNING,
  119. "Using libwebp for RGB-to-YUV conversion. You may want "
  120. "to consider passing in YUV instead for lossy "
  121. "encoding.\n");
  122. s->conversion_warning = 1;
  123. }
  124. }
  125. pic->use_argb = 1;
  126. pic->argb = (uint32_t *)frame->data[0];
  127. pic->argb_stride = frame->linesize[0] / 4;
  128. } else {
  129. if (frame->linesize[1] != frame->linesize[2]) {
  130. if (!s->chroma_warning) {
  131. av_log(avctx, AV_LOG_WARNING,
  132. "Copying frame due to differing chroma linesizes.\n");
  133. s->chroma_warning = 1;
  134. }
  135. alt_frame = av_frame_alloc();
  136. if (!alt_frame) {
  137. ret = AVERROR(ENOMEM);
  138. goto end;
  139. }
  140. alt_frame->width = frame->width;
  141. alt_frame->height = frame->height;
  142. alt_frame->format = frame->format;
  143. ret = av_frame_get_buffer(alt_frame, 32);
  144. if (ret < 0)
  145. goto end;
  146. av_frame_copy(alt_frame, frame);
  147. frame = alt_frame;
  148. }
  149. pic->use_argb = 0;
  150. pic->y = frame->data[0];
  151. pic->u = frame->data[1];
  152. pic->v = frame->data[2];
  153. pic->y_stride = frame->linesize[0];
  154. pic->uv_stride = frame->linesize[1];
  155. if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
  156. pic->colorspace = WEBP_YUV420A;
  157. pic->a = frame->data[3];
  158. pic->a_stride = frame->linesize[3];
  159. } else {
  160. pic->colorspace = WEBP_YUV420;
  161. }
  162. if (s->lossless) {
  163. /* We do not have a way to automatically prioritize RGB over YUV
  164. in automatic pixel format conversion based on whether we're
  165. encoding lossless or lossy, so we do conversion with libwebp as
  166. a convenience. */
  167. if (!s->conversion_warning) {
  168. av_log(avctx, AV_LOG_WARNING,
  169. "Using libwebp for YUV-to-RGB conversion. You may want "
  170. "to consider passing in RGB instead for lossless "
  171. "encoding.\n");
  172. s->conversion_warning = 1;
  173. }
  174. #if (WEBP_ENCODER_ABI_VERSION <= 0x201)
  175. /* libwebp should do the conversion automatically, but there is a
  176. bug that causes it to return an error instead, so a work-around
  177. is required.
  178. See https://code.google.com/p/webp/issues/detail?id=178 */
  179. pic->memory_ = (void*)1; /* something non-null */
  180. ret = WebPPictureYUVAToARGB(pic);
  181. if (!ret) {
  182. av_log(avctx, AV_LOG_ERROR,
  183. "WebPPictureYUVAToARGB() failed with error: %d\n",
  184. pic->error_code);
  185. ret = libwebp_error_to_averror(pic->error_code);
  186. goto end;
  187. }
  188. pic->memory_ = NULL; /* restore pointer */
  189. #endif
  190. }
  191. }
  192. WebPMemoryWriterInit(&mw);
  193. pic->custom_ptr = &mw;
  194. pic->writer = WebPMemoryWrite;
  195. ret = WebPEncode(&s->config, pic);
  196. if (!ret) {
  197. av_log(avctx, AV_LOG_ERROR, "WebPEncode() failed with error: %d\n",
  198. pic->error_code);
  199. ret = libwebp_error_to_averror(pic->error_code);
  200. goto end;
  201. }
  202. ret = ff_alloc_packet(pkt, mw.size);
  203. if (ret < 0)
  204. goto end;
  205. memcpy(pkt->data, mw.mem, mw.size);
  206. pkt->flags |= AV_PKT_FLAG_KEY;
  207. *got_packet = 1;
  208. end:
  209. #if (WEBP_ENCODER_ABI_VERSION > 0x0203)
  210. WebPMemoryWriterClear(&mw);
  211. #else
  212. free(mw.mem); /* must use free() according to libwebp documentation */
  213. #endif
  214. WebPPictureFree(pic);
  215. av_freep(&pic);
  216. av_frame_free(&alt_frame);
  217. return ret;
  218. }
  219. #define OFFSET(x) offsetof(LibWebPContext, x)
  220. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  221. static const AVOption options[] = {
  222. { "lossless", "Use lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  223. { "preset", "Configuration preset", OFFSET(preset), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, WEBP_PRESET_TEXT, VE, "preset" },
  224. { "none", "do not use a preset", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0, VE, "preset" },
  225. { "default", "default preset", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DEFAULT }, 0, 0, VE, "preset" },
  226. { "picture", "digital picture, like portrait, inner shot", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PICTURE }, 0, 0, VE, "preset" },
  227. { "photo", "outdoor photograph, with natural lighting", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_PHOTO }, 0, 0, VE, "preset" },
  228. { "drawing", "hand or line drawing, with high-contrast details", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_DRAWING }, 0, 0, VE, "preset" },
  229. { "icon", "small-sized colorful images", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_ICON }, 0, 0, VE, "preset" },
  230. { "text", "text-like", 0, AV_OPT_TYPE_CONST, { .i64 = WEBP_PRESET_TEXT }, 0, 0, VE, "preset" },
  231. { NULL },
  232. };
  233. static const AVClass class = {
  234. .class_name = "libwebp",
  235. .item_name = av_default_item_name,
  236. .option = options,
  237. .version = LIBAVUTIL_VERSION_INT,
  238. };
  239. static const AVCodecDefault libwebp_defaults[] = {
  240. { "compression_level", "4" },
  241. { "global_quality", "-1" },
  242. { NULL },
  243. };
  244. AVCodec ff_libwebp_encoder = {
  245. .name = "libwebp",
  246. .long_name = NULL_IF_CONFIG_SMALL("libwebp WebP image"),
  247. .type = AVMEDIA_TYPE_VIDEO,
  248. .id = AV_CODEC_ID_WEBP,
  249. .priv_data_size = sizeof(LibWebPContext),
  250. .init = libwebp_encode_init,
  251. .encode2 = libwebp_encode_frame,
  252. .pix_fmts = (const enum AVPixelFormat[]) {
  253. AV_PIX_FMT_RGB32,
  254. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
  255. AV_PIX_FMT_NONE
  256. },
  257. .priv_class = &class,
  258. .defaults = libwebp_defaults,
  259. };