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.

220 lines
6.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. int wrote_webp_header;
  31. int using_webp_anim_encoder;
  32. } WebpContext;
  33. static int webp_init(AVFormatContext *s)
  34. {
  35. AVStream *st;
  36. if (s->nb_streams != 1) {
  37. av_log(s, AV_LOG_ERROR, "Only exactly 1 stream is supported\n");
  38. return AVERROR(EINVAL);
  39. }
  40. st = s->streams[0];
  41. if (st->codecpar->codec_id != AV_CODEC_ID_WEBP) {
  42. av_log(s, AV_LOG_ERROR, "Only WebP is supported\n");
  43. return AVERROR(EINVAL);
  44. }
  45. avpriv_set_pts_info(st, 24, 1, 1000);
  46. return 0;
  47. }
  48. static int is_animated_webp_packet(AVPacket *pkt)
  49. {
  50. int skip = 0;
  51. unsigned flags = 0;
  52. if (pkt->size < 4)
  53. return AVERROR_INVALIDDATA;
  54. if (AV_RL32(pkt->data) == AV_RL32("RIFF"))
  55. skip = 12;
  56. // Safe to do this as a valid WebP bitstream is >=30 bytes.
  57. if (pkt->size < skip + 4)
  58. return AVERROR_INVALIDDATA;
  59. if (AV_RL32(pkt->data + skip) == AV_RL32("VP8X")) {
  60. flags |= pkt->data[skip + 4 + 4];
  61. }
  62. if (flags & 2) // ANIMATION_FLAG is on
  63. return 1;
  64. return 0;
  65. }
  66. static int flush(AVFormatContext *s, int trailer, int64_t pts)
  67. {
  68. WebpContext *w = s->priv_data;
  69. AVStream *st = s->streams[0];
  70. if (w->last_pkt.size) {
  71. int skip = 0;
  72. unsigned flags = 0;
  73. int vp8x = 0;
  74. if (AV_RL32(w->last_pkt.data) == AV_RL32("RIFF"))
  75. skip = 12;
  76. if (AV_RL32(w->last_pkt.data + skip) == AV_RL32("VP8X")) {
  77. flags |= w->last_pkt.data[skip + 4 + 4];
  78. vp8x = 1;
  79. skip += AV_RL32(w->last_pkt.data + skip + 4) + 8;
  80. }
  81. if (!w->wrote_webp_header) {
  82. avio_write(s->pb, "RIFF\0\0\0\0WEBP", 12);
  83. w->wrote_webp_header = 1;
  84. if (w->frame_count > 1) // first non-empty packet
  85. w->frame_count = 1; // so we don't count previous empty packets.
  86. }
  87. if (w->frame_count == 1) {
  88. if (!trailer) {
  89. vp8x = 1;
  90. flags |= 2 + 16;
  91. }
  92. if (vp8x) {
  93. avio_write(s->pb, "VP8X", 4);
  94. avio_wl32(s->pb, 10);
  95. avio_w8(s->pb, flags);
  96. avio_wl24(s->pb, 0);
  97. avio_wl24(s->pb, st->codecpar->width - 1);
  98. avio_wl24(s->pb, st->codecpar->height - 1);
  99. }
  100. if (!trailer) {
  101. avio_write(s->pb, "ANIM", 4);
  102. avio_wl32(s->pb, 6);
  103. avio_wl32(s->pb, 0xFFFFFFFF);
  104. avio_wl16(s->pb, w->loop);
  105. }
  106. }
  107. if (w->frame_count > trailer) {
  108. avio_write(s->pb, "ANMF", 4);
  109. avio_wl32(s->pb, 16 + w->last_pkt.size - skip);
  110. avio_wl24(s->pb, 0);
  111. avio_wl24(s->pb, 0);
  112. avio_wl24(s->pb, st->codecpar->width - 1);
  113. avio_wl24(s->pb, st->codecpar->height - 1);
  114. if (w->last_pkt.pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
  115. avio_wl24(s->pb, pts - w->last_pkt.pts);
  116. } else
  117. avio_wl24(s->pb, w->last_pkt.duration);
  118. avio_w8(s->pb, 0);
  119. }
  120. avio_write(s->pb, w->last_pkt.data + skip, w->last_pkt.size - skip);
  121. av_packet_unref(&w->last_pkt);
  122. }
  123. return 0;
  124. }
  125. static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
  126. {
  127. WebpContext *w = s->priv_data;
  128. int ret;
  129. if (!pkt->size)
  130. return 0;
  131. ret = is_animated_webp_packet(pkt);
  132. if (ret < 0)
  133. return ret;
  134. w->using_webp_anim_encoder |= ret;
  135. if (w->using_webp_anim_encoder) {
  136. avio_write(s->pb, pkt->data, pkt->size);
  137. w->wrote_webp_header = 1; // for good measure
  138. } else {
  139. int ret;
  140. if ((ret = flush(s, 0, pkt->pts)) < 0)
  141. return ret;
  142. av_packet_ref(&w->last_pkt, pkt);
  143. }
  144. ++w->frame_count;
  145. return 0;
  146. }
  147. static int webp_write_trailer(AVFormatContext *s)
  148. {
  149. unsigned filesize;
  150. WebpContext *w = s->priv_data;
  151. if (w->using_webp_anim_encoder) {
  152. if ((w->frame_count > 1) && w->loop) { // Write loop count.
  153. avio_seek(s->pb, 42, SEEK_SET);
  154. avio_wl16(s->pb, w->loop);
  155. }
  156. } else {
  157. int ret;
  158. if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0)
  159. return ret;
  160. filesize = avio_tell(s->pb);
  161. avio_seek(s->pb, 4, SEEK_SET);
  162. avio_wl32(s->pb, filesize - 8);
  163. // Note: without the following, avio only writes 8 bytes to the file.
  164. avio_seek(s->pb, filesize, SEEK_SET);
  165. }
  166. return 0;
  167. }
  168. #define OFFSET(x) offsetof(WebpContext, x)
  169. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  170. static const AVOption options[] = {
  171. { "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop),
  172. AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC },
  173. { NULL },
  174. };
  175. static const AVClass webp_muxer_class = {
  176. .class_name = "WebP muxer",
  177. .item_name = av_default_item_name,
  178. .version = LIBAVUTIL_VERSION_INT,
  179. .option = options,
  180. };
  181. AVOutputFormat ff_webp_muxer = {
  182. .name = "webp",
  183. .long_name = NULL_IF_CONFIG_SMALL("WebP"),
  184. .extensions = "webp",
  185. .priv_data_size = sizeof(WebpContext),
  186. .video_codec = AV_CODEC_ID_WEBP,
  187. .init = webp_init,
  188. .write_packet = webp_write_packet,
  189. .write_trailer = webp_write_trailer,
  190. .priv_class = &webp_muxer_class,
  191. .flags = AVFMT_VARIABLE_FPS,
  192. };