1 /**
2 Copyright (C) 2005 Michael Ahlberg, Måns Rullgård
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use, copy,
8 modify, merge, publish, distribute, sublicense, and/or sell copies
9 of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 **/
24
25 #include <stdlib.h>
26
27 #include "libavutil/intreadwrite.h"
28
29 #include "libavcodec/bytestream.h"
30
31 #include "avformat.h"
32 #include "internal.h"
33 #include "oggdec.h"
34 #include "riff.h"
35
36 static int
ogm_header(AVFormatContext * s,int idx)37 ogm_header(AVFormatContext *s, int idx)
38 {
39 struct ogg *ogg = s->priv_data;
40 struct ogg_stream *os = ogg->streams + idx;
41 AVStream *st = s->streams[idx];
42 FFStream *const sti = ffstream(st);
43 GetByteContext p;
44 uint64_t time_unit;
45 uint64_t spu;
46 uint32_t size;
47 int ret;
48
49 bytestream2_init(&p, os->buf + os->pstart, os->psize);
50 if (!(bytestream2_peek_byte(&p) & 1))
51 return 0;
52
53 if (bytestream2_peek_byte(&p) == 1) {
54 bytestream2_skip(&p, 1);
55
56 if (bytestream2_peek_byte(&p) == 'v'){
57 int tag;
58 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
59 bytestream2_skip(&p, 8);
60 tag = bytestream2_get_le32(&p);
61 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag);
62 st->codecpar->codec_tag = tag;
63 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4)
64 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
65 } else if (bytestream2_peek_byte(&p) == 't') {
66 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
67 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
68 bytestream2_skip(&p, 12);
69 } else {
70 uint8_t acid[5] = { 0 };
71 int cid;
72 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
73 bytestream2_skip(&p, 8);
74 bytestream2_get_buffer(&p, acid, 4);
75 acid[4] = 0;
76 cid = strtol(acid, NULL, 16);
77 st->codecpar->codec_id = ff_codec_get_id(ff_codec_wav_tags, cid);
78 // our parser completely breaks AAC in Ogg
79 if (st->codecpar->codec_id != AV_CODEC_ID_AAC)
80 sti->need_parsing = AVSTREAM_PARSE_FULL;
81 }
82
83 size = bytestream2_get_le32(&p);
84 size = FFMIN(size, os->psize);
85 time_unit = bytestream2_get_le64(&p);
86 spu = bytestream2_get_le64(&p);
87 if (!time_unit || !spu) {
88 av_log(s, AV_LOG_ERROR, "Invalid timing values.\n");
89 return AVERROR_INVALIDDATA;
90 }
91
92 bytestream2_skip(&p, 4); /* default_len */
93 bytestream2_skip(&p, 8); /* buffersize + bits_per_sample */
94
95 if(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
96 st->codecpar->width = bytestream2_get_le32(&p);
97 st->codecpar->height = bytestream2_get_le32(&p);
98 avpriv_set_pts_info(st, 64, time_unit, spu * 10000000);
99 } else {
100 st->codecpar->ch_layout.nb_channels = bytestream2_get_le16(&p);
101 bytestream2_skip(&p, 2); /* block_align */
102 st->codecpar->bit_rate = bytestream2_get_le32(&p) * 8;
103 st->codecpar->sample_rate = spu * 10000000 / time_unit;
104 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
105 if (size >= 56 && st->codecpar->codec_id == AV_CODEC_ID_AAC) {
106 bytestream2_skip(&p, 4);
107 size -= 4;
108 }
109 if (size > 52) {
110 size -= 52;
111 if (bytestream2_get_bytes_left(&p) < size)
112 return AVERROR_INVALIDDATA;
113 if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
114 return ret;
115 bytestream2_get_buffer(&p, st->codecpar->extradata, st->codecpar->extradata_size);
116 }
117 }
118
119 // Update internal avctx with changes to codecpar above.
120 sti->need_context_update = 1;
121 } else if (bytestream2_peek_byte(&p) == 3) {
122 bytestream2_skip(&p, 7);
123 if (bytestream2_get_bytes_left(&p) > 1)
124 ff_vorbis_stream_comment(s, st, p.buffer, bytestream2_get_bytes_left(&p) - 1);
125 }
126
127 return 1;
128 }
129
130 static int
ogm_dshow_header(AVFormatContext * s,int idx)131 ogm_dshow_header(AVFormatContext *s, int idx)
132 {
133 struct ogg *ogg = s->priv_data;
134 struct ogg_stream *os = ogg->streams + idx;
135 AVStream *st = s->streams[idx];
136 uint8_t *p = os->buf + os->pstart;
137 uint32_t t;
138
139 if(!(*p & 1))
140 return 0;
141 if(*p != 1)
142 return 1;
143
144 if (os->psize < 100)
145 return AVERROR_INVALIDDATA;
146 t = AV_RL32(p + 96);
147
148 if(t == 0x05589f80){
149 if (os->psize < 184)
150 return AVERROR_INVALIDDATA;
151
152 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
153 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags, AV_RL32(p + 68));
154 avpriv_set_pts_info(st, 64, AV_RL64(p + 164), 10000000);
155 st->codecpar->width = AV_RL32(p + 176);
156 st->codecpar->height = AV_RL32(p + 180);
157 } else if(t == 0x05589f81){
158 if (os->psize < 136)
159 return AVERROR_INVALIDDATA;
160
161 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
162 st->codecpar->codec_id = ff_codec_get_id(ff_codec_wav_tags, AV_RL16(p + 124));
163 st->codecpar->ch_layout.nb_channels = AV_RL16(p + 126);
164 st->codecpar->sample_rate = AV_RL32(p + 128);
165 st->codecpar->bit_rate = AV_RL32(p + 132) * 8;
166 }
167
168 return 1;
169 }
170
171 static int
ogm_packet(AVFormatContext * s,int idx)172 ogm_packet(AVFormatContext *s, int idx)
173 {
174 struct ogg *ogg = s->priv_data;
175 struct ogg_stream *os = ogg->streams + idx;
176 uint8_t *p = os->buf + os->pstart;
177 int lb;
178
179 if(*p & 8)
180 os->pflags |= AV_PKT_FLAG_KEY;
181
182 lb = ((*p & 2) << 1) | ((*p >> 6) & 3);
183 if (os->psize < lb + 1)
184 return AVERROR_INVALIDDATA;
185
186 os->pstart += lb + 1;
187 os->psize -= lb + 1;
188
189 while (lb--)
190 os->pduration += (uint64_t)p[lb+1] << (lb*8);
191
192 return 0;
193 }
194
195 const struct ogg_codec ff_ogm_video_codec = {
196 .magic = "\001video",
197 .magicsize = 6,
198 .header = ogm_header,
199 .packet = ogm_packet,
200 .granule_is_start = 1,
201 .nb_header = 2,
202 };
203
204 const struct ogg_codec ff_ogm_audio_codec = {
205 .magic = "\001audio",
206 .magicsize = 6,
207 .header = ogm_header,
208 .packet = ogm_packet,
209 .granule_is_start = 1,
210 .nb_header = 2,
211 };
212
213 const struct ogg_codec ff_ogm_text_codec = {
214 .magic = "\001text",
215 .magicsize = 5,
216 .header = ogm_header,
217 .packet = ogm_packet,
218 .granule_is_start = 1,
219 .nb_header = 2,
220 };
221
222 const struct ogg_codec ff_ogm_old_codec = {
223 .magic = "\001Direct Show Samples embedded in Ogg",
224 .magicsize = 35,
225 .header = ogm_dshow_header,
226 .packet = ogm_packet,
227 .granule_is_start = 1,
228 .nb_header = 1,
229 };
230