]> git.sesse.net Git - ffmpeg/blob - libavformat/oggparsedirac.c
lavc/mdct_template: use lrint instead of floor hack
[ffmpeg] / 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 "internal.h"
25 #include "oggdec.h"
26
27 static int dirac_header(AVFormatContext *s, int idx)
28 {
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     DiracVersionInfo version;
34     GetBitContext gb;
35     int ret, bit_depth;
36
37     // already parsed the header
38     if (st->codec->codec_id == AV_CODEC_ID_DIRAC)
39         return 0;
40
41     ret = init_get_bits8(&gb, os->buf + os->pstart + 13, (os->psize - 13));
42     if (ret < 0)
43         return ret;
44
45     ret = avpriv_dirac_parse_sequence_header(st->codec, &gb, &source, &version,
46                                              &bit_depth);
47     if (ret < 0)
48         return ret;
49
50     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
51     st->codec->codec_id = AV_CODEC_ID_DIRAC;
52     // dirac in ogg always stores timestamps as though the video were interlaced
53     avpriv_set_pts_info(st, 64, st->codec->framerate.den, 2*st->codec->framerate.num);
54     return 1;
55 }
56
57 // various undocument things: granule is signed (only for dirac!)
58 static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule,
59                               int64_t *dts_out)
60 {
61     int64_t gp = granule;
62     struct ogg *ogg = s->priv_data;
63     struct ogg_stream *os = ogg->streams + idx;
64
65     unsigned dist  = ((gp >> 14) & 0xff00) | (gp & 0xff);
66     int64_t  dts   = (gp >> 31);
67     int64_t  pts   = dts + ((gp >> 9) & 0x1fff);
68
69     if (!dist)
70         os->pflags |= AV_PKT_FLAG_KEY;
71
72     if (dts_out)
73         *dts_out = dts;
74
75     return pts;
76 }
77
78 static int old_dirac_header(AVFormatContext *s, int idx)
79 {
80     struct ogg *ogg = s->priv_data;
81     struct ogg_stream *os = ogg->streams + idx;
82     AVStream *st = s->streams[idx];
83     uint8_t *buf = os->buf + os->pstart;
84
85     if (buf[0] != 'K')
86         return 0;
87
88     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
89     st->codec->codec_id = AV_CODEC_ID_DIRAC;
90     avpriv_set_pts_info(st, 64, AV_RB32(buf+12), AV_RB32(buf+8));
91     return 1;
92 }
93
94 static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp,
95                                   int64_t *dts)
96 {
97     struct ogg *ogg = s->priv_data;
98     struct ogg_stream *os = ogg->streams + idx;
99     uint64_t iframe = gp >> 30;
100     uint64_t pframe = gp & 0x3fffffff;
101
102     if (!pframe)
103         os->pflags |= AV_PKT_FLAG_KEY;
104
105     return iframe + pframe;
106 }
107
108 const struct ogg_codec ff_dirac_codec = {
109     .magic = "BBCD\0",
110     .magicsize = 5,
111     .header = dirac_header,
112     .gptopts = dirac_gptopts,
113     .granule_is_start = 1,
114     .nb_header = 1,
115 };
116
117 const struct ogg_codec ff_old_dirac_codec = {
118     .magic = "KW-DIRAC",
119     .magicsize = 8,
120     .header = old_dirac_header,
121     .gptopts = old_dirac_gptopts,
122     .granule_is_start = 1,
123     .nb_header = 1,
124 };