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.

156 lines
4.4KB

  1. /*
  2. * webp muxer
  3. * Copyright (c) 2014 Michael Niedermayer
  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. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/opt.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. typedef struct WebpContext{
  26. AVClass *class;
  27. int frame_count;
  28. AVPacket last_pkt;
  29. int loop;
  30. } WebpContext;
  31. static int webp_write_header(AVFormatContext *s)
  32. {
  33. AVStream *st;
  34. if (s->nb_streams != 1) {
  35. av_log(s, AV_LOG_ERROR, "Only exactly 1 stream is supported\n");
  36. return AVERROR(EINVAL);
  37. }
  38. st = s->streams[0];
  39. if (st->codec->codec_id != AV_CODEC_ID_WEBP) {
  40. av_log(s, AV_LOG_ERROR, "Only WebP is supported\n");
  41. return AVERROR(EINVAL);
  42. }
  43. avpriv_set_pts_info(st, 24, 1, 1000);
  44. avio_write(s->pb, "RIFF\0\0\0\0WEBP", 12);
  45. return 0;
  46. }
  47. static int flush(AVFormatContext *s, int trailer, int64_t pts)
  48. {
  49. WebpContext *w = s->priv_data;
  50. AVStream *st = s->streams[0];
  51. if (w->last_pkt.size) {
  52. int skip = 0;
  53. if (AV_RL32(w->last_pkt.data) == AV_RL32("RIFF"))
  54. skip = 12;
  55. if (AV_RL32(w->last_pkt.data + skip) == AV_RL32("VP8X"))
  56. skip += AV_RL32(w->last_pkt.data + skip + 4) + 8;
  57. w->frame_count ++;
  58. if (w->frame_count == 1 && !trailer) {
  59. avio_write(s->pb, "VP8X", 4);
  60. avio_wl32(s->pb, 10);
  61. avio_w8(s->pb, 2+16);
  62. avio_wl24(s->pb, 0);
  63. avio_wl24(s->pb, st->codec->width - 1);
  64. avio_wl24(s->pb, st->codec->height - 1);
  65. avio_write(s->pb, "ANIM", 4);
  66. avio_wl32(s->pb, 6);
  67. avio_wl32(s->pb, 0xFFFFFFFF);
  68. avio_wl16(s->pb, w->loop);
  69. }
  70. if (w->frame_count > trailer) {
  71. avio_write(s->pb, "ANMF", 4);
  72. avio_wl32(s->pb, 16 + w->last_pkt.size - skip);
  73. avio_wl24(s->pb, 0);
  74. avio_wl24(s->pb, 0);
  75. avio_wl24(s->pb, st->codec->width - 1);
  76. avio_wl24(s->pb, st->codec->height - 1);
  77. if (w->last_pkt.pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
  78. avio_wl24(s->pb, pts - w->last_pkt.pts);
  79. } else
  80. avio_wl24(s->pb, w->last_pkt.duration);
  81. avio_w8(s->pb, 0);
  82. }
  83. avio_write(s->pb, w->last_pkt.data + skip, w->last_pkt.size - skip);
  84. av_free_packet(&w->last_pkt);
  85. }
  86. return 0;
  87. }
  88. static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
  89. {
  90. WebpContext *w = s->priv_data;
  91. int ret;
  92. if ((ret = flush(s, 0, pkt->pts)) < 0)
  93. return ret;
  94. av_copy_packet(&w->last_pkt, pkt);
  95. return 0;
  96. }
  97. static int webp_write_trailer(AVFormatContext *s)
  98. {
  99. unsigned filesize;
  100. int ret;
  101. if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0)
  102. return ret;
  103. filesize = avio_tell(s->pb);
  104. avio_seek(s->pb, 4, SEEK_SET);
  105. avio_wl32(s->pb, filesize - 8);
  106. return 0;
  107. }
  108. #define OFFSET(x) offsetof(WebpContext, x)
  109. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  110. static const AVOption options[] = {
  111. { "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop),
  112. AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC },
  113. { NULL },
  114. };
  115. static const AVClass webp_muxer_class = {
  116. .class_name = "WebP muxer",
  117. .item_name = av_default_item_name,
  118. .version = LIBAVUTIL_VERSION_INT,
  119. .option = options,
  120. };
  121. AVOutputFormat ff_webp_muxer = {
  122. .name = "webp",
  123. .long_name = NULL_IF_CONFIG_SMALL("WebP"),
  124. .extensions = "webp",
  125. .priv_data_size = sizeof(WebpContext),
  126. .video_codec = AV_CODEC_ID_WEBP,
  127. .write_header = webp_write_header,
  128. .write_packet = webp_write_packet,
  129. .write_trailer = webp_write_trailer,
  130. .priv_class = &webp_muxer_class,
  131. .flags = AVFMT_VARIABLE_FPS,
  132. };