yading@11: /* yading@11: * Copyright (C) 2008 David Conrad yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "libavcodec/get_bits.h" yading@11: #include "libavcodec/dirac.h" yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "oggdec.h" yading@11: yading@11: static int dirac_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: dirac_source_params source; yading@11: GetBitContext gb; yading@11: yading@11: // already parsed the header yading@11: if (st->codec->codec_id == AV_CODEC_ID_DIRAC) yading@11: return 0; yading@11: yading@11: init_get_bits(&gb, os->buf + os->pstart + 13, (os->psize - 13) * 8); yading@11: if (avpriv_dirac_parse_sequence_header(st->codec, &gb, &source) < 0) yading@11: return -1; yading@11: yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_DIRAC; yading@11: // dirac in ogg always stores timestamps as though the video were interlaced yading@11: avpriv_set_pts_info(st, 64, st->codec->time_base.num, 2*st->codec->time_base.den); yading@11: return 1; yading@11: } yading@11: yading@11: // various undocument things: granule is signed (only for dirac!) yading@11: static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule, yading@11: int64_t *dts_out) yading@11: { yading@11: int64_t gp = granule; yading@11: struct ogg *ogg = s->priv_data; yading@11: struct ogg_stream *os = ogg->streams + idx; yading@11: yading@11: unsigned dist = ((gp >> 14) & 0xff00) | (gp & 0xff); yading@11: int64_t dts = (gp >> 31); yading@11: int64_t pts = dts + ((gp >> 9) & 0x1fff); yading@11: yading@11: if (!dist) yading@11: os->pflags |= AV_PKT_FLAG_KEY; yading@11: yading@11: if (dts_out) yading@11: *dts_out = dts; yading@11: yading@11: return pts; yading@11: } yading@11: yading@11: static int old_dirac_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: uint8_t *buf = os->buf + os->pstart; yading@11: yading@11: if (buf[0] != 'K') yading@11: return 0; yading@11: yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_DIRAC; yading@11: avpriv_set_pts_info(st, 64, AV_RB32(buf+12), AV_RB32(buf+8)); yading@11: return 1; yading@11: } yading@11: yading@11: static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp, yading@11: int64_t *dts) yading@11: { yading@11: struct ogg *ogg = s->priv_data; yading@11: struct ogg_stream *os = ogg->streams + idx; yading@11: uint64_t iframe = gp >> 30; yading@11: uint64_t pframe = gp & 0x3fffffff; yading@11: yading@11: if (!pframe) yading@11: os->pflags |= AV_PKT_FLAG_KEY; yading@11: yading@11: return iframe + pframe; yading@11: } yading@11: yading@11: const struct ogg_codec ff_dirac_codec = { yading@11: .magic = "BBCD\0", yading@11: .magicsize = 5, yading@11: .header = dirac_header, yading@11: .gptopts = dirac_gptopts, yading@11: .granule_is_start = 1, yading@11: .nb_header = 1, yading@11: }; yading@11: yading@11: const struct ogg_codec ff_old_dirac_codec = { yading@11: .magic = "KW-DIRAC", yading@11: .magicsize = 8, yading@11: .header = old_dirac_header, yading@11: .gptopts = old_dirac_gptopts, yading@11: .granule_is_start = 1, yading@11: .nb_header = 1, yading@11: };