Mercurial > libavformat.hg
annotate mpeg.c @ 283:dcc3f5510818 libavformat
Enable decoding of more mpeg-ts streams for pcHDTV patch by (Doug Larrick <doug at ties dot org>)
author | michael |
---|---|
date | Wed, 15 Oct 2003 18:06:44 +0000 |
parents | a313e1080322 |
children | b19f70a6d60f |
rev | line source |
---|---|
0 | 1 /* |
2 * MPEG1/2 mux/demux | |
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 #include "avformat.h" | |
20 | |
21 #define MAX_PAYLOAD_SIZE 4096 | |
22 #define NB_STREAMS 2 | |
23 | |
24 typedef struct { | |
65 | 25 uint8_t buffer[MAX_PAYLOAD_SIZE]; |
0 | 26 int buffer_ptr; |
65 | 27 uint8_t id; |
0 | 28 int max_buffer_size; /* in bytes */ |
29 int packet_number; | |
65 | 30 int64_t start_pts; |
0 | 31 } StreamInfo; |
32 | |
33 typedef struct { | |
34 int packet_size; /* required packet size */ | |
35 int packet_data_max_size; /* maximum data size inside a packet */ | |
36 int packet_number; | |
37 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */ | |
38 int system_header_freq; | |
39 int mux_rate; /* bitrate in units of 50 bytes/s */ | |
40 /* stream info */ | |
41 int audio_bound; | |
42 int video_bound; | |
43 int is_mpeg2; | |
44 int is_vcd; | |
45 } MpegMuxContext; | |
46 | |
47 #define PACK_START_CODE ((unsigned int)0x000001ba) | |
48 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb) | |
49 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7) | |
50 #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00) | |
51 #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100) | |
52 #define ISO_11172_END_CODE ((unsigned int)0x000001b9) | |
53 | |
54 /* mpeg2 */ | |
55 #define PROGRAM_STREAM_MAP 0x1bc | |
56 #define PRIVATE_STREAM_1 0x1bd | |
57 #define PADDING_STREAM 0x1be | |
58 #define PRIVATE_STREAM_2 0x1bf | |
59 | |
60 | |
61 #define AUDIO_ID 0xc0 | |
62 #define VIDEO_ID 0xe0 | |
63 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
276
diff
changeset
|
64 #ifdef CONFIG_ENCODERS |
0 | 65 extern AVOutputFormat mpeg1system_mux; |
66 extern AVOutputFormat mpeg1vcd_mux; | |
67 extern AVOutputFormat mpeg2vob_mux; | |
68 | |
69 static int put_pack_header(AVFormatContext *ctx, | |
65 | 70 uint8_t *buf, int64_t timestamp) |
0 | 71 { |
72 MpegMuxContext *s = ctx->priv_data; | |
73 PutBitContext pb; | |
74 | |
276 | 75 init_put_bits(&pb, buf, 128); |
0 | 76 |
77 put_bits(&pb, 32, PACK_START_CODE); | |
78 if (s->is_mpeg2) { | |
174
7d56e9f83fdb
Write correct MPEG2-PS streams patch by (mru at users dot sourceforge dot net (Mns Rullgrd))
michaelni
parents:
165
diff
changeset
|
79 put_bits(&pb, 2, 0x1); |
0 | 80 } else { |
81 put_bits(&pb, 4, 0x2); | |
82 } | |
65 | 83 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07)); |
0 | 84 put_bits(&pb, 1, 1); |
65 | 85 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff)); |
0 | 86 put_bits(&pb, 1, 1); |
65 | 87 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff)); |
0 | 88 put_bits(&pb, 1, 1); |
89 if (s->is_mpeg2) { | |
90 /* clock extension */ | |
91 put_bits(&pb, 9, 0); | |
92 put_bits(&pb, 1, 1); | |
93 } | |
94 put_bits(&pb, 1, 1); | |
95 put_bits(&pb, 22, s->mux_rate); | |
96 put_bits(&pb, 1, 1); | |
97 if (s->is_mpeg2) { | |
98 put_bits(&pb, 5, 0x1f); /* reserved */ | |
99 put_bits(&pb, 3, 0); /* stuffing length */ | |
100 } | |
101 flush_put_bits(&pb); | |
102 return pbBufPtr(&pb) - pb.buf; | |
103 } | |
104 | |
65 | 105 static int put_system_header(AVFormatContext *ctx, uint8_t *buf) |
0 | 106 { |
107 MpegMuxContext *s = ctx->priv_data; | |
108 int size, rate_bound, i, private_stream_coded, id; | |
109 PutBitContext pb; | |
110 | |
276 | 111 init_put_bits(&pb, buf, 128); |
0 | 112 |
113 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE); | |
114 put_bits(&pb, 16, 0); | |
115 put_bits(&pb, 1, 1); | |
116 | |
117 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */ | |
118 put_bits(&pb, 22, rate_bound); | |
119 put_bits(&pb, 1, 1); /* marker */ | |
120 put_bits(&pb, 6, s->audio_bound); | |
121 | |
122 put_bits(&pb, 1, 1); /* variable bitrate */ | |
123 put_bits(&pb, 1, 1); /* non constrainted bit stream */ | |
124 | |
125 put_bits(&pb, 1, 0); /* audio locked */ | |
126 put_bits(&pb, 1, 0); /* video locked */ | |
127 put_bits(&pb, 1, 1); /* marker */ | |
128 | |
129 put_bits(&pb, 5, s->video_bound); | |
130 put_bits(&pb, 8, 0xff); /* reserved byte */ | |
131 | |
132 /* audio stream info */ | |
133 private_stream_coded = 0; | |
134 for(i=0;i<ctx->nb_streams;i++) { | |
135 StreamInfo *stream = ctx->streams[i]->priv_data; | |
136 id = stream->id; | |
137 if (id < 0xc0) { | |
138 /* special case for private streams (AC3 use that) */ | |
139 if (private_stream_coded) | |
140 continue; | |
141 private_stream_coded = 1; | |
142 id = 0xbd; | |
143 } | |
144 put_bits(&pb, 8, id); /* stream ID */ | |
145 put_bits(&pb, 2, 3); | |
146 if (id < 0xe0) { | |
147 /* audio */ | |
148 put_bits(&pb, 1, 0); | |
149 put_bits(&pb, 13, stream->max_buffer_size / 128); | |
150 } else { | |
151 /* video */ | |
152 put_bits(&pb, 1, 1); | |
153 put_bits(&pb, 13, stream->max_buffer_size / 1024); | |
154 } | |
155 } | |
156 flush_put_bits(&pb); | |
157 size = pbBufPtr(&pb) - pb.buf; | |
158 /* patch packet size */ | |
159 buf[4] = (size - 6) >> 8; | |
160 buf[5] = (size - 6) & 0xff; | |
161 | |
162 return size; | |
163 } | |
164 | |
165 static int mpeg_mux_init(AVFormatContext *ctx) | |
166 { | |
167 MpegMuxContext *s = ctx->priv_data; | |
168 int bitrate, i, mpa_id, mpv_id, ac3_id; | |
169 AVStream *st; | |
170 StreamInfo *stream; | |
171 | |
172 s->packet_number = 0; | |
173 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux); | |
174 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux); | |
175 | |
176 if (s->is_vcd) | |
177 s->packet_size = 2324; /* VCD packet size */ | |
178 else | |
179 s->packet_size = 2048; | |
180 | |
181 /* startcode(4) + length(2) + flags(1) */ | |
182 s->packet_data_max_size = s->packet_size - 7; | |
183 s->audio_bound = 0; | |
184 s->video_bound = 0; | |
185 mpa_id = AUDIO_ID; | |
186 ac3_id = 0x80; | |
187 mpv_id = VIDEO_ID; | |
188 for(i=0;i<ctx->nb_streams;i++) { | |
189 st = ctx->streams[i]; | |
190 stream = av_mallocz(sizeof(StreamInfo)); | |
191 if (!stream) | |
192 goto fail; | |
193 st->priv_data = stream; | |
194 | |
195 switch(st->codec.codec_type) { | |
196 case CODEC_TYPE_AUDIO: | |
197 if (st->codec.codec_id == CODEC_ID_AC3) | |
198 stream->id = ac3_id++; | |
199 else | |
200 stream->id = mpa_id++; | |
201 stream->max_buffer_size = 4 * 1024; | |
202 s->audio_bound++; | |
203 break; | |
204 case CODEC_TYPE_VIDEO: | |
205 stream->id = mpv_id++; | |
206 stream->max_buffer_size = 46 * 1024; | |
207 s->video_bound++; | |
208 break; | |
209 default: | |
210 av_abort(); | |
211 } | |
212 } | |
213 | |
214 /* we increase slightly the bitrate to take into account the | |
215 headers. XXX: compute it exactly */ | |
216 bitrate = 2000; | |
217 for(i=0;i<ctx->nb_streams;i++) { | |
218 st = ctx->streams[i]; | |
219 bitrate += st->codec.bit_rate; | |
220 } | |
221 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); | |
222 | |
223 if (s->is_vcd || s->is_mpeg2) | |
224 /* every packet */ | |
225 s->pack_header_freq = 1; | |
226 else | |
227 /* every 2 seconds */ | |
228 s->pack_header_freq = 2 * bitrate / s->packet_size / 8; | |
229 | |
230 if (s->is_mpeg2) | |
231 /* every 200 packets. Need to look at the spec. */ | |
232 s->system_header_freq = s->pack_header_freq * 40; | |
233 else if (s->is_vcd) | |
234 /* every 40 packets, this is my invention */ | |
235 s->system_header_freq = s->pack_header_freq * 40; | |
236 else | |
237 s->system_header_freq = s->pack_header_freq * 5; | |
238 | |
239 for(i=0;i<ctx->nb_streams;i++) { | |
240 stream = ctx->streams[i]->priv_data; | |
241 stream->buffer_ptr = 0; | |
242 stream->packet_number = 0; | |
243 stream->start_pts = -1; | |
244 } | |
245 return 0; | |
246 fail: | |
247 for(i=0;i<ctx->nb_streams;i++) { | |
248 av_free(ctx->streams[i]->priv_data); | |
249 } | |
250 return -ENOMEM; | |
251 } | |
252 | |
253 /* flush the packet on stream stream_index */ | |
242 | 254 static void flush_packet(AVFormatContext *ctx, int stream_index) |
0 | 255 { |
256 MpegMuxContext *s = ctx->priv_data; | |
257 StreamInfo *stream = ctx->streams[stream_index]->priv_data; | |
65 | 258 uint8_t *buf_ptr; |
0 | 259 int size, payload_size, startcode, id, len, stuffing_size, i, header_len; |
65 | 260 int64_t timestamp; |
261 uint8_t buffer[128]; | |
0 | 262 |
263 id = stream->id; | |
264 timestamp = stream->start_pts; | |
265 | |
266 #if 0 | |
267 printf("packet ID=%2x PTS=%0.3f\n", | |
268 id, timestamp / 90000.0); | |
269 #endif | |
270 | |
271 buf_ptr = buffer; | |
272 if (((s->packet_number % s->pack_header_freq) == 0)) { | |
273 /* output pack and systems header if needed */ | |
274 size = put_pack_header(ctx, buf_ptr, timestamp); | |
275 buf_ptr += size; | |
276 if ((s->packet_number % s->system_header_freq) == 0) { | |
277 size = put_system_header(ctx, buf_ptr); | |
278 buf_ptr += size; | |
279 } | |
280 } | |
281 size = buf_ptr - buffer; | |
282 put_buffer(&ctx->pb, buffer, size); | |
283 | |
284 /* packet header */ | |
285 if (s->is_mpeg2) { | |
286 header_len = 8; | |
287 } else { | |
288 header_len = 5; | |
289 } | |
242 | 290 payload_size = s->packet_size - (size + 6 + header_len); |
0 | 291 if (id < 0xc0) { |
292 startcode = PRIVATE_STREAM_1; | |
293 payload_size -= 4; | |
294 } else { | |
295 startcode = 0x100 + id; | |
296 } | |
297 stuffing_size = payload_size - stream->buffer_ptr; | |
298 if (stuffing_size < 0) | |
299 stuffing_size = 0; | |
300 | |
301 put_be32(&ctx->pb, startcode); | |
302 | |
303 put_be16(&ctx->pb, payload_size + header_len); | |
304 /* stuffing */ | |
305 for(i=0;i<stuffing_size;i++) | |
306 put_byte(&ctx->pb, 0xff); | |
307 | |
308 if (s->is_mpeg2) { | |
309 put_byte(&ctx->pb, 0x80); /* mpeg2 id */ | |
310 put_byte(&ctx->pb, 0x80); /* flags */ | |
311 put_byte(&ctx->pb, 0x05); /* header len (only pts is included) */ | |
312 } | |
313 put_byte(&ctx->pb, | |
314 (0x02 << 4) | | |
315 (((timestamp >> 30) & 0x07) << 1) | | |
316 1); | |
65 | 317 put_be16(&ctx->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1)); |
318 put_be16(&ctx->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1)); | |
0 | 319 |
320 if (startcode == PRIVATE_STREAM_1) { | |
321 put_byte(&ctx->pb, id); | |
322 if (id >= 0x80 && id <= 0xbf) { | |
323 /* XXX: need to check AC3 spec */ | |
324 put_byte(&ctx->pb, 1); | |
325 put_byte(&ctx->pb, 0); | |
326 put_byte(&ctx->pb, 2); | |
327 } | |
328 } | |
329 | |
330 /* output data */ | |
331 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size); | |
332 put_flush_packet(&ctx->pb); | |
333 | |
334 /* preserve remaining data */ | |
335 len = stream->buffer_ptr - payload_size; | |
336 if (len < 0) | |
337 len = 0; | |
338 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len); | |
339 stream->buffer_ptr = len; | |
340 | |
341 s->packet_number++; | |
342 stream->packet_number++; | |
343 stream->start_pts = -1; | |
344 } | |
345 | |
346 static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, | |
241 | 347 const uint8_t *buf, int size, int64_t pts) |
0 | 348 { |
349 MpegMuxContext *s = ctx->priv_data; | |
350 AVStream *st = ctx->streams[stream_index]; | |
351 StreamInfo *stream = st->priv_data; | |
352 int len; | |
353 | |
354 while (size > 0) { | |
355 /* set pts */ | |
356 if (stream->start_pts == -1) { | |
357 stream->start_pts = pts; | |
358 } | |
359 len = s->packet_data_max_size - stream->buffer_ptr; | |
360 if (len > size) | |
361 len = size; | |
362 memcpy(stream->buffer + stream->buffer_ptr, buf, len); | |
363 stream->buffer_ptr += len; | |
364 buf += len; | |
365 size -= len; | |
366 while (stream->buffer_ptr >= s->packet_data_max_size) { | |
367 /* output the packet */ | |
368 if (stream->start_pts == -1) | |
369 stream->start_pts = pts; | |
242 | 370 flush_packet(ctx, stream_index); |
0 | 371 } |
372 } | |
373 return 0; | |
374 } | |
375 | |
376 static int mpeg_mux_end(AVFormatContext *ctx) | |
377 { | |
378 StreamInfo *stream; | |
379 int i; | |
380 | |
381 /* flush each packet */ | |
382 for(i=0;i<ctx->nb_streams;i++) { | |
383 stream = ctx->streams[i]->priv_data; | |
384 if (stream->buffer_ptr > 0) { | |
242 | 385 flush_packet(ctx, i); |
0 | 386 } |
387 } | |
388 | |
242 | 389 /* End header according to MPEG1 systems standard. We do not write |
390 it as it is usually not needed by decoders and because it | |
391 complicates MPEG stream concatenation. */ | |
0 | 392 //put_be32(&ctx->pb, ISO_11172_END_CODE); |
393 //put_flush_packet(&ctx->pb); | |
237
35231c0be8e5
memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michaelni
parents:
210
diff
changeset
|
394 |
35231c0be8e5
memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michaelni
parents:
210
diff
changeset
|
395 for(i=0;i<ctx->nb_streams;i++) |
35231c0be8e5
memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michaelni
parents:
210
diff
changeset
|
396 av_freep(&ctx->streams[i]->priv_data); |
35231c0be8e5
memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michaelni
parents:
210
diff
changeset
|
397 |
0 | 398 return 0; |
399 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
276
diff
changeset
|
400 #endif //CONFIG_ENCODERS |
0 | 401 |
402 /*********************************************/ | |
403 /* demux code */ | |
404 | |
405 #define MAX_SYNC_SIZE 100000 | |
406 | |
407 static int mpegps_probe(AVProbeData *p) | |
408 { | |
165
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
409 int code, c, i; |
0 | 410 |
165
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
411 code = 0xff; |
0 | 412 /* we search the first start code. If it is a packet start code, |
413 then we decide it is mpeg ps. We do not send highest value to | |
414 give a chance to mpegts */ | |
49 | 415 /* NOTE: the search range was restricted to avoid too many false |
416 detections */ | |
417 | |
418 if (p->buf_size < 6) | |
419 return 0; | |
165
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
420 |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
421 for (i = 0; i < 20; i++) { |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
422 c = p->buf[i]; |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
423 code = (code << 8) | c; |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
424 if ((code & 0xffffff00) == 0x100) { |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
425 if (code == PACK_START_CODE || |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
426 code == SYSTEM_HEADER_START_CODE || |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
427 (code >= 0x1e0 && code <= 0x1ef) || |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
428 (code >= 0x1c0 && code <= 0x1df) || |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
429 code == PRIVATE_STREAM_2 || |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
430 code == PROGRAM_STREAM_MAP || |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
431 code == PRIVATE_STREAM_1 || |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
432 code == PADDING_STREAM) |
210 | 433 return AVPROBE_SCORE_MAX - 2; |
165
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
434 else |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
435 return 0; |
e4d2f704bf80
- Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents:
65
diff
changeset
|
436 } |
0 | 437 } |
438 return 0; | |
439 } | |
440 | |
441 | |
442 typedef struct MpegDemuxContext { | |
443 int header_state; | |
444 } MpegDemuxContext; | |
445 | |
446 static int find_start_code(ByteIOContext *pb, int *size_ptr, | |
65 | 447 uint32_t *header_state) |
0 | 448 { |
449 unsigned int state, v; | |
450 int val, n; | |
451 | |
452 state = *header_state; | |
453 n = *size_ptr; | |
454 while (n > 0) { | |
455 if (url_feof(pb)) | |
456 break; | |
457 v = get_byte(pb); | |
458 n--; | |
459 if (state == 0x000001) { | |
460 state = ((state << 8) | v) & 0xffffff; | |
461 val = state; | |
462 goto found; | |
463 } | |
464 state = ((state << 8) | v) & 0xffffff; | |
465 } | |
466 val = -1; | |
467 found: | |
468 *header_state = state; | |
469 *size_ptr = n; | |
470 return val; | |
471 } | |
472 | |
473 static int mpegps_read_header(AVFormatContext *s, | |
474 AVFormatParameters *ap) | |
475 { | |
476 MpegDemuxContext *m = s->priv_data; | |
477 m->header_state = 0xff; | |
478 /* no need to do more */ | |
479 return 0; | |
480 } | |
481 | |
65 | 482 static int64_t get_pts(ByteIOContext *pb, int c) |
0 | 483 { |
65 | 484 int64_t pts; |
0 | 485 int val; |
486 | |
487 if (c < 0) | |
488 c = get_byte(pb); | |
65 | 489 pts = (int64_t)((c >> 1) & 0x07) << 30; |
0 | 490 val = get_be16(pb); |
65 | 491 pts |= (int64_t)(val >> 1) << 15; |
0 | 492 val = get_be16(pb); |
65 | 493 pts |= (int64_t)(val >> 1); |
0 | 494 return pts; |
495 } | |
496 | |
497 static int mpegps_read_packet(AVFormatContext *s, | |
498 AVPacket *pkt) | |
499 { | |
500 MpegDemuxContext *m = s->priv_data; | |
501 AVStream *st; | |
502 int len, size, startcode, i, c, flags, header_len, type, codec_id; | |
65 | 503 int64_t pts, dts; |
0 | 504 |
505 /* next start code (should be immediately after) */ | |
506 redo: | |
507 m->header_state = 0xff; | |
508 size = MAX_SYNC_SIZE; | |
509 startcode = find_start_code(&s->pb, &size, &m->header_state); | |
510 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb)); | |
511 if (startcode < 0) | |
512 return -EIO; | |
513 if (startcode == PACK_START_CODE) | |
514 goto redo; | |
515 if (startcode == SYSTEM_HEADER_START_CODE) | |
516 goto redo; | |
517 if (startcode == PADDING_STREAM || | |
518 startcode == PRIVATE_STREAM_2) { | |
519 /* skip them */ | |
520 len = get_be16(&s->pb); | |
521 url_fskip(&s->pb, len); | |
522 goto redo; | |
523 } | |
524 /* find matching stream */ | |
525 if (!((startcode >= 0x1c0 && startcode <= 0x1df) || | |
526 (startcode >= 0x1e0 && startcode <= 0x1ef) || | |
527 (startcode == 0x1bd))) | |
528 goto redo; | |
529 | |
530 len = get_be16(&s->pb); | |
531 pts = AV_NOPTS_VALUE; | |
532 dts = AV_NOPTS_VALUE; | |
533 /* stuffing */ | |
534 for(;;) { | |
535 c = get_byte(&s->pb); | |
536 len--; | |
537 /* XXX: for mpeg1, should test only bit 7 */ | |
538 if (c != 0xff) | |
539 break; | |
540 } | |
541 if ((c & 0xc0) == 0x40) { | |
542 /* buffer scale & size */ | |
543 get_byte(&s->pb); | |
544 c = get_byte(&s->pb); | |
545 len -= 2; | |
546 } | |
547 if ((c & 0xf0) == 0x20) { | |
548 pts = get_pts(&s->pb, c); | |
549 len -= 4; | |
550 } else if ((c & 0xf0) == 0x30) { | |
551 pts = get_pts(&s->pb, c); | |
552 dts = get_pts(&s->pb, -1); | |
553 len -= 9; | |
554 } else if ((c & 0xc0) == 0x80) { | |
555 /* mpeg 2 PES */ | |
556 if ((c & 0x30) != 0) { | |
557 fprintf(stderr, "Encrypted multiplex not handled\n"); | |
558 return -EIO; | |
559 } | |
560 flags = get_byte(&s->pb); | |
561 header_len = get_byte(&s->pb); | |
562 len -= 2; | |
563 if (header_len > len) | |
564 goto redo; | |
565 if ((flags & 0xc0) == 0x80) { | |
566 pts = get_pts(&s->pb, -1); | |
567 header_len -= 5; | |
568 len -= 5; | |
569 } if ((flags & 0xc0) == 0xc0) { | |
570 pts = get_pts(&s->pb, -1); | |
571 dts = get_pts(&s->pb, -1); | |
572 header_len -= 10; | |
573 len -= 10; | |
574 } | |
575 len -= header_len; | |
576 while (header_len > 0) { | |
577 get_byte(&s->pb); | |
578 header_len--; | |
579 } | |
580 } | |
581 if (startcode == 0x1bd) { | |
582 startcode = get_byte(&s->pb); | |
583 len--; | |
584 if (startcode >= 0x80 && startcode <= 0xbf) { | |
585 /* audio: skip header */ | |
586 get_byte(&s->pb); | |
587 get_byte(&s->pb); | |
588 get_byte(&s->pb); | |
589 len -= 3; | |
590 } | |
591 } | |
592 | |
593 /* now find stream */ | |
594 for(i=0;i<s->nb_streams;i++) { | |
595 st = s->streams[i]; | |
596 if (st->id == startcode) | |
597 goto found; | |
598 } | |
599 if (startcode >= 0x1e0 && startcode <= 0x1ef) { | |
600 type = CODEC_TYPE_VIDEO; | |
601 codec_id = CODEC_ID_MPEG1VIDEO; | |
602 } else if (startcode >= 0x1c0 && startcode <= 0x1df) { | |
603 type = CODEC_TYPE_AUDIO; | |
604 codec_id = CODEC_ID_MP2; | |
605 } else if (startcode >= 0x80 && startcode <= 0x9f) { | |
606 type = CODEC_TYPE_AUDIO; | |
607 codec_id = CODEC_ID_AC3; | |
41 | 608 } else if (startcode >= 0xa0 && startcode <= 0xbf) { |
609 type = CODEC_TYPE_AUDIO; | |
610 codec_id = CODEC_ID_PCM_S16BE; | |
0 | 611 } else { |
612 skip: | |
613 /* skip packet */ | |
614 url_fskip(&s->pb, len); | |
615 goto redo; | |
616 } | |
617 /* no stream found: add a new stream */ | |
618 st = av_new_stream(s, startcode); | |
619 if (!st) | |
620 goto skip; | |
621 st->codec.codec_type = type; | |
622 st->codec.codec_id = codec_id; | |
623 found: | |
41 | 624 if (startcode >= 0xa0 && startcode <= 0xbf) { |
625 int b1, freq; | |
626 static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 }; | |
627 | |
628 /* for LPCM, we just skip the header and consider it is raw | |
629 audio data */ | |
630 if (len <= 3) | |
631 goto skip; | |
632 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */ | |
633 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */ | |
634 get_byte(&s->pb); /* dynamic range control (0x80 = off) */ | |
635 len -= 3; | |
636 freq = (b1 >> 4) & 3; | |
637 st->codec.sample_rate = lpcm_freq_tab[freq]; | |
638 st->codec.channels = 1 + (b1 & 7); | |
639 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2; | |
640 } | |
0 | 641 av_new_packet(pkt, len); |
642 //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode, | |
643 // (float)pts/90000, len); | |
644 get_buffer(&s->pb, pkt->data, pkt->size); | |
645 pkt->pts = pts; | |
646 pkt->stream_index = st->index; | |
647 return 0; | |
648 } | |
649 | |
650 static int mpegps_read_close(AVFormatContext *s) | |
651 { | |
652 return 0; | |
653 } | |
654 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
276
diff
changeset
|
655 #ifdef CONFIG_ENCODERS |
0 | 656 static AVOutputFormat mpeg1system_mux = { |
657 "mpeg", | |
658 "MPEG1 System format", | |
14
b167760cd0aa
mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents:
0
diff
changeset
|
659 "video/mpeg", |
0 | 660 "mpg,mpeg", |
661 sizeof(MpegMuxContext), | |
662 CODEC_ID_MP2, | |
663 CODEC_ID_MPEG1VIDEO, | |
664 mpeg_mux_init, | |
665 mpeg_mux_write_packet, | |
666 mpeg_mux_end, | |
667 }; | |
668 | |
669 static AVOutputFormat mpeg1vcd_mux = { | |
670 "vcd", | |
671 "MPEG1 System format (VCD)", | |
14
b167760cd0aa
mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents:
0
diff
changeset
|
672 "video/mpeg", |
0 | 673 NULL, |
674 sizeof(MpegMuxContext), | |
675 CODEC_ID_MP2, | |
676 CODEC_ID_MPEG1VIDEO, | |
677 mpeg_mux_init, | |
678 mpeg_mux_write_packet, | |
679 mpeg_mux_end, | |
680 }; | |
681 | |
682 static AVOutputFormat mpeg2vob_mux = { | |
683 "vob", | |
684 "MPEG2 PS format (VOB)", | |
14
b167760cd0aa
mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents:
0
diff
changeset
|
685 "video/mpeg", |
0 | 686 "vob", |
687 sizeof(MpegMuxContext), | |
688 CODEC_ID_MP2, | |
689 CODEC_ID_MPEG1VIDEO, | |
690 mpeg_mux_init, | |
691 mpeg_mux_write_packet, | |
692 mpeg_mux_end, | |
693 }; | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
276
diff
changeset
|
694 #endif //CONFIG_ENCODERS |
0 | 695 |
190 | 696 AVInputFormat mpegps_demux = { |
0 | 697 "mpeg", |
698 "MPEG PS format", | |
699 sizeof(MpegDemuxContext), | |
700 mpegps_probe, | |
701 mpegps_read_header, | |
702 mpegps_read_packet, | |
703 mpegps_read_close, | |
704 .flags = AVFMT_NOHEADER, | |
705 }; | |
706 | |
707 int mpegps_init(void) | |
708 { | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
276
diff
changeset
|
709 #ifdef CONFIG_ENCODERS |
0 | 710 av_register_output_format(&mpeg1system_mux); |
711 av_register_output_format(&mpeg1vcd_mux); | |
712 av_register_output_format(&mpeg2vob_mux); | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
276
diff
changeset
|
713 #endif //CONFIG_ENCODERS |
0 | 714 av_register_input_format(&mpegps_demux); |
715 return 0; | |
716 } |