Mercurial > libavformat.hg
comparison mpeg.c @ 366:cbcbaeff1f2c libavformat
improved VCD support patch by ("Hauke Duden" <H.NS.Duden at gmx dot net>)
- the first audio and video packs now contain only a system header and lots
of padding.
- no system headers in any packs other than the first ones
- the two system headers only contain information about "their" stream
- fixed some header values (muxrate, some flags, ...) so that they have the
values specified by the standard
- padding packs are inserted if the mux rate would be below 75 packs per
second (the rate must not be below or above that value).
- fixed the SCR of the packs
- 20 zero bytes are now inserted at the end of each audio pack, after the
data packet
author | michael |
---|---|
date | Thu, 19 Feb 2004 22:34:13 +0000 |
parents | f4f573c7dc56 |
children | 845f9de2c883 |
comparison
equal
deleted
inserted
replaced
365:4ae9fac22a5d | 366:cbcbaeff1f2c |
---|---|
48 /* stream info */ | 48 /* stream info */ |
49 int audio_bound; | 49 int audio_bound; |
50 int video_bound; | 50 int video_bound; |
51 int is_mpeg2; | 51 int is_mpeg2; |
52 int is_vcd; | 52 int is_vcd; |
53 int is_svcd; | |
53 int scr_stream_index; /* stream from which the system clock is | 54 int scr_stream_index; /* stream from which the system clock is |
54 computed (VBR case) */ | 55 computed (VBR case) */ |
55 int64_t last_scr; /* current system clock */ | 56 int64_t last_scr; /* current system clock */ |
57 | |
58 double vcd_padding_bitrate; | |
59 int64_t vcd_padding_bytes_written; | |
60 | |
56 } MpegMuxContext; | 61 } MpegMuxContext; |
57 | 62 |
58 #define PACK_START_CODE ((unsigned int)0x000001ba) | 63 #define PACK_START_CODE ((unsigned int)0x000001ba) |
59 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb) | 64 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb) |
60 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7) | 65 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7) |
78 | 83 |
79 #ifdef CONFIG_ENCODERS | 84 #ifdef CONFIG_ENCODERS |
80 extern AVOutputFormat mpeg1system_mux; | 85 extern AVOutputFormat mpeg1system_mux; |
81 extern AVOutputFormat mpeg1vcd_mux; | 86 extern AVOutputFormat mpeg1vcd_mux; |
82 extern AVOutputFormat mpeg2vob_mux; | 87 extern AVOutputFormat mpeg2vob_mux; |
88 extern AVOutputFormat mpeg2svcd_mux; | |
83 | 89 |
84 static int put_pack_header(AVFormatContext *ctx, | 90 static int put_pack_header(AVFormatContext *ctx, |
85 uint8_t *buf, int64_t timestamp) | 91 uint8_t *buf, int64_t timestamp) |
86 { | 92 { |
87 MpegMuxContext *s = ctx->priv_data; | 93 MpegMuxContext *s = ctx->priv_data; |
115 } | 121 } |
116 flush_put_bits(&pb); | 122 flush_put_bits(&pb); |
117 return pbBufPtr(&pb) - pb.buf; | 123 return pbBufPtr(&pb) - pb.buf; |
118 } | 124 } |
119 | 125 |
120 static int put_system_header(AVFormatContext *ctx, uint8_t *buf) | 126 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id) |
121 { | 127 { |
122 MpegMuxContext *s = ctx->priv_data; | 128 MpegMuxContext *s = ctx->priv_data; |
123 int size, rate_bound, i, private_stream_coded, id; | 129 int size, rate_bound, i, private_stream_coded, id; |
124 PutBitContext pb; | 130 PutBitContext pb; |
125 | 131 |
130 put_bits(&pb, 1, 1); | 136 put_bits(&pb, 1, 1); |
131 | 137 |
132 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */ | 138 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */ |
133 put_bits(&pb, 22, rate_bound); | 139 put_bits(&pb, 22, rate_bound); |
134 put_bits(&pb, 1, 1); /* marker */ | 140 put_bits(&pb, 1, 1); /* marker */ |
135 put_bits(&pb, 6, s->audio_bound); | 141 if (s->is_vcd && only_for_stream_id==VIDEO_ID) { |
136 | 142 /* This header applies only to the video stream (see VCD standard p. IV-7)*/ |
137 put_bits(&pb, 1, 1); /* variable bitrate */ | 143 put_bits(&pb, 6, 0); |
144 } else | |
145 put_bits(&pb, 6, s->audio_bound); | |
146 | |
147 if (s->is_vcd) | |
148 put_bits(&pb, 1, 0); /* see VCD standard, p. IV-7*/ | |
149 else | |
150 put_bits(&pb, 1, 1); /* variable bitrate*/ | |
138 put_bits(&pb, 1, 1); /* non constrainted bit stream */ | 151 put_bits(&pb, 1, 1); /* non constrainted bit stream */ |
139 | 152 |
140 put_bits(&pb, 1, 0); /* audio locked */ | 153 if (s->is_vcd) { |
141 put_bits(&pb, 1, 0); /* video locked */ | 154 /* see VCD standard p IV-7 */ |
155 put_bits(&pb, 1, 1); /* audio locked */ | |
156 put_bits(&pb, 1, 1); /* video locked */ | |
157 } else { | |
158 put_bits(&pb, 1, 0); /* audio locked */ | |
159 put_bits(&pb, 1, 0); /* video locked */ | |
160 } | |
161 | |
142 put_bits(&pb, 1, 1); /* marker */ | 162 put_bits(&pb, 1, 1); /* marker */ |
143 | 163 |
144 put_bits(&pb, 5, s->video_bound); | 164 if (s->is_vcd && only_for_stream_id==AUDIO_ID) { |
165 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/ | |
166 put_bits(&pb, 5, 0); | |
167 } else | |
168 put_bits(&pb, 5, s->video_bound); | |
169 | |
145 put_bits(&pb, 8, 0xff); /* reserved byte */ | 170 put_bits(&pb, 8, 0xff); /* reserved byte */ |
146 | 171 |
147 /* audio stream info */ | 172 /* audio stream info */ |
148 private_stream_coded = 0; | 173 private_stream_coded = 0; |
149 for(i=0;i<ctx->nb_streams;i++) { | 174 for(i=0;i<ctx->nb_streams;i++) { |
150 StreamInfo *stream = ctx->streams[i]->priv_data; | 175 StreamInfo *stream = ctx->streams[i]->priv_data; |
151 id = stream->id; | 176 |
152 if (id < 0xc0) { | 177 /* For VCDs, only include the stream info for the stream |
153 /* special case for private streams (AC3 use that) */ | 178 that the pack which contains this system belongs to. |
154 if (private_stream_coded) | 179 (see VCD standard p. IV-7) */ |
155 continue; | 180 if ( !s->is_vcd || stream->id==only_for_stream_id |
156 private_stream_coded = 1; | 181 || only_for_stream_id==0) { |
157 id = 0xbd; | 182 |
158 } | 183 id = stream->id; |
159 put_bits(&pb, 8, id); /* stream ID */ | 184 if (id < 0xc0) { |
160 put_bits(&pb, 2, 3); | 185 /* special case for private streams (AC3 use that) */ |
161 if (id < 0xe0) { | 186 if (private_stream_coded) |
162 /* audio */ | 187 continue; |
163 put_bits(&pb, 1, 0); | 188 private_stream_coded = 1; |
164 put_bits(&pb, 13, stream->max_buffer_size / 128); | 189 id = 0xbd; |
165 } else { | 190 } |
166 /* video */ | 191 put_bits(&pb, 8, id); /* stream ID */ |
167 put_bits(&pb, 1, 1); | 192 put_bits(&pb, 2, 3); |
168 put_bits(&pb, 13, stream->max_buffer_size / 1024); | 193 if (id < 0xe0) { |
194 /* audio */ | |
195 put_bits(&pb, 1, 0); | |
196 put_bits(&pb, 13, stream->max_buffer_size / 128); | |
197 } else { | |
198 /* video */ | |
199 put_bits(&pb, 1, 1); | |
200 put_bits(&pb, 13, stream->max_buffer_size / 1024); | |
201 } | |
169 } | 202 } |
170 } | 203 } |
171 flush_put_bits(&pb); | 204 flush_put_bits(&pb); |
172 size = pbBufPtr(&pb) - pb.buf; | 205 size = pbBufPtr(&pb) - pb.buf; |
173 /* patch packet size */ | 206 /* patch packet size */ |
200 { | 233 { |
201 MpegMuxContext *s = ctx->priv_data; | 234 MpegMuxContext *s = ctx->priv_data; |
202 int bitrate, i, mpa_id, mpv_id, ac3_id, lpcm_id, j; | 235 int bitrate, i, mpa_id, mpv_id, ac3_id, lpcm_id, j; |
203 AVStream *st; | 236 AVStream *st; |
204 StreamInfo *stream; | 237 StreamInfo *stream; |
238 int audio_bitrate; | |
239 int video_bitrate; | |
205 | 240 |
206 s->packet_number = 0; | 241 s->packet_number = 0; |
207 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux); | 242 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux); |
208 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux); | 243 s->is_svcd = (ctx->oformat == &mpeg2svcd_mux); |
209 | 244 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux || ctx->oformat == &mpeg2svcd_mux); |
210 if (s->is_vcd) | 245 |
211 s->packet_size = 2324; /* VCD packet size */ | 246 if (s->is_vcd || s->is_svcd) |
247 s->packet_size = 2324; /* VCD/SVCD packet size */ | |
212 else | 248 else |
213 s->packet_size = 2048; | 249 s->packet_size = 2048; |
250 | |
251 s->vcd_padding_bytes_written = 0; | |
252 s->vcd_padding_bitrate=0; | |
214 | 253 |
215 s->audio_bound = 0; | 254 s->audio_bound = 0; |
216 s->video_bound = 0; | 255 s->video_bound = 0; |
217 mpa_id = AUDIO_ID; | 256 mpa_id = AUDIO_ID; |
218 ac3_id = AC3_ID; | 257 ac3_id = AC3_ID; |
264 } | 303 } |
265 /* if no SCR, use first stream (audio) */ | 304 /* if no SCR, use first stream (audio) */ |
266 if (s->scr_stream_index == -1) | 305 if (s->scr_stream_index == -1) |
267 s->scr_stream_index = 0; | 306 s->scr_stream_index = 0; |
268 | 307 |
269 /* we increase slightly the bitrate to take into account the | 308 bitrate = 0; |
270 headers. XXX: compute it exactly */ | 309 audio_bitrate = 0; |
271 bitrate = 2000; | 310 video_bitrate = 0; |
272 for(i=0;i<ctx->nb_streams;i++) { | 311 for(i=0;i<ctx->nb_streams;i++) { |
273 st = ctx->streams[i]; | 312 st = ctx->streams[i]; |
313 stream = (StreamInfo*) st->priv_data; | |
314 | |
274 bitrate += st->codec.bit_rate; | 315 bitrate += st->codec.bit_rate; |
275 } | 316 |
276 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); | 317 if (stream->id==AUDIO_ID) |
318 audio_bitrate += st->codec.bit_rate; | |
319 else if (stream->id==VIDEO_ID) | |
320 video_bitrate += st->codec.bit_rate; | |
321 } | |
322 | |
323 if (s->is_vcd) { | |
324 double overhead_rate; | |
325 | |
326 /* The VCD standard mandates that the mux_rate field is 3528 | |
327 (see standard p. IV-6). | |
328 The value is actually "wrong", i.e. if you calculate | |
329 it using the normal formula and the 75 sectors per second transfer | |
330 rate you get a different value because the real pack size is 2324, | |
331 not 2352. But the standard explicitly specifies that the mux_rate | |
332 field in the header must have this value.*/ | |
333 s->mux_rate=2352 * 75 / 50; /* = 3528*/ | |
334 | |
335 /* The VCD standard states that the muxed stream must be | |
336 exactly 75 packs / second (the data rate of a single speed cdrom). | |
337 Since the video bitrate (probably 1150000 bits/sec) will be below | |
338 the theoretical maximum we have to add some padding packets | |
339 to make up for the lower data rate. | |
340 (cf. VCD standard p. IV-6 )*/ | |
341 | |
342 /* Add the header overhead to the data rate. | |
343 2279 data bytes per audio pack, 2294 data bytes per video pack*/ | |
344 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279); | |
345 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294); | |
346 overhead_rate *= 8; | |
347 | |
348 /* Add padding so that the full bitrate is 2324*75 bytes/sec */ | |
349 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate); | |
350 | |
351 } else { | |
352 /* we increase slightly the bitrate to take into account the | |
353 headers. XXX: compute it exactly */ | |
354 bitrate += 2000; | |
355 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); | |
356 } | |
277 | 357 |
278 if (s->is_vcd || s->is_mpeg2) | 358 if (s->is_vcd || s->is_mpeg2) |
279 /* every packet */ | 359 /* every packet */ |
280 s->pack_header_freq = 1; | 360 s->pack_header_freq = 1; |
281 else | 361 else |
288 | 368 |
289 if (s->is_mpeg2) | 369 if (s->is_mpeg2) |
290 /* every 200 packets. Need to look at the spec. */ | 370 /* every 200 packets. Need to look at the spec. */ |
291 s->system_header_freq = s->pack_header_freq * 40; | 371 s->system_header_freq = s->pack_header_freq * 40; |
292 else if (s->is_vcd) | 372 else if (s->is_vcd) |
293 /* every 40 packets, this is my invention */ | 373 /* the standard mandates that there are only two system headers |
294 s->system_header_freq = s->pack_header_freq * 40; | 374 in the whole file: one in the first packet of each stream. |
375 (see standard p. IV-7 and IV-8) */ | |
376 s->system_header_freq = 0x7fffffff; | |
295 else | 377 else |
296 s->system_header_freq = s->pack_header_freq * 5; | 378 s->system_header_freq = s->pack_header_freq * 5; |
297 | 379 |
298 for(i=0;i<ctx->nb_streams;i++) { | 380 for(i=0;i<ctx->nb_streams;i++) { |
299 stream = ctx->streams[i]->priv_data; | 381 stream = ctx->streams[i]->priv_data; |
321 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1)); | 403 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1)); |
322 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1)); | 404 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1)); |
323 } | 405 } |
324 | 406 |
325 | 407 |
408 /* return the number of padding bytes that should be inserted into | |
409 the multiplexed stream.*/ | |
410 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts) | |
411 { | |
412 MpegMuxContext *s = ctx->priv_data; | |
413 int pad_bytes = 0; | |
414 | |
415 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE) | |
416 { | |
417 int64_t full_pad_bytes; | |
418 | |
419 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); | |
420 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written); | |
421 | |
422 if (pad_bytes<0) | |
423 /* might happen if we have already padded to a later timestamp. This | |
424 can occur if another stream has already advanced further.*/ | |
425 pad_bytes=0; | |
426 } | |
427 | |
428 return pad_bytes; | |
429 } | |
430 | |
431 | |
326 /* return the exact available payload size for the next packet for | 432 /* return the exact available payload size for the next packet for |
327 stream 'stream_index'. 'pts' and 'dts' are only used to know if | 433 stream 'stream_index'. 'pts' and 'dts' are only used to know if |
328 timestamps are needed in the packet header. */ | 434 timestamps are needed in the packet header. */ |
329 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index, | 435 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index, |
330 int64_t pts, int64_t dts) | 436 int64_t pts, int64_t dts) |
331 { | 437 { |
332 MpegMuxContext *s = ctx->priv_data; | 438 MpegMuxContext *s = ctx->priv_data; |
333 int buf_index; | 439 int buf_index; |
334 StreamInfo *stream; | 440 StreamInfo *stream; |
441 | |
442 stream = ctx->streams[stream_index]->priv_data; | |
335 | 443 |
336 buf_index = 0; | 444 buf_index = 0; |
337 if (((s->packet_number % s->pack_header_freq) == 0)) { | 445 if (((s->packet_number % s->pack_header_freq) == 0)) { |
338 /* pack header size */ | 446 /* pack header size */ |
339 if (s->is_mpeg2) | 447 if (s->is_mpeg2) |
340 buf_index += 14; | 448 buf_index += 14; |
341 else | 449 else |
342 buf_index += 12; | 450 buf_index += 12; |
343 if ((s->packet_number % s->system_header_freq) == 0) | 451 |
344 buf_index += s->system_header_size; | 452 if (s->is_vcd) { |
345 } | 453 /* there is exactly one system header for each stream in a VCD MPEG, |
346 | 454 One in the very first video packet and one in the very first |
347 /* packet header size */ | 455 audio packet (see VCD standard p. IV-7 and IV-8).*/ |
348 buf_index += 6; | 456 |
349 if (s->is_mpeg2) | 457 if (stream->packet_number==0) |
350 buf_index += 3; | 458 /* The system headers refer only to the stream they occur in, |
351 if (pts != AV_NOPTS_VALUE) { | 459 so they have a constant size.*/ |
352 if (dts != pts) | 460 buf_index += 15; |
353 buf_index += 5 + 5; | 461 |
354 else | 462 } else { |
355 buf_index += 5; | 463 if ((s->packet_number % s->system_header_freq) == 0) |
356 } else { | 464 buf_index += s->system_header_size; |
357 if (!s->is_mpeg2) | 465 } |
358 buf_index++; | 466 } |
359 } | 467 |
360 | 468 if (s->is_vcd && stream->packet_number==0) |
361 stream = ctx->streams[stream_index]->priv_data; | 469 /* the first pack of each stream contains only the pack header, |
362 if (stream->id < 0xc0) { | 470 the system header and some padding (see VCD standard p. IV-6) |
363 /* AC3/LPCM private data header */ | 471 Add the padding size, so that the actual payload becomes 0.*/ |
364 buf_index += 4; | 472 buf_index += s->packet_size - buf_index; |
365 if (stream->id >= 0xa0) { | 473 else { |
366 int n; | 474 /* packet header size */ |
475 buf_index += 6; | |
476 if (s->is_mpeg2) | |
367 buf_index += 3; | 477 buf_index += 3; |
368 /* NOTE: we round the payload size to an integer number of | 478 if (pts != AV_NOPTS_VALUE) { |
369 LPCM samples */ | 479 if (dts != pts) |
370 n = (s->packet_size - buf_index) % stream->lpcm_align; | 480 buf_index += 5 + 5; |
371 if (n) | 481 else |
372 buf_index += (stream->lpcm_align - n); | 482 buf_index += 5; |
373 } | 483 |
484 } else { | |
485 if (!s->is_mpeg2) | |
486 buf_index++; | |
487 } | |
488 | |
489 if (stream->id < 0xc0) { | |
490 /* AC3/LPCM private data header */ | |
491 buf_index += 4; | |
492 if (stream->id >= 0xa0) { | |
493 int n; | |
494 buf_index += 3; | |
495 /* NOTE: we round the payload size to an integer number of | |
496 LPCM samples */ | |
497 n = (s->packet_size - buf_index) % stream->lpcm_align; | |
498 if (n) | |
499 buf_index += (stream->lpcm_align - n); | |
500 } | |
501 } | |
502 | |
503 if (s->is_vcd && stream->id == AUDIO_ID) | |
504 /* The VCD standard demands that 20 zero bytes follow | |
505 each audio packet (see standard p. IV-8).*/ | |
506 buf_index+=20; | |
374 } | 507 } |
375 return s->packet_size - buf_index; | 508 return s->packet_size - buf_index; |
376 } | 509 } |
510 | |
511 /* Write an MPEG padding packet header. */ | |
512 static int put_padding_header(AVFormatContext *ctx,uint8_t* buf, int full_padding_size) | |
513 { | |
514 MpegMuxContext *s = ctx->priv_data; | |
515 int size = full_padding_size - 6; /* subtract header length */ | |
516 | |
517 buf[0] = (uint8_t)(PADDING_STREAM >> 24); | |
518 buf[1] = (uint8_t)(PADDING_STREAM >> 16); | |
519 buf[2] = (uint8_t)(PADDING_STREAM >> 8); | |
520 buf[3] = (uint8_t)(PADDING_STREAM); | |
521 buf[4] = (uint8_t)(size >> 8); | |
522 buf[5] = (uint8_t)(size & 0xff); | |
523 | |
524 if (!s->is_mpeg2) { | |
525 buf[6] = 0x0f; | |
526 return 7; | |
527 } else | |
528 return 6; | |
529 } | |
530 | |
531 static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes) | |
532 { | |
533 uint8_t buffer[7]; | |
534 int size, i; | |
535 | |
536 size = put_padding_header(ctx,buffer, packet_bytes); | |
537 put_buffer(pb, buffer, size); | |
538 packet_bytes -= size; | |
539 | |
540 for(i=0;i<packet_bytes;i++) | |
541 put_byte(pb, 0xff); | |
542 } | |
543 | |
377 | 544 |
378 /* flush the packet on stream stream_index */ | 545 /* flush the packet on stream stream_index */ |
379 static void flush_packet(AVFormatContext *ctx, int stream_index, | 546 static void flush_packet(AVFormatContext *ctx, int stream_index, |
380 int64_t pts, int64_t dts, int64_t scr) | 547 int64_t pts, int64_t dts, int64_t scr) |
381 { | 548 { |
383 StreamInfo *stream = ctx->streams[stream_index]->priv_data; | 550 StreamInfo *stream = ctx->streams[stream_index]->priv_data; |
384 uint8_t *buf_ptr; | 551 uint8_t *buf_ptr; |
385 int size, payload_size, startcode, id, stuffing_size, i, header_len; | 552 int size, payload_size, startcode, id, stuffing_size, i, header_len; |
386 int packet_size; | 553 int packet_size; |
387 uint8_t buffer[128]; | 554 uint8_t buffer[128]; |
555 int zero_trail_bytes = 0; | |
556 int pad_packet_bytes = 0; | |
388 | 557 |
389 id = stream->id; | 558 id = stream->id; |
390 | 559 |
391 #if 0 | 560 #if 0 |
392 printf("packet ID=%2x PTS=%0.3f\n", | 561 printf("packet ID=%2x PTS=%0.3f\n", |
393 id, pts / 90000.0); | 562 id, pts / 90000.0); |
394 #endif | 563 #endif |
395 | 564 |
396 buf_ptr = buffer; | 565 buf_ptr = buffer; |
566 | |
397 if (((s->packet_number % s->pack_header_freq) == 0)) { | 567 if (((s->packet_number % s->pack_header_freq) == 0)) { |
398 /* output pack and systems header if needed */ | 568 /* output pack and systems header if needed */ |
399 size = put_pack_header(ctx, buf_ptr, scr); | 569 size = put_pack_header(ctx, buf_ptr, scr); |
400 buf_ptr += size; | 570 buf_ptr += size; |
401 if ((s->packet_number % s->system_header_freq) == 0) { | 571 |
402 size = put_system_header(ctx, buf_ptr); | 572 if (s->is_vcd) { |
403 buf_ptr += size; | 573 /* there is exactly one system header for each stream in a VCD MPEG, |
574 One in the very first video packet and one in the very first | |
575 audio packet (see VCD standard p. IV-7 and IV-8).*/ | |
576 | |
577 if (stream->packet_number==0) { | |
578 size = put_system_header(ctx, buf_ptr, id); | |
579 buf_ptr += size; | |
580 } | |
581 } else { | |
582 if ((s->packet_number % s->system_header_freq) == 0) { | |
583 size = put_system_header(ctx, buf_ptr, 0); | |
584 buf_ptr += size; | |
585 } | |
404 } | 586 } |
405 } | 587 } |
406 size = buf_ptr - buffer; | 588 size = buf_ptr - buffer; |
407 put_buffer(&ctx->pb, buffer, size); | 589 put_buffer(&ctx->pb, buffer, size); |
408 | 590 |
409 /* packet header */ | 591 packet_size = s->packet_size - size; |
410 if (s->is_mpeg2) { | 592 |
411 header_len = 3; | 593 if (s->is_vcd && id == AUDIO_ID) |
412 } else { | 594 /* The VCD standard demands that 20 zero bytes follow |
413 header_len = 0; | 595 each audio pack (see standard p. IV-8).*/ |
414 } | 596 zero_trail_bytes += 20; |
415 if (pts != AV_NOPTS_VALUE) { | 597 |
416 if (dts != pts) | 598 if (s->is_vcd && stream->packet_number==0) { |
417 header_len += 5 + 5; | 599 /* the first pack of each stream contains only the pack header, |
418 else | 600 the system header and lots of padding (see VCD standard p. IV-6). |
419 header_len += 5; | 601 In the case of an audio pack, 20 zero bytes are also added at |
420 } else { | 602 the end.*/ |
603 pad_packet_bytes = packet_size - zero_trail_bytes; | |
604 } | |
605 | |
606 packet_size -= pad_packet_bytes + zero_trail_bytes; | |
607 | |
608 if (packet_size > 0) { | |
609 | |
610 /* packet header size */ | |
611 packet_size -= 6; | |
612 | |
613 /* packet header */ | |
614 if (s->is_mpeg2) { | |
615 header_len = 3; | |
616 } else { | |
617 header_len = 0; | |
618 } | |
619 if (pts != AV_NOPTS_VALUE) { | |
620 if (dts != pts) | |
621 header_len += 5 + 5; | |
622 else | |
623 header_len += 5; | |
624 } else { | |
625 if (!s->is_mpeg2) | |
626 header_len++; | |
627 } | |
628 | |
629 payload_size = packet_size - header_len; | |
630 if (id < 0xc0) { | |
631 startcode = PRIVATE_STREAM_1; | |
632 payload_size -= 4; | |
633 if (id >= 0xa0) | |
634 payload_size -= 3; | |
635 } else { | |
636 startcode = 0x100 + id; | |
637 } | |
638 | |
639 stuffing_size = payload_size - stream->buffer_ptr; | |
640 if (stuffing_size < 0) | |
641 stuffing_size = 0; | |
642 put_be32(&ctx->pb, startcode); | |
643 | |
644 put_be16(&ctx->pb, packet_size); | |
645 | |
421 if (!s->is_mpeg2) | 646 if (!s->is_mpeg2) |
422 header_len++; | 647 for(i=0;i<stuffing_size;i++) |
423 } | 648 put_byte(&ctx->pb, 0xff); |
424 | 649 |
425 packet_size = s->packet_size - (size + 6); | 650 if (s->is_mpeg2) { |
426 payload_size = packet_size - header_len; | 651 put_byte(&ctx->pb, 0x80); /* mpeg2 id */ |
427 if (id < 0xc0) { | 652 |
428 startcode = PRIVATE_STREAM_1; | 653 if (pts != AV_NOPTS_VALUE) { |
429 payload_size -= 4; | 654 if (dts != pts) { |
430 if (id >= 0xa0) | 655 put_byte(&ctx->pb, 0xc0); /* flags */ |
431 payload_size -= 3; | 656 put_byte(&ctx->pb, header_len - 3 + stuffing_size); |
432 } else { | 657 put_timestamp(&ctx->pb, 0x03, pts); |
433 startcode = 0x100 + id; | 658 put_timestamp(&ctx->pb, 0x01, dts); |
434 } | 659 } else { |
435 | 660 put_byte(&ctx->pb, 0x80); /* flags */ |
436 stuffing_size = payload_size - stream->buffer_ptr; | 661 put_byte(&ctx->pb, header_len - 3 + stuffing_size); |
437 if (stuffing_size < 0) | 662 put_timestamp(&ctx->pb, 0x02, pts); |
438 stuffing_size = 0; | 663 } |
439 put_be32(&ctx->pb, startcode); | 664 } else { |
440 | 665 put_byte(&ctx->pb, 0x00); /* flags */ |
441 put_be16(&ctx->pb, packet_size); | |
442 | |
443 if (!s->is_mpeg2) | |
444 for(i=0;i<stuffing_size;i++) | |
445 put_byte(&ctx->pb, 0xff); | |
446 | |
447 if (s->is_mpeg2) { | |
448 put_byte(&ctx->pb, 0x80); /* mpeg2 id */ | |
449 | |
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); | 666 put_byte(&ctx->pb, header_len - 3 + stuffing_size); |
454 put_timestamp(&ctx->pb, 0x03, pts); | |
455 put_timestamp(&ctx->pb, 0x01, dts); | |
456 } else { | |
457 put_byte(&ctx->pb, 0x80); /* flags */ | |
458 put_byte(&ctx->pb, header_len - 3 + stuffing_size); | |
459 put_timestamp(&ctx->pb, 0x02, pts); | |
460 } | 667 } |
461 } else { | 668 } else { |
462 put_byte(&ctx->pb, 0x00); /* flags */ | 669 if (pts != AV_NOPTS_VALUE) { |
463 put_byte(&ctx->pb, header_len - 3 + stuffing_size); | 670 if (dts != pts) { |
464 } | 671 put_timestamp(&ctx->pb, 0x03, pts); |
465 } else { | 672 put_timestamp(&ctx->pb, 0x01, dts); |
466 if (pts != AV_NOPTS_VALUE) { | 673 } else { |
467 if (dts != pts) { | 674 put_timestamp(&ctx->pb, 0x02, pts); |
468 put_timestamp(&ctx->pb, 0x03, pts); | 675 } |
469 put_timestamp(&ctx->pb, 0x01, dts); | |
470 } else { | 676 } else { |
471 put_timestamp(&ctx->pb, 0x02, pts); | 677 put_byte(&ctx->pb, 0x0f); |
472 } | 678 } |
473 } else { | 679 } |
474 put_byte(&ctx->pb, 0x0f); | 680 |
475 } | 681 if (startcode == PRIVATE_STREAM_1) { |
476 } | 682 put_byte(&ctx->pb, id); |
477 | 683 if (id >= 0xa0) { |
478 if (startcode == PRIVATE_STREAM_1) { | 684 /* LPCM (XXX: check nb_frames) */ |
479 put_byte(&ctx->pb, id); | 685 put_byte(&ctx->pb, 7); |
480 if (id >= 0xa0) { | 686 put_be16(&ctx->pb, 4); /* skip 3 header bytes */ |
481 /* LPCM (XXX: check nb_frames) */ | 687 put_byte(&ctx->pb, stream->lpcm_header[0]); |
482 put_byte(&ctx->pb, 7); | 688 put_byte(&ctx->pb, stream->lpcm_header[1]); |
483 put_be16(&ctx->pb, 4); /* skip 3 header bytes */ | 689 put_byte(&ctx->pb, stream->lpcm_header[2]); |
484 put_byte(&ctx->pb, stream->lpcm_header[0]); | 690 } else { |
485 put_byte(&ctx->pb, stream->lpcm_header[1]); | 691 /* AC3 */ |
486 put_byte(&ctx->pb, stream->lpcm_header[2]); | 692 put_byte(&ctx->pb, stream->nb_frames); |
487 } else { | 693 put_be16(&ctx->pb, stream->frame_start_offset); |
488 /* AC3 */ | 694 } |
489 put_byte(&ctx->pb, stream->nb_frames); | 695 } |
490 put_be16(&ctx->pb, stream->frame_start_offset); | 696 |
491 } | 697 if (s->is_mpeg2) |
492 } | 698 for(i=0;i<stuffing_size;i++) |
493 | 699 put_byte(&ctx->pb, 0xff); |
494 if (s->is_mpeg2) | 700 |
495 for(i=0;i<stuffing_size;i++) | 701 /* output data */ |
496 put_byte(&ctx->pb, 0xff); | 702 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size); |
497 | 703 } |
498 /* output data */ | 704 |
499 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size); | 705 if (pad_packet_bytes > 0) |
706 put_padding_packet(ctx,&ctx->pb, pad_packet_bytes); | |
707 | |
708 for(i=0;i<zero_trail_bytes;i++) | |
709 put_byte(&ctx->pb, 0x00); | |
710 | |
500 put_flush_packet(&ctx->pb); | 711 put_flush_packet(&ctx->pb); |
501 | 712 |
502 s->packet_number++; | 713 s->packet_number++; |
503 stream->packet_number++; | 714 stream->packet_number++; |
504 stream->nb_frames = 0; | 715 stream->nb_frames = 0; |
505 stream->frame_start_offset = 0; | 716 stream->frame_start_offset = 0; |
717 } | |
718 | |
719 static void put_vcd_padding_sector(AVFormatContext *ctx) | |
720 { | |
721 /* There are two ways to do this padding: writing a sector/pack | |
722 of 0 values, or writing an MPEG padding pack. Both seem to | |
723 work with most decoders, BUT the VCD standard only allows a 0-sector | |
724 (see standard p. IV-4, IV-5). | |
725 So a 0-sector it is...*/ | |
726 | |
727 MpegMuxContext *s = ctx->priv_data; | |
728 int i; | |
729 | |
730 for(i=0;i<s->packet_size;i++) | |
731 put_byte(&ctx->pb, 0); | |
732 | |
733 s->vcd_padding_bytes_written += s->packet_size; | |
734 | |
735 put_flush_packet(&ctx->pb); | |
736 | |
737 /* increasing the packet number is correct. The SCR of the following packs | |
738 is calculated from the packet_number and it has to include the padding | |
739 sector (it represents the sector index, not the MPEG pack index) | |
740 (see VCD standard p. IV-6)*/ | |
741 s->packet_number++; | |
506 } | 742 } |
507 | 743 |
508 /* XXX: move that to upper layer */ | 744 /* XXX: move that to upper layer */ |
509 /* XXX: we assume that there are always 'max_b_frames' between | 745 /* XXX: we assume that there are always 'max_b_frames' between |
510 reference frames. A better solution would be to use the AVFrame pts | 746 reference frames. A better solution would be to use the AVFrame pts |
547 } | 783 } |
548 *ppts = pts & ((1LL << 33) - 1); | 784 *ppts = pts & ((1LL << 33) - 1); |
549 *pdts = dts & ((1LL << 33) - 1); | 785 *pdts = dts & ((1LL << 33) - 1); |
550 } | 786 } |
551 | 787 |
788 static int64_t update_scr(AVFormatContext *ctx,int stream_index,int64_t pts) | |
789 { | |
790 MpegMuxContext *s = ctx->priv_data; | |
791 int64_t scr; | |
792 | |
793 if (s->is_vcd) | |
794 /* Since the data delivery rate is constant, SCR is computed | |
795 using the formula C + i * 1200 where C is the start constant | |
796 and i is the pack index. | |
797 It is recommended that SCR 0 is at the beginning of the VCD front | |
798 margin (a sequence of empty Form 2 sectors on the CD). | |
799 It is recommended that the front margin is 30 sectors long, so | |
800 we use C = 30*1200 = 36000 | |
801 (Note that even if the front margin is not 30 sectors the file | |
802 will still be correct according to the standard. It just won't have | |
803 the "recommended" value).*/ | |
804 scr = 36000 + s->packet_number * 1200; | |
805 else { | |
806 /* XXX I believe this calculation of SCR is wrong. SCR | |
807 specifies at which time the data should enter the decoder. | |
808 Two packs cannot enter the decoder at the same time. */ | |
809 | |
810 /* XXX: system clock should be computed precisely, especially for | |
811 CBR case. The current mode gives at least something coherent */ | |
812 if (stream_index == s->scr_stream_index | |
813 && pts != AV_NOPTS_VALUE) | |
814 scr = pts; | |
815 else | |
816 scr = s->last_scr; | |
817 } | |
818 | |
819 s->last_scr=scr; | |
820 | |
821 return scr; | |
822 } | |
823 | |
824 | |
552 static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, | 825 static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, |
553 const uint8_t *buf, int size, | 826 const uint8_t *buf, int size, |
554 int64_t timestamp) | 827 int64_t timestamp) |
555 { | 828 { |
556 MpegMuxContext *s = ctx->priv_data; | 829 MpegMuxContext *s = ctx->priv_data; |
557 AVStream *st = ctx->streams[stream_index]; | 830 AVStream *st = ctx->streams[stream_index]; |
558 StreamInfo *stream = st->priv_data; | 831 StreamInfo *stream = st->priv_data; |
559 int64_t pts, dts, new_start_pts, new_start_dts; | 832 int64_t pts, dts, new_start_pts, new_start_dts; |
560 int len, avail_size; | 833 int len, avail_size; |
561 | 834 |
562 compute_pts_dts(st, &pts, &dts, timestamp); | 835 compute_pts_dts(st, &pts, &dts, timestamp); |
563 | 836 |
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 | 837 |
569 #if 0 | 838 #if 0 |
839 update_scr(ctx,stream_index,pts); | |
840 | |
570 printf("%d: pts=%0.3f dts=%0.3f scr=%0.3f\n", | 841 printf("%d: pts=%0.3f dts=%0.3f scr=%0.3f\n", |
571 stream_index, | 842 stream_index, |
572 pts / 90000.0, | 843 pts / 90000.0, |
573 dts / 90000.0, | 844 dts / 90000.0, |
574 s->last_scr / 90000.0); | 845 s->last_scr / 90000.0); |
584 } | 855 } |
585 avail_size = get_packet_payload_size(ctx, stream_index, | 856 avail_size = get_packet_payload_size(ctx, stream_index, |
586 new_start_pts, | 857 new_start_pts, |
587 new_start_dts); | 858 new_start_dts); |
588 if (stream->buffer_ptr >= avail_size) { | 859 if (stream->buffer_ptr >= avail_size) { |
860 | |
861 update_scr(ctx,stream_index,stream->start_pts); | |
862 | |
589 /* unlikely case: outputing the pts or dts increase the packet | 863 /* unlikely case: outputing the pts or dts increase the packet |
590 size so that we cannot write the start of the next | 864 size so that we cannot write the start of the next |
591 packet. In this case, we must flush the current packet with | 865 packet. In this case, we must flush the current packet with |
592 padding */ | 866 padding. |
867 Note: this always happens for the first audio and video packet | |
868 in a VCD file, since they do not carry any data.*/ | |
593 flush_packet(ctx, stream_index, | 869 flush_packet(ctx, stream_index, |
594 stream->start_pts, stream->start_dts, s->last_scr); | 870 stream->start_pts, stream->start_dts, s->last_scr); |
595 stream->buffer_ptr = 0; | 871 stream->buffer_ptr = 0; |
596 } | 872 } |
597 stream->start_pts = new_start_pts; | 873 stream->start_pts = new_start_pts; |
609 memcpy(stream->buffer + stream->buffer_ptr, buf, len); | 885 memcpy(stream->buffer + stream->buffer_ptr, buf, len); |
610 stream->buffer_ptr += len; | 886 stream->buffer_ptr += len; |
611 buf += len; | 887 buf += len; |
612 size -= len; | 888 size -= len; |
613 if (stream->buffer_ptr >= avail_size) { | 889 if (stream->buffer_ptr >= avail_size) { |
890 | |
891 update_scr(ctx,stream_index,stream->start_pts); | |
892 | |
614 /* if packet full, we send it now */ | 893 /* if packet full, we send it now */ |
615 flush_packet(ctx, stream_index, | 894 flush_packet(ctx, stream_index, |
616 stream->start_pts, stream->start_dts, s->last_scr); | 895 stream->start_pts, stream->start_dts, s->last_scr); |
617 stream->buffer_ptr = 0; | 896 stream->buffer_ptr = 0; |
897 | |
898 if (s->is_vcd) { | |
899 /* Write one or more padding sectors, if necessary, to reach | |
900 the constant overall bitrate.*/ | |
901 int vcd_pad_bytes; | |
902 | |
903 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size) | |
904 put_vcd_padding_sector(ctx); | |
905 } | |
906 | |
618 /* Make sure only the FIRST pes packet for this frame has | 907 /* Make sure only the FIRST pes packet for this frame has |
619 a timestamp */ | 908 a timestamp */ |
620 stream->start_pts = AV_NOPTS_VALUE; | 909 stream->start_pts = AV_NOPTS_VALUE; |
621 stream->start_dts = AV_NOPTS_VALUE; | 910 stream->start_dts = AV_NOPTS_VALUE; |
622 } | 911 } |
633 | 922 |
634 /* flush each packet */ | 923 /* flush each packet */ |
635 for(i=0;i<ctx->nb_streams;i++) { | 924 for(i=0;i<ctx->nb_streams;i++) { |
636 stream = ctx->streams[i]->priv_data; | 925 stream = ctx->streams[i]->priv_data; |
637 if (stream->buffer_ptr > 0) { | 926 if (stream->buffer_ptr > 0) { |
927 update_scr(ctx,i,stream->start_pts); | |
928 | |
638 /* NOTE: we can always write the remaining data as it was | 929 /* NOTE: we can always write the remaining data as it was |
639 tested before in mpeg_mux_write_packet() */ | 930 tested before in mpeg_mux_write_packet() */ |
640 flush_packet(ctx, i, stream->start_pts, stream->start_dts, | 931 flush_packet(ctx, i, stream->start_pts, stream->start_dts, |
641 s->last_scr); | 932 s->last_scr); |
642 } | 933 } |
1137 else if(pos > pos_limit) | 1428 else if(pos > pos_limit) |
1138 pos= pos_limit; | 1429 pos= pos_limit; |
1139 start_pos= pos; | 1430 start_pos= pos; |
1140 | 1431 |
1141 // read the next timestamp | 1432 // read the next timestamp |
1142 dts = mpegps_read_dts(s, stream_index, &pos, 1); | 1433 dts = mpegps_read_dts(s, stream_index, &pos, 1); |
1143 if(pos == pos_max) | 1434 if(pos == pos_max) |
1144 no_change++; | 1435 no_change++; |
1145 else | 1436 else |
1146 no_change=0; | 1437 no_change=0; |
1147 #ifdef DEBUG_SEEK | 1438 #ifdef DEBUG_SEEK |
1212 CODEC_ID_MPEG2VIDEO, | 1503 CODEC_ID_MPEG2VIDEO, |
1213 mpeg_mux_init, | 1504 mpeg_mux_init, |
1214 mpeg_mux_write_packet, | 1505 mpeg_mux_write_packet, |
1215 mpeg_mux_end, | 1506 mpeg_mux_end, |
1216 }; | 1507 }; |
1508 | |
1509 /* Same as mpeg2vob_mux except that the pack size is 2324 */ | |
1510 static AVOutputFormat mpeg2svcd_mux = { | |
1511 "svcd", | |
1512 "MPEG2 PS format (VOB)", | |
1513 "video/mpeg", | |
1514 "vob", | |
1515 sizeof(MpegMuxContext), | |
1516 CODEC_ID_MP2, | |
1517 CODEC_ID_MPEG2VIDEO, | |
1518 mpeg_mux_init, | |
1519 mpeg_mux_write_packet, | |
1520 mpeg_mux_end, | |
1521 }; | |
1522 | |
1523 | |
1524 | |
1217 #endif //CONFIG_ENCODERS | 1525 #endif //CONFIG_ENCODERS |
1218 | 1526 |
1219 AVInputFormat mpegps_demux = { | 1527 AVInputFormat mpegps_demux = { |
1220 "mpeg", | 1528 "mpeg", |
1221 "MPEG PS format", | 1529 "MPEG PS format", |
1231 { | 1539 { |
1232 #ifdef CONFIG_ENCODERS | 1540 #ifdef CONFIG_ENCODERS |
1233 av_register_output_format(&mpeg1system_mux); | 1541 av_register_output_format(&mpeg1system_mux); |
1234 av_register_output_format(&mpeg1vcd_mux); | 1542 av_register_output_format(&mpeg1vcd_mux); |
1235 av_register_output_format(&mpeg2vob_mux); | 1543 av_register_output_format(&mpeg2vob_mux); |
1544 av_register_output_format(&mpeg2svcd_mux); | |
1236 #endif //CONFIG_ENCODERS | 1545 #endif //CONFIG_ENCODERS |
1237 av_register_input_format(&mpegps_demux); | 1546 av_register_input_format(&mpegps_demux); |
1238 return 0; | 1547 return 0; |
1239 } | 1548 } |