blob: 447b6d483d3cdebea49f3d1514f4cb372cce9cd8 [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
Fabrice Bellard27f388a2003-11-10 18:47:5222//#define DEBUG_SEEK
Fabrice Bellardde6d9b62001-07-22 14:18:5623
Michael Niedermayerb7549782004-01-13 22:02:4924#undef NDEBUG
25#include <assert.h>
26
Fabrice Bellardde6d9b62001-07-22 14:18:5627typedef struct {
Zdenek Kabelac0c1a9ed2003-02-11 16:35:4828 uint8_t buffer[MAX_PAYLOAD_SIZE];
Fabrice Bellardde6d9b62001-07-22 14:18:5629 int buffer_ptr;
Fabrice Bellard0dbb48d2003-12-16 11:25:3030 int nb_frames; /* number of starting frame encountered (AC3) */
31 int frame_start_offset; /* starting offset of the frame + 1 (0 if none) */
Zdenek Kabelac0c1a9ed2003-02-11 16:35:4832 uint8_t id;
Fabrice Bellardde6d9b62001-07-22 14:18:5633 int max_buffer_size; /* in bytes */
34 int packet_number;
Zdenek Kabelac0c1a9ed2003-02-11 16:35:4835 int64_t start_pts;
Michel Bardiaux27a206e2003-12-09 18:06:1836 int64_t start_dts;
Fabrice Bellard044007c2003-12-16 14:00:1837 uint8_t lpcm_header[3];
38 int lpcm_align;
Fabrice Bellardde6d9b62001-07-22 14:18:5639} StreamInfo;
40
41typedef struct {
42 int packet_size; /* required packet size */
Fabrice Bellardde6d9b62001-07-22 14:18:5643 int packet_number;
44 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
45 int system_header_freq;
Fabrice Bellard0dbb48d2003-12-16 11:25:3046 int system_header_size;
Fabrice Bellardde6d9b62001-07-22 14:18:5647 int mux_rate; /* bitrate in units of 50 bytes/s */
48 /* stream info */
49 int audio_bound;
50 int video_bound;
Fabrice Bellardfb7566d2002-10-15 10:22:2351 int is_mpeg2;
52 int is_vcd;
Michel Bardiaux27a206e2003-12-09 18:06:1853 int scr_stream_index; /* stream from which the system clock is
54 computed (VBR case) */
55 int64_t last_scr; /* current system clock */
Fabrice Bellardde6d9b62001-07-22 14:18:5656} MpegMuxContext;
57
58#define PACK_START_CODE ((unsigned int)0x000001ba)
59#define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
Juanjo92b3e122002-05-12 21:38:5460#define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
Fabrice Bellardde6d9b62001-07-22 14:18:5661#define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
62#define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
63#define ISO_11172_END_CODE ((unsigned int)0x000001b9)
64
65/* mpeg2 */
66#define PROGRAM_STREAM_MAP 0x1bc
67#define PRIVATE_STREAM_1 0x1bd
68#define PADDING_STREAM 0x1be
69#define PRIVATE_STREAM_2 0x1bf
70
71
72#define AUDIO_ID 0xc0
73#define VIDEO_ID 0xe0
Fabrice Bellard044007c2003-12-16 14:00:1874#define AC3_ID 0x80
75#define LPCM_ID 0xa0
Fabrice Bellardde6d9b62001-07-22 14:18:5676
Mike Melanson764ef402003-10-14 04:15:5377#ifdef CONFIG_ENCODERS
Fabrice Bellardfb7566d2002-10-15 10:22:2378extern AVOutputFormat mpeg1system_mux;
79extern AVOutputFormat mpeg1vcd_mux;
80extern AVOutputFormat mpeg2vob_mux;
81
Fabrice Bellard044007c2003-12-16 14:00:1882static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
83
Fabrice Bellardde6d9b62001-07-22 14:18:5684static int put_pack_header(AVFormatContext *ctx,
Zdenek Kabelac0c1a9ed2003-02-11 16:35:4885 uint8_t *buf, int64_t timestamp)
Fabrice Bellardde6d9b62001-07-22 14:18:5686{
87 MpegMuxContext *s = ctx->priv_data;
88 PutBitContext pb;
89
Alex Beregszaszi117a5492003-10-13 10:59:5790 init_put_bits(&pb, buf, 128);
Fabrice Bellardde6d9b62001-07-22 14:18:5691
92 put_bits(&pb, 32, PACK_START_CODE);
Fabrice Bellardb2cac182002-10-21 15:57:2193 if (s->is_mpeg2) {
Måns Rullgård8683e4a2003-07-15 22:15:3794 put_bits(&pb, 2, 0x1);
Fabrice Bellardb2cac182002-10-21 15:57:2195 } else {
96 put_bits(&pb, 4, 0x2);
97 }
Zdenek Kabelac0c1a9ed2003-02-11 16:35:4898 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
Fabrice Bellardde6d9b62001-07-22 14:18:5699 put_bits(&pb, 1, 1);
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48100 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
Fabrice Bellardde6d9b62001-07-22 14:18:56101 put_bits(&pb, 1, 1);
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48102 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
Fabrice Bellardde6d9b62001-07-22 14:18:56103 put_bits(&pb, 1, 1);
Fabrice Bellardb2cac182002-10-21 15:57:21104 if (s->is_mpeg2) {
105 /* clock extension */
106 put_bits(&pb, 9, 0);
107 put_bits(&pb, 1, 1);
108 }
Fabrice Bellardde6d9b62001-07-22 14:18:56109 put_bits(&pb, 1, 1);
110 put_bits(&pb, 22, s->mux_rate);
111 put_bits(&pb, 1, 1);
Fabrice Bellardb2cac182002-10-21 15:57:21112 if (s->is_mpeg2) {
113 put_bits(&pb, 5, 0x1f); /* reserved */
114 put_bits(&pb, 3, 0); /* stuffing length */
115 }
Fabrice Bellardde6d9b62001-07-22 14:18:56116 flush_put_bits(&pb);
Michael Niedermayer17592472002-02-12 15:43:16117 return pbBufPtr(&pb) - pb.buf;
Fabrice Bellardde6d9b62001-07-22 14:18:56118}
119
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48120static int put_system_header(AVFormatContext *ctx, uint8_t *buf)
Fabrice Bellardde6d9b62001-07-22 14:18:56121{
122 MpegMuxContext *s = ctx->priv_data;
123 int size, rate_bound, i, private_stream_coded, id;
124 PutBitContext pb;
125
Alex Beregszaszi117a5492003-10-13 10:59:57126 init_put_bits(&pb, buf, 128);
Fabrice Bellardde6d9b62001-07-22 14:18:56127
128 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
129 put_bits(&pb, 16, 0);
130 put_bits(&pb, 1, 1);
131
132 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
133 put_bits(&pb, 22, rate_bound);
134 put_bits(&pb, 1, 1); /* marker */
135 put_bits(&pb, 6, s->audio_bound);
136
137 put_bits(&pb, 1, 1); /* variable bitrate */
138 put_bits(&pb, 1, 1); /* non constrainted bit stream */
139
140 put_bits(&pb, 1, 0); /* audio locked */
141 put_bits(&pb, 1, 0); /* video locked */
142 put_bits(&pb, 1, 1); /* marker */
143
144 put_bits(&pb, 5, s->video_bound);
145 put_bits(&pb, 8, 0xff); /* reserved byte */
146
147 /* audio stream info */
148 private_stream_coded = 0;
149 for(i=0;i<ctx->nb_streams;i++) {
150 StreamInfo *stream = ctx->streams[i]->priv_data;
151 id = stream->id;
152 if (id < 0xc0) {
153 /* special case for private streams (AC3 use that) */
154 if (private_stream_coded)
155 continue;
156 private_stream_coded = 1;
157 id = 0xbd;
158 }
159 put_bits(&pb, 8, id); /* stream ID */
160 put_bits(&pb, 2, 3);
161 if (id < 0xe0) {
162 /* audio */
163 put_bits(&pb, 1, 0);
164 put_bits(&pb, 13, stream->max_buffer_size / 128);
165 } else {
166 /* video */
167 put_bits(&pb, 1, 1);
168 put_bits(&pb, 13, stream->max_buffer_size / 1024);
169 }
170 }
171 flush_put_bits(&pb);
Michael Niedermayer17592472002-02-12 15:43:16172 size = pbBufPtr(&pb) - pb.buf;
Fabrice Bellardde6d9b62001-07-22 14:18:56173 /* patch packet size */
174 buf[4] = (size - 6) >> 8;
175 buf[5] = (size - 6) & 0xff;
176
177 return size;
178}
179
Fabrice Bellard0dbb48d2003-12-16 11:25:30180static int get_system_header_size(AVFormatContext *ctx)
181{
182 int buf_index, i, private_stream_coded;
183 StreamInfo *stream;
184
185 buf_index = 12;
186 private_stream_coded = 0;
187 for(i=0;i<ctx->nb_streams;i++) {
188 stream = ctx->streams[i]->priv_data;
189 if (stream->id < 0xc0) {
190 if (private_stream_coded)
191 continue;
192 private_stream_coded = 1;
193 }
194 buf_index += 3;
195 }
196 return buf_index;
197}
198
Fabrice Bellardde6d9b62001-07-22 14:18:56199static int mpeg_mux_init(AVFormatContext *ctx)
200{
Fabrice Bellarddb7f1f92002-05-20 16:29:40201 MpegMuxContext *s = ctx->priv_data;
Fabrice Bellard044007c2003-12-16 14:00:18202 int bitrate, i, mpa_id, mpv_id, ac3_id, lpcm_id, j;
Fabrice Bellardde6d9b62001-07-22 14:18:56203 AVStream *st;
204 StreamInfo *stream;
205
Fabrice Bellardde6d9b62001-07-22 14:18:56206 s->packet_number = 0;
Fabrice Bellardfb7566d2002-10-15 10:22:23207 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
208 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux);
209
210 if (s->is_vcd)
Juanjo92b3e122002-05-12 21:38:54211 s->packet_size = 2324; /* VCD packet size */
212 else
213 s->packet_size = 2048;
214
Fabrice Bellardde6d9b62001-07-22 14:18:56215 s->audio_bound = 0;
216 s->video_bound = 0;
217 mpa_id = AUDIO_ID;
Fabrice Bellard044007c2003-12-16 14:00:18218 ac3_id = AC3_ID;
Fabrice Bellardde6d9b62001-07-22 14:18:56219 mpv_id = VIDEO_ID;
Fabrice Bellard044007c2003-12-16 14:00:18220 lpcm_id = LPCM_ID;
Michel Bardiaux27a206e2003-12-09 18:06:18221 s->scr_stream_index = -1;
Fabrice Bellardde6d9b62001-07-22 14:18:56222 for(i=0;i<ctx->nb_streams;i++) {
223 st = ctx->streams[i];
224 stream = av_mallocz(sizeof(StreamInfo));
225 if (!stream)
226 goto fail;
227 st->priv_data = stream;
228
229 switch(st->codec.codec_type) {
230 case CODEC_TYPE_AUDIO:
Fabrice Bellard044007c2003-12-16 14:00:18231 if (st->codec.codec_id == CODEC_ID_AC3) {
Fabrice Bellardde6d9b62001-07-22 14:18:56232 stream->id = ac3_id++;
Fabrice Bellard044007c2003-12-16 14:00:18233 } else if (st->codec.codec_id == CODEC_ID_PCM_S16BE) {
234 stream->id = lpcm_id++;
235 for(j = 0; j < 4; j++) {
236 if (lpcm_freq_tab[j] == st->codec.sample_rate)
237 break;
238 }
239 if (j == 4)
240 goto fail;
241 if (st->codec.channels > 8)
242 return -1;
243 stream->lpcm_header[0] = 0x0c;
244 stream->lpcm_header[1] = (st->codec.channels - 1) | (j << 4);
245 stream->lpcm_header[2] = 0x80;
246 stream->lpcm_align = st->codec.channels * 2;
247 } else {
Fabrice Bellardde6d9b62001-07-22 14:18:56248 stream->id = mpa_id++;
Fabrice Bellard044007c2003-12-16 14:00:18249 }
Fabrice Bellardde6d9b62001-07-22 14:18:56250 stream->max_buffer_size = 4 * 1024;
251 s->audio_bound++;
252 break;
253 case CODEC_TYPE_VIDEO:
Michel Bardiaux27a206e2003-12-09 18:06:18254 /* by default, video is used for the SCR computation */
255 if (s->scr_stream_index == -1)
256 s->scr_stream_index = i;
Fabrice Bellardde6d9b62001-07-22 14:18:56257 stream->id = mpv_id++;
258 stream->max_buffer_size = 46 * 1024;
259 s->video_bound++;
260 break;
Philip Gladstoneac5e6a52002-05-09 01:19:33261 default:
Philip Gladstone42343f72002-09-12 02:34:01262 av_abort();
Fabrice Bellardde6d9b62001-07-22 14:18:56263 }
264 }
Michel Bardiaux27a206e2003-12-09 18:06:18265 /* if no SCR, use first stream (audio) */
266 if (s->scr_stream_index == -1)
267 s->scr_stream_index = 0;
Fabrice Bellardde6d9b62001-07-22 14:18:56268
269 /* we increase slightly the bitrate to take into account the
270 headers. XXX: compute it exactly */
271 bitrate = 2000;
272 for(i=0;i<ctx->nb_streams;i++) {
273 st = ctx->streams[i];
274 bitrate += st->codec.bit_rate;
275 }
276 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
Juanjo92b3e122002-05-12 21:38:54277
Fabrice Bellardfb7566d2002-10-15 10:22:23278 if (s->is_vcd || s->is_mpeg2)
Juanjo92b3e122002-05-12 21:38:54279 /* every packet */
280 s->pack_header_freq = 1;
281 else
282 /* every 2 seconds */
283 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
Michael Niedermayerb623bbc2003-10-28 10:55:15284
285 /* the above seems to make pack_header_freq zero sometimes */
286 if (s->pack_header_freq == 0)
287 s->pack_header_freq = 1;
Juanjo92b3e122002-05-12 21:38:54288
Fabrice Bellardb2cac182002-10-21 15:57:21289 if (s->is_mpeg2)
290 /* every 200 packets. Need to look at the spec. */
291 s->system_header_freq = s->pack_header_freq * 40;
292 else if (s->is_vcd)
Juanjo92b3e122002-05-12 21:38:54293 /* every 40 packets, this is my invention */
294 s->system_header_freq = s->pack_header_freq * 40;
295 else
Juanjo92b3e122002-05-12 21:38:54296 s->system_header_freq = s->pack_header_freq * 5;
297
Fabrice Bellardde6d9b62001-07-22 14:18:56298 for(i=0;i<ctx->nb_streams;i++) {
299 stream = ctx->streams[i]->priv_data;
300 stream->buffer_ptr = 0;
301 stream->packet_number = 0;
Michel Bardiaux27a206e2003-12-09 18:06:18302 stream->start_pts = AV_NOPTS_VALUE;
303 stream->start_dts = AV_NOPTS_VALUE;
Fabrice Bellardde6d9b62001-07-22 14:18:56304 }
Fabrice Bellard0dbb48d2003-12-16 11:25:30305 s->system_header_size = get_system_header_size(ctx);
Michel Bardiaux27a206e2003-12-09 18:06:18306 s->last_scr = 0;
Fabrice Bellardde6d9b62001-07-22 14:18:56307 return 0;
308 fail:
309 for(i=0;i<ctx->nb_streams;i++) {
Fabrice Bellard1ea4f592002-05-18 23:11:09310 av_free(ctx->streams[i]->priv_data);
Fabrice Bellardde6d9b62001-07-22 14:18:56311 }
Fabrice Bellardde6d9b62001-07-22 14:18:56312 return -ENOMEM;
313}
314
Michel Bardiaux27a206e2003-12-09 18:06:18315static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
316{
317 put_byte(pb,
318 (id << 4) |
319 (((timestamp >> 30) & 0x07) << 1) |
320 1);
321 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
322 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
323}
324
Fabrice Bellard0dbb48d2003-12-16 11:25:30325
326/* return the exact available payload size for the next packet for
327 stream 'stream_index'. 'pts' and 'dts' are only used to know if
328 timestamps are needed in the packet header. */
329static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
330 int64_t pts, int64_t dts)
331{
332 MpegMuxContext *s = ctx->priv_data;
333 int buf_index;
334 StreamInfo *stream;
335
336 buf_index = 0;
337 if (((s->packet_number % s->pack_header_freq) == 0)) {
338 /* pack header size */
339 if (s->is_mpeg2)
340 buf_index += 14;
341 else
342 buf_index += 12;
343 if ((s->packet_number % s->system_header_freq) == 0)
344 buf_index += s->system_header_size;
345 }
346
347 /* packet header size */
348 buf_index += 6;
349 if (s->is_mpeg2)
350 buf_index += 3;
351 if (pts != AV_NOPTS_VALUE) {
Fabrice Bellarde45f1942003-12-18 13:03:37352 if (dts != pts)
Fabrice Bellard0dbb48d2003-12-16 11:25:30353 buf_index += 5 + 5;
354 else
355 buf_index += 5;
356 } else {
357 if (!s->is_mpeg2)
358 buf_index++;
359 }
360
361 stream = ctx->streams[stream_index]->priv_data;
362 if (stream->id < 0xc0) {
Fabrice Bellard044007c2003-12-16 14:00:18363 /* AC3/LPCM private data header */
Fabrice Bellard0dbb48d2003-12-16 11:25:30364 buf_index += 4;
Fabrice Bellard044007c2003-12-16 14:00:18365 if (stream->id >= 0xa0) {
366 int n;
367 buf_index += 3;
368 /* NOTE: we round the payload size to an integer number of
369 LPCM samples */
370 n = (s->packet_size - buf_index) % stream->lpcm_align;
371 if (n)
372 buf_index += (stream->lpcm_align - n);
373 }
Fabrice Bellard0dbb48d2003-12-16 11:25:30374 }
375 return s->packet_size - buf_index;
376}
377
Fabrice Bellardde6d9b62001-07-22 14:18:56378/* flush the packet on stream stream_index */
Michel Bardiaux27a206e2003-12-09 18:06:18379static void flush_packet(AVFormatContext *ctx, int stream_index,
380 int64_t pts, int64_t dts, int64_t scr)
Fabrice Bellardde6d9b62001-07-22 14:18:56381{
382 MpegMuxContext *s = ctx->priv_data;
383 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48384 uint8_t *buf_ptr;
Fabrice Bellard0dbb48d2003-12-16 11:25:30385 int size, payload_size, startcode, id, stuffing_size, i, header_len;
386 int packet_size;
Zdenek Kabelac0c1a9ed2003-02-11 16:35:48387 uint8_t buffer[128];
Juanjo92b3e122002-05-12 21:38:54388
Fabrice Bellardde6d9b62001-07-22 14:18:56389 id = stream->id;
Michel Bardiaux27a206e2003-12-09 18:06:18390
Fabrice Bellardde6d9b62001-07-22 14:18:56391#if 0
392 printf("packet ID=%2x PTS=%0.3f\n",
Michel Bardiaux27a206e2003-12-09 18:06:18393 id, pts / 90000.0);
Fabrice Bellardde6d9b62001-07-22 14:18:56394#endif
395
396 buf_ptr = buffer;
Juanjo92b3e122002-05-12 21:38:54397 if (((s->packet_number % s->pack_header_freq) == 0)) {
Fabrice Bellardde6d9b62001-07-22 14:18:56398 /* output pack and systems header if needed */
Michel Bardiaux27a206e2003-12-09 18:06:18399 size = put_pack_header(ctx, buf_ptr, scr);
Fabrice Bellardde6d9b62001-07-22 14:18:56400 buf_ptr += size;
401 if ((s->packet_number % s->system_header_freq) == 0) {
402 size = put_system_header(ctx, buf_ptr);
403 buf_ptr += size;
404 }
405 }
406 size = buf_ptr - buffer;
407 put_buffer(&ctx->pb, buffer, size);
408
409 /* packet header */
Fabrice Bellardfb7566d2002-10-15 10:22:23410 if (s->is_mpeg2) {
Michel Bardiaux27a206e2003-12-09 18:06:18411 header_len = 3;
Fabrice Bellardfb7566d2002-10-15 10:22:23412 } else {
Michel Bardiaux27a206e2003-12-09 18:06:18413 header_len = 0;
Fabrice Bellardfb7566d2002-10-15 10:22:23414 }
Michel Bardiaux27a206e2003-12-09 18:06:18415 if (pts != AV_NOPTS_VALUE) {
Fabrice Bellarde45f1942003-12-18 13:03:37416 if (dts != pts)
Michel Bardiaux27a206e2003-12-09 18:06:18417 header_len += 5 + 5;
418 else
419 header_len += 5;
420 } else {
421 if (!s->is_mpeg2)
422 header_len++;
423 }
424
Fabrice Bellard0dbb48d2003-12-16 11:25:30425 packet_size = s->packet_size - (size + 6);
426 payload_size = packet_size - header_len;
Fabrice Bellardde6d9b62001-07-22 14:18:56427 if (id < 0xc0) {
428 startcode = PRIVATE_STREAM_1;
429 payload_size -= 4;
Fabrice Bellard044007c2003-12-16 14:00:18430 if (id >= 0xa0)
431 payload_size -= 3;
Fabrice Bellardde6d9b62001-07-22 14:18:56432 } else {
433 startcode = 0x100 + id;
434 }
Fabrice Bellard0dbb48d2003-12-16 11:25:30435
Fabrice Bellardde6d9b62001-07-22 14:18:56436 stuffing_size = payload_size - stream->buffer_ptr;
437 if (stuffing_size < 0)
438 stuffing_size = 0;
Fabrice Bellardde6d9b62001-07-22 14:18:56439 put_be32(&ctx->pb, startcode);
440
Fabrice Bellard0dbb48d2003-12-16 11:25:30441 put_be16(&ctx->pb, packet_size);
Fabrice Bellardde6d9b62001-07-22 14:18:56442 /* stuffing */
443 for(i=0;i<stuffing_size;i++)
444 put_byte(&ctx->pb, 0xff);
Fabrice Bellardfb7566d2002-10-15 10:22:23445
446 if (s->is_mpeg2) {
447 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
Michel Bardiaux27a206e2003-12-09 18:06:18448
449 if (pts != AV_NOPTS_VALUE) {
Fabrice Bellarde45f1942003-12-18 13:03:37450 if (dts != pts) {
Michel Bardiaux27a206e2003-12-09 18:06:18451 put_byte(&ctx->pb, 0xc0); /* flags */
452 put_byte(&ctx->pb, header_len - 3);
453 put_timestamp(&ctx->pb, 0x03, pts);
454 put_timestamp(&ctx->pb, 0x01, dts);
455 } else {
456 put_byte(&ctx->pb, 0x80); /* flags */
457 put_byte(&ctx->pb, header_len - 3);
458 put_timestamp(&ctx->pb, 0x02, pts);
459 }
460 } else {
461 put_byte(&ctx->pb, 0x00); /* flags */
462 put_byte(&ctx->pb, header_len - 3);
463 }
464 } else {
465 if (pts != AV_NOPTS_VALUE) {
Fabrice Bellarde45f1942003-12-18 13:03:37466 if (dts != pts) {
Michel Bardiaux27a206e2003-12-09 18:06:18467 put_timestamp(&ctx->pb, 0x03, pts);
468 put_timestamp(&ctx->pb, 0x01, dts);
469 } else {
470 put_timestamp(&ctx->pb, 0x02, pts);
471 }
472 } else {
473 put_byte(&ctx->pb, 0x0f);
474 }
Fabrice Bellardfb7566d2002-10-15 10:22:23475 }
Fabrice Bellardde6d9b62001-07-22 14:18:56476
477 if (startcode == PRIVATE_STREAM_1) {
478 put_byte(&ctx->pb, id);
Fabrice Bellard044007c2003-12-16 14:00:18479 if (id >= 0xa0) {
480 /* LPCM (XXX: check nb_frames) */
481 put_byte(&ctx->pb, 7);
482 put_be16(&ctx->pb, 4); /* skip 3 header bytes */
483 put_byte(&ctx->pb, stream->lpcm_header[0]);
484 put_byte(&ctx->pb, stream->lpcm_header[1]);
485 put_byte(&ctx->pb, stream->lpcm_header[2]);
486 } else {
487 /* AC3 */
Fabrice Bellard0dbb48d2003-12-16 11:25:30488 put_byte(&ctx->pb, stream->nb_frames);
489 put_be16(&ctx->pb, stream->frame_start_offset);
Fabrice Bellardde6d9b62001-07-22 14:18:56490 }
491 }
492
493 /* output data */
494 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
495 put_flush_packet(&ctx->pb);
496
Fabrice Bellardde6d9b62001-07-22 14:18:56497 s->packet_number++;
498 stream->packet_number++;
Fabrice Bellard0dbb48d2003-12-16 11:25:30499 stream->nb_frames = 0;
500 stream->frame_start_offset = 0;
Fabrice Bellardde6d9b62001-07-22 14:18:56501}
502
Fabrice Bellarde45f1942003-12-18 13:03:37503/* XXX: move that to upper layer */
504/* XXX: we assume that there are always 'max_b_frames' between
505 reference frames. A better solution would be to use the AVFrame pts
506 field */
507static void compute_pts_dts(AVStream *st, int64_t *ppts, int64_t *pdts,
508 int64_t timestamp)
509{
510 int frame_delay;
511 int64_t pts, dts;
512
513 if (st->codec.codec_type == CODEC_TYPE_VIDEO &&
514 st->codec.max_b_frames != 0) {
515 frame_delay = (st->codec.frame_rate_base * 90000LL) /
516 st->codec.frame_rate;
517 if (timestamp == 0) {
518 /* specific case for first frame : DTS just before */
519 pts = timestamp;
520 dts = timestamp - frame_delay;
521 } else {
522 timestamp -= frame_delay;
523 if (st->codec.coded_frame->pict_type == FF_B_TYPE) {
524 /* B frames has identical pts/dts */
525 pts = timestamp;
526 dts = timestamp;
527 } else {
528 /* a reference frame has a pts equal to the dts of the
529 _next_ one */
530 dts = timestamp;
531 pts = timestamp + (st->codec.max_b_frames + 1) * frame_delay;
532 }
533 }
534#if 1
535 printf("pts=%0.3f dts=%0.3f pict_type=%c\n",
536 pts / 90000.0, dts / 90000.0,
537 av_get_pict_type_char(st->codec.coded_frame->pict_type));
538#endif
539 } else {
540 pts = timestamp;
541 dts = timestamp;
542 }
543 *ppts = pts & ((1LL << 33) - 1);
544 *pdts = dts & ((1LL << 33) - 1);
545}
546
Juanjo10bb7022002-04-07 21:44:29547static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
Fabrice Bellarde45f1942003-12-18 13:03:37548 const uint8_t *buf, int size,
549 int64_t timestamp)
Fabrice Bellardde6d9b62001-07-22 14:18:56550{
551 MpegMuxContext *s = ctx->priv_data;
552 AVStream *st = ctx->streams[stream_index];
553 StreamInfo *stream = st->priv_data;
Fabrice Bellarde45f1942003-12-18 13:03:37554 int64_t pts, dts, new_start_pts, new_start_dts;
Fabrice Bellard0dbb48d2003-12-16 11:25:30555 int len, avail_size;
Michel Bardiaux27a206e2003-12-09 18:06:18556
Fabrice Bellarde45f1942003-12-18 13:03:37557 compute_pts_dts(st, &pts, &dts, timestamp);
558
Michel Bardiaux27a206e2003-12-09 18:06:18559 /* XXX: system clock should be computed precisely, especially for
560 CBR case. The current mode gives at least something coherent */
561 if (stream_index == s->scr_stream_index)
562 s->last_scr = pts;
Juanjo10bb7022002-04-07 21:44:29563
Michel Bardiaux27a206e2003-12-09 18:06:18564#if 0
Fabrice Bellarde45f1942003-12-18 13:03:37565 printf("%d: pts=%0.3f dts=%0.3f scr=%0.3f\n",
566 stream_index,
567 pts / 90000.0,
568 dts / 90000.0,
569 s->last_scr / 90000.0);
Michel Bardiaux27a206e2003-12-09 18:06:18570#endif
571
Michel Bardiaux27a206e2003-12-09 18:06:18572 /* we assume here that pts != AV_NOPTS_VALUE */
Fabrice Bellard0dbb48d2003-12-16 11:25:30573 new_start_pts = stream->start_pts;
574 new_start_dts = stream->start_dts;
575
Michel Bardiaux27a206e2003-12-09 18:06:18576 if (stream->start_pts == AV_NOPTS_VALUE) {
Fabrice Bellard0dbb48d2003-12-16 11:25:30577 new_start_pts = pts;
578 new_start_dts = dts;
Michel Bardiaux27a206e2003-12-09 18:06:18579 }
Fabrice Bellard0dbb48d2003-12-16 11:25:30580 avail_size = get_packet_payload_size(ctx, stream_index,
581 new_start_pts,
582 new_start_dts);
583 if (stream->buffer_ptr >= avail_size) {
584 /* unlikely case: outputing the pts or dts increase the packet
585 size so that we cannot write the start of the next
586 packet. In this case, we must flush the current packet with
587 padding */
588 flush_packet(ctx, stream_index,
589 stream->start_pts, stream->start_dts, s->last_scr);
590 stream->buffer_ptr = 0;
591 }
592 stream->start_pts = new_start_pts;
593 stream->start_dts = new_start_dts;
594 stream->nb_frames++;
595 if (stream->frame_start_offset == 0)
596 stream->frame_start_offset = stream->buffer_ptr;
Fabrice Bellardde6d9b62001-07-22 14:18:56597 while (size > 0) {
Fabrice Bellard0dbb48d2003-12-16 11:25:30598 avail_size = get_packet_payload_size(ctx, stream_index,
599 stream->start_pts,
600 stream->start_dts);
601 len = avail_size - stream->buffer_ptr;
Fabrice Bellardde6d9b62001-07-22 14:18:56602 if (len > size)
603 len = size;
604 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
605 stream->buffer_ptr += len;
606 buf += len;
607 size -= len;
Fabrice Bellard0dbb48d2003-12-16 11:25:30608 if (stream->buffer_ptr >= avail_size) {
609 /* if packet full, we send it now */
Michel Bardiaux27a206e2003-12-09 18:06:18610 flush_packet(ctx, stream_index,
611 stream->start_pts, stream->start_dts, s->last_scr);
Fabrice Bellard0dbb48d2003-12-16 11:25:30612 stream->buffer_ptr = 0;
Michel Bardiaux27a206e2003-12-09 18:06:18613 /* Make sure only the FIRST pes packet for this frame has
614 a timestamp */
615 stream->start_pts = AV_NOPTS_VALUE;
616 stream->start_dts = AV_NOPTS_VALUE;
Fabrice Bellardde6d9b62001-07-22 14:18:56617 }
618 }
Fabrice Bellard0dbb48d2003-12-16 11:25:30619
Fabrice Bellardde6d9b62001-07-22 14:18:56620 return 0;
621}
622
623static int mpeg_mux_end(AVFormatContext *ctx)
624{
Michel Bardiaux27a206e2003-12-09 18:06:18625 MpegMuxContext *s = ctx->priv_data;
Fabrice Bellardde6d9b62001-07-22 14:18:56626 StreamInfo *stream;
627 int i;
628
629 /* flush each packet */
630 for(i=0;i<ctx->nb_streams;i++) {
631 stream = ctx->streams[i]->priv_data;
Fabrice Bellard0dbb48d2003-12-16 11:25:30632 if (stream->buffer_ptr > 0) {
633 /* NOTE: we can always write the remaining data as it was
634 tested before in mpeg_mux_write_packet() */
635 flush_packet(ctx, i, stream->start_pts, stream->start_dts,
636 s->last_scr);
Juanjo92b3e122002-05-12 21:38:54637 }
Fabrice Bellardde6d9b62001-07-22 14:18:56638 }
639
Fabrice Bellardfa0f62c2003-09-10 22:44:30640 /* End header according to MPEG1 systems standard. We do not write
641 it as it is usually not needed by decoders and because it
642 complicates MPEG stream concatenation. */
Juanjo92b3e122002-05-12 21:38:54643 //put_be32(&ctx->pb, ISO_11172_END_CODE);
644 //put_flush_packet(&ctx->pb);
Michael Niedermayer9d90c372003-09-09 19:32:52645
646 for(i=0;i<ctx->nb_streams;i++)
647 av_freep(&ctx->streams[i]->priv_data);
648
Fabrice Bellardde6d9b62001-07-22 14:18:56649 return 0;
650}
Mike Melanson764ef402003-10-14 04:15:53651#endif //CONFIG_ENCODERS
Fabrice Bellardde6d9b62001-07-22 14:18:56652
653/*********************************************/
654/* demux code */
655
656#define MAX_SYNC_SIZE 100000
657
Fabrice Bellarddb7f1f92002-05-20 16:29:40658static int mpegps_probe(AVProbeData *p)
659{
Isaac Richardsec23a472003-07-10 09:04:04660 int code, c, i;
Fabrice Bellarddb7f1f92002-05-20 16:29:40661
Isaac Richardsec23a472003-07-10 09:04:04662 code = 0xff;
Fabrice Bellarddb7f1f92002-05-20 16:29:40663 /* we search the first start code. If it is a packet start code,
664 then we decide it is mpeg ps. We do not send highest value to
665 give a chance to mpegts */
Fabrice Bellardfa7773212003-02-02 20:04:03666 /* NOTE: the search range was restricted to avoid too many false
667 detections */
668
669 if (p->buf_size < 6)
670 return 0;
Isaac Richardsec23a472003-07-10 09:04:04671
672 for (i = 0; i < 20; i++) {
673 c = p->buf[i];
674 code = (code << 8) | c;
675 if ((code & 0xffffff00) == 0x100) {
676 if (code == PACK_START_CODE ||
677 code == SYSTEM_HEADER_START_CODE ||
678 (code >= 0x1e0 && code <= 0x1ef) ||
679 (code >= 0x1c0 && code <= 0x1df) ||
680 code == PRIVATE_STREAM_2 ||
681 code == PROGRAM_STREAM_MAP ||
682 code == PRIVATE_STREAM_1 ||
683 code == PADDING_STREAM)
Michael Niedermayer149f7c02003-09-01 18:30:02684 return AVPROBE_SCORE_MAX - 2;
Isaac Richardsec23a472003-07-10 09:04:04685 else
686 return 0;
687 }
Fabrice Bellarddb7f1f92002-05-20 16:29:40688 }
689 return 0;
690}
691
692
Fabrice Bellardde6d9b62001-07-22 14:18:56693typedef struct MpegDemuxContext {
694 int header_state;
Fabrice Bellardde6d9b62001-07-22 14:18:56695} MpegDemuxContext;
696
Fabrice Bellard27f388a2003-11-10 18:47:52697static int mpegps_read_header(AVFormatContext *s,
698 AVFormatParameters *ap)
699{
700 MpegDemuxContext *m = s->priv_data;
701 m->header_state = 0xff;
702 s->ctx_flags |= AVFMTCTX_NOHEADER;
703
704 /* no need to do more */
705 return 0;
706}
707
708static int64_t get_pts(ByteIOContext *pb, int c)
709{
710 int64_t pts;
711 int val;
712
713 if (c < 0)
714 c = get_byte(pb);
715 pts = (int64_t)((c >> 1) & 0x07) << 30;
716 val = get_be16(pb);
717 pts |= (int64_t)(val >> 1) << 15;
718 val = get_be16(pb);
719 pts |= (int64_t)(val >> 1);
720 return pts;
721}
722
723static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
724 uint32_t *header_state)
Fabrice Bellardde6d9b62001-07-22 14:18:56725{
726 unsigned int state, v;
727 int val, n;
728
729 state = *header_state;
730 n = *size_ptr;
731 while (n > 0) {
732 if (url_feof(pb))
733 break;
734 v = get_byte(pb);
735 n--;
736 if (state == 0x000001) {
737 state = ((state << 8) | v) & 0xffffff;
738 val = state;
739 goto found;
740 }
741 state = ((state << 8) | v) & 0xffffff;
742 }
743 val = -1;
744 found:
745 *header_state = state;
746 *size_ptr = n;
747 return val;
748}
749
Fabrice Bellard27f388a2003-11-10 18:47:52750/* XXX: optimize */
751static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
Juanjo001e3f52002-03-17 17:44:45752{
Fabrice Bellard27f388a2003-11-10 18:47:52753 int64_t pos, pos_start;
754 int max_size, start_code;
Fabrice Bellardda24c5e2003-10-29 14:20:56755
Fabrice Bellard27f388a2003-11-10 18:47:52756 max_size = *size_ptr;
757 pos_start = url_ftell(pb);
758
759 /* in order to go faster, we fill the buffer */
760 pos = pos_start - 16386;
761 if (pos < 0)
762 pos = 0;
763 url_fseek(pb, pos, SEEK_SET);
764 get_byte(pb);
765
766 pos = pos_start;
767 for(;;) {
768 pos--;
769 if (pos < 0 || (pos_start - pos) >= max_size) {
770 start_code = -1;
771 goto the_end;
772 }
773 url_fseek(pb, pos, SEEK_SET);
774 start_code = get_be32(pb);
775 if ((start_code & 0xffffff00) == 0x100)
776 break;
777 }
778 the_end:
779 *size_ptr = pos_start - pos;
780 return start_code;
Fabrice Bellardde6d9b62001-07-22 14:18:56781}
782
Fabrice Bellard27f388a2003-11-10 18:47:52783/* read the next (or previous) PES header. Return its position in ppos
784 (if not NULL), and its start code, pts and dts.
785 */
786static int mpegps_read_pes_header(AVFormatContext *s,
787 int64_t *ppos, int *pstart_code,
788 int64_t *ppts, int64_t *pdts, int find_next)
Fabrice Bellardde6d9b62001-07-22 14:18:56789{
790 MpegDemuxContext *m = s->priv_data;
Fabrice Bellard27f388a2003-11-10 18:47:52791 int len, size, startcode, c, flags, header_len;
792 int64_t pts, dts, last_pos;
Fabrice Bellardde6d9b62001-07-22 14:18:56793
Fabrice Bellard27f388a2003-11-10 18:47:52794 last_pos = -1;
Fabrice Bellardde6d9b62001-07-22 14:18:56795 redo:
Fabrice Bellard27f388a2003-11-10 18:47:52796 if (find_next) {
797 /* next start code (should be immediately after) */
798 m->header_state = 0xff;
799 size = MAX_SYNC_SIZE;
800 startcode = find_next_start_code(&s->pb, &size, &m->header_state);
801 } else {
802 if (last_pos >= 0)
803 url_fseek(&s->pb, last_pos, SEEK_SET);
804 size = MAX_SYNC_SIZE;
805 startcode = find_prev_start_code(&s->pb, &size);
806 last_pos = url_ftell(&s->pb) - 4;
807 }
Juanjo001e3f52002-03-17 17:44:45808 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
Fabrice Bellardde6d9b62001-07-22 14:18:56809 if (startcode < 0)
810 return -EIO;
811 if (startcode == PACK_START_CODE)
812 goto redo;
813 if (startcode == SYSTEM_HEADER_START_CODE)
814 goto redo;
815 if (startcode == PADDING_STREAM ||
816 startcode == PRIVATE_STREAM_2) {
817 /* skip them */
818 len = get_be16(&s->pb);
819 url_fskip(&s->pb, len);
820 goto redo;
821 }
822 /* find matching stream */
823 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
824 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
825 (startcode == 0x1bd)))
826 goto redo;
Fabrice Bellard27f388a2003-11-10 18:47:52827 if (ppos) {
828 *ppos = url_ftell(&s->pb) - 4;
829 }
Fabrice Bellardde6d9b62001-07-22 14:18:56830 len = get_be16(&s->pb);
Fabrice Bellardb2cac182002-10-21 15:57:21831 pts = AV_NOPTS_VALUE;
832 dts = AV_NOPTS_VALUE;
Fabrice Bellardde6d9b62001-07-22 14:18:56833 /* stuffing */
834 for(;;) {
Fabrice Bellard27f388a2003-11-10 18:47:52835 if (len < 1)
836 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56837 c = get_byte(&s->pb);
838 len--;
839 /* XXX: for mpeg1, should test only bit 7 */
840 if (c != 0xff)
841 break;
842 }
843 if ((c & 0xc0) == 0x40) {
844 /* buffer scale & size */
Fabrice Bellard27f388a2003-11-10 18:47:52845 if (len < 2)
846 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56847 get_byte(&s->pb);
848 c = get_byte(&s->pb);
849 len -= 2;
850 }
851 if ((c & 0xf0) == 0x20) {
Fabrice Bellard27f388a2003-11-10 18:47:52852 if (len < 4)
853 goto redo;
854 dts = pts = get_pts(&s->pb, c);
Fabrice Bellardde6d9b62001-07-22 14:18:56855 len -= 4;
Fabrice Bellardde6d9b62001-07-22 14:18:56856 } else if ((c & 0xf0) == 0x30) {
Fabrice Bellard27f388a2003-11-10 18:47:52857 if (len < 9)
858 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56859 pts = get_pts(&s->pb, c);
860 dts = get_pts(&s->pb, -1);
861 len -= 9;
862 } else if ((c & 0xc0) == 0x80) {
863 /* mpeg 2 PES */
864 if ((c & 0x30) != 0) {
Fabrice Bellard27f388a2003-11-10 18:47:52865 /* Encrypted multiplex not handled */
866 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56867 }
868 flags = get_byte(&s->pb);
869 header_len = get_byte(&s->pb);
870 len -= 2;
871 if (header_len > len)
872 goto redo;
Fabrice Bellard1e5c6672002-10-04 15:46:59873 if ((flags & 0xc0) == 0x80) {
Fabrice Bellard27f388a2003-11-10 18:47:52874 dts = pts = get_pts(&s->pb, -1);
875 if (header_len < 5)
876 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56877 header_len -= 5;
878 len -= 5;
879 } if ((flags & 0xc0) == 0xc0) {
880 pts = get_pts(&s->pb, -1);
881 dts = get_pts(&s->pb, -1);
Fabrice Bellard27f388a2003-11-10 18:47:52882 if (header_len < 10)
883 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56884 header_len -= 10;
885 len -= 10;
886 }
887 len -= header_len;
888 while (header_len > 0) {
889 get_byte(&s->pb);
890 header_len--;
891 }
892 }
893 if (startcode == 0x1bd) {
Fabrice Bellard27f388a2003-11-10 18:47:52894 if (len < 1)
895 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56896 startcode = get_byte(&s->pb);
897 len--;
898 if (startcode >= 0x80 && startcode <= 0xbf) {
899 /* audio: skip header */
Fabrice Bellard27f388a2003-11-10 18:47:52900 if (len < 3)
901 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56902 get_byte(&s->pb);
903 get_byte(&s->pb);
904 get_byte(&s->pb);
905 len -= 3;
906 }
907 }
Michael Niedermayerb7549782004-01-13 22:02:49908 if(dts != AV_NOPTS_VALUE && ppos){
909 int i;
910 for(i=0; i<s->nb_streams; i++){
911 if(startcode == s->streams[i]->id) {
Michael Niedermayer3e9245a2004-01-17 18:06:52912 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0 /* FIXME keyframe? */);
Michael Niedermayerb7549782004-01-13 22:02:49913 }
914 }
915 }
916
Fabrice Bellard27f388a2003-11-10 18:47:52917 *pstart_code = startcode;
918 *ppts = pts;
919 *pdts = dts;
920 return len;
921}
922
923static int mpegps_read_packet(AVFormatContext *s,
924 AVPacket *pkt)
925{
926 AVStream *st;
927 int len, startcode, i, type, codec_id;
Michael Niedermayerb7549782004-01-13 22:02:49928 int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
Fabrice Bellard27f388a2003-11-10 18:47:52929
930 redo:
Michael Niedermayerb7549782004-01-13 22:02:49931 len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts, 1);
Fabrice Bellard27f388a2003-11-10 18:47:52932 if (len < 0)
933 return len;
Michel Bardiaux27a206e2003-12-09 18:06:18934
Fabrice Bellardde6d9b62001-07-22 14:18:56935 /* now find stream */
936 for(i=0;i<s->nb_streams;i++) {
937 st = s->streams[i];
938 if (st->id == startcode)
939 goto found;
940 }
Fabrice Bellarddb7f1f92002-05-20 16:29:40941 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
942 type = CODEC_TYPE_VIDEO;
Fabrice Bellard0dbb48d2003-12-16 11:25:30943 codec_id = CODEC_ID_MPEG2VIDEO;
Fabrice Bellarddb7f1f92002-05-20 16:29:40944 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
945 type = CODEC_TYPE_AUDIO;
946 codec_id = CODEC_ID_MP2;
947 } else if (startcode >= 0x80 && startcode <= 0x9f) {
948 type = CODEC_TYPE_AUDIO;
949 codec_id = CODEC_ID_AC3;
Fabrice Bellard9ec05e32003-01-31 17:04:46950 } else if (startcode >= 0xa0 && startcode <= 0xbf) {
951 type = CODEC_TYPE_AUDIO;
952 codec_id = CODEC_ID_PCM_S16BE;
Fabrice Bellarddb7f1f92002-05-20 16:29:40953 } else {
954 skip:
955 /* skip packet */
956 url_fskip(&s->pb, len);
957 goto redo;
958 }
Fabrice Bellard1e5c6672002-10-04 15:46:59959 /* no stream found: add a new stream */
960 st = av_new_stream(s, startcode);
961 if (!st)
962 goto skip;
Fabrice Bellarddb7f1f92002-05-20 16:29:40963 st->codec.codec_type = type;
964 st->codec.codec_id = codec_id;
Fabrice Bellard27f388a2003-11-10 18:47:52965 if (codec_id != CODEC_ID_PCM_S16BE)
966 st->need_parsing = 1;
Fabrice Bellardde6d9b62001-07-22 14:18:56967 found:
Fabrice Bellard9ec05e32003-01-31 17:04:46968 if (startcode >= 0xa0 && startcode <= 0xbf) {
969 int b1, freq;
Fabrice Bellard9ec05e32003-01-31 17:04:46970
971 /* for LPCM, we just skip the header and consider it is raw
972 audio data */
973 if (len <= 3)
974 goto skip;
975 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
976 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
977 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
978 len -= 3;
979 freq = (b1 >> 4) & 3;
980 st->codec.sample_rate = lpcm_freq_tab[freq];
981 st->codec.channels = 1 + (b1 & 7);
982 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
983 }
Fabrice Bellardde6d9b62001-07-22 14:18:56984 av_new_packet(pkt, len);
Fabrice Bellardde6d9b62001-07-22 14:18:56985 get_buffer(&s->pb, pkt->data, pkt->size);
986 pkt->pts = pts;
Fabrice Bellard27f388a2003-11-10 18:47:52987 pkt->dts = dts;
Fabrice Bellarddb7f1f92002-05-20 16:29:40988 pkt->stream_index = st->index;
Michel Bardiaux27a206e2003-12-09 18:06:18989#if 0
990 printf("%d: pts=%0.3f dts=%0.3f\n",
991 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0);
992#endif
Fabrice Bellardde6d9b62001-07-22 14:18:56993 return 0;
994}
995
Fabrice Bellarddb7f1f92002-05-20 16:29:40996static int mpegps_read_close(AVFormatContext *s)
Juanjo001e3f52002-03-17 17:44:45997{
Fabrice Bellardde6d9b62001-07-22 14:18:56998 return 0;
999}
1000
Fabrice Bellard27f388a2003-11-10 18:47:521001static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
1002 int64_t *ppos, int find_next)
1003{
1004 int len, startcode;
1005 int64_t pos, pts, dts;
1006
1007 pos = *ppos;
1008#ifdef DEBUG_SEEK
1009 printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
1010#endif
1011 url_fseek(&s->pb, pos, SEEK_SET);
1012 for(;;) {
1013 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts, find_next);
1014 if (len < 0) {
1015#ifdef DEBUG_SEEK
1016 printf("none (ret=%d)\n", len);
1017#endif
1018 return AV_NOPTS_VALUE;
1019 }
1020 if (startcode == s->streams[stream_index]->id &&
1021 dts != AV_NOPTS_VALUE) {
1022 break;
1023 }
1024 if (find_next) {
1025 url_fskip(&s->pb, len);
1026 } else {
1027 url_fseek(&s->pb, pos, SEEK_SET);
1028 }
1029 }
1030#ifdef DEBUG_SEEK
1031 printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
1032#endif
1033 *ppos = pos;
1034 return dts;
1035}
1036
Fabrice Bellard27f388a2003-11-10 18:47:521037static int mpegps_read_seek(AVFormatContext *s,
1038 int stream_index, int64_t timestamp)
1039{
Michael Niedermayer0888ac42004-01-17 20:26:441040 int64_t pos_min, pos_max, pos, pos_limit;
Fabrice Bellard27f388a2003-11-10 18:47:521041 int64_t dts_min, dts_max, dts;
Michael Niedermayer0888ac42004-01-17 20:26:441042 int index, no_change;
Michael Niedermayerb7549782004-01-13 22:02:491043 AVStream *st;
Fabrice Bellard27f388a2003-11-10 18:47:521044
1045 timestamp = (timestamp * 90000) / AV_TIME_BASE;
1046
1047#ifdef DEBUG_SEEK
1048 printf("read_seek: %d %0.3f\n", stream_index, timestamp / 90000.0);
1049#endif
1050
1051 /* XXX: find stream_index by looking at the first PES packet found */
1052 if (stream_index < 0) {
Michael Niedermayerb7549782004-01-13 22:02:491053 stream_index = av_find_default_stream_index(s);
Fabrice Bellard27f388a2003-11-10 18:47:521054 if (stream_index < 0)
1055 return -1;
1056 }
Michael Niedermayerb7549782004-01-13 22:02:491057
1058 dts_max=
1059 dts_min= AV_NOPTS_VALUE;
Michael Niedermayer0888ac42004-01-17 20:26:441060 pos_limit= -1; //gcc falsely says it may be uninitalized
Michael Niedermayerb7549782004-01-13 22:02:491061
1062 st= s->streams[stream_index];
1063 if(st->index_entries){
1064 AVIndexEntry *e;
1065
1066 index= av_index_search_timestamp(st, timestamp);
1067 e= &st->index_entries[index];
1068 if(e->timestamp <= timestamp){
1069 pos_min= e->pos;
1070 dts_min= e->timestamp;
1071#ifdef DEBUG_SEEK
1072 printf("unsing cached pos_min=0x%llx dts_min=%0.3f\n",
1073 pos_min,dts_min / 90000.0);
1074#endif
1075 }else{
1076 assert(index==0);
1077 }
1078 index++;
1079 if(index < st->nb_index_entries){
1080 e= &st->index_entries[index];
1081 assert(e->timestamp >= timestamp);
1082 pos_max= e->pos;
1083 dts_max= e->timestamp;
Michael Niedermayer0888ac42004-01-17 20:26:441084 pos_limit= pos_max - e->min_distance;
Michael Niedermayerb7549782004-01-13 22:02:491085#ifdef DEBUG_SEEK
1086 printf("unsing cached pos_max=0x%llx dts_max=%0.3f\n",
1087 pos_max,dts_max / 90000.0);
1088#endif
1089 }
Fabrice Bellard27f388a2003-11-10 18:47:521090 }
Michael Niedermayerb7549782004-01-13 22:02:491091
1092 if(dts_min == AV_NOPTS_VALUE){
1093 pos_min = 0;
1094 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
1095 if (dts_min == AV_NOPTS_VALUE) {
1096 /* we can reach this case only if no PTS are present in
1097 the whole stream */
1098 return -1;
1099 }
1100 }
1101 if(dts_max == AV_NOPTS_VALUE){
1102 pos_max = url_filesize(url_fileno(&s->pb)) - 1;
1103 dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
Michael Niedermayer0888ac42004-01-17 20:26:441104 pos_limit= pos_max;
Michael Niedermayerb7549782004-01-13 22:02:491105 }
Michael Niedermayer0888ac42004-01-17 20:26:441106
1107 no_change=0;
1108 while (pos_min < pos_limit) {
Fabrice Bellard27f388a2003-11-10 18:47:521109#ifdef DEBUG_SEEK
1110 printf("pos_min=0x%llx pos_max=0x%llx dts_min=%0.3f dts_max=%0.3f\n",
1111 pos_min, pos_max,
1112 dts_min / 90000.0, dts_max / 90000.0);
1113#endif
Michael Niedermayer0888ac42004-01-17 20:26:441114 int64_t start_pos;
1115 assert(pos_limit <= pos_max);
1116
1117 if(no_change==0){
1118 int64_t approximate_keyframe_distance= pos_max - pos_limit;
1119 // interpolate position (better than dichotomy)
1120 pos = (int64_t)((double)(pos_max - pos_min) *
Fabrice Bellard27f388a2003-11-10 18:47:521121 (double)(timestamp - dts_min) /
Michael Niedermayer0888ac42004-01-17 20:26:441122 (double)(dts_max - dts_min)) + pos_min - approximate_keyframe_distance;
1123 }else if(no_change==1){
1124 // bisection, if interpolation failed to change min or max pos last time
1125 pos = (pos_min + pos_limit)>>1;
1126 }else{
1127 // linear search if bisection failed, can only happen if there are very few or no keframes between min/max
1128 pos=pos_min;
Fabrice Bellard27f388a2003-11-10 18:47:521129 }
Michael Niedermayer0888ac42004-01-17 20:26:441130 if(pos <= pos_min)
1131 pos= pos_min + 1;
1132 else if(pos > pos_limit)
1133 pos= pos_limit;
1134 start_pos= pos;
1135
1136 // read the next timestamp
1137 dts = mpegps_read_dts(s, stream_index, &pos, 1);
1138 if(pos == pos_max)
1139 no_change++;
1140 else
1141 no_change=0;
Fabrice Bellard27f388a2003-11-10 18:47:521142#ifdef DEBUG_SEEK
Michael Niedermayer0888ac42004-01-17 20:26:441143printf("%Ld %Ld %Ld / %Ld %Ld %Ld target:%Ld limit:%Ld start:%Ld noc:%d\n", pos_min, pos, pos_max, dts_min, dts, dts_max, timestamp, pos_limit, start_pos, no_change);
Fabrice Bellard27f388a2003-11-10 18:47:521144#endif
Michael Niedermayer0888ac42004-01-17 20:26:441145 assert(dts != AV_NOPTS_VALUE);
1146 if (timestamp < dts) {
1147 pos_limit = start_pos - 1;
Fabrice Bellard27f388a2003-11-10 18:47:521148 pos_max = pos;
Michael Niedermayer0888ac42004-01-17 20:26:441149 dts_max = dts;
Fabrice Bellard27f388a2003-11-10 18:47:521150 } else {
Michael Niedermayer0888ac42004-01-17 20:26:441151 pos_min = pos;
1152 dts_min = dts;
1153 /* check if we are lucky */
1154 if (timestamp == dts)
1155 break;
Fabrice Bellard27f388a2003-11-10 18:47:521156 }
1157 }
Michael Niedermayer0888ac42004-01-17 20:26:441158
Fabrice Bellard27f388a2003-11-10 18:47:521159 pos = pos_min;
Fabrice Bellard27f388a2003-11-10 18:47:521160#ifdef DEBUG_SEEK
1161 pos_min = pos;
1162 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
1163 pos_min++;
1164 dts_max = mpegps_read_dts(s, stream_index, &pos_min, 1);
1165 printf("pos=0x%llx %0.3f<=%0.3f<=%0.3f\n",
1166 pos, dts_min / 90000.0, timestamp / 90000.0, dts_max / 90000.0);
1167#endif
1168 /* do the seek */
1169 url_fseek(&s->pb, pos, SEEK_SET);
1170 return 0;
1171}
1172
Mike Melanson764ef402003-10-14 04:15:531173#ifdef CONFIG_ENCODERS
Fabrice Bellardfb7566d2002-10-15 10:22:231174static AVOutputFormat mpeg1system_mux = {
Fabrice Bellardde6d9b62001-07-22 14:18:561175 "mpeg",
Fabrice Bellardfb7566d2002-10-15 10:22:231176 "MPEG1 System format",
Ryutaroh Matsumotoc6c11cb2002-12-20 23:10:581177 "video/mpeg",
Fabrice Bellardfb7566d2002-10-15 10:22:231178 "mpg,mpeg",
1179 sizeof(MpegMuxContext),
1180 CODEC_ID_MP2,
1181 CODEC_ID_MPEG1VIDEO,
1182 mpeg_mux_init,
1183 mpeg_mux_write_packet,
1184 mpeg_mux_end,
1185};
1186
1187static AVOutputFormat mpeg1vcd_mux = {
1188 "vcd",
1189 "MPEG1 System format (VCD)",
Ryutaroh Matsumotoc6c11cb2002-12-20 23:10:581190 "video/mpeg",
Fabrice Bellardfb7566d2002-10-15 10:22:231191 NULL,
1192 sizeof(MpegMuxContext),
1193 CODEC_ID_MP2,
1194 CODEC_ID_MPEG1VIDEO,
1195 mpeg_mux_init,
1196 mpeg_mux_write_packet,
1197 mpeg_mux_end,
1198};
1199
1200static AVOutputFormat mpeg2vob_mux = {
1201 "vob",
1202 "MPEG2 PS format (VOB)",
Ryutaroh Matsumotoc6c11cb2002-12-20 23:10:581203 "video/mpeg",
Fabrice Bellardfb7566d2002-10-15 10:22:231204 "vob",
Fabrice Bellarddb7f1f92002-05-20 16:29:401205 sizeof(MpegMuxContext),
Fabrice Bellardde6d9b62001-07-22 14:18:561206 CODEC_ID_MP2,
Fabrice Bellard0dbb48d2003-12-16 11:25:301207 CODEC_ID_MPEG2VIDEO,
Fabrice Bellardde6d9b62001-07-22 14:18:561208 mpeg_mux_init,
1209 mpeg_mux_write_packet,
1210 mpeg_mux_end,
Fabrice Bellardde6d9b62001-07-22 14:18:561211};
Mike Melanson764ef402003-10-14 04:15:531212#endif //CONFIG_ENCODERS
Fabrice Bellarddb7f1f92002-05-20 16:29:401213
Fabrice Bellard32f38cb2003-08-08 17:54:051214AVInputFormat mpegps_demux = {
Fabrice Bellarddb7f1f92002-05-20 16:29:401215 "mpeg",
1216 "MPEG PS format",
1217 sizeof(MpegDemuxContext),
1218 mpegps_probe,
1219 mpegps_read_header,
1220 mpegps_read_packet,
1221 mpegps_read_close,
Fabrice Bellard27f388a2003-11-10 18:47:521222 mpegps_read_seek,
Fabrice Bellarddb7f1f92002-05-20 16:29:401223};
1224
1225int mpegps_init(void)
1226{
Mike Melanson764ef402003-10-14 04:15:531227#ifdef CONFIG_ENCODERS
Fabrice Bellardfb7566d2002-10-15 10:22:231228 av_register_output_format(&mpeg1system_mux);
1229 av_register_output_format(&mpeg1vcd_mux);
1230 av_register_output_format(&mpeg2vob_mux);
Mike Melanson764ef402003-10-14 04:15:531231#endif //CONFIG_ENCODERS
Fabrice Bellarddb7f1f92002-05-20 16:29:401232 av_register_input_format(&mpegps_demux);
1233 return 0;
1234}