3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
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.
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.
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
20 #include "bitstream.h"
22 #define MAX_PAYLOAD_SIZE 4096
28 typedef struct PacketDesc
{
34 struct PacketDesc
*next
;
40 int max_buffer_size
; /* in bytes */
42 PacketDesc
*predecode_packet
;
43 PacketDesc
*premux_packet
;
44 PacketDesc
**next_packet
;
46 uint8_t lpcm_header
[3];
48 uint8_t *fifo_iframe_ptr
;
50 int64_t vobu_start_pts
;
54 int packet_size
; /* required packet size */
56 int pack_header_freq
; /* frequency (in packets^-1) at which we send pack headers */
57 int system_header_freq
;
58 int system_header_size
;
59 int mux_rate
; /* bitrate in units of 50 bytes/s */
67 int64_t last_scr
; /* current system clock */
69 double vcd_padding_bitrate
; //FIXME floats
70 int64_t vcd_padding_bytes_written
;
74 #define PACK_START_CODE ((unsigned int)0x000001ba)
75 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
76 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
77 #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
78 #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
79 #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
82 #define PROGRAM_STREAM_MAP 0x1bc
83 #define PRIVATE_STREAM_1 0x1bd
84 #define PADDING_STREAM 0x1be
85 #define PRIVATE_STREAM_2 0x1bf
94 static const int lpcm_freq_tab
[4] = { 48000, 96000, 44100, 32000 };
96 #ifdef CONFIG_ENCODERS
97 static AVOutputFormat mpeg1system_mux
;
98 static AVOutputFormat mpeg1vcd_mux
;
99 static AVOutputFormat mpeg2vob_mux
;
100 static AVOutputFormat mpeg2svcd_mux
;
101 static AVOutputFormat mpeg2dvd_mux
;
103 static int put_pack_header(AVFormatContext
*ctx
,
104 uint8_t *buf
, int64_t timestamp
)
106 MpegMuxContext
*s
= ctx
->priv_data
;
109 init_put_bits(&pb
, buf
, 128);
111 put_bits(&pb
, 32, PACK_START_CODE
);
113 put_bits(&pb
, 2, 0x1);
115 put_bits(&pb
, 4, 0x2);
117 put_bits(&pb
, 3, (uint32_t)((timestamp
>> 30) & 0x07));
119 put_bits(&pb
, 15, (uint32_t)((timestamp
>> 15) & 0x7fff));
121 put_bits(&pb
, 15, (uint32_t)((timestamp
) & 0x7fff));
124 /* clock extension */
128 put_bits(&pb
, 22, s
->mux_rate
);
132 put_bits(&pb
, 5, 0x1f); /* reserved */
133 put_bits(&pb
, 3, 0); /* stuffing length */
136 return pbBufPtr(&pb
) - pb
.buf
;
139 static int put_system_header(AVFormatContext
*ctx
, uint8_t *buf
,int only_for_stream_id
)
141 MpegMuxContext
*s
= ctx
->priv_data
;
142 int size
, i
, private_stream_coded
, id
;
145 init_put_bits(&pb
, buf
, 128);
147 put_bits(&pb
, 32, SYSTEM_HEADER_START_CODE
);
148 put_bits(&pb
, 16, 0);
151 put_bits(&pb
, 22, s
->mux_rate
); /* maximum bit rate of the multiplexed stream */
152 put_bits(&pb
, 1, 1); /* marker */
153 if (s
->is_vcd
&& only_for_stream_id
==VIDEO_ID
) {
154 /* This header applies only to the video stream (see VCD standard p. IV-7)*/
157 put_bits(&pb
, 6, s
->audio_bound
);
160 /* see VCD standard, p. IV-7*/
164 put_bits(&pb
, 1, 0); /* variable bitrate*/
165 put_bits(&pb
, 1, 0); /* non constrainted bit stream */
168 if (s
->is_vcd
|| s
->is_dvd
) {
169 /* see VCD standard p IV-7 */
170 put_bits(&pb
, 1, 1); /* audio locked */
171 put_bits(&pb
, 1, 1); /* video locked */
173 put_bits(&pb
, 1, 0); /* audio locked */
174 put_bits(&pb
, 1, 0); /* video locked */
177 put_bits(&pb
, 1, 1); /* marker */
179 if (s
->is_vcd
&& only_for_stream_id
==AUDIO_ID
) {
180 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
183 put_bits(&pb
, 5, s
->video_bound
);
186 put_bits(&pb
, 1, 0); /* packet_rate_restriction_flag */
187 put_bits(&pb
, 7, 0x7f); /* reserved byte */
189 put_bits(&pb
, 8, 0xff); /* reserved byte */
191 /* DVD-Video Stream_bound entries
192 id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
193 id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0)
194 id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
195 id (0xBF) private stream 2, NAV packs, set to 2x1024. */
198 int P_STD_max_video
= 0;
199 int P_STD_max_mpeg_audio
= 0;
200 int P_STD_max_mpeg_PS1
= 0;
202 for(i
=0;i
<ctx
->nb_streams
;i
++) {
203 StreamInfo
*stream
= ctx
->streams
[i
]->priv_data
;
206 if (id
== 0xbd && stream
->max_buffer_size
> P_STD_max_mpeg_PS1
) {
207 P_STD_max_mpeg_PS1
= stream
->max_buffer_size
;
208 } else if (id
>= 0xc0 && id
<= 0xc7 && stream
->max_buffer_size
> P_STD_max_mpeg_audio
) {
209 P_STD_max_mpeg_audio
= stream
->max_buffer_size
;
210 } else if (id
== 0xe0 && stream
->max_buffer_size
> P_STD_max_video
) {
211 P_STD_max_video
= stream
->max_buffer_size
;
216 put_bits(&pb
, 8, 0xb9); /* stream ID */
219 put_bits(&pb
, 13, P_STD_max_video
/ 1024);
222 if (P_STD_max_mpeg_audio
== 0)
223 P_STD_max_mpeg_audio
= 4096;
224 put_bits(&pb
, 8, 0xb8); /* stream ID */
227 put_bits(&pb
, 13, P_STD_max_mpeg_audio
/ 128);
229 /* private stream 1 */
230 put_bits(&pb
, 8, 0xbd); /* stream ID */
233 put_bits(&pb
, 13, P_STD_max_mpeg_PS1
/ 128);
235 /* private stream 2 */
236 put_bits(&pb
, 8, 0xbf); /* stream ID */
239 put_bits(&pb
, 13, 2);
242 /* audio stream info */
243 private_stream_coded
= 0;
244 for(i
=0;i
<ctx
->nb_streams
;i
++) {
245 StreamInfo
*stream
= ctx
->streams
[i
]->priv_data
;
248 /* For VCDs, only include the stream info for the stream
249 that the pack which contains this system belongs to.
250 (see VCD standard p. IV-7) */
251 if ( !s
->is_vcd
|| stream
->id
==only_for_stream_id
252 || only_for_stream_id
==0) {
256 /* special case for private streams (AC3 use that) */
257 if (private_stream_coded
)
259 private_stream_coded
= 1;
262 put_bits(&pb
, 8, id
); /* stream ID */
267 put_bits(&pb
, 13, stream
->max_buffer_size
/ 128);
271 put_bits(&pb
, 13, stream
->max_buffer_size
/ 1024);
278 size
= pbBufPtr(&pb
) - pb
.buf
;
279 /* patch packet size */
280 buf
[4] = (size
- 6) >> 8;
281 buf
[5] = (size
- 6) & 0xff;
286 static int get_system_header_size(AVFormatContext
*ctx
)
288 int buf_index
, i
, private_stream_coded
;
290 MpegMuxContext
*s
= ctx
->priv_data
;
293 return 18; // DVD-Video system headers are 18 bytes fixed length.
296 private_stream_coded
= 0;
297 for(i
=0;i
<ctx
->nb_streams
;i
++) {
298 stream
= ctx
->streams
[i
]->priv_data
;
299 if (stream
->id
< 0xc0) {
300 if (private_stream_coded
)
302 private_stream_coded
= 1;
309 static int mpeg_mux_init(AVFormatContext
*ctx
)
311 MpegMuxContext
*s
= ctx
->priv_data
;
312 int bitrate
, i
, mpa_id
, mpv_id
, ac3_id
, dts_id
, lpcm_id
, j
;
318 s
->packet_number
= 0;
319 s
->is_vcd
= (ctx
->oformat
== &mpeg1vcd_mux
);
320 s
->is_svcd
= (ctx
->oformat
== &mpeg2svcd_mux
);
321 s
->is_mpeg2
= (ctx
->oformat
== &mpeg2vob_mux
|| ctx
->oformat
== &mpeg2svcd_mux
|| ctx
->oformat
== &mpeg2dvd_mux
);
322 s
->is_dvd
= (ctx
->oformat
== &mpeg2dvd_mux
);
325 s
->packet_size
= ctx
->packet_size
;
327 s
->packet_size
= 2048;
329 s
->vcd_padding_bytes_written
= 0;
330 s
->vcd_padding_bitrate
=0;
339 for(i
=0;i
<ctx
->nb_streams
;i
++) {
340 st
= ctx
->streams
[i
];
341 stream
= av_mallocz(sizeof(StreamInfo
));
344 st
->priv_data
= stream
;
346 av_set_pts_info(st
, 64, 1, 90000);
348 switch(st
->codec
.codec_type
) {
349 case CODEC_TYPE_AUDIO
:
350 if (st
->codec
.codec_id
== CODEC_ID_AC3
) {
351 stream
->id
= ac3_id
++;
352 } else if (st
->codec
.codec_id
== CODEC_ID_DTS
) {
353 stream
->id
= dts_id
++;
354 } else if (st
->codec
.codec_id
== CODEC_ID_PCM_S16BE
) {
355 stream
->id
= lpcm_id
++;
356 for(j
= 0; j
< 4; j
++) {
357 if (lpcm_freq_tab
[j
] == st
->codec
.sample_rate
)
362 if (st
->codec
.channels
> 8)
364 stream
->lpcm_header
[0] = 0x0c;
365 stream
->lpcm_header
[1] = (st
->codec
.channels
- 1) | (j
<< 4);
366 stream
->lpcm_header
[2] = 0x80;
367 stream
->lpcm_align
= st
->codec
.channels
* 2;
369 stream
->id
= mpa_id
++;
372 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
373 Right now it is also used for everything else.*/
374 stream
->max_buffer_size
= 4 * 1024;
377 case CODEC_TYPE_VIDEO
:
378 stream
->id
= mpv_id
++;
379 if (st
->codec
.rc_buffer_size
)
380 stream
->max_buffer_size
= 6*1024 + st
->codec
.rc_buffer_size
/8;
382 stream
->max_buffer_size
= 230*1024; //FIXME this is probably too small as default
384 /* see VCD standard, p. IV-7*/
385 stream
->max_buffer_size
= 46 * 1024;
387 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
388 Right now it is also used for everything else.*/
389 stream
->max_buffer_size
= 230 * 1024;
396 fifo_init(&stream
->fifo
, 16);
397 stream
->next_packet
= &stream
->premux_packet
;
402 for(i
=0;i
<ctx
->nb_streams
;i
++) {
404 st
= ctx
->streams
[i
];
405 stream
= (StreamInfo
*) st
->priv_data
;
407 if(st
->codec
.rc_max_rate
|| stream
->id
==VIDEO_ID
)
408 codec_rate
= st
->codec
.rc_max_rate
;
410 codec_rate
= st
->codec
.bit_rate
;
413 codec_rate
= (1<<21)*8*50/ctx
->nb_streams
;
415 bitrate
+= codec_rate
;
417 if (stream
->id
==AUDIO_ID
)
418 audio_bitrate
+= codec_rate
;
419 else if (stream
->id
==VIDEO_ID
)
420 video_bitrate
+= codec_rate
;
424 s
->mux_rate
= (ctx
->mux_rate
+ (8 * 50) - 1) / (8 * 50);
426 /* we increase slightly the bitrate to take into account the
427 headers. XXX: compute it exactly */
428 bitrate
+= bitrate
*5/100;
430 s
->mux_rate
= (bitrate
+ (8 * 50) - 1) / (8 * 50);
434 double overhead_rate
;
436 /* The VCD standard mandates that the mux_rate field is 3528
437 (see standard p. IV-6).
438 The value is actually "wrong", i.e. if you calculate
439 it using the normal formula and the 75 sectors per second transfer
440 rate you get a different value because the real pack size is 2324,
441 not 2352. But the standard explicitly specifies that the mux_rate
442 field in the header must have this value.*/
443 // s->mux_rate=2352 * 75 / 50; /* = 3528*/
445 /* The VCD standard states that the muxed stream must be
446 exactly 75 packs / second (the data rate of a single speed cdrom).
447 Since the video bitrate (probably 1150000 bits/sec) will be below
448 the theoretical maximum we have to add some padding packets
449 to make up for the lower data rate.
450 (cf. VCD standard p. IV-6 )*/
452 /* Add the header overhead to the data rate.
453 2279 data bytes per audio pack, 2294 data bytes per video pack*/
454 overhead_rate
= ((audio_bitrate
/ 8.0) / 2279) * (2324 - 2279);
455 overhead_rate
+= ((video_bitrate
/ 8.0) / 2294) * (2324 - 2294);
458 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
459 s
->vcd_padding_bitrate
= 2324 * 75 * 8 - (bitrate
+ overhead_rate
);
462 if (s
->is_vcd
|| s
->is_mpeg2
)
464 s
->pack_header_freq
= 1;
466 /* every 2 seconds */
467 s
->pack_header_freq
= 2 * bitrate
/ s
->packet_size
/ 8;
469 /* the above seems to make pack_header_freq zero sometimes */
470 if (s
->pack_header_freq
== 0)
471 s
->pack_header_freq
= 1;
474 /* every 200 packets. Need to look at the spec. */
475 s
->system_header_freq
= s
->pack_header_freq
* 40;
477 /* the standard mandates that there are only two system headers
478 in the whole file: one in the first packet of each stream.
479 (see standard p. IV-7 and IV-8) */
480 s
->system_header_freq
= 0x7fffffff;
482 s
->system_header_freq
= s
->pack_header_freq
* 5;
484 for(i
=0;i
<ctx
->nb_streams
;i
++) {
485 stream
= ctx
->streams
[i
]->priv_data
;
486 stream
->packet_number
= 0;
488 s
->system_header_size
= get_system_header_size(ctx
);
492 for(i
=0;i
<ctx
->nb_streams
;i
++) {
493 av_free(ctx
->streams
[i
]->priv_data
);
498 static inline void put_timestamp(ByteIOContext
*pb
, int id
, int64_t timestamp
)
502 (((timestamp
>> 30) & 0x07) << 1) |
504 put_be16(pb
, (uint16_t)((((timestamp
>> 15) & 0x7fff) << 1) | 1));
505 put_be16(pb
, (uint16_t)((((timestamp
) & 0x7fff) << 1) | 1));
509 /* return the number of padding bytes that should be inserted into
510 the multiplexed stream.*/
511 static int get_vcd_padding_size(AVFormatContext
*ctx
, int64_t pts
)
513 MpegMuxContext
*s
= ctx
->priv_data
;
516 if (s
->vcd_padding_bitrate
> 0 && pts
!=AV_NOPTS_VALUE
)
518 int64_t full_pad_bytes
;
520 full_pad_bytes
= (int64_t)((s
->vcd_padding_bitrate
* (pts
/ 90000.0)) / 8.0); //FIXME this is wrong
521 pad_bytes
= (int) (full_pad_bytes
- s
->vcd_padding_bytes_written
);
524 /* might happen if we have already padded to a later timestamp. This
525 can occur if another stream has already advanced further.*/
533 #if 0 /* unused, remove? */
534 /* return the exact available payload size for the next packet for
535 stream 'stream_index'. 'pts' and 'dts' are only used to know if
536 timestamps are needed in the packet header. */
537 static int get_packet_payload_size(AVFormatContext
*ctx
, int stream_index
,
538 int64_t pts
, int64_t dts
)
540 MpegMuxContext
*s
= ctx
->priv_data
;
544 stream
= ctx
->streams
[stream_index
]->priv_data
;
547 if (((s
->packet_number
% s
->pack_header_freq
) == 0)) {
548 /* pack header size */
555 /* there is exactly one system header for each stream in a VCD MPEG,
556 One in the very first video packet and one in the very first
557 audio packet (see VCD standard p. IV-7 and IV-8).*/
559 if (stream
->packet_number
==0)
560 /* The system headers refer only to the stream they occur in,
561 so they have a constant size.*/
565 if ((s
->packet_number
% s
->system_header_freq
) == 0)
566 buf_index
+= s
->system_header_size
;
570 if ((s
->is_vcd
&& stream
->packet_number
==0)
571 || (s
->is_svcd
&& s
->packet_number
==0))
572 /* the first pack of each stream contains only the pack header,
573 the system header and some padding (see VCD standard p. IV-6)
574 Add the padding size, so that the actual payload becomes 0.*/
575 buf_index
+= s
->packet_size
- buf_index
;
577 /* packet header size */
581 if (stream
->packet_number
==0)
582 buf_index
+= 3; /* PES extension */
583 buf_index
+= 1; /* obligatory stuffing byte */
585 if (pts
!= AV_NOPTS_VALUE
) {
596 if (stream
->id
< 0xc0) {
597 /* AC3/LPCM private data header */
599 if (stream
->id
>= 0xa0) {
602 /* NOTE: we round the payload size to an integer number of
604 n
= (s
->packet_size
- buf_index
) % stream
->lpcm_align
;
606 buf_index
+= (stream
->lpcm_align
- n
);
610 if (s
->is_vcd
&& stream
->id
== AUDIO_ID
)
611 /* The VCD standard demands that 20 zero bytes follow
612 each audio packet (see standard p. IV-8).*/
615 return s
->packet_size
- buf_index
;
619 /* Write an MPEG padding packet header. */
620 static void put_padding_packet(AVFormatContext
*ctx
, ByteIOContext
*pb
,int packet_bytes
)
622 MpegMuxContext
*s
= ctx
->priv_data
;
625 put_be32(pb
, PADDING_STREAM
);
626 put_be16(pb
, packet_bytes
- 6);
633 for(i
=0;i
<packet_bytes
;i
++)
637 static int get_nb_frames(AVFormatContext
*ctx
, StreamInfo
*stream
, int len
){
639 PacketDesc
*pkt_desc
= stream
->premux_packet
;
642 if(pkt_desc
->size
== pkt_desc
->unwritten_size
)
644 len
-= pkt_desc
->unwritten_size
;
645 pkt_desc
= pkt_desc
->next
;
651 /* flush the packet on stream stream_index */
652 static int flush_packet(AVFormatContext
*ctx
, int stream_index
,
653 int64_t pts
, int64_t dts
, int64_t scr
, int trailer_size
)
655 MpegMuxContext
*s
= ctx
->priv_data
;
656 StreamInfo
*stream
= ctx
->streams
[stream_index
]->priv_data
;
658 int size
, payload_size
, startcode
, id
, stuffing_size
, i
, header_len
;
661 int zero_trail_bytes
= 0;
662 int pad_packet_bytes
= 0;
664 int general_pack
= 0; /*"general" pack without data specific to one stream?*/
670 printf("packet ID=%2x PTS=%0.3f\n",
676 if ((s
->packet_number
% s
->pack_header_freq
) == 0 || s
->last_scr
!= scr
) {
677 /* output pack and systems header if needed */
678 size
= put_pack_header(ctx
, buf_ptr
, scr
);
683 /* there is exactly one system header for each stream in a VCD MPEG,
684 One in the very first video packet and one in the very first
685 audio packet (see VCD standard p. IV-7 and IV-8).*/
687 if (stream
->packet_number
==0) {
688 size
= put_system_header(ctx
, buf_ptr
, id
);
691 } else if (s
->is_dvd
) {
692 if (stream
->align_iframe
|| s
->packet_number
== 0){
694 int PES_bytes_to_fill
;
695 if (stream
->fifo_iframe_ptr
>= stream
->fifo
.rptr
) {
696 bytes_to_iframe
= stream
->fifo_iframe_ptr
- stream
->fifo
.rptr
;
698 bytes_to_iframe
= (stream
->fifo
.end
- stream
->fifo
.rptr
) + (stream
->fifo_iframe_ptr
- stream
->fifo
.buffer
);
700 PES_bytes_to_fill
= s
->packet_size
- size
- 10;
702 if (pts
!= AV_NOPTS_VALUE
) {
704 PES_bytes_to_fill
-= 5 + 5;
706 PES_bytes_to_fill
-= 5;
709 if (bytes_to_iframe
== 0 || s
->packet_number
== 0) {
710 size
= put_system_header(ctx
, buf_ptr
, 0);
712 size
= buf_ptr
- buffer
;
713 put_buffer(&ctx
->pb
, buffer
, size
);
715 put_be32(&ctx
->pb
, PRIVATE_STREAM_2
);
716 put_be16(&ctx
->pb
, 0x03d4); // length
717 put_byte(&ctx
->pb
, 0x00); // substream ID, 00=PCI
718 for (i
= 0; i
< 979; i
++)
719 put_byte(&ctx
->pb
, 0x00);
721 put_be32(&ctx
->pb
, PRIVATE_STREAM_2
);
722 put_be16(&ctx
->pb
, 0x03fa); // length
723 put_byte(&ctx
->pb
, 0x01); // substream ID, 01=DSI
724 for (i
= 0; i
< 1017; i
++)
725 put_byte(&ctx
->pb
, 0x00);
727 memset(buffer
, 0, 128);
730 stream
->align_iframe
= 0;
731 scr
+= s
->packet_size
*90000LL / (s
->mux_rate
*50LL); //FIXME rounding and first few bytes of each packet
732 size
= put_pack_header(ctx
, buf_ptr
, scr
);
736 } else if (bytes_to_iframe
< PES_bytes_to_fill
) {
737 pad_packet_bytes
= PES_bytes_to_fill
- bytes_to_iframe
;
741 if ((s
->packet_number
% s
->system_header_freq
) == 0) {
742 size
= put_system_header(ctx
, buf_ptr
, 0);
747 size
= buf_ptr
- buffer
;
748 put_buffer(&ctx
->pb
, buffer
, size
);
750 packet_size
= s
->packet_size
- size
;
752 if (s
->is_vcd
&& id
== AUDIO_ID
)
753 /* The VCD standard demands that 20 zero bytes follow
754 each audio pack (see standard p. IV-8).*/
755 zero_trail_bytes
+= 20;
757 if ((s
->is_vcd
&& stream
->packet_number
==0)
758 || (s
->is_svcd
&& s
->packet_number
==0)) {
759 /* for VCD the first pack of each stream contains only the pack header,
760 the system header and lots of padding (see VCD standard p. IV-6).
761 In the case of an audio pack, 20 zero bytes are also added at
763 /* For SVCD we fill the very first pack to increase compatibility with
764 some DVD players. Not mandated by the standard.*/
766 general_pack
= 1; /* the system header refers to both streams and no stream data*/
767 pad_packet_bytes
= packet_size
- zero_trail_bytes
;
770 packet_size
-= pad_packet_bytes
+ zero_trail_bytes
;
772 if (packet_size
> 0) {
774 /* packet header size */
780 if (stream
->packet_number
==0)
781 header_len
+= 3; /* PES extension */
782 header_len
+= 1; /* obligatory stuffing byte */
786 if (pts
!= AV_NOPTS_VALUE
) {
796 payload_size
= packet_size
- header_len
;
798 startcode
= PRIVATE_STREAM_1
;
803 startcode
= 0x100 + id
;
806 stuffing_size
= payload_size
- fifo_size(&stream
->fifo
, stream
->fifo
.rptr
);
808 // first byte doesnt fit -> reset pts/dts + stuffing
809 if(payload_size
<= trailer_size
&& pts
!= AV_NOPTS_VALUE
){
813 if(pts
!= AV_NOPTS_VALUE
)
814 timestamp_len
+= s
->is_mpeg2 ?
5 : 4;
815 pts
=dts
= AV_NOPTS_VALUE
;
816 header_len
-= timestamp_len
;
817 if (s
->is_dvd
&& stream
->align_iframe
) {
818 pad_packet_bytes
+= timestamp_len
;
819 packet_size
-= timestamp_len
;
821 payload_size
+= timestamp_len
;
823 stuffing_size
+= timestamp_len
;
824 if(payload_size
> trailer_size
)
825 stuffing_size
+= payload_size
- trailer_size
;
828 if (pad_packet_bytes
> 0 && pad_packet_bytes
<= 7) { // can't use padding, so use stuffing
829 packet_size
+= pad_packet_bytes
;
830 payload_size
+= pad_packet_bytes
; // undo the previous adjustment
831 if (stuffing_size
< 0) {
832 stuffing_size
= pad_packet_bytes
;
834 stuffing_size
+= pad_packet_bytes
;
836 pad_packet_bytes
= 0;
839 if (stuffing_size
< 0)
841 if (stuffing_size
> 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
842 pad_packet_bytes
+= stuffing_size
;
843 packet_size
-= stuffing_size
;
844 payload_size
-= stuffing_size
;
848 nb_frames
= get_nb_frames(ctx
, stream
, payload_size
- stuffing_size
);
850 put_be32(&ctx
->pb
, startcode
);
852 put_be16(&ctx
->pb
, packet_size
);
855 for(i
=0;i
<stuffing_size
;i
++)
856 put_byte(&ctx
->pb
, 0xff);
859 put_byte(&ctx
->pb
, 0x80); /* mpeg2 id */
863 if (pts
!= AV_NOPTS_VALUE
) {
869 /* Both the MPEG-2 and the SVCD standards demand that the
870 P-STD_buffer_size field be included in the first packet of
871 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
872 and MPEG-2 standard 2.7.7) */
873 if (stream
->packet_number
== 0)
876 put_byte(&ctx
->pb
, pes_flags
); /* flags */
877 put_byte(&ctx
->pb
, header_len
- 3 + stuffing_size
);
879 if (pes_flags
& 0x80) /*write pts*/
880 put_timestamp(&ctx
->pb
, (pes_flags
& 0x40) ?
0x03 : 0x02, pts
);
881 if (pes_flags
& 0x40) /*write dts*/
882 put_timestamp(&ctx
->pb
, 0x01, dts
);
884 if (pes_flags
& 0x01) { /*write pes extension*/
885 put_byte(&ctx
->pb
, 0x10); /* flags */
887 /* P-STD buffer info */
889 put_be16(&ctx
->pb
, 0x4000 | stream
->max_buffer_size
/128);
891 put_be16(&ctx
->pb
, 0x6000 | stream
->max_buffer_size
/1024);
895 if (pts
!= AV_NOPTS_VALUE
) {
897 put_timestamp(&ctx
->pb
, 0x03, pts
);
898 put_timestamp(&ctx
->pb
, 0x01, dts
);
900 put_timestamp(&ctx
->pb
, 0x02, pts
);
903 put_byte(&ctx
->pb
, 0x0f);
908 /* special stuffing byte that is always written
909 to prevent accidental generation of start codes. */
910 put_byte(&ctx
->pb
, 0xff);
912 for(i
=0;i
<stuffing_size
;i
++)
913 put_byte(&ctx
->pb
, 0xff);
916 if (startcode
== PRIVATE_STREAM_1
) {
917 put_byte(&ctx
->pb
, id
);
919 /* LPCM (XXX: check nb_frames) */
920 put_byte(&ctx
->pb
, 7);
921 put_be16(&ctx
->pb
, 4); /* skip 3 header bytes */
922 put_byte(&ctx
->pb
, stream
->lpcm_header
[0]);
923 put_byte(&ctx
->pb
, stream
->lpcm_header
[1]);
924 put_byte(&ctx
->pb
, stream
->lpcm_header
[2]);
927 put_byte(&ctx
->pb
, nb_frames
);
928 put_be16(&ctx
->pb
, trailer_size
+1);
933 if(put_fifo(&ctx
->pb
, &stream
->fifo
, payload_size
- stuffing_size
, &stream
->fifo
.rptr
) < 0)
940 if (pad_packet_bytes
> 0)
941 put_padding_packet(ctx
,&ctx
->pb
, pad_packet_bytes
);
943 for(i
=0;i
<zero_trail_bytes
;i
++)
944 put_byte(&ctx
->pb
, 0x00);
946 put_flush_packet(&ctx
->pb
);
950 /* only increase the stream packet number if this pack actually contains
951 something that is specific to this stream! I.e. a dedicated header
954 stream
->packet_number
++;
956 return payload_size
- stuffing_size
;
959 static void put_vcd_padding_sector(AVFormatContext
*ctx
)
961 /* There are two ways to do this padding: writing a sector/pack
962 of 0 values, or writing an MPEG padding pack. Both seem to
963 work with most decoders, BUT the VCD standard only allows a 0-sector
964 (see standard p. IV-4, IV-5).
965 So a 0-sector it is...*/
967 MpegMuxContext
*s
= ctx
->priv_data
;
970 for(i
=0;i
<s
->packet_size
;i
++)
971 put_byte(&ctx
->pb
, 0);
973 s
->vcd_padding_bytes_written
+= s
->packet_size
;
975 put_flush_packet(&ctx
->pb
);
977 /* increasing the packet number is correct. The SCR of the following packs
978 is calculated from the packet_number and it has to include the padding
979 sector (it represents the sector index, not the MPEG pack index)
980 (see VCD standard p. IV-6)*/
984 #if 0 /* unused, remove? */
985 static int64_t get_vcd_scr(AVFormatContext
*ctx
,int stream_index
,int64_t pts
)
987 MpegMuxContext
*s
= ctx
->priv_data
;
990 /* Since the data delivery rate is constant, SCR is computed
991 using the formula C + i * 1200 where C is the start constant
992 and i is the pack index.
993 It is recommended that SCR 0 is at the beginning of the VCD front
994 margin (a sequence of empty Form 2 sectors on the CD).
995 It is recommended that the front margin is 30 sectors long, so
996 we use C = 30*1200 = 36000
997 (Note that even if the front margin is not 30 sectors the file
998 will still be correct according to the standard. It just won't have
999 the "recommended" value).*/
1000 scr
= 36000 + s
->packet_number
* 1200;
1006 static int remove_decoded_packets(AVFormatContext
*ctx
, int64_t scr
){
1007 // MpegMuxContext *s = ctx->priv_data;
1010 for(i
=0; i
<ctx
->nb_streams
; i
++){
1011 AVStream
*st
= ctx
->streams
[i
];
1012 StreamInfo
*stream
= st
->priv_data
;
1013 PacketDesc
*pkt_desc
= stream
->predecode_packet
;
1015 while(pkt_desc
&& scr
> pkt_desc
->dts
){ //FIXME > vs >=
1016 if(stream
->buffer_index
< pkt_desc
->size
||
1017 stream
->predecode_packet
== stream
->premux_packet
){
1018 av_log(ctx
, AV_LOG_ERROR
, "buffer underflow\n");
1021 stream
->buffer_index
-= pkt_desc
->size
;
1023 stream
->predecode_packet
= pkt_desc
->next
;
1024 av_freep(&pkt_desc
);
1031 static int output_packet(AVFormatContext
*ctx
, int flush
){
1032 MpegMuxContext
*s
= ctx
->priv_data
;
1035 int i
, avail_space
, es_size
, trailer_size
;
1037 int best_score
= INT_MIN
;
1038 int ignore_constraints
=0;
1039 int64_t scr
= s
->last_scr
;
1040 PacketDesc
*timestamp_packet
;
1041 const int64_t max_delay
= av_rescale(ctx
->max_delay
, 90000, AV_TIME_BASE
);
1044 for(i
=0; i
<ctx
->nb_streams
; i
++){
1045 AVStream
*st
= ctx
->streams
[i
];
1046 StreamInfo
*stream
= st
->priv_data
;
1047 const int avail_data
= fifo_size(&stream
->fifo
, stream
->fifo
.rptr
);
1048 const int space
= stream
->max_buffer_size
- stream
->buffer_index
;
1049 int rel_space
= 1024*space
/ stream
->max_buffer_size
;
1050 PacketDesc
*next_pkt
= stream
->premux_packet
;
1052 if(s
->packet_size
> avail_data
&& !flush
)
1056 assert(avail_data
>0);
1058 if(space
< s
->packet_size
&& !ignore_constraints
)
1061 if(next_pkt
&& next_pkt
->dts
- scr
> max_delay
)
1064 if(rel_space
> best_score
){
1065 best_score
= rel_space
;
1072 int64_t best_dts
= INT64_MAX
;
1074 for(i
=0; i
<ctx
->nb_streams
; i
++){
1075 AVStream
*st
= ctx
->streams
[i
];
1076 StreamInfo
*stream
= st
->priv_data
;
1077 PacketDesc
*pkt_desc
= stream
->predecode_packet
;
1078 if(pkt_desc
&& pkt_desc
->dts
< best_dts
)
1079 best_dts
= pkt_desc
->dts
;
1083 av_log(ctx
, AV_LOG_DEBUG
, "bumping scr, scr:%f, dts:%f\n",
1084 scr
/90000.0, best_dts
/90000.0);
1086 if(best_dts
== INT64_MAX
)
1089 if(scr
>= best_dts
+1 && !ignore_constraints
){
1090 av_log(ctx
, AV_LOG_ERROR
, "packet too large, ignoring buffer limits to mux it\n");
1091 ignore_constraints
= 1;
1093 scr
= FFMAX(best_dts
+1, scr
);
1094 if(remove_decoded_packets(ctx
, scr
) < 0)
1099 assert(best_i
>= 0);
1101 st
= ctx
->streams
[best_i
];
1102 stream
= st
->priv_data
;
1104 assert(fifo_size(&stream
->fifo
, stream
->fifo
.rptr
) > 0);
1106 assert(avail_space
>= s
->packet_size
|| ignore_constraints
);
1108 timestamp_packet
= stream
->premux_packet
;
1109 if(timestamp_packet
->unwritten_size
== timestamp_packet
->size
){
1112 trailer_size
= timestamp_packet
->unwritten_size
;
1113 timestamp_packet
= timestamp_packet
->next
;
1116 if(timestamp_packet
){
1117 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f scr:%f stream:%d\n", timestamp_packet->dts/90000.0, timestamp_packet->pts/90000.0, scr/90000.0, best_i);
1118 es_size
= flush_packet(ctx
, best_i
, timestamp_packet
->pts
, timestamp_packet
->dts
, scr
, trailer_size
);
1120 assert(fifo_size(&stream
->fifo
, stream
->fifo
.rptr
) == trailer_size
);
1121 es_size
= flush_packet(ctx
, best_i
, AV_NOPTS_VALUE
, AV_NOPTS_VALUE
, scr
, trailer_size
);
1125 /* Write one or more padding sectors, if necessary, to reach
1126 the constant overall bitrate.*/
1129 while((vcd_pad_bytes
= get_vcd_padding_size(ctx
,stream
->premux_packet
->pts
) ) >= s
->packet_size
){ //FIXME pts cannot be correct here
1130 put_vcd_padding_sector(ctx
);
1131 s
->last_scr
+= s
->packet_size
*90000LL / (s
->mux_rate
*50LL); //FIXME rounding and first few bytes of each packet
1135 stream
->buffer_index
+= es_size
;
1136 s
->last_scr
+= s
->packet_size
*90000LL / (s
->mux_rate
*50LL); //FIXME rounding and first few bytes of each packet
1138 while(stream
->premux_packet
&& stream
->premux_packet
->unwritten_size
<= es_size
){
1139 es_size
-= stream
->premux_packet
->unwritten_size
;
1140 stream
->premux_packet
= stream
->premux_packet
->next
;
1143 stream
->premux_packet
->unwritten_size
-= es_size
;
1145 if(remove_decoded_packets(ctx
, s
->last_scr
) < 0)
1151 static int mpeg_mux_write_packet(AVFormatContext
*ctx
, AVPacket
*pkt
)
1153 MpegMuxContext
*s
= ctx
->priv_data
;
1154 int stream_index
= pkt
->stream_index
;
1155 int size
= pkt
->size
;
1156 uint8_t *buf
= pkt
->data
;
1157 AVStream
*st
= ctx
->streams
[stream_index
];
1158 StreamInfo
*stream
= st
->priv_data
;
1160 PacketDesc
*pkt_desc
;
1161 const int preload
= av_rescale(ctx
->preload
, 90000, AV_TIME_BASE
);
1162 const int is_iframe
= st
->codec
.codec_type
== CODEC_TYPE_VIDEO
&& (pkt
->flags
& PKT_FLAG_KEY
);
1167 if(pts
!= AV_NOPTS_VALUE
) pts
+= preload
;
1168 if(dts
!= AV_NOPTS_VALUE
) dts
+= preload
;
1170 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE);
1171 *stream
->next_packet
=
1172 pkt_desc
= av_mallocz(sizeof(PacketDesc
));
1175 pkt_desc
->unwritten_size
=
1176 pkt_desc
->size
= size
;
1177 if(!stream
->predecode_packet
)
1178 stream
->predecode_packet
= pkt_desc
;
1179 stream
->next_packet
= &pkt_desc
->next
;
1181 fifo_realloc(&stream
->fifo
, fifo_size(&stream
->fifo
, NULL
) + size
+ 1);
1184 if (is_iframe
&& (s
->packet_number
== 0 || (pts
- stream
->vobu_start_pts
>= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
1185 stream
->fifo_iframe_ptr
= stream
->fifo
.wptr
;
1186 stream
->align_iframe
= 1;
1187 stream
->vobu_start_pts
= pts
;
1189 stream
->align_iframe
= 0;
1193 fifo_write(&stream
->fifo
, buf
, size
, &stream
->fifo
.wptr
);
1196 int ret
= output_packet(ctx
, 0);
1202 static int mpeg_mux_end(AVFormatContext
*ctx
)
1204 // MpegMuxContext *s = ctx->priv_data;
1209 int ret
= output_packet(ctx
, 1);
1216 /* End header according to MPEG1 systems standard. We do not write
1217 it as it is usually not needed by decoders and because it
1218 complicates MPEG stream concatenation. */
1219 //put_be32(&ctx->pb, ISO_11172_END_CODE);
1220 //put_flush_packet(&ctx->pb);
1222 for(i
=0;i
<ctx
->nb_streams
;i
++) {
1223 stream
= ctx
->streams
[i
]->priv_data
;
1225 assert(fifo_size(&stream
->fifo
, stream
->fifo
.rptr
) == 0);
1226 fifo_free(&stream
->fifo
);
1230 #endif //CONFIG_ENCODERS
1232 /*********************************************/
1235 #define MAX_SYNC_SIZE 100000
1237 static int mpegps_probe(AVProbeData
*p
)
1240 int size
= FFMIN(20, p
->buf_size
);
1243 /* we search the first start code. If it is a packet start code,
1244 then we decide it is mpeg ps. We do not send highest value to
1245 give a chance to mpegts */
1246 /* NOTE: the search range was restricted to avoid too many false
1249 for (i
= 0; i
< size
; i
++) {
1250 code
= (code
<< 8) | p
->buf
[i
];
1251 if ((code
& 0xffffff00) == 0x100) {
1252 if (code
== PACK_START_CODE
||
1253 code
== SYSTEM_HEADER_START_CODE
||
1254 (code
>= 0x1e0 && code
<= 0x1ef) ||
1255 (code
>= 0x1c0 && code
<= 0x1df) ||
1256 code
== PRIVATE_STREAM_2
||
1257 code
== PROGRAM_STREAM_MAP
||
1258 code
== PRIVATE_STREAM_1
||
1259 code
== PADDING_STREAM
)
1260 return AVPROBE_SCORE_MAX
- 2;
1269 typedef struct MpegDemuxContext
{
1273 static int mpegps_read_header(AVFormatContext
*s
,
1274 AVFormatParameters
*ap
)
1276 MpegDemuxContext
*m
= s
->priv_data
;
1277 m
->header_state
= 0xff;
1278 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
1280 /* no need to do more */
1284 static int64_t get_pts(ByteIOContext
*pb
, int c
)
1291 pts
= (int64_t)((c
>> 1) & 0x07) << 30;
1293 pts
|= (int64_t)(val
>> 1) << 15;
1295 pts
|= (int64_t)(val
>> 1);
1299 static int find_next_start_code(ByteIOContext
*pb
, int *size_ptr
,
1300 uint32_t *header_state
)
1302 unsigned int state
, v
;
1305 state
= *header_state
;
1312 if (state
== 0x000001) {
1313 state
= ((state
<< 8) | v
) & 0xffffff;
1317 state
= ((state
<< 8) | v
) & 0xffffff;
1321 *header_state
= state
;
1326 #if 0 /* unused, remove? */
1328 static int find_prev_start_code(ByteIOContext
*pb
, int *size_ptr
)
1330 int64_t pos
, pos_start
;
1331 int max_size
, start_code
;
1333 max_size
= *size_ptr
;
1334 pos_start
= url_ftell(pb
);
1336 /* in order to go faster, we fill the buffer */
1337 pos
= pos_start
- 16386;
1340 url_fseek(pb
, pos
, SEEK_SET
);
1346 if (pos
< 0 || (pos_start
- pos
) >= max_size
) {
1350 url_fseek(pb
, pos
, SEEK_SET
);
1351 start_code
= get_be32(pb
);
1352 if ((start_code
& 0xffffff00) == 0x100)
1356 *size_ptr
= pos_start
- pos
;
1361 /* read the next PES header. Return its position in ppos
1362 (if not NULL), and its start code, pts and dts.
1364 static int mpegps_read_pes_header(AVFormatContext
*s
,
1365 int64_t *ppos
, int *pstart_code
,
1366 int64_t *ppts
, int64_t *pdts
)
1368 MpegDemuxContext
*m
= s
->priv_data
;
1369 int len
, size
, startcode
, c
, flags
, header_len
;
1370 int64_t pts
, dts
, last_pos
;
1374 /* next start code (should be immediately after) */
1375 m
->header_state
= 0xff;
1376 size
= MAX_SYNC_SIZE
;
1377 startcode
= find_next_start_code(&s
->pb
, &size
, &m
->header_state
);
1378 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
1381 if (startcode
== PACK_START_CODE
)
1383 if (startcode
== SYSTEM_HEADER_START_CODE
)
1385 if (startcode
== PADDING_STREAM
||
1386 startcode
== PRIVATE_STREAM_2
) {
1388 len
= get_be16(&s
->pb
);
1389 url_fskip(&s
->pb
, len
);
1392 /* find matching stream */
1393 if (!((startcode
>= 0x1c0 && startcode
<= 0x1df) ||
1394 (startcode
>= 0x1e0 && startcode
<= 0x1ef) ||
1395 (startcode
== 0x1bd)))
1398 *ppos
= url_ftell(&s
->pb
) - 4;
1400 len
= get_be16(&s
->pb
);
1401 pts
= AV_NOPTS_VALUE
;
1402 dts
= AV_NOPTS_VALUE
;
1407 c
= get_byte(&s
->pb
);
1409 /* XXX: for mpeg1, should test only bit 7 */
1413 if ((c
& 0xc0) == 0x40) {
1414 /* buffer scale & size */
1418 c
= get_byte(&s
->pb
);
1421 if ((c
& 0xf0) == 0x20) {
1424 dts
= pts
= get_pts(&s
->pb
, c
);
1426 } else if ((c
& 0xf0) == 0x30) {
1429 pts
= get_pts(&s
->pb
, c
);
1430 dts
= get_pts(&s
->pb
, -1);
1432 } else if ((c
& 0xc0) == 0x80) {
1434 if ((c
& 0x30) != 0) {
1435 /* Encrypted multiplex not handled */
1438 flags
= get_byte(&s
->pb
);
1439 header_len
= get_byte(&s
->pb
);
1441 if (header_len
> len
)
1443 if ((flags
& 0xc0) == 0x80) {
1444 dts
= pts
= get_pts(&s
->pb
, -1);
1449 } if ((flags
& 0xc0) == 0xc0) {
1450 pts
= get_pts(&s
->pb
, -1);
1451 dts
= get_pts(&s
->pb
, -1);
1452 if (header_len
< 10)
1458 while (header_len
> 0) {
1466 if (startcode
== 0x1bd) {
1469 startcode
= get_byte(&s
->pb
);
1471 if (startcode
>= 0x80 && startcode
<= 0xbf) {
1472 /* audio: skip header */
1481 if(dts
!= AV_NOPTS_VALUE
&& ppos
){
1483 for(i
=0; i
<s
->nb_streams
; i
++){
1484 if(startcode
== s
->streams
[i
]->id
) {
1485 av_add_index_entry(s
->streams
[i
], *ppos
, dts
, 0, AVINDEX_KEYFRAME
/* FIXME keyframe? */);
1490 *pstart_code
= startcode
;
1496 static int mpegps_read_packet(AVFormatContext
*s
,
1500 int len
, startcode
, i
, type
, codec_id
= 0;
1501 int64_t pts
, dts
, dummy_pos
; //dummy_pos is needed for the index building to work
1504 len
= mpegps_read_pes_header(s
, &dummy_pos
, &startcode
, &pts
, &dts
);
1508 /* now find stream */
1509 for(i
=0;i
<s
->nb_streams
;i
++) {
1511 if (st
->id
== startcode
)
1514 if (startcode
>= 0x1e0 && startcode
<= 0x1ef) {
1515 type
= CODEC_TYPE_VIDEO
;
1516 codec_id
= CODEC_ID_MPEG2VIDEO
;
1517 } else if (startcode
>= 0x1c0 && startcode
<= 0x1df) {
1518 type
= CODEC_TYPE_AUDIO
;
1519 codec_id
= CODEC_ID_MP2
;
1520 } else if (startcode
>= 0x80 && startcode
<= 0x89) {
1521 type
= CODEC_TYPE_AUDIO
;
1522 codec_id
= CODEC_ID_AC3
;
1523 } else if (startcode
>= 0x8a && startcode
<= 0x9f) {
1524 type
= CODEC_TYPE_AUDIO
;
1525 codec_id
= CODEC_ID_DTS
;
1526 } else if (startcode
>= 0xa0 && startcode
<= 0xbf) {
1527 type
= CODEC_TYPE_AUDIO
;
1528 codec_id
= CODEC_ID_PCM_S16BE
;
1532 url_fskip(&s
->pb
, len
);
1535 /* no stream found: add a new stream */
1536 st
= av_new_stream(s
, startcode
);
1539 st
->codec
.codec_type
= type
;
1540 st
->codec
.codec_id
= codec_id
;
1541 if (codec_id
!= CODEC_ID_PCM_S16BE
)
1542 st
->need_parsing
= 1;
1546 if (startcode
>= 0xa0 && startcode
<= 0xbf) {
1549 /* for LPCM, we just skip the header and consider it is raw
1553 get_byte(&s
->pb
); /* emphasis (1), muse(1), reserved(1), frame number(5) */
1554 b1
= get_byte(&s
->pb
); /* quant (2), freq(2), reserved(1), channels(3) */
1555 get_byte(&s
->pb
); /* dynamic range control (0x80 = off) */
1557 freq
= (b1
>> 4) & 3;
1558 st
->codec
.sample_rate
= lpcm_freq_tab
[freq
];
1559 st
->codec
.channels
= 1 + (b1
& 7);
1560 st
->codec
.bit_rate
= st
->codec
.channels
* st
->codec
.sample_rate
* 2;
1562 av_new_packet(pkt
, len
);
1563 get_buffer(&s
->pb
, pkt
->data
, pkt
->size
);
1566 pkt
->stream_index
= st
->index
;
1568 av_log(s
, AV_LOG_DEBUG
, "%d: pts=%0.3f dts=%0.3f size=%d\n",
1569 pkt
->stream_index
, pkt
->pts
/ 90000.0, pkt
->dts
/ 90000.0, pkt
->size
);
1575 static int mpegps_read_close(AVFormatContext
*s
)
1580 static int64_t mpegps_read_dts(AVFormatContext
*s
, int stream_index
,
1581 int64_t *ppos
, int64_t pos_limit
)
1584 int64_t pos
, pts
, dts
;
1588 printf("read_dts: pos=0x%llx next=%d -> ", pos
, find_next
);
1590 url_fseek(&s
->pb
, pos
, SEEK_SET
);
1592 len
= mpegps_read_pes_header(s
, &pos
, &startcode
, &pts
, &dts
);
1595 printf("none (ret=%d)\n", len
);
1597 return AV_NOPTS_VALUE
;
1599 if (startcode
== s
->streams
[stream_index
]->id
&&
1600 dts
!= AV_NOPTS_VALUE
) {
1603 url_fskip(&s
->pb
, len
);
1606 printf("pos=0x%llx dts=0x%llx %0.3f\n", pos
, dts
, dts
/ 90000.0);
1612 #ifdef CONFIG_ENCODERS
1613 static AVOutputFormat mpeg1system_mux
= {
1615 "MPEG1 System format",
1618 sizeof(MpegMuxContext
),
1620 CODEC_ID_MPEG1VIDEO
,
1622 mpeg_mux_write_packet
,
1626 static AVOutputFormat mpeg1vcd_mux
= {
1628 "MPEG1 System format (VCD)",
1631 sizeof(MpegMuxContext
),
1633 CODEC_ID_MPEG1VIDEO
,
1635 mpeg_mux_write_packet
,
1639 static AVOutputFormat mpeg2vob_mux
= {
1641 "MPEG2 PS format (VOB)",
1644 sizeof(MpegMuxContext
),
1646 CODEC_ID_MPEG2VIDEO
,
1648 mpeg_mux_write_packet
,
1652 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1653 static AVOutputFormat mpeg2svcd_mux
= {
1655 "MPEG2 PS format (VOB)",
1658 sizeof(MpegMuxContext
),
1660 CODEC_ID_MPEG2VIDEO
,
1662 mpeg_mux_write_packet
,
1666 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1667 static AVOutputFormat mpeg2dvd_mux
= {
1669 "MPEG2 PS format (DVD VOB)",
1672 sizeof(MpegMuxContext
),
1674 CODEC_ID_MPEG2VIDEO
,
1676 mpeg_mux_write_packet
,
1680 #endif //CONFIG_ENCODERS
1682 AVInputFormat mpegps_demux
= {
1685 sizeof(MpegDemuxContext
),
1690 NULL
, //mpegps_read_seek,
1694 int mpegps_init(void)
1696 #ifdef CONFIG_ENCODERS
1697 av_register_output_format(&mpeg1system_mux
);
1698 av_register_output_format(&mpeg1vcd_mux
);
1699 av_register_output_format(&mpeg2vob_mux
);
1700 av_register_output_format(&mpeg2svcd_mux
);
1701 av_register_output_format(&mpeg2dvd_mux
);
1702 #endif //CONFIG_ENCODERS
1703 av_register_input_format(&mpegps_demux
);