5a907ec17816cee86ab2624d1a2e74a099ace676
[libav.git] / libavformat / oggparsedirac.c
1 /*
2 * Copyright (C) 2008 David Conrad
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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.
10 *
11 * FFmpeg 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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavcodec/get_bits.h"
22 #include "libavcodec/dirac.h"
23 #include "avformat.h"
24 #include "oggdec.h"
25
26 static int dirac_header(AVFormatContext *s, int idx)
27 {
28 struct ogg *ogg = s->priv_data;
29 struct ogg_stream *os = ogg->streams + idx;
30 AVStream *st = s->streams[idx];
31 dirac_source_params source;
32 GetBitContext gb;
33
34 // already parsed the header
35 if (st->codec->codec_id == CODEC_ID_DIRAC)
36 return 0;
37
38 init_get_bits(&gb, os->buf + os->pstart + 13, (os->psize - 13) * 8);
39 if (ff_dirac_parse_sequence_header(st->codec, &gb, &source) < 0)
40 return -1;
41
42 st->codec->codec_type = CODEC_TYPE_VIDEO;
43 st->codec->codec_id = CODEC_ID_DIRAC;
44 // dirac in ogg always stores timestamps as though the video were interlaced
45 st->time_base = (AVRational){st->codec->time_base.num, 2*st->codec->time_base.den};
46 return 1;
47 }
48
49 // various undocument things: granule is signed (only for dirac!)
50 static uint64_t dirac_gptopts(AVFormatContext *s, int idx, int64_t gp,
51 int64_t *dts_out)
52 {
53 struct ogg *ogg = s->priv_data;
54 struct ogg_stream *os = ogg->streams + idx;
55
56 unsigned dist = ((gp >> 14) & 0xff00) | (gp & 0xff);
57 int64_t dts = (gp >> 31);
58 int64_t pts = dts + ((gp >> 9) & 0x1fff);
59
60 if (!dist)
61 os->pflags |= PKT_FLAG_KEY;
62
63 if (dts_out)
64 *dts_out = dts;
65
66 return pts;
67 }
68
69 static int old_dirac_header(AVFormatContext *s, int idx)
70 {
71 struct ogg *ogg = s->priv_data;
72 struct ogg_stream *os = ogg->streams + idx;
73 AVStream *st = s->streams[idx];
74 uint8_t *buf = os->buf + os->pstart;
75
76 if (buf[0] != 'K')
77 return 0;
78
79 st->codec->codec_type = CODEC_TYPE_VIDEO;
80 st->codec->codec_id = CODEC_ID_DIRAC;
81 st->time_base.den = AV_RB32(buf+8);
82 st->time_base.num = AV_RB32(buf+12);
83 return 1;
84 }
85
86 static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp,
87 int64_t *dts)
88 {
89 struct ogg *ogg = s->priv_data;
90 struct ogg_stream *os = ogg->streams + idx;
91 uint64_t iframe = gp >> 30;
92 uint64_t pframe = gp & 0x3fffffff;
93
94 if (!pframe)
95 os->pflags |= PKT_FLAG_KEY;
96
97 return iframe + pframe;
98 }
99
100 const struct ogg_codec ff_dirac_codec = {
101 .magic = "BBCD\0",
102 .magicsize = 5,
103 .header = dirac_header,
104 .gptopts = dirac_gptopts,
105 .granule_is_start = 1,
106 };
107
108 const struct ogg_codec ff_old_dirac_codec = {
109 .magic = "KW-DIRAC",
110 .magicsize = 8,
111 .header = old_dirac_header,
112 .gptopts = old_dirac_gptopts,
113 .granule_is_start = 1,
114 };