2011-01-17 20:11:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Reimar Döffinger
|
|
|
|
*
|
|
|
|
* 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 "avformat.h"
|
2016-02-29 14:43:07 +00:00
|
|
|
#include "internal.h"
|
2011-01-17 20:11:51 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
|
2015-07-12 10:47:20 +00:00
|
|
|
typedef struct IVFEncContext {
|
|
|
|
unsigned frame_cnt;
|
2021-03-02 18:52:44 +00:00
|
|
|
uint64_t last_pts, sum_delta_pts, last_pkt_duration;
|
2015-07-12 10:47:20 +00:00
|
|
|
} IVFEncContext;
|
|
|
|
|
2020-05-10 13:49:15 +00:00
|
|
|
static int ivf_init(AVFormatContext *s)
|
2011-01-17 20:11:51 +00:00
|
|
|
{
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
AVCodecParameters *par;
|
2011-01-17 20:11:51 +00:00
|
|
|
|
|
|
|
if (s->nb_streams != 1) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Format supports only exactly one video stream\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
par = s->streams[0]->codecpar;
|
2016-04-10 19:58:15 +00:00
|
|
|
if (par->codec_type != AVMEDIA_TYPE_VIDEO ||
|
2018-02-11 20:32:04 +00:00
|
|
|
!(par->codec_id == AV_CODEC_ID_AV1 ||
|
|
|
|
par->codec_id == AV_CODEC_ID_VP8 ||
|
|
|
|
par->codec_id == AV_CODEC_ID_VP9)) {
|
2017-12-10 23:28:50 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Currently only VP8, VP9 and AV1 are supported!\n");
|
2011-01-17 20:11:51 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2020-05-10 13:49:15 +00:00
|
|
|
|
2020-05-10 13:50:47 +00:00
|
|
|
if (par->codec_id == AV_CODEC_ID_VP9) {
|
|
|
|
int ret = ff_stream_add_bitstream_filter(s->streams[0], "vp9_superframe", NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else if (par->codec_id == AV_CODEC_ID_AV1) {
|
|
|
|
int ret = ff_stream_add_bitstream_filter(s->streams[0], "av1_metadata", "td=insert");
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:49:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ivf_write_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
AVCodecParameters *par = s->streams[0]->codecpar;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_write(pb, "DKIF", 4);
|
|
|
|
avio_wl16(pb, 0); // version
|
|
|
|
avio_wl16(pb, 32); // header length
|
2018-09-24 00:54:24 +00:00
|
|
|
avio_wl32(pb,
|
2017-12-10 23:28:50 +00:00
|
|
|
par->codec_id == AV_CODEC_ID_VP9 ? AV_RL32("VP90") :
|
|
|
|
par->codec_id == AV_CODEC_ID_VP8 ? AV_RL32("VP80") : AV_RL32("AV01"));
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
avio_wl16(pb, par->width);
|
|
|
|
avio_wl16(pb, par->height);
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wl32(pb, s->streams[0]->time_base.den);
|
|
|
|
avio_wl32(pb, s->streams[0]->time_base.num);
|
2019-10-01 17:40:55 +00:00
|
|
|
avio_wl64(pb, 0xFFFFFFFFFFFFFFFFULL); // length is overwritten at the end of muxing
|
2011-01-17 20:11:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ivf_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2015-07-12 10:47:20 +00:00
|
|
|
IVFEncContext *ctx = s->priv_data;
|
|
|
|
|
2011-02-21 18:28:17 +00:00
|
|
|
avio_wl32(pb, pkt->size);
|
|
|
|
avio_wl64(pb, pkt->pts);
|
|
|
|
avio_write(pb, pkt->data, pkt->size);
|
2015-07-12 10:47:20 +00:00
|
|
|
if (ctx->frame_cnt)
|
|
|
|
ctx->sum_delta_pts += pkt->pts - ctx->last_pts;
|
2021-03-02 18:52:44 +00:00
|
|
|
ctx->last_pkt_duration = pkt->duration;
|
2015-07-12 10:47:20 +00:00
|
|
|
ctx->frame_cnt++;
|
|
|
|
ctx->last_pts = pkt->pts;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ivf_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
AVIOContext *pb = s->pb;
|
2016-01-08 22:01:30 +00:00
|
|
|
IVFEncContext *ctx = s->priv_data;
|
|
|
|
|
2021-03-02 18:52:44 +00:00
|
|
|
if ((pb->seekable & AVIO_SEEKABLE_NORMAL) &&
|
|
|
|
(ctx->frame_cnt > 1 || (ctx->frame_cnt == 1 && ctx->last_pkt_duration))) {
|
2020-02-27 05:33:50 +00:00
|
|
|
int64_t end = avio_tell(pb);
|
2015-07-12 10:47:20 +00:00
|
|
|
|
|
|
|
avio_seek(pb, 24, SEEK_SET);
|
2019-10-01 17:40:55 +00:00
|
|
|
// overwrite the "length" field (duration)
|
2021-03-02 18:52:44 +00:00
|
|
|
avio_wl32(pb, ctx->last_pkt_duration ?
|
|
|
|
ctx->sum_delta_pts + ctx->last_pkt_duration :
|
|
|
|
ctx->frame_cnt * ctx->sum_delta_pts / (ctx->frame_cnt - 1));
|
2019-10-02 13:04:59 +00:00
|
|
|
avio_wl32(pb, 0); // zero out unused bytes
|
2015-07-12 10:47:20 +00:00
|
|
|
avio_seek(pb, end, SEEK_SET);
|
|
|
|
}
|
2011-01-17 20:11:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:05:20 +00:00
|
|
|
static const AVCodecTag codec_ivf_tags[] = {
|
|
|
|
{ AV_CODEC_ID_VP8, MKTAG('V', 'P', '8', '0') },
|
|
|
|
{ AV_CODEC_ID_VP9, MKTAG('V', 'P', '9', '0') },
|
2017-12-10 23:28:50 +00:00
|
|
|
{ AV_CODEC_ID_AV1, MKTAG('A', 'V', '0', '1') },
|
2016-09-09 15:05:20 +00:00
|
|
|
{ AV_CODEC_ID_NONE, 0 }
|
|
|
|
};
|
|
|
|
|
2021-04-19 17:45:24 +00:00
|
|
|
const AVOutputFormat ff_ivf_muxer = {
|
2015-07-12 10:47:20 +00:00
|
|
|
.priv_data_size = sizeof(IVFEncContext),
|
2012-04-06 14:50:48 +00:00
|
|
|
.name = "ivf",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("On2 IVF"),
|
|
|
|
.extensions = "ivf",
|
2012-08-05 09:11:04 +00:00
|
|
|
.audio_codec = AV_CODEC_ID_NONE,
|
|
|
|
.video_codec = AV_CODEC_ID_VP8,
|
2020-05-10 13:49:15 +00:00
|
|
|
.init = ivf_init,
|
2011-01-17 20:11:51 +00:00
|
|
|
.write_header = ivf_write_header,
|
|
|
|
.write_packet = ivf_write_packet,
|
2015-07-12 10:47:20 +00:00
|
|
|
.write_trailer = ivf_write_trailer,
|
2016-09-09 15:05:20 +00:00
|
|
|
.codec_tag = (const AVCodecTag* const []){ codec_ivf_tags, 0 },
|
2011-01-17 20:11:51 +00:00
|
|
|
};
|