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