Debian Codegraph @

src/psfile.cpp

raw
1 /* dvbcut
2 Copyright (c) 2005 Sven Over <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /* $Id$ */
20
21 #include <cassert>
22
23 #include "psfile.h"
24 #include "streamhandle.h"
25 #include "stream.h"
26
27 static inline streamtype::type
28 stream_type(int sid) {
29 if (sid >= 0xc0 && sid <= 0xdf)
30 return streamtype::mpegaudio;
31 if (sid >= 0x180 && sid <= 0x187)
32 return streamtype::ac3audio;
33 /* not supported yet:
34 if (sid >= 0x188 && sid <= 0x18f)
35 return streamtype::dtsaudio;
36 if (sid >= 0x120 && sid <= 0x13f)
37 return streamtype::vobsub;
38 */
39 return streamtype::unknown;
40 }
41
42 static inline bool
43 is_audio_stream(streamtype::type t) {
44 return t == streamtype::mpegaudio
45 || t == streamtype::ac3audio
46 /* not yet
47 || t == streamtype::dtsaudio
48 */
49 ;
50 }
51
52 psfile::psfile(inbuffer &b, int initial_offset)
53 : mpgfile(b, initial_offset)
54 {
55 int vid=-1;
56 for(unsigned int i=0;i<0x300;++i)
57 streamnumber[i]=-1;
58
59 int inbytes=buf.providedata(buf.getsize(),initialoffset);
60 const uint8_t* data=(const uint8_t*) buf.data();
61
62 bool streamfound[0x300]={};
63 int count = 0;
64 int sid;
65
66 while (inbytes>=9) {
67 if (data[2]&0xfe) {
68 data+=3;
69 inbytes-=3;
70 continue;
71 }
72
73 if (data[0]!=0 || data[1]!=0 || data[2]!=1 || data[3]<0xb9) {
74 // sync lost
75 ++data;
76 --inbytes;
77 continue;
78 }
79
80 sid=data[3];
81 int len=6;
82
83 if (sid==0xba) { // pack header
84 if (inbytes<14)
85 break;
86 len=14+(data[13]&0x07);
87 }
88 else if (sid==0xb9) { // program end
89 break;
90 }
91 else {
92 len=((data[4]<<8)|data[5])+6;
93 }
94
95 if (sid>=0xe0 && sid<=0xef) {
96 if (vid<0) {
97 vid=sid;
98 streamnumber[vid]=VIDEOSTREAM;
99 }
100 inbytes-=len;
101 data+=len;
102 continue;
103 }
104
105 if (sid==0xbd || sid==0xbf) { // private stream
106 if (inbytes<(10+data[8]))
107 break;
108 int ssid=data[9+data[8]];
109 sid=((sid==0xbd)?0x100:0x200) | ssid;
110 }
111
112 if (!streamfound[sid]) { // first occurrence of this stream
113 streamfound[sid] = true;
114 streamtype::type t = stream_type(sid);
115 if (is_audio_stream(t) && count < MAXAUDIOSTREAMS) {
116 // note: streams will be renumbered later
117 streamnumber[sid] = audiostream(0);
118 ++count;
119 }
120 }
121 inbytes-=len;
122 data+=len;
123 continue;
124 }
125
126 // renumber audio streams
127 for (sid = 0; sid < 0x300; ++sid) {
128 if (streamnumber[sid] == audiostream(0)) {
129 fprintf(stderr, "Found audio stream %d (sid 0x%03x)\n", audiostreams, sid);
130 streamnumber[sid] = audiostream(audiostreams);
131 stream *S = &s[audiostream(audiostreams++)];
132 S->id = sid;
133 S->type = stream_type(sid);
134 }
135 }
136 assert(audiostreams == count);
137
138 initcodeccontexts(vid);
139 }
140
141
142 psfile::~psfile()
143 {}
144
145 int psfile::streamreader(streamhandle &sh)
146 {
147 int bytes=0;
148 const uint8_t *data=0;
149 int skipped=0;
150 int minbytes=14;
151
152 for(;;) {
153 if (bytes<minbytes) {
154 sh.fileposition+=skipped;
155 skipped=0;
156 bytes=buf.providedata(minbytes,sh.fileposition);
157 if (bytes<0)
158 return bytes;
159 if (bytes<minbytes)
160 return 0;
161 data=(const uint8_t*) buf.data();
162 }
163
164 minbytes=14;
165
166 if (data[2]&0xfe) { //shortcut
167 skipped+=3;
168 data+=3;
169 bytes-=3;
170 continue;
171 }
172
173 if (data[0]!=0 || data[1]!=0 || data[2]!=1 || data[3]<0xb9) {
174 // sync lost
175 ++skipped;
176 ++data;
177 --bytes;
178 continue;
179 }
180
181 int sid=data[3];
182 int len=6;
183
184 if (sid==0xba) { // packheader
185 len=14+(data[13]&0x07);
186 }
187 else if (sid==0xb9) // program end
188 return 0;
189 else
190 len=((data[4]<<8)|data[5])+6;
191
192 if (sid==0xbd || sid==0xbf) { // private stream
193 if (bytes<(10+data[8])) {
194 minbytes=10+data[8];
195 continue;
196 }
197 int ssid=data[9+data[8]];
198 sid=((sid==0xbd)?0x100:0x200) | ssid;
199 }
200
201 int sn=streamnumber[sid];
202 if (sn>=0) {
203 if (bytes<len) {
204 minbytes=len;
205 continue;
206 }
207 sh.fileposition+=skipped;
208 skipped=0;
209
210 int payloadbegin=data[8]+9;
211 if (sid >= 0x180 && sid <= 0x18f) // ac3audio or dtsaudio
212 payloadbegin += 4;
213 else if (sid&0x300)
214 ++payloadbegin;
215 else if ((sid&0xf0)==0xe0) {
216 if ( *(uint32_t*)(data+payloadbegin)==mbo32(0x00000001) )
217 ++payloadbegin;
218 }
219
220 streamdata *sd=sh.stream[sn];
221 if (len>payloadbegin && sd) {
222
223 sd->appenditem(filepos_t(sh.fileposition,0), std::string((const char*)data+6,payloadbegin-6),
224 data+payloadbegin, len-payloadbegin);
225 int returnvalue = len-payloadbegin;
226
227 sh.fileposition+=len;
228 return returnvalue;
229 }
230 }
231
232 skipped+=len;
233 data+=len;
234 bytes-=len;
235 }
236
237 }
238
239 /// This function probes the data in the given inbuffer for an mpeg program stream.
240 /// It returns the buffer offset at which the program stream starts, or -1 if
241 /// no program stream was identified.
242 int psfile::probe(inbuffer &buf)
243 {
244 int latestsync=buf.inbytes()-2048-16;
245 if (latestsync>(8<<10))
246 latestsync=8<<10;
247
248 int testupto=buf.inbytes()-16;
249 if (testupto > (2 << 20)) // scan at most 2 MB
250 testupto = 2 << 20;
251
252 const uint8_t *data = (const uint8_t*) buf.data();
253 int ps;
254 for (ps = 0; ps < latestsync; ++ps) {
255 if (data[ps+2]&0xfe) {
256 ps+=3;
257 continue;
258 }
259
260 int pos=ps;
261 while(pos < testupto) {
262 const uint8_t *d=&data[pos];
263
264 if (d[0]!=0 || d[1]!=0 || d[2]!=1 || d[3]<0xba)
265 break;
266 if (d[3]==0xba)
267 pos+=14+(d[13]&0x07);
268 else
269 pos+=((d[4]<<8)|d[5])+6;
270 }
271 if (pos >= testupto) { // this is a MPEG PS file
272 return ps;
273 }
274 }
275
276 return -1;
277 }