| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 22 | #include "libavutil/intreadwrite.h" | ||
| 23 | #include "libavutil/opt.h" | ||
| 24 | #include "libavcodec/bytestream.h" | ||
| 25 | #include "avformat.h" | ||
| 26 | #include "internal.h" | ||
| 27 | #include "mux.h" | ||
| 28 | |||
| 29 | typedef struct WebpContext{ | ||
| 30 | AVClass *class; | ||
| 31 | int frame_count; | ||
| 32 | AVPacket *last_pkt; /* Not owned by us */ | ||
| 33 | int loop; | ||
| 34 | int wrote_webp_header; | ||
| 35 | int using_webp_anim_encoder; | ||
| 36 | } WebpContext; | ||
| 37 | |||
| 38 | ✗ | static int webp_init(AVFormatContext *s) | |
| 39 | { | ||
| 40 | ✗ | WebpContext *const w = s->priv_data; | |
| 41 | ✗ | AVStream *st = s->streams[0]; | |
| 42 | |||
| 43 | ✗ | w->last_pkt = ffformatcontext(s)->pkt; | |
| 44 | |||
| 45 | ✗ | avpriv_set_pts_info(st, 24, 1, 1000); | |
| 46 | |||
| 47 | ✗ | return 0; | |
| 48 | } | ||
| 49 | |||
| 50 | ✗ | static int is_animated_webp_packet(AVPacket *pkt) | |
| 51 | { | ||
| 52 | ✗ | int skip = 0; | |
| 53 | ✗ | unsigned flags = 0; | |
| 54 | |||
| 55 | ✗ | if (pkt->size < 4) | |
| 56 | ✗ | return AVERROR_INVALIDDATA; | |
| 57 | ✗ | if (AV_RL32(pkt->data) == AV_RL32("RIFF")) | |
| 58 | ✗ | skip = 12; | |
| 59 | // Safe to do this as a valid WebP bitstream is >=30 bytes. | ||
| 60 | ✗ | if (pkt->size < skip + 4) | |
| 61 | ✗ | return AVERROR_INVALIDDATA; | |
| 62 | ✗ | if (AV_RL32(pkt->data + skip) == AV_RL32("VP8X")) { | |
| 63 | ✗ | flags |= pkt->data[skip + 4 + 4]; | |
| 64 | } | ||
| 65 | |||
| 66 | ✗ | if (flags & 2) // ANIMATION_FLAG is on | |
| 67 | ✗ | return 1; | |
| 68 | ✗ | return 0; | |
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * Returns 1 if it has written a RIFF header with a correct length field | ||
| 73 | */ | ||
| 74 | ✗ | static int flush(AVFormatContext *s, int trailer, int64_t pts) | |
| 75 | { | ||
| 76 | ✗ | WebpContext *w = s->priv_data; | |
| 77 | ✗ | AVStream *st = s->streams[0]; | |
| 78 | uint8_t buf[12 /* RIFF+WEBP */ + 18 /* VP8X */ + | ||
| 79 | ✗ | 14 /* ANIM */ + 24 /* ANMF */], *bufp = buf; | |
| 80 | ✗ | int writing_webp_header = 0, skip = 0; | |
| 81 | ✗ | unsigned flags = 0; | |
| 82 | ✗ | int vp8x = 0; | |
| 83 | |||
| 84 | ✗ | if (!w->last_pkt->size) | |
| 85 | ✗ | return 0; | |
| 86 | |||
| 87 | ✗ | if (AV_RL32(w->last_pkt->data) == AV_RL32("RIFF")) | |
| 88 | ✗ | skip = 12; | |
| 89 | |||
| 90 | ✗ | if (AV_RL32(w->last_pkt->data + skip) == AV_RL32("VP8X")) { | |
| 91 | ✗ | flags |= w->last_pkt->data[skip + 4 + 4]; | |
| 92 | ✗ | vp8x = 1; | |
| 93 | ✗ | skip += AV_RL32(w->last_pkt->data + skip + 4) + 8; | |
| 94 | } | ||
| 95 | |||
| 96 | ✗ | if (!w->wrote_webp_header) { | |
| 97 | ✗ | bytestream_put_le32(&bufp, MKTAG('R', 'I', 'F', 'F')); | |
| 98 | ✗ | bytestream_put_le32(&bufp, 0); /* Size to be patched later */ | |
| 99 | ✗ | bytestream_put_le32(&bufp, MKTAG('W', 'E', 'B', 'P')); | |
| 100 | ✗ | writing_webp_header = 1; | |
| 101 | ✗ | w->wrote_webp_header = 1; | |
| 102 | ✗ | if (w->frame_count > 1) // first non-empty packet | |
| 103 | ✗ | w->frame_count = 1; // so we don't count previous empty packets. | |
| 104 | } | ||
| 105 | |||
| 106 | ✗ | if (w->frame_count == 1) { | |
| 107 | ✗ | if (!trailer) { | |
| 108 | ✗ | vp8x = 1; | |
| 109 | ✗ | flags |= 2 + 16; | |
| 110 | } | ||
| 111 | |||
| 112 | ✗ | if (vp8x) { | |
| 113 | ✗ | bytestream_put_le32(&bufp, MKTAG('V', 'P', '8', 'X')); | |
| 114 | ✗ | bytestream_put_le32(&bufp, 10); | |
| 115 | ✗ | bytestream_put_byte(&bufp, flags); | |
| 116 | ✗ | bytestream_put_le24(&bufp, 0); | |
| 117 | ✗ | bytestream_put_le24(&bufp, st->codecpar->width - 1); | |
| 118 | ✗ | bytestream_put_le24(&bufp, st->codecpar->height - 1); | |
| 119 | } | ||
| 120 | ✗ | if (!trailer) { | |
| 121 | ✗ | bytestream_put_le32(&bufp, MKTAG('A', 'N', 'I', 'M')); | |
| 122 | ✗ | bytestream_put_le32(&bufp, 6); | |
| 123 | ✗ | bytestream_put_le32(&bufp, 0xFFFFFFFF); | |
| 124 | ✗ | bytestream_put_le16(&bufp, w->loop); | |
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | ✗ | if (w->frame_count > trailer) { | |
| 129 | ✗ | bytestream_put_le32(&bufp, MKTAG('A', 'N', 'M', 'F')); | |
| 130 | ✗ | bytestream_put_le32(&bufp, 16 + w->last_pkt->size - skip); | |
| 131 | ✗ | bytestream_put_le24(&bufp, 0); | |
| 132 | ✗ | bytestream_put_le24(&bufp, 0); | |
| 133 | ✗ | bytestream_put_le24(&bufp, st->codecpar->width - 1); | |
| 134 | ✗ | bytestream_put_le24(&bufp, st->codecpar->height - 1); | |
| 135 | ✗ | if (w->last_pkt->pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) { | |
| 136 | ✗ | bytestream_put_le24(&bufp, pts - w->last_pkt->pts); | |
| 137 | } else | ||
| 138 | ✗ | bytestream_put_le24(&bufp, w->last_pkt->duration); | |
| 139 | ✗ | bytestream_put_byte(&bufp, 0); | |
| 140 | } | ||
| 141 | ✗ | if (trailer && writing_webp_header) | |
| 142 | ✗ | AV_WL32(buf + 4, bufp - (buf + 8) + w->last_pkt->size - skip); | |
| 143 | ✗ | avio_write(s->pb, buf, bufp - buf); | |
| 144 | ✗ | avio_write(s->pb, w->last_pkt->data + skip, w->last_pkt->size - skip); | |
| 145 | ✗ | av_packet_unref(w->last_pkt); | |
| 146 | |||
| 147 | ✗ | return trailer && writing_webp_header; | |
| 148 | } | ||
| 149 | |||
| 150 | ✗ | static int webp_write_packet(AVFormatContext *s, AVPacket *pkt) | |
| 151 | { | ||
| 152 | ✗ | WebpContext *w = s->priv_data; | |
| 153 | int ret; | ||
| 154 | |||
| 155 | ✗ | if (!pkt->size) | |
| 156 | ✗ | return 0; | |
| 157 | ✗ | ret = is_animated_webp_packet(pkt); | |
| 158 | ✗ | if (ret < 0) | |
| 159 | ✗ | return ret; | |
| 160 | ✗ | w->using_webp_anim_encoder |= ret; | |
| 161 | |||
| 162 | ✗ | if (w->using_webp_anim_encoder) { | |
| 163 | ✗ | avio_write(s->pb, pkt->data, pkt->size); | |
| 164 | ✗ | w->wrote_webp_header = 1; // for good measure | |
| 165 | } else { | ||
| 166 | ✗ | if ((ret = flush(s, 0, pkt->pts)) < 0) | |
| 167 | ✗ | return ret; | |
| 168 | ✗ | av_packet_ref(w->last_pkt, pkt); | |
| 169 | } | ||
| 170 | ✗ | ++w->frame_count; | |
| 171 | |||
| 172 | ✗ | return 0; | |
| 173 | } | ||
| 174 | |||
| 175 | ✗ | static int webp_write_trailer(AVFormatContext *s) | |
| 176 | { | ||
| 177 | unsigned filesize; | ||
| 178 | ✗ | WebpContext *w = s->priv_data; | |
| 179 | |||
| 180 | ✗ | if (w->using_webp_anim_encoder) { | |
| 181 | ✗ | if (w->loop) { // Write loop count. | |
| 182 | ✗ | if (avio_seek(s->pb, 42, SEEK_SET) == 42) | |
| 183 | ✗ | avio_wl16(s->pb, w->loop); | |
| 184 | } | ||
| 185 | } else { | ||
| 186 | int ret; | ||
| 187 | ✗ | if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0) | |
| 188 | ✗ | return ret; | |
| 189 | |||
| 190 | ✗ | if (!ret) { | |
| 191 | ✗ | filesize = avio_tell(s->pb); | |
| 192 | ✗ | if (filesize >= 8 && avio_seek(s->pb, 4, SEEK_SET) == 4) { | |
| 193 | ✗ | avio_wl32(s->pb, filesize - 8); | |
| 194 | // Note: without the following, avio only writes 8 bytes to the file. | ||
| 195 | ✗ | avio_seek(s->pb, filesize, SEEK_SET); | |
| 196 | } | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | ✗ | return 0; | |
| 201 | } | ||
| 202 | |||
| 203 | #define OFFSET(x) offsetof(WebpContext, x) | ||
| 204 | #define ENC AV_OPT_FLAG_ENCODING_PARAM | ||
| 205 | static const AVOption options[] = { | ||
| 206 | { "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop), | ||
| 207 | AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC }, | ||
| 208 | { NULL }, | ||
| 209 | }; | ||
| 210 | |||
| 211 | static const AVClass webp_muxer_class = { | ||
| 212 | .class_name = "WebP muxer", | ||
| 213 | .item_name = av_default_item_name, | ||
| 214 | .version = LIBAVUTIL_VERSION_INT, | ||
| 215 | .option = options, | ||
| 216 | }; | ||
| 217 | const FFOutputFormat ff_webp_muxer = { | ||
| 218 | .p.name = "webp", | ||
| 219 | .p.long_name = NULL_IF_CONFIG_SMALL("WebP"), | ||
| 220 | .p.extensions = "webp", | ||
| 221 | .priv_data_size = sizeof(WebpContext), | ||
| 222 | .p.video_codec = AV_CODEC_ID_WEBP, | ||
| 223 | .p.audio_codec = AV_CODEC_ID_NONE, | ||
| 224 | .p.subtitle_codec = AV_CODEC_ID_NONE, | ||
| 225 | .init = webp_init, | ||
| 226 | .write_packet = webp_write_packet, | ||
| 227 | .write_trailer = webp_write_trailer, | ||
| 228 | .p.priv_class = &webp_muxer_class, | ||
| 229 | .p.flags = AVFMT_VARIABLE_FPS, | ||
| 230 | .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH | | ||
| 231 | FF_OFMT_FLAG_ONLY_DEFAULT_CODECS, | ||
| 232 | }; | ||
| 233 |