FFmpeg
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 
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/opt.h"
24 #include "avformat.h"
25 #include "internal.h"
26 #include "mux.h"
27 
28 typedef struct WebpContext{
29  AVClass *class;
31  AVPacket *last_pkt; /* Not owned by us */
32  int loop;
35 } WebpContext;
36 
38 {
39  WebpContext *const w = s->priv_data;
40  AVStream *st;
41 
42  w->last_pkt = ffformatcontext(s)->pkt;
43 
44  if (s->nb_streams != 1) {
45  av_log(s, AV_LOG_ERROR, "Only exactly 1 stream is supported\n");
46  return AVERROR(EINVAL);
47  }
48  st = s->streams[0];
49  if (st->codecpar->codec_id != AV_CODEC_ID_WEBP) {
50  av_log(s, AV_LOG_ERROR, "Only WebP is supported\n");
51  return AVERROR(EINVAL);
52  }
53  avpriv_set_pts_info(st, 24, 1, 1000);
54 
55  return 0;
56 }
57 
59 {
60  int skip = 0;
61  unsigned flags = 0;
62 
63  if (pkt->size < 4)
64  return AVERROR_INVALIDDATA;
65  if (AV_RL32(pkt->data) == AV_RL32("RIFF"))
66  skip = 12;
67  // Safe to do this as a valid WebP bitstream is >=30 bytes.
68  if (pkt->size < skip + 4)
69  return AVERROR_INVALIDDATA;
70  if (AV_RL32(pkt->data + skip) == AV_RL32("VP8X")) {
71  flags |= pkt->data[skip + 4 + 4];
72  }
73 
74  if (flags & 2) // ANIMATION_FLAG is on
75  return 1;
76  return 0;
77 }
78 
79 static int flush(AVFormatContext *s, int trailer, int64_t pts)
80 {
81  WebpContext *w = s->priv_data;
82  AVStream *st = s->streams[0];
83 
84  if (w->last_pkt->size) {
85  int skip = 0;
86  unsigned flags = 0;
87  int vp8x = 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  flags |= w->last_pkt->data[skip + 4 + 4];
94  vp8x = 1;
95  skip += AV_RL32(w->last_pkt->data + skip + 4) + 8;
96  }
97 
98  if (!w->wrote_webp_header) {
99  avio_write(s->pb, "RIFF\0\0\0\0WEBP", 12);
100  w->wrote_webp_header = 1;
101  if (w->frame_count > 1) // first non-empty packet
102  w->frame_count = 1; // so we don't count previous empty packets.
103  }
104 
105  if (w->frame_count == 1) {
106  if (!trailer) {
107  vp8x = 1;
108  flags |= 2 + 16;
109  }
110 
111  if (vp8x) {
112  avio_write(s->pb, "VP8X", 4);
113  avio_wl32(s->pb, 10);
114  avio_w8(s->pb, flags);
115  avio_wl24(s->pb, 0);
116  avio_wl24(s->pb, st->codecpar->width - 1);
117  avio_wl24(s->pb, st->codecpar->height - 1);
118  }
119  if (!trailer) {
120  avio_write(s->pb, "ANIM", 4);
121  avio_wl32(s->pb, 6);
122  avio_wl32(s->pb, 0xFFFFFFFF);
123  avio_wl16(s->pb, w->loop);
124  }
125  }
126 
127  if (w->frame_count > trailer) {
128  avio_write(s->pb, "ANMF", 4);
129  avio_wl32(s->pb, 16 + w->last_pkt->size - skip);
130  avio_wl24(s->pb, 0);
131  avio_wl24(s->pb, 0);
132  avio_wl24(s->pb, st->codecpar->width - 1);
133  avio_wl24(s->pb, st->codecpar->height - 1);
134  if (w->last_pkt->pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
135  avio_wl24(s->pb, pts - w->last_pkt->pts);
136  } else
137  avio_wl24(s->pb, w->last_pkt->duration);
138  avio_w8(s->pb, 0);
139  }
140  avio_write(s->pb, w->last_pkt->data + skip, w->last_pkt->size - skip);
141  av_packet_unref(w->last_pkt);
142  }
143 
144  return 0;
145 }
146 
148 {
149  WebpContext *w = s->priv_data;
150  int ret;
151 
152  if (!pkt->size)
153  return 0;
155  if (ret < 0)
156  return ret;
157  w->using_webp_anim_encoder |= ret;
158 
159  if (w->using_webp_anim_encoder) {
160  avio_write(s->pb, pkt->data, pkt->size);
161  w->wrote_webp_header = 1; // for good measure
162  } else {
163  int ret;
164  if ((ret = flush(s, 0, pkt->pts)) < 0)
165  return ret;
166  av_packet_ref(w->last_pkt, pkt);
167  }
168  ++w->frame_count;
169 
170  return 0;
171 }
172 
174 {
175  unsigned filesize;
176  WebpContext *w = s->priv_data;
177 
178  if (w->using_webp_anim_encoder) {
179  if (w->loop) { // Write loop count.
180  avio_seek(s->pb, 42, SEEK_SET);
181  avio_wl16(s->pb, w->loop);
182  }
183  } else {
184  int ret;
185  if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0)
186  return ret;
187 
188  filesize = avio_tell(s->pb);
189  avio_seek(s->pb, 4, SEEK_SET);
190  avio_wl32(s->pb, filesize - 8);
191  // Note: without the following, avio only writes 8 bytes to the file.
192  avio_seek(s->pb, filesize, SEEK_SET);
193  }
194 
195  return 0;
196 }
197 
198 #define OFFSET(x) offsetof(WebpContext, x)
199 #define ENC AV_OPT_FLAG_ENCODING_PARAM
200 static const AVOption options[] = {
201  { "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop),
202  AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC },
203  { NULL },
204 };
205 
206 static const AVClass webp_muxer_class = {
207  .class_name = "WebP muxer",
208  .item_name = av_default_item_name,
209  .version = LIBAVUTIL_VERSION_INT,
210  .option = options,
211 };
213  .p.name = "webp",
214  .p.long_name = NULL_IF_CONFIG_SMALL("WebP"),
215  .p.extensions = "webp",
216  .priv_data_size = sizeof(WebpContext),
217  .p.video_codec = AV_CODEC_ID_WEBP,
218  .init = webp_init,
219  .write_packet = webp_write_packet,
220  .write_trailer = webp_write_trailer,
221  .p.priv_class = &webp_muxer_class,
222  .p.flags = AVFMT_VARIABLE_FPS,
223 };
ENC
#define ENC
Definition: webpenc.c:199
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AVOutputFormat::name
const char * name
Definition: avformat.h:508
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
WebpContext::using_webp_anim_encoder
int using_webp_anim_encoder
Definition: webpenc.c:34
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:191
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:482
WebpContext::frame_count
int frame_count
Definition: webpenc.c:30
WebpContext::wrote_webp_header
int wrote_webp_header
Definition: webpenc.c:33
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:34
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:458
avpriv_set_pts_info
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:771
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:500
pts
static int64_t pts
Definition: transcode_aac.c:653
loop
static int loop
Definition: ffplay.c:340
options
static const AVOption options[]
Definition: webpenc.c:200
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:128
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FFOutputFormat
Definition: mux.h:30
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:200
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:430
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
is_animated_webp_packet
static int is_animated_webp_packet(AVPacket *pkt)
Definition: webpenc.c:58
WebpContext::last_pkt
AVPacket * last_pkt
Definition: webpenc.c:31
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:222
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:378
webp_muxer_class
static const AVClass webp_muxer_class
Definition: webpenc.c:206
filesize
static int64_t filesize(AVIOContext *pb)
Definition: ffmpeg_mux.c:48
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AVCodecParameters::height
int height
Definition: codec_par.h:129
flush
static int flush(AVFormatContext *s, int trailer, int64_t pts)
Definition: webpenc.c:79
webp_write_packet
static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: webpenc.c:147
WebpContext::loop
int loop
Definition: webpenc.c:32
AV_CODEC_ID_WEBP
@ AV_CODEC_ID_WEBP
Definition: codec_id.h:224
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:838
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:252
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
ff_webp_muxer
const FFOutputFormat ff_webp_muxer
Definition: webpenc.c:212
FFFormatContext::pkt
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition: internal.h:141
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
webp_init
static int webp_init(AVFormatContext *s)
Definition: webpenc.c:37
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
WebpContext
Definition: webpenc.c:28
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
avio_wl24
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:470
OFFSET
#define OFFSET(x)
Definition: webpenc.c:198
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:375
webp_write_trailer
static int webp_write_trailer(AVFormatContext *s)
Definition: webpenc.c:173
mux.h