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.

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