1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +02:00

avformat/oggenc: add vp8 muxing support

Addresses ticket #5687

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2016-07-20 22:29:54 -03:00
parent 66408fce49
commit 120f34b6ac
5 changed files with 98 additions and 10 deletions

View File

@ -6,6 +6,7 @@ version <next>:
- tee protocol
- Changed metadata print option to accept general urls
- Alias muxer for Ogg Video (.ogv)
- VP8 in Ogg muxing
version 3.1:

View File

@ -54,6 +54,8 @@ typedef struct OGGStreamContext {
int kfgshift;
int64_t last_kf_pts;
int vrev;
/* for VP8 granule */
int isvp8;
int eos;
unsigned page_count; ///< number of page buffered
OGGPage page; ///< current page
@ -146,7 +148,8 @@ static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
{
return oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1));
return (oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1))) ||
(oggstream->isvp8 && !((granule >> 3) & 0x07ffffff));
}
static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
@ -154,6 +157,8 @@ static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t gra
if (oggstream->kfgshift)
return (granule>>oggstream->kfgshift) +
(granule & ((1<<oggstream->kfgshift)-1));
else if (oggstream->isvp8)
return granule >> 32;
else
return granule;
}
@ -219,11 +224,11 @@ static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
int i, segments, len, flush = 0;
// Handles VFR by flushing page because this frame needs to have a timestamp
// For theora, keyframes also need to have a timestamp to correctly mark
// For theora and VP8, keyframes also need to have a timestamp to correctly mark
// them as such, otherwise seeking will not work correctly at the very
// least with old libogg versions.
// Do not try to flush header packets though, that will create broken files.
if (st->codecpar->codec_id == AV_CODEC_ID_THEORA && !header &&
if ((st->codecpar->codec_id == AV_CODEC_ID_THEORA || st->codecpar->codec_id == AV_CODEC_ID_VP8) && !header &&
(ogg_granule_to_timestamp(oggstream, granule) >
ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
ogg_key_granule(oggstream, granule))) {
@ -405,6 +410,57 @@ static int ogg_build_opus_headers(AVCodecParameters *par,
return 0;
}
#define VP8_HEADER_SIZE 26
static int ogg_build_vp8_headers(AVFormatContext *s, AVStream *st,
OGGStreamContext *oggstream, int bitexact)
{
AVCodecParameters *par = st->codecpar;
uint8_t *p;
/* first packet: VP8 header */
p = av_mallocz(VP8_HEADER_SIZE);
if (!p)
return AVERROR(ENOMEM);
oggstream->header[0] = p;
oggstream->header_len[0] = VP8_HEADER_SIZE;
bytestream_put_byte(&p, 0x4f); // HDRID
bytestream_put_buffer(&p, "VP80", 4); // Identifier
bytestream_put_byte(&p, 1); // HDRTYP
bytestream_put_byte(&p, 1); // VMAJ
bytestream_put_byte(&p, 0); // VMIN
bytestream_put_be16(&p, par->width);
bytestream_put_be16(&p, par->height);
bytestream_put_be24(&p, par->sample_aspect_ratio.num);
bytestream_put_be24(&p, par->sample_aspect_ratio.den);
if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0) {
// OggVP8 requires pts to increase by 1 per visible frame, so use the least common
// multiple framerate if available.
av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
st->time_base.num, st->time_base.den,
st->r_frame_rate.den, st->r_frame_rate.num);
avpriv_set_pts_info(st, 64, st->r_frame_rate.den, st->r_frame_rate.num);
}
bytestream_put_be32(&p, st->time_base.den);
bytestream_put_be32(&p, st->time_base.num);
/* optional second packet: VorbisComment */
if (av_dict_get(st->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
p = ogg_write_vorbiscomment(7, bitexact, &oggstream->header_len[1], &st->metadata, 0);
if (!p)
return AVERROR(ENOMEM);
oggstream->header[1] = p;
bytestream_put_byte(&p, 0x4f); // HDRID
bytestream_put_buffer(&p, "VP80", 4); // Identifier
bytestream_put_byte(&p, 2); // HDRTYP
bytestream_put_byte(&p, 0x20);
}
oggstream->isvp8 = 1;
return 0;
}
static void ogg_write_pages(AVFormatContext *s, int flush)
{
OGGContext *ogg = s->priv_data;
@ -452,12 +508,14 @@ static int ogg_write_header(AVFormatContext *s)
st->codecpar->codec_id != AV_CODEC_ID_THEORA &&
st->codecpar->codec_id != AV_CODEC_ID_SPEEX &&
st->codecpar->codec_id != AV_CODEC_ID_FLAC &&
st->codecpar->codec_id != AV_CODEC_ID_OPUS) {
st->codecpar->codec_id != AV_CODEC_ID_OPUS &&
st->codecpar->codec_id != AV_CODEC_ID_VP8) {
av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
return AVERROR(EINVAL);
}
if (!st->codecpar->extradata || !st->codecpar->extradata_size) {
if ((!st->codecpar->extradata || !st->codecpar->extradata_size) &&
st->codecpar->codec_id != AV_CODEC_ID_VP8) {
av_log(s, AV_LOG_ERROR, "No extradata present\n");
return AVERROR_INVALIDDATA;
}
@ -508,6 +566,14 @@ static int ogg_write_header(AVFormatContext *s)
av_freep(&st->priv_data);
return err;
}
} else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
int err = ogg_build_vp8_headers(s, st, oggstream,
s->flags & AVFMT_FLAG_BITEXACT);
if (err) {
av_log(s, AV_LOG_ERROR, "Error writing VP8 headers\n");
av_freep(&st->priv_data);
return err;
}
} else {
uint8_t *p;
const char *cstr = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
@ -600,7 +666,18 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
av_rescale_q(st->codecpar->initial_padding,
(AVRational){ 1, st->codecpar->sample_rate },
st->time_base);
else
else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
int64_t pts, invcnt, dist;
int visible;
visible = (pkt->data[0] >> 4) & 1;
pts = pkt->pts + pkt->duration;
invcnt = (oggstream->last_granule >> 30) & 3;
invcnt = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1);
dist = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1;
granule = (pts << 32) | (invcnt << 30) | (dist << 3);
} else
granule = pkt->pts + pkt->duration;
if (oggstream->page.start_granule == AV_NOPTS_VALUE)
@ -653,7 +730,8 @@ static int ogg_write_trailer(AVFormatContext *s)
OGGStreamContext *oggstream = st->priv_data;
if (st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
st->codecpar->codec_id == AV_CODEC_ID_SPEEX ||
st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
st->codecpar->codec_id == AV_CODEC_ID_OPUS ||
st->codecpar->codec_id == AV_CODEC_ID_VP8) {
av_freep(&oggstream->header[0]);
}
av_freep(&oggstream->header[1]);
@ -686,7 +764,7 @@ AVOutputFormat ff_ogg_muxer = {
.write_header = ogg_write_header,
.write_packet = ogg_write_packet,
.write_trailer = ogg_write_trailer,
.flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
.flags = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
.priv_class = &ogg_muxer_class,
};
#endif
@ -718,11 +796,12 @@ AVOutputFormat ff_ogv_muxer = {
.priv_data_size = sizeof(OGGContext),
.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
.video_codec = AV_CODEC_ID_THEORA,
.video_codec = CONFIG_LIBTHEORA_ENCODER ?
AV_CODEC_ID_THEORA : AV_CODEC_ID_VP8,
.write_header = ogg_write_header,
.write_packet = ogg_write_packet,
.write_trailer = ogg_write_trailer,
.flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
.flags = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
.priv_class = &ogv_muxer_class,
};
#endif

View File

@ -67,6 +67,7 @@ FATE_AVCONV += $(FATE_LAVF)
fate-lavf: $(FATE_LAVF)
FATE_LAVF_FATE-$(call ALLYES, MATROSKA_DEMUXER OGG_MUXER) += ogg_vp3
FATE_LAVF_FATE-$(call ALLYES, MATROSKA_DEMUXER OGV_MUXER) += ogg_vp8
FATE_LAVF_FATE-$(call ALLYES, MOV_DEMUXER LATM_MUXER) += latm
FATE_LAVF_FATE-$(call ALLYES, MP3_DEMUXER MP3_MUXER) += mp3
FATE_LAVF_FATE-$(call ALLYES, MOV_DEMUXER MOV_MUXER) += mov_qtrle_mace6

View File

@ -165,6 +165,10 @@ DEC_OPTS="$DEC_OPTS -idct auto"
do_lavf_fate ogg "vp3/coeff_level64.mkv"
fi
if [ -n "$do_ogg_vp8" ] ; then
do_lavf_fate ogv "vp8/RRSF49-short.webm" "-acodec copy"
fi
if [ -n "$do_mov_qtrle_mace6" ] ; then
DEC_OPTS="$DEC_OPTS -idct auto"
do_lavf_fate mov "qtrle/Animation-16Greys.mov"

View File

@ -0,0 +1,3 @@
c56d8dce728d46d4f0ab4c7cc9f86abc *./tests/data/lavf-fate/lavf.ogv
95009 ./tests/data/lavf-fate/lavf.ogv
./tests/data/lavf-fate/lavf.ogv CRC=0x8c067a66