comparison vobsub.c @ 6773:24f3276523af

Remove depency on libmpdemux streams, use ANSI IO instead.
author kmkaplan
date Tue, 23 Jul 2002 20:19:42 +0000
parents e1428c2c971f
children b1f788bca721
comparison
equal deleted inserted replaced
6772:8a816f073707 6773:24f3276523af
14 #include <sys/types.h> 14 #include <sys/types.h>
15 15
16 #include "config.h" 16 #include "config.h"
17 #include "version.h" 17 #include "version.h"
18 18
19 #include "stream.h"
20 #include "vobsub.h" 19 #include "vobsub.h"
21 #include "libvo/video_out.h" 20 #include "libvo/video_out.h"
22 #include "spudec.h" 21 #include "spudec.h"
23 #include "mp_msg.h" 22 #include "mp_msg.h"
24 23
75 /********************************************************************** 74 /**********************************************************************
76 * MPEG parsing 75 * MPEG parsing
77 **********************************************************************/ 76 **********************************************************************/
78 77
79 typedef struct { 78 typedef struct {
80 stream_t *stream; 79 FILE *stream;
81 unsigned int pts; 80 unsigned int pts;
82 int aid; 81 int aid;
83 unsigned char *packet; 82 unsigned char *packet;
84 unsigned int packet_reserve; 83 unsigned int packet_reserve;
85 unsigned int packet_size; 84 unsigned int packet_size;
89 mpeg_open(const char *filename) 88 mpeg_open(const char *filename)
90 { 89 {
91 mpeg_t *res = malloc(sizeof(mpeg_t)); 90 mpeg_t *res = malloc(sizeof(mpeg_t));
92 int err = res == NULL; 91 int err = res == NULL;
93 if (!err) { 92 if (!err) {
94 int fd;
95 res->pts = 0; 93 res->pts = 0;
96 res->aid = -1; 94 res->aid = -1;
97 res->packet = NULL; 95 res->packet = NULL;
98 res->packet_size = 0; 96 res->packet_size = 0;
99 res->packet_reserve = 0; 97 res->packet_reserve = 0;
100 fd = open(filename, O_RDONLY); 98 res->stream = fopen(filename, "r");
101 err = fd < 0; 99 err = res->stream == NULL;
102 if (!err) { 100 if (err)
103 res->stream = new_stream(fd, STREAMTYPE_FILE); 101 perror("fopen Vobsub file failed");
104 err = res->stream == NULL;
105 if (err)
106 close(fd);
107 }
108 if (err) 102 if (err)
109 free(res); 103 free(res);
110 } 104 }
111 return err ? NULL : res; 105 return err ? NULL : res;
112 } 106 }
113 107
114 static void 108 static void
115 mpeg_free(mpeg_t *mpeg) 109 mpeg_free(mpeg_t *mpeg)
116 { 110 {
117 int fd;
118 if (mpeg->packet) 111 if (mpeg->packet)
119 free(mpeg->packet); 112 free(mpeg->packet);
120 fd = mpeg->stream->fd; 113 if (mpeg->stream)
121 free_stream(mpeg->stream); 114 fclose(mpeg->stream);
122 close(fd);
123 free(mpeg); 115 free(mpeg);
124 } 116 }
125 117
126 static int 118 static int
127 mpeg_eof(mpeg_t *mpeg) 119 mpeg_eof(mpeg_t *mpeg)
128 { 120 {
129 return stream_eof(mpeg->stream); 121 return feof(mpeg->stream);
130 } 122 }
131 123
132 static off_t 124 static off_t
133 mpeg_tell(mpeg_t *mpeg) 125 mpeg_tell(mpeg_t *mpeg)
134 { 126 {
135 return stream_tell(mpeg->stream); 127 return ftell(mpeg->stream);
136 } 128 }
137 129
138 static int 130 static int
139 mpeg_run(mpeg_t *mpeg) 131 mpeg_run(mpeg_t *mpeg)
140 { 132 {
144 const unsigned char wanted[] = { 0, 0, 1 }; 136 const unsigned char wanted[] = { 0, 0, 1 };
145 unsigned char buf[5]; 137 unsigned char buf[5];
146 138
147 mpeg->aid = -1; 139 mpeg->aid = -1;
148 mpeg->packet_size = 0; 140 mpeg->packet_size = 0;
149 if (stream_read(mpeg->stream, buf, 4) != 4) 141 if (fread(buf, 4, 1, mpeg->stream) != 1)
150 return -1; 142 return -1;
151 while (memcmp(buf, wanted, sizeof(wanted)) != 0) { 143 while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
152 c = stream_read_char(mpeg->stream); 144 c = getc(mpeg->stream);
153 if (c < 0) 145 if (c < 0)
154 return -1; 146 return -1;
155 memmove(buf, buf + 1, 3); 147 memmove(buf, buf + 1, 3);
156 buf[3] = c; 148 buf[3] = c;
157 } 149 }
158 switch (buf[3]) { 150 switch (buf[3]) {
159 case 0xb9: /* System End Code */ 151 case 0xb9: /* System End Code */
160 break; 152 break;
161 case 0xba: /* Packet start code */ 153 case 0xba: /* Packet start code */
162 c = stream_read_char(mpeg->stream); 154 c = getc(mpeg->stream);
163 if (c < 0) 155 if (c < 0)
164 return -1; 156 return -1;
165 if ((c & 0xc0) == 0x40) 157 if ((c & 0xc0) == 0x40)
166 version = 4; 158 version = 4;
167 else if ((c & 0xf0) == 0x20) 159 else if ((c & 0xf0) == 0x20)
169 else { 161 else {
170 mp_msg(MSGT_VOBSUB,MSGL_ERR, "Unsupported MPEG version: 0x%02x", c); 162 mp_msg(MSGT_VOBSUB,MSGL_ERR, "Unsupported MPEG version: 0x%02x", c);
171 return -1; 163 return -1;
172 } 164 }
173 if (version == 4) { 165 if (version == 4) {
174 if (!stream_skip(mpeg->stream, 9)) 166 if (fseek(mpeg->stream, 9, SEEK_CUR))
175 return -1; 167 return -1;
176 } 168 }
177 else if (version == 2) { 169 else if (version == 2) {
178 if (!stream_skip(mpeg->stream, 7)) 170 if (fseek(mpeg->stream, 7, SEEK_CUR))
179 return -1; 171 return -1;
180 } 172 }
181 else 173 else
182 abort(); 174 abort();
183 break; 175 break;
184 case 0xbd: /* packet */ 176 case 0xbd: /* packet */
185 if (stream_read(mpeg->stream, buf, 2) != 2) 177 if (fread(buf, 2, 1, mpeg->stream) != 1)
186 return -1; 178 return -1;
187 len = buf[0] << 8 | buf[1]; 179 len = buf[0] << 8 | buf[1];
188 idx = mpeg_tell(mpeg); 180 idx = mpeg_tell(mpeg);
189 c = stream_read_char(mpeg->stream); 181 c = getc(mpeg->stream);
190 if (c < 0) 182 if (c < 0)
191 return -1; 183 return -1;
192 if ((c & 0xC0) == 0x40) { /* skip STD scale & size */ 184 if ((c & 0xC0) == 0x40) { /* skip STD scale & size */
193 if (stream_read_char(mpeg->stream) < 0) 185 if (getc(mpeg->stream) < 0)
194 return -1; 186 return -1;
195 c = stream_read_char(mpeg->stream); 187 c = getc(mpeg->stream);
196 if (c < 0) 188 if (c < 0)
197 return -1; 189 return -1;
198 } 190 }
199 if ((c & 0xf0) == 0x20) { /* System-1 stream timestamp */ 191 if ((c & 0xf0) == 0x20) { /* System-1 stream timestamp */
200 /* Do we need this? */ 192 /* Do we need this? */
204 /* Do we need this? */ 196 /* Do we need this? */
205 abort(); 197 abort();
206 } 198 }
207 else if ((c & 0xc0) == 0x80) { /* System-2 (.VOB) stream */ 199 else if ((c & 0xc0) == 0x80) { /* System-2 (.VOB) stream */
208 unsigned int pts_flags, hdrlen, dataidx; 200 unsigned int pts_flags, hdrlen, dataidx;
209 c = stream_read_char(mpeg->stream); 201 c = getc(mpeg->stream);
210 if (c < 0) 202 if (c < 0)
211 return -1; 203 return -1;
212 pts_flags = c; 204 pts_flags = c;
213 c = stream_read_char(mpeg->stream); 205 c = getc(mpeg->stream);
214 if (c < 0) 206 if (c < 0)
215 return -1; 207 return -1;
216 hdrlen = c; 208 hdrlen = c;
217 dataidx = mpeg_tell(mpeg) + hdrlen; 209 dataidx = mpeg_tell(mpeg) + hdrlen;
218 if (dataidx > idx + len) { 210 if (dataidx > idx + len) {
219 mp_msg(MSGT_VOBSUB,MSGL_ERR, "Invalid header length: %d (total length: %d, idx: %d, dataidx: %d)\n", 211 mp_msg(MSGT_VOBSUB,MSGL_ERR, "Invalid header length: %d (total length: %d, idx: %d, dataidx: %d)\n",
220 hdrlen, len, idx, dataidx); 212 hdrlen, len, idx, dataidx);
221 return -1; 213 return -1;
222 } 214 }
223 if ((pts_flags & 0xc0) == 0x80) { 215 if ((pts_flags & 0xc0) == 0x80) {
224 if (stream_read(mpeg->stream, buf, 5) != 5) 216 if (fread(buf, 5, 1, mpeg->stream) != 1)
225 return -1; 217 return -1;
226 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) && (buf[4] & 1))) { 218 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) && (buf[4] & 1))) {
227 mp_msg(MSGT_VOBSUB,MSGL_ERR, "vobsub PTS error: 0x%02x %02x%02x %02x%02x \n", 219 mp_msg(MSGT_VOBSUB,MSGL_ERR, "vobsub PTS error: 0x%02x %02x%02x %02x%02x \n",
228 buf[0], buf[1], buf[2], buf[3], buf[4]); 220 buf[0], buf[1], buf[2], buf[3], buf[4]);
229 mpeg->pts = 0; 221 mpeg->pts = 0;
234 } 226 }
235 else /* if ((pts_flags & 0xc0) == 0xc0) */ { 227 else /* if ((pts_flags & 0xc0) == 0xc0) */ {
236 /* what's this? */ 228 /* what's this? */
237 /* abort(); */ 229 /* abort(); */
238 } 230 }
239 stream_seek(mpeg->stream, dataidx); 231 fseek(mpeg->stream, dataidx, SEEK_SET);
240 mpeg->aid = stream_read_char(mpeg->stream); 232 mpeg->aid = getc(mpeg->stream);
241 if (mpeg->aid < 0) { 233 if (mpeg->aid < 0) {
242 mp_msg(MSGT_VOBSUB,MSGL_ERR, "Bogus aid %d\n", mpeg->aid); 234 mp_msg(MSGT_VOBSUB,MSGL_ERR, "Bogus aid %d\n", mpeg->aid);
243 return -1; 235 return -1;
244 } 236 }
245 mpeg->packet_size = len - ((unsigned int) mpeg_tell(mpeg) - idx); 237 mpeg->packet_size = len - ((unsigned int) mpeg_tell(mpeg) - idx);
254 mp_msg(MSGT_VOBSUB,MSGL_FATAL,"malloc failure"); 246 mp_msg(MSGT_VOBSUB,MSGL_FATAL,"malloc failure");
255 mpeg->packet_reserve = 0; 247 mpeg->packet_reserve = 0;
256 mpeg->packet_size = 0; 248 mpeg->packet_size = 0;
257 return -1; 249 return -1;
258 } 250 }
259 if ((unsigned int)stream_read(mpeg->stream, mpeg->packet, mpeg->packet_size) != mpeg->packet_size) { 251 if (fread(mpeg->packet, mpeg->packet_size, 1, mpeg->stream) != 1) {
260 mp_msg(MSGT_VOBSUB,MSGL_ERR,"stream_read failure"); 252 mp_msg(MSGT_VOBSUB,MSGL_ERR,"fread failure");
261 mpeg->packet_size = 0; 253 mpeg->packet_size = 0;
262 return -1; 254 return -1;
263 } 255 }
264 idx = len; 256 idx = len;
265 } 257 }
266 break; 258 break;
267 case 0xbe: /* Padding */ 259 case 0xbe: /* Padding */
268 if (stream_read(mpeg->stream, buf, 2) != 2) 260 if (fread(buf, 2, 1, mpeg->stream) != 1)
269 return -1; 261 return -1;
270 len = buf[0] << 8 | buf[1]; 262 len = buf[0] << 8 | buf[1];
271 if (len > 0 && !stream_skip(mpeg->stream, len)) 263 if (len > 0 && fseek(mpeg->stream, len, SEEK_CUR))
272 return -1; 264 return -1;
273 break; 265 break;
274 default: 266 default:
275 if (0xc0 <= buf[3] && buf[3] < 0xf0) { 267 if (0xc0 <= buf[3] && buf[3] < 0xf0) {
276 /* MPEG audio or video */ 268 /* MPEG audio or video */
277 if (stream_read(mpeg->stream, buf, 2) != 2) 269 if (fread(buf, 2, 1, mpeg->stream) != 1)
278 return -1; 270 return -1;
279 len = buf[0] << 8 | buf[1]; 271 len = buf[0] << 8 | buf[1];
280 if (len > 0 && !stream_skip(mpeg->stream, len)) 272 if (len > 0 && fseek(mpeg->stream, len, SEEK_CUR))
281 return -1; 273 return -1;
282 274
283 } 275 }
284 else { 276 else {
285 mp_msg(MSGT_VOBSUB,MSGL_ERR,"unknown header 0x%02X%02X%02X%02X\n", 277 mp_msg(MSGT_VOBSUB,MSGL_ERR,"unknown header 0x%02X%02X%02X%02X\n",