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.

170 lines
4.8KB

  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. unsigned flags = 0;
  54. int vp8x = 0;
  55. if (AV_RL32(w->last_pkt.data) == AV_RL32("RIFF"))
  56. skip = 12;
  57. if (AV_RL32(w->last_pkt.data + skip) == AV_RL32("VP8X")) {
  58. flags |= w->last_pkt.data[skip + 4 + 4];
  59. vp8x = 1;
  60. skip += AV_RL32(w->last_pkt.data + skip + 4) + 8;
  61. }
  62. w->frame_count ++;
  63. if (w->frame_count == 1) {
  64. if (!trailer) {
  65. vp8x = 1;
  66. flags |= 2 + 16;
  67. }
  68. if (vp8x) {
  69. avio_write(s->pb, "VP8X", 4);
  70. avio_wl32(s->pb, 10);
  71. avio_w8(s->pb, flags);
  72. avio_wl24(s->pb, 0);
  73. avio_wl24(s->pb, st->codec->width - 1);
  74. avio_wl24(s->pb, st->codec->height - 1);
  75. }
  76. if (!trailer) {
  77. avio_write(s->pb, "ANIM", 4);
  78. avio_wl32(s->pb, 6);
  79. avio_wl32(s->pb, 0xFFFFFFFF);
  80. avio_wl16(s->pb, w->loop);
  81. }
  82. }
  83. if (w->frame_count > trailer) {
  84. avio_write(s->pb, "ANMF", 4);
  85. avio_wl32(s->pb, 16 + w->last_pkt.size - skip);
  86. avio_wl24(s->pb, 0);
  87. avio_wl24(s->pb, 0);
  88. avio_wl24(s->pb, st->codec->width - 1);
  89. avio_wl24(s->pb, st->codec->height - 1);
  90. if (w->last_pkt.pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
  91. avio_wl24(s->pb, pts - w->last_pkt.pts);
  92. } else
  93. avio_wl24(s->pb, w->last_pkt.duration);
  94. avio_w8(s->pb, 0);
  95. }
  96. avio_write(s->pb, w->last_pkt.data + skip, w->last_pkt.size - skip);
  97. av_free_packet(&w->last_pkt);
  98. }
  99. return 0;
  100. }
  101. static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
  102. {
  103. WebpContext *w = s->priv_data;
  104. int ret;
  105. if ((ret = flush(s, 0, pkt->pts)) < 0)
  106. return ret;
  107. av_copy_packet(&w->last_pkt, pkt);
  108. return 0;
  109. }
  110. static int webp_write_trailer(AVFormatContext *s)
  111. {
  112. unsigned filesize;
  113. int ret;
  114. if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0)
  115. return ret;
  116. filesize = avio_tell(s->pb);
  117. avio_seek(s->pb, 4, SEEK_SET);
  118. avio_wl32(s->pb, filesize - 8);
  119. return 0;
  120. }
  121. #define OFFSET(x) offsetof(WebpContext, x)
  122. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  123. static const AVOption options[] = {
  124. { "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop),
  125. AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC },
  126. { NULL },
  127. };
  128. static const AVClass webp_muxer_class = {
  129. .class_name = "WebP muxer",
  130. .item_name = av_default_item_name,
  131. .version = LIBAVUTIL_VERSION_INT,
  132. .option = options,
  133. };
  134. AVOutputFormat ff_webp_muxer = {
  135. .name = "webp",
  136. .long_name = NULL_IF_CONFIG_SMALL("WebP"),
  137. .extensions = "webp",
  138. .priv_data_size = sizeof(WebpContext),
  139. .video_codec = AV_CODEC_ID_WEBP,
  140. .write_header = webp_write_header,
  141. .write_packet = webp_write_packet,
  142. .write_trailer = webp_write_trailer,
  143. .priv_class = &webp_muxer_class,
  144. .flags = AVFMT_VARIABLE_FPS,
  145. };