Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 1 | /* |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 2 | * MPEG1 mux/demux |
Fabrice Bellard | 19720f1 | 2002-05-25 22:34:32 | [diff] [blame] | 3 | * Copyright (c) 2000, 2001, 2002 Fabrice Bellard. |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 4 | * |
Fabrice Bellard | 19720f1 | 2002-05-25 22:34:32 | [diff] [blame] | 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 9 | * |
Fabrice Bellard | 19720f1 | 2002-05-25 22:34:32 | [diff] [blame] | 10 | * This library is distributed in the hope that it will be useful, |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Fabrice Bellard | 19720f1 | 2002-05-25 22:34:32 | [diff] [blame] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 14 | * |
Fabrice Bellard | 19720f1 | 2002-05-25 22:34:32 | [diff] [blame] | 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 18 | */ |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 19 | #include "avformat.h" |
Juanjo | af46942 | 2002-03-20 11:16:07 | [diff] [blame] | 20 | #include "tick.h" |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 21 | |
| 22 | #define MAX_PAYLOAD_SIZE 4096 |
| 23 | #define NB_STREAMS 2 |
| 24 | |
| 25 | typedef struct { |
| 26 | UINT8 buffer[MAX_PAYLOAD_SIZE]; |
| 27 | int buffer_ptr; |
| 28 | UINT8 id; |
| 29 | int max_buffer_size; /* in bytes */ |
| 30 | int packet_number; |
Juanjo | af46942 | 2002-03-20 11:16:07 | [diff] [blame] | 31 | INT64 pts; |
| 32 | Ticker pts_ticker; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 33 | INT64 start_pts; |
| 34 | } StreamInfo; |
| 35 | |
| 36 | typedef struct { |
| 37 | int packet_size; /* required packet size */ |
| 38 | int packet_data_max_size; /* maximum data size inside a packet */ |
| 39 | int packet_number; |
| 40 | int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */ |
| 41 | int system_header_freq; |
| 42 | int mux_rate; /* bitrate in units of 50 bytes/s */ |
| 43 | /* stream info */ |
| 44 | int audio_bound; |
| 45 | int video_bound; |
| 46 | } MpegMuxContext; |
| 47 | |
| 48 | #define PACK_START_CODE ((unsigned int)0x000001ba) |
| 49 | #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb) |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 50 | #define SEQUENCE_END_CODE ((unsigned int)0x000001b7) |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 51 | #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00) |
| 52 | #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100) |
| 53 | #define ISO_11172_END_CODE ((unsigned int)0x000001b9) |
| 54 | |
| 55 | /* mpeg2 */ |
| 56 | #define PROGRAM_STREAM_MAP 0x1bc |
| 57 | #define PRIVATE_STREAM_1 0x1bd |
| 58 | #define PADDING_STREAM 0x1be |
| 59 | #define PRIVATE_STREAM_2 0x1bf |
| 60 | |
| 61 | |
| 62 | #define AUDIO_ID 0xc0 |
| 63 | #define VIDEO_ID 0xe0 |
| 64 | |
| 65 | static int put_pack_header(AVFormatContext *ctx, |
Fabrice Bellard | 8be1c65 | 2001-08-13 21:37:10 | [diff] [blame] | 66 | UINT8 *buf, INT64 timestamp) |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 67 | { |
| 68 | MpegMuxContext *s = ctx->priv_data; |
| 69 | PutBitContext pb; |
| 70 | |
| 71 | init_put_bits(&pb, buf, 128, NULL, NULL); |
| 72 | |
| 73 | put_bits(&pb, 32, PACK_START_CODE); |
| 74 | put_bits(&pb, 4, 0x2); |
Fabrice Bellard | 8be1c65 | 2001-08-13 21:37:10 | [diff] [blame] | 75 | put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07)); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 76 | put_bits(&pb, 1, 1); |
Fabrice Bellard | 8be1c65 | 2001-08-13 21:37:10 | [diff] [blame] | 77 | put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff)); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 78 | put_bits(&pb, 1, 1); |
Fabrice Bellard | 8be1c65 | 2001-08-13 21:37:10 | [diff] [blame] | 79 | put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff)); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 80 | put_bits(&pb, 1, 1); |
| 81 | put_bits(&pb, 1, 1); |
| 82 | put_bits(&pb, 22, s->mux_rate); |
| 83 | put_bits(&pb, 1, 1); |
| 84 | |
| 85 | flush_put_bits(&pb); |
Michael Niedermayer | 1759247 | 2002-02-12 15:43:16 | [diff] [blame] | 86 | return pbBufPtr(&pb) - pb.buf; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int put_system_header(AVFormatContext *ctx, UINT8 *buf) |
| 90 | { |
| 91 | MpegMuxContext *s = ctx->priv_data; |
| 92 | int size, rate_bound, i, private_stream_coded, id; |
| 93 | PutBitContext pb; |
| 94 | |
| 95 | init_put_bits(&pb, buf, 128, NULL, NULL); |
| 96 | |
| 97 | put_bits(&pb, 32, SYSTEM_HEADER_START_CODE); |
| 98 | put_bits(&pb, 16, 0); |
| 99 | put_bits(&pb, 1, 1); |
| 100 | |
| 101 | rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */ |
| 102 | put_bits(&pb, 22, rate_bound); |
| 103 | put_bits(&pb, 1, 1); /* marker */ |
| 104 | put_bits(&pb, 6, s->audio_bound); |
| 105 | |
| 106 | put_bits(&pb, 1, 1); /* variable bitrate */ |
| 107 | put_bits(&pb, 1, 1); /* non constrainted bit stream */ |
| 108 | |
| 109 | put_bits(&pb, 1, 0); /* audio locked */ |
| 110 | put_bits(&pb, 1, 0); /* video locked */ |
| 111 | put_bits(&pb, 1, 1); /* marker */ |
| 112 | |
| 113 | put_bits(&pb, 5, s->video_bound); |
| 114 | put_bits(&pb, 8, 0xff); /* reserved byte */ |
| 115 | |
| 116 | /* audio stream info */ |
| 117 | private_stream_coded = 0; |
| 118 | for(i=0;i<ctx->nb_streams;i++) { |
| 119 | StreamInfo *stream = ctx->streams[i]->priv_data; |
| 120 | id = stream->id; |
| 121 | if (id < 0xc0) { |
| 122 | /* special case for private streams (AC3 use that) */ |
| 123 | if (private_stream_coded) |
| 124 | continue; |
| 125 | private_stream_coded = 1; |
| 126 | id = 0xbd; |
| 127 | } |
| 128 | put_bits(&pb, 8, id); /* stream ID */ |
| 129 | put_bits(&pb, 2, 3); |
| 130 | if (id < 0xe0) { |
| 131 | /* audio */ |
| 132 | put_bits(&pb, 1, 0); |
| 133 | put_bits(&pb, 13, stream->max_buffer_size / 128); |
| 134 | } else { |
| 135 | /* video */ |
| 136 | put_bits(&pb, 1, 1); |
| 137 | put_bits(&pb, 13, stream->max_buffer_size / 1024); |
| 138 | } |
| 139 | } |
| 140 | flush_put_bits(&pb); |
Michael Niedermayer | 1759247 | 2002-02-12 15:43:16 | [diff] [blame] | 141 | size = pbBufPtr(&pb) - pb.buf; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 142 | /* patch packet size */ |
| 143 | buf[4] = (size - 6) >> 8; |
| 144 | buf[5] = (size - 6) & 0xff; |
| 145 | |
| 146 | return size; |
| 147 | } |
| 148 | |
| 149 | static int mpeg_mux_init(AVFormatContext *ctx) |
| 150 | { |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 151 | MpegMuxContext *s = ctx->priv_data; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 152 | int bitrate, i, mpa_id, mpv_id, ac3_id; |
| 153 | AVStream *st; |
| 154 | StreamInfo *stream; |
| 155 | |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 156 | s->packet_number = 0; |
| 157 | |
| 158 | /* XXX: hardcoded */ |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 159 | if (ctx->flags & AVF_FLAG_VCD) |
| 160 | s->packet_size = 2324; /* VCD packet size */ |
| 161 | else |
| 162 | s->packet_size = 2048; |
| 163 | |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 164 | /* startcode(4) + length(2) + flags(1) */ |
| 165 | s->packet_data_max_size = s->packet_size - 7; |
| 166 | s->audio_bound = 0; |
| 167 | s->video_bound = 0; |
| 168 | mpa_id = AUDIO_ID; |
| 169 | ac3_id = 0x80; |
| 170 | mpv_id = VIDEO_ID; |
| 171 | for(i=0;i<ctx->nb_streams;i++) { |
| 172 | st = ctx->streams[i]; |
| 173 | stream = av_mallocz(sizeof(StreamInfo)); |
| 174 | if (!stream) |
| 175 | goto fail; |
| 176 | st->priv_data = stream; |
| 177 | |
| 178 | switch(st->codec.codec_type) { |
| 179 | case CODEC_TYPE_AUDIO: |
| 180 | if (st->codec.codec_id == CODEC_ID_AC3) |
| 181 | stream->id = ac3_id++; |
| 182 | else |
| 183 | stream->id = mpa_id++; |
| 184 | stream->max_buffer_size = 4 * 1024; |
| 185 | s->audio_bound++; |
| 186 | break; |
| 187 | case CODEC_TYPE_VIDEO: |
| 188 | stream->id = mpv_id++; |
| 189 | stream->max_buffer_size = 46 * 1024; |
| 190 | s->video_bound++; |
| 191 | break; |
Philip Gladstone | ac5e6a5 | 2002-05-09 01:19:33 | [diff] [blame] | 192 | default: |
Philip Gladstone | 42343f7 | 2002-09-12 02:34:01 | [diff] [blame] | 193 | av_abort(); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | /* we increase slightly the bitrate to take into account the |
| 198 | headers. XXX: compute it exactly */ |
| 199 | bitrate = 2000; |
| 200 | for(i=0;i<ctx->nb_streams;i++) { |
| 201 | st = ctx->streams[i]; |
| 202 | bitrate += st->codec.bit_rate; |
| 203 | } |
| 204 | s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 205 | |
| 206 | if (ctx->flags & AVF_FLAG_VCD) |
| 207 | /* every packet */ |
| 208 | s->pack_header_freq = 1; |
| 209 | else |
| 210 | /* every 2 seconds */ |
| 211 | s->pack_header_freq = 2 * bitrate / s->packet_size / 8; |
| 212 | |
| 213 | if (ctx->flags & AVF_FLAG_VCD) |
| 214 | /* every 40 packets, this is my invention */ |
| 215 | s->system_header_freq = s->pack_header_freq * 40; |
| 216 | else |
| 217 | /* every 10 seconds */ |
| 218 | s->system_header_freq = s->pack_header_freq * 5; |
| 219 | |
| 220 | |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 221 | for(i=0;i<ctx->nb_streams;i++) { |
| 222 | stream = ctx->streams[i]->priv_data; |
| 223 | stream->buffer_ptr = 0; |
| 224 | stream->packet_number = 0; |
| 225 | stream->pts = 0; |
| 226 | stream->start_pts = -1; |
Juanjo | af46942 | 2002-03-20 11:16:07 | [diff] [blame] | 227 | |
| 228 | st = ctx->streams[i]; |
| 229 | switch (st->codec.codec_type) { |
| 230 | case CODEC_TYPE_AUDIO: |
| 231 | ticker_init(&stream->pts_ticker, |
| 232 | st->codec.sample_rate, |
| 233 | 90000 * st->codec.frame_size); |
| 234 | break; |
| 235 | case CODEC_TYPE_VIDEO: |
| 236 | ticker_init(&stream->pts_ticker, |
| 237 | st->codec.frame_rate, |
| 238 | 90000 * FRAME_RATE_BASE); |
| 239 | break; |
Philip Gladstone | ac5e6a5 | 2002-05-09 01:19:33 | [diff] [blame] | 240 | default: |
Philip Gladstone | 42343f7 | 2002-09-12 02:34:01 | [diff] [blame] | 241 | av_abort(); |
Juanjo | af46942 | 2002-03-20 11:16:07 | [diff] [blame] | 242 | } |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 243 | } |
| 244 | return 0; |
| 245 | fail: |
| 246 | for(i=0;i<ctx->nb_streams;i++) { |
Fabrice Bellard | 1ea4f59 | 2002-05-18 23:11:09 | [diff] [blame] | 247 | av_free(ctx->streams[i]->priv_data); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 248 | } |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 249 | return -ENOMEM; |
| 250 | } |
| 251 | |
| 252 | /* flush the packet on stream stream_index */ |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 253 | static void flush_packet(AVFormatContext *ctx, int stream_index, int last_pkt) |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 254 | { |
| 255 | MpegMuxContext *s = ctx->priv_data; |
| 256 | StreamInfo *stream = ctx->streams[stream_index]->priv_data; |
| 257 | UINT8 *buf_ptr; |
| 258 | int size, payload_size, startcode, id, len, stuffing_size, i; |
| 259 | INT64 timestamp; |
| 260 | UINT8 buffer[128]; |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 261 | int last = last_pkt ? 4 : 0; |
| 262 | |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 263 | id = stream->id; |
| 264 | timestamp = stream->start_pts; |
| 265 | |
| 266 | #if 0 |
| 267 | printf("packet ID=%2x PTS=%0.3f\n", |
| 268 | id, timestamp / 90000.0); |
| 269 | #endif |
| 270 | |
| 271 | buf_ptr = buffer; |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 272 | if (((s->packet_number % s->pack_header_freq) == 0)) { |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 273 | /* output pack and systems header if needed */ |
| 274 | size = put_pack_header(ctx, buf_ptr, timestamp); |
| 275 | buf_ptr += size; |
| 276 | if ((s->packet_number % s->system_header_freq) == 0) { |
| 277 | size = put_system_header(ctx, buf_ptr); |
| 278 | buf_ptr += size; |
| 279 | } |
| 280 | } |
| 281 | size = buf_ptr - buffer; |
| 282 | put_buffer(&ctx->pb, buffer, size); |
| 283 | |
| 284 | /* packet header */ |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 285 | payload_size = s->packet_size - (size + 6 + 5 + last); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 286 | if (id < 0xc0) { |
| 287 | startcode = PRIVATE_STREAM_1; |
| 288 | payload_size -= 4; |
| 289 | } else { |
| 290 | startcode = 0x100 + id; |
| 291 | } |
| 292 | stuffing_size = payload_size - stream->buffer_ptr; |
| 293 | if (stuffing_size < 0) |
| 294 | stuffing_size = 0; |
| 295 | |
| 296 | put_be32(&ctx->pb, startcode); |
| 297 | |
| 298 | put_be16(&ctx->pb, payload_size + 5); |
| 299 | /* stuffing */ |
| 300 | for(i=0;i<stuffing_size;i++) |
| 301 | put_byte(&ctx->pb, 0xff); |
| 302 | |
| 303 | /* presentation time stamp */ |
| 304 | put_byte(&ctx->pb, |
| 305 | (0x02 << 4) | |
| 306 | (((timestamp >> 30) & 0x07) << 1) | |
| 307 | 1); |
Fabrice Bellard | 8be1c65 | 2001-08-13 21:37:10 | [diff] [blame] | 308 | put_be16(&ctx->pb, (UINT16)((((timestamp >> 15) & 0x7fff) << 1) | 1)); |
| 309 | put_be16(&ctx->pb, (UINT16)((((timestamp) & 0x7fff) << 1) | 1)); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 310 | |
| 311 | if (startcode == PRIVATE_STREAM_1) { |
| 312 | put_byte(&ctx->pb, id); |
| 313 | if (id >= 0x80 && id <= 0xbf) { |
| 314 | /* XXX: need to check AC3 spec */ |
| 315 | put_byte(&ctx->pb, 1); |
| 316 | put_byte(&ctx->pb, 0); |
| 317 | put_byte(&ctx->pb, 2); |
| 318 | } |
| 319 | } |
| 320 | |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 321 | if (last_pkt) { |
| 322 | put_be32(&ctx->pb, ISO_11172_END_CODE); |
| 323 | } |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 324 | /* output data */ |
| 325 | put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size); |
| 326 | put_flush_packet(&ctx->pb); |
| 327 | |
| 328 | /* preserve remaining data */ |
| 329 | len = stream->buffer_ptr - payload_size; |
| 330 | if (len < 0) |
| 331 | len = 0; |
| 332 | memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len); |
| 333 | stream->buffer_ptr = len; |
| 334 | |
| 335 | s->packet_number++; |
| 336 | stream->packet_number++; |
| 337 | stream->start_pts = -1; |
| 338 | } |
| 339 | |
Juanjo | 10bb702 | 2002-04-07 21:44:29 | [diff] [blame] | 340 | static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, |
| 341 | UINT8 *buf, int size, int force_pts) |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 342 | { |
| 343 | MpegMuxContext *s = ctx->priv_data; |
| 344 | AVStream *st = ctx->streams[stream_index]; |
| 345 | StreamInfo *stream = st->priv_data; |
| 346 | int len; |
Juanjo | 10bb702 | 2002-04-07 21:44:29 | [diff] [blame] | 347 | |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 348 | while (size > 0) { |
| 349 | /* set pts */ |
Juanjo | 10bb702 | 2002-04-07 21:44:29 | [diff] [blame] | 350 | if (stream->start_pts == -1) { |
| 351 | if (force_pts) |
| 352 | stream->pts = force_pts; |
Juanjo | af46942 | 2002-03-20 11:16:07 | [diff] [blame] | 353 | stream->start_pts = stream->pts; |
Juanjo | 10bb702 | 2002-04-07 21:44:29 | [diff] [blame] | 354 | } |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 355 | len = s->packet_data_max_size - stream->buffer_ptr; |
| 356 | if (len > size) |
| 357 | len = size; |
| 358 | memcpy(stream->buffer + stream->buffer_ptr, buf, len); |
| 359 | stream->buffer_ptr += len; |
| 360 | buf += len; |
| 361 | size -= len; |
| 362 | while (stream->buffer_ptr >= s->packet_data_max_size) { |
| 363 | /* output the packet */ |
| 364 | if (stream->start_pts == -1) |
Juanjo | af46942 | 2002-03-20 11:16:07 | [diff] [blame] | 365 | stream->start_pts = stream->pts; |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 366 | flush_packet(ctx, stream_index, 0); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Juanjo | af46942 | 2002-03-20 11:16:07 | [diff] [blame] | 370 | stream->pts += ticker_tick(&stream->pts_ticker, 1); |
Juanjo | 76c0441 | 2002-05-14 21:50:00 | [diff] [blame] | 371 | //if (st->codec.codec_type == CODEC_TYPE_VIDEO) |
| 372 | // fprintf(stderr,"\nVideo PTS: %6lld", stream->pts); |
| 373 | //else |
| 374 | // fprintf(stderr,"\nAudio PTS: %6lld", stream->pts); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static int mpeg_mux_end(AVFormatContext *ctx) |
| 379 | { |
| 380 | StreamInfo *stream; |
| 381 | int i; |
| 382 | |
| 383 | /* flush each packet */ |
| 384 | for(i=0;i<ctx->nb_streams;i++) { |
| 385 | stream = ctx->streams[i]->priv_data; |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 386 | if (stream->buffer_ptr > 0) { |
| 387 | if (i == (ctx->nb_streams - 1)) |
| 388 | flush_packet(ctx, i, 1); |
| 389 | else |
| 390 | flush_packet(ctx, i, 0); |
| 391 | } |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* write the end header */ |
Juanjo | 92b3e12 | 2002-05-12 21:38:54 | [diff] [blame] | 395 | //put_be32(&ctx->pb, ISO_11172_END_CODE); |
| 396 | //put_flush_packet(&ctx->pb); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /*********************************************/ |
| 401 | /* demux code */ |
| 402 | |
| 403 | #define MAX_SYNC_SIZE 100000 |
| 404 | |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 405 | static int mpegps_probe(AVProbeData *p) |
| 406 | { |
| 407 | int code, c, i; |
| 408 | code = 0xff; |
| 409 | |
| 410 | /* we search the first start code. If it is a packet start code, |
| 411 | then we decide it is mpeg ps. We do not send highest value to |
| 412 | give a chance to mpegts */ |
| 413 | for(i=0;i<p->buf_size;i++) { |
| 414 | c = p->buf[i]; |
| 415 | code = (code << 8) | c; |
| 416 | if ((code & 0xffffff00) == 0x100) { |
| 417 | if (code == PACK_START_CODE || |
| 418 | code == SYSTEM_HEADER_START_CODE || |
| 419 | (code >= 0x1e0 && code <= 0x1ef) || |
| 420 | (code >= 0x1c0 && code <= 0x1df) || |
| 421 | code == PRIVATE_STREAM_2 || |
| 422 | code == PROGRAM_STREAM_MAP || |
| 423 | code == PRIVATE_STREAM_1 || |
| 424 | code == PADDING_STREAM) |
| 425 | return AVPROBE_SCORE_MAX - 1; |
| 426 | else |
| 427 | return 0; |
| 428 | } |
| 429 | } |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 434 | typedef struct MpegDemuxContext { |
| 435 | int header_state; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 436 | } MpegDemuxContext; |
| 437 | |
| 438 | static int find_start_code(ByteIOContext *pb, int *size_ptr, |
| 439 | UINT32 *header_state) |
| 440 | { |
| 441 | unsigned int state, v; |
| 442 | int val, n; |
| 443 | |
| 444 | state = *header_state; |
| 445 | n = *size_ptr; |
| 446 | while (n > 0) { |
| 447 | if (url_feof(pb)) |
| 448 | break; |
| 449 | v = get_byte(pb); |
| 450 | n--; |
| 451 | if (state == 0x000001) { |
| 452 | state = ((state << 8) | v) & 0xffffff; |
| 453 | val = state; |
| 454 | goto found; |
| 455 | } |
| 456 | state = ((state << 8) | v) & 0xffffff; |
| 457 | } |
| 458 | val = -1; |
| 459 | found: |
| 460 | *header_state = state; |
| 461 | *size_ptr = n; |
| 462 | return val; |
| 463 | } |
| 464 | |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 465 | static int mpegps_read_header(AVFormatContext *s, |
| 466 | AVFormatParameters *ap) |
Juanjo | 001e3f5 | 2002-03-17 17:44:45 | [diff] [blame] | 467 | { |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 468 | MpegDemuxContext *m = s->priv_data; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 469 | m->header_state = 0xff; |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 470 | /* no need to do more */ |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static INT64 get_pts(ByteIOContext *pb, int c) |
| 475 | { |
| 476 | INT64 pts; |
| 477 | int val; |
| 478 | |
| 479 | if (c < 0) |
| 480 | c = get_byte(pb); |
| 481 | pts = (INT64)((c >> 1) & 0x07) << 30; |
| 482 | val = get_be16(pb); |
| 483 | pts |= (INT64)(val >> 1) << 15; |
| 484 | val = get_be16(pb); |
| 485 | pts |= (INT64)(val >> 1); |
| 486 | return pts; |
| 487 | } |
| 488 | |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 489 | static int mpegps_read_packet(AVFormatContext *s, |
| 490 | AVPacket *pkt) |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 491 | { |
| 492 | MpegDemuxContext *m = s->priv_data; |
| 493 | AVStream *st; |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 494 | int len, size, startcode, i, c, flags, header_len, type, codec_id; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 495 | INT64 pts, dts; |
| 496 | |
| 497 | /* next start code (should be immediately after */ |
| 498 | redo: |
| 499 | m->header_state = 0xff; |
| 500 | size = MAX_SYNC_SIZE; |
| 501 | startcode = find_start_code(&s->pb, &size, &m->header_state); |
Juanjo | 001e3f5 | 2002-03-17 17:44:45 | [diff] [blame] | 502 | //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb)); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 503 | if (startcode < 0) |
| 504 | return -EIO; |
| 505 | if (startcode == PACK_START_CODE) |
| 506 | goto redo; |
| 507 | if (startcode == SYSTEM_HEADER_START_CODE) |
| 508 | goto redo; |
| 509 | if (startcode == PADDING_STREAM || |
| 510 | startcode == PRIVATE_STREAM_2) { |
| 511 | /* skip them */ |
| 512 | len = get_be16(&s->pb); |
| 513 | url_fskip(&s->pb, len); |
| 514 | goto redo; |
| 515 | } |
| 516 | /* find matching stream */ |
| 517 | if (!((startcode >= 0x1c0 && startcode <= 0x1df) || |
| 518 | (startcode >= 0x1e0 && startcode <= 0x1ef) || |
| 519 | (startcode == 0x1bd))) |
| 520 | goto redo; |
| 521 | |
| 522 | len = get_be16(&s->pb); |
| 523 | pts = 0; |
| 524 | dts = 0; |
| 525 | /* stuffing */ |
| 526 | for(;;) { |
| 527 | c = get_byte(&s->pb); |
| 528 | len--; |
| 529 | /* XXX: for mpeg1, should test only bit 7 */ |
| 530 | if (c != 0xff) |
| 531 | break; |
| 532 | } |
| 533 | if ((c & 0xc0) == 0x40) { |
| 534 | /* buffer scale & size */ |
| 535 | get_byte(&s->pb); |
| 536 | c = get_byte(&s->pb); |
| 537 | len -= 2; |
| 538 | } |
| 539 | if ((c & 0xf0) == 0x20) { |
| 540 | pts = get_pts(&s->pb, c); |
| 541 | len -= 4; |
| 542 | dts = pts; |
| 543 | } else if ((c & 0xf0) == 0x30) { |
| 544 | pts = get_pts(&s->pb, c); |
| 545 | dts = get_pts(&s->pb, -1); |
| 546 | len -= 9; |
| 547 | } else if ((c & 0xc0) == 0x80) { |
| 548 | /* mpeg 2 PES */ |
| 549 | if ((c & 0x30) != 0) { |
| 550 | fprintf(stderr, "Encrypted multiplex not handled\n"); |
| 551 | return -EIO; |
| 552 | } |
| 553 | flags = get_byte(&s->pb); |
| 554 | header_len = get_byte(&s->pb); |
| 555 | len -= 2; |
| 556 | if (header_len > len) |
| 557 | goto redo; |
Fabrice Bellard | 1e5c667 | 2002-10-04 15:46:59 | [diff] [blame^] | 558 | if ((flags & 0xc0) == 0x80) { |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 559 | pts = get_pts(&s->pb, -1); |
| 560 | dts = pts; |
| 561 | header_len -= 5; |
| 562 | len -= 5; |
| 563 | } if ((flags & 0xc0) == 0xc0) { |
| 564 | pts = get_pts(&s->pb, -1); |
| 565 | dts = get_pts(&s->pb, -1); |
| 566 | header_len -= 10; |
| 567 | len -= 10; |
| 568 | } |
| 569 | len -= header_len; |
| 570 | while (header_len > 0) { |
| 571 | get_byte(&s->pb); |
| 572 | header_len--; |
| 573 | } |
| 574 | } |
| 575 | if (startcode == 0x1bd) { |
| 576 | startcode = get_byte(&s->pb); |
| 577 | len--; |
| 578 | if (startcode >= 0x80 && startcode <= 0xbf) { |
| 579 | /* audio: skip header */ |
| 580 | get_byte(&s->pb); |
| 581 | get_byte(&s->pb); |
| 582 | get_byte(&s->pb); |
| 583 | len -= 3; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /* now find stream */ |
| 588 | for(i=0;i<s->nb_streams;i++) { |
| 589 | st = s->streams[i]; |
| 590 | if (st->id == startcode) |
| 591 | goto found; |
| 592 | } |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 593 | if (startcode >= 0x1e0 && startcode <= 0x1ef) { |
| 594 | type = CODEC_TYPE_VIDEO; |
| 595 | codec_id = CODEC_ID_MPEG1VIDEO; |
| 596 | } else if (startcode >= 0x1c0 && startcode <= 0x1df) { |
| 597 | type = CODEC_TYPE_AUDIO; |
| 598 | codec_id = CODEC_ID_MP2; |
| 599 | } else if (startcode >= 0x80 && startcode <= 0x9f) { |
| 600 | type = CODEC_TYPE_AUDIO; |
| 601 | codec_id = CODEC_ID_AC3; |
| 602 | } else { |
| 603 | skip: |
| 604 | /* skip packet */ |
| 605 | url_fskip(&s->pb, len); |
| 606 | goto redo; |
| 607 | } |
Fabrice Bellard | 1e5c667 | 2002-10-04 15:46:59 | [diff] [blame^] | 608 | /* no stream found: add a new stream */ |
| 609 | st = av_new_stream(s, startcode); |
| 610 | if (!st) |
| 611 | goto skip; |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 612 | st->codec.codec_type = type; |
| 613 | st->codec.codec_id = codec_id; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 614 | found: |
| 615 | av_new_packet(pkt, len); |
Juanjo | 10bb702 | 2002-04-07 21:44:29 | [diff] [blame] | 616 | //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode, |
| 617 | // (float)pts/90000, len); |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 618 | get_buffer(&s->pb, pkt->data, pkt->size); |
| 619 | pkt->pts = pts; |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 620 | pkt->stream_index = st->index; |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 624 | static int mpegps_read_close(AVFormatContext *s) |
Juanjo | 001e3f5 | 2002-03-17 17:44:45 | [diff] [blame] | 625 | { |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 629 | static AVOutputFormat mpegps_mux = { |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 630 | "mpeg", |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 631 | "MPEG PS format", |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 632 | "video/x-mpeg", |
| 633 | "mpg,mpeg,vob", |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 634 | sizeof(MpegMuxContext), |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 635 | CODEC_ID_MP2, |
| 636 | CODEC_ID_MPEG1VIDEO, |
| 637 | mpeg_mux_init, |
| 638 | mpeg_mux_write_packet, |
| 639 | mpeg_mux_end, |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 640 | }; |
Fabrice Bellard | db7f1f9 | 2002-05-20 16:29:40 | [diff] [blame] | 641 | |
| 642 | static AVInputFormat mpegps_demux = { |
| 643 | "mpeg", |
| 644 | "MPEG PS format", |
| 645 | sizeof(MpegDemuxContext), |
| 646 | mpegps_probe, |
| 647 | mpegps_read_header, |
| 648 | mpegps_read_packet, |
| 649 | mpegps_read_close, |
| 650 | flags: AVFMT_NOHEADER, |
| 651 | }; |
| 652 | |
| 653 | int mpegps_init(void) |
| 654 | { |
| 655 | av_register_output_format(&mpegps_mux); |
| 656 | av_register_input_format(&mpegps_demux); |
| 657 | return 0; |
| 658 | } |