yading@11: /** yading@11: Copyright (C) 2005 Matthieu CASTET, Alex Beregszaszi yading@11: yading@11: Permission is hereby granted, free of charge, to any person yading@11: obtaining a copy of this software and associated documentation yading@11: files (the "Software"), to deal in the Software without yading@11: restriction, including without limitation the rights to use, copy, yading@11: modify, merge, publish, distribute, sublicense, and/or sell copies yading@11: of the Software, and to permit persons to whom the Software is yading@11: furnished to do so, subject to the following conditions: yading@11: yading@11: The above copyright notice and this permission notice shall be yading@11: included in all copies or substantial portions of the Software. yading@11: yading@11: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, yading@11: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF yading@11: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND yading@11: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT yading@11: HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, yading@11: WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, yading@11: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER yading@11: DEALINGS IN THE SOFTWARE. yading@11: **/ yading@11: yading@11: #include yading@11: #include "libavutil/bswap.h" yading@11: #include "libavcodec/get_bits.h" yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "oggdec.h" yading@11: yading@11: struct theora_params { yading@11: int gpshift; yading@11: int gpmask; yading@11: unsigned version; yading@11: }; yading@11: yading@11: static int yading@11: theora_header (AVFormatContext * s, int idx) yading@11: { yading@11: struct ogg *ogg = s->priv_data; yading@11: struct ogg_stream *os = ogg->streams + idx; yading@11: AVStream *st = s->streams[idx]; yading@11: struct theora_params *thp = os->private; yading@11: int cds = st->codec->extradata_size + os->psize + 2; yading@11: uint8_t *cdp; yading@11: yading@11: if(!(os->buf[os->pstart] & 0x80)) yading@11: return 0; yading@11: yading@11: if(!thp){ yading@11: thp = av_mallocz(sizeof(*thp)); yading@11: os->private = thp; yading@11: } yading@11: yading@11: switch (os->buf[os->pstart]) { yading@11: case 0x80: { yading@11: GetBitContext gb; yading@11: int width, height; yading@11: AVRational timebase; yading@11: yading@11: init_get_bits(&gb, os->buf + os->pstart, os->psize*8); yading@11: yading@11: skip_bits_long(&gb, 7*8); /* 0x80"theora" */ yading@11: yading@11: thp->version = get_bits_long(&gb, 24); yading@11: if (thp->version < 0x030100) yading@11: { yading@11: av_log(s, AV_LOG_ERROR, yading@11: "Too old or unsupported Theora (%x)\n", thp->version); yading@11: return -1; yading@11: } yading@11: yading@11: width = get_bits(&gb, 16) << 4; yading@11: height = get_bits(&gb, 16) << 4; yading@11: avcodec_set_dimensions(st->codec, width, height); yading@11: yading@11: if (thp->version >= 0x030400) yading@11: skip_bits(&gb, 100); yading@11: yading@11: if (thp->version >= 0x030200) { yading@11: width = get_bits_long(&gb, 24); yading@11: height = get_bits_long(&gb, 24); yading@11: if ( width <= st->codec->width && width > st->codec->width-16 yading@11: && height <= st->codec->height && height > st->codec->height-16) yading@11: avcodec_set_dimensions(st->codec, width, height); yading@11: yading@11: skip_bits(&gb, 16); yading@11: } yading@11: timebase.den = get_bits_long(&gb, 32); yading@11: timebase.num = get_bits_long(&gb, 32); yading@11: if (!(timebase.num > 0 && timebase.den > 0)) { yading@11: av_log(s, AV_LOG_WARNING, "Invalid time base in theora stream, assuming 25 FPS\n"); yading@11: timebase.num = 1; yading@11: timebase.den = 25; yading@11: } yading@11: avpriv_set_pts_info(st, 64, timebase.num, timebase.den); yading@11: yading@11: st->sample_aspect_ratio.num = get_bits_long(&gb, 24); yading@11: st->sample_aspect_ratio.den = get_bits_long(&gb, 24); yading@11: yading@11: if (thp->version >= 0x030200) yading@11: skip_bits_long(&gb, 38); yading@11: if (thp->version >= 0x304000) yading@11: skip_bits(&gb, 2); yading@11: yading@11: thp->gpshift = get_bits(&gb, 5); yading@11: thp->gpmask = (1 << thp->gpshift) - 1; yading@11: yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_THEORA; yading@11: st->need_parsing = AVSTREAM_PARSE_HEADERS; yading@11: } yading@11: break; yading@11: case 0x81: yading@11: ff_vorbis_comment(s, &st->metadata, os->buf + os->pstart + 7, os->psize - 7); yading@11: case 0x82: yading@11: if (!thp->version) yading@11: return -1; yading@11: break; yading@11: default: yading@11: av_log(s, AV_LOG_ERROR, "Unknown header type %X\n", os->buf[os->pstart]); yading@11: return -1; yading@11: } yading@11: yading@11: st->codec->extradata = av_realloc (st->codec->extradata, yading@11: cds + FF_INPUT_BUFFER_PADDING_SIZE); yading@11: cdp = st->codec->extradata + st->codec->extradata_size; yading@11: *cdp++ = os->psize >> 8; yading@11: *cdp++ = os->psize & 0xff; yading@11: memcpy (cdp, os->buf + os->pstart, os->psize); yading@11: st->codec->extradata_size = cds; yading@11: yading@11: return 1; yading@11: } yading@11: yading@11: static uint64_t yading@11: theora_gptopts(AVFormatContext *ctx, int idx, uint64_t gp, int64_t *dts) yading@11: { yading@11: struct ogg *ogg = ctx->priv_data; yading@11: struct ogg_stream *os = ogg->streams + idx; yading@11: struct theora_params *thp = os->private; yading@11: uint64_t iframe, pframe; yading@11: yading@11: if (!thp) yading@11: return AV_NOPTS_VALUE; yading@11: yading@11: iframe = gp >> thp->gpshift; yading@11: pframe = gp & thp->gpmask; yading@11: yading@11: if (thp->version < 0x030201) yading@11: iframe++; yading@11: yading@11: if(!pframe) yading@11: os->pflags |= AV_PKT_FLAG_KEY; yading@11: yading@11: if (dts) yading@11: *dts = iframe + pframe; yading@11: yading@11: return iframe + pframe; yading@11: } yading@11: yading@11: static int theora_packet(AVFormatContext *s, int idx) yading@11: { yading@11: struct ogg *ogg = s->priv_data; yading@11: struct ogg_stream *os = ogg->streams + idx; yading@11: int duration; yading@11: yading@11: /* first packet handling yading@11: here we parse the duration of each packet in the first page and compare yading@11: the total duration to the page granule to find the encoder delay and yading@11: set the first timestamp */ yading@11: yading@11: if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) { yading@11: int seg; yading@11: yading@11: duration = 1; yading@11: for (seg = os->segp; seg < os->nsegs; seg++) { yading@11: if (os->segments[seg] < 255) yading@11: duration ++; yading@11: } yading@11: yading@11: os->lastpts = os->lastdts = theora_gptopts(s, idx, os->granule, NULL) - duration; yading@11: if(s->streams[idx]->start_time == AV_NOPTS_VALUE) { yading@11: s->streams[idx]->start_time = os->lastpts; yading@11: if (s->streams[idx]->duration) yading@11: s->streams[idx]->duration -= s->streams[idx]->start_time; yading@11: } yading@11: } yading@11: yading@11: /* parse packet duration */ yading@11: if (os->psize > 0) { yading@11: os->pduration = 1; yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: const struct ogg_codec ff_theora_codec = { yading@11: .magic = "\200theora", yading@11: .magicsize = 7, yading@11: .header = theora_header, yading@11: .packet = theora_packet, yading@11: .gptopts = theora_gptopts, yading@11: .nb_header = 3, yading@11: };