unsigned page_count; ///< number of page buffered
OGGPage page; ///< current page
unsigned serial_num; ///< serial number
+ int64_t last_granule; ///< last packet granule
} OGGStreamContext;
typedef struct OGGPageList {
put_buffer(s->pb, buf, size);
put_flush_packet(s->pb);
+ av_free(buf);
oggstream->page_count--;
return 0;
}
-static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, OGGPage *page)
+static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
{
if (oggstream->kfgshift)
- return (page->granule>>oggstream->kfgshift) +
- (page->granule & ((1<<oggstream->kfgshift)-1));
+ return (granule>>oggstream->kfgshift) +
+ (granule & ((1<<oggstream->kfgshift)-1));
else
- return page->granule;
+ return granule;
}
static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
if (next->granule == -1 || page->granule == -1)
return 0;
- next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next),
+ next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
st2->time_base, AV_TIME_BASE_Q);
- cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page),
+ cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
st ->time_base, AV_TIME_BASE_Q);
return next_granule > cur_granule;
}
OGGStreamContext *oggstream = st->priv_data;
int total_segments = size / 255 + 1;
uint8_t *p = data;
- int i, segments, len;
+ int i, segments, len, flush = 0;
+
+ // Handles VFR by flushing page because this frame needs to have a timestamp
+ if (st->codec->codec_id == CODEC_ID_THEORA &&
+ ogg_granule_to_timestamp(oggstream, granule) >
+ ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1) {
+ if (oggstream->page.granule != -1)
+ ogg_buffer_page(s, oggstream);
+ flush = 1;
+ }
for (i = 0; i < total_segments; ) {
OGGPage *page = &oggstream->page;
ogg_buffer_page(s, oggstream);
}
}
+
+ if (flush && oggstream->page.granule != -1)
+ ogg_buffer_page(s, oggstream);
+
return 0;
}
static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
- int *header_len, AVMetadata *m)
+ int *header_len, AVMetadata **m, int framing_bit)
{
const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
int size;
uint8_t *p, *p0;
unsigned int count;
- size = offset + ff_vorbiscomment_length(m, vendor, &count);
+ ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
+
+ size = offset + ff_vorbiscomment_length(*m, vendor, &count) + framing_bit;
p = av_mallocz(size);
if (!p)
return NULL;
p += offset;
ff_vorbiscomment_write(&p, m, vendor, count);
+ if (framing_bit)
+ bytestream_put_byte(&p, 1);
*header_len = size;
return p0;
static int ogg_build_flac_headers(AVCodecContext *avctx,
OGGStreamContext *oggstream, int bitexact,
- AVMetadata *m)
+ AVMetadata **m)
{
enum FLACExtradataFormat format;
uint8_t *streaminfo;
bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
// second packet: VorbisComment
- p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m);
+ p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
if (!p)
return AVERROR(ENOMEM);
oggstream->header[1] = p;
static int ogg_build_speex_headers(AVCodecContext *avctx,
OGGStreamContext *oggstream, int bitexact,
- AVMetadata *m)
+ AVMetadata **m)
{
uint8_t *p;
AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
// second packet: VorbisComment
- p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m);
+ p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
if (!p)
return AVERROR(ENOMEM);
oggstream->header[1] = p;
if (st->codec->codec_id == CODEC_ID_FLAC) {
int err = ogg_build_flac_headers(st->codec, oggstream,
st->codec->flags & CODEC_FLAG_BITEXACT,
- s->metadata);
+ &s->metadata);
if (err) {
av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
av_freep(&st->priv_data);
} else if (st->codec->codec_id == CODEC_ID_SPEEX) {
int err = ogg_build_speex_headers(st->codec, oggstream,
st->codec->flags & CODEC_FLAG_BITEXACT,
- s->metadata);
+ &s->metadata);
if (err) {
av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
av_freep(&st->priv_data);
return err;
}
} else {
+ uint8_t *p;
+ const char *cstr = st->codec->codec_id == CODEC_ID_VORBIS ? "vorbis" : "theora";
+ int header_type = st->codec->codec_id == CODEC_ID_VORBIS ? 3 : 0x81;
+ int framing_bit = st->codec->codec_id == CODEC_ID_VORBIS ? 1 : 0;
+
if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
oggstream->header, oggstream->header_len) < 0) {
av_freep(&st->priv_data);
return -1;
}
+
+ p = ogg_write_vorbiscomment(7, st->codec->flags & CODEC_FLAG_BITEXACT,
+ &oggstream->header_len[1], &s->metadata,
+ framing_bit);
+ if (!p)
+ return AVERROR(ENOMEM);
+
+ oggstream->header[1] = p;
+ bytestream_put_byte(&p, header_type);
+ bytestream_put_buffer(&p, cstr, 6);
+
if (st->codec->codec_id == CODEC_ID_THEORA) {
/** 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 */
ogg_write_pages(s, 0);
+ oggstream->last_granule = granule;
+
return 0;
}
ogg_write_header,
ogg_write_packet,
ogg_write_trailer,
- .metadata_conv = ff_vorbiscomment_metadata_conv,
};