better and simpler logic for MPEG muxing - fixed rare MPEG muxing PTS generation...
[libav.git] / libavformat / mpeg.c
CommitLineData
de6d9b64 1/*
fb7566d0 2 * MPEG1/2 mux/demux
19720f15 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20
21#define MAX_PAYLOAD_SIZE 4096
27f388aa 22//#define DEBUG_SEEK
de6d9b64
FB
23
24typedef struct {
0c1a9eda 25 uint8_t buffer[MAX_PAYLOAD_SIZE];
de6d9b64 26 int buffer_ptr;
0dbb48d9
FB
27 int nb_frames; /* number of starting frame encountered (AC3) */
28 int frame_start_offset; /* starting offset of the frame + 1 (0 if none) */
0c1a9eda 29 uint8_t id;
de6d9b64
FB
30 int max_buffer_size; /* in bytes */
31 int packet_number;
0c1a9eda 32 int64_t start_pts;
27a206e0 33 int64_t start_dts;
de6d9b64
FB
34} StreamInfo;
35
36typedef struct {
37 int packet_size; /* required packet size */
de6d9b64
FB
38 int packet_number;
39 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
40 int system_header_freq;
0dbb48d9 41 int system_header_size;
de6d9b64
FB
42 int mux_rate; /* bitrate in units of 50 bytes/s */
43 /* stream info */
44 int audio_bound;
45 int video_bound;
fb7566d0
FB
46 int is_mpeg2;
47 int is_vcd;
27a206e0
MB
48 int scr_stream_index; /* stream from which the system clock is
49 computed (VBR case) */
50 int64_t last_scr; /* current system clock */
de6d9b64
FB
51} MpegMuxContext;
52
53#define PACK_START_CODE ((unsigned int)0x000001ba)
54#define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
92b3e125 55#define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
de6d9b64
FB
56#define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
57#define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
58#define ISO_11172_END_CODE ((unsigned int)0x000001b9)
59
60/* mpeg2 */
61#define PROGRAM_STREAM_MAP 0x1bc
62#define PRIVATE_STREAM_1 0x1bd
63#define PADDING_STREAM 0x1be
64#define PRIVATE_STREAM_2 0x1bf
65
66
67#define AUDIO_ID 0xc0
68#define VIDEO_ID 0xe0
69
764ef400 70#ifdef CONFIG_ENCODERS
fb7566d0
FB
71extern AVOutputFormat mpeg1system_mux;
72extern AVOutputFormat mpeg1vcd_mux;
73extern AVOutputFormat mpeg2vob_mux;
74
de6d9b64 75static int put_pack_header(AVFormatContext *ctx,
0c1a9eda 76 uint8_t *buf, int64_t timestamp)
de6d9b64
FB
77{
78 MpegMuxContext *s = ctx->priv_data;
79 PutBitContext pb;
80
117a5490 81 init_put_bits(&pb, buf, 128);
de6d9b64
FB
82
83 put_bits(&pb, 32, PACK_START_CODE);
b2cac184 84 if (s->is_mpeg2) {
8683e4a0 85 put_bits(&pb, 2, 0x1);
b2cac184
FB
86 } else {
87 put_bits(&pb, 4, 0x2);
88 }
0c1a9eda 89 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
de6d9b64 90 put_bits(&pb, 1, 1);
0c1a9eda 91 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
de6d9b64 92 put_bits(&pb, 1, 1);
0c1a9eda 93 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
de6d9b64 94 put_bits(&pb, 1, 1);
b2cac184
FB
95 if (s->is_mpeg2) {
96 /* clock extension */
97 put_bits(&pb, 9, 0);
98 put_bits(&pb, 1, 1);
99 }
de6d9b64
FB
100 put_bits(&pb, 1, 1);
101 put_bits(&pb, 22, s->mux_rate);
102 put_bits(&pb, 1, 1);
b2cac184
FB
103 if (s->is_mpeg2) {
104 put_bits(&pb, 5, 0x1f); /* reserved */
105 put_bits(&pb, 3, 0); /* stuffing length */
106 }
de6d9b64 107 flush_put_bits(&pb);
17592475 108 return pbBufPtr(&pb) - pb.buf;
de6d9b64
FB
109}
110
0c1a9eda 111static int put_system_header(AVFormatContext *ctx, uint8_t *buf)
de6d9b64
FB
112{
113 MpegMuxContext *s = ctx->priv_data;
114 int size, rate_bound, i, private_stream_coded, id;
115 PutBitContext pb;
116
117a5490 117 init_put_bits(&pb, buf, 128);
de6d9b64
FB
118
119 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
120 put_bits(&pb, 16, 0);
121 put_bits(&pb, 1, 1);
122
123 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
124 put_bits(&pb, 22, rate_bound);
125 put_bits(&pb, 1, 1); /* marker */
126 put_bits(&pb, 6, s->audio_bound);
127
128 put_bits(&pb, 1, 1); /* variable bitrate */
129 put_bits(&pb, 1, 1); /* non constrainted bit stream */
130
131 put_bits(&pb, 1, 0); /* audio locked */
132 put_bits(&pb, 1, 0); /* video locked */
133 put_bits(&pb, 1, 1); /* marker */
134
135 put_bits(&pb, 5, s->video_bound);
136 put_bits(&pb, 8, 0xff); /* reserved byte */
137
138 /* audio stream info */
139 private_stream_coded = 0;
140 for(i=0;i<ctx->nb_streams;i++) {
141 StreamInfo *stream = ctx->streams[i]->priv_data;
142 id = stream->id;
143 if (id < 0xc0) {
144 /* special case for private streams (AC3 use that) */
145 if (private_stream_coded)
146 continue;
147 private_stream_coded = 1;
148 id = 0xbd;
149 }
150 put_bits(&pb, 8, id); /* stream ID */
151 put_bits(&pb, 2, 3);
152 if (id < 0xe0) {
153 /* audio */
154 put_bits(&pb, 1, 0);
155 put_bits(&pb, 13, stream->max_buffer_size / 128);
156 } else {
157 /* video */
158 put_bits(&pb, 1, 1);
159 put_bits(&pb, 13, stream->max_buffer_size / 1024);
160 }
161 }
162 flush_put_bits(&pb);
17592475 163 size = pbBufPtr(&pb) - pb.buf;
de6d9b64
FB
164 /* patch packet size */
165 buf[4] = (size - 6) >> 8;
166 buf[5] = (size - 6) & 0xff;
167
168 return size;
169}
170
0dbb48d9
FB
171static int get_system_header_size(AVFormatContext *ctx)
172{
173 int buf_index, i, private_stream_coded;
174 StreamInfo *stream;
175
176 buf_index = 12;
177 private_stream_coded = 0;
178 for(i=0;i<ctx->nb_streams;i++) {
179 stream = ctx->streams[i]->priv_data;
180 if (stream->id < 0xc0) {
181 if (private_stream_coded)
182 continue;
183 private_stream_coded = 1;
184 }
185 buf_index += 3;
186 }
187 return buf_index;
188}
189
de6d9b64
FB
190static int mpeg_mux_init(AVFormatContext *ctx)
191{
db7f1f95 192 MpegMuxContext *s = ctx->priv_data;
de6d9b64
FB
193 int bitrate, i, mpa_id, mpv_id, ac3_id;
194 AVStream *st;
195 StreamInfo *stream;
196
de6d9b64 197 s->packet_number = 0;
fb7566d0
FB
198 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
199 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux);
200
201 if (s->is_vcd)
92b3e125
J
202 s->packet_size = 2324; /* VCD packet size */
203 else
204 s->packet_size = 2048;
205
de6d9b64
FB
206 s->audio_bound = 0;
207 s->video_bound = 0;
208 mpa_id = AUDIO_ID;
209 ac3_id = 0x80;
210 mpv_id = VIDEO_ID;
27a206e0 211 s->scr_stream_index = -1;
de6d9b64
FB
212 for(i=0;i<ctx->nb_streams;i++) {
213 st = ctx->streams[i];
214 stream = av_mallocz(sizeof(StreamInfo));
215 if (!stream)
216 goto fail;
217 st->priv_data = stream;
218
219 switch(st->codec.codec_type) {
220 case CODEC_TYPE_AUDIO:
221 if (st->codec.codec_id == CODEC_ID_AC3)
222 stream->id = ac3_id++;
223 else
224 stream->id = mpa_id++;
225 stream->max_buffer_size = 4 * 1024;
226 s->audio_bound++;
227 break;
228 case CODEC_TYPE_VIDEO:
27a206e0
MB
229 /* by default, video is used for the SCR computation */
230 if (s->scr_stream_index == -1)
231 s->scr_stream_index = i;
de6d9b64
FB
232 stream->id = mpv_id++;
233 stream->max_buffer_size = 46 * 1024;
234 s->video_bound++;
235 break;
ac5e6a5b 236 default:
42343f7e 237 av_abort();
de6d9b64
FB
238 }
239 }
27a206e0
MB
240 /* if no SCR, use first stream (audio) */
241 if (s->scr_stream_index == -1)
242 s->scr_stream_index = 0;
de6d9b64
FB
243
244 /* we increase slightly the bitrate to take into account the
245 headers. XXX: compute it exactly */
246 bitrate = 2000;
247 for(i=0;i<ctx->nb_streams;i++) {
248 st = ctx->streams[i];
249 bitrate += st->codec.bit_rate;
250 }
251 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
92b3e125 252
fb7566d0 253 if (s->is_vcd || s->is_mpeg2)
92b3e125
J
254 /* every packet */
255 s->pack_header_freq = 1;
256 else
257 /* every 2 seconds */
258 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
b623bbcb
MN
259
260 /* the above seems to make pack_header_freq zero sometimes */
261 if (s->pack_header_freq == 0)
262 s->pack_header_freq = 1;
92b3e125 263
b2cac184
FB
264 if (s->is_mpeg2)
265 /* every 200 packets. Need to look at the spec. */
266 s->system_header_freq = s->pack_header_freq * 40;
267 else if (s->is_vcd)
92b3e125
J
268 /* every 40 packets, this is my invention */
269 s->system_header_freq = s->pack_header_freq * 40;
270 else
92b3e125
J
271 s->system_header_freq = s->pack_header_freq * 5;
272
de6d9b64
FB
273 for(i=0;i<ctx->nb_streams;i++) {
274 stream = ctx->streams[i]->priv_data;
275 stream->buffer_ptr = 0;
276 stream->packet_number = 0;
27a206e0
MB
277 stream->start_pts = AV_NOPTS_VALUE;
278 stream->start_dts = AV_NOPTS_VALUE;
de6d9b64 279 }
0dbb48d9 280 s->system_header_size = get_system_header_size(ctx);
27a206e0 281 s->last_scr = 0;
de6d9b64
FB
282 return 0;
283 fail:
284 for(i=0;i<ctx->nb_streams;i++) {
1ea4f593 285 av_free(ctx->streams[i]->priv_data);
de6d9b64 286 }
de6d9b64
FB
287 return -ENOMEM;
288}
289
27a206e0
MB
290static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
291{
292 put_byte(pb,
293 (id << 4) |
294 (((timestamp >> 30) & 0x07) << 1) |
295 1);
296 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
297 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
298}
299
0dbb48d9
FB
300
301/* return the exact available payload size for the next packet for
302 stream 'stream_index'. 'pts' and 'dts' are only used to know if
303 timestamps are needed in the packet header. */
304static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
305 int64_t pts, int64_t dts)
306{
307 MpegMuxContext *s = ctx->priv_data;
308 int buf_index;
309 StreamInfo *stream;
310
311 buf_index = 0;
312 if (((s->packet_number % s->pack_header_freq) == 0)) {
313 /* pack header size */
314 if (s->is_mpeg2)
315 buf_index += 14;
316 else
317 buf_index += 12;
318 if ((s->packet_number % s->system_header_freq) == 0)
319 buf_index += s->system_header_size;
320 }
321
322 /* packet header size */
323 buf_index += 6;
324 if (s->is_mpeg2)
325 buf_index += 3;
326 if (pts != AV_NOPTS_VALUE) {
327 if (dts != AV_NOPTS_VALUE)
328 buf_index += 5 + 5;
329 else
330 buf_index += 5;
331 } else {
332 if (!s->is_mpeg2)
333 buf_index++;
334 }
335
336 stream = ctx->streams[stream_index]->priv_data;
337 if (stream->id < 0xc0) {
338 /* AC3 private data header */
339 buf_index += 4;
340 }
341 return s->packet_size - buf_index;
342}
343
de6d9b64 344/* flush the packet on stream stream_index */
27a206e0
MB
345static void flush_packet(AVFormatContext *ctx, int stream_index,
346 int64_t pts, int64_t dts, int64_t scr)
de6d9b64
FB
347{
348 MpegMuxContext *s = ctx->priv_data;
349 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
0c1a9eda 350 uint8_t *buf_ptr;
0dbb48d9
FB
351 int size, payload_size, startcode, id, stuffing_size, i, header_len;
352 int packet_size;
0c1a9eda 353 uint8_t buffer[128];
92b3e125 354
de6d9b64 355 id = stream->id;
27a206e0 356
de6d9b64
FB
357#if 0
358 printf("packet ID=%2x PTS=%0.3f\n",
27a206e0 359 id, pts / 90000.0);
de6d9b64
FB
360#endif
361
362 buf_ptr = buffer;
92b3e125 363 if (((s->packet_number % s->pack_header_freq) == 0)) {
de6d9b64 364 /* output pack and systems header if needed */
27a206e0 365 size = put_pack_header(ctx, buf_ptr, scr);
de6d9b64
FB
366 buf_ptr += size;
367 if ((s->packet_number % s->system_header_freq) == 0) {
368 size = put_system_header(ctx, buf_ptr);
369 buf_ptr += size;
370 }
371 }
372 size = buf_ptr - buffer;
373 put_buffer(&ctx->pb, buffer, size);
374
375 /* packet header */
fb7566d0 376 if (s->is_mpeg2) {
27a206e0 377 header_len = 3;
fb7566d0 378 } else {
27a206e0 379 header_len = 0;
fb7566d0 380 }
27a206e0
MB
381 if (pts != AV_NOPTS_VALUE) {
382 if (dts != AV_NOPTS_VALUE)
383 header_len += 5 + 5;
384 else
385 header_len += 5;
386 } else {
387 if (!s->is_mpeg2)
388 header_len++;
389 }
390
0dbb48d9
FB
391 packet_size = s->packet_size - (size + 6);
392 payload_size = packet_size - header_len;
de6d9b64
FB
393 if (id < 0xc0) {
394 startcode = PRIVATE_STREAM_1;
395 payload_size -= 4;
396 } else {
397 startcode = 0x100 + id;
398 }
0dbb48d9 399
de6d9b64
FB
400 stuffing_size = payload_size - stream->buffer_ptr;
401 if (stuffing_size < 0)
402 stuffing_size = 0;
de6d9b64
FB
403 put_be32(&ctx->pb, startcode);
404
0dbb48d9 405 put_be16(&ctx->pb, packet_size);
de6d9b64
FB
406 /* stuffing */
407 for(i=0;i<stuffing_size;i++)
408 put_byte(&ctx->pb, 0xff);
fb7566d0
FB
409
410 if (s->is_mpeg2) {
411 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
27a206e0
MB
412
413 if (pts != AV_NOPTS_VALUE) {
414 if (dts != AV_NOPTS_VALUE) {
415 put_byte(&ctx->pb, 0xc0); /* flags */
416 put_byte(&ctx->pb, header_len - 3);
417 put_timestamp(&ctx->pb, 0x03, pts);
418 put_timestamp(&ctx->pb, 0x01, dts);
419 } else {
420 put_byte(&ctx->pb, 0x80); /* flags */
421 put_byte(&ctx->pb, header_len - 3);
422 put_timestamp(&ctx->pb, 0x02, pts);
423 }
424 } else {
425 put_byte(&ctx->pb, 0x00); /* flags */
426 put_byte(&ctx->pb, header_len - 3);
427 }
428 } else {
429 if (pts != AV_NOPTS_VALUE) {
430 if (dts != AV_NOPTS_VALUE) {
431 put_timestamp(&ctx->pb, 0x03, pts);
432 put_timestamp(&ctx->pb, 0x01, dts);
433 } else {
434 put_timestamp(&ctx->pb, 0x02, pts);
435 }
436 } else {
437 put_byte(&ctx->pb, 0x0f);
438 }
fb7566d0 439 }
de6d9b64
FB
440
441 if (startcode == PRIVATE_STREAM_1) {
442 put_byte(&ctx->pb, id);
443 if (id >= 0x80 && id <= 0xbf) {
0dbb48d9
FB
444 put_byte(&ctx->pb, stream->nb_frames);
445 put_be16(&ctx->pb, stream->frame_start_offset);
de6d9b64
FB
446 }
447 }
448
449 /* output data */
450 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
451 put_flush_packet(&ctx->pb);
452
de6d9b64
FB
453 s->packet_number++;
454 stream->packet_number++;
0dbb48d9
FB
455 stream->nb_frames = 0;
456 stream->frame_start_offset = 0;
de6d9b64
FB
457}
458
10bb7023 459static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
49057904 460 const uint8_t *buf, int size, int64_t pts)
de6d9b64
FB
461{
462 MpegMuxContext *s = ctx->priv_data;
463 AVStream *st = ctx->streams[stream_index];
464 StreamInfo *stream = st->priv_data;
0dbb48d9
FB
465 int64_t dts, new_start_pts, new_start_dts;
466 int len, avail_size;
27a206e0
MB
467
468 /* XXX: system clock should be computed precisely, especially for
469 CBR case. The current mode gives at least something coherent */
470 if (stream_index == s->scr_stream_index)
471 s->last_scr = pts;
10bb7023 472
27a206e0
MB
473#if 0
474 printf("%d: pts=%0.3f scr=%0.3f\n",
475 stream_index, pts / 90000.0, s->last_scr / 90000.0);
476#endif
477
478 /* XXX: currently no way to pass dts, will change soon */
479 dts = AV_NOPTS_VALUE;
480
481 /* we assume here that pts != AV_NOPTS_VALUE */
0dbb48d9
FB
482 new_start_pts = stream->start_pts;
483 new_start_dts = stream->start_dts;
484
27a206e0 485 if (stream->start_pts == AV_NOPTS_VALUE) {
0dbb48d9
FB
486 new_start_pts = pts;
487 new_start_dts = dts;
488 }
489 avail_size = get_packet_payload_size(ctx, stream_index,
490 new_start_pts,
491 new_start_dts);
492 if (stream->buffer_ptr >= avail_size) {
493 /* unlikely case: outputing the pts or dts increase the packet
494 size so that we cannot write the start of the next
495 packet. In this case, we must flush the current packet with
496 padding */
497 flush_packet(ctx, stream_index,
498 stream->start_pts, stream->start_dts, s->last_scr);
499 stream->buffer_ptr = 0;
27a206e0 500 }
0dbb48d9
FB
501 stream->start_pts = new_start_pts;
502 stream->start_dts = new_start_dts;
503 stream->nb_frames++;
504 if (stream->frame_start_offset == 0)
505 stream->frame_start_offset = stream->buffer_ptr;
de6d9b64 506 while (size > 0) {
0dbb48d9
FB
507 avail_size = get_packet_payload_size(ctx, stream_index,
508 stream->start_pts,
509 stream->start_dts);
510 len = avail_size - stream->buffer_ptr;
de6d9b64
FB
511 if (len > size)
512 len = size;
513 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
514 stream->buffer_ptr += len;
515 buf += len;
516 size -= len;
0dbb48d9
FB
517 if (stream->buffer_ptr >= avail_size) {
518 /* if packet full, we send it now */
27a206e0
MB
519 flush_packet(ctx, stream_index,
520 stream->start_pts, stream->start_dts, s->last_scr);
0dbb48d9 521 stream->buffer_ptr = 0;
27a206e0
MB
522 /* Make sure only the FIRST pes packet for this frame has
523 a timestamp */
524 stream->start_pts = AV_NOPTS_VALUE;
525 stream->start_dts = AV_NOPTS_VALUE;
de6d9b64
FB
526 }
527 }
0dbb48d9 528
de6d9b64
FB
529 return 0;
530}
531
532static int mpeg_mux_end(AVFormatContext *ctx)
533{
27a206e0 534 MpegMuxContext *s = ctx->priv_data;
de6d9b64
FB
535 StreamInfo *stream;
536 int i;
537
538 /* flush each packet */
539 for(i=0;i<ctx->nb_streams;i++) {
540 stream = ctx->streams[i]->priv_data;
0dbb48d9
FB
541 if (stream->buffer_ptr > 0) {
542 /* NOTE: we can always write the remaining data as it was
543 tested before in mpeg_mux_write_packet() */
544 flush_packet(ctx, i, stream->start_pts, stream->start_dts,
545 s->last_scr);
92b3e125 546 }
de6d9b64
FB
547 }
548
fa0f62c3
FB
549 /* End header according to MPEG1 systems standard. We do not write
550 it as it is usually not needed by decoders and because it
551 complicates MPEG stream concatenation. */
92b3e125
J
552 //put_be32(&ctx->pb, ISO_11172_END_CODE);
553 //put_flush_packet(&ctx->pb);
9d90c37f
MN
554
555 for(i=0;i<ctx->nb_streams;i++)
556 av_freep(&ctx->streams[i]->priv_data);
557
de6d9b64
FB
558 return 0;
559}
764ef400 560#endif //CONFIG_ENCODERS
de6d9b64
FB
561
562/*********************************************/
563/* demux code */
564
565#define MAX_SYNC_SIZE 100000
566
db7f1f95
FB
567static int mpegps_probe(AVProbeData *p)
568{
ec23a472 569 int code, c, i;
db7f1f95 570
ec23a472 571 code = 0xff;
db7f1f95
FB
572 /* we search the first start code. If it is a packet start code,
573 then we decide it is mpeg ps. We do not send highest value to
574 give a chance to mpegts */
fa777321
FB
575 /* NOTE: the search range was restricted to avoid too many false
576 detections */
577
578 if (p->buf_size < 6)
579 return 0;
ec23a472
IR
580
581 for (i = 0; i < 20; i++) {
582 c = p->buf[i];
583 code = (code << 8) | c;
584 if ((code & 0xffffff00) == 0x100) {
585 if (code == PACK_START_CODE ||
586 code == SYSTEM_HEADER_START_CODE ||
587 (code >= 0x1e0 && code <= 0x1ef) ||
588 (code >= 0x1c0 && code <= 0x1df) ||
589 code == PRIVATE_STREAM_2 ||
590 code == PROGRAM_STREAM_MAP ||
591 code == PRIVATE_STREAM_1 ||
592 code == PADDING_STREAM)
149f7c02 593 return AVPROBE_SCORE_MAX - 2;
ec23a472
IR
594 else
595 return 0;
596 }
db7f1f95
FB
597 }
598 return 0;
599}
600
601
de6d9b64
FB
602typedef struct MpegDemuxContext {
603 int header_state;
de6d9b64
FB
604} MpegDemuxContext;
605
27f388aa
FB
606static int mpegps_read_header(AVFormatContext *s,
607 AVFormatParameters *ap)
608{
609 MpegDemuxContext *m = s->priv_data;
610 m->header_state = 0xff;
611 s->ctx_flags |= AVFMTCTX_NOHEADER;
612
613 /* no need to do more */
614 return 0;
615}
616
617static int64_t get_pts(ByteIOContext *pb, int c)
618{
619 int64_t pts;
620 int val;
621
622 if (c < 0)
623 c = get_byte(pb);
624 pts = (int64_t)((c >> 1) & 0x07) << 30;
625 val = get_be16(pb);
626 pts |= (int64_t)(val >> 1) << 15;
627 val = get_be16(pb);
628 pts |= (int64_t)(val >> 1);
629 return pts;
630}
631
632static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
633 uint32_t *header_state)
de6d9b64
FB
634{
635 unsigned int state, v;
636 int val, n;
637
638 state = *header_state;
639 n = *size_ptr;
640 while (n > 0) {
641 if (url_feof(pb))
642 break;
643 v = get_byte(pb);
644 n--;
645 if (state == 0x000001) {
646 state = ((state << 8) | v) & 0xffffff;
647 val = state;
648 goto found;
649 }
650 state = ((state << 8) | v) & 0xffffff;
651 }
652 val = -1;
653 found:
654 *header_state = state;
655 *size_ptr = n;
656 return val;
657}
658
27f388aa
FB
659/* XXX: optimize */
660static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
de6d9b64 661{
27f388aa
FB
662 int64_t pos, pos_start;
663 int max_size, start_code;
da24c5e3 664
27f388aa
FB
665 max_size = *size_ptr;
666 pos_start = url_ftell(pb);
de6d9b64 667
27f388aa
FB
668 /* in order to go faster, we fill the buffer */
669 pos = pos_start - 16386;
670 if (pos < 0)
671 pos = 0;
672 url_fseek(pb, pos, SEEK_SET);
673 get_byte(pb);
de6d9b64 674
27f388aa
FB
675 pos = pos_start;
676 for(;;) {
677 pos--;
678 if (pos < 0 || (pos_start - pos) >= max_size) {
679 start_code = -1;
680 goto the_end;
681 }
682 url_fseek(pb, pos, SEEK_SET);
683 start_code = get_be32(pb);
684 if ((start_code & 0xffffff00) == 0x100)
685 break;
686 }
687 the_end:
688 *size_ptr = pos_start - pos;
689 return start_code;
de6d9b64
FB
690}
691
27f388aa
FB
692/* read the next (or previous) PES header. Return its position in ppos
693 (if not NULL), and its start code, pts and dts.
694 */
695static int mpegps_read_pes_header(AVFormatContext *s,
696 int64_t *ppos, int *pstart_code,
697 int64_t *ppts, int64_t *pdts, int find_next)
de6d9b64
FB
698{
699 MpegDemuxContext *m = s->priv_data;
27f388aa
FB
700 int len, size, startcode, c, flags, header_len;
701 int64_t pts, dts, last_pos;
de6d9b64 702
27f388aa 703 last_pos = -1;
de6d9b64 704 redo:
27f388aa
FB
705 if (find_next) {
706 /* next start code (should be immediately after) */
707 m->header_state = 0xff;
708 size = MAX_SYNC_SIZE;
709 startcode = find_next_start_code(&s->pb, &size, &m->header_state);
710 } else {
711 if (last_pos >= 0)
712 url_fseek(&s->pb, last_pos, SEEK_SET);
713 size = MAX_SYNC_SIZE;
714 startcode = find_prev_start_code(&s->pb, &size);
715 last_pos = url_ftell(&s->pb) - 4;
716 }
001e3f55 717 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
de6d9b64
FB
718 if (startcode < 0)
719 return -EIO;
720 if (startcode == PACK_START_CODE)
721 goto redo;
722 if (startcode == SYSTEM_HEADER_START_CODE)
723 goto redo;
724 if (startcode == PADDING_STREAM ||
725 startcode == PRIVATE_STREAM_2) {
726 /* skip them */
727 len = get_be16(&s->pb);
728 url_fskip(&s->pb, len);
729 goto redo;
730 }
731 /* find matching stream */
732 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
733 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
734 (startcode == 0x1bd)))
735 goto redo;
27f388aa
FB
736 if (ppos) {
737 *ppos = url_ftell(&s->pb) - 4;
738 }
de6d9b64 739 len = get_be16(&s->pb);
b2cac184
FB
740 pts = AV_NOPTS_VALUE;
741 dts = AV_NOPTS_VALUE;
de6d9b64
FB
742 /* stuffing */
743 for(;;) {
27f388aa
FB
744 if (len < 1)
745 goto redo;
de6d9b64
FB
746 c = get_byte(&s->pb);
747 len--;
748 /* XXX: for mpeg1, should test only bit 7 */
749 if (c != 0xff)
750 break;
751 }
752 if ((c & 0xc0) == 0x40) {
753 /* buffer scale & size */
27f388aa
FB
754 if (len < 2)
755 goto redo;
de6d9b64
FB
756 get_byte(&s->pb);
757 c = get_byte(&s->pb);
758 len -= 2;
759 }
760 if ((c & 0xf0) == 0x20) {
27f388aa
FB
761 if (len < 4)
762 goto redo;
763 dts = pts = get_pts(&s->pb, c);
de6d9b64 764 len -= 4;
de6d9b64 765 } else if ((c & 0xf0) == 0x30) {
27f388aa
FB
766 if (len < 9)
767 goto redo;
de6d9b64
FB
768 pts = get_pts(&s->pb, c);
769 dts = get_pts(&s->pb, -1);
770 len -= 9;
771 } else if ((c & 0xc0) == 0x80) {
772 /* mpeg 2 PES */
773 if ((c & 0x30) != 0) {
27f388aa
FB
774 /* Encrypted multiplex not handled */
775 goto redo;
de6d9b64
FB
776 }
777 flags = get_byte(&s->pb);
778 header_len = get_byte(&s->pb);
779 len -= 2;
780 if (header_len > len)
781 goto redo;
1e5c667c 782 if ((flags & 0xc0) == 0x80) {
27f388aa
FB
783 dts = pts = get_pts(&s->pb, -1);
784 if (header_len < 5)
785 goto redo;
de6d9b64
FB
786 header_len -= 5;
787 len -= 5;
788 } if ((flags & 0xc0) == 0xc0) {
789 pts = get_pts(&s->pb, -1);
790 dts = get_pts(&s->pb, -1);
27f388aa
FB
791 if (header_len < 10)
792 goto redo;
de6d9b64
FB
793 header_len -= 10;
794 len -= 10;
795 }
796 len -= header_len;
797 while (header_len > 0) {
798 get_byte(&s->pb);
799 header_len--;
800 }
801 }
802 if (startcode == 0x1bd) {
27f388aa
FB
803 if (len < 1)
804 goto redo;
de6d9b64
FB
805 startcode = get_byte(&s->pb);
806 len--;
807 if (startcode >= 0x80 && startcode <= 0xbf) {
808 /* audio: skip header */
27f388aa
FB
809 if (len < 3)
810 goto redo;
de6d9b64
FB
811 get_byte(&s->pb);
812 get_byte(&s->pb);
813 get_byte(&s->pb);
814 len -= 3;
815 }
816 }
27f388aa
FB
817 *pstart_code = startcode;
818 *ppts = pts;
819 *pdts = dts;
820 return len;
821}
822
823static int mpegps_read_packet(AVFormatContext *s,
824 AVPacket *pkt)
825{
826 AVStream *st;
827 int len, startcode, i, type, codec_id;
828 int64_t pts, dts;
829
830 redo:
831 len = mpegps_read_pes_header(s, NULL, &startcode, &pts, &dts, 1);
832 if (len < 0)
833 return len;
27a206e0 834
de6d9b64
FB
835 /* now find stream */
836 for(i=0;i<s->nb_streams;i++) {
837 st = s->streams[i];
838 if (st->id == startcode)
839 goto found;
840 }
db7f1f95
FB
841 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
842 type = CODEC_TYPE_VIDEO;
0dbb48d9 843 codec_id = CODEC_ID_MPEG2VIDEO;
db7f1f95
FB
844 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
845 type = CODEC_TYPE_AUDIO;
846 codec_id = CODEC_ID_MP2;
847 } else if (startcode >= 0x80 && startcode <= 0x9f) {
848 type = CODEC_TYPE_AUDIO;
849 codec_id = CODEC_ID_AC3;
9ec05e36
FB
850 } else if (startcode >= 0xa0 && startcode <= 0xbf) {
851 type = CODEC_TYPE_AUDIO;
852 codec_id = CODEC_ID_PCM_S16BE;
db7f1f95
FB
853 } else {
854 skip:
855 /* skip packet */
856 url_fskip(&s->pb, len);
857 goto redo;
858 }
1e5c667c
FB
859 /* no stream found: add a new stream */
860 st = av_new_stream(s, startcode);
861 if (!st)
862 goto skip;
db7f1f95
FB
863 st->codec.codec_type = type;
864 st->codec.codec_id = codec_id;
27f388aa
FB
865 if (codec_id != CODEC_ID_PCM_S16BE)
866 st->need_parsing = 1;
de6d9b64 867 found:
9ec05e36
FB
868 if (startcode >= 0xa0 && startcode <= 0xbf) {
869 int b1, freq;
870 static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
871
872 /* for LPCM, we just skip the header and consider it is raw
873 audio data */
874 if (len <= 3)
875 goto skip;
876 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
877 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
878 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
879 len -= 3;
880 freq = (b1 >> 4) & 3;
881 st->codec.sample_rate = lpcm_freq_tab[freq];
882 st->codec.channels = 1 + (b1 & 7);
883 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
884 }
de6d9b64
FB
885 av_new_packet(pkt, len);
886 get_buffer(&s->pb, pkt->data, pkt->size);
887 pkt->pts = pts;
27f388aa 888 pkt->dts = dts;
db7f1f95 889 pkt->stream_index = st->index;
27a206e0
MB
890#if 0
891 printf("%d: pts=%0.3f dts=%0.3f\n",
892 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0);
893#endif
de6d9b64
FB
894 return 0;
895}
896
db7f1f95 897static int mpegps_read_close(AVFormatContext *s)
de6d9b64 898{
de6d9b64
FB
899 return 0;
900}
901
27f388aa
FB
902static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
903 int64_t *ppos, int find_next)
904{
905 int len, startcode;
906 int64_t pos, pts, dts;
907
908 pos = *ppos;
909#ifdef DEBUG_SEEK
910 printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
911#endif
912 url_fseek(&s->pb, pos, SEEK_SET);
913 for(;;) {
914 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts, find_next);
915 if (len < 0) {
916#ifdef DEBUG_SEEK
917 printf("none (ret=%d)\n", len);
918#endif
919 return AV_NOPTS_VALUE;
920 }
921 if (startcode == s->streams[stream_index]->id &&
922 dts != AV_NOPTS_VALUE) {
923 break;
924 }
925 if (find_next) {
926 url_fskip(&s->pb, len);
927 } else {
928 url_fseek(&s->pb, pos, SEEK_SET);
929 }
930 }
931#ifdef DEBUG_SEEK
932 printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
933#endif
934 *ppos = pos;
935 return dts;
936}
937
938static int find_stream_index(AVFormatContext *s)
939{
940 int i;
941 AVStream *st;
942
943 if (s->nb_streams <= 0)
944 return -1;
945 for(i = 0; i < s->nb_streams; i++) {
946 st = s->streams[i];
947 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
948 return i;
949 }
950 }
951 return 0;
952}
953
954static int mpegps_read_seek(AVFormatContext *s,
955 int stream_index, int64_t timestamp)
956{
957 int64_t pos_min, pos_max, pos;
958 int64_t dts_min, dts_max, dts;
959
960 timestamp = (timestamp * 90000) / AV_TIME_BASE;
961
962#ifdef DEBUG_SEEK
963 printf("read_seek: %d %0.3f\n", stream_index, timestamp / 90000.0);
964#endif
965
966 /* XXX: find stream_index by looking at the first PES packet found */
967 if (stream_index < 0) {
968 stream_index = find_stream_index(s);
969 if (stream_index < 0)
970 return -1;
971 }
972 pos_min = 0;
973 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
974 if (dts_min == AV_NOPTS_VALUE) {
975 /* we can reach this case only if no PTS are present in
976 the whole stream */
977 return -1;
978 }
979 pos_max = url_filesize(url_fileno(&s->pb)) - 1;
980 dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
981
982 while (pos_min <= pos_max) {
983#ifdef DEBUG_SEEK
984 printf("pos_min=0x%llx pos_max=0x%llx dts_min=%0.3f dts_max=%0.3f\n",
985 pos_min, pos_max,
986 dts_min / 90000.0, dts_max / 90000.0);
987#endif
988 if (timestamp <= dts_min) {
989 pos = pos_min;
990 goto found;
991 } else if (timestamp >= dts_max) {
992 pos = pos_max;
993 goto found;
994 } else {
995 /* interpolate position (better than dichotomy) */
996 pos = (int64_t)((double)(pos_max - pos_min) *
997 (double)(timestamp - dts_min) /
998 (double)(dts_max - dts_min)) + pos_min;
999 }
1000#ifdef DEBUG_SEEK
1001 printf("pos=0x%llx\n", pos);
1002#endif
1003 /* read the next timestamp */
1004 dts = mpegps_read_dts(s, stream_index, &pos, 1);
1005 /* check if we are lucky */
1006 if (dts == AV_NOPTS_VALUE) {
1007 /* should never happen */
1008 pos = pos_min;
1009 goto found;
1010 } else if (timestamp == dts) {
1011 goto found;
1012 } else if (timestamp < dts) {
1013 pos_max = pos;
1014 dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
1015 if (dts_max == AV_NOPTS_VALUE) {
1016 /* should never happen */
1017 break;
1018 } else if (timestamp >= dts_max) {
1019 pos = pos_max;
1020 goto found;
1021 }
1022 } else {
1023 pos_min = pos + 1;
1024 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
1025 if (dts_min == AV_NOPTS_VALUE) {
1026 /* should never happen */
1027 goto found;
1028 } else if (timestamp <= dts_min) {
1029 goto found;
1030 }
1031 }
1032 }
1033 pos = pos_min;
1034 found:
1035#ifdef DEBUG_SEEK
1036 pos_min = pos;
1037 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
1038 pos_min++;
1039 dts_max = mpegps_read_dts(s, stream_index, &pos_min, 1);
1040 printf("pos=0x%llx %0.3f<=%0.3f<=%0.3f\n",
1041 pos, dts_min / 90000.0, timestamp / 90000.0, dts_max / 90000.0);
1042#endif
1043 /* do the seek */
1044 url_fseek(&s->pb, pos, SEEK_SET);
1045 return 0;
1046}
1047
764ef400 1048#ifdef CONFIG_ENCODERS
fb7566d0 1049static AVOutputFormat mpeg1system_mux = {
de6d9b64 1050 "mpeg",
fb7566d0 1051 "MPEG1 System format",
c6c11cb6 1052 "video/mpeg",
fb7566d0
FB
1053 "mpg,mpeg",
1054 sizeof(MpegMuxContext),
1055 CODEC_ID_MP2,
1056 CODEC_ID_MPEG1VIDEO,
1057 mpeg_mux_init,
1058 mpeg_mux_write_packet,
1059 mpeg_mux_end,
1060};
1061
1062static AVOutputFormat mpeg1vcd_mux = {
1063 "vcd",
1064 "MPEG1 System format (VCD)",
c6c11cb6 1065 "video/mpeg",
fb7566d0
FB
1066 NULL,
1067 sizeof(MpegMuxContext),
1068 CODEC_ID_MP2,
1069 CODEC_ID_MPEG1VIDEO,
1070 mpeg_mux_init,
1071 mpeg_mux_write_packet,
1072 mpeg_mux_end,
1073};
1074
1075static AVOutputFormat mpeg2vob_mux = {
1076 "vob",
1077 "MPEG2 PS format (VOB)",
c6c11cb6 1078 "video/mpeg",
fb7566d0 1079 "vob",
db7f1f95 1080 sizeof(MpegMuxContext),
de6d9b64 1081 CODEC_ID_MP2,
0dbb48d9 1082 CODEC_ID_MPEG2VIDEO,
de6d9b64
FB
1083 mpeg_mux_init,
1084 mpeg_mux_write_packet,
1085 mpeg_mux_end,
db7f1f95 1086};
764ef400 1087#endif //CONFIG_ENCODERS
de6d9b64 1088
32f38cb4 1089AVInputFormat mpegps_demux = {
db7f1f95
FB
1090 "mpeg",
1091 "MPEG PS format",
1092 sizeof(MpegDemuxContext),
1093 mpegps_probe,
1094 mpegps_read_header,
1095 mpegps_read_packet,
1096 mpegps_read_close,
27f388aa 1097 mpegps_read_seek,
de6d9b64 1098};
db7f1f95
FB
1099
1100int mpegps_init(void)
1101{
764ef400 1102#ifdef CONFIG_ENCODERS
fb7566d0
FB
1103 av_register_output_format(&mpeg1system_mux);
1104 av_register_output_format(&mpeg1vcd_mux);
1105 av_register_output_format(&mpeg2vob_mux);
764ef400 1106#endif //CONFIG_ENCODERS
db7f1f95
FB
1107 av_register_input_format(&mpegps_demux);
1108 return 0;
1109}