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