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