diff --git a/libavformat/Makefile b/libavformat/Makefile index 83259b2fc0..02e8a85baf 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -17,7 +17,7 @@ OBJS+=mpeg.o mpegts.o mpegtsenc.o ffm.o crc.o img.o img2.o raw.o rm.o \ yuv4mpeg.o 4xm.o flvenc.o flvdec.o movenc.o psxstr.o idroq.o ipmovie.o \ nut.o wc3movie.o mp3.o westwood.o segafilm.o idcin.o flic.o \ sierravmd.o matroska.o sol.o electronicarts.o nsvdec.o asf.o asf-enc.o \ - ogg2.o oggparsevorbis.o + ogg2.o oggparsevorbis.o oggparsetheora.o AMROBJS= ifeq ($(AMR_NB),yes) AMROBJS= amr.o diff --git a/libavformat/ogg2.c b/libavformat/ogg2.c index 89e545eb27..f2f94aa7a3 100644 --- a/libavformat/ogg2.c +++ b/libavformat/ogg2.c @@ -39,6 +39,7 @@ static ogg_codec_t *ogg_codecs[] = { &vorbis_codec, + &theora_codec, NULL }; @@ -425,17 +426,18 @@ ogg_get_headers (AVFormatContext * s) static uint64_t ogg_gptopts (AVFormatContext * s, int i, uint64_t gp) { + ogg_t *ogg = s->priv_data; + ogg_stream_t *os = ogg->streams + i; AVStream *st = s->streams[i]; AVCodecContext *codec = &st->codec; uint64_t pts = AV_NOPTS_VALUE; - if (codec->codec_type == CODEC_TYPE_AUDIO){ + if(os->codec->gptopts){ + pts = os->codec->gptopts(s, i, gp); + } else 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; + pts = gp; } return pts; @@ -534,6 +536,7 @@ ogg_read_close (AVFormatContext * s) for (i = 0; i < ogg->nstreams; i++){ av_free (ogg->streams[i].buf); + av_free (ogg->streams[i].private); av_freep (&s->streams[i]->codec.extradata); } av_free (ogg->streams); diff --git a/libavformat/ogg2.h b/libavformat/ogg2.h index f397a57197..416a504274 100644 --- a/libavformat/ogg2.h +++ b/libavformat/ogg2.h @@ -33,6 +33,7 @@ typedef struct ogg_codec { int8_t *name; int (*header)(AVFormatContext *, int); int (*packet)(AVFormatContext *, int); + uint64_t (*gptopts)(AVFormatContext *, int, uint64_t); } ogg_codec_t; typedef struct ogg_stream { @@ -49,6 +50,7 @@ typedef struct ogg_stream { int header; int nsegs, segp; uint8_t segments[255]; + void *private; } ogg_stream_t; typedef struct ogg_state { @@ -72,6 +74,7 @@ typedef struct ogg { #define OGG_FLAG_EOS 4 extern ogg_codec_t vorbis_codec; +extern ogg_codec_t theora_codec; #if 0 extern ogg_codec_t ogm_video_codec; extern ogg_codec_t ogm_audio_codec; diff --git a/libavformat/oggparsetheora.c b/libavformat/oggparsetheora.c new file mode 100644 index 0000000000..3ca5d91da8 --- /dev/null +++ b/libavformat/oggparsetheora.c @@ -0,0 +1,112 @@ +/** + Copyright (C) 2005 Matthieu CASTET + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, copy, + modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +**/ + +#include +#include "avformat.h" +#include "bitstream.h" +#include "bswap.h" +#include "ogg2.h" + +typedef struct theora_params { + int gpshift; + int gpmask; +} theora_params_t; + +static int +theora_header (AVFormatContext * s, int idx) +{ + ogg_t *ogg = s->priv_data; + ogg_stream_t *os = ogg->streams + idx; + AVStream *st = s->streams[idx]; + theora_params_t *thp = os->private; + int cds = st->codec.extradata_size + os->psize + 2; + uint8_t *cdp; + + if (os->seq > 2) + return 0; + + if(!thp){ + thp = av_mallocz(sizeof(*thp)); + os->private = thp; + } + + if (os->buf[os->pstart] == 0x80) { + GetBitContext gb; + init_get_bits(&gb, os->buf + os->pstart, os->psize*8); + + skip_bits(&gb, 7*8); /* 0x80"theora" */ + skip_bits(&gb, 3*8); + + st->codec.width = get_bits(&gb, 16) << 4; + st->codec.height = get_bits(&gb, 16) << 4; + + skip_bits(&gb, 64); + st->codec.time_base.den = get_bits(&gb, 32); + st->codec.time_base.num = get_bits(&gb, 32); + + st->codec.sample_aspect_ratio.num = get_bits(&gb, 24); + st->codec.sample_aspect_ratio.den = get_bits(&gb, 24); + + skip_bits(&gb, 38); + thp->gpshift = get_bits(&gb, 5); + thp->gpmask = (1 << thp->gpshift) - 1; + + st->codec.codec_type = CODEC_TYPE_VIDEO; + st->codec.codec_id = CODEC_ID_THEORA; + + } else if (os->buf[os->pstart] == 0x83) { + vorbis_comment (s, os->buf + os->pstart + 7, os->psize - 8); + } + + st->codec.extradata = av_realloc (st->codec.extradata, cds); + cdp = st->codec.extradata + st->codec.extradata_size; + *cdp++ = os->psize >> 8; + *cdp++ = os->psize & 0xff; + memcpy (cdp, os->buf + os->pstart, os->psize); + st->codec.extradata_size = cds; + + + return os->seq < 3; +} + +static uint64_t +theora_gptopts(AVFormatContext *ctx, int idx, uint64_t gp) +{ + AVStream *st = ctx->streams[idx]; + ogg_t *ogg = ctx->priv_data; + ogg_stream_t *os = ogg->streams + idx; + theora_params_t *thp = os->private; + uint64_t iframe = gp >> thp->gpshift; + uint64_t pframe = gp & thp->gpmask; + + return (iframe + pframe) * AV_TIME_BASE * st->codec.time_base.num / + st->codec.time_base.den; +} + +ogg_codec_t theora_codec = { + .magic = "\200theora", + .magicsize = 7, + .header = theora_header, + .gptopts = theora_gptopts +};