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