blob: 450fab57152b1e939ad5ec5dce4bb6e888290cb5 [file] [log] [blame]
Fabrice Bellardde6d9b62001-07-22 14:18:561/*
Fabrice Bellardfb7566d2002-10-15 10:22:232 * MPEG1/2 mux/demux
Fabrice Bellard19720f12002-05-25 22:34:323 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
Fabrice Bellardde6d9b62001-07-22 14:18:564 *
Fabrice Bellard19720f12002-05-25 22:34:325 * 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.
Fabrice Bellardde6d9b62001-07-22 14:18:569 *
Fabrice Bellard19720f12002-05-25 22:34:3210 * This library is distributed in the hope that it will be useful,
Fabrice Bellardde6d9b62001-07-22 14:18:5611 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellard19720f12002-05-25 22:34:3212 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
Fabrice Bellardde6d9b62001-07-22 14:18:5614 *
Fabrice Bellard19720f12002-05-25 22:34:3215 * 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
Fabrice Bellardde6d9b62001-07-22 14:18:5618 */
Fabrice Bellardde6d9b62001-07-22 14:18:5619#include "avformat.h"
20
21#define MAX_PAYLOAD_SIZE 4096
Michael Niedermayer7000a172004-10-03 02:42:0122#define PRELOAD 45000 //0.5sec
Fabrice Bellard27f388a2003-11-10 18:47:5223//#define DEBUG_SEEK
Fabrice Bellardde6d9b62001-07-22 14:18:5624
Michael Niedermayerb7549782004-01-13 22:02:4925#undef NDEBUG
26#include <assert.h>
27
Michael Niedermayer7000a172004-10-03 02:42:0128typedef 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
Fabrice Bellardde6d9b62001-07-22 14:18:5637typedef struct {
Michael Niedermayer7000a172004-10-03 02:42:0138 FifoBuffer fifo;
Zdenek Kabelac0c1a9ed2003-02-11 16:35:4839 uint8_t id;
Fabrice Bellardde6d9b62001-07-22 14:18:5640 int max_buffer_size; /* in bytes */
Michael Niedermayer7000a172004-10-03 02:42:0141 int buffer_index;
42 PacketDesc *predecode_packet;
43 PacketDesc *premux_packet;
44 PacketDesc **next_packet;
Fabrice Bellardde6d9b62001-07-22 14:18:5645 int packet_number;
Zdenek Kabelac0c1a9ed2003-02-11 16:35:4846 int64_t start_pts;
Michel Bardiaux27a206e2003-12-09 18:06:1847 int64_t start_dts;
Fabrice Bellard044007c2003-12-16 14:00:1848 uint8_t lpcm_header[3];
49 int lpcm_align;
Fabrice Bellardde6d9b62001-07-22 14:18:5650} StreamInfo;
51
52typedef struct {
53 int packet_size; /* required packet size */
Fabrice Bellardde6d9b62001-07-22 14:18:5654 int packet_number;
55 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
56 int system_header_freq;
Fabrice Bellard0dbb48d2003-12-16 11:25:3057 int system_header_size;
Fabrice Bellardde6d9b62001-07-22 14:18:5658 int mux_rate; /* bitrate in units of 50 bytes/s */
59 /* stream info */
60 int audio_bound;
61 int video_bound;
Fabrice Bellardfb7566d2002-10-15 10:22:2362 int is_mpeg2;
63 int is_vcd;
Hauke Duden24515922004-02-19 22:34:1364 int is_svcd;
Michel Bardiaux27a206e2003-12-09 18:06:1865 int64_t last_scr; /* current system clock */
Hauke Duden24515922004-02-19 22:34:1366
Michael Niedermayerd8b5abf2004-10-01 20:05:0467 double vcd_padding_bitrate; //FIXME floats
Hauke Duden24515922004-02-19 22:34:1368 int64_t vcd_padding_bytes_written;
69
Fabrice Bellardde6d9b62001-07-22 14:18:5670} MpegMuxContext;
71
72#define PACK_START_CODE ((unsigned int)0x000001ba)
73#define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
Juanjo92b3e122002-05-12 21:38:5474#define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
Fabrice Bellardde6d9b62001-07-22 14:18:5675#define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
76#define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
77#define ISO_11172_END_CODE ((unsigned int)0x000001b9)
78
79/* mpeg2 */
80#define PROGRAM_STREAM_MAP 0x1bc
81#define PRIVATE_STREAM_1 0x1bd
82#define PADDING_STREAM 0x1be
83#define PRIVATE_STREAM_2 0x1bf
84
85
86#define AUDIO_ID 0xc0
87#define VIDEO_ID 0xe0
Fabrice Bellard044007c2003-12-16 14:00:1888#define AC3_ID 0x80
Michael Niedermayer23c99252004-07-14 01:32:1489#define DTS_ID 0x8a
Fabrice Bellard044007c2003-12-16 14:00:1890#define LPCM_ID 0xa0
Fabrice Bellardde6d9b62001-07-22 14:18:5691
Michael Niedermayer8a05bca2004-01-17 22:02:0792static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
93
Mike Melanson764ef402003-10-14 04:15:5394#ifdef CONFIG_ENCODERS
Falk Hüffner79060852004-03-24 23:32:4895static AVOutputFormat mpeg1system_mux;
96static AVOutputFormat mpeg1vcd_mux;
97static AVOutputFormat mpeg2vob_mux;
98static AVOutputFormat mpeg2svcd_mux;
Fabrice Bellardfb7566d2002-10-15 10:22:2399
Fabrice Bellardde6d9b62001-07-22 14:18:56100static int put_pack_header(AVFormatContext *ctx,
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48101 uint8_t *buf, int64_t timestamp)
Fabrice Bellardde6d9b62001-07-22 14:18:56102{
103 MpegMuxContext *s = ctx->priv_data;
104 PutBitContext pb;
105
Alex Beregszaszi117a5492003-10-13 10:59:57106 init_put_bits(&pb, buf, 128);
Fabrice Bellardde6d9b62001-07-22 14:18:56107
108 put_bits(&pb, 32, PACK_START_CODE);
Fabrice Bellardb2cac182002-10-21 15:57:21109 if (s->is_mpeg2) {
Måns Rullgård8683e4a2003-07-15 22:15:37110 put_bits(&pb, 2, 0x1);
Fabrice Bellardb2cac182002-10-21 15:57:21111 } else {
112 put_bits(&pb, 4, 0x2);
113 }
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48114 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
Fabrice Bellardde6d9b62001-07-22 14:18:56115 put_bits(&pb, 1, 1);
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48116 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
Fabrice Bellardde6d9b62001-07-22 14:18:56117 put_bits(&pb, 1, 1);
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48118 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
Fabrice Bellardde6d9b62001-07-22 14:18:56119 put_bits(&pb, 1, 1);
Fabrice Bellardb2cac182002-10-21 15:57:21120 if (s->is_mpeg2) {
121 /* clock extension */
122 put_bits(&pb, 9, 0);
Fabrice Bellardb2cac182002-10-21 15:57:21123 }
Fabrice Bellardde6d9b62001-07-22 14:18:56124 put_bits(&pb, 1, 1);
125 put_bits(&pb, 22, s->mux_rate);
126 put_bits(&pb, 1, 1);
Fabrice Bellardb2cac182002-10-21 15:57:21127 if (s->is_mpeg2) {
Michael Niedermayer4aa533b2004-02-01 13:06:46128 put_bits(&pb, 1, 1);
Fabrice Bellardb2cac182002-10-21 15:57:21129 put_bits(&pb, 5, 0x1f); /* reserved */
130 put_bits(&pb, 3, 0); /* stuffing length */
131 }
Fabrice Bellardde6d9b62001-07-22 14:18:56132 flush_put_bits(&pb);
Michael Niedermayer17592472002-02-12 15:43:16133 return pbBufPtr(&pb) - pb.buf;
Fabrice Bellardde6d9b62001-07-22 14:18:56134}
135
Hauke Duden24515922004-02-19 22:34:13136static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
Fabrice Bellardde6d9b62001-07-22 14:18:56137{
138 MpegMuxContext *s = ctx->priv_data;
Michael Niedermayer7000a172004-10-03 02:42:01139 int size, i, private_stream_coded, id;
Fabrice Bellardde6d9b62001-07-22 14:18:56140 PutBitContext pb;
141
Alex Beregszaszi117a5492003-10-13 10:59:57142 init_put_bits(&pb, buf, 128);
Fabrice Bellardde6d9b62001-07-22 14:18:56143
144 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
145 put_bits(&pb, 16, 0);
146 put_bits(&pb, 1, 1);
147
Michael Niedermayer7000a172004-10-03 02:42:01148 put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
Fabrice Bellardde6d9b62001-07-22 14:18:56149 put_bits(&pb, 1, 1); /* marker */
Hauke Duden24515922004-02-19 22:34:13150 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);
Fabrice Bellardde6d9b62001-07-22 14:18:56155
Hauke Duden22494482004-04-26 22:16:06156 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 }
Fabrice Bellardde6d9b62001-07-22 14:18:56164
Hauke Duden24515922004-02-19 22:34:13165 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
Fabrice Bellardde6d9b62001-07-22 14:18:56174 put_bits(&pb, 1, 1); /* marker */
175
Hauke Duden24515922004-02-19 22:34:13176 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
Fabrice Bellardde6d9b62001-07-22 14:18:56182 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;
Hauke Duden24515922004-02-19 22:34:13188
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 }
Fabrice Bellardde6d9b62001-07-22 14:18:56214 }
215 }
216 flush_put_bits(&pb);
Michael Niedermayer17592472002-02-12 15:43:16217 size = pbBufPtr(&pb) - pb.buf;
Fabrice Bellardde6d9b62001-07-22 14:18:56218 /* patch packet size */
219 buf[4] = (size - 6) >> 8;
220 buf[5] = (size - 6) & 0xff;
221
222 return size;
223}
224
Fabrice Bellard0dbb48d2003-12-16 11:25:30225static 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
Fabrice Bellardde6d9b62001-07-22 14:18:56244static int mpeg_mux_init(AVFormatContext *ctx)
245{
Fabrice Bellarddb7f1f92002-05-20 16:29:40246 MpegMuxContext *s = ctx->priv_data;
Michael Niedermayer23c99252004-07-14 01:32:14247 int bitrate, i, mpa_id, mpv_id, ac3_id, dts_id, lpcm_id, j;
Fabrice Bellardde6d9b62001-07-22 14:18:56248 AVStream *st;
249 StreamInfo *stream;
Hauke Duden24515922004-02-19 22:34:13250 int audio_bitrate;
251 int video_bitrate;
Fabrice Bellardde6d9b62001-07-22 14:18:56252
Fabrice Bellardde6d9b62001-07-22 14:18:56253 s->packet_number = 0;
Fabrice Bellardfb7566d2002-10-15 10:22:23254 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
Hauke Duden24515922004-02-19 22:34:13255 s->is_svcd = (ctx->oformat == &mpeg2svcd_mux);
256 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux || ctx->oformat == &mpeg2svcd_mux);
Fabrice Bellardfb7566d2002-10-15 10:22:23257
Hauke Duden24515922004-02-19 22:34:13258 if (s->is_vcd || s->is_svcd)
259 s->packet_size = 2324; /* VCD/SVCD packet size */
Juanjo92b3e122002-05-12 21:38:54260 else
261 s->packet_size = 2048;
Hauke Duden24515922004-02-19 22:34:13262
263 s->vcd_padding_bytes_written = 0;
264 s->vcd_padding_bitrate=0;
Juanjo92b3e122002-05-12 21:38:54265
Fabrice Bellardde6d9b62001-07-22 14:18:56266 s->audio_bound = 0;
267 s->video_bound = 0;
268 mpa_id = AUDIO_ID;
Fabrice Bellard044007c2003-12-16 14:00:18269 ac3_id = AC3_ID;
Michael Niedermayer23c99252004-07-14 01:32:14270 dts_id = DTS_ID;
Fabrice Bellardde6d9b62001-07-22 14:18:56271 mpv_id = VIDEO_ID;
Fabrice Bellard044007c2003-12-16 14:00:18272 lpcm_id = LPCM_ID;
Fabrice Bellardde6d9b62001-07-22 14:18:56273 for(i=0;i<ctx->nb_streams;i++) {
274 st = ctx->streams[i];
275 stream = av_mallocz(sizeof(StreamInfo));
276 if (!stream)
277 goto fail;
278 st->priv_data = stream;
279
280 switch(st->codec.codec_type) {
281 case CODEC_TYPE_AUDIO:
Fabrice Bellard044007c2003-12-16 14:00:18282 if (st->codec.codec_id == CODEC_ID_AC3) {
Fabrice Bellardde6d9b62001-07-22 14:18:56283 stream->id = ac3_id++;
Michael Niedermayer23c99252004-07-14 01:32:14284 } else if (st->codec.codec_id == CODEC_ID_DTS) {
285 stream->id = dts_id++;
Fabrice Bellard044007c2003-12-16 14:00:18286 } else if (st->codec.codec_id == CODEC_ID_PCM_S16BE) {
287 stream->id = lpcm_id++;
288 for(j = 0; j < 4; j++) {
289 if (lpcm_freq_tab[j] == st->codec.sample_rate)
290 break;
291 }
292 if (j == 4)
293 goto fail;
294 if (st->codec.channels > 8)
295 return -1;
296 stream->lpcm_header[0] = 0x0c;
297 stream->lpcm_header[1] = (st->codec.channels - 1) | (j << 4);
298 stream->lpcm_header[2] = 0x80;
299 stream->lpcm_align = st->codec.channels * 2;
300 } else {
Fabrice Bellardde6d9b62001-07-22 14:18:56301 stream->id = mpa_id++;
Fabrice Bellard044007c2003-12-16 14:00:18302 }
Hauke Duden22494482004-04-26 22:16:06303
304 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
305 Right now it is also used for everything else.*/
Fabrice Bellardde6d9b62001-07-22 14:18:56306 stream->max_buffer_size = 4 * 1024;
307 s->audio_bound++;
308 break;
309 case CODEC_TYPE_VIDEO:
Fabrice Bellardde6d9b62001-07-22 14:18:56310 stream->id = mpv_id++;
Hauke Duden22494482004-04-26 22:16:06311 if (s->is_vcd)
312 /* see VCD standard, p. IV-7*/
313 stream->max_buffer_size = 46 * 1024;
314 else
315 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
316 Right now it is also used for everything else.*/
317 stream->max_buffer_size = 230 * 1024;
Fabrice Bellardde6d9b62001-07-22 14:18:56318 s->video_bound++;
319 break;
Philip Gladstoneac5e6a52002-05-09 01:19:33320 default:
Michael Niedermayer71c32f12004-10-01 13:16:16321 return -1;
Fabrice Bellardde6d9b62001-07-22 14:18:56322 }
Michael Niedermayer7000a172004-10-03 02:42:01323 fifo_init(&stream->fifo, 2*stream->max_buffer_size + 100*MAX_PAYLOAD_SIZE); //FIXME think about the size maybe dynamically realloc
324 stream->next_packet= &stream->premux_packet;
Fabrice Bellardde6d9b62001-07-22 14:18:56325 }
Hauke Duden24515922004-02-19 22:34:13326 bitrate = 0;
327 audio_bitrate = 0;
328 video_bitrate = 0;
Fabrice Bellardde6d9b62001-07-22 14:18:56329 for(i=0;i<ctx->nb_streams;i++) {
Michael Niedermayer7000a172004-10-03 02:42:01330 int codec_rate;
Fabrice Bellardde6d9b62001-07-22 14:18:56331 st = ctx->streams[i];
Hauke Duden24515922004-02-19 22:34:13332 stream = (StreamInfo*) st->priv_data;
Michael Niedermayer7000a172004-10-03 02:42:01333
334 if(st->codec.rc_max_rate || stream->id==VIDEO_ID)
335 codec_rate= st->codec.rc_max_rate;
336 else
337 codec_rate= st->codec.bit_rate;
338
339 if(!codec_rate)
340 codec_rate= (1<<21)*8*50/ctx->nb_streams;
341
342 bitrate += codec_rate;
Hauke Duden24515922004-02-19 22:34:13343
344 if (stream->id==AUDIO_ID)
Michael Niedermayer7000a172004-10-03 02:42:01345 audio_bitrate += codec_rate;
Hauke Duden24515922004-02-19 22:34:13346 else if (stream->id==VIDEO_ID)
Michael Niedermayer7000a172004-10-03 02:42:01347 video_bitrate += codec_rate;
Fabrice Bellardde6d9b62001-07-22 14:18:56348 }
Hauke Duden24515922004-02-19 22:34:13349
350 if (s->is_vcd) {
351 double overhead_rate;
352
353 /* The VCD standard mandates that the mux_rate field is 3528
354 (see standard p. IV-6).
355 The value is actually "wrong", i.e. if you calculate
356 it using the normal formula and the 75 sectors per second transfer
357 rate you get a different value because the real pack size is 2324,
358 not 2352. But the standard explicitly specifies that the mux_rate
359 field in the header must have this value.*/
360 s->mux_rate=2352 * 75 / 50; /* = 3528*/
361
362 /* The VCD standard states that the muxed stream must be
363 exactly 75 packs / second (the data rate of a single speed cdrom).
364 Since the video bitrate (probably 1150000 bits/sec) will be below
365 the theoretical maximum we have to add some padding packets
366 to make up for the lower data rate.
367 (cf. VCD standard p. IV-6 )*/
368
369 /* Add the header overhead to the data rate.
370 2279 data bytes per audio pack, 2294 data bytes per video pack*/
371 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
372 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
373 overhead_rate *= 8;
374
375 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
376 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
377
378 } else {
379 /* we increase slightly the bitrate to take into account the
380 headers. XXX: compute it exactly */
Michael Niedermayer7000a172004-10-03 02:42:01381 bitrate += bitrate*5/100;
382 bitrate += 10000;
Hauke Duden24515922004-02-19 22:34:13383 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
384 }
Juanjo92b3e122002-05-12 21:38:54385
Fabrice Bellardfb7566d2002-10-15 10:22:23386 if (s->is_vcd || s->is_mpeg2)
Juanjo92b3e122002-05-12 21:38:54387 /* every packet */
388 s->pack_header_freq = 1;
389 else
390 /* every 2 seconds */
391 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
Michael Niedermayerb623bbc2003-10-28 10:55:15392
393 /* the above seems to make pack_header_freq zero sometimes */
394 if (s->pack_header_freq == 0)
395 s->pack_header_freq = 1;
Juanjo92b3e122002-05-12 21:38:54396
Fabrice Bellardb2cac182002-10-21 15:57:21397 if (s->is_mpeg2)
398 /* every 200 packets. Need to look at the spec. */
399 s->system_header_freq = s->pack_header_freq * 40;
400 else if (s->is_vcd)
Hauke Duden24515922004-02-19 22:34:13401 /* the standard mandates that there are only two system headers
402 in the whole file: one in the first packet of each stream.
403 (see standard p. IV-7 and IV-8) */
404 s->system_header_freq = 0x7fffffff;
Juanjo92b3e122002-05-12 21:38:54405 else
Juanjo92b3e122002-05-12 21:38:54406 s->system_header_freq = s->pack_header_freq * 5;
407
Fabrice Bellardde6d9b62001-07-22 14:18:56408 for(i=0;i<ctx->nb_streams;i++) {
409 stream = ctx->streams[i]->priv_data;
Fabrice Bellardde6d9b62001-07-22 14:18:56410 stream->packet_number = 0;
Michel Bardiaux27a206e2003-12-09 18:06:18411 stream->start_pts = AV_NOPTS_VALUE;
412 stream->start_dts = AV_NOPTS_VALUE;
Fabrice Bellardde6d9b62001-07-22 14:18:56413 }
Fabrice Bellard0dbb48d2003-12-16 11:25:30414 s->system_header_size = get_system_header_size(ctx);
Michel Bardiaux27a206e2003-12-09 18:06:18415 s->last_scr = 0;
Fabrice Bellardde6d9b62001-07-22 14:18:56416 return 0;
417 fail:
418 for(i=0;i<ctx->nb_streams;i++) {
Fabrice Bellard1ea4f592002-05-18 23:11:09419 av_free(ctx->streams[i]->priv_data);
Fabrice Bellardde6d9b62001-07-22 14:18:56420 }
Fabrice Bellardde6d9b62001-07-22 14:18:56421 return -ENOMEM;
422}
423
Michel Bardiaux27a206e2003-12-09 18:06:18424static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
425{
426 put_byte(pb,
427 (id << 4) |
428 (((timestamp >> 30) & 0x07) << 1) |
429 1);
430 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
431 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
432}
433
Fabrice Bellard0dbb48d2003-12-16 11:25:30434
Hauke Duden24515922004-02-19 22:34:13435/* return the number of padding bytes that should be inserted into
436 the multiplexed stream.*/
437static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
438{
439 MpegMuxContext *s = ctx->priv_data;
440 int pad_bytes = 0;
441
442 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
443 {
444 int64_t full_pad_bytes;
445
Michael Niedermayer7000a172004-10-03 02:42:01446 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
Hauke Duden24515922004-02-19 22:34:13447 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
448
449 if (pad_bytes<0)
450 /* might happen if we have already padded to a later timestamp. This
451 can occur if another stream has already advanced further.*/
452 pad_bytes=0;
453 }
454
455 return pad_bytes;
456}
457
458
Fabrice Bellard0dbb48d2003-12-16 11:25:30459/* return the exact available payload size for the next packet for
460 stream 'stream_index'. 'pts' and 'dts' are only used to know if
461 timestamps are needed in the packet header. */
462static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
463 int64_t pts, int64_t dts)
464{
465 MpegMuxContext *s = ctx->priv_data;
466 int buf_index;
467 StreamInfo *stream;
468
Hauke Duden24515922004-02-19 22:34:13469 stream = ctx->streams[stream_index]->priv_data;
470
Fabrice Bellard0dbb48d2003-12-16 11:25:30471 buf_index = 0;
472 if (((s->packet_number % s->pack_header_freq) == 0)) {
473 /* pack header size */
474 if (s->is_mpeg2)
475 buf_index += 14;
476 else
477 buf_index += 12;
Hauke Duden24515922004-02-19 22:34:13478
479 if (s->is_vcd) {
480 /* there is exactly one system header for each stream in a VCD MPEG,
481 One in the very first video packet and one in the very first
482 audio packet (see VCD standard p. IV-7 and IV-8).*/
483
484 if (stream->packet_number==0)
485 /* The system headers refer only to the stream they occur in,
486 so they have a constant size.*/
487 buf_index += 15;
488
489 } else {
490 if ((s->packet_number % s->system_header_freq) == 0)
491 buf_index += s->system_header_size;
492 }
Fabrice Bellard0dbb48d2003-12-16 11:25:30493 }
494
Hauke Duden22494482004-04-26 22:16:06495 if ((s->is_vcd && stream->packet_number==0)
496 || (s->is_svcd && s->packet_number==0))
Hauke Duden24515922004-02-19 22:34:13497 /* the first pack of each stream contains only the pack header,
498 the system header and some padding (see VCD standard p. IV-6)
499 Add the padding size, so that the actual payload becomes 0.*/
500 buf_index += s->packet_size - buf_index;
501 else {
502 /* packet header size */
503 buf_index += 6;
Hauke Duden22494482004-04-26 22:16:06504 if (s->is_mpeg2) {
Fabrice Bellard044007c2003-12-16 14:00:18505 buf_index += 3;
Hauke Duden22494482004-04-26 22:16:06506 if (stream->packet_number==0)
507 buf_index += 3; /* PES extension */
508 buf_index += 1; /* obligatory stuffing byte */
509 }
Hauke Duden24515922004-02-19 22:34:13510 if (pts != AV_NOPTS_VALUE) {
511 if (dts != pts)
512 buf_index += 5 + 5;
513 else
514 buf_index += 5;
515
516 } else {
517 if (!s->is_mpeg2)
518 buf_index++;
Fabrice Bellard044007c2003-12-16 14:00:18519 }
Hauke Duden24515922004-02-19 22:34:13520
521 if (stream->id < 0xc0) {
522 /* AC3/LPCM private data header */
523 buf_index += 4;
524 if (stream->id >= 0xa0) {
525 int n;
526 buf_index += 3;
527 /* NOTE: we round the payload size to an integer number of
528 LPCM samples */
529 n = (s->packet_size - buf_index) % stream->lpcm_align;
530 if (n)
531 buf_index += (stream->lpcm_align - n);
532 }
533 }
534
535 if (s->is_vcd && stream->id == AUDIO_ID)
536 /* The VCD standard demands that 20 zero bytes follow
537 each audio packet (see standard p. IV-8).*/
538 buf_index+=20;
Fabrice Bellard0dbb48d2003-12-16 11:25:30539 }
540 return s->packet_size - buf_index;
541}
542
Hauke Duden24515922004-02-19 22:34:13543/* Write an MPEG padding packet header. */
Hauke Duden24515922004-02-19 22:34:13544static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
545{
Michael Niedermayerd8b5abf2004-10-01 20:05:04546 MpegMuxContext *s = ctx->priv_data;
547 int i;
Hauke Duden24515922004-02-19 22:34:13548
Michael Niedermayerd8b5abf2004-10-01 20:05:04549 put_be32(pb, PADDING_STREAM);
550 put_be16(pb, packet_bytes - 6);
551 if (!s->is_mpeg2) {
552 put_byte(pb, 0x0f);
553 packet_bytes -= 7;
554 } else
555 packet_bytes -= 6;
Hauke Duden24515922004-02-19 22:34:13556
557 for(i=0;i<packet_bytes;i++)
558 put_byte(pb, 0xff);
559}
560
Michael Niedermayer7000a172004-10-03 02:42:01561static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
562 int nb_frames=0;
563 PacketDesc *pkt_desc= stream->premux_packet;
564
565 while(len>0){
566 if(pkt_desc->size == pkt_desc->unwritten_size)
567 nb_frames++;
568 len -= pkt_desc->unwritten_size;
569 pkt_desc= pkt_desc->next;
570 }
571
572 return nb_frames;
573}
Hauke Duden24515922004-02-19 22:34:13574
Fabrice Bellardde6d9b62001-07-22 14:18:56575/* flush the packet on stream stream_index */
Michael Niedermayer7000a172004-10-03 02:42:01576static int flush_packet(AVFormatContext *ctx, int stream_index,
577 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
Fabrice Bellardde6d9b62001-07-22 14:18:56578{
579 MpegMuxContext *s = ctx->priv_data;
580 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48581 uint8_t *buf_ptr;
Fabrice Bellard0dbb48d2003-12-16 11:25:30582 int size, payload_size, startcode, id, stuffing_size, i, header_len;
583 int packet_size;
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48584 uint8_t buffer[128];
Hauke Duden24515922004-02-19 22:34:13585 int zero_trail_bytes = 0;
586 int pad_packet_bytes = 0;
Hauke Duden22494482004-04-26 22:16:06587 int pes_flags;
588 int general_pack = 0; /*"general" pack without data specific to one stream?*/
Michael Niedermayer7000a172004-10-03 02:42:01589 int nb_frames;
Juanjo92b3e122002-05-12 21:38:54590
Fabrice Bellardde6d9b62001-07-22 14:18:56591 id = stream->id;
Michel Bardiaux27a206e2003-12-09 18:06:18592
Fabrice Bellardde6d9b62001-07-22 14:18:56593#if 0
594 printf("packet ID=%2x PTS=%0.3f\n",
Michel Bardiaux27a206e2003-12-09 18:06:18595 id, pts / 90000.0);
Fabrice Bellardde6d9b62001-07-22 14:18:56596#endif
597
598 buf_ptr = buffer;
Hauke Duden24515922004-02-19 22:34:13599
Michael Niedermayer7000a172004-10-03 02:42:01600 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
Fabrice Bellardde6d9b62001-07-22 14:18:56601 /* output pack and systems header if needed */
Michel Bardiaux27a206e2003-12-09 18:06:18602 size = put_pack_header(ctx, buf_ptr, scr);
Fabrice Bellardde6d9b62001-07-22 14:18:56603 buf_ptr += size;
Michael Niedermayer7000a172004-10-03 02:42:01604 s->last_scr= scr;
Hauke Duden24515922004-02-19 22:34:13605
606 if (s->is_vcd) {
607 /* there is exactly one system header for each stream in a VCD MPEG,
608 One in the very first video packet and one in the very first
609 audio packet (see VCD standard p. IV-7 and IV-8).*/
610
611 if (stream->packet_number==0) {
612 size = put_system_header(ctx, buf_ptr, id);
613 buf_ptr += size;
614 }
615 } else {
616 if ((s->packet_number % s->system_header_freq) == 0) {
617 size = put_system_header(ctx, buf_ptr, 0);
618 buf_ptr += size;
619 }
Fabrice Bellardde6d9b62001-07-22 14:18:56620 }
621 }
622 size = buf_ptr - buffer;
623 put_buffer(&ctx->pb, buffer, size);
624
Hauke Duden24515922004-02-19 22:34:13625 packet_size = s->packet_size - size;
626
627 if (s->is_vcd && id == AUDIO_ID)
628 /* The VCD standard demands that 20 zero bytes follow
629 each audio pack (see standard p. IV-8).*/
630 zero_trail_bytes += 20;
631
Hauke Duden22494482004-04-26 22:16:06632 if ((s->is_vcd && stream->packet_number==0)
633 || (s->is_svcd && s->packet_number==0)) {
634 /* for VCD the first pack of each stream contains only the pack header,
Hauke Duden24515922004-02-19 22:34:13635 the system header and lots of padding (see VCD standard p. IV-6).
636 In the case of an audio pack, 20 zero bytes are also added at
637 the end.*/
Hauke Duden22494482004-04-26 22:16:06638 /* For SVCD we fill the very first pack to increase compatibility with
639 some DVD players. Not mandated by the standard.*/
640 if (s->is_svcd)
641 general_pack = 1; /* the system header refers to both streams and no stream data*/
Hauke Duden24515922004-02-19 22:34:13642 pad_packet_bytes = packet_size - zero_trail_bytes;
Fabrice Bellardfb7566d2002-10-15 10:22:23643 }
Hauke Duden24515922004-02-19 22:34:13644
645 packet_size -= pad_packet_bytes + zero_trail_bytes;
646
647 if (packet_size > 0) {
648
649 /* packet header size */
650 packet_size -= 6;
651
652 /* packet header */
653 if (s->is_mpeg2) {
654 header_len = 3;
Hauke Duden22494482004-04-26 22:16:06655 if (stream->packet_number==0)
656 header_len += 3; /* PES extension */
657 header_len += 1; /* obligatory stuffing byte */
Hauke Duden24515922004-02-19 22:34:13658 } else {
659 header_len = 0;
660 }
661 if (pts != AV_NOPTS_VALUE) {
662 if (dts != pts)
663 header_len += 5 + 5;
664 else
665 header_len += 5;
666 } else {
667 if (!s->is_mpeg2)
668 header_len++;
669 }
670
671 payload_size = packet_size - header_len;
672 if (id < 0xc0) {
673 startcode = PRIVATE_STREAM_1;
674 payload_size -= 4;
675 if (id >= 0xa0)
676 payload_size -= 3;
677 } else {
678 startcode = 0x100 + id;
679 }
680
Michael Niedermayer7000a172004-10-03 02:42:01681 stuffing_size = payload_size - fifo_size(&stream->fifo, stream->fifo.rptr);
682
683 // first byte doesnt fit -> reset pts/dts + stuffing
684 if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
685 int timestamp_len=0;
686 if(dts != pts)
687 timestamp_len += 5;
688 if(pts != AV_NOPTS_VALUE)
689 timestamp_len += s->is_mpeg2 ? 5 : 4;
690 pts=dts= AV_NOPTS_VALUE;
691 header_len -= timestamp_len;
692 payload_size += timestamp_len;
693 stuffing_size += timestamp_len;
694 if(payload_size > trailer_size)
695 stuffing_size += payload_size - trailer_size;
696 }
697
Hauke Duden24515922004-02-19 22:34:13698 if (stuffing_size < 0)
699 stuffing_size = 0;
Hauke Duden22494482004-04-26 22:16:06700 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
701 pad_packet_bytes += stuffing_size;
702 packet_size -= stuffing_size;
703 payload_size -= stuffing_size;
704 stuffing_size = 0;
705 }
Michael Niedermayer7000a172004-10-03 02:42:01706
707 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
Hauke Duden22494482004-04-26 22:16:06708
Hauke Duden24515922004-02-19 22:34:13709 put_be32(&ctx->pb, startcode);
710
711 put_be16(&ctx->pb, packet_size);
712
Michel Bardiaux27a206e2003-12-09 18:06:18713 if (!s->is_mpeg2)
Hauke Duden24515922004-02-19 22:34:13714 for(i=0;i<stuffing_size;i++)
715 put_byte(&ctx->pb, 0xff);
Michel Bardiaux27a206e2003-12-09 18:06:18716
Hauke Duden24515922004-02-19 22:34:13717 if (s->is_mpeg2) {
718 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
Fabrice Bellard0dbb48d2003-12-16 11:25:30719
Hauke Duden22494482004-04-26 22:16:06720 pes_flags=0;
721
Hauke Duden24515922004-02-19 22:34:13722 if (pts != AV_NOPTS_VALUE) {
Hauke Duden22494482004-04-26 22:16:06723 pes_flags |= 0x80;
724 if (dts != pts)
725 pes_flags |= 0x40;
Michel Bardiaux27a206e2003-12-09 18:06:18726 }
Hauke Duden22494482004-04-26 22:16:06727
728 /* Both the MPEG-2 and the SVCD standards demand that the
729 P-STD_buffer_size field be included in the first packet of
730 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
731 and MPEG-2 standard 2.7.7) */
732 if (stream->packet_number == 0)
733 pes_flags |= 0x01;
734
735 put_byte(&ctx->pb, pes_flags); /* flags */
736 put_byte(&ctx->pb, header_len - 3 + stuffing_size);
737
738 if (pes_flags & 0x80) /*write pts*/
739 put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
740 if (pes_flags & 0x40) /*write dts*/
741 put_timestamp(&ctx->pb, 0x01, dts);
742
743 if (pes_flags & 0x01) { /*write pes extension*/
744 put_byte(&ctx->pb, 0x10); /* flags */
745
746 /* P-STD buffer info */
747 if (id == AUDIO_ID)
748 put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
749 else
750 put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
751 }
752
Michel Bardiaux27a206e2003-12-09 18:06:18753 } else {
Hauke Duden24515922004-02-19 22:34:13754 if (pts != AV_NOPTS_VALUE) {
755 if (dts != pts) {
756 put_timestamp(&ctx->pb, 0x03, pts);
757 put_timestamp(&ctx->pb, 0x01, dts);
758 } else {
759 put_timestamp(&ctx->pb, 0x02, pts);
760 }
Michel Bardiaux27a206e2003-12-09 18:06:18761 } else {
Hauke Duden24515922004-02-19 22:34:13762 put_byte(&ctx->pb, 0x0f);
Michel Bardiaux27a206e2003-12-09 18:06:18763 }
Michel Bardiaux27a206e2003-12-09 18:06:18764 }
Hauke Duden24515922004-02-19 22:34:13765
Sidik Isani9e9080b2004-05-25 23:06:00766 if (s->is_mpeg2) {
767 /* special stuffing byte that is always written
768 to prevent accidental generation of start codes. */
769 put_byte(&ctx->pb, 0xff);
770
771 for(i=0;i<stuffing_size;i++)
772 put_byte(&ctx->pb, 0xff);
773 }
774
Hauke Duden24515922004-02-19 22:34:13775 if (startcode == PRIVATE_STREAM_1) {
776 put_byte(&ctx->pb, id);
777 if (id >= 0xa0) {
778 /* LPCM (XXX: check nb_frames) */
779 put_byte(&ctx->pb, 7);
780 put_be16(&ctx->pb, 4); /* skip 3 header bytes */
781 put_byte(&ctx->pb, stream->lpcm_header[0]);
782 put_byte(&ctx->pb, stream->lpcm_header[1]);
783 put_byte(&ctx->pb, stream->lpcm_header[2]);
784 } else {
785 /* AC3 */
Michael Niedermayer7000a172004-10-03 02:42:01786 put_byte(&ctx->pb, nb_frames);
787 put_be16(&ctx->pb, trailer_size+1);
Hauke Duden24515922004-02-19 22:34:13788 }
789 }
790
Hauke Duden24515922004-02-19 22:34:13791 /* output data */
Michael Niedermayer7000a172004-10-03 02:42:01792 if(put_fifo(&ctx->pb, &stream->fifo, payload_size - stuffing_size, &stream->fifo.rptr) < 0)
793 return -1;
794 }else{
795 payload_size=
796 stuffing_size= 0;
Fabrice Bellardfb7566d2002-10-15 10:22:23797 }
Fabrice Bellardde6d9b62001-07-22 14:18:56798
Hauke Duden24515922004-02-19 22:34:13799 if (pad_packet_bytes > 0)
800 put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
Fabrice Bellardde6d9b62001-07-22 14:18:56801
Hauke Duden24515922004-02-19 22:34:13802 for(i=0;i<zero_trail_bytes;i++)
803 put_byte(&ctx->pb, 0x00);
804
Fabrice Bellardde6d9b62001-07-22 14:18:56805 put_flush_packet(&ctx->pb);
806
Fabrice Bellardde6d9b62001-07-22 14:18:56807 s->packet_number++;
Hauke Duden22494482004-04-26 22:16:06808
809 /* only increase the stream packet number if this pack actually contains
810 something that is specific to this stream! I.e. a dedicated header
811 or some data.*/
812 if (!general_pack)
813 stream->packet_number++;
Michael Niedermayer7000a172004-10-03 02:42:01814
815 return payload_size - stuffing_size;
Fabrice Bellardde6d9b62001-07-22 14:18:56816}
817
Hauke Duden24515922004-02-19 22:34:13818static void put_vcd_padding_sector(AVFormatContext *ctx)
819{
820 /* There are two ways to do this padding: writing a sector/pack
821 of 0 values, or writing an MPEG padding pack. Both seem to
822 work with most decoders, BUT the VCD standard only allows a 0-sector
823 (see standard p. IV-4, IV-5).
824 So a 0-sector it is...*/
825
826 MpegMuxContext *s = ctx->priv_data;
827 int i;
828
829 for(i=0;i<s->packet_size;i++)
830 put_byte(&ctx->pb, 0);
831
832 s->vcd_padding_bytes_written += s->packet_size;
833
834 put_flush_packet(&ctx->pb);
835
836 /* increasing the packet number is correct. The SCR of the following packs
837 is calculated from the packet_number and it has to include the padding
838 sector (it represents the sector index, not the MPEG pack index)
839 (see VCD standard p. IV-6)*/
840 s->packet_number++;
841}
842
Hauke Duden24515922004-02-19 22:34:13843static int64_t update_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
844{
845 MpegMuxContext *s = ctx->priv_data;
846 int64_t scr;
Hauke Duden22494482004-04-26 22:16:06847 StreamInfo *stream;
848 int i;
Hauke Duden24515922004-02-19 22:34:13849
Hauke Duden22494482004-04-26 22:16:06850 if (s->is_vcd) {
Hauke Duden24515922004-02-19 22:34:13851 /* Since the data delivery rate is constant, SCR is computed
852 using the formula C + i * 1200 where C is the start constant
853 and i is the pack index.
854 It is recommended that SCR 0 is at the beginning of the VCD front
855 margin (a sequence of empty Form 2 sectors on the CD).
856 It is recommended that the front margin is 30 sectors long, so
857 we use C = 30*1200 = 36000
858 (Note that even if the front margin is not 30 sectors the file
859 will still be correct according to the standard. It just won't have
860 the "recommended" value).*/
861 scr = 36000 + s->packet_number * 1200;
Hauke Duden22494482004-04-26 22:16:06862
863
864#if 0
865 for(i=0;i<ctx->nb_streams;i++) {
866 stream = ctx->streams[i]->priv_data;
867
868 if(scr > stream->start_pts && stream->start_pts!=AV_NOPTS_VALUE) {
869 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);
870 }
871 }
872#endif
873 }
Hauke Duden24515922004-02-19 22:34:13874 else {
Hauke Duden22494482004-04-26 22:16:06875
876
Hauke Duden24515922004-02-19 22:34:13877 /* XXX I believe this calculation of SCR is wrong. SCR
878 specifies at which time the data should enter the decoder.
879 Two packs cannot enter the decoder at the same time. */
880
881 /* XXX: system clock should be computed precisely, especially for
882 CBR case. The current mode gives at least something coherent */
Michael Niedermayer7000a172004-10-03 02:42:01883 if (stream_index == 0
Hauke Duden24515922004-02-19 22:34:13884 && pts != AV_NOPTS_VALUE)
885 scr = pts;
886 else
887 scr = s->last_scr;
Hauke Duden24515922004-02-19 22:34:13888 }
889
890 s->last_scr=scr;
891
892 return scr;
893}
894
Michael Niedermayer7000a172004-10-03 02:42:01895static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
896// MpegMuxContext *s = ctx->priv_data;
897 int i;
898
899 for(i=0; i<ctx->nb_streams; i++){
900 AVStream *st = ctx->streams[i];
901 StreamInfo *stream = st->priv_data;
902 PacketDesc *pkt_desc= stream->predecode_packet;
903
904 while(pkt_desc && scr > pkt_desc->dts){ //FIXME > vs >=
905 if(stream->buffer_index < pkt_desc->size ||
906 stream->predecode_packet == stream->premux_packet){
907 av_log(ctx, AV_LOG_ERROR, "buffer underflow\n");
908 break;
909 }
910 stream->buffer_index -= pkt_desc->size;
911
912 stream->predecode_packet= pkt_desc->next;
913 av_freep(&pkt_desc);
914 }
915 }
916
917 return 0;
918}
919
920static int output_packet(AVFormatContext *ctx, int flush){
921 MpegMuxContext *s = ctx->priv_data;
922 AVStream *st;
923 StreamInfo *stream;
924 int i, avail_space, es_size, trailer_size;
925 int best_i= -1;
926 int best_score= INT_MIN;
927 int ignore_constraints=0;
928 int64_t scr= s->last_scr;
929
930retry:
931 for(i=0; i<ctx->nb_streams; i++){
932 AVStream *st = ctx->streams[i];
933 StreamInfo *stream = st->priv_data;
934 const int avail_data= fifo_size(&stream->fifo, stream->fifo.rptr);
935 const int space= stream->max_buffer_size - stream->buffer_index;
936 int rel_space= 1024*space / stream->max_buffer_size;
937
938 if(s->packet_size > avail_data && !flush)
939 return 0;
940 if(avail_data==0)
941 continue;
942 assert(avail_data>0);
943
944 if(space < s->packet_size && !ignore_constraints)
945 continue;
946
947 if(rel_space > best_score){
948 best_score= rel_space;
949 best_i = i;
950 avail_space= space;
951 }
952 }
953
954 if(best_i < 0){
955 int64_t best_dts= INT64_MAX;
956
957 for(i=0; i<ctx->nb_streams; i++){
958 AVStream *st = ctx->streams[i];
959 StreamInfo *stream = st->priv_data;
960 PacketDesc *pkt_desc= stream->predecode_packet;
961 if(pkt_desc && pkt_desc->dts < best_dts)
962 best_dts= pkt_desc->dts;
963 }
964
965#if 0
966 av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
967 scr/90000.0, best_dts/90000.0);
968#endif
969 if(best_dts == INT64_MAX)
970 return 0;
971
972 if(scr >= best_dts+1 && !ignore_constraints){
973 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
974 ignore_constraints= 1;
975 }
976 scr= FFMAX(best_dts+1, scr);
977 if(remove_decoded_packets(ctx, scr) < 0)
978 return -1;
979 goto retry;
980 }
981
982 assert(best_i >= 0);
983
984 st = ctx->streams[best_i];
985 stream = st->priv_data;
986
987 assert(fifo_size(&stream->fifo, stream->fifo.rptr) > 0);
988
989 assert(avail_space >= s->packet_size || ignore_constraints);
990
991 if(stream->premux_packet->unwritten_size == stream->premux_packet->size)
992 trailer_size= 0;
993 else
994 trailer_size= stream->premux_packet->unwritten_size;
995
996 es_size= flush_packet(ctx, best_i, stream->premux_packet->pts, stream->premux_packet->dts, scr, trailer_size);
997
998 if (s->is_vcd) {
999 /* Write one or more padding sectors, if necessary, to reach
1000 the constant overall bitrate.*/
1001 int vcd_pad_bytes;
1002
1003 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1004 put_vcd_padding_sector(ctx);
1005 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1006 }
1007 }
1008
1009 stream->buffer_index += es_size;
1010 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1011
1012 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
1013 es_size -= stream->premux_packet->unwritten_size;
1014 stream->premux_packet= stream->premux_packet->next;
1015 }
1016 if(es_size)
1017 stream->premux_packet->unwritten_size -= es_size;
1018
1019 if(remove_decoded_packets(ctx, s->last_scr) < 0)
1020 return -1;
1021
1022 return 1;
1023}
Hauke Duden24515922004-02-19 22:34:131024
Michael Niedermayere9286492004-05-29 02:06:321025static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Fabrice Bellardde6d9b62001-07-22 14:18:561026{
1027 MpegMuxContext *s = ctx->priv_data;
Michael Niedermayere9286492004-05-29 02:06:321028 int stream_index= pkt->stream_index;
1029 int size= pkt->size;
1030 uint8_t *buf= pkt->data;
Fabrice Bellardde6d9b62001-07-22 14:18:561031 AVStream *st = ctx->streams[stream_index];
1032 StreamInfo *stream = st->priv_data;
Fabrice Bellarde45f1942003-12-18 13:03:371033 int64_t pts, dts, new_start_pts, new_start_dts;
Fabrice Bellard0dbb48d2003-12-16 11:25:301034 int len, avail_size;
Michael Niedermayer7000a172004-10-03 02:42:011035 PacketDesc *pkt_desc;
Hauke Duden24515922004-02-19 22:34:131036
Michael Niedermayere9286492004-05-29 02:06:321037 pts= pkt->pts;
1038 dts= pkt->dts;
Fabrice Bellarde45f1942003-12-18 13:03:371039
Hauke Duden22494482004-04-26 22:16:061040 if(s->is_svcd) {
1041 /* offset pts and dts slightly into the future to be able
1042 to do the compatibility fix below.*/
1043 pts = (pts + 2) & ((1LL << 33) - 1);
1044 dts = (dts + 2) & ((1LL << 33) - 1);
1045
1046 if (stream->packet_number == 0 && dts == pts)
1047 /* For the very first packet we want to force the DTS to be included.
1048 This increases compatibility with lots of DVD players.
1049 Since the MPEG-2 standard mandates that DTS is only written when
1050 it is different from PTS we have to move it slightly into the past.*/
1051 dts = (dts - 2) & ((1LL << 33) - 1);
1052 }
1053 if(s->is_vcd) {
1054 /* We have to offset the PTS, so that it is consistent with the SCR.
1055 SCR starts at 36000, but the first two packs contain only padding
1056 and the first pack from the other stream, respectively, may also have
1057 been written before.
1058 So the real data starts at SCR 36000+3*1200. */
1059 pts = (pts + 36000 + 3600) & ((1LL << 33) - 1);
1060 dts = (dts + 36000 + 3600) & ((1LL << 33) - 1);
Michael Niedermayer7000a172004-10-03 02:42:011061 }else{
1062 pts = (pts + PRELOAD) & ((1LL << 33) - 1);
1063 dts = (dts + PRELOAD) & ((1LL << 33) - 1);
1064 }
1065
1066 *stream->next_packet=
1067 pkt_desc= av_mallocz(sizeof(PacketDesc));
1068 pkt_desc->pts= pts;
1069 pkt_desc->dts= dts;
1070 pkt_desc->unwritten_size=
1071 pkt_desc->size= size;
1072 if(!stream->predecode_packet)
1073 stream->predecode_packet= pkt_desc;
1074 stream->next_packet= &pkt_desc->next;
1075
1076 if(stream->fifo.end - stream->fifo.buffer - fifo_size(&stream->fifo, stream->fifo.rptr) < size){
1077 av_log(ctx, AV_LOG_ERROR, "fifo overflow\n");
1078 return -1;
1079 }
1080 fifo_write(&stream->fifo, buf, size, &stream->fifo.wptr);
1081
1082 for(;;){
1083 int ret= output_packet(ctx, 0);
1084 if(ret<0)
1085 return ret;
1086 else if(ret==0)
1087 break;
Hauke Duden22494482004-04-26 22:16:061088 }
Juanjo10bb7022002-04-07 21:44:291089
Michel Bardiaux27a206e2003-12-09 18:06:181090#if 0
Michel Bardiaux27a206e2003-12-09 18:06:181091
Michel Bardiaux27a206e2003-12-09 18:06:181092 /* we assume here that pts != AV_NOPTS_VALUE */
Fabrice Bellard0dbb48d2003-12-16 11:25:301093 new_start_pts = stream->start_pts;
1094 new_start_dts = stream->start_dts;
1095
Michel Bardiaux27a206e2003-12-09 18:06:181096 if (stream->start_pts == AV_NOPTS_VALUE) {
Fabrice Bellard0dbb48d2003-12-16 11:25:301097 new_start_pts = pts;
1098 new_start_dts = dts;
Michel Bardiaux27a206e2003-12-09 18:06:181099 }
Fabrice Bellard0dbb48d2003-12-16 11:25:301100 avail_size = get_packet_payload_size(ctx, stream_index,
1101 new_start_pts,
1102 new_start_dts);
1103 if (stream->buffer_ptr >= avail_size) {
Hauke Duden24515922004-02-19 22:34:131104
1105 update_scr(ctx,stream_index,stream->start_pts);
1106
Fabrice Bellard0dbb48d2003-12-16 11:25:301107 /* unlikely case: outputing the pts or dts increase the packet
1108 size so that we cannot write the start of the next
1109 packet. In this case, we must flush the current packet with
Hauke Duden24515922004-02-19 22:34:131110 padding.
1111 Note: this always happens for the first audio and video packet
1112 in a VCD file, since they do not carry any data.*/
Fabrice Bellard0dbb48d2003-12-16 11:25:301113 flush_packet(ctx, stream_index,
1114 stream->start_pts, stream->start_dts, s->last_scr);
1115 stream->buffer_ptr = 0;
1116 }
1117 stream->start_pts = new_start_pts;
1118 stream->start_dts = new_start_dts;
1119 stream->nb_frames++;
Fabrice Bellardde6d9b62001-07-22 14:18:561120 while (size > 0) {
Fabrice Bellard0dbb48d2003-12-16 11:25:301121 avail_size = get_packet_payload_size(ctx, stream_index,
1122 stream->start_pts,
1123 stream->start_dts);
1124 len = avail_size - stream->buffer_ptr;
Fabrice Bellardde6d9b62001-07-22 14:18:561125 if (len > size)
1126 len = size;
1127 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
1128 stream->buffer_ptr += len;
1129 buf += len;
1130 size -= len;
Fabrice Bellard0dbb48d2003-12-16 11:25:301131 if (stream->buffer_ptr >= avail_size) {
Hauke Duden24515922004-02-19 22:34:131132
1133 update_scr(ctx,stream_index,stream->start_pts);
1134
Fabrice Bellard0dbb48d2003-12-16 11:25:301135 /* if packet full, we send it now */
Michel Bardiaux27a206e2003-12-09 18:06:181136 flush_packet(ctx, stream_index,
1137 stream->start_pts, stream->start_dts, s->last_scr);
Fabrice Bellard0dbb48d2003-12-16 11:25:301138 stream->buffer_ptr = 0;
Hauke Duden24515922004-02-19 22:34:131139
1140 if (s->is_vcd) {
1141 /* Write one or more padding sectors, if necessary, to reach
1142 the constant overall bitrate.*/
1143 int vcd_pad_bytes;
1144
1145 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size)
1146 put_vcd_padding_sector(ctx);
1147 }
1148
Michel Bardiaux27a206e2003-12-09 18:06:181149 /* Make sure only the FIRST pes packet for this frame has
1150 a timestamp */
1151 stream->start_pts = AV_NOPTS_VALUE;
1152 stream->start_dts = AV_NOPTS_VALUE;
Fabrice Bellardde6d9b62001-07-22 14:18:561153 }
1154 }
Michael Niedermayer7000a172004-10-03 02:42:011155#endif
Fabrice Bellardde6d9b62001-07-22 14:18:561156 return 0;
1157}
1158
1159static int mpeg_mux_end(AVFormatContext *ctx)
1160{
Michael Niedermayer7000a172004-10-03 02:42:011161// MpegMuxContext *s = ctx->priv_data;
Fabrice Bellardde6d9b62001-07-22 14:18:561162 StreamInfo *stream;
1163 int i;
Michael Niedermayer7000a172004-10-03 02:42:011164
1165 for(;;){
1166 int ret= output_packet(ctx, 1);
1167 if(ret<0)
1168 return ret;
1169 else if(ret==0)
1170 break;
Fabrice Bellardde6d9b62001-07-22 14:18:561171 }
1172
Fabrice Bellardfa0f62c2003-09-10 22:44:301173 /* End header according to MPEG1 systems standard. We do not write
1174 it as it is usually not needed by decoders and because it
1175 complicates MPEG stream concatenation. */
Juanjo92b3e122002-05-12 21:38:541176 //put_be32(&ctx->pb, ISO_11172_END_CODE);
1177 //put_flush_packet(&ctx->pb);
Michael Niedermayer9d90c372003-09-09 19:32:521178
Michael Niedermayer7000a172004-10-03 02:42:011179 for(i=0;i<ctx->nb_streams;i++) {
1180 stream = ctx->streams[i]->priv_data;
1181
1182 assert(fifo_size(&stream->fifo, stream->fifo.rptr) == 0);
1183 fifo_free(&stream->fifo);
1184 }
Fabrice Bellardde6d9b62001-07-22 14:18:561185 return 0;
1186}
Mike Melanson764ef402003-10-14 04:15:531187#endif //CONFIG_ENCODERS
Fabrice Bellardde6d9b62001-07-22 14:18:561188
1189/*********************************************/
1190/* demux code */
1191
1192#define MAX_SYNC_SIZE 100000
1193
Fabrice Bellarddb7f1f92002-05-20 16:29:401194static int mpegps_probe(AVProbeData *p)
1195{
Michael Niedermayer95f97de2004-10-01 16:00:001196 int i;
1197 int size= FFMIN(20, p->buf_size);
1198 uint32_t code=0xFF;
Fabrice Bellarddb7f1f92002-05-20 16:29:401199
Fabrice Bellarddb7f1f92002-05-20 16:29:401200 /* we search the first start code. If it is a packet start code,
1201 then we decide it is mpeg ps. We do not send highest value to
1202 give a chance to mpegts */
Fabrice Bellardfa7773212003-02-02 20:04:031203 /* NOTE: the search range was restricted to avoid too many false
1204 detections */
1205
Michael Niedermayer95f97de2004-10-01 16:00:001206 for (i = 0; i < size; i++) {
1207 code = (code << 8) | p->buf[i];
Isaac Richardsec23a472003-07-10 09:04:041208 if ((code & 0xffffff00) == 0x100) {
1209 if (code == PACK_START_CODE ||
1210 code == SYSTEM_HEADER_START_CODE ||
1211 (code >= 0x1e0 && code <= 0x1ef) ||
1212 (code >= 0x1c0 && code <= 0x1df) ||
1213 code == PRIVATE_STREAM_2 ||
1214 code == PROGRAM_STREAM_MAP ||
1215 code == PRIVATE_STREAM_1 ||
1216 code == PADDING_STREAM)
Michael Niedermayer149f7c02003-09-01 18:30:021217 return AVPROBE_SCORE_MAX - 2;
Isaac Richardsec23a472003-07-10 09:04:041218 else
1219 return 0;
1220 }
Fabrice Bellarddb7f1f92002-05-20 16:29:401221 }
1222 return 0;
1223}
1224
1225
Fabrice Bellardde6d9b62001-07-22 14:18:561226typedef struct MpegDemuxContext {
1227 int header_state;
Fabrice Bellardde6d9b62001-07-22 14:18:561228} MpegDemuxContext;
1229
Fabrice Bellard27f388a2003-11-10 18:47:521230static int mpegps_read_header(AVFormatContext *s,
1231 AVFormatParameters *ap)
1232{
1233 MpegDemuxContext *m = s->priv_data;
1234 m->header_state = 0xff;
1235 s->ctx_flags |= AVFMTCTX_NOHEADER;
1236
1237 /* no need to do more */
1238 return 0;
1239}
1240
1241static int64_t get_pts(ByteIOContext *pb, int c)
1242{
1243 int64_t pts;
1244 int val;
1245
1246 if (c < 0)
1247 c = get_byte(pb);
1248 pts = (int64_t)((c >> 1) & 0x07) << 30;
1249 val = get_be16(pb);
1250 pts |= (int64_t)(val >> 1) << 15;
1251 val = get_be16(pb);
1252 pts |= (int64_t)(val >> 1);
1253 return pts;
1254}
1255
1256static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
1257 uint32_t *header_state)
Fabrice Bellardde6d9b62001-07-22 14:18:561258{
1259 unsigned int state, v;
1260 int val, n;
1261
1262 state = *header_state;
1263 n = *size_ptr;
1264 while (n > 0) {
1265 if (url_feof(pb))
1266 break;
1267 v = get_byte(pb);
1268 n--;
1269 if (state == 0x000001) {
1270 state = ((state << 8) | v) & 0xffffff;
1271 val = state;
1272 goto found;
1273 }
1274 state = ((state << 8) | v) & 0xffffff;
1275 }
1276 val = -1;
1277 found:
1278 *header_state = state;
1279 *size_ptr = n;
1280 return val;
1281}
1282
Fabrice Bellard27f388a2003-11-10 18:47:521283/* XXX: optimize */
1284static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
Juanjo001e3f52002-03-17 17:44:451285{
Fabrice Bellard27f388a2003-11-10 18:47:521286 int64_t pos, pos_start;
1287 int max_size, start_code;
Fabrice Bellardda24c5e2003-10-29 14:20:561288
Fabrice Bellard27f388a2003-11-10 18:47:521289 max_size = *size_ptr;
1290 pos_start = url_ftell(pb);
1291
1292 /* in order to go faster, we fill the buffer */
1293 pos = pos_start - 16386;
1294 if (pos < 0)
1295 pos = 0;
1296 url_fseek(pb, pos, SEEK_SET);
1297 get_byte(pb);
1298
1299 pos = pos_start;
1300 for(;;) {
1301 pos--;
1302 if (pos < 0 || (pos_start - pos) >= max_size) {
1303 start_code = -1;
1304 goto the_end;
1305 }
1306 url_fseek(pb, pos, SEEK_SET);
1307 start_code = get_be32(pb);
1308 if ((start_code & 0xffffff00) == 0x100)
1309 break;
1310 }
1311 the_end:
1312 *size_ptr = pos_start - pos;
1313 return start_code;
Fabrice Bellardde6d9b62001-07-22 14:18:561314}
1315
Michael Niedermayer8d14a252004-04-12 16:50:031316/* read the next PES header. Return its position in ppos
Fabrice Bellard27f388a2003-11-10 18:47:521317 (if not NULL), and its start code, pts and dts.
1318 */
1319static int mpegps_read_pes_header(AVFormatContext *s,
1320 int64_t *ppos, int *pstart_code,
Michael Niedermayer8d14a252004-04-12 16:50:031321 int64_t *ppts, int64_t *pdts)
Fabrice Bellardde6d9b62001-07-22 14:18:561322{
1323 MpegDemuxContext *m = s->priv_data;
Fabrice Bellard27f388a2003-11-10 18:47:521324 int len, size, startcode, c, flags, header_len;
1325 int64_t pts, dts, last_pos;
Fabrice Bellardde6d9b62001-07-22 14:18:561326
Fabrice Bellard27f388a2003-11-10 18:47:521327 last_pos = -1;
Fabrice Bellardde6d9b62001-07-22 14:18:561328 redo:
Fabrice Bellard27f388a2003-11-10 18:47:521329 /* next start code (should be immediately after) */
1330 m->header_state = 0xff;
1331 size = MAX_SYNC_SIZE;
1332 startcode = find_next_start_code(&s->pb, &size, &m->header_state);
Juanjo001e3f52002-03-17 17:44:451333 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
Fabrice Bellardde6d9b62001-07-22 14:18:561334 if (startcode < 0)
Mike Melanson0bd586c2004-06-19 03:59:341335 return AVERROR_IO;
Fabrice Bellardde6d9b62001-07-22 14:18:561336 if (startcode == PACK_START_CODE)
1337 goto redo;
1338 if (startcode == SYSTEM_HEADER_START_CODE)
1339 goto redo;
1340 if (startcode == PADDING_STREAM ||
1341 startcode == PRIVATE_STREAM_2) {
1342 /* skip them */
1343 len = get_be16(&s->pb);
1344 url_fskip(&s->pb, len);
1345 goto redo;
1346 }
1347 /* find matching stream */
1348 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
1349 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
1350 (startcode == 0x1bd)))
1351 goto redo;
Fabrice Bellard27f388a2003-11-10 18:47:521352 if (ppos) {
1353 *ppos = url_ftell(&s->pb) - 4;
1354 }
Fabrice Bellardde6d9b62001-07-22 14:18:561355 len = get_be16(&s->pb);
Fabrice Bellardb2cac182002-10-21 15:57:211356 pts = AV_NOPTS_VALUE;
1357 dts = AV_NOPTS_VALUE;
Fabrice Bellardde6d9b62001-07-22 14:18:561358 /* stuffing */
1359 for(;;) {
Fabrice Bellard27f388a2003-11-10 18:47:521360 if (len < 1)
1361 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:561362 c = get_byte(&s->pb);
1363 len--;
1364 /* XXX: for mpeg1, should test only bit 7 */
1365 if (c != 0xff)
1366 break;
1367 }
1368 if ((c & 0xc0) == 0x40) {
1369 /* buffer scale & size */
Fabrice Bellard27f388a2003-11-10 18:47:521370 if (len < 2)
1371 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:561372 get_byte(&s->pb);
1373 c = get_byte(&s->pb);
1374 len -= 2;
1375 }
1376 if ((c & 0xf0) == 0x20) {
Fabrice Bellard27f388a2003-11-10 18:47:521377 if (len < 4)
1378 goto redo;
1379 dts = pts = get_pts(&s->pb, c);
Fabrice Bellardde6d9b62001-07-22 14:18:561380 len -= 4;
Fabrice Bellardde6d9b62001-07-22 14:18:561381 } else if ((c & 0xf0) == 0x30) {
Fabrice Bellard27f388a2003-11-10 18:47:521382 if (len < 9)
1383 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:561384 pts = get_pts(&s->pb, c);
1385 dts = get_pts(&s->pb, -1);
1386 len -= 9;
1387 } else if ((c & 0xc0) == 0x80) {
1388 /* mpeg 2 PES */
1389 if ((c & 0x30) != 0) {
Fabrice Bellard27f388a2003-11-10 18:47:521390 /* Encrypted multiplex not handled */
1391 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:561392 }
1393 flags = get_byte(&s->pb);
1394 header_len = get_byte(&s->pb);
1395 len -= 2;
1396 if (header_len > len)
1397 goto redo;
Fabrice Bellard1e5c6672002-10-04 15:46:591398 if ((flags & 0xc0) == 0x80) {
Fabrice Bellard27f388a2003-11-10 18:47:521399 dts = pts = get_pts(&s->pb, -1);
1400 if (header_len < 5)
1401 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:561402 header_len -= 5;
1403 len -= 5;
1404 } if ((flags & 0xc0) == 0xc0) {
1405 pts = get_pts(&s->pb, -1);
1406 dts = get_pts(&s->pb, -1);
Fabrice Bellard27f388a2003-11-10 18:47:521407 if (header_len < 10)
1408 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:561409 header_len -= 10;
1410 len -= 10;
1411 }
1412 len -= header_len;
1413 while (header_len > 0) {
1414 get_byte(&s->pb);
1415 header_len--;
1416 }
1417 }
Dmitry Borisovdf70de12004-04-23 21:02:011418 else if( c!= 0xf )
1419 goto redo;
1420
Fabrice Bellardde6d9b62001-07-22 14:18:561421 if (startcode == 0x1bd) {
Fabrice Bellard27f388a2003-11-10 18:47:521422 if (len < 1)
1423 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:561424 startcode = get_byte(&s->pb);
1425 len--;
1426 if (startcode >= 0x80 && startcode <= 0xbf) {
1427 /* audio: skip header */
Fabrice Bellard27f388a2003-11-10 18:47:521428 if (len < 3)
1429 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:561430 get_byte(&s->pb);
1431 get_byte(&s->pb);
1432 get_byte(&s->pb);
1433 len -= 3;
1434 }
1435 }
Michael Niedermayerb7549782004-01-13 22:02:491436 if(dts != AV_NOPTS_VALUE && ppos){
1437 int i;
1438 for(i=0; i<s->nb_streams; i++){
1439 if(startcode == s->streams[i]->id) {
Michael Niedermayercdd50342004-05-23 16:26:121440 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0 /* FIXME keyframe? */);
Michael Niedermayerb7549782004-01-13 22:02:491441 }
1442 }
1443 }
1444
Fabrice Bellard27f388a2003-11-10 18:47:521445 *pstart_code = startcode;
1446 *ppts = pts;
1447 *pdts = dts;
1448 return len;
1449}
1450
1451static int mpegps_read_packet(AVFormatContext *s,
1452 AVPacket *pkt)
1453{
1454 AVStream *st;
Mike Melanson0bd586c2004-06-19 03:59:341455 int len, startcode, i, type, codec_id = 0;
Michael Niedermayerb7549782004-01-13 22:02:491456 int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
Fabrice Bellard27f388a2003-11-10 18:47:521457
1458 redo:
Michael Niedermayer8d14a252004-04-12 16:50:031459 len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
Fabrice Bellard27f388a2003-11-10 18:47:521460 if (len < 0)
1461 return len;
Michel Bardiaux27a206e2003-12-09 18:06:181462
Fabrice Bellardde6d9b62001-07-22 14:18:561463 /* now find stream */
1464 for(i=0;i<s->nb_streams;i++) {
1465 st = s->streams[i];
1466 if (st->id == startcode)
1467 goto found;
1468 }
Fabrice Bellarddb7f1f92002-05-20 16:29:401469 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
1470 type = CODEC_TYPE_VIDEO;
Fabrice Bellard0dbb48d2003-12-16 11:25:301471 codec_id = CODEC_ID_MPEG2VIDEO;
Fabrice Bellarddb7f1f92002-05-20 16:29:401472 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
1473 type = CODEC_TYPE_AUDIO;
1474 codec_id = CODEC_ID_MP2;
Michael Niedermayer23c99252004-07-14 01:32:141475 } else if (startcode >= 0x80 && startcode <= 0x89) {
Fabrice Bellarddb7f1f92002-05-20 16:29:401476 type = CODEC_TYPE_AUDIO;
1477 codec_id = CODEC_ID_AC3;
Michael Niedermayer23c99252004-07-14 01:32:141478 } else if (startcode >= 0x8a && startcode <= 0x9f) {
1479 type = CODEC_TYPE_AUDIO;
1480 codec_id = CODEC_ID_DTS;
Fabrice Bellard9ec05e32003-01-31 17:04:461481 } else if (startcode >= 0xa0 && startcode <= 0xbf) {
1482 type = CODEC_TYPE_AUDIO;
1483 codec_id = CODEC_ID_PCM_S16BE;
Fabrice Bellarddb7f1f92002-05-20 16:29:401484 } else {
1485 skip:
1486 /* skip packet */
1487 url_fskip(&s->pb, len);
1488 goto redo;
1489 }
Fabrice Bellard1e5c6672002-10-04 15:46:591490 /* no stream found: add a new stream */
1491 st = av_new_stream(s, startcode);
1492 if (!st)
1493 goto skip;
Fabrice Bellarddb7f1f92002-05-20 16:29:401494 st->codec.codec_type = type;
1495 st->codec.codec_id = codec_id;
Fabrice Bellard27f388a2003-11-10 18:47:521496 if (codec_id != CODEC_ID_PCM_S16BE)
1497 st->need_parsing = 1;
Fabrice Bellardde6d9b62001-07-22 14:18:561498 found:
Fabrice Bellard9ec05e32003-01-31 17:04:461499 if (startcode >= 0xa0 && startcode <= 0xbf) {
1500 int b1, freq;
Fabrice Bellard9ec05e32003-01-31 17:04:461501
1502 /* for LPCM, we just skip the header and consider it is raw
1503 audio data */
1504 if (len <= 3)
1505 goto skip;
1506 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
1507 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
1508 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
1509 len -= 3;
1510 freq = (b1 >> 4) & 3;
1511 st->codec.sample_rate = lpcm_freq_tab[freq];
1512 st->codec.channels = 1 + (b1 & 7);
1513 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
1514 }
Fabrice Bellardde6d9b62001-07-22 14:18:561515 av_new_packet(pkt, len);
1516 get_buffer(&s->pb, pkt->data, pkt->size);
1517 pkt->pts = pts;
Fabrice Bellard27f388a2003-11-10 18:47:521518 pkt->dts = dts;
Fabrice Bellarddb7f1f92002-05-20 16:29:401519 pkt->stream_index = st->index;
Michel Bardiaux27a206e2003-12-09 18:06:181520#if 0
Michael Niedermayer3c895fc2004-05-29 18:50:311521 av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f\n",
Michel Bardiaux27a206e2003-12-09 18:06:181522 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0);
1523#endif
Mike Melanson0bd586c2004-06-19 03:59:341524
Fabrice Bellardde6d9b62001-07-22 14:18:561525 return 0;
1526}
1527
Fabrice Bellarddb7f1f92002-05-20 16:29:401528static int mpegps_read_close(AVFormatContext *s)
Juanjo001e3f52002-03-17 17:44:451529{
Fabrice Bellardde6d9b62001-07-22 14:18:561530 return 0;
1531}
1532
Fabrice Bellard27f388a2003-11-10 18:47:521533static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
Michael Niedermayer8d14a252004-04-12 16:50:031534 int64_t *ppos, int64_t pos_limit)
Fabrice Bellard27f388a2003-11-10 18:47:521535{
1536 int len, startcode;
1537 int64_t pos, pts, dts;
1538
1539 pos = *ppos;
1540#ifdef DEBUG_SEEK
1541 printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
1542#endif
1543 url_fseek(&s->pb, pos, SEEK_SET);
1544 for(;;) {
Michael Niedermayer8d14a252004-04-12 16:50:031545 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
Fabrice Bellard27f388a2003-11-10 18:47:521546 if (len < 0) {
1547#ifdef DEBUG_SEEK
1548 printf("none (ret=%d)\n", len);
1549#endif
1550 return AV_NOPTS_VALUE;
1551 }
1552 if (startcode == s->streams[stream_index]->id &&
1553 dts != AV_NOPTS_VALUE) {
1554 break;
1555 }
Michael Niedermayer8d14a252004-04-12 16:50:031556 url_fskip(&s->pb, len);
Fabrice Bellard27f388a2003-11-10 18:47:521557 }
1558#ifdef DEBUG_SEEK
1559 printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
1560#endif
1561 *ppos = pos;
Michael Niedermayercdd50342004-05-23 16:26:121562 return dts;
Fabrice Bellard27f388a2003-11-10 18:47:521563}
1564
Mike Melanson764ef402003-10-14 04:15:531565#ifdef CONFIG_ENCODERS
Fabrice Bellardfb7566d2002-10-15 10:22:231566static AVOutputFormat mpeg1system_mux = {
Fabrice Bellardde6d9b62001-07-22 14:18:561567 "mpeg",
Fabrice Bellardfb7566d2002-10-15 10:22:231568 "MPEG1 System format",
Ryutaroh Matsumotoc6c11cb2002-12-20 23:10:581569 "video/mpeg",
Fabrice Bellardfb7566d2002-10-15 10:22:231570 "mpg,mpeg",
1571 sizeof(MpegMuxContext),
1572 CODEC_ID_MP2,
1573 CODEC_ID_MPEG1VIDEO,
1574 mpeg_mux_init,
1575 mpeg_mux_write_packet,
1576 mpeg_mux_end,
1577};
1578
1579static AVOutputFormat mpeg1vcd_mux = {
1580 "vcd",
1581 "MPEG1 System format (VCD)",
Ryutaroh Matsumotoc6c11cb2002-12-20 23:10:581582 "video/mpeg",
Fabrice Bellardfb7566d2002-10-15 10:22:231583 NULL,
1584 sizeof(MpegMuxContext),
1585 CODEC_ID_MP2,
1586 CODEC_ID_MPEG1VIDEO,
1587 mpeg_mux_init,
1588 mpeg_mux_write_packet,
1589 mpeg_mux_end,
1590};
1591
1592static AVOutputFormat mpeg2vob_mux = {
1593 "vob",
1594 "MPEG2 PS format (VOB)",
Ryutaroh Matsumotoc6c11cb2002-12-20 23:10:581595 "video/mpeg",
Fabrice Bellardfb7566d2002-10-15 10:22:231596 "vob",
Fabrice Bellarddb7f1f92002-05-20 16:29:401597 sizeof(MpegMuxContext),
Fabrice Bellardde6d9b62001-07-22 14:18:561598 CODEC_ID_MP2,
Fabrice Bellard0dbb48d2003-12-16 11:25:301599 CODEC_ID_MPEG2VIDEO,
Fabrice Bellardde6d9b62001-07-22 14:18:561600 mpeg_mux_init,
1601 mpeg_mux_write_packet,
1602 mpeg_mux_end,
Fabrice Bellardde6d9b62001-07-22 14:18:561603};
Hauke Duden24515922004-02-19 22:34:131604
1605/* Same as mpeg2vob_mux except that the pack size is 2324 */
1606static AVOutputFormat mpeg2svcd_mux = {
1607 "svcd",
1608 "MPEG2 PS format (VOB)",
1609 "video/mpeg",
1610 "vob",
1611 sizeof(MpegMuxContext),
1612 CODEC_ID_MP2,
1613 CODEC_ID_MPEG2VIDEO,
1614 mpeg_mux_init,
1615 mpeg_mux_write_packet,
1616 mpeg_mux_end,
1617};
1618
1619
1620
Mike Melanson764ef402003-10-14 04:15:531621#endif //CONFIG_ENCODERS
Fabrice Bellarddb7f1f92002-05-20 16:29:401622
Fabrice Bellard32f38cb2003-08-08 17:54:051623AVInputFormat mpegps_demux = {
Fabrice Bellarddb7f1f92002-05-20 16:29:401624 "mpeg",
1625 "MPEG PS format",
1626 sizeof(MpegDemuxContext),
1627 mpegps_probe,
1628 mpegps_read_header,
1629 mpegps_read_packet,
1630 mpegps_read_close,
Michael Niedermayer8d14a252004-04-12 16:50:031631 NULL, //mpegps_read_seek,
1632 mpegps_read_dts,
Fabrice Bellarddb7f1f92002-05-20 16:29:401633};
1634
1635int mpegps_init(void)
1636{
Mike Melanson764ef402003-10-14 04:15:531637#ifdef CONFIG_ENCODERS
Fabrice Bellardfb7566d2002-10-15 10:22:231638 av_register_output_format(&mpeg1system_mux);
1639 av_register_output_format(&mpeg1vcd_mux);
1640 av_register_output_format(&mpeg2vob_mux);
Hauke Duden24515922004-02-19 22:34:131641 av_register_output_format(&mpeg2svcd_mux);
Mike Melanson764ef402003-10-14 04:15:531642#endif //CONFIG_ENCODERS
Fabrice Bellarddb7f1f92002-05-20 16:29:401643 av_register_input_format(&mpegps_demux);
1644 return 0;
1645}