X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Foggenc.c;h=0713a13a7075bb716bae48fd0e59ab6d0e924d10;hb=f0b6f7253f3e69a475722b8f0b0e4289201245bf;hp=f998af39ac3f532b790bdd306c10846f0e87ddf0;hpb=cdecb39fca81967ff9e021c776b3212b1870188e;p=ffmpeg diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c index f998af39ac3..0713a13a707 100644 --- a/libavformat/oggenc.c +++ b/libavformat/oggenc.c @@ -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<kfgshift)-1)); + return (oggstream->kfgshift && !(granule & ((1<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<kfgshift)-1)); + else if (oggstream->isvp8) + return granule >> 32; else return granule; } @@ -193,7 +198,7 @@ static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream) return AVERROR(ENOMEM); l->page = oggstream->page; - oggstream->page.start_granule = oggstream->page.granule; + oggstream->page.start_granule = ogg_granule_to_timestamp(oggstream, oggstream->page.granule); oggstream->page_count++; ogg_reset_cur_page(oggstream); @@ -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))) { @@ -265,8 +270,8 @@ static int ogg_buffer_data(AVFormatContext *s, AVStream *st, int64_t start = av_rescale_q(page->start_granule, st->time_base, AV_TIME_BASE_Q); - int64_t next = av_rescale_q(page->granule, st->time_base, - AV_TIME_BASE_Q); + int64_t next = av_rescale_q(ogg_granule_to_timestamp(oggstream, page->granule), + st->time_base, AV_TIME_BASE_Q); if (page->segments_count == 255) { ogg_buffer_page(s, oggstream); @@ -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"; @@ -533,6 +599,14 @@ static int ogg_write_header(AVFormatContext *s) bytestream_put_buffer(&p, cstr, 6); if (st->codecpar->codec_id == AV_CODEC_ID_THEORA) { + int den = AV_RB32(oggstream->header[0] + 22), num = AV_RB32(oggstream->header[0] + 26); + /* Make sure to use time base stored in the Theora stream header to write + correct timestamps */ + if (st->time_base.num != num || st->time_base.den != den) { + av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n", + st->time_base.num, st->time_base.den, num, den); + avpriv_set_pts_info(st, 64, num, den); + } /** KFGSHIFT is the width of the less significant section of the granule position The less significant section is the frame count since the last keyframe */ oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5); @@ -592,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) @@ -645,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]); @@ -660,7 +746,10 @@ AVOutputFormat ff_ogg_muxer = { .name = "ogg", .long_name = NULL_IF_CONFIG_SMALL("Ogg"), .mime_type = "application/ogg", - .extensions = "ogg,ogv" + .extensions = "ogg" +#if !CONFIG_OGV_MUXER + ",ogv" +#endif #if !CONFIG_SPX_MUXER ",spx" #endif @@ -675,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 @@ -688,8 +777,7 @@ AVOutputFormat ff_oga_muxer = { .mime_type = "audio/ogg", .extensions = "oga", .priv_data_size = sizeof(OGGContext), - .audio_codec = CONFIG_LIBVORBIS_ENCODER ? - AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC, + .audio_codec = AV_CODEC_ID_FLAC, .write_header = ogg_write_header, .write_packet = ogg_write_packet, .write_trailer = ogg_write_trailer, @@ -698,6 +786,26 @@ AVOutputFormat ff_oga_muxer = { }; #endif +#if CONFIG_OGV_MUXER +OGG_CLASS(ogv, Ogg video) +AVOutputFormat ff_ogv_muxer = { + .name = "ogv", + .long_name = NULL_IF_CONFIG_SMALL("Ogg Video"), + .mime_type = "video/ogg", + .extensions = "ogv", + .priv_data_size = sizeof(OGGContext), + .audio_codec = CONFIG_LIBVORBIS_ENCODER ? + AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC, + .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_TS_NONSTRICT | AVFMT_ALLOW_FLUSH, + .priv_class = &ogv_muxer_class, +}; +#endif + #if CONFIG_SPX_MUXER OGG_CLASS(spx, Ogg Speex) AVOutputFormat ff_spx_muxer = {