simplify put_padding_packet()
[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 23
b754978a
MN
24#undef NDEBUG
25#include <assert.h>
26
de6d9b64 27typedef struct {
0c1a9eda 28 uint8_t buffer[MAX_PAYLOAD_SIZE];
de6d9b64 29 int buffer_ptr;
0dbb48d9
FB
30 int nb_frames; /* number of starting frame encountered (AC3) */
31 int frame_start_offset; /* starting offset of the frame + 1 (0 if none) */
0c1a9eda 32 uint8_t id;
de6d9b64
FB
33 int max_buffer_size; /* in bytes */
34 int packet_number;
0c1a9eda 35 int64_t start_pts;
27a206e0 36 int64_t start_dts;
044007c2
FB
37 uint8_t lpcm_header[3];
38 int lpcm_align;
de6d9b64
FB
39} StreamInfo;
40
41typedef struct {
42 int packet_size; /* required packet size */
de6d9b64
FB
43 int packet_number;
44 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
45 int system_header_freq;
0dbb48d9 46 int system_header_size;
de6d9b64
FB
47 int mux_rate; /* bitrate in units of 50 bytes/s */
48 /* stream info */
49 int audio_bound;
50 int video_bound;
fb7566d0
FB
51 int is_mpeg2;
52 int is_vcd;
24515926 53 int is_svcd;
27a206e0
MB
54 int scr_stream_index; /* stream from which the system clock is
55 computed (VBR case) */
56 int64_t last_scr; /* current system clock */
24515926 57
d8b5abfa 58 double vcd_padding_bitrate; //FIXME floats
24515926
HD
59 int64_t vcd_padding_bytes_written;
60
de6d9b64
FB
61} MpegMuxContext;
62
63#define PACK_START_CODE ((unsigned int)0x000001ba)
64#define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
92b3e125 65#define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
de6d9b64
FB
66#define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
67#define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
68#define ISO_11172_END_CODE ((unsigned int)0x000001b9)
69
70/* mpeg2 */
71#define PROGRAM_STREAM_MAP 0x1bc
72#define PRIVATE_STREAM_1 0x1bd
73#define PADDING_STREAM 0x1be
74#define PRIVATE_STREAM_2 0x1bf
75
76
77#define AUDIO_ID 0xc0
78#define VIDEO_ID 0xe0
044007c2 79#define AC3_ID 0x80
23c99253 80#define DTS_ID 0x8a
044007c2 81#define LPCM_ID 0xa0
de6d9b64 82
8a05bca4
MN
83static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
84
764ef400 85#ifdef CONFIG_ENCODERS
7906085f
FH
86static AVOutputFormat mpeg1system_mux;
87static AVOutputFormat mpeg1vcd_mux;
88static AVOutputFormat mpeg2vob_mux;
89static AVOutputFormat mpeg2svcd_mux;
fb7566d0 90
de6d9b64 91static int put_pack_header(AVFormatContext *ctx,
0c1a9eda 92 uint8_t *buf, int64_t timestamp)
de6d9b64
FB
93{
94 MpegMuxContext *s = ctx->priv_data;
95 PutBitContext pb;
96
117a5490 97 init_put_bits(&pb, buf, 128);
de6d9b64
FB
98
99 put_bits(&pb, 32, PACK_START_CODE);
b2cac184 100 if (s->is_mpeg2) {
8683e4a0 101 put_bits(&pb, 2, 0x1);
b2cac184
FB
102 } else {
103 put_bits(&pb, 4, 0x2);
104 }
0c1a9eda 105 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
de6d9b64 106 put_bits(&pb, 1, 1);
0c1a9eda 107 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
de6d9b64 108 put_bits(&pb, 1, 1);
0c1a9eda 109 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
de6d9b64 110 put_bits(&pb, 1, 1);
b2cac184
FB
111 if (s->is_mpeg2) {
112 /* clock extension */
113 put_bits(&pb, 9, 0);
b2cac184 114 }
de6d9b64
FB
115 put_bits(&pb, 1, 1);
116 put_bits(&pb, 22, s->mux_rate);
117 put_bits(&pb, 1, 1);
b2cac184 118 if (s->is_mpeg2) {
4aa533be 119 put_bits(&pb, 1, 1);
b2cac184
FB
120 put_bits(&pb, 5, 0x1f); /* reserved */
121 put_bits(&pb, 3, 0); /* stuffing length */
122 }
de6d9b64 123 flush_put_bits(&pb);
17592475 124 return pbBufPtr(&pb) - pb.buf;
de6d9b64
FB
125}
126
24515926 127static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
de6d9b64
FB
128{
129 MpegMuxContext *s = ctx->priv_data;
130 int size, rate_bound, i, private_stream_coded, id;
131 PutBitContext pb;
132
117a5490 133 init_put_bits(&pb, buf, 128);
de6d9b64
FB
134
135 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
136 put_bits(&pb, 16, 0);
137 put_bits(&pb, 1, 1);
138
139 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
140 put_bits(&pb, 22, rate_bound);
141 put_bits(&pb, 1, 1); /* marker */
24515926
HD
142 if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
143 /* This header applies only to the video stream (see VCD standard p. IV-7)*/
144 put_bits(&pb, 6, 0);
145 } else
146 put_bits(&pb, 6, s->audio_bound);
de6d9b64 147
22494489
HD
148 if (s->is_vcd) {
149 /* see VCD standard, p. IV-7*/
150 put_bits(&pb, 1, 0);
151 put_bits(&pb, 1, 1);
152 } else {
153 put_bits(&pb, 1, 0); /* variable bitrate*/
154 put_bits(&pb, 1, 0); /* non constrainted bit stream */
155 }
de6d9b64 156
24515926
HD
157 if (s->is_vcd) {
158 /* see VCD standard p IV-7 */
159 put_bits(&pb, 1, 1); /* audio locked */
160 put_bits(&pb, 1, 1); /* video locked */
161 } else {
162 put_bits(&pb, 1, 0); /* audio locked */
163 put_bits(&pb, 1, 0); /* video locked */
164 }
165
de6d9b64
FB
166 put_bits(&pb, 1, 1); /* marker */
167
24515926
HD
168 if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
169 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
170 put_bits(&pb, 5, 0);
171 } else
172 put_bits(&pb, 5, s->video_bound);
173
de6d9b64
FB
174 put_bits(&pb, 8, 0xff); /* reserved byte */
175
176 /* audio stream info */
177 private_stream_coded = 0;
178 for(i=0;i<ctx->nb_streams;i++) {
179 StreamInfo *stream = ctx->streams[i]->priv_data;
24515926
HD
180
181 /* For VCDs, only include the stream info for the stream
182 that the pack which contains this system belongs to.
183 (see VCD standard p. IV-7) */
184 if ( !s->is_vcd || stream->id==only_for_stream_id
185 || only_for_stream_id==0) {
186
187 id = stream->id;
188 if (id < 0xc0) {
189 /* special case for private streams (AC3 use that) */
190 if (private_stream_coded)
191 continue;
192 private_stream_coded = 1;
193 id = 0xbd;
194 }
195 put_bits(&pb, 8, id); /* stream ID */
196 put_bits(&pb, 2, 3);
197 if (id < 0xe0) {
198 /* audio */
199 put_bits(&pb, 1, 0);
200 put_bits(&pb, 13, stream->max_buffer_size / 128);
201 } else {
202 /* video */
203 put_bits(&pb, 1, 1);
204 put_bits(&pb, 13, stream->max_buffer_size / 1024);
205 }
de6d9b64
FB
206 }
207 }
208 flush_put_bits(&pb);
17592475 209 size = pbBufPtr(&pb) - pb.buf;
de6d9b64
FB
210 /* patch packet size */
211 buf[4] = (size - 6) >> 8;
212 buf[5] = (size - 6) & 0xff;
213
214 return size;
215}
216
0dbb48d9
FB
217static int get_system_header_size(AVFormatContext *ctx)
218{
219 int buf_index, i, private_stream_coded;
220 StreamInfo *stream;
221
222 buf_index = 12;
223 private_stream_coded = 0;
224 for(i=0;i<ctx->nb_streams;i++) {
225 stream = ctx->streams[i]->priv_data;
226 if (stream->id < 0xc0) {
227 if (private_stream_coded)
228 continue;
229 private_stream_coded = 1;
230 }
231 buf_index += 3;
232 }
233 return buf_index;
234}
235
de6d9b64
FB
236static int mpeg_mux_init(AVFormatContext *ctx)
237{
db7f1f95 238 MpegMuxContext *s = ctx->priv_data;
23c99253 239 int bitrate, i, mpa_id, mpv_id, ac3_id, dts_id, lpcm_id, j;
de6d9b64
FB
240 AVStream *st;
241 StreamInfo *stream;
24515926
HD
242 int audio_bitrate;
243 int video_bitrate;
de6d9b64 244
de6d9b64 245 s->packet_number = 0;
fb7566d0 246 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
24515926
HD
247 s->is_svcd = (ctx->oformat == &mpeg2svcd_mux);
248 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux || ctx->oformat == &mpeg2svcd_mux);
fb7566d0 249
24515926
HD
250 if (s->is_vcd || s->is_svcd)
251 s->packet_size = 2324; /* VCD/SVCD packet size */
92b3e125
J
252 else
253 s->packet_size = 2048;
24515926
HD
254
255 s->vcd_padding_bytes_written = 0;
256 s->vcd_padding_bitrate=0;
92b3e125 257
de6d9b64
FB
258 s->audio_bound = 0;
259 s->video_bound = 0;
260 mpa_id = AUDIO_ID;
044007c2 261 ac3_id = AC3_ID;
23c99253 262 dts_id = DTS_ID;
de6d9b64 263 mpv_id = VIDEO_ID;
044007c2 264 lpcm_id = LPCM_ID;
27a206e0 265 s->scr_stream_index = -1;
de6d9b64
FB
266 for(i=0;i<ctx->nb_streams;i++) {
267 st = ctx->streams[i];
268 stream = av_mallocz(sizeof(StreamInfo));
269 if (!stream)
270 goto fail;
271 st->priv_data = stream;
272
273 switch(st->codec.codec_type) {
274 case CODEC_TYPE_AUDIO:
044007c2 275 if (st->codec.codec_id == CODEC_ID_AC3) {
de6d9b64 276 stream->id = ac3_id++;
23c99253
MN
277 } else if (st->codec.codec_id == CODEC_ID_DTS) {
278 stream->id = dts_id++;
044007c2
FB
279 } else if (st->codec.codec_id == CODEC_ID_PCM_S16BE) {
280 stream->id = lpcm_id++;
281 for(j = 0; j < 4; j++) {
282 if (lpcm_freq_tab[j] == st->codec.sample_rate)
283 break;
284 }
285 if (j == 4)
286 goto fail;
287 if (st->codec.channels > 8)
288 return -1;
289 stream->lpcm_header[0] = 0x0c;
290 stream->lpcm_header[1] = (st->codec.channels - 1) | (j << 4);
291 stream->lpcm_header[2] = 0x80;
292 stream->lpcm_align = st->codec.channels * 2;
293 } else {
de6d9b64 294 stream->id = mpa_id++;
044007c2 295 }
22494489
HD
296
297 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
298 Right now it is also used for everything else.*/
de6d9b64
FB
299 stream->max_buffer_size = 4 * 1024;
300 s->audio_bound++;
301 break;
302 case CODEC_TYPE_VIDEO:
27a206e0
MB
303 /* by default, video is used for the SCR computation */
304 if (s->scr_stream_index == -1)
305 s->scr_stream_index = i;
de6d9b64 306 stream->id = mpv_id++;
22494489
HD
307 if (s->is_vcd)
308 /* see VCD standard, p. IV-7*/
309 stream->max_buffer_size = 46 * 1024;
310 else
311 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
312 Right now it is also used for everything else.*/
313 stream->max_buffer_size = 230 * 1024;
de6d9b64
FB
314 s->video_bound++;
315 break;
ac5e6a5b 316 default:
71c32f19 317 return -1;
de6d9b64
FB
318 }
319 }
27a206e0
MB
320 /* if no SCR, use first stream (audio) */
321 if (s->scr_stream_index == -1)
322 s->scr_stream_index = 0;
de6d9b64 323
24515926
HD
324 bitrate = 0;
325 audio_bitrate = 0;
326 video_bitrate = 0;
de6d9b64
FB
327 for(i=0;i<ctx->nb_streams;i++) {
328 st = ctx->streams[i];
24515926
HD
329 stream = (StreamInfo*) st->priv_data;
330
de6d9b64 331 bitrate += st->codec.bit_rate;
24515926
HD
332
333 if (stream->id==AUDIO_ID)
334 audio_bitrate += st->codec.bit_rate;
335 else if (stream->id==VIDEO_ID)
336 video_bitrate += st->codec.bit_rate;
337 }
338
339 if (s->is_vcd) {
340 double overhead_rate;
341
342 /* The VCD standard mandates that the mux_rate field is 3528
343 (see standard p. IV-6).
344 The value is actually "wrong", i.e. if you calculate
345 it using the normal formula and the 75 sectors per second transfer
346 rate you get a different value because the real pack size is 2324,
347 not 2352. But the standard explicitly specifies that the mux_rate
348 field in the header must have this value.*/
349 s->mux_rate=2352 * 75 / 50; /* = 3528*/
350
351 /* The VCD standard states that the muxed stream must be
352 exactly 75 packs / second (the data rate of a single speed cdrom).
353 Since the video bitrate (probably 1150000 bits/sec) will be below
354 the theoretical maximum we have to add some padding packets
355 to make up for the lower data rate.
356 (cf. VCD standard p. IV-6 )*/
357
358 /* Add the header overhead to the data rate.
359 2279 data bytes per audio pack, 2294 data bytes per video pack*/
360 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
361 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
362 overhead_rate *= 8;
363
364 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
365 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
366
367 } else {
368 /* we increase slightly the bitrate to take into account the
369 headers. XXX: compute it exactly */
370 bitrate += 2000;
371 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
de6d9b64 372 }
92b3e125 373
fb7566d0 374 if (s->is_vcd || s->is_mpeg2)
92b3e125
J
375 /* every packet */
376 s->pack_header_freq = 1;
377 else
378 /* every 2 seconds */
379 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
b623bbcb
MN
380
381 /* the above seems to make pack_header_freq zero sometimes */
382 if (s->pack_header_freq == 0)
383 s->pack_header_freq = 1;
92b3e125 384
b2cac184
FB
385 if (s->is_mpeg2)
386 /* every 200 packets. Need to look at the spec. */
387 s->system_header_freq = s->pack_header_freq * 40;
388 else if (s->is_vcd)
24515926
HD
389 /* the standard mandates that there are only two system headers
390 in the whole file: one in the first packet of each stream.
391 (see standard p. IV-7 and IV-8) */
392 s->system_header_freq = 0x7fffffff;
92b3e125 393 else
92b3e125
J
394 s->system_header_freq = s->pack_header_freq * 5;
395
de6d9b64
FB
396 for(i=0;i<ctx->nb_streams;i++) {
397 stream = ctx->streams[i]->priv_data;
398 stream->buffer_ptr = 0;
399 stream->packet_number = 0;
27a206e0
MB
400 stream->start_pts = AV_NOPTS_VALUE;
401 stream->start_dts = AV_NOPTS_VALUE;
de6d9b64 402 }
0dbb48d9 403 s->system_header_size = get_system_header_size(ctx);
27a206e0 404 s->last_scr = 0;
de6d9b64
FB
405 return 0;
406 fail:
407 for(i=0;i<ctx->nb_streams;i++) {
1ea4f593 408 av_free(ctx->streams[i]->priv_data);
de6d9b64 409 }
de6d9b64
FB
410 return -ENOMEM;
411}
412
27a206e0
MB
413static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
414{
415 put_byte(pb,
416 (id << 4) |
417 (((timestamp >> 30) & 0x07) << 1) |
418 1);
419 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
420 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
421}
422
0dbb48d9 423
24515926
HD
424/* return the number of padding bytes that should be inserted into
425 the multiplexed stream.*/
426static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
427{
428 MpegMuxContext *s = ctx->priv_data;
429 int pad_bytes = 0;
430
431 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
432 {
433 int64_t full_pad_bytes;
434
435 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0);
436 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
437
438 if (pad_bytes<0)
439 /* might happen if we have already padded to a later timestamp. This
440 can occur if another stream has already advanced further.*/
441 pad_bytes=0;
442 }
443
444 return pad_bytes;
445}
446
447
0dbb48d9
FB
448/* return the exact available payload size for the next packet for
449 stream 'stream_index'. 'pts' and 'dts' are only used to know if
450 timestamps are needed in the packet header. */
451static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
452 int64_t pts, int64_t dts)
453{
454 MpegMuxContext *s = ctx->priv_data;
455 int buf_index;
456 StreamInfo *stream;
457
24515926
HD
458 stream = ctx->streams[stream_index]->priv_data;
459
0dbb48d9
FB
460 buf_index = 0;
461 if (((s->packet_number % s->pack_header_freq) == 0)) {
462 /* pack header size */
463 if (s->is_mpeg2)
464 buf_index += 14;
465 else
466 buf_index += 12;
24515926
HD
467
468 if (s->is_vcd) {
469 /* there is exactly one system header for each stream in a VCD MPEG,
470 One in the very first video packet and one in the very first
471 audio packet (see VCD standard p. IV-7 and IV-8).*/
472
473 if (stream->packet_number==0)
474 /* The system headers refer only to the stream they occur in,
475 so they have a constant size.*/
476 buf_index += 15;
477
478 } else {
479 if ((s->packet_number % s->system_header_freq) == 0)
480 buf_index += s->system_header_size;
481 }
0dbb48d9
FB
482 }
483
22494489
HD
484 if ((s->is_vcd && stream->packet_number==0)
485 || (s->is_svcd && s->packet_number==0))
24515926
HD
486 /* the first pack of each stream contains only the pack header,
487 the system header and some padding (see VCD standard p. IV-6)
488 Add the padding size, so that the actual payload becomes 0.*/
489 buf_index += s->packet_size - buf_index;
490 else {
491 /* packet header size */
492 buf_index += 6;
22494489 493 if (s->is_mpeg2) {
044007c2 494 buf_index += 3;
22494489
HD
495 if (stream->packet_number==0)
496 buf_index += 3; /* PES extension */
497 buf_index += 1; /* obligatory stuffing byte */
498 }
24515926
HD
499 if (pts != AV_NOPTS_VALUE) {
500 if (dts != pts)
501 buf_index += 5 + 5;
502 else
503 buf_index += 5;
504
505 } else {
506 if (!s->is_mpeg2)
507 buf_index++;
508 }
509
510 if (stream->id < 0xc0) {
511 /* AC3/LPCM private data header */
512 buf_index += 4;
513 if (stream->id >= 0xa0) {
514 int n;
515 buf_index += 3;
516 /* NOTE: we round the payload size to an integer number of
517 LPCM samples */
518 n = (s->packet_size - buf_index) % stream->lpcm_align;
519 if (n)
520 buf_index += (stream->lpcm_align - n);
521 }
044007c2 522 }
24515926
HD
523
524 if (s->is_vcd && stream->id == AUDIO_ID)
525 /* The VCD standard demands that 20 zero bytes follow
526 each audio packet (see standard p. IV-8).*/
527 buf_index+=20;
0dbb48d9
FB
528 }
529 return s->packet_size - buf_index;
530}
531
24515926 532/* Write an MPEG padding packet header. */
d8b5abfa 533static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
24515926
HD
534{
535 MpegMuxContext *s = ctx->priv_data;
d8b5abfa
MN
536 int i;
537
538 put_be32(pb, PADDING_STREAM);
539 put_be16(pb, packet_bytes - 6);
24515926 540 if (!s->is_mpeg2) {
d8b5abfa
MN
541 put_byte(pb, 0x0f);
542 packet_bytes -= 7;
24515926 543 } else
d8b5abfa 544 packet_bytes -= 6;
24515926
HD
545
546 for(i=0;i<packet_bytes;i++)
547 put_byte(pb, 0xff);
548}
549
550
de6d9b64 551/* flush the packet on stream stream_index */
27a206e0
MB
552static void flush_packet(AVFormatContext *ctx, int stream_index,
553 int64_t pts, int64_t dts, int64_t scr)
de6d9b64
FB
554{
555 MpegMuxContext *s = ctx->priv_data;
556 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
0c1a9eda 557 uint8_t *buf_ptr;
0dbb48d9
FB
558 int size, payload_size, startcode, id, stuffing_size, i, header_len;
559 int packet_size;
0c1a9eda 560 uint8_t buffer[128];
24515926
HD
561 int zero_trail_bytes = 0;
562 int pad_packet_bytes = 0;
22494489
HD
563 int pes_flags;
564 int general_pack = 0; /*"general" pack without data specific to one stream?*/
92b3e125 565
de6d9b64 566 id = stream->id;
27a206e0 567
de6d9b64
FB
568#if 0
569 printf("packet ID=%2x PTS=%0.3f\n",
27a206e0 570 id, pts / 90000.0);
de6d9b64
FB
571#endif
572
573 buf_ptr = buffer;
24515926 574
92b3e125 575 if (((s->packet_number % s->pack_header_freq) == 0)) {
de6d9b64 576 /* output pack and systems header if needed */
27a206e0 577 size = put_pack_header(ctx, buf_ptr, scr);
de6d9b64 578 buf_ptr += size;
24515926
HD
579
580 if (s->is_vcd) {
581 /* there is exactly one system header for each stream in a VCD MPEG,
582 One in the very first video packet and one in the very first
583 audio packet (see VCD standard p. IV-7 and IV-8).*/
584
585 if (stream->packet_number==0) {
586 size = put_system_header(ctx, buf_ptr, id);
587 buf_ptr += size;
588 }
589 } else {
590 if ((s->packet_number % s->system_header_freq) == 0) {
591 size = put_system_header(ctx, buf_ptr, 0);
592 buf_ptr += size;
593 }
de6d9b64
FB
594 }
595 }
596 size = buf_ptr - buffer;
597 put_buffer(&ctx->pb, buffer, size);
598
24515926
HD
599 packet_size = s->packet_size - size;
600
601 if (s->is_vcd && id == AUDIO_ID)
602 /* The VCD standard demands that 20 zero bytes follow
603 each audio pack (see standard p. IV-8).*/
604 zero_trail_bytes += 20;
605
22494489
HD
606 if ((s->is_vcd && stream->packet_number==0)
607 || (s->is_svcd && s->packet_number==0)) {
608 /* for VCD the first pack of each stream contains only the pack header,
24515926
HD
609 the system header and lots of padding (see VCD standard p. IV-6).
610 In the case of an audio pack, 20 zero bytes are also added at
611 the end.*/
22494489
HD
612 /* For SVCD we fill the very first pack to increase compatibility with
613 some DVD players. Not mandated by the standard.*/
614 if (s->is_svcd)
615 general_pack = 1; /* the system header refers to both streams and no stream data*/
24515926 616 pad_packet_bytes = packet_size - zero_trail_bytes;
27a206e0
MB
617 }
618
24515926 619 packet_size -= pad_packet_bytes + zero_trail_bytes;
0dbb48d9 620
24515926 621 if (packet_size > 0) {
de6d9b64 622
24515926
HD
623 /* packet header size */
624 packet_size -= 6;
625
626 /* packet header */
627 if (s->is_mpeg2) {
628 header_len = 3;
22494489
HD
629 if (stream->packet_number==0)
630 header_len += 3; /* PES extension */
631 header_len += 1; /* obligatory stuffing byte */
24515926
HD
632 } else {
633 header_len = 0;
634 }
635 if (pts != AV_NOPTS_VALUE) {
636 if (dts != pts)
637 header_len += 5 + 5;
638 else
639 header_len += 5;
640 } else {
641 if (!s->is_mpeg2)
642 header_len++;
643 }
4aa533be 644
24515926
HD
645 payload_size = packet_size - header_len;
646 if (id < 0xc0) {
647 startcode = PRIVATE_STREAM_1;
648 payload_size -= 4;
649 if (id >= 0xa0)
650 payload_size -= 3;
651 } else {
652 startcode = 0x100 + id;
653 }
fb7566d0 654
24515926
HD
655 stuffing_size = payload_size - stream->buffer_ptr;
656 if (stuffing_size < 0)
657 stuffing_size = 0;
22494489
HD
658 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
659 pad_packet_bytes += stuffing_size;
660 packet_size -= stuffing_size;
661 payload_size -= stuffing_size;
662 stuffing_size = 0;
663 }
664
24515926 665 put_be32(&ctx->pb, startcode);
27a206e0 666
24515926
HD
667 put_be16(&ctx->pb, packet_size);
668
669 if (!s->is_mpeg2)
670 for(i=0;i<stuffing_size;i++)
671 put_byte(&ctx->pb, 0xff);
672
673 if (s->is_mpeg2) {
674 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
675
22494489
HD
676 pes_flags=0;
677
24515926 678 if (pts != AV_NOPTS_VALUE) {
22494489
HD
679 pes_flags |= 0x80;
680 if (dts != pts)
681 pes_flags |= 0x40;
27a206e0 682 }
22494489
HD
683
684 /* Both the MPEG-2 and the SVCD standards demand that the
685 P-STD_buffer_size field be included in the first packet of
686 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
687 and MPEG-2 standard 2.7.7) */
688 if (stream->packet_number == 0)
689 pes_flags |= 0x01;
690
691 put_byte(&ctx->pb, pes_flags); /* flags */
692 put_byte(&ctx->pb, header_len - 3 + stuffing_size);
693
694 if (pes_flags & 0x80) /*write pts*/
695 put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
696 if (pes_flags & 0x40) /*write dts*/
697 put_timestamp(&ctx->pb, 0x01, dts);
698
699 if (pes_flags & 0x01) { /*write pes extension*/
700 put_byte(&ctx->pb, 0x10); /* flags */
701
702 /* P-STD buffer info */
703 if (id == AUDIO_ID)
704 put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
705 else
706 put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
707 }
708
27a206e0 709 } else {
24515926
HD
710 if (pts != AV_NOPTS_VALUE) {
711 if (dts != pts) {
712 put_timestamp(&ctx->pb, 0x03, pts);
713 put_timestamp(&ctx->pb, 0x01, dts);
714 } else {
715 put_timestamp(&ctx->pb, 0x02, pts);
716 }
27a206e0 717 } else {
24515926 718 put_byte(&ctx->pb, 0x0f);
27a206e0 719 }
27a206e0 720 }
de6d9b64 721
9e9080b6
SI
722 if (s->is_mpeg2) {
723 /* special stuffing byte that is always written
724 to prevent accidental generation of start codes. */
725 put_byte(&ctx->pb, 0xff);
726
727 for(i=0;i<stuffing_size;i++)
728 put_byte(&ctx->pb, 0xff);
729 }
730
24515926
HD
731 if (startcode == PRIVATE_STREAM_1) {
732 put_byte(&ctx->pb, id);
733 if (id >= 0xa0) {
734 /* LPCM (XXX: check nb_frames) */
735 put_byte(&ctx->pb, 7);
736 put_be16(&ctx->pb, 4); /* skip 3 header bytes */
737 put_byte(&ctx->pb, stream->lpcm_header[0]);
738 put_byte(&ctx->pb, stream->lpcm_header[1]);
739 put_byte(&ctx->pb, stream->lpcm_header[2]);
740 } else {
741 /* AC3 */
742 put_byte(&ctx->pb, stream->nb_frames);
743 put_be16(&ctx->pb, stream->frame_start_offset);
744 }
de6d9b64 745 }
24515926 746
24515926
HD
747 /* output data */
748 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
de6d9b64
FB
749 }
750
24515926
HD
751 if (pad_packet_bytes > 0)
752 put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
4aa533be 753
24515926
HD
754 for(i=0;i<zero_trail_bytes;i++)
755 put_byte(&ctx->pb, 0x00);
756
de6d9b64
FB
757 put_flush_packet(&ctx->pb);
758
de6d9b64 759 s->packet_number++;
22494489
HD
760
761 /* only increase the stream packet number if this pack actually contains
762 something that is specific to this stream! I.e. a dedicated header
763 or some data.*/
764 if (!general_pack)
765 stream->packet_number++;
0dbb48d9
FB
766 stream->nb_frames = 0;
767 stream->frame_start_offset = 0;
de6d9b64
FB
768}
769
24515926
HD
770static void put_vcd_padding_sector(AVFormatContext *ctx)
771{
772 /* There are two ways to do this padding: writing a sector/pack
773 of 0 values, or writing an MPEG padding pack. Both seem to
774 work with most decoders, BUT the VCD standard only allows a 0-sector
775 (see standard p. IV-4, IV-5).
776 So a 0-sector it is...*/
777
778 MpegMuxContext *s = ctx->priv_data;
779 int i;
780
781 for(i=0;i<s->packet_size;i++)
782 put_byte(&ctx->pb, 0);
783
784 s->vcd_padding_bytes_written += s->packet_size;
785
786 put_flush_packet(&ctx->pb);
787
788 /* increasing the packet number is correct. The SCR of the following packs
789 is calculated from the packet_number and it has to include the padding
790 sector (it represents the sector index, not the MPEG pack index)
791 (see VCD standard p. IV-6)*/
792 s->packet_number++;
793}
794
24515926
HD
795static int64_t update_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
796{
797 MpegMuxContext *s = ctx->priv_data;
798 int64_t scr;
22494489
HD
799 StreamInfo *stream;
800 int i;
24515926 801
22494489 802 if (s->is_vcd) {
24515926
HD
803 /* Since the data delivery rate is constant, SCR is computed
804 using the formula C + i * 1200 where C is the start constant
805 and i is the pack index.
806 It is recommended that SCR 0 is at the beginning of the VCD front
807 margin (a sequence of empty Form 2 sectors on the CD).
808 It is recommended that the front margin is 30 sectors long, so
809 we use C = 30*1200 = 36000
810 (Note that even if the front margin is not 30 sectors the file
811 will still be correct according to the standard. It just won't have
812 the "recommended" value).*/
813 scr = 36000 + s->packet_number * 1200;
22494489
HD
814
815
816#if 0
817 for(i=0;i<ctx->nb_streams;i++) {
818 stream = ctx->streams[i]->priv_data;
819
820 if(scr > stream->start_pts && stream->start_pts!=AV_NOPTS_VALUE) {
821 av_log(ctx, AV_LOG_DEBUG, "mpeg vcd: SCR above PTS (scr=%0.3f, stream index=%d, stream_pts=%0.3f).\n", scr/90000.0, i, stream->start_pts / 90000.0);
822 }
823 }
824#endif
825 }
24515926 826 else {
22494489
HD
827
828
24515926
HD
829 /* XXX I believe this calculation of SCR is wrong. SCR
830 specifies at which time the data should enter the decoder.
831 Two packs cannot enter the decoder at the same time. */
832
833 /* XXX: system clock should be computed precisely, especially for
834 CBR case. The current mode gives at least something coherent */
835 if (stream_index == s->scr_stream_index
836 && pts != AV_NOPTS_VALUE)
837 scr = pts;
838 else
839 scr = s->last_scr;
22494489
HD
840
841 /* "Sanity hack": make sure that the SCR does not overtake the pts of
842 buffered data that is still waiting to be written.*/
843 for(i=0;i<ctx->nb_streams;i++) {
844 stream = ctx->streams[i]->priv_data;
845
846 if(scr > stream->start_pts && stream->start_pts!=AV_NOPTS_VALUE) {
847 /* av_log(ctx, AV_LOG_DEBUG, "mpeg: restricting scr to stream pts (scr=%0.3f, stream index=%d, stream_pts=%0.3f).\n", scr/90000.0, i, stream->start_pts / 90000.0); */
848 scr = stream->start_pts;
849 }
850 }
24515926
HD
851 }
852
853 s->last_scr=scr;
854
855 return scr;
856}
857
858
e928649b 859static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
de6d9b64
FB
860{
861 MpegMuxContext *s = ctx->priv_data;
e928649b
MN
862 int stream_index= pkt->stream_index;
863 int size= pkt->size;
864 uint8_t *buf= pkt->data;
de6d9b64
FB
865 AVStream *st = ctx->streams[stream_index];
866 StreamInfo *stream = st->priv_data;
e45f1943 867 int64_t pts, dts, new_start_pts, new_start_dts;
0dbb48d9 868 int len, avail_size;
24515926 869
e928649b
MN
870 pts= pkt->pts;
871 dts= pkt->dts;
e45f1943 872
22494489
HD
873 if(s->is_svcd) {
874 /* offset pts and dts slightly into the future to be able
875 to do the compatibility fix below.*/
876 pts = (pts + 2) & ((1LL << 33) - 1);
877 dts = (dts + 2) & ((1LL << 33) - 1);
878
879 if (stream->packet_number == 0 && dts == pts)
880 /* For the very first packet we want to force the DTS to be included.
881 This increases compatibility with lots of DVD players.
882 Since the MPEG-2 standard mandates that DTS is only written when
883 it is different from PTS we have to move it slightly into the past.*/
884 dts = (dts - 2) & ((1LL << 33) - 1);
885 }
886 if(s->is_vcd) {
887 /* We have to offset the PTS, so that it is consistent with the SCR.
888 SCR starts at 36000, but the first two packs contain only padding
889 and the first pack from the other stream, respectively, may also have
890 been written before.
891 So the real data starts at SCR 36000+3*1200. */
892 pts = (pts + 36000 + 3600) & ((1LL << 33) - 1);
893 dts = (dts + 36000 + 3600) & ((1LL << 33) - 1);
894 }
10bb7023 895
27a206e0 896#if 0
24515926
HD
897 update_scr(ctx,stream_index,pts);
898
e45f1943
FB
899 printf("%d: pts=%0.3f dts=%0.3f scr=%0.3f\n",
900 stream_index,
901 pts / 90000.0,
902 dts / 90000.0,
903 s->last_scr / 90000.0);
27a206e0
MB
904#endif
905
27a206e0 906 /* we assume here that pts != AV_NOPTS_VALUE */
0dbb48d9
FB
907 new_start_pts = stream->start_pts;
908 new_start_dts = stream->start_dts;
909
27a206e0 910 if (stream->start_pts == AV_NOPTS_VALUE) {
0dbb48d9
FB
911 new_start_pts = pts;
912 new_start_dts = dts;
913 }
914 avail_size = get_packet_payload_size(ctx, stream_index,
915 new_start_pts,
916 new_start_dts);
917 if (stream->buffer_ptr >= avail_size) {
24515926
HD
918
919 update_scr(ctx,stream_index,stream->start_pts);
920
0dbb48d9
FB
921 /* unlikely case: outputing the pts or dts increase the packet
922 size so that we cannot write the start of the next
923 packet. In this case, we must flush the current packet with
24515926
HD
924 padding.
925 Note: this always happens for the first audio and video packet
926 in a VCD file, since they do not carry any data.*/
0dbb48d9
FB
927 flush_packet(ctx, stream_index,
928 stream->start_pts, stream->start_dts, s->last_scr);
929 stream->buffer_ptr = 0;
27a206e0 930 }
0dbb48d9
FB
931 stream->start_pts = new_start_pts;
932 stream->start_dts = new_start_dts;
933 stream->nb_frames++;
934 if (stream->frame_start_offset == 0)
573ab01e 935 stream->frame_start_offset = stream->buffer_ptr + 1;
de6d9b64 936 while (size > 0) {
0dbb48d9
FB
937 avail_size = get_packet_payload_size(ctx, stream_index,
938 stream->start_pts,
939 stream->start_dts);
940 len = avail_size - stream->buffer_ptr;
de6d9b64
FB
941 if (len > size)
942 len = size;
943 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
944 stream->buffer_ptr += len;
945 buf += len;
946 size -= len;
0dbb48d9 947 if (stream->buffer_ptr >= avail_size) {
24515926
HD
948
949 update_scr(ctx,stream_index,stream->start_pts);
950
0dbb48d9 951 /* if packet full, we send it now */
27a206e0
MB
952 flush_packet(ctx, stream_index,
953 stream->start_pts, stream->start_dts, s->last_scr);
0dbb48d9 954 stream->buffer_ptr = 0;
24515926
HD
955
956 if (s->is_vcd) {
957 /* Write one or more padding sectors, if necessary, to reach
958 the constant overall bitrate.*/
959 int vcd_pad_bytes;
960
961 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size)
962 put_vcd_padding_sector(ctx);
963 }
964
27a206e0
MB
965 /* Make sure only the FIRST pes packet for this frame has
966 a timestamp */
967 stream->start_pts = AV_NOPTS_VALUE;
968 stream->start_dts = AV_NOPTS_VALUE;
de6d9b64
FB
969 }
970 }
0dbb48d9 971
de6d9b64
FB
972 return 0;
973}
974
975static int mpeg_mux_end(AVFormatContext *ctx)
976{
27a206e0 977 MpegMuxContext *s = ctx->priv_data;
de6d9b64
FB
978 StreamInfo *stream;
979 int i;
980
981 /* flush each packet */
982 for(i=0;i<ctx->nb_streams;i++) {
983 stream = ctx->streams[i]->priv_data;
0dbb48d9 984 if (stream->buffer_ptr > 0) {
24515926
HD
985 update_scr(ctx,i,stream->start_pts);
986
0dbb48d9
FB
987 /* NOTE: we can always write the remaining data as it was
988 tested before in mpeg_mux_write_packet() */
989 flush_packet(ctx, i, stream->start_pts, stream->start_dts,
990 s->last_scr);
92b3e125 991 }
de6d9b64
FB
992 }
993
fa0f62c3
FB
994 /* End header according to MPEG1 systems standard. We do not write
995 it as it is usually not needed by decoders and because it
996 complicates MPEG stream concatenation. */
92b3e125
J
997 //put_be32(&ctx->pb, ISO_11172_END_CODE);
998 //put_flush_packet(&ctx->pb);
9d90c37f 999
de6d9b64
FB
1000 return 0;
1001}
764ef400 1002#endif //CONFIG_ENCODERS
de6d9b64
FB
1003
1004/*********************************************/
1005/* demux code */
1006
1007#define MAX_SYNC_SIZE 100000
1008
db7f1f95
FB
1009static int mpegps_probe(AVProbeData *p)
1010{
95f97de1
MN
1011 int i;
1012 int size= FFMIN(20, p->buf_size);
1013 uint32_t code=0xFF;
db7f1f95
FB
1014
1015 /* we search the first start code. If it is a packet start code,
1016 then we decide it is mpeg ps. We do not send highest value to
1017 give a chance to mpegts */
fa777321
FB
1018 /* NOTE: the search range was restricted to avoid too many false
1019 detections */
1020
95f97de1
MN
1021 for (i = 0; i < size; i++) {
1022 code = (code << 8) | p->buf[i];
ec23a472
IR
1023 if ((code & 0xffffff00) == 0x100) {
1024 if (code == PACK_START_CODE ||
1025 code == SYSTEM_HEADER_START_CODE ||
1026 (code >= 0x1e0 && code <= 0x1ef) ||
1027 (code >= 0x1c0 && code <= 0x1df) ||
1028 code == PRIVATE_STREAM_2 ||
1029 code == PROGRAM_STREAM_MAP ||
1030 code == PRIVATE_STREAM_1 ||
1031 code == PADDING_STREAM)
149f7c02 1032 return AVPROBE_SCORE_MAX - 2;
ec23a472
IR
1033 else
1034 return 0;
1035 }
db7f1f95
FB
1036 }
1037 return 0;
1038}
1039
1040
de6d9b64
FB
1041typedef struct MpegDemuxContext {
1042 int header_state;
de6d9b64
FB
1043} MpegDemuxContext;
1044
27f388aa
FB
1045static int mpegps_read_header(AVFormatContext *s,
1046 AVFormatParameters *ap)
1047{
1048 MpegDemuxContext *m = s->priv_data;
1049 m->header_state = 0xff;
1050 s->ctx_flags |= AVFMTCTX_NOHEADER;
1051
1052 /* no need to do more */
1053 return 0;
1054}
1055
1056static int64_t get_pts(ByteIOContext *pb, int c)
1057{
1058 int64_t pts;
1059 int val;
1060
1061 if (c < 0)
1062 c = get_byte(pb);
1063 pts = (int64_t)((c >> 1) & 0x07) << 30;
1064 val = get_be16(pb);
1065 pts |= (int64_t)(val >> 1) << 15;
1066 val = get_be16(pb);
1067 pts |= (int64_t)(val >> 1);
1068 return pts;
1069}
1070
1071static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
1072 uint32_t *header_state)
de6d9b64
FB
1073{
1074 unsigned int state, v;
1075 int val, n;
1076
1077 state = *header_state;
1078 n = *size_ptr;
1079 while (n > 0) {
1080 if (url_feof(pb))
1081 break;
1082 v = get_byte(pb);
1083 n--;
1084 if (state == 0x000001) {
1085 state = ((state << 8) | v) & 0xffffff;
1086 val = state;
1087 goto found;
1088 }
1089 state = ((state << 8) | v) & 0xffffff;
1090 }
1091 val = -1;
1092 found:
1093 *header_state = state;
1094 *size_ptr = n;
1095 return val;
1096}
1097
27f388aa
FB
1098/* XXX: optimize */
1099static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
de6d9b64 1100{
27f388aa
FB
1101 int64_t pos, pos_start;
1102 int max_size, start_code;
da24c5e3 1103
27f388aa
FB
1104 max_size = *size_ptr;
1105 pos_start = url_ftell(pb);
de6d9b64 1106
27f388aa
FB
1107 /* in order to go faster, we fill the buffer */
1108 pos = pos_start - 16386;
1109 if (pos < 0)
1110 pos = 0;
1111 url_fseek(pb, pos, SEEK_SET);
1112 get_byte(pb);
de6d9b64 1113
27f388aa
FB
1114 pos = pos_start;
1115 for(;;) {
1116 pos--;
1117 if (pos < 0 || (pos_start - pos) >= max_size) {
1118 start_code = -1;
1119 goto the_end;
1120 }
1121 url_fseek(pb, pos, SEEK_SET);
1122 start_code = get_be32(pb);
1123 if ((start_code & 0xffffff00) == 0x100)
1124 break;
1125 }
1126 the_end:
1127 *size_ptr = pos_start - pos;
1128 return start_code;
de6d9b64
FB
1129}
1130
8d14a25c 1131/* read the next PES header. Return its position in ppos
27f388aa
FB
1132 (if not NULL), and its start code, pts and dts.
1133 */
1134static int mpegps_read_pes_header(AVFormatContext *s,
1135 int64_t *ppos, int *pstart_code,
8d14a25c 1136 int64_t *ppts, int64_t *pdts)
de6d9b64
FB
1137{
1138 MpegDemuxContext *m = s->priv_data;
27f388aa
FB
1139 int len, size, startcode, c, flags, header_len;
1140 int64_t pts, dts, last_pos;
de6d9b64 1141
27f388aa 1142 last_pos = -1;
de6d9b64 1143 redo:
27f388aa
FB
1144 /* next start code (should be immediately after) */
1145 m->header_state = 0xff;
1146 size = MAX_SYNC_SIZE;
1147 startcode = find_next_start_code(&s->pb, &size, &m->header_state);
001e3f55 1148 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
de6d9b64 1149 if (startcode < 0)
0bd586c5 1150 return AVERROR_IO;
de6d9b64
FB
1151 if (startcode == PACK_START_CODE)
1152 goto redo;
1153 if (startcode == SYSTEM_HEADER_START_CODE)
1154 goto redo;
1155 if (startcode == PADDING_STREAM ||
1156 startcode == PRIVATE_STREAM_2) {
1157 /* skip them */
1158 len = get_be16(&s->pb);
1159 url_fskip(&s->pb, len);
1160 goto redo;
1161 }
1162 /* find matching stream */
1163 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
1164 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
1165 (startcode == 0x1bd)))
1166 goto redo;
27f388aa
FB
1167 if (ppos) {
1168 *ppos = url_ftell(&s->pb) - 4;
1169 }
de6d9b64 1170 len = get_be16(&s->pb);
b2cac184
FB
1171 pts = AV_NOPTS_VALUE;
1172 dts = AV_NOPTS_VALUE;
de6d9b64
FB
1173 /* stuffing */
1174 for(;;) {
27f388aa
FB
1175 if (len < 1)
1176 goto redo;
de6d9b64
FB
1177 c = get_byte(&s->pb);
1178 len--;
1179 /* XXX: for mpeg1, should test only bit 7 */
1180 if (c != 0xff)
1181 break;
1182 }
1183 if ((c & 0xc0) == 0x40) {
1184 /* buffer scale & size */
27f388aa
FB
1185 if (len < 2)
1186 goto redo;
de6d9b64
FB
1187 get_byte(&s->pb);
1188 c = get_byte(&s->pb);
1189 len -= 2;
1190 }
1191 if ((c & 0xf0) == 0x20) {
27f388aa
FB
1192 if (len < 4)
1193 goto redo;
1194 dts = pts = get_pts(&s->pb, c);
de6d9b64 1195 len -= 4;
de6d9b64 1196 } else if ((c & 0xf0) == 0x30) {
27f388aa
FB
1197 if (len < 9)
1198 goto redo;
de6d9b64
FB
1199 pts = get_pts(&s->pb, c);
1200 dts = get_pts(&s->pb, -1);
1201 len -= 9;
1202 } else if ((c & 0xc0) == 0x80) {
1203 /* mpeg 2 PES */
1204 if ((c & 0x30) != 0) {
27f388aa
FB
1205 /* Encrypted multiplex not handled */
1206 goto redo;
de6d9b64
FB
1207 }
1208 flags = get_byte(&s->pb);
1209 header_len = get_byte(&s->pb);
1210 len -= 2;
1211 if (header_len > len)
1212 goto redo;
1e5c667c 1213 if ((flags & 0xc0) == 0x80) {
27f388aa
FB
1214 dts = pts = get_pts(&s->pb, -1);
1215 if (header_len < 5)
1216 goto redo;
de6d9b64
FB
1217 header_len -= 5;
1218 len -= 5;
1219 } if ((flags & 0xc0) == 0xc0) {
1220 pts = get_pts(&s->pb, -1);
1221 dts = get_pts(&s->pb, -1);
27f388aa
FB
1222 if (header_len < 10)
1223 goto redo;
de6d9b64
FB
1224 header_len -= 10;
1225 len -= 10;
1226 }
1227 len -= header_len;
1228 while (header_len > 0) {
1229 get_byte(&s->pb);
1230 header_len--;
1231 }
1232 }
df70de1f
DB
1233 else if( c!= 0xf )
1234 goto redo;
1235
de6d9b64 1236 if (startcode == 0x1bd) {
27f388aa
FB
1237 if (len < 1)
1238 goto redo;
de6d9b64
FB
1239 startcode = get_byte(&s->pb);
1240 len--;
1241 if (startcode >= 0x80 && startcode <= 0xbf) {
1242 /* audio: skip header */
27f388aa
FB
1243 if (len < 3)
1244 goto redo;
de6d9b64
FB
1245 get_byte(&s->pb);
1246 get_byte(&s->pb);
1247 get_byte(&s->pb);
1248 len -= 3;
1249 }
1250 }
b754978a
MN
1251 if(dts != AV_NOPTS_VALUE && ppos){
1252 int i;
1253 for(i=0; i<s->nb_streams; i++){
1254 if(startcode == s->streams[i]->id) {
cdd5034f 1255 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0 /* FIXME keyframe? */);
b754978a
MN
1256 }
1257 }
1258 }
1259
27f388aa
FB
1260 *pstart_code = startcode;
1261 *ppts = pts;
1262 *pdts = dts;
1263 return len;
1264}
1265
1266static int mpegps_read_packet(AVFormatContext *s,
1267 AVPacket *pkt)
1268{
1269 AVStream *st;
0bd586c5 1270 int len, startcode, i, type, codec_id = 0;
b754978a 1271 int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
27f388aa
FB
1272
1273 redo:
8d14a25c 1274 len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
27f388aa
FB
1275 if (len < 0)
1276 return len;
27a206e0 1277
de6d9b64
FB
1278 /* now find stream */
1279 for(i=0;i<s->nb_streams;i++) {
1280 st = s->streams[i];
1281 if (st->id == startcode)
1282 goto found;
1283 }
db7f1f95
FB
1284 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
1285 type = CODEC_TYPE_VIDEO;
0dbb48d9 1286 codec_id = CODEC_ID_MPEG2VIDEO;
db7f1f95
FB
1287 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
1288 type = CODEC_TYPE_AUDIO;
1289 codec_id = CODEC_ID_MP2;
23c99253 1290 } else if (startcode >= 0x80 && startcode <= 0x89) {
db7f1f95
FB
1291 type = CODEC_TYPE_AUDIO;
1292 codec_id = CODEC_ID_AC3;
23c99253
MN
1293 } else if (startcode >= 0x8a && startcode <= 0x9f) {
1294 type = CODEC_TYPE_AUDIO;
1295 codec_id = CODEC_ID_DTS;
9ec05e36
FB
1296 } else if (startcode >= 0xa0 && startcode <= 0xbf) {
1297 type = CODEC_TYPE_AUDIO;
1298 codec_id = CODEC_ID_PCM_S16BE;
db7f1f95
FB
1299 } else {
1300 skip:
1301 /* skip packet */
1302 url_fskip(&s->pb, len);
1303 goto redo;
1304 }
1e5c667c
FB
1305 /* no stream found: add a new stream */
1306 st = av_new_stream(s, startcode);
1307 if (!st)
1308 goto skip;
db7f1f95
FB
1309 st->codec.codec_type = type;
1310 st->codec.codec_id = codec_id;
27f388aa
FB
1311 if (codec_id != CODEC_ID_PCM_S16BE)
1312 st->need_parsing = 1;
de6d9b64 1313 found:
9ec05e36
FB
1314 if (startcode >= 0xa0 && startcode <= 0xbf) {
1315 int b1, freq;
9ec05e36
FB
1316
1317 /* for LPCM, we just skip the header and consider it is raw
1318 audio data */
1319 if (len <= 3)
1320 goto skip;
1321 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
1322 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
1323 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
1324 len -= 3;
1325 freq = (b1 >> 4) & 3;
1326 st->codec.sample_rate = lpcm_freq_tab[freq];
1327 st->codec.channels = 1 + (b1 & 7);
1328 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
1329 }
de6d9b64
FB
1330 av_new_packet(pkt, len);
1331 get_buffer(&s->pb, pkt->data, pkt->size);
1332 pkt->pts = pts;
27f388aa 1333 pkt->dts = dts;
db7f1f95 1334 pkt->stream_index = st->index;
27a206e0 1335#if 0
3c895fc0 1336 av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f\n",
27a206e0
MB
1337 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0);
1338#endif
0bd586c5 1339
de6d9b64
FB
1340 return 0;
1341}
1342
db7f1f95 1343static int mpegps_read_close(AVFormatContext *s)
de6d9b64 1344{
de6d9b64
FB
1345 return 0;
1346}
1347
27f388aa 1348static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
8d14a25c 1349 int64_t *ppos, int64_t pos_limit)
27f388aa
FB
1350{
1351 int len, startcode;
1352 int64_t pos, pts, dts;
1353
1354 pos = *ppos;
1355#ifdef DEBUG_SEEK
1356 printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
1357#endif
1358 url_fseek(&s->pb, pos, SEEK_SET);
1359 for(;;) {
8d14a25c 1360 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
27f388aa
FB
1361 if (len < 0) {
1362#ifdef DEBUG_SEEK
1363 printf("none (ret=%d)\n", len);
1364#endif
1365 return AV_NOPTS_VALUE;
1366 }
1367 if (startcode == s->streams[stream_index]->id &&
1368 dts != AV_NOPTS_VALUE) {
1369 break;
1370 }
8d14a25c 1371 url_fskip(&s->pb, len);
27f388aa
FB
1372 }
1373#ifdef DEBUG_SEEK
1374 printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
1375#endif
1376 *ppos = pos;
cdd5034f 1377 return dts;
27f388aa
FB
1378}
1379
764ef400 1380#ifdef CONFIG_ENCODERS
fb7566d0 1381static AVOutputFormat mpeg1system_mux = {
de6d9b64 1382 "mpeg",
fb7566d0 1383 "MPEG1 System format",
c6c11cb6 1384 "video/mpeg",
fb7566d0
FB
1385 "mpg,mpeg",
1386 sizeof(MpegMuxContext),
1387 CODEC_ID_MP2,
1388 CODEC_ID_MPEG1VIDEO,
1389 mpeg_mux_init,
1390 mpeg_mux_write_packet,
1391 mpeg_mux_end,
1392};
1393
1394static AVOutputFormat mpeg1vcd_mux = {
1395 "vcd",
1396 "MPEG1 System format (VCD)",
c6c11cb6 1397 "video/mpeg",
fb7566d0
FB
1398 NULL,
1399 sizeof(MpegMuxContext),
1400 CODEC_ID_MP2,
1401 CODEC_ID_MPEG1VIDEO,
1402 mpeg_mux_init,
1403 mpeg_mux_write_packet,
1404 mpeg_mux_end,
1405};
1406
1407static AVOutputFormat mpeg2vob_mux = {
1408 "vob",
1409 "MPEG2 PS format (VOB)",
c6c11cb6 1410 "video/mpeg",
fb7566d0 1411 "vob",
db7f1f95 1412 sizeof(MpegMuxContext),
de6d9b64 1413 CODEC_ID_MP2,
0dbb48d9 1414 CODEC_ID_MPEG2VIDEO,
de6d9b64
FB
1415 mpeg_mux_init,
1416 mpeg_mux_write_packet,
1417 mpeg_mux_end,
db7f1f95 1418};
24515926
HD
1419
1420/* Same as mpeg2vob_mux except that the pack size is 2324 */
1421static AVOutputFormat mpeg2svcd_mux = {
1422 "svcd",
1423 "MPEG2 PS format (VOB)",
1424 "video/mpeg",
1425 "vob",
1426 sizeof(MpegMuxContext),
1427 CODEC_ID_MP2,
1428 CODEC_ID_MPEG2VIDEO,
1429 mpeg_mux_init,
1430 mpeg_mux_write_packet,
1431 mpeg_mux_end,
1432};
1433
1434
1435
764ef400 1436#endif //CONFIG_ENCODERS
de6d9b64 1437
32f38cb4 1438AVInputFormat mpegps_demux = {
db7f1f95
FB
1439 "mpeg",
1440 "MPEG PS format",
1441 sizeof(MpegDemuxContext),
1442 mpegps_probe,
1443 mpegps_read_header,
1444 mpegps_read_packet,
1445 mpegps_read_close,
8d14a25c
MN
1446 NULL, //mpegps_read_seek,
1447 mpegps_read_dts,
de6d9b64 1448};
db7f1f95
FB
1449
1450int mpegps_init(void)
1451{
764ef400 1452#ifdef CONFIG_ENCODERS
fb7566d0
FB
1453 av_register_output_format(&mpeg1system_mux);
1454 av_register_output_format(&mpeg1vcd_mux);
1455 av_register_output_format(&mpeg2vob_mux);
24515926 1456 av_register_output_format(&mpeg2svcd_mux);
764ef400 1457#endif //CONFIG_ENCODERS
db7f1f95
FB
1458 av_register_input_format(&mpegps_demux);
1459 return 0;
1460}