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.

255 lines
9.6KB

  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. * @file
  23. * WebP encoder using libwebp: common structs and methods.
  24. */
  25. #include "libwebpenc_common.h"
  26. int ff_libwebp_error_to_averror(int err)
  27. {
  28. switch (err) {
  29. case VP8_ENC_ERROR_OUT_OF_MEMORY:
  30. case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
  31. return AVERROR(ENOMEM);
  32. case VP8_ENC_ERROR_NULL_PARAMETER:
  33. case VP8_ENC_ERROR_INVALID_CONFIGURATION:
  34. case VP8_ENC_ERROR_BAD_DIMENSION:
  35. return AVERROR(EINVAL);
  36. }
  37. return AVERROR_UNKNOWN;
  38. }
  39. av_cold int ff_libwebp_encode_init_common(AVCodecContext *avctx)
  40. {
  41. LibWebPContextCommon *s = avctx->priv_data;
  42. int ret;
  43. if (avctx->global_quality >= 0)
  44. s->quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
  45. 0.0f, 100.0f);
  46. if (avctx->compression_level < 0 || avctx->compression_level > 6) {
  47. av_log(avctx, AV_LOG_WARNING, "invalid compression level: %d\n",
  48. avctx->compression_level);
  49. avctx->compression_level = av_clip(avctx->compression_level, 0, 6);
  50. }
  51. if (s->preset >= WEBP_PRESET_DEFAULT) {
  52. ret = WebPConfigPreset(&s->config, s->preset, s->quality);
  53. if (!ret)
  54. return AVERROR_UNKNOWN;
  55. s->lossless = s->config.lossless;
  56. s->quality = s->config.quality;
  57. avctx->compression_level = s->config.method;
  58. } else {
  59. ret = WebPConfigInit(&s->config);
  60. if (!ret)
  61. return AVERROR_UNKNOWN;
  62. s->config.lossless = s->lossless;
  63. s->config.quality = s->quality;
  64. s->config.method = avctx->compression_level;
  65. ret = WebPValidateConfig(&s->config);
  66. if (!ret)
  67. return AVERROR(EINVAL);
  68. }
  69. av_log(avctx, AV_LOG_DEBUG, "%s - quality=%.1f method=%d\n",
  70. s->lossless ? "Lossless" : "Lossy", s->quality,
  71. avctx->compression_level);
  72. return 0;
  73. }
  74. int ff_libwebp_get_frame(AVCodecContext *avctx, LibWebPContextCommon *s,
  75. const AVFrame *frame, AVFrame **alt_frame_ptr,
  76. WebPPicture **pic_ptr) {
  77. int ret;
  78. WebPPicture *pic = NULL;
  79. AVFrame *alt_frame = NULL;
  80. if (avctx->width > WEBP_MAX_DIMENSION || avctx->height > WEBP_MAX_DIMENSION) {
  81. av_log(avctx, AV_LOG_ERROR, "Picture size is too large. Max is %dx%d.\n",
  82. WEBP_MAX_DIMENSION, WEBP_MAX_DIMENSION);
  83. return AVERROR(EINVAL);
  84. }
  85. *pic_ptr = av_malloc(sizeof(*pic));
  86. pic = *pic_ptr;
  87. if (!pic)
  88. return AVERROR(ENOMEM);
  89. ret = WebPPictureInit(pic);
  90. if (!ret) {
  91. ret = AVERROR_UNKNOWN;
  92. goto end;
  93. }
  94. pic->width = avctx->width;
  95. pic->height = avctx->height;
  96. if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  97. if (!s->lossless) {
  98. /* libwebp will automatically convert RGB input to YUV when
  99. encoding lossy. */
  100. if (!s->conversion_warning) {
  101. av_log(avctx, AV_LOG_WARNING,
  102. "Using libwebp for RGB-to-YUV conversion. You may want "
  103. "to consider passing in YUV instead for lossy "
  104. "encoding.\n");
  105. s->conversion_warning = 1;
  106. }
  107. }
  108. pic->use_argb = 1;
  109. pic->argb = (uint32_t *)frame->data[0];
  110. pic->argb_stride = frame->linesize[0] / 4;
  111. } else {
  112. if (frame->linesize[1] != frame->linesize[2] || s->cr_threshold) {
  113. if (!s->chroma_warning && !s->cr_threshold) {
  114. av_log(avctx, AV_LOG_WARNING,
  115. "Copying frame due to differing chroma linesizes.\n");
  116. s->chroma_warning = 1;
  117. }
  118. *alt_frame_ptr = av_frame_alloc();
  119. alt_frame = *alt_frame_ptr;
  120. if (!alt_frame) {
  121. ret = AVERROR(ENOMEM);
  122. goto end;
  123. }
  124. alt_frame->width = frame->width;
  125. alt_frame->height = frame->height;
  126. alt_frame->format = frame->format;
  127. if (s->cr_threshold)
  128. alt_frame->format = AV_PIX_FMT_YUVA420P;
  129. ret = av_frame_get_buffer(alt_frame, 32);
  130. if (ret < 0)
  131. goto end;
  132. alt_frame->format = frame->format;
  133. av_frame_copy(alt_frame, frame);
  134. frame = alt_frame;
  135. if (s->cr_threshold) {
  136. int x,y, x2, y2, p;
  137. int bs = s->cr_size;
  138. if (!s->ref) {
  139. s->ref = av_frame_clone(frame);
  140. if (!s->ref) {
  141. ret = AVERROR(ENOMEM);
  142. goto end;
  143. }
  144. }
  145. alt_frame->format = AV_PIX_FMT_YUVA420P;
  146. for (y = 0; y < frame->height; y+= bs) {
  147. for (x = 0; x < frame->width; x+= bs) {
  148. int skip;
  149. int sse = 0;
  150. for (p = 0; p < 3; p++) {
  151. int bs2 = bs >> !!p;
  152. int w = AV_CEIL_RSHIFT(frame->width , !!p);
  153. int h = AV_CEIL_RSHIFT(frame->height, !!p);
  154. int xs = x >> !!p;
  155. int ys = y >> !!p;
  156. for (y2 = ys; y2 < FFMIN(ys + bs2, h); y2++) {
  157. for (x2 = xs; x2 < FFMIN(xs + bs2, w); x2++) {
  158. int diff = frame->data[p][frame->linesize[p] * y2 + x2]
  159. -s->ref->data[p][frame->linesize[p] * y2 + x2];
  160. sse += diff*diff;
  161. }
  162. }
  163. }
  164. skip = sse < s->cr_threshold && frame->data[3] != s->ref->data[3];
  165. if (!skip)
  166. for (p = 0; p < 3; p++) {
  167. int bs2 = bs >> !!p;
  168. int w = AV_CEIL_RSHIFT(frame->width , !!p);
  169. int h = AV_CEIL_RSHIFT(frame->height, !!p);
  170. int xs = x >> !!p;
  171. int ys = y >> !!p;
  172. for (y2 = ys; y2 < FFMIN(ys + bs2, h); y2++) {
  173. memcpy(&s->ref->data[p][frame->linesize[p] * y2 + xs],
  174. & frame->data[p][frame->linesize[p] * y2 + xs], FFMIN(bs2, w-xs));
  175. }
  176. }
  177. for (y2 = y; y2 < FFMIN(y+bs, frame->height); y2++) {
  178. memset(&frame->data[3][frame->linesize[3] * y2 + x],
  179. skip ? 0 : 255,
  180. FFMIN(bs, frame->width-x));
  181. }
  182. }
  183. }
  184. }
  185. }
  186. pic->use_argb = 0;
  187. pic->y = frame->data[0];
  188. pic->u = frame->data[1];
  189. pic->v = frame->data[2];
  190. pic->y_stride = frame->linesize[0];
  191. pic->uv_stride = frame->linesize[1];
  192. if (frame->format == AV_PIX_FMT_YUVA420P) {
  193. pic->colorspace = WEBP_YUV420A;
  194. pic->a = frame->data[3];
  195. pic->a_stride = frame->linesize[3];
  196. if (alt_frame)
  197. WebPCleanupTransparentArea(pic);
  198. } else {
  199. pic->colorspace = WEBP_YUV420;
  200. }
  201. if (s->lossless) {
  202. /* We do not have a way to automatically prioritize RGB over YUV
  203. in automatic pixel format conversion based on whether we're
  204. encoding lossless or lossy, so we do conversion with libwebp as
  205. a convenience. */
  206. if (!s->conversion_warning) {
  207. av_log(avctx, AV_LOG_WARNING,
  208. "Using libwebp for YUV-to-RGB conversion. You may want "
  209. "to consider passing in RGB instead for lossless "
  210. "encoding.\n");
  211. s->conversion_warning = 1;
  212. }
  213. #if (WEBP_ENCODER_ABI_VERSION <= 0x201)
  214. /* libwebp should do the conversion automatically, but there is a
  215. bug that causes it to return an error instead, so a work-around
  216. is required.
  217. See https://code.google.com/p/webp/issues/detail?id=178 */
  218. pic->memory_ = (void*)1; /* something non-null */
  219. ret = WebPPictureYUVAToARGB(pic);
  220. if (!ret) {
  221. av_log(avctx, AV_LOG_ERROR,
  222. "WebPPictureYUVAToARGB() failed with error: %d\n",
  223. pic->error_code);
  224. ret = libwebp_error_to_averror(pic->error_code);
  225. goto end;
  226. }
  227. pic->memory_ = NULL; /* restore pointer */
  228. #endif
  229. }
  230. }
  231. end:
  232. return ret;
  233. }