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