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.

153 lines
5.1KB

  1. /*
  2. * WebP encoding support via libwebp
  3. * Copyright (c) 2015 Urvang Joshi
  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 (WebPAnimEncoder API)
  24. */
  25. #include "config.h"
  26. #include "libwebpenc_common.h"
  27. #include <webp/mux.h>
  28. typedef struct LibWebPAnimContext {
  29. LibWebPContextCommon cc;
  30. WebPAnimEncoder *enc; // the main AnimEncoder object
  31. int64_t prev_frame_pts; // pts of the previously encoded frame.
  32. int done; // If true, we have assembled the bitstream already
  33. } LibWebPAnimContext;
  34. static av_cold int libwebp_anim_encode_init(AVCodecContext *avctx)
  35. {
  36. int ret = ff_libwebp_encode_init_common(avctx);
  37. if (!ret) {
  38. LibWebPAnimContext *s = avctx->priv_data;
  39. WebPAnimEncoderOptions enc_options = { { 0 } };
  40. WebPAnimEncoderOptionsInit(&enc_options);
  41. enc_options.verbose = av_log_get_level() >= AV_LOG_VERBOSE;
  42. // TODO(urvang): Expose some options on command-line perhaps.
  43. s->enc = WebPAnimEncoderNew(avctx->width, avctx->height, &enc_options);
  44. if (!s->enc)
  45. return AVERROR(EINVAL);
  46. s->prev_frame_pts = -1;
  47. s->done = 0;
  48. }
  49. return ret;
  50. }
  51. static int libwebp_anim_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  52. const AVFrame *frame, int *got_packet) {
  53. LibWebPAnimContext *s = avctx->priv_data;
  54. int ret;
  55. if (!frame) {
  56. if (s->done) { // Second flush: return empty package to denote finish.
  57. *got_packet = 0;
  58. return 0;
  59. } else { // First flush: assemble bitstream and return it.
  60. WebPData assembled_data = { 0 };
  61. ret = WebPAnimEncoderAssemble(s->enc, &assembled_data);
  62. if (ret) {
  63. ret = ff_alloc_packet2(avctx, pkt, assembled_data.size, assembled_data.size);
  64. if (ret < 0)
  65. return ret;
  66. memcpy(pkt->data, assembled_data.bytes, assembled_data.size);
  67. s->done = 1;
  68. pkt->flags |= AV_PKT_FLAG_KEY;
  69. pkt->pts = pkt->dts = s->prev_frame_pts + 1;
  70. *got_packet = 1;
  71. return 0;
  72. } else {
  73. av_log(s, AV_LOG_ERROR,
  74. "WebPAnimEncoderAssemble() failed with error: %d\n",
  75. VP8_ENC_ERROR_OUT_OF_MEMORY);
  76. return AVERROR(ENOMEM);
  77. }
  78. }
  79. } else {
  80. int timestamp_ms;
  81. WebPPicture *pic = NULL;
  82. AVFrame *alt_frame = NULL;
  83. ret = ff_libwebp_get_frame(avctx, &s->cc, frame, &alt_frame, &pic);
  84. if (ret < 0)
  85. goto end;
  86. timestamp_ms =
  87. avctx->time_base.num * frame->pts * 1000 / avctx->time_base.den;
  88. ret = WebPAnimEncoderAdd(s->enc, pic, timestamp_ms, &s->cc.config);
  89. if (!ret) {
  90. av_log(avctx, AV_LOG_ERROR,
  91. "Encoding WebP frame failed with error: %d\n",
  92. pic->error_code);
  93. ret = ff_libwebp_error_to_averror(pic->error_code);
  94. goto end;
  95. }
  96. pkt->pts = pkt->dts = frame->pts;
  97. s->prev_frame_pts = frame->pts; // Save for next frame.
  98. ret = 0;
  99. *got_packet = 1;
  100. end:
  101. WebPPictureFree(pic);
  102. av_freep(&pic);
  103. av_frame_free(&alt_frame);
  104. return ret;
  105. }
  106. }
  107. static int libwebp_anim_encode_close(AVCodecContext *avctx)
  108. {
  109. LibWebPAnimContext *s = avctx->priv_data;
  110. av_frame_free(&s->cc.ref);
  111. WebPAnimEncoderDelete(s->enc);
  112. return 0;
  113. }
  114. static const AVClass class = {
  115. .class_name = "libwebp_anim",
  116. .item_name = av_default_item_name,
  117. .option = options,
  118. .version = LIBAVUTIL_VERSION_INT,
  119. };
  120. AVCodec ff_libwebp_anim_encoder = {
  121. .name = "libwebp_anim",
  122. .long_name = NULL_IF_CONFIG_SMALL("libwebp WebP image"),
  123. .type = AVMEDIA_TYPE_VIDEO,
  124. .id = AV_CODEC_ID_WEBP,
  125. .priv_data_size = sizeof(LibWebPAnimContext),
  126. .init = libwebp_anim_encode_init,
  127. .encode2 = libwebp_anim_encode_frame,
  128. .close = libwebp_anim_encode_close,
  129. .capabilities = AV_CODEC_CAP_DELAY,
  130. .pix_fmts = (const enum AVPixelFormat[]) {
  131. AV_PIX_FMT_RGB32,
  132. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
  133. AV_PIX_FMT_NONE
  134. },
  135. .priv_class = &class,
  136. .defaults = libwebp_defaults,
  137. .wrapper_name = "libwebp",
  138. };