2014-10-31 17:10:33 +02:00
|
|
|
/*
|
|
|
|
* webp muxer
|
|
|
|
* Copyright (c) 2014 Michael Niedermayer
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
2023-01-27 16:06:00 +02:00
|
|
|
#include "mux.h"
|
2014-10-31 17:10:33 +02:00
|
|
|
|
|
|
|
typedef struct WebpContext{
|
|
|
|
AVClass *class;
|
|
|
|
int frame_count;
|
2021-03-19 00:44:07 +02:00
|
|
|
AVPacket *last_pkt; /* Not owned by us */
|
2014-10-31 17:10:33 +02:00
|
|
|
int loop;
|
2015-05-20 02:39:54 +02:00
|
|
|
int wrote_webp_header;
|
|
|
|
int using_webp_anim_encoder;
|
2014-10-31 17:10:33 +02:00
|
|
|
} WebpContext;
|
|
|
|
|
2021-03-18 23:15:36 +02:00
|
|
|
static int webp_init(AVFormatContext *s)
|
2014-10-31 17:10:33 +02:00
|
|
|
{
|
2021-03-19 00:44:07 +02:00
|
|
|
WebpContext *const w = s->priv_data;
|
2014-10-31 17:10:33 +02:00
|
|
|
AVStream *st;
|
|
|
|
|
2021-03-19 00:44:07 +02:00
|
|
|
w->last_pkt = ffformatcontext(s)->pkt;
|
|
|
|
|
2014-10-31 17:10:33 +02:00
|
|
|
if (s->nb_streams != 1) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Only exactly 1 stream is supported\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
st = s->streams[0];
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id != AV_CODEC_ID_WEBP) {
|
2014-10-31 17:10:33 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "Only WebP is supported\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
avpriv_set_pts_info(st, 24, 1, 1000);
|
|
|
|
|
2015-05-20 02:39:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_animated_webp_packet(AVPacket *pkt)
|
|
|
|
{
|
2021-03-19 00:16:30 +02:00
|
|
|
int skip = 0;
|
|
|
|
unsigned flags = 0;
|
2015-05-20 02:39:54 +02:00
|
|
|
|
2021-03-19 00:16:30 +02:00
|
|
|
if (pkt->size < 4)
|
2021-03-19 00:04:37 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2021-03-19 00:16:30 +02:00
|
|
|
if (AV_RL32(pkt->data) == AV_RL32("RIFF"))
|
|
|
|
skip = 12;
|
2021-03-19 00:04:37 +02:00
|
|
|
// Safe to do this as a valid WebP bitstream is >=30 bytes.
|
2021-03-19 00:16:30 +02:00
|
|
|
if (pkt->size < skip + 4)
|
2021-03-19 00:04:37 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2021-03-19 00:16:30 +02:00
|
|
|
if (AV_RL32(pkt->data + skip) == AV_RL32("VP8X")) {
|
|
|
|
flags |= pkt->data[skip + 4 + 4];
|
|
|
|
}
|
2014-10-31 17:10:33 +02:00
|
|
|
|
2021-03-19 00:16:30 +02:00
|
|
|
if (flags & 2) // ANIMATION_FLAG is on
|
|
|
|
return 1;
|
2014-10-31 17:10:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flush(AVFormatContext *s, int trailer, int64_t pts)
|
|
|
|
{
|
|
|
|
WebpContext *w = s->priv_data;
|
|
|
|
AVStream *st = s->streams[0];
|
|
|
|
|
2021-03-19 00:44:07 +02:00
|
|
|
if (w->last_pkt->size) {
|
2014-10-31 17:10:33 +02:00
|
|
|
int skip = 0;
|
2014-11-07 05:08:18 +02:00
|
|
|
unsigned flags = 0;
|
|
|
|
int vp8x = 0;
|
2014-10-31 17:10:33 +02:00
|
|
|
|
2021-03-19 00:44:07 +02:00
|
|
|
if (AV_RL32(w->last_pkt->data) == AV_RL32("RIFF"))
|
2014-10-31 17:10:33 +02:00
|
|
|
skip = 12;
|
2015-05-20 02:39:54 +02:00
|
|
|
|
2021-03-19 00:44:07 +02:00
|
|
|
if (AV_RL32(w->last_pkt->data + skip) == AV_RL32("VP8X")) {
|
|
|
|
flags |= w->last_pkt->data[skip + 4 + 4];
|
2014-11-07 05:08:18 +02:00
|
|
|
vp8x = 1;
|
2021-03-19 00:44:07 +02:00
|
|
|
skip += AV_RL32(w->last_pkt->data + skip + 4) + 8;
|
2014-11-07 05:08:18 +02:00
|
|
|
}
|
2014-10-31 17:10:33 +02:00
|
|
|
|
2015-05-20 02:39:54 +02:00
|
|
|
if (!w->wrote_webp_header) {
|
|
|
|
avio_write(s->pb, "RIFF\0\0\0\0WEBP", 12);
|
|
|
|
w->wrote_webp_header = 1;
|
|
|
|
if (w->frame_count > 1) // first non-empty packet
|
|
|
|
w->frame_count = 1; // so we don't count previous empty packets.
|
|
|
|
}
|
2014-10-31 17:10:33 +02:00
|
|
|
|
2014-11-07 05:08:18 +02:00
|
|
|
if (w->frame_count == 1) {
|
|
|
|
if (!trailer) {
|
|
|
|
vp8x = 1;
|
|
|
|
flags |= 2 + 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vp8x) {
|
|
|
|
avio_write(s->pb, "VP8X", 4);
|
|
|
|
avio_wl32(s->pb, 10);
|
|
|
|
avio_w8(s->pb, flags);
|
|
|
|
avio_wl24(s->pb, 0);
|
2016-04-10 21:58:15 +02:00
|
|
|
avio_wl24(s->pb, st->codecpar->width - 1);
|
|
|
|
avio_wl24(s->pb, st->codecpar->height - 1);
|
2014-11-07 05:08:18 +02:00
|
|
|
}
|
|
|
|
if (!trailer) {
|
|
|
|
avio_write(s->pb, "ANIM", 4);
|
|
|
|
avio_wl32(s->pb, 6);
|
|
|
|
avio_wl32(s->pb, 0xFFFFFFFF);
|
|
|
|
avio_wl16(s->pb, w->loop);
|
|
|
|
}
|
2014-10-31 17:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (w->frame_count > trailer) {
|
|
|
|
avio_write(s->pb, "ANMF", 4);
|
2021-03-19 00:44:07 +02:00
|
|
|
avio_wl32(s->pb, 16 + w->last_pkt->size - skip);
|
2014-10-31 17:10:33 +02:00
|
|
|
avio_wl24(s->pb, 0);
|
|
|
|
avio_wl24(s->pb, 0);
|
2016-04-10 21:58:15 +02:00
|
|
|
avio_wl24(s->pb, st->codecpar->width - 1);
|
|
|
|
avio_wl24(s->pb, st->codecpar->height - 1);
|
2021-03-19 00:44:07 +02:00
|
|
|
if (w->last_pkt->pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
|
|
|
|
avio_wl24(s->pb, pts - w->last_pkt->pts);
|
2014-10-31 17:10:33 +02:00
|
|
|
} else
|
2021-03-19 00:44:07 +02:00
|
|
|
avio_wl24(s->pb, w->last_pkt->duration);
|
2014-10-31 17:10:33 +02:00
|
|
|
avio_w8(s->pb, 0);
|
|
|
|
}
|
2021-03-19 00:44:07 +02:00
|
|
|
avio_write(s->pb, w->last_pkt->data + skip, w->last_pkt->size - skip);
|
|
|
|
av_packet_unref(w->last_pkt);
|
2014-10-31 17:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
WebpContext *w = s->priv_data;
|
2021-03-19 00:04:37 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!pkt->size)
|
|
|
|
return 0;
|
|
|
|
ret = is_animated_webp_packet(pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
w->using_webp_anim_encoder |= ret;
|
2015-05-20 02:39:54 +02:00
|
|
|
|
|
|
|
if (w->using_webp_anim_encoder) {
|
|
|
|
avio_write(s->pb, pkt->data, pkt->size);
|
|
|
|
w->wrote_webp_header = 1; // for good measure
|
|
|
|
} else {
|
|
|
|
int ret;
|
|
|
|
if ((ret = flush(s, 0, pkt->pts)) < 0)
|
|
|
|
return ret;
|
2021-03-19 00:44:07 +02:00
|
|
|
av_packet_ref(w->last_pkt, pkt);
|
2015-05-20 02:39:54 +02:00
|
|
|
}
|
|
|
|
++w->frame_count;
|
2014-10-31 17:10:33 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int webp_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
unsigned filesize;
|
2015-05-20 02:39:54 +02:00
|
|
|
WebpContext *w = s->priv_data;
|
2014-10-31 17:10:33 +02:00
|
|
|
|
2015-05-20 02:39:54 +02:00
|
|
|
if (w->using_webp_anim_encoder) {
|
2021-04-11 03:53:34 +02:00
|
|
|
if (w->loop) { // Write loop count.
|
2015-05-20 02:39:54 +02:00
|
|
|
avio_seek(s->pb, 42, SEEK_SET);
|
|
|
|
avio_wl16(s->pb, w->loop);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int ret;
|
|
|
|
if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
filesize = avio_tell(s->pb);
|
|
|
|
avio_seek(s->pb, 4, SEEK_SET);
|
|
|
|
avio_wl32(s->pb, filesize - 8);
|
|
|
|
// Note: without the following, avio only writes 8 bytes to the file.
|
|
|
|
avio_seek(s->pb, filesize, SEEK_SET);
|
|
|
|
}
|
2014-10-31 17:10:33 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(WebpContext, x)
|
|
|
|
#define ENC AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop),
|
|
|
|
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass webp_muxer_class = {
|
|
|
|
.class_name = "WebP muxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.option = options,
|
|
|
|
};
|
2023-01-27 16:06:00 +02:00
|
|
|
const FFOutputFormat ff_webp_muxer = {
|
|
|
|
.p.name = "webp",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("WebP"),
|
|
|
|
.p.extensions = "webp",
|
2014-10-31 17:10:33 +02:00
|
|
|
.priv_data_size = sizeof(WebpContext),
|
2023-01-27 16:06:00 +02:00
|
|
|
.p.video_codec = AV_CODEC_ID_WEBP,
|
2021-03-18 23:15:36 +02:00
|
|
|
.init = webp_init,
|
2014-10-31 17:10:33 +02:00
|
|
|
.write_packet = webp_write_packet,
|
|
|
|
.write_trailer = webp_write_trailer,
|
2023-01-27 16:06:00 +02:00
|
|
|
.p.priv_class = &webp_muxer_class,
|
|
|
|
.p.flags = AVFMT_VARIABLE_FPS,
|
2014-10-31 17:10:33 +02:00
|
|
|
};
|