annotate oggparseogm.c @ 6048:e507a21a9566 libavformat

matroskaenc: Write codec time base as default duration for video tracks. This isn't exactly semantically equivalent, but the field has already been long abused to mean this, and writing it helps in determining a decent cfr time base when transcoding from a mkv where the video codec stores none (VP8).
author conrad
date Mon, 24 May 2010 08:58:19 +0000
parents 11bb10c37225
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
1 /**
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
2 Copyright (C) 2005 Michael Ahlberg, Måns Rullgård
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
3
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
4 Permission is hereby granted, free of charge, to any person
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
5 obtaining a copy of this software and associated documentation
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
6 files (the "Software"), to deal in the Software without
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
7 restriction, including without limitation the rights to use, copy,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
8 modify, merge, publish, distribute, sublicense, and/or sell copies
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
9 of the Software, and to permit persons to whom the Software is
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
10 furnished to do so, subject to the following conditions:
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
11
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
12 The above copyright notice and this permission notice shall be
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
13 included in all copies or substantial portions of the Software.
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
14
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
22 DEALINGS IN THE SOFTWARE.
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
23 **/
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
24
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
25 #include <stdlib.h>
3286
6f61c3b36632 Use full path for #includes from another directory.
diego
parents: 2997
diff changeset
26 #include "libavutil/intreadwrite.h"
4872
304a0ea063f0 Rename bitstream.h to get_bits.h.
stefano
parents: 4016
diff changeset
27 #include "libavcodec/get_bits.h"
3286
6f61c3b36632 Use full path for #includes from another directory.
diego
parents: 2997
diff changeset
28 #include "libavcodec/bytestream.h"
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
29 #include "avformat.h"
2714
b22ba392ac21 Rename ogg2.[ch] to oggdec.[ch].
diego
parents: 2229
diff changeset
30 #include "oggdec.h"
1172
6a5e58d2114b move common stuff from avienc.c and wav.c to new file riff.c
mru
parents: 1077
diff changeset
31 #include "riff.h"
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
32
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
33 static int
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
34 ogm_header(AVFormatContext *s, int idx)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
35 {
4016
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
36 struct ogg *ogg = s->priv_data;
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
37 struct ogg_stream *os = ogg->streams + idx;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
38 AVStream *st = s->streams[idx];
2997
bcoudurier
parents: 2994
diff changeset
39 const uint8_t *p = os->buf + os->pstart;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
40 uint64_t time_unit;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
41 uint64_t spu;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
42 uint32_t default_len;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
43
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
44 if(!(*p & 1))
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
45 return 0;
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
46
5826
d75025af2e7b oggdec: Read ogm metadata
conrad
parents: 5434
diff changeset
47 if(*p == 1) {
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
48 p++;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
49
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
50 if(*p == 'v'){
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
51 int tag;
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5833
diff changeset
52 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
53 p += 8;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
54 tag = bytestream_get_le32(&p);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
55 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
56 st->codec->codec_tag = tag;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
57 } else if (*p == 't') {
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5833
diff changeset
58 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
59 st->codec->codec_id = CODEC_ID_TEXT;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
60 p += 12;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
61 } else {
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
62 uint8_t acid[5];
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
63 int cid;
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5833
diff changeset
64 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
65 p += 8;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
66 bytestream_get_buffer(&p, acid, 4);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
67 acid[4] = 0;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
68 cid = strtol(acid, NULL, 16);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
69 st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, cid);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
70 st->need_parsing = AVSTREAM_PARSE_FULL;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
71 }
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
72
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
73 p += 4; /* useless size field */
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
74
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
75 time_unit = bytestream_get_le64(&p);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
76 spu = bytestream_get_le64(&p);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
77 default_len = bytestream_get_le32(&p);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
78
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
79 p += 8; /* buffersize + bits_per_sample */
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
80
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5833
diff changeset
81 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
5827
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
82 st->codec->width = bytestream_get_le32(&p);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
83 st->codec->height = bytestream_get_le32(&p);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
84 st->codec->time_base.den = spu * 10000000;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
85 st->codec->time_base.num = time_unit;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
86 st->time_base = st->codec->time_base;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
87 } else {
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
88 st->codec->channels = bytestream_get_le16(&p);
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
89 p += 2; /* block_align */
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
90 st->codec->bit_rate = bytestream_get_le32(&p) * 8;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
91 st->codec->sample_rate = spu * 10000000 / time_unit;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
92 st->time_base.num = 1;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
93 st->time_base.den = st->codec->sample_rate;
4535dfdf5ad3 oggdec: Cosmetics
conrad
parents: 5826
diff changeset
94 }
5826
d75025af2e7b oggdec: Read ogm metadata
conrad
parents: 5434
diff changeset
95 } else if (*p == 3) {
d75025af2e7b oggdec: Read ogm metadata
conrad
parents: 5434
diff changeset
96 if (os->psize > 8)
d75025af2e7b oggdec: Read ogm metadata
conrad
parents: 5434
diff changeset
97 ff_vorbis_comment(s, &st->metadata, p+7, os->psize-8);
d75025af2e7b oggdec: Read ogm metadata
conrad
parents: 5434
diff changeset
98 }
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
99
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
100 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
101 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
102
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
103 static int
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
104 ogm_dshow_header(AVFormatContext *s, int idx)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
105 {
4016
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
106 struct ogg *ogg = s->priv_data;
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
107 struct ogg_stream *os = ogg->streams + idx;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
108 AVStream *st = s->streams[idx];
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
109 uint8_t *p = os->buf + os->pstart;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
110 uint32_t t;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
111
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
112 if(!(*p & 1))
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
113 return 0;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
114 if(*p != 1)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
115 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
116
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
117 t = AV_RL32(p + 96);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
118
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
119 if(t == 0x05589f80){
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5833
diff changeset
120 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
5058
33a244b7ca65 Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents: 4872
diff changeset
121 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, AV_RL32(p + 68));
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
122 st->codec->time_base.den = 10000000;
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
123 st->codec->time_base.num = AV_RL64(p + 164);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
124 st->codec->width = AV_RL32(p + 176);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
125 st->codec->height = AV_RL32(p + 180);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
126 } else if(t == 0x05589f81){
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5833
diff changeset
127 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
5058
33a244b7ca65 Add ff_ prefixes to exported symbols in libavformat/riff.h.
diego
parents: 4872
diff changeset
128 st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, AV_RL16(p + 124));
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
129 st->codec->channels = AV_RL16(p + 126);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
130 st->codec->sample_rate = AV_RL32(p + 128);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
131 st->codec->bit_rate = AV_RL32(p + 132) * 8;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
132 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
133
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
134 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
135 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
136
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
137 static int
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
138 ogm_packet(AVFormatContext *s, int idx)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
139 {
4016
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
140 struct ogg *ogg = s->priv_data;
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
141 struct ogg_stream *os = ogg->streams + idx;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
142 uint8_t *p = os->buf + os->pstart;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
143 int lb;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
144
2732
2b101e9d25c0 set PKT_FLAG_KEY for Theora and OGM streams
mru
parents: 2714
diff changeset
145 if(*p & 8)
5913
11bb10c37225 Replace all occurences of PKT_FLAG_KEY with AV_PKT_FLAG_KEY.
cehoyos
parents: 5910
diff changeset
146 os->pflags |= AV_PKT_FLAG_KEY;
2732
2b101e9d25c0 set PKT_FLAG_KEY for Theora and OGM streams
mru
parents: 2714
diff changeset
147
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
148 lb = ((*p & 2) << 1) | ((*p >> 6) & 3);
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
149 os->pstart += lb + 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
150 os->psize -= lb + 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
151
5833
c4d0166d9128 oggdec: Set packet duration for ogm streams
conrad
parents: 5827
diff changeset
152 while (lb--)
c4d0166d9128 oggdec: Set packet duration for ogm streams
conrad
parents: 5827
diff changeset
153 os->pduration += p[lb+1] << (lb*8);
c4d0166d9128 oggdec: Set packet duration for ogm streams
conrad
parents: 5827
diff changeset
154
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
155 return 0;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
156 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
157
4016
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
158 const struct ogg_codec ff_ogm_video_codec = {
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
159 .magic = "\001video",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
160 .magicsize = 6,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
161 .header = ogm_header,
5434
68c8e7affd44 Fix PTS for OGM codecs.
conrad
parents: 5058
diff changeset
162 .packet = ogm_packet,
68c8e7affd44 Fix PTS for OGM codecs.
conrad
parents: 5058
diff changeset
163 .granule_is_start = 1,
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
164 };
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
165
4016
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
166 const struct ogg_codec ff_ogm_audio_codec = {
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
167 .magic = "\001audio",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
168 .magicsize = 6,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
169 .header = ogm_header,
5434
68c8e7affd44 Fix PTS for OGM codecs.
conrad
parents: 5058
diff changeset
170 .packet = ogm_packet,
68c8e7affd44 Fix PTS for OGM codecs.
conrad
parents: 5058
diff changeset
171 .granule_is_start = 1,
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
172 };
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
173
4016
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
174 const struct ogg_codec ff_ogm_text_codec = {
2994
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
175 .magic = "\001text",
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
176 .magicsize = 5,
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
177 .header = ogm_header,
5434
68c8e7affd44 Fix PTS for OGM codecs.
conrad
parents: 5058
diff changeset
178 .packet = ogm_packet,
68c8e7affd44 Fix PTS for OGM codecs.
conrad
parents: 5058
diff changeset
179 .granule_is_start = 1,
2994
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
180 };
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
181
4016
6cd006bc2de9 OGG: untypedef demuxer structs
mru
parents: 3770
diff changeset
182 const struct ogg_codec ff_ogm_old_codec = {
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
183 .magic = "\001Direct Show Samples embedded in Ogg",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
184 .magicsize = 35,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
185 .header = ogm_dshow_header,
5434
68c8e7affd44 Fix PTS for OGM codecs.
conrad
parents: 5058
diff changeset
186 .packet = ogm_packet,
68c8e7affd44 Fix PTS for OGM codecs.
conrad
parents: 5058
diff changeset
187 .granule_is_start = 1,
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
188 };