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.

151 lines
5.0KB

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