Originally committed as revision 2804 to svn://svn.ffmpeg.org/ffmpeg/trunk
Hauke Duden authored on 2004/02/20 07:34:13... | ... |
@@ -3001,7 +3001,7 @@ static void opt_target(const char *arg) |
3001 | 3001 |
|
3002 | 3002 |
opt_video_codec("mpeg2video"); |
3003 | 3003 |
opt_audio_codec("mp2"); |
3004 |
- opt_format("vcd"); |
|
3004 |
+ opt_format("svcd"); |
|
3005 | 3005 |
|
3006 | 3006 |
opt_frame_size(norm ? "480x480" : "480x576"); |
3007 | 3007 |
opt_gop_size(norm ? "18" : "15"); |
... | ... |
@@ -50,9 +50,14 @@ typedef struct { |
50 | 50 |
int video_bound; |
51 | 51 |
int is_mpeg2; |
52 | 52 |
int is_vcd; |
53 |
+ int is_svcd; |
|
53 | 54 |
int scr_stream_index; /* stream from which the system clock is |
54 | 55 |
computed (VBR case) */ |
55 | 56 |
int64_t last_scr; /* current system clock */ |
57 |
+ |
|
58 |
+ double vcd_padding_bitrate; |
|
59 |
+ int64_t vcd_padding_bytes_written; |
|
60 |
+ |
|
56 | 61 |
} MpegMuxContext; |
57 | 62 |
|
58 | 63 |
#define PACK_START_CODE ((unsigned int)0x000001ba) |
... | ... |
@@ -80,6 +85,7 @@ static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 }; |
80 | 80 |
extern AVOutputFormat mpeg1system_mux; |
81 | 81 |
extern AVOutputFormat mpeg1vcd_mux; |
82 | 82 |
extern AVOutputFormat mpeg2vob_mux; |
83 |
+extern AVOutputFormat mpeg2svcd_mux; |
|
83 | 84 |
|
84 | 85 |
static int put_pack_header(AVFormatContext *ctx, |
85 | 86 |
uint8_t *buf, int64_t timestamp) |
... | ... |
@@ -117,7 +123,7 @@ static int put_pack_header(AVFormatContext *ctx, |
117 | 117 |
return pbBufPtr(&pb) - pb.buf; |
118 | 118 |
} |
119 | 119 |
|
120 |
-static int put_system_header(AVFormatContext *ctx, uint8_t *buf) |
|
120 |
+static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id) |
|
121 | 121 |
{ |
122 | 122 |
MpegMuxContext *s = ctx->priv_data; |
123 | 123 |
int size, rate_bound, i, private_stream_coded, id; |
... | ... |
@@ -132,40 +138,67 @@ static int put_system_header(AVFormatContext *ctx, uint8_t *buf) |
132 | 132 |
rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */ |
133 | 133 |
put_bits(&pb, 22, rate_bound); |
134 | 134 |
put_bits(&pb, 1, 1); /* marker */ |
135 |
- put_bits(&pb, 6, s->audio_bound); |
|
135 |
+ if (s->is_vcd && only_for_stream_id==VIDEO_ID) { |
|
136 |
+ /* This header applies only to the video stream (see VCD standard p. IV-7)*/ |
|
137 |
+ put_bits(&pb, 6, 0); |
|
138 |
+ } else |
|
139 |
+ put_bits(&pb, 6, s->audio_bound); |
|
136 | 140 |
|
137 |
- put_bits(&pb, 1, 1); /* variable bitrate */ |
|
141 |
+ if (s->is_vcd) |
|
142 |
+ put_bits(&pb, 1, 0); /* see VCD standard, p. IV-7*/ |
|
143 |
+ else |
|
144 |
+ put_bits(&pb, 1, 1); /* variable bitrate*/ |
|
138 | 145 |
put_bits(&pb, 1, 1); /* non constrainted bit stream */ |
139 | 146 |
|
140 |
- put_bits(&pb, 1, 0); /* audio locked */ |
|
141 |
- put_bits(&pb, 1, 0); /* video locked */ |
|
147 |
+ if (s->is_vcd) { |
|
148 |
+ /* see VCD standard p IV-7 */ |
|
149 |
+ put_bits(&pb, 1, 1); /* audio locked */ |
|
150 |
+ put_bits(&pb, 1, 1); /* video locked */ |
|
151 |
+ } else { |
|
152 |
+ put_bits(&pb, 1, 0); /* audio locked */ |
|
153 |
+ put_bits(&pb, 1, 0); /* video locked */ |
|
154 |
+ } |
|
155 |
+ |
|
142 | 156 |
put_bits(&pb, 1, 1); /* marker */ |
143 | 157 |
|
144 |
- put_bits(&pb, 5, s->video_bound); |
|
158 |
+ if (s->is_vcd && only_for_stream_id==AUDIO_ID) { |
|
159 |
+ /* This header applies only to the audio stream (see VCD standard p. IV-7)*/ |
|
160 |
+ put_bits(&pb, 5, 0); |
|
161 |
+ } else |
|
162 |
+ put_bits(&pb, 5, s->video_bound); |
|
163 |
+ |
|
145 | 164 |
put_bits(&pb, 8, 0xff); /* reserved byte */ |
146 | 165 |
|
147 | 166 |
/* audio stream info */ |
148 | 167 |
private_stream_coded = 0; |
149 | 168 |
for(i=0;i<ctx->nb_streams;i++) { |
150 | 169 |
StreamInfo *stream = ctx->streams[i]->priv_data; |
151 |
- id = stream->id; |
|
152 |
- if (id < 0xc0) { |
|
153 |
- /* special case for private streams (AC3 use that) */ |
|
154 |
- if (private_stream_coded) |
|
155 |
- continue; |
|
156 |
- private_stream_coded = 1; |
|
157 |
- id = 0xbd; |
|
158 |
- } |
|
159 |
- put_bits(&pb, 8, id); /* stream ID */ |
|
160 |
- put_bits(&pb, 2, 3); |
|
161 |
- if (id < 0xe0) { |
|
162 |
- /* audio */ |
|
163 |
- put_bits(&pb, 1, 0); |
|
164 |
- put_bits(&pb, 13, stream->max_buffer_size / 128); |
|
165 |
- } else { |
|
166 |
- /* video */ |
|
167 |
- put_bits(&pb, 1, 1); |
|
168 |
- put_bits(&pb, 13, stream->max_buffer_size / 1024); |
|
170 |
+ |
|
171 |
+ /* For VCDs, only include the stream info for the stream |
|
172 |
+ that the pack which contains this system belongs to. |
|
173 |
+ (see VCD standard p. IV-7) */ |
|
174 |
+ if ( !s->is_vcd || stream->id==only_for_stream_id |
|
175 |
+ || only_for_stream_id==0) { |
|
176 |
+ |
|
177 |
+ id = stream->id; |
|
178 |
+ if (id < 0xc0) { |
|
179 |
+ /* special case for private streams (AC3 use that) */ |
|
180 |
+ if (private_stream_coded) |
|
181 |
+ continue; |
|
182 |
+ private_stream_coded = 1; |
|
183 |
+ id = 0xbd; |
|
184 |
+ } |
|
185 |
+ put_bits(&pb, 8, id); /* stream ID */ |
|
186 |
+ put_bits(&pb, 2, 3); |
|
187 |
+ if (id < 0xe0) { |
|
188 |
+ /* audio */ |
|
189 |
+ put_bits(&pb, 1, 0); |
|
190 |
+ put_bits(&pb, 13, stream->max_buffer_size / 128); |
|
191 |
+ } else { |
|
192 |
+ /* video */ |
|
193 |
+ put_bits(&pb, 1, 1); |
|
194 |
+ put_bits(&pb, 13, stream->max_buffer_size / 1024); |
|
195 |
+ } |
|
169 | 196 |
} |
170 | 197 |
} |
171 | 198 |
flush_put_bits(&pb); |
... | ... |
@@ -202,15 +235,21 @@ static int mpeg_mux_init(AVFormatContext *ctx) |
202 | 202 |
int bitrate, i, mpa_id, mpv_id, ac3_id, lpcm_id, j; |
203 | 203 |
AVStream *st; |
204 | 204 |
StreamInfo *stream; |
205 |
+ int audio_bitrate; |
|
206 |
+ int video_bitrate; |
|
205 | 207 |
|
206 | 208 |
s->packet_number = 0; |
207 | 209 |
s->is_vcd = (ctx->oformat == &mpeg1vcd_mux); |
208 |
- s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux); |
|
210 |
+ s->is_svcd = (ctx->oformat == &mpeg2svcd_mux); |
|
211 |
+ s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux || ctx->oformat == &mpeg2svcd_mux); |
|
209 | 212 |
|
210 |
- if (s->is_vcd) |
|
211 |
- s->packet_size = 2324; /* VCD packet size */ |
|
213 |
+ if (s->is_vcd || s->is_svcd) |
|
214 |
+ s->packet_size = 2324; /* VCD/SVCD packet size */ |
|
212 | 215 |
else |
213 | 216 |
s->packet_size = 2048; |
217 |
+ |
|
218 |
+ s->vcd_padding_bytes_written = 0; |
|
219 |
+ s->vcd_padding_bitrate=0; |
|
214 | 220 |
|
215 | 221 |
s->audio_bound = 0; |
216 | 222 |
s->video_bound = 0; |
... | ... |
@@ -266,14 +305,55 @@ static int mpeg_mux_init(AVFormatContext *ctx) |
266 | 266 |
if (s->scr_stream_index == -1) |
267 | 267 |
s->scr_stream_index = 0; |
268 | 268 |
|
269 |
- /* we increase slightly the bitrate to take into account the |
|
270 |
- headers. XXX: compute it exactly */ |
|
271 |
- bitrate = 2000; |
|
269 |
+ bitrate = 0; |
|
270 |
+ audio_bitrate = 0; |
|
271 |
+ video_bitrate = 0; |
|
272 | 272 |
for(i=0;i<ctx->nb_streams;i++) { |
273 | 273 |
st = ctx->streams[i]; |
274 |
+ stream = (StreamInfo*) st->priv_data; |
|
275 |
+ |
|
274 | 276 |
bitrate += st->codec.bit_rate; |
277 |
+ |
|
278 |
+ if (stream->id==AUDIO_ID) |
|
279 |
+ audio_bitrate += st->codec.bit_rate; |
|
280 |
+ else if (stream->id==VIDEO_ID) |
|
281 |
+ video_bitrate += st->codec.bit_rate; |
|
282 |
+ } |
|
283 |
+ |
|
284 |
+ if (s->is_vcd) { |
|
285 |
+ double overhead_rate; |
|
286 |
+ |
|
287 |
+ /* The VCD standard mandates that the mux_rate field is 3528 |
|
288 |
+ (see standard p. IV-6). |
|
289 |
+ The value is actually "wrong", i.e. if you calculate |
|
290 |
+ it using the normal formula and the 75 sectors per second transfer |
|
291 |
+ rate you get a different value because the real pack size is 2324, |
|
292 |
+ not 2352. But the standard explicitly specifies that the mux_rate |
|
293 |
+ field in the header must have this value.*/ |
|
294 |
+ s->mux_rate=2352 * 75 / 50; /* = 3528*/ |
|
295 |
+ |
|
296 |
+ /* The VCD standard states that the muxed stream must be |
|
297 |
+ exactly 75 packs / second (the data rate of a single speed cdrom). |
|
298 |
+ Since the video bitrate (probably 1150000 bits/sec) will be below |
|
299 |
+ the theoretical maximum we have to add some padding packets |
|
300 |
+ to make up for the lower data rate. |
|
301 |
+ (cf. VCD standard p. IV-6 )*/ |
|
302 |
+ |
|
303 |
+ /* Add the header overhead to the data rate. |
|
304 |
+ 2279 data bytes per audio pack, 2294 data bytes per video pack*/ |
|
305 |
+ overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279); |
|
306 |
+ overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294); |
|
307 |
+ overhead_rate *= 8; |
|
308 |
+ |
|
309 |
+ /* Add padding so that the full bitrate is 2324*75 bytes/sec */ |
|
310 |
+ s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate); |
|
311 |
+ |
|
312 |
+ } else { |
|
313 |
+ /* we increase slightly the bitrate to take into account the |
|
314 |
+ headers. XXX: compute it exactly */ |
|
315 |
+ bitrate += 2000; |
|
316 |
+ s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); |
|
275 | 317 |
} |
276 |
- s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); |
|
277 | 318 |
|
278 | 319 |
if (s->is_vcd || s->is_mpeg2) |
279 | 320 |
/* every packet */ |
... | ... |
@@ -290,8 +370,10 @@ static int mpeg_mux_init(AVFormatContext *ctx) |
290 | 290 |
/* every 200 packets. Need to look at the spec. */ |
291 | 291 |
s->system_header_freq = s->pack_header_freq * 40; |
292 | 292 |
else if (s->is_vcd) |
293 |
- /* every 40 packets, this is my invention */ |
|
294 |
- s->system_header_freq = s->pack_header_freq * 40; |
|
293 |
+ /* the standard mandates that there are only two system headers |
|
294 |
+ in the whole file: one in the first packet of each stream. |
|
295 |
+ (see standard p. IV-7 and IV-8) */ |
|
296 |
+ s->system_header_freq = 0x7fffffff; |
|
295 | 297 |
else |
296 | 298 |
s->system_header_freq = s->pack_header_freq * 5; |
297 | 299 |
|
... | ... |
@@ -323,6 +405,30 @@ static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp) |
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
|
326 |
+/* return the number of padding bytes that should be inserted into |
|
327 |
+ the multiplexed stream.*/ |
|
328 |
+static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts) |
|
329 |
+{ |
|
330 |
+ MpegMuxContext *s = ctx->priv_data; |
|
331 |
+ int pad_bytes = 0; |
|
332 |
+ |
|
333 |
+ if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE) |
|
334 |
+ { |
|
335 |
+ int64_t full_pad_bytes; |
|
336 |
+ |
|
337 |
+ full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); |
|
338 |
+ pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written); |
|
339 |
+ |
|
340 |
+ if (pad_bytes<0) |
|
341 |
+ /* might happen if we have already padded to a later timestamp. This |
|
342 |
+ can occur if another stream has already advanced further.*/ |
|
343 |
+ pad_bytes=0; |
|
344 |
+ } |
|
345 |
+ |
|
346 |
+ return pad_bytes; |
|
347 |
+} |
|
348 |
+ |
|
349 |
+ |
|
326 | 350 |
/* return the exact available payload size for the next packet for |
327 | 351 |
stream 'stream_index'. 'pts' and 'dts' are only used to know if |
328 | 352 |
timestamps are needed in the packet header. */ |
... | ... |
@@ -333,6 +439,8 @@ static int get_packet_payload_size(AVFormatContext *ctx, int stream_index, |
333 | 333 |
int buf_index; |
334 | 334 |
StreamInfo *stream; |
335 | 335 |
|
336 |
+ stream = ctx->streams[stream_index]->priv_data; |
|
337 |
+ |
|
336 | 338 |
buf_index = 0; |
337 | 339 |
if (((s->packet_number % s->pack_header_freq) == 0)) { |
338 | 340 |
/* pack header size */ |
... | ... |
@@ -340,41 +448,100 @@ static int get_packet_payload_size(AVFormatContext *ctx, int stream_index, |
340 | 340 |
buf_index += 14; |
341 | 341 |
else |
342 | 342 |
buf_index += 12; |
343 |
- if ((s->packet_number % s->system_header_freq) == 0) |
|
344 |
- buf_index += s->system_header_size; |
|
343 |
+ |
|
344 |
+ if (s->is_vcd) { |
|
345 |
+ /* there is exactly one system header for each stream in a VCD MPEG, |
|
346 |
+ One in the very first video packet and one in the very first |
|
347 |
+ audio packet (see VCD standard p. IV-7 and IV-8).*/ |
|
348 |
+ |
|
349 |
+ if (stream->packet_number==0) |
|
350 |
+ /* The system headers refer only to the stream they occur in, |
|
351 |
+ so they have a constant size.*/ |
|
352 |
+ buf_index += 15; |
|
353 |
+ |
|
354 |
+ } else { |
|
355 |
+ if ((s->packet_number % s->system_header_freq) == 0) |
|
356 |
+ buf_index += s->system_header_size; |
|
357 |
+ } |
|
345 | 358 |
} |
346 | 359 |
|
347 |
- /* packet header size */ |
|
348 |
- buf_index += 6; |
|
349 |
- if (s->is_mpeg2) |
|
350 |
- buf_index += 3; |
|
351 |
- if (pts != AV_NOPTS_VALUE) { |
|
352 |
- if (dts != pts) |
|
353 |
- buf_index += 5 + 5; |
|
354 |
- else |
|
355 |
- buf_index += 5; |
|
356 |
- } else { |
|
357 |
- if (!s->is_mpeg2) |
|
358 |
- buf_index++; |
|
359 |
- } |
|
360 |
- |
|
361 |
- stream = ctx->streams[stream_index]->priv_data; |
|
362 |
- if (stream->id < 0xc0) { |
|
363 |
- /* AC3/LPCM private data header */ |
|
364 |
- buf_index += 4; |
|
365 |
- if (stream->id >= 0xa0) { |
|
366 |
- int n; |
|
360 |
+ if (s->is_vcd && stream->packet_number==0) |
|
361 |
+ /* the first pack of each stream contains only the pack header, |
|
362 |
+ the system header and some padding (see VCD standard p. IV-6) |
|
363 |
+ Add the padding size, so that the actual payload becomes 0.*/ |
|
364 |
+ buf_index += s->packet_size - buf_index; |
|
365 |
+ else { |
|
366 |
+ /* packet header size */ |
|
367 |
+ buf_index += 6; |
|
368 |
+ if (s->is_mpeg2) |
|
367 | 369 |
buf_index += 3; |
368 |
- /* NOTE: we round the payload size to an integer number of |
|
369 |
- LPCM samples */ |
|
370 |
- n = (s->packet_size - buf_index) % stream->lpcm_align; |
|
371 |
- if (n) |
|
372 |
- buf_index += (stream->lpcm_align - n); |
|
370 |
+ if (pts != AV_NOPTS_VALUE) { |
|
371 |
+ if (dts != pts) |
|
372 |
+ buf_index += 5 + 5; |
|
373 |
+ else |
|
374 |
+ buf_index += 5; |
|
375 |
+ |
|
376 |
+ } else { |
|
377 |
+ if (!s->is_mpeg2) |
|
378 |
+ buf_index++; |
|
379 |
+ } |
|
380 |
+ |
|
381 |
+ if (stream->id < 0xc0) { |
|
382 |
+ /* AC3/LPCM private data header */ |
|
383 |
+ buf_index += 4; |
|
384 |
+ if (stream->id >= 0xa0) { |
|
385 |
+ int n; |
|
386 |
+ buf_index += 3; |
|
387 |
+ /* NOTE: we round the payload size to an integer number of |
|
388 |
+ LPCM samples */ |
|
389 |
+ n = (s->packet_size - buf_index) % stream->lpcm_align; |
|
390 |
+ if (n) |
|
391 |
+ buf_index += (stream->lpcm_align - n); |
|
392 |
+ } |
|
373 | 393 |
} |
394 |
+ |
|
395 |
+ if (s->is_vcd && stream->id == AUDIO_ID) |
|
396 |
+ /* The VCD standard demands that 20 zero bytes follow |
|
397 |
+ each audio packet (see standard p. IV-8).*/ |
|
398 |
+ buf_index+=20; |
|
374 | 399 |
} |
375 | 400 |
return s->packet_size - buf_index; |
376 | 401 |
} |
377 | 402 |
|
403 |
+/* Write an MPEG padding packet header. */ |
|
404 |
+static int put_padding_header(AVFormatContext *ctx,uint8_t* buf, int full_padding_size) |
|
405 |
+{ |
|
406 |
+ MpegMuxContext *s = ctx->priv_data; |
|
407 |
+ int size = full_padding_size - 6; /* subtract header length */ |
|
408 |
+ |
|
409 |
+ buf[0] = (uint8_t)(PADDING_STREAM >> 24); |
|
410 |
+ buf[1] = (uint8_t)(PADDING_STREAM >> 16); |
|
411 |
+ buf[2] = (uint8_t)(PADDING_STREAM >> 8); |
|
412 |
+ buf[3] = (uint8_t)(PADDING_STREAM); |
|
413 |
+ buf[4] = (uint8_t)(size >> 8); |
|
414 |
+ buf[5] = (uint8_t)(size & 0xff); |
|
415 |
+ |
|
416 |
+ if (!s->is_mpeg2) { |
|
417 |
+ buf[6] = 0x0f; |
|
418 |
+ return 7; |
|
419 |
+ } else |
|
420 |
+ return 6; |
|
421 |
+} |
|
422 |
+ |
|
423 |
+static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes) |
|
424 |
+{ |
|
425 |
+ uint8_t buffer[7]; |
|
426 |
+ int size, i; |
|
427 |
+ |
|
428 |
+ size = put_padding_header(ctx,buffer, packet_bytes); |
|
429 |
+ put_buffer(pb, buffer, size); |
|
430 |
+ packet_bytes -= size; |
|
431 |
+ |
|
432 |
+ for(i=0;i<packet_bytes;i++) |
|
433 |
+ put_byte(pb, 0xff); |
|
434 |
+} |
|
435 |
+ |
|
436 |
+ |
|
378 | 437 |
/* flush the packet on stream stream_index */ |
379 | 438 |
static void flush_packet(AVFormatContext *ctx, int stream_index, |
380 | 439 |
int64_t pts, int64_t dts, int64_t scr) |
... | ... |
@@ -385,6 +552,8 @@ static void flush_packet(AVFormatContext *ctx, int stream_index, |
385 | 385 |
int size, payload_size, startcode, id, stuffing_size, i, header_len; |
386 | 386 |
int packet_size; |
387 | 387 |
uint8_t buffer[128]; |
388 |
+ int zero_trail_bytes = 0; |
|
389 |
+ int pad_packet_bytes = 0; |
|
388 | 390 |
|
389 | 391 |
id = stream->id; |
390 | 392 |
|
... | ... |
@@ -394,109 +563,151 @@ static void flush_packet(AVFormatContext *ctx, int stream_index, |
394 | 394 |
#endif |
395 | 395 |
|
396 | 396 |
buf_ptr = buffer; |
397 |
+ |
|
397 | 398 |
if (((s->packet_number % s->pack_header_freq) == 0)) { |
398 | 399 |
/* output pack and systems header if needed */ |
399 | 400 |
size = put_pack_header(ctx, buf_ptr, scr); |
400 | 401 |
buf_ptr += size; |
401 |
- if ((s->packet_number % s->system_header_freq) == 0) { |
|
402 |
- size = put_system_header(ctx, buf_ptr); |
|
403 |
- buf_ptr += size; |
|
402 |
+ |
|
403 |
+ if (s->is_vcd) { |
|
404 |
+ /* there is exactly one system header for each stream in a VCD MPEG, |
|
405 |
+ One in the very first video packet and one in the very first |
|
406 |
+ audio packet (see VCD standard p. IV-7 and IV-8).*/ |
|
407 |
+ |
|
408 |
+ if (stream->packet_number==0) { |
|
409 |
+ size = put_system_header(ctx, buf_ptr, id); |
|
410 |
+ buf_ptr += size; |
|
411 |
+ } |
|
412 |
+ } else { |
|
413 |
+ if ((s->packet_number % s->system_header_freq) == 0) { |
|
414 |
+ size = put_system_header(ctx, buf_ptr, 0); |
|
415 |
+ buf_ptr += size; |
|
416 |
+ } |
|
404 | 417 |
} |
405 | 418 |
} |
406 | 419 |
size = buf_ptr - buffer; |
407 | 420 |
put_buffer(&ctx->pb, buffer, size); |
408 | 421 |
|
409 |
- /* packet header */ |
|
410 |
- if (s->is_mpeg2) { |
|
411 |
- header_len = 3; |
|
412 |
- } else { |
|
413 |
- header_len = 0; |
|
414 |
- } |
|
415 |
- if (pts != AV_NOPTS_VALUE) { |
|
416 |
- if (dts != pts) |
|
417 |
- header_len += 5 + 5; |
|
418 |
- else |
|
419 |
- header_len += 5; |
|
420 |
- } else { |
|
421 |
- if (!s->is_mpeg2) |
|
422 |
- header_len++; |
|
422 |
+ packet_size = s->packet_size - size; |
|
423 |
+ |
|
424 |
+ if (s->is_vcd && id == AUDIO_ID) |
|
425 |
+ /* The VCD standard demands that 20 zero bytes follow |
|
426 |
+ each audio pack (see standard p. IV-8).*/ |
|
427 |
+ zero_trail_bytes += 20; |
|
428 |
+ |
|
429 |
+ if (s->is_vcd && stream->packet_number==0) { |
|
430 |
+ /* the first pack of each stream contains only the pack header, |
|
431 |
+ the system header and lots of padding (see VCD standard p. IV-6). |
|
432 |
+ In the case of an audio pack, 20 zero bytes are also added at |
|
433 |
+ the end.*/ |
|
434 |
+ pad_packet_bytes = packet_size - zero_trail_bytes; |
|
423 | 435 |
} |
424 | 436 |
|
425 |
- packet_size = s->packet_size - (size + 6); |
|
426 |
- payload_size = packet_size - header_len; |
|
427 |
- if (id < 0xc0) { |
|
428 |
- startcode = PRIVATE_STREAM_1; |
|
429 |
- payload_size -= 4; |
|
430 |
- if (id >= 0xa0) |
|
431 |
- payload_size -= 3; |
|
432 |
- } else { |
|
433 |
- startcode = 0x100 + id; |
|
434 |
- } |
|
437 |
+ packet_size -= pad_packet_bytes + zero_trail_bytes; |
|
435 | 438 |
|
436 |
- stuffing_size = payload_size - stream->buffer_ptr; |
|
437 |
- if (stuffing_size < 0) |
|
438 |
- stuffing_size = 0; |
|
439 |
- put_be32(&ctx->pb, startcode); |
|
439 |
+ if (packet_size > 0) { |
|
440 | 440 |
|
441 |
- put_be16(&ctx->pb, packet_size); |
|
441 |
+ /* packet header size */ |
|
442 |
+ packet_size -= 6; |
|
443 |
+ |
|
444 |
+ /* packet header */ |
|
445 |
+ if (s->is_mpeg2) { |
|
446 |
+ header_len = 3; |
|
447 |
+ } else { |
|
448 |
+ header_len = 0; |
|
449 |
+ } |
|
450 |
+ if (pts != AV_NOPTS_VALUE) { |
|
451 |
+ if (dts != pts) |
|
452 |
+ header_len += 5 + 5; |
|
453 |
+ else |
|
454 |
+ header_len += 5; |
|
455 |
+ } else { |
|
456 |
+ if (!s->is_mpeg2) |
|
457 |
+ header_len++; |
|
458 |
+ } |
|
442 | 459 |
|
443 |
- if (!s->is_mpeg2) |
|
444 |
- for(i=0;i<stuffing_size;i++) |
|
445 |
- put_byte(&ctx->pb, 0xff); |
|
460 |
+ payload_size = packet_size - header_len; |
|
461 |
+ if (id < 0xc0) { |
|
462 |
+ startcode = PRIVATE_STREAM_1; |
|
463 |
+ payload_size -= 4; |
|
464 |
+ if (id >= 0xa0) |
|
465 |
+ payload_size -= 3; |
|
466 |
+ } else { |
|
467 |
+ startcode = 0x100 + id; |
|
468 |
+ } |
|
446 | 469 |
|
447 |
- if (s->is_mpeg2) { |
|
448 |
- put_byte(&ctx->pb, 0x80); /* mpeg2 id */ |
|
470 |
+ stuffing_size = payload_size - stream->buffer_ptr; |
|
471 |
+ if (stuffing_size < 0) |
|
472 |
+ stuffing_size = 0; |
|
473 |
+ put_be32(&ctx->pb, startcode); |
|
449 | 474 |
|
450 |
- if (pts != AV_NOPTS_VALUE) { |
|
451 |
- if (dts != pts) { |
|
452 |
- put_byte(&ctx->pb, 0xc0); /* flags */ |
|
453 |
- put_byte(&ctx->pb, header_len - 3 + stuffing_size); |
|
454 |
- put_timestamp(&ctx->pb, 0x03, pts); |
|
455 |
- put_timestamp(&ctx->pb, 0x01, dts); |
|
475 |
+ put_be16(&ctx->pb, packet_size); |
|
476 |
+ |
|
477 |
+ if (!s->is_mpeg2) |
|
478 |
+ for(i=0;i<stuffing_size;i++) |
|
479 |
+ put_byte(&ctx->pb, 0xff); |
|
480 |
+ |
|
481 |
+ if (s->is_mpeg2) { |
|
482 |
+ put_byte(&ctx->pb, 0x80); /* mpeg2 id */ |
|
483 |
+ |
|
484 |
+ if (pts != AV_NOPTS_VALUE) { |
|
485 |
+ if (dts != pts) { |
|
486 |
+ put_byte(&ctx->pb, 0xc0); /* flags */ |
|
487 |
+ put_byte(&ctx->pb, header_len - 3 + stuffing_size); |
|
488 |
+ put_timestamp(&ctx->pb, 0x03, pts); |
|
489 |
+ put_timestamp(&ctx->pb, 0x01, dts); |
|
490 |
+ } else { |
|
491 |
+ put_byte(&ctx->pb, 0x80); /* flags */ |
|
492 |
+ put_byte(&ctx->pb, header_len - 3 + stuffing_size); |
|
493 |
+ put_timestamp(&ctx->pb, 0x02, pts); |
|
494 |
+ } |
|
456 | 495 |
} else { |
457 |
- put_byte(&ctx->pb, 0x80); /* flags */ |
|
496 |
+ put_byte(&ctx->pb, 0x00); /* flags */ |
|
458 | 497 |
put_byte(&ctx->pb, header_len - 3 + stuffing_size); |
459 |
- put_timestamp(&ctx->pb, 0x02, pts); |
|
460 | 498 |
} |
461 | 499 |
} else { |
462 |
- put_byte(&ctx->pb, 0x00); /* flags */ |
|
463 |
- put_byte(&ctx->pb, header_len - 3 + stuffing_size); |
|
464 |
- } |
|
465 |
- } else { |
|
466 |
- if (pts != AV_NOPTS_VALUE) { |
|
467 |
- if (dts != pts) { |
|
468 |
- put_timestamp(&ctx->pb, 0x03, pts); |
|
469 |
- put_timestamp(&ctx->pb, 0x01, dts); |
|
500 |
+ if (pts != AV_NOPTS_VALUE) { |
|
501 |
+ if (dts != pts) { |
|
502 |
+ put_timestamp(&ctx->pb, 0x03, pts); |
|
503 |
+ put_timestamp(&ctx->pb, 0x01, dts); |
|
504 |
+ } else { |
|
505 |
+ put_timestamp(&ctx->pb, 0x02, pts); |
|
506 |
+ } |
|
470 | 507 |
} else { |
471 |
- put_timestamp(&ctx->pb, 0x02, pts); |
|
508 |
+ put_byte(&ctx->pb, 0x0f); |
|
472 | 509 |
} |
473 |
- } else { |
|
474 |
- put_byte(&ctx->pb, 0x0f); |
|
475 | 510 |
} |
476 |
- } |
|
477 | 511 |
|
478 |
- if (startcode == PRIVATE_STREAM_1) { |
|
479 |
- put_byte(&ctx->pb, id); |
|
480 |
- if (id >= 0xa0) { |
|
481 |
- /* LPCM (XXX: check nb_frames) */ |
|
482 |
- put_byte(&ctx->pb, 7); |
|
483 |
- put_be16(&ctx->pb, 4); /* skip 3 header bytes */ |
|
484 |
- put_byte(&ctx->pb, stream->lpcm_header[0]); |
|
485 |
- put_byte(&ctx->pb, stream->lpcm_header[1]); |
|
486 |
- put_byte(&ctx->pb, stream->lpcm_header[2]); |
|
487 |
- } else { |
|
488 |
- /* AC3 */ |
|
489 |
- put_byte(&ctx->pb, stream->nb_frames); |
|
490 |
- put_be16(&ctx->pb, stream->frame_start_offset); |
|
512 |
+ if (startcode == PRIVATE_STREAM_1) { |
|
513 |
+ put_byte(&ctx->pb, id); |
|
514 |
+ if (id >= 0xa0) { |
|
515 |
+ /* LPCM (XXX: check nb_frames) */ |
|
516 |
+ put_byte(&ctx->pb, 7); |
|
517 |
+ put_be16(&ctx->pb, 4); /* skip 3 header bytes */ |
|
518 |
+ put_byte(&ctx->pb, stream->lpcm_header[0]); |
|
519 |
+ put_byte(&ctx->pb, stream->lpcm_header[1]); |
|
520 |
+ put_byte(&ctx->pb, stream->lpcm_header[2]); |
|
521 |
+ } else { |
|
522 |
+ /* AC3 */ |
|
523 |
+ put_byte(&ctx->pb, stream->nb_frames); |
|
524 |
+ put_be16(&ctx->pb, stream->frame_start_offset); |
|
525 |
+ } |
|
491 | 526 |
} |
527 |
+ |
|
528 |
+ if (s->is_mpeg2) |
|
529 |
+ for(i=0;i<stuffing_size;i++) |
|
530 |
+ put_byte(&ctx->pb, 0xff); |
|
531 |
+ |
|
532 |
+ /* output data */ |
|
533 |
+ put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size); |
|
492 | 534 |
} |
493 | 535 |
|
494 |
- if (s->is_mpeg2) |
|
495 |
- for(i=0;i<stuffing_size;i++) |
|
496 |
- put_byte(&ctx->pb, 0xff); |
|
536 |
+ if (pad_packet_bytes > 0) |
|
537 |
+ put_padding_packet(ctx,&ctx->pb, pad_packet_bytes); |
|
497 | 538 |
|
498 |
- /* output data */ |
|
499 |
- put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size); |
|
539 |
+ for(i=0;i<zero_trail_bytes;i++) |
|
540 |
+ put_byte(&ctx->pb, 0x00); |
|
541 |
+ |
|
500 | 542 |
put_flush_packet(&ctx->pb); |
501 | 543 |
|
502 | 544 |
s->packet_number++; |
... | ... |
@@ -505,6 +716,31 @@ static void flush_packet(AVFormatContext *ctx, int stream_index, |
505 | 505 |
stream->frame_start_offset = 0; |
506 | 506 |
} |
507 | 507 |
|
508 |
+static void put_vcd_padding_sector(AVFormatContext *ctx) |
|
509 |
+{ |
|
510 |
+ /* There are two ways to do this padding: writing a sector/pack |
|
511 |
+ of 0 values, or writing an MPEG padding pack. Both seem to |
|
512 |
+ work with most decoders, BUT the VCD standard only allows a 0-sector |
|
513 |
+ (see standard p. IV-4, IV-5). |
|
514 |
+ So a 0-sector it is...*/ |
|
515 |
+ |
|
516 |
+ MpegMuxContext *s = ctx->priv_data; |
|
517 |
+ int i; |
|
518 |
+ |
|
519 |
+ for(i=0;i<s->packet_size;i++) |
|
520 |
+ put_byte(&ctx->pb, 0); |
|
521 |
+ |
|
522 |
+ s->vcd_padding_bytes_written += s->packet_size; |
|
523 |
+ |
|
524 |
+ put_flush_packet(&ctx->pb); |
|
525 |
+ |
|
526 |
+ /* increasing the packet number is correct. The SCR of the following packs |
|
527 |
+ is calculated from the packet_number and it has to include the padding |
|
528 |
+ sector (it represents the sector index, not the MPEG pack index) |
|
529 |
+ (see VCD standard p. IV-6)*/ |
|
530 |
+ s->packet_number++; |
|
531 |
+} |
|
532 |
+ |
|
508 | 533 |
/* XXX: move that to upper layer */ |
509 | 534 |
/* XXX: we assume that there are always 'max_b_frames' between |
510 | 535 |
reference frames. A better solution would be to use the AVFrame pts |
... | ... |
@@ -549,6 +785,43 @@ static void compute_pts_dts(AVStream *st, int64_t *ppts, int64_t *pdts, |
549 | 549 |
*pdts = dts & ((1LL << 33) - 1); |
550 | 550 |
} |
551 | 551 |
|
552 |
+static int64_t update_scr(AVFormatContext *ctx,int stream_index,int64_t pts) |
|
553 |
+{ |
|
554 |
+ MpegMuxContext *s = ctx->priv_data; |
|
555 |
+ int64_t scr; |
|
556 |
+ |
|
557 |
+ if (s->is_vcd) |
|
558 |
+ /* Since the data delivery rate is constant, SCR is computed |
|
559 |
+ using the formula C + i * 1200 where C is the start constant |
|
560 |
+ and i is the pack index. |
|
561 |
+ It is recommended that SCR 0 is at the beginning of the VCD front |
|
562 |
+ margin (a sequence of empty Form 2 sectors on the CD). |
|
563 |
+ It is recommended that the front margin is 30 sectors long, so |
|
564 |
+ we use C = 30*1200 = 36000 |
|
565 |
+ (Note that even if the front margin is not 30 sectors the file |
|
566 |
+ will still be correct according to the standard. It just won't have |
|
567 |
+ the "recommended" value).*/ |
|
568 |
+ scr = 36000 + s->packet_number * 1200; |
|
569 |
+ else { |
|
570 |
+ /* XXX I believe this calculation of SCR is wrong. SCR |
|
571 |
+ specifies at which time the data should enter the decoder. |
|
572 |
+ Two packs cannot enter the decoder at the same time. */ |
|
573 |
+ |
|
574 |
+ /* XXX: system clock should be computed precisely, especially for |
|
575 |
+ CBR case. The current mode gives at least something coherent */ |
|
576 |
+ if (stream_index == s->scr_stream_index |
|
577 |
+ && pts != AV_NOPTS_VALUE) |
|
578 |
+ scr = pts; |
|
579 |
+ else |
|
580 |
+ scr = s->last_scr; |
|
581 |
+ } |
|
582 |
+ |
|
583 |
+ s->last_scr=scr; |
|
584 |
+ |
|
585 |
+ return scr; |
|
586 |
+} |
|
587 |
+ |
|
588 |
+ |
|
552 | 589 |
static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, |
553 | 590 |
const uint8_t *buf, int size, |
554 | 591 |
int64_t timestamp) |
... | ... |
@@ -558,15 +831,13 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, |
558 | 558 |
StreamInfo *stream = st->priv_data; |
559 | 559 |
int64_t pts, dts, new_start_pts, new_start_dts; |
560 | 560 |
int len, avail_size; |
561 |
- |
|
561 |
+ |
|
562 | 562 |
compute_pts_dts(st, &pts, &dts, timestamp); |
563 | 563 |
|
564 |
- /* XXX: system clock should be computed precisely, especially for |
|
565 |
- CBR case. The current mode gives at least something coherent */ |
|
566 |
- if (stream_index == s->scr_stream_index) |
|
567 |
- s->last_scr = pts; |
|
568 | 564 |
|
569 | 565 |
#if 0 |
566 |
+ update_scr(ctx,stream_index,pts); |
|
567 |
+ |
|
570 | 568 |
printf("%d: pts=%0.3f dts=%0.3f scr=%0.3f\n", |
571 | 569 |
stream_index, |
572 | 570 |
pts / 90000.0, |
... | ... |
@@ -586,10 +857,15 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, |
586 | 586 |
new_start_pts, |
587 | 587 |
new_start_dts); |
588 | 588 |
if (stream->buffer_ptr >= avail_size) { |
589 |
+ |
|
590 |
+ update_scr(ctx,stream_index,stream->start_pts); |
|
591 |
+ |
|
589 | 592 |
/* unlikely case: outputing the pts or dts increase the packet |
590 | 593 |
size so that we cannot write the start of the next |
591 | 594 |
packet. In this case, we must flush the current packet with |
592 |
- padding */ |
|
595 |
+ padding. |
|
596 |
+ Note: this always happens for the first audio and video packet |
|
597 |
+ in a VCD file, since they do not carry any data.*/ |
|
593 | 598 |
flush_packet(ctx, stream_index, |
594 | 599 |
stream->start_pts, stream->start_dts, s->last_scr); |
595 | 600 |
stream->buffer_ptr = 0; |
... | ... |
@@ -611,10 +887,23 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, |
611 | 611 |
buf += len; |
612 | 612 |
size -= len; |
613 | 613 |
if (stream->buffer_ptr >= avail_size) { |
614 |
+ |
|
615 |
+ update_scr(ctx,stream_index,stream->start_pts); |
|
616 |
+ |
|
614 | 617 |
/* if packet full, we send it now */ |
615 | 618 |
flush_packet(ctx, stream_index, |
616 | 619 |
stream->start_pts, stream->start_dts, s->last_scr); |
617 | 620 |
stream->buffer_ptr = 0; |
621 |
+ |
|
622 |
+ if (s->is_vcd) { |
|
623 |
+ /* Write one or more padding sectors, if necessary, to reach |
|
624 |
+ the constant overall bitrate.*/ |
|
625 |
+ int vcd_pad_bytes; |
|
626 |
+ |
|
627 |
+ while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size) |
|
628 |
+ put_vcd_padding_sector(ctx); |
|
629 |
+ } |
|
630 |
+ |
|
618 | 631 |
/* Make sure only the FIRST pes packet for this frame has |
619 | 632 |
a timestamp */ |
620 | 633 |
stream->start_pts = AV_NOPTS_VALUE; |
... | ... |
@@ -635,6 +924,8 @@ static int mpeg_mux_end(AVFormatContext *ctx) |
635 | 635 |
for(i=0;i<ctx->nb_streams;i++) { |
636 | 636 |
stream = ctx->streams[i]->priv_data; |
637 | 637 |
if (stream->buffer_ptr > 0) { |
638 |
+ update_scr(ctx,i,stream->start_pts); |
|
639 |
+ |
|
638 | 640 |
/* NOTE: we can always write the remaining data as it was |
639 | 641 |
tested before in mpeg_mux_write_packet() */ |
640 | 642 |
flush_packet(ctx, i, stream->start_pts, stream->start_dts, |
... | ... |
@@ -1139,7 +1430,7 @@ static int mpegps_read_seek(AVFormatContext *s, |
1139 | 1139 |
start_pos= pos; |
1140 | 1140 |
|
1141 | 1141 |
// read the next timestamp |
1142 |
- dts = mpegps_read_dts(s, stream_index, &pos, 1); |
|
1142 |
+ dts = mpegps_read_dts(s, stream_index, &pos, 1); |
|
1143 | 1143 |
if(pos == pos_max) |
1144 | 1144 |
no_change++; |
1145 | 1145 |
else |
... | ... |
@@ -1214,6 +1505,23 @@ static AVOutputFormat mpeg2vob_mux = { |
1214 | 1214 |
mpeg_mux_write_packet, |
1215 | 1215 |
mpeg_mux_end, |
1216 | 1216 |
}; |
1217 |
+ |
|
1218 |
+/* Same as mpeg2vob_mux except that the pack size is 2324 */ |
|
1219 |
+static AVOutputFormat mpeg2svcd_mux = { |
|
1220 |
+ "svcd", |
|
1221 |
+ "MPEG2 PS format (VOB)", |
|
1222 |
+ "video/mpeg", |
|
1223 |
+ "vob", |
|
1224 |
+ sizeof(MpegMuxContext), |
|
1225 |
+ CODEC_ID_MP2, |
|
1226 |
+ CODEC_ID_MPEG2VIDEO, |
|
1227 |
+ mpeg_mux_init, |
|
1228 |
+ mpeg_mux_write_packet, |
|
1229 |
+ mpeg_mux_end, |
|
1230 |
+}; |
|
1231 |
+ |
|
1232 |
+ |
|
1233 |
+ |
|
1217 | 1234 |
#endif //CONFIG_ENCODERS |
1218 | 1235 |
|
1219 | 1236 |
AVInputFormat mpegps_demux = { |
... | ... |
@@ -1233,6 +1541,7 @@ int mpegps_init(void) |
1233 | 1233 |
av_register_output_format(&mpeg1system_mux); |
1234 | 1234 |
av_register_output_format(&mpeg1vcd_mux); |
1235 | 1235 |
av_register_output_format(&mpeg2vob_mux); |
1236 |
+ av_register_output_format(&mpeg2svcd_mux); |
|
1236 | 1237 |
#endif //CONFIG_ENCODERS |
1237 | 1238 |
av_register_input_format(&mpegps_demux); |
1238 | 1239 |
return 0; |