annotate oggparseogm.c @ 2984:d993d7e321d6 libavformat

SMPTE 421 Annex L format demuxer
author kostya
date Tue, 29 Jan 2008 09:25:49 +0000
parents 2b101e9d25c0
children 35b3eaafd9fe
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>
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
26 #include "avformat.h"
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
27 #include "bitstream.h"
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
28 #include "bytestream.h"
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
29 #include "intreadwrite.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];
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
39 uint8_t *p = os->buf + os->pstart;
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;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
58 } else {
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
59 uint8_t acid[5];
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
60 int cid;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
61 st->codec->codec_type = CODEC_TYPE_AUDIO;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
62 p += 8;
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
63 bytestream_get_buffer(&p, acid, 4);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
64 acid[4] = 0;
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
65 cid = strtol(acid, NULL, 16);
2229
4db0808c3f91 ogm: don't use deprecated codec_get_{bmp,wav}_id()
mru
parents: 2226
diff changeset
66 st->codec->codec_id = codec_get_id(codec_wav_tags, cid);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
67 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
68
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
69 p += 4; /* useless size field */
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
70
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
71 time_unit = bytestream_get_le64(&p);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
72 spu = bytestream_get_le64(&p);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
73 default_len = bytestream_get_le32(&p);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
74
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
75 p += 8; /* buffersize + bits_per_sample */
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
76
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
77 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
78 st->codec->width = bytestream_get_le32(&p);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
79 st->codec->height = bytestream_get_le32(&p);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
80 st->codec->time_base.den = spu * 10000000;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
81 st->codec->time_base.num = time_unit;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
82 st->time_base = st->codec->time_base;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
83 } else {
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
84 st->codec->channels = bytestream_get_le16(&p);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
85 p += 2; /* block_align */
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
86 st->codec->bit_rate = bytestream_get_le32(&p) * 8;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
87 st->codec->sample_rate = spu * 10000000 / time_unit;
1077
91677ac6fb19 set stream time_base properly
mru
parents: 1076
diff changeset
88 st->time_base.num = 1;
91677ac6fb19 set stream time_base properly
mru
parents: 1076
diff changeset
89 st->time_base.den = st->codec->sample_rate;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
90 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
91
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
92 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
93 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
94
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
95 static int
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
96 ogm_dshow_header(AVFormatContext *s, int idx)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
97 {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
98 ogg_t *ogg = s->priv_data;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
99 ogg_stream_t *os = ogg->streams + idx;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
100 AVStream *st = s->streams[idx];
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
101 uint8_t *p = os->buf + os->pstart;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
102 uint32_t t;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
103
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
104 if(!(*p & 1))
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
105 return 0;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
106 if(*p != 1)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
107 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
108
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
109 t = AV_RL32(p + 96);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
110
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
111 if(t == 0x05589f80){
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
112 st->codec->codec_type = CODEC_TYPE_VIDEO;
2229
4db0808c3f91 ogm: don't use deprecated codec_get_{bmp,wav}_id()
mru
parents: 2226
diff changeset
113 st->codec->codec_id = codec_get_id(codec_bmp_tags, AV_RL32(p + 68));
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
114 st->codec->time_base.den = 10000000;
2226
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
115 st->codec->time_base.num = AV_RL64(p + 164);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
116 st->codec->width = AV_RL32(p + 176);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
117 st->codec->height = AV_RL32(p + 180);
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
118 } else if(t == 0x05589f81){
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
119 st->codec->codec_type = CODEC_TYPE_AUDIO;
2229
4db0808c3f91 ogm: don't use deprecated codec_get_{bmp,wav}_id()
mru
parents: 2226
diff changeset
120 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
121 st->codec->channels = AV_RL16(p + 126);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
122 st->codec->sample_rate = AV_RL32(p + 128);
c2ad35fbbd30 use bytestream_get_* and AV_RLxx
mru
parents: 1172
diff changeset
123 st->codec->bit_rate = AV_RL32(p + 132) * 8;
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
124 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
125
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
126 return 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
127 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
128
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
129 static int
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
130 ogm_packet(AVFormatContext *s, int idx)
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
131 {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
132 ogg_t *ogg = s->priv_data;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
133 ogg_stream_t *os = ogg->streams + idx;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
134 uint8_t *p = os->buf + os->pstart;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
135 int lb;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
136
2732
2b101e9d25c0 set PKT_FLAG_KEY for Theora and OGM streams
mru
parents: 2714
diff changeset
137 if(*p & 8)
2b101e9d25c0 set PKT_FLAG_KEY for Theora and OGM streams
mru
parents: 2714
diff changeset
138 os->pflags |= PKT_FLAG_KEY;
2b101e9d25c0 set PKT_FLAG_KEY for Theora and OGM streams
mru
parents: 2714
diff changeset
139
1076
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
140 lb = ((*p & 2) << 1) | ((*p >> 6) & 3);
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
141 os->pstart += lb + 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
142 os->psize -= lb + 1;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
143
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
144 return 0;
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
145 }
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
146
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
147 ogg_codec_t ogm_video_codec = {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
148 .magic = "\001video",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
149 .magicsize = 6,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
150 .header = ogm_header,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
151 .packet = ogm_packet
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
152 };
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
153
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
154 ogg_codec_t ogm_audio_codec = {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
155 .magic = "\001audio",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
156 .magicsize = 6,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
157 .header = ogm_header,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
158 .packet = ogm_packet
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
159 };
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
160
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
161 ogg_codec_t ogm_old_codec = {
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
162 .magic = "\001Direct Show Samples embedded in Ogg",
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
163 .magicsize = 35,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
164 .header = ogm_dshow_header,
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
165 .packet = ogm_packet
a1c07ce3943c ogm demuxing
mru
parents:
diff changeset
166 };