* Ogg bitstream support
* Luca Barbato <lu_zero@gentoo.org>
* Based on tcvp implementation
- *
+ *
*/
/**
static ogg_codec_t *ogg_codecs[] = {
&vorbis_codec,
+ &theora_codec,
+ &flac_codec,
+ &ogm_video_codec,
+ &ogm_audio_codec,
+ &ogm_old_codec,
NULL
};
-#if 0 // CONFIG_ENCODERS
+#if 0 // CONFIG_MUXERS
static int
ogg_write_header (AVFormatContext * avfcontext)
{
}
-static AVOutputFormat ogg_oformat = {
+AVOutputFormat ogg_muxer = {
"ogg",
"Ogg Vorbis",
"audio/x-vorbis",
ogg_write_packet,
ogg_write_trailer,
};
-#endif //CONFIG_ENCODERS
+#endif //CONFIG_MUXERS
//FIXME We could avoid some structure duplication
static int
os = ogg->streams + idx;
os->serial = serial;
os->bufsize = DECODER_BUFFER_SIZE;
- os->buf = av_malloc (os->bufsize);
- memset (os->buf, 0, os->bufsize);
+ os->buf = av_malloc(os->bufsize);
os->header = -1;
st = av_new_stream (s, idx);
return AVERROR_NOMEM;
av_set_pts_info(st, 64, 1, 1000000);
- st->start_time = 0;
return idx;
}
+static int
+ogg_new_buf(ogg_t *ogg, int idx)
+{
+ ogg_stream_t *os = ogg->streams + idx;
+ uint8_t *nb = av_malloc(os->bufsize);
+ int size = os->bufpos - os->pstart;
+ if(os->buf){
+ memcpy(nb, os->buf + os->pstart, size);
+ av_free(os->buf);
+ }
+ os->buf = nb;
+ os->bufpos = size;
+ os->pstart = 0;
+
+ return 0;
+}
+
static int
ogg_read_page (AVFormatContext * s, int *str)
{
uint32_t seq;
uint32_t crc;
int size, idx;
- char sync[4];
+ uint8_t sync[4];
int sp = 0;
if (get_buffer (bc, sync, 4) < 4)
os = ogg->streams + idx;
+ if(os->psize > 0)
+ ogg_new_buf(ogg, idx);
+
if (get_buffer (bc, os->segments, nsegs) < nsegs)
return -1;
if (os->bufsize - os->bufpos < size){
uint8_t *nb = av_malloc (os->bufsize *= 2);
- memset (nb, 0, os->bufsize);
memcpy (nb, os->buf, os->bufpos);
av_free (os->buf);
os->buf = nb;
}
static int
-ogg_packet (AVFormatContext * s, int *str)
+ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize)
{
ogg_t *ogg = s->priv_data;
int idx;
}
if (!complete && os->segp == os->nsegs){
- uint8_t *nb = av_malloc (os->bufsize);
- int size = os->bufpos - os->pstart;
- memset (nb, 0, os->bufsize);
- memcpy (nb, os->buf + os->pstart, size);
- av_free (os->buf);
- os->buf = nb;
- os->bufpos = size;
- os->pstart = 0;
ogg->curidx = -1;
}
}while (!complete);
os->codec->packet (s, idx);
if (str)
*str = idx;
+ if (dstart)
+ *dstart = os->pstart;
+ if (dsize)
+ *dsize = os->psize;
+ os->pstart += os->psize;
+ os->psize = 0;
}
os->seq++;
ogg_t *ogg = s->priv_data;
do{
- if (ogg_packet (s, NULL) < 0)
+ if (ogg_packet (s, NULL, NULL, NULL) < 0)
return -1;
}while (!ogg->headers);
static uint64_t
ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
{
- AVStream *st = s->streams[i];
- AVCodecContext *codec = &st->codec;
+ ogg_t *ogg = s->priv_data;
+ ogg_stream_t *os = ogg->streams + i;
uint64_t pts = AV_NOPTS_VALUE;
- if (codec->codec_type == CODEC_TYPE_AUDIO){
- pts = gp * 1000000LL / codec->sample_rate;
- }else if (codec->codec_type == CODEC_TYPE_VIDEO){
-//FIXME
- pts = gp * 1000000LL / codec->sample_rate;
-// pts = gp * st->video.frame_rate.den * 27000000LL /
-// st->video.frame_rate.num;
+ if(os->codec->gptopts){
+ pts = os->codec->gptopts(s, i, gp);
+ } else {
+ pts = gp;
}
return pts;
ogg_get_length (AVFormatContext * s)
{
ogg_t *ogg = s->priv_data;
- URLContext *h = url_fileno (&s->pb);
int idx = -1, i;
-//FIXME: get the right ctx flag to know if is seekable or not
-// if(ogg->f->flags & URL_FLAG_STREAMED)
-// return 0;
+ offset_t size, end;
+
+ if(s->pb.is_streamed)
+ return 0;
// already set
if (s->duration != AV_NOPTS_VALUE)
return 0;
+ size = url_fsize(&s->pb);
+ if(size < 0)
+ return 0;
+ end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: size;
+
ogg_save (s);
- url_seek (h, -MAX_PAGE_SIZE, SEEK_END);
+ url_fseek (&s->pb, end, SEEK_SET);
while (!ogg_read_page (s, &i)){
if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
ogg_gptopts (s, idx, ogg->streams[idx].granule);
}
- ogg->size = url_filesize(h);
+ ogg->size = size;
+ ogg_restore (s, 0);
+ ogg_save (s);
+ while (ogg_read_page (s, &i)) {
+ if (i == idx && ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
+ break;
+ }
+ if (i == idx) {
+ s->streams[idx]->start_time = ogg_gptopts (s, idx, ogg->streams[idx].granule);
+ s->streams[idx]->duration -= s->streams[idx]->start_time;
+ }
ogg_restore (s, 0);
return 0;
ogg_t *ogg;
ogg_stream_t *os;
int idx = -1;
+ int pstart, psize;
- //Get an ogg packet
+ //Get an ogg packet
do{
- if (ogg_packet (s, &idx) < 0)
+ if (ogg_packet (s, &idx, &pstart, &psize) < 0)
return AVERROR_IO;
}while (idx < 0 || !s->streams[idx]);
os = ogg->streams + idx;
//Alloc a pkt
- if (av_new_packet (pkt, os->psize) < 0)
+ if (av_new_packet (pkt, psize) < 0)
return AVERROR_IO;
pkt->stream_index = idx;
- memcpy (pkt->data, os->buf + os->pstart, os->psize);
+ memcpy (pkt->data, os->buf + pstart, psize);
if (os->lastgp != -1LL){
pkt->pts = ogg_gptopts (s, idx, os->lastgp);
os->lastgp = -1;
}
- //next
- os->pstart += os->psize;
- os->psize = 0;
- return os->psize;
+
+ return psize;
}
for (i = 0; i < ogg->nstreams; i++){
av_free (ogg->streams[i].buf);
- av_freep (&s->streams[i]->codec.extradata);
+ av_free (ogg->streams[i].private);
}
av_free (ogg->streams);
return 0;
ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
int flags)
{
+ AVStream *st = s->streams[stream_index];
ogg_t *ogg = s->priv_data;
ByteIOContext *bc = &s->pb;
uint64_t min = 0, max = ogg->size;
- uint64_t tmin = 0, tmax = s->duration;
+ uint64_t tmin = st->start_time, tmax = st->start_time + st->duration;
int64_t pts = AV_NOPTS_VALUE;
ogg_save (s);
- while (min <= max){
+ if ((uint64_t)target_ts < tmin || target_ts < 0)
+ target_ts = tmin;
+ while (min <= max && tmin < tmax){
uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
int i = -1;
url_fseek (bc, p, SEEK_SET);
while (!ogg_read_page (s, &i)){
- if (ogg->streams[i].granule != 0 && ogg->streams[i].granule != -1)
+ if (i == stream_index && ogg->streams[i].granule != 0 &&
+ ogg->streams[i].granule != -1)
break;
}
pts = ogg_gptopts (s, i, ogg->streams[i].granule);
p = url_ftell (bc);
- if (ABS (pts - target_ts) < 1000000LL)
+ if (ABS (pts - target_ts) * st->time_base.num < st->time_base.den)
break;
if (pts > target_ts){
+ if (max == p && tmax == pts) {
+ // probably our tmin is wrong, causing us to always end up too late in the file
+ tmin = (target_ts + tmin + 1) / 2;
+ if (tmin == target_ts) {
+ url_fseek(bc, min, SEEK_SET);
+ break;
+ }
+ }
max = p;
tmax = pts;
}else{
+ if (min == p && tmin == pts) {
+ // probably our tmax is wrong, causing us to always end up too early in the file
+ tmax = (target_ts + tmax) / 2;
+ if (tmax == target_ts) {
+ url_fseek(bc, max, SEEK_SET);
+ break;
+ }
+ }
min = p;
tmin = pts;
}
}
- if (ABS (pts - target_ts) < 1000000LL){
+ if (ABS (pts - target_ts) * st->time_base.num < st->time_base.den){
ogg_restore (s, 1);
ogg_reset (ogg);
}else{
pts = AV_NOPTS_VALUE;
}
- return pts;
+ av_update_cur_dts(s, st, pts);
+ return 0;
#if 0
//later...
}
#endif
-static AVInputFormat ogg_iformat = {
+static int ogg_probe(AVProbeData *p)
+{
+ if (p->buf_size < 6)
+ return 0;
+ if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
+ p->buf[2] == 'g' && p->buf[3] == 'S' &&
+ p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
+ return AVPROBE_SCORE_MAX;
+ else
+ return 0;
+}
+
+AVInputFormat ogg_demuxer = {
"ogg",
"Ogg",
sizeof (ogg_t),
- NULL,
+ ogg_probe,
ogg_read_header,
ogg_read_packet,
ogg_read_close,
ogg_read_seek,
-// ogg_read_timestamp,
+// ogg_read_timestamp,
.extensions = "ogg",
};
-
-int
-ogg_init (void)
-{
-#if 0 // CONFIG_ENCODERS
- av_register_output_format (&ogg_oformat);
-#endif
- av_register_input_format (&ogg_iformat);
- return 0;
-}