annotate oggparseogm.c @ 3499:e54614e83ba4 libavformat

Prevent duration calculation code from generating random durations for vorbis in ogg.
author michael
date Tue, 24 Jun 2008 23:56:16 +0000
parents 4d7210fc23a5
children 11169564fbdc
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"
6f61c3b36632 Use full path for #includes from another directory.
diego
parents: 2997
diff changeset
27 #include "libavcodec/bitstream.h"
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 {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
36 ogg_t *ogg = s->priv_data;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
37 ogg_stream_t *os = ogg->streams + idx;
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;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
46 if(*p != 1)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
47 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
48
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
49 p++;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
50
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
51 if(*p == 'v'){
1077
91677ac6fb19 set stream time_base properly
mru
parents: 1076
diff changeset
52 int tag;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
53 st->codec->codec_type = CODEC_TYPE_VIDEO;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
54 p += 8;
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
55 tag = bytestream_get_le32(&p);
2229
4db0808c3f91 ogm: don't use deprecated codec_get_{bmp,wav}_id()
mru
parents: 2226
diff changeset
56 st->codec->codec_id = codec_get_id(codec_bmp_tags, tag);
1077
91677ac6fb19 set stream time_base properly
mru
parents: 1076
diff changeset
57 st->codec->codec_tag = tag;
2994
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
58 } else if (*p == 't') {
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
59 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
60 st->codec->codec_id = CODEC_ID_TEXT;
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
61 p += 12;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
62 } else {
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
63 uint8_t acid[5];
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
64 int cid;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
65 st->codec->codec_type = CODEC_TYPE_AUDIO;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
66 p += 8;
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
67 bytestream_get_buffer(&p, acid, 4);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
68 acid[4] = 0;
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
69 cid = strtol(acid, NULL, 16);
2229
4db0808c3f91 ogm: don't use deprecated codec_get_{bmp,wav}_id()
mru
parents: 2226
diff changeset
70 st->codec->codec_id = codec_get_id(codec_wav_tags, cid);
3346
4d7210fc23a5 parse audio in ogm demuxer.
jbr
parents: 3286
diff changeset
71 st->need_parsing = AVSTREAM_PARSE_FULL;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
72 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
73
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
74 p += 4; /* useless size field */
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
75
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
76 time_unit = bytestream_get_le64(&p);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
77 spu = bytestream_get_le64(&p);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
78 default_len = bytestream_get_le32(&p);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
79
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
80 p += 8; /* buffersize + bits_per_sample */
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
81
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
82 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
83 st->codec->width = bytestream_get_le32(&p);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
84 st->codec->height = bytestream_get_le32(&p);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
85 st->codec->time_base.den = spu * 10000000;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
86 st->codec->time_base.num = time_unit;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
87 st->time_base = st->codec->time_base;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
88 } else {
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
89 st->codec->channels = bytestream_get_le16(&p);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
90 p += 2; /* block_align */
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
91 st->codec->bit_rate = bytestream_get_le32(&p) * 8;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
92 st->codec->sample_rate = spu * 10000000 / time_unit;
1077
91677ac6fb19 set stream time_base properly
mru
parents: 1076
diff changeset
93 st->time_base.num = 1;
91677ac6fb19 set stream time_base properly
mru
parents: 1076
diff changeset
94 st->time_base.den = st->codec->sample_rate;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
95 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
96
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
97 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
98 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
99
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
100 static int
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
101 ogm_dshow_header(AVFormatContext *s, int idx)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
102 {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
103 ogg_t *ogg = s->priv_data;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
104 ogg_stream_t *os = ogg->streams + idx;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
105 AVStream *st = s->streams[idx];
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
106 uint8_t *p = os->buf + os->pstart;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
107 uint32_t t;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
108
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
109 if(!(*p & 1))
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
110 return 0;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
111 if(*p != 1)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
112 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
113
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
114 t = AV_RL32(p + 96);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
115
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
116 if(t == 0x05589f80){
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
117 st->codec->codec_type = CODEC_TYPE_VIDEO;
2229
4db0808c3f91 ogm: don't use deprecated codec_get_{bmp,wav}_id()
mru
parents: 2226
diff changeset
118 st->codec->codec_id = codec_get_id(codec_bmp_tags, AV_RL32(p + 68));
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
119 st->codec->time_base.den = 10000000;
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
120 st->codec->time_base.num = AV_RL64(p + 164);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
121 st->codec->width = AV_RL32(p + 176);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
122 st->codec->height = AV_RL32(p + 180);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
123 } else if(t == 0x05589f81){
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
124 st->codec->codec_type = CODEC_TYPE_AUDIO;
2229
4db0808c3f91 ogm: don't use deprecated codec_get_{bmp,wav}_id()
mru
parents: 2226
diff changeset
125 st->codec->codec_id = codec_get_id(codec_wav_tags, AV_RL16(p + 124));
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
126 st->codec->channels = AV_RL16(p + 126);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
127 st->codec->sample_rate = AV_RL32(p + 128);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
128 st->codec->bit_rate = AV_RL32(p + 132) * 8;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
129 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
130
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
131 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
132 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
133
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
134 static int
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
135 ogm_packet(AVFormatContext *s, int idx)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
136 {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
137 ogg_t *ogg = s->priv_data;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
138 ogg_stream_t *os = ogg->streams + idx;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
139 uint8_t *p = os->buf + os->pstart;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
140 int lb;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
141
2732
2b101e9d25c0 set PKT_FLAG_KEY for Theora and OGM streams
mru
parents: 2714
diff changeset
142 if(*p & 8)
2b101e9d25c0 set PKT_FLAG_KEY for Theora and OGM streams
mru
parents: 2714
diff changeset
143 os->pflags |= PKT_FLAG_KEY;
2b101e9d25c0 set PKT_FLAG_KEY for Theora and OGM streams
mru
parents: 2714
diff changeset
144
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
145 lb = ((*p & 2) << 1) | ((*p >> 6) & 3);
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
146 os->pstart += lb + 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
147 os->psize -= lb + 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
148
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
149 return 0;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
150 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
151
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
152 ogg_codec_t ogm_video_codec = {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
153 .magic = "\001video",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
154 .magicsize = 6,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
155 .header = ogm_header,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
156 .packet = ogm_packet
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
157 };
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
158
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
159 ogg_codec_t ogm_audio_codec = {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
160 .magic = "\001audio",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
161 .magicsize = 6,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
162 .header = ogm_header,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
163 .packet = ogm_packet
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
164 };
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
165
2994
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
166 ogg_codec_t ogm_text_codec = {
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
167 .magic = "\001text",
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
168 .magicsize = 5,
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
169 .header = ogm_header,
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
170 .packet = ogm_packet
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
171 };
35b3eaafd9fe Add support for ogg text subtitles.
reimar
parents: 2732
diff changeset
172
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
173 ogg_codec_t ogm_old_codec = {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
174 .magic = "\001Direct Show Samples embedded in Ogg",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
175 .magicsize = 35,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
176 .header = ogm_dshow_header,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
177 .packet = ogm_packet
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
178 };