FFmpeg
Loading...
Searching...
No Matches
webpenc.c
Go to the documentation of this file.
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
23#include "libavutil/opt.h"
25#include "avformat.h"
26#include "internal.h"
27#include "mux.h"
28
29typedef struct WebpContext{
30 AVClass *class;
32 AVPacket *last_pkt; /* Not owned by us */
33 int loop;
37
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
51{
52 int skip = 0;
53 unsigned flags = 0;
54
55 if (pkt->size < 4)
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)
62 if (AV_RL32(pkt->data + skip) == AV_RL32("VP8X")) {
63 if (pkt->size < skip + 9)
65 flags |= pkt->data[skip + 4 + 4];
66 }
67
68 if (flags & 2) // ANIMATION_FLAG is on
69 return 1;
70 return 0;
71}
72
73/**
74 * Returns 1 if it has written a RIFF header with a correct length field
75 */
76static int flush(AVFormatContext *s, int trailer, int64_t pts)
77{
78 WebpContext *w = s->priv_data;
79 AVStream *st = s->streams[0];
80 uint8_t buf[12 /* RIFF+WEBP */ + 18 /* VP8X */ +
81 14 /* ANIM */ + 24 /* ANMF */], *bufp = buf;
82 int writing_webp_header = 0, skip = 0;
83 unsigned flags = 0;
84 int vp8x = 0;
85
86 if (!w->last_pkt->size)
87 return 0;
88
89 if (AV_RL32(w->last_pkt->data) == AV_RL32("RIFF"))
90 skip = 12;
91
92 if (AV_RL32(w->last_pkt->data + skip) == AV_RL32("VP8X")) {
93 int64_t vp8x_end = skip + 8 + (int64_t)AV_RL32(w->last_pkt->data + skip + 4);
94 if (vp8x_end > w->last_pkt->size)
96 flags |= w->last_pkt->data[skip + 4 + 4];
97 vp8x = 1;
98 skip = vp8x_end;
99 }
100
101 if (!w->wrote_webp_header) {
102 bytestream_put_le32(&bufp, MKTAG('R', 'I', 'F', 'F'));
103 bytestream_put_le32(&bufp, 0); /* Size to be patched later */
104 bytestream_put_le32(&bufp, MKTAG('W', 'E', 'B', 'P'));
105 writing_webp_header = 1;
106 w->wrote_webp_header = 1;
107 if (w->frame_count > 1) // first non-empty packet
108 w->frame_count = 1; // so we don't count previous empty packets.
109 }
110
111 if (w->frame_count == 1) {
112 if (!trailer) {
113 vp8x = 1;
114 flags |= 2 + 16;
115 }
116
117 if (vp8x) {
118 bytestream_put_le32(&bufp, MKTAG('V', 'P', '8', 'X'));
119 bytestream_put_le32(&bufp, 10);
120 bytestream_put_byte(&bufp, flags);
121 bytestream_put_le24(&bufp, 0);
122 bytestream_put_le24(&bufp, st->codecpar->width - 1);
123 bytestream_put_le24(&bufp, st->codecpar->height - 1);
124 }
125 if (!trailer) {
126 bytestream_put_le32(&bufp, MKTAG('A', 'N', 'I', 'M'));
127 bytestream_put_le32(&bufp, 6);
128 bytestream_put_le32(&bufp, 0xFFFFFFFF);
129 bytestream_put_le16(&bufp, w->loop);
130 }
131 }
132
133 if (w->frame_count > trailer) {
134 bytestream_put_le32(&bufp, MKTAG('A', 'N', 'M', 'F'));
135 bytestream_put_le32(&bufp, 16 + w->last_pkt->size - skip);
136 bytestream_put_le24(&bufp, 0);
137 bytestream_put_le24(&bufp, 0);
138 bytestream_put_le24(&bufp, st->codecpar->width - 1);
139 bytestream_put_le24(&bufp, st->codecpar->height - 1);
140 if (w->last_pkt->pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
141 bytestream_put_le24(&bufp, pts - w->last_pkt->pts);
142 } else
143 bytestream_put_le24(&bufp, w->last_pkt->duration);
144 bytestream_put_byte(&bufp, 0);
145 }
146 if (trailer && writing_webp_header)
147 AV_WL32(buf + 4, bufp - (buf + 8) + w->last_pkt->size - skip);
148 avio_write(s->pb, buf, bufp - buf);
149 avio_write(s->pb, w->last_pkt->data + skip, w->last_pkt->size - skip);
150 av_packet_unref(w->last_pkt);
151
152 return trailer && writing_webp_header;
153}
154
156{
157 WebpContext *w = s->priv_data;
158 int ret;
159
160 if (!pkt->size)
161 return 0;
163 if (ret < 0)
164 return ret;
165 w->using_webp_anim_encoder |= ret;
166
167 if (w->using_webp_anim_encoder) {
168 avio_write(s->pb, pkt->data, pkt->size);
169 w->wrote_webp_header = 1; // for good measure
170 } else {
171 if ((ret = flush(s, 0, pkt->pts)) < 0)
172 return ret;
173 av_packet_ref(w->last_pkt, pkt);
174 }
175 ++w->frame_count;
176
177 return 0;
178}
179
181{
182 unsigned filesize;
183 WebpContext *w = s->priv_data;
184
185 if (w->using_webp_anim_encoder) {
186 if (w->loop) { // Write loop count.
187 if (avio_seek(s->pb, 42, SEEK_SET) == 42)
188 avio_wl16(s->pb, w->loop);
189 }
190 } else {
191 int ret;
192 if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0)
193 return ret;
194
195 if (!ret) {
196 filesize = avio_tell(s->pb);
197 if (filesize >= 8 && avio_seek(s->pb, 4, SEEK_SET) == 4) {
198 avio_wl32(s->pb, filesize - 8);
199 // Note: without the following, avio only writes 8 bytes to the file.
200 avio_seek(s->pb, filesize, SEEK_SET);
201 }
202 }
203 }
204
205 return 0;
206}
207
208#define OFFSET(x) offsetof(WebpContext, x)
209#define ENC AV_OPT_FLAG_ENCODING_PARAM
210static const AVOption options[] = {
211 { "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop),
212 AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC },
213 { NULL },
214};
215
216static const AVClass webp_muxer_class = {
217 .class_name = "WebP muxer",
218 .item_name = av_default_item_name,
219 .version = LIBAVUTIL_VERSION_INT,
220 .option = options,
221};
223 .p.name = "webp",
224 .p.long_name = NULL_IF_CONFIG_SMALL("WebP"),
225 .p.extensions = "webp",
226 .priv_data_size = sizeof(WebpContext),
227 .p.video_codec = AV_CODEC_ID_WEBP,
228 .p.audio_codec = AV_CODEC_ID_NONE,
229 .p.subtitle_codec = AV_CODEC_ID_NONE,
230 .init = webp_init,
231 .write_packet = webp_write_packet,
232 .write_trailer = webp_write_trailer,
233 .p.priv_class = &webp_muxer_class,
234 .p.flags = AVFMT_VARIABLE_FPS,
235 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
237};
const FFOutputFormat ff_webp_muxer
Definition webpenc.c:222
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:839
Main libavformat public API header.
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition avformat.h:503
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
void avio_wl16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:440
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define ENC
Definition caca.c:198
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
static int64_t filesize(AVIOContext *pb)
Definition ffmpeg_mux.c:52
static int loop
Definition ffplay.c:407
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_WEBP
Definition codec_id.h:221
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition packet.c:442
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_WL32(p, v)
#define AV_RL32(p)
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
uint8_t w
Definition llvidencdsp.c:39
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition mux.h:59
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
Format I/O context.
Definition avformat.h:1337
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition internal.h:111
int frame_count
Definition webpenc.c:31
int using_webp_anim_encoder
Definition webpenc.c:35
AVPacket * last_pkt
Definition webpenc.c:32
int wrote_webp_header
Definition webpenc.c:34
static int64_t pts
static int webp_write_trailer(AVFormatContext *s)
Definition webpenc.c:180
static int webp_init(AVFormatContext *s)
Definition webpenc.c:38
static const AVClass webp_muxer_class
Definition webpenc.c:216
#define OFFSET(x)
Definition webpenc.c:208
static int is_animated_webp_packet(AVPacket *pkt)
Definition webpenc.c:50
static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition webpenc.c:155