Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavformat / oggparsedirac.c
blob55a0b59127ed5a0297bbd607fd50fe20b9341550
1 /*
2 * Copyright (C) 2008 David Conrad
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavcodec/get_bits.h"
22 #include "libavcodec/dirac.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "oggdec.h"
27 static int dirac_header(AVFormatContext *s, int idx)
29 struct ogg *ogg = s->priv_data;
30 struct ogg_stream *os = ogg->streams + idx;
31 AVStream *st = s->streams[idx];
32 dirac_source_params source;
33 GetBitContext gb;
35 // already parsed the header
36 if (st->codec->codec_id == AV_CODEC_ID_DIRAC)
37 return 0;
39 init_get_bits(&gb, os->buf + os->pstart + 13, (os->psize - 13) * 8);
40 if (avpriv_dirac_parse_sequence_header(st->codec, &gb, &source) < 0)
41 return -1;
43 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
44 st->codec->codec_id = AV_CODEC_ID_DIRAC;
45 // dirac in ogg always stores timestamps as though the video were interlaced
46 avpriv_set_pts_info(st, 64, st->codec->time_base.num, 2*st->codec->time_base.den);
47 return 1;
50 // various undocument things: granule is signed (only for dirac!)
51 static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule,
52 int64_t *dts_out)
54 int64_t gp = granule;
55 struct ogg *ogg = s->priv_data;
56 struct ogg_stream *os = ogg->streams + idx;
58 unsigned dist = ((gp >> 14) & 0xff00) | (gp & 0xff);
59 int64_t dts = (gp >> 31);
60 int64_t pts = dts + ((gp >> 9) & 0x1fff);
62 if (!dist)
63 os->pflags |= AV_PKT_FLAG_KEY;
65 if (dts_out)
66 *dts_out = dts;
68 return pts;
71 static int old_dirac_header(AVFormatContext *s, int idx)
73 struct ogg *ogg = s->priv_data;
74 struct ogg_stream *os = ogg->streams + idx;
75 AVStream *st = s->streams[idx];
76 uint8_t *buf = os->buf + os->pstart;
78 if (buf[0] != 'K')
79 return 0;
81 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
82 st->codec->codec_id = AV_CODEC_ID_DIRAC;
83 avpriv_set_pts_info(st, 64, AV_RB32(buf+12), AV_RB32(buf+8));
84 return 1;
87 static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp,
88 int64_t *dts)
90 struct ogg *ogg = s->priv_data;
91 struct ogg_stream *os = ogg->streams + idx;
92 uint64_t iframe = gp >> 30;
93 uint64_t pframe = gp & 0x3fffffff;
95 if (!pframe)
96 os->pflags |= AV_PKT_FLAG_KEY;
98 return iframe + pframe;
101 const struct ogg_codec ff_dirac_codec = {
102 .magic = "BBCD\0",
103 .magicsize = 5,
104 .header = dirac_header,
105 .gptopts = dirac_gptopts,
106 .granule_is_start = 1,
107 .nb_header = 1,
110 const struct ogg_codec ff_old_dirac_codec = {
111 .magic = "KW-DIRAC",
112 .magicsize = 8,
113 .header = old_dirac_header,
114 .gptopts = old_dirac_gptopts,
115 .granule_is_start = 1,
116 .nb_header = 1,