00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/fifo.h"
00023 #include "libavutil/log.h"
00024 #include "libavutil/mathematics.h"
00025 #include "libavutil/opt.h"
00026 #include "libavcodec/put_bits.h"
00027 #include "avformat.h"
00028 #include "internal.h"
00029 #include "mpeg.h"
00030
00031 #define MAX_PAYLOAD_SIZE 4096
00032
00033 #undef NDEBUG
00034 #include <assert.h>
00035
00036 typedef struct PacketDesc {
00037 int64_t pts;
00038 int64_t dts;
00039 int size;
00040 int unwritten_size;
00041 int flags;
00042 struct PacketDesc *next;
00043 } PacketDesc;
00044
00045 typedef struct {
00046 AVFifoBuffer *fifo;
00047 uint8_t id;
00048 int max_buffer_size;
00049 int buffer_index;
00050 PacketDesc *predecode_packet;
00051 PacketDesc *premux_packet;
00052 PacketDesc **next_packet;
00053 int packet_number;
00054 uint8_t lpcm_header[3];
00055 int lpcm_align;
00056 int bytes_to_iframe;
00057 int align_iframe;
00058 int64_t vobu_start_pts;
00059 } StreamInfo;
00060
00061 typedef struct {
00062 const AVClass *class;
00063 int packet_size;
00064 int packet_number;
00065 int pack_header_freq;
00066 int system_header_freq;
00067 int system_header_size;
00068 int user_mux_rate;
00069 int mux_rate;
00070
00071 int audio_bound;
00072 int video_bound;
00073 int is_mpeg2;
00074 int is_vcd;
00075 int is_svcd;
00076 int is_dvd;
00077 int64_t last_scr;
00078
00079 double vcd_padding_bitrate;
00080 int64_t vcd_padding_bytes_written;
00081
00082 int preload;
00083 } MpegMuxContext;
00084
00085 extern AVOutputFormat ff_mpeg1vcd_muxer;
00086 extern AVOutputFormat ff_mpeg2dvd_muxer;
00087 extern AVOutputFormat ff_mpeg2svcd_muxer;
00088 extern AVOutputFormat ff_mpeg2vob_muxer;
00089
00090 static int put_pack_header(AVFormatContext *ctx,
00091 uint8_t *buf, int64_t timestamp)
00092 {
00093 MpegMuxContext *s = ctx->priv_data;
00094 PutBitContext pb;
00095
00096 init_put_bits(&pb, buf, 128);
00097
00098 put_bits32(&pb, PACK_START_CODE);
00099 if (s->is_mpeg2) {
00100 put_bits(&pb, 2, 0x1);
00101 } else {
00102 put_bits(&pb, 4, 0x2);
00103 }
00104 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
00105 put_bits(&pb, 1, 1);
00106 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
00107 put_bits(&pb, 1, 1);
00108 put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff));
00109 put_bits(&pb, 1, 1);
00110 if (s->is_mpeg2) {
00111
00112 put_bits(&pb, 9, 0);
00113 }
00114 put_bits(&pb, 1, 1);
00115 put_bits(&pb, 22, s->mux_rate);
00116 put_bits(&pb, 1, 1);
00117 if (s->is_mpeg2) {
00118 put_bits(&pb, 1, 1);
00119 put_bits(&pb, 5, 0x1f);
00120 put_bits(&pb, 3, 0);
00121 }
00122 flush_put_bits(&pb);
00123 return put_bits_ptr(&pb) - pb.buf;
00124 }
00125
00126 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
00127 {
00128 MpegMuxContext *s = ctx->priv_data;
00129 int size, i, private_stream_coded, id;
00130 PutBitContext pb;
00131
00132 init_put_bits(&pb, buf, 128);
00133
00134 put_bits32(&pb, SYSTEM_HEADER_START_CODE);
00135 put_bits(&pb, 16, 0);
00136 put_bits(&pb, 1, 1);
00137
00138 put_bits(&pb, 22, s->mux_rate);
00139 put_bits(&pb, 1, 1);
00140 if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
00141
00142 put_bits(&pb, 6, 0);
00143 } else
00144 put_bits(&pb, 6, s->audio_bound);
00145
00146 if (s->is_vcd) {
00147
00148 put_bits(&pb, 1, 0);
00149 put_bits(&pb, 1, 1);
00150 } else {
00151 put_bits(&pb, 1, 0);
00152 put_bits(&pb, 1, 0);
00153 }
00154
00155 if (s->is_vcd || s->is_dvd) {
00156
00157 put_bits(&pb, 1, 1);
00158 put_bits(&pb, 1, 1);
00159 } else {
00160 put_bits(&pb, 1, 0);
00161 put_bits(&pb, 1, 0);
00162 }
00163
00164 put_bits(&pb, 1, 1);
00165
00166 if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
00167
00168 put_bits(&pb, 5, 0);
00169 } else
00170 put_bits(&pb, 5, s->video_bound);
00171
00172 if (s->is_dvd) {
00173 put_bits(&pb, 1, 0);
00174 put_bits(&pb, 7, 0x7f);
00175 } else
00176 put_bits(&pb, 8, 0xff);
00177
00178
00179
00180
00181
00182
00183 if (s->is_dvd) {
00184
00185 int P_STD_max_video = 0;
00186 int P_STD_max_mpeg_audio = 0;
00187 int P_STD_max_mpeg_PS1 = 0;
00188
00189 for(i=0;i<ctx->nb_streams;i++) {
00190 StreamInfo *stream = ctx->streams[i]->priv_data;
00191
00192 id = stream->id;
00193 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
00194 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
00195 } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
00196 P_STD_max_mpeg_audio = stream->max_buffer_size;
00197 } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
00198 P_STD_max_video = stream->max_buffer_size;
00199 }
00200 }
00201
00202
00203 put_bits(&pb, 8, 0xb9);
00204 put_bits(&pb, 2, 3);
00205 put_bits(&pb, 1, 1);
00206 put_bits(&pb, 13, P_STD_max_video / 1024);
00207
00208
00209 if (P_STD_max_mpeg_audio == 0)
00210 P_STD_max_mpeg_audio = 4096;
00211 put_bits(&pb, 8, 0xb8);
00212 put_bits(&pb, 2, 3);
00213 put_bits(&pb, 1, 0);
00214 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
00215
00216
00217 put_bits(&pb, 8, 0xbd);
00218 put_bits(&pb, 2, 3);
00219 put_bits(&pb, 1, 0);
00220 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
00221
00222
00223 put_bits(&pb, 8, 0xbf);
00224 put_bits(&pb, 2, 3);
00225 put_bits(&pb, 1, 1);
00226 put_bits(&pb, 13, 2);
00227 }
00228 else {
00229
00230 private_stream_coded = 0;
00231 for(i=0;i<ctx->nb_streams;i++) {
00232 StreamInfo *stream = ctx->streams[i]->priv_data;
00233
00234
00235
00236
00237
00238 if ( !s->is_vcd || stream->id==only_for_stream_id
00239 || only_for_stream_id==0) {
00240
00241 id = stream->id;
00242 if (id < 0xc0) {
00243
00244 if (private_stream_coded)
00245 continue;
00246 private_stream_coded = 1;
00247 id = 0xbd;
00248 }
00249 put_bits(&pb, 8, id);
00250 put_bits(&pb, 2, 3);
00251 if (id < 0xe0) {
00252
00253 put_bits(&pb, 1, 0);
00254 put_bits(&pb, 13, stream->max_buffer_size / 128);
00255 } else {
00256
00257 put_bits(&pb, 1, 1);
00258 put_bits(&pb, 13, stream->max_buffer_size / 1024);
00259 }
00260 }
00261 }
00262 }
00263
00264 flush_put_bits(&pb);
00265 size = put_bits_ptr(&pb) - pb.buf;
00266
00267 buf[4] = (size - 6) >> 8;
00268 buf[5] = (size - 6) & 0xff;
00269
00270 return size;
00271 }
00272
00273 static int get_system_header_size(AVFormatContext *ctx)
00274 {
00275 int buf_index, i, private_stream_coded;
00276 StreamInfo *stream;
00277 MpegMuxContext *s = ctx->priv_data;
00278
00279 if (s->is_dvd)
00280 return 18;
00281
00282 buf_index = 12;
00283 private_stream_coded = 0;
00284 for(i=0;i<ctx->nb_streams;i++) {
00285 stream = ctx->streams[i]->priv_data;
00286 if (stream->id < 0xc0) {
00287 if (private_stream_coded)
00288 continue;
00289 private_stream_coded = 1;
00290 }
00291 buf_index += 3;
00292 }
00293 return buf_index;
00294 }
00295
00296 static int mpeg_mux_init(AVFormatContext *ctx)
00297 {
00298 MpegMuxContext *s = ctx->priv_data;
00299 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
00300 AVStream *st;
00301 StreamInfo *stream;
00302 int audio_bitrate;
00303 int video_bitrate;
00304
00305 s->packet_number = 0;
00306 s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer);
00307 s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
00308 s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer) ||
00309 (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer) ||
00310 (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
00311 s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer);
00312
00313 if(ctx->packet_size) {
00314 if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
00315 av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
00316 ctx->packet_size);
00317 goto fail;
00318 }
00319 s->packet_size = ctx->packet_size;
00320 } else
00321 s->packet_size = 2048;
00322 if (ctx->max_delay < 0)
00323 ctx->max_delay = 0;
00324
00325 s->vcd_padding_bytes_written = 0;
00326 s->vcd_padding_bitrate=0;
00327
00328 s->audio_bound = 0;
00329 s->video_bound = 0;
00330 mpa_id = AUDIO_ID;
00331 ac3_id = AC3_ID;
00332 dts_id = DTS_ID;
00333 mpv_id = VIDEO_ID;
00334 mps_id = SUB_ID;
00335 lpcm_id = LPCM_ID;
00336 for(i=0;i<ctx->nb_streams;i++) {
00337 st = ctx->streams[i];
00338 stream = av_mallocz(sizeof(StreamInfo));
00339 if (!stream)
00340 goto fail;
00341 st->priv_data = stream;
00342
00343 avpriv_set_pts_info(st, 64, 1, 90000);
00344
00345 switch(st->codec->codec_type) {
00346 case AVMEDIA_TYPE_AUDIO:
00347 if (st->codec->codec_id == CODEC_ID_AC3) {
00348 stream->id = ac3_id++;
00349 } else if (st->codec->codec_id == CODEC_ID_DTS) {
00350 stream->id = dts_id++;
00351 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
00352 stream->id = lpcm_id++;
00353 for(j = 0; j < 4; j++) {
00354 if (lpcm_freq_tab[j] == st->codec->sample_rate)
00355 break;
00356 }
00357 if (j == 4)
00358 goto fail;
00359 if (st->codec->channels > 8)
00360 return -1;
00361 stream->lpcm_header[0] = 0x0c;
00362 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
00363 stream->lpcm_header[2] = 0x80;
00364 stream->lpcm_align = st->codec->channels * 2;
00365 } else {
00366 stream->id = mpa_id++;
00367 }
00368
00369
00370
00371 stream->max_buffer_size = 4 * 1024;
00372 s->audio_bound++;
00373 break;
00374 case AVMEDIA_TYPE_VIDEO:
00375 stream->id = mpv_id++;
00376 if (st->codec->rc_buffer_size)
00377 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
00378 else {
00379 av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n");
00380 stream->max_buffer_size = 230*1024;
00381 }
00382 #if 0
00383
00384 stream->max_buffer_size = 46 * 1024;
00385 else
00386
00387
00388 stream->max_buffer_size = 230 * 1024;
00389 #endif
00390 s->video_bound++;
00391 break;
00392 case AVMEDIA_TYPE_SUBTITLE:
00393 stream->id = mps_id++;
00394 stream->max_buffer_size = 16 * 1024;
00395 break;
00396 default:
00397 return -1;
00398 }
00399 stream->fifo= av_fifo_alloc(16);
00400 if (!stream->fifo)
00401 goto fail;
00402 }
00403 bitrate = 0;
00404 audio_bitrate = 0;
00405 video_bitrate = 0;
00406 for(i=0;i<ctx->nb_streams;i++) {
00407 int codec_rate;
00408 st = ctx->streams[i];
00409 stream = (StreamInfo*) st->priv_data;
00410
00411 if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
00412 codec_rate= st->codec->rc_max_rate;
00413 else
00414 codec_rate= st->codec->bit_rate;
00415
00416 if(!codec_rate)
00417 codec_rate= (1<<21)*8*50/ctx->nb_streams;
00418
00419 bitrate += codec_rate;
00420
00421 if ((stream->id & 0xe0) == AUDIO_ID)
00422 audio_bitrate += codec_rate;
00423 else if (stream->id==VIDEO_ID)
00424 video_bitrate += codec_rate;
00425 }
00426
00427 if (s->user_mux_rate) {
00428 s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
00429 } else {
00430
00431
00432 bitrate += bitrate / 20;
00433 bitrate += 10000;
00434 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
00435 }
00436
00437 if (s->is_vcd) {
00438 double overhead_rate;
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
00459 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
00460 overhead_rate *= 8;
00461
00462
00463 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
00464 }
00465
00466 if (s->is_vcd || s->is_mpeg2)
00467
00468 s->pack_header_freq = 1;
00469 else
00470
00471 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
00472
00473
00474 if (s->pack_header_freq == 0)
00475 s->pack_header_freq = 1;
00476
00477 if (s->is_mpeg2)
00478
00479 s->system_header_freq = s->pack_header_freq * 40;
00480 else if (s->is_vcd)
00481
00482
00483
00484 s->system_header_freq = 0x7fffffff;
00485 else
00486 s->system_header_freq = s->pack_header_freq * 5;
00487
00488 for(i=0;i<ctx->nb_streams;i++) {
00489 stream = ctx->streams[i]->priv_data;
00490 stream->packet_number = 0;
00491 }
00492 s->system_header_size = get_system_header_size(ctx);
00493 s->last_scr = 0;
00494 return 0;
00495 fail:
00496 for(i=0;i<ctx->nb_streams;i++) {
00497 av_free(ctx->streams[i]->priv_data);
00498 }
00499 return AVERROR(ENOMEM);
00500 }
00501
00502 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
00503 {
00504 avio_w8(pb,
00505 (id << 4) |
00506 (((timestamp >> 30) & 0x07) << 1) |
00507 1);
00508 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
00509 avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
00510 }
00511
00512
00513
00514
00515 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
00516 {
00517 MpegMuxContext *s = ctx->priv_data;
00518 int pad_bytes = 0;
00519
00520 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
00521 {
00522 int64_t full_pad_bytes;
00523
00524 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0);
00525 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
00526
00527 if (pad_bytes<0)
00528
00529
00530 pad_bytes=0;
00531 }
00532
00533 return pad_bytes;
00534 }
00535
00536
00537 #if 0
00538
00539
00540
00541 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
00542 int64_t pts, int64_t dts)
00543 {
00544 MpegMuxContext *s = ctx->priv_data;
00545 int buf_index;
00546 StreamInfo *stream;
00547
00548 stream = ctx->streams[stream_index]->priv_data;
00549
00550 buf_index = 0;
00551 if (((s->packet_number % s->pack_header_freq) == 0)) {
00552
00553 if (s->is_mpeg2)
00554 buf_index += 14;
00555 else
00556 buf_index += 12;
00557
00558 if (s->is_vcd) {
00559
00560
00561
00562
00563 if (stream->packet_number==0)
00564
00565
00566 buf_index += 15;
00567
00568 } else {
00569 if ((s->packet_number % s->system_header_freq) == 0)
00570 buf_index += s->system_header_size;
00571 }
00572 }
00573
00574 if ((s->is_vcd && stream->packet_number==0)
00575 || (s->is_svcd && s->packet_number==0))
00576
00577
00578
00579 buf_index += s->packet_size - buf_index;
00580 else {
00581
00582 buf_index += 6;
00583 if (s->is_mpeg2) {
00584 buf_index += 3;
00585 if (stream->packet_number==0)
00586 buf_index += 3;
00587 buf_index += 1;
00588 }
00589 if (pts != AV_NOPTS_VALUE) {
00590 if (dts != pts)
00591 buf_index += 5 + 5;
00592 else
00593 buf_index += 5;
00594
00595 } else {
00596 if (!s->is_mpeg2)
00597 buf_index++;
00598 }
00599
00600 if (stream->id < 0xc0) {
00601
00602 buf_index += 4;
00603 if (stream->id >= 0xa0) {
00604 int n;
00605 buf_index += 3;
00606
00607
00608 n = (s->packet_size - buf_index) % stream->lpcm_align;
00609 if (n)
00610 buf_index += (stream->lpcm_align - n);
00611 }
00612 }
00613
00614 if (s->is_vcd && (stream->id & 0xe0) == AUDIO_ID)
00615
00616
00617 buf_index+=20;
00618 }
00619 return s->packet_size - buf_index;
00620 }
00621 #endif
00622
00623
00624 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
00625 {
00626 MpegMuxContext *s = ctx->priv_data;
00627 int i;
00628
00629 avio_wb32(pb, PADDING_STREAM);
00630 avio_wb16(pb, packet_bytes - 6);
00631 if (!s->is_mpeg2) {
00632 avio_w8(pb, 0x0f);
00633 packet_bytes -= 7;
00634 } else
00635 packet_bytes -= 6;
00636
00637 for(i=0;i<packet_bytes;i++)
00638 avio_w8(pb, 0xff);
00639 }
00640
00641 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
00642 int nb_frames=0;
00643 PacketDesc *pkt_desc= stream->premux_packet;
00644
00645 while(len>0){
00646 if(pkt_desc->size == pkt_desc->unwritten_size)
00647 nb_frames++;
00648 len -= pkt_desc->unwritten_size;
00649 pkt_desc= pkt_desc->next;
00650 }
00651
00652 return nb_frames;
00653 }
00654
00655
00656 static int flush_packet(AVFormatContext *ctx, int stream_index,
00657 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
00658 {
00659 MpegMuxContext *s = ctx->priv_data;
00660 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
00661 uint8_t *buf_ptr;
00662 int size, payload_size, startcode, id, stuffing_size, i, header_len;
00663 int packet_size;
00664 uint8_t buffer[128];
00665 int zero_trail_bytes = 0;
00666 int pad_packet_bytes = 0;
00667 int pes_flags;
00668 int general_pack = 0;
00669 int nb_frames;
00670
00671 id = stream->id;
00672
00673 av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
00674
00675 buf_ptr = buffer;
00676
00677 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
00678
00679 size = put_pack_header(ctx, buf_ptr, scr);
00680 buf_ptr += size;
00681 s->last_scr= scr;
00682
00683 if (s->is_vcd) {
00684
00685
00686
00687
00688 if (stream->packet_number==0) {
00689 size = put_system_header(ctx, buf_ptr, id);
00690 buf_ptr += size;
00691 }
00692 } else if (s->is_dvd) {
00693 if (stream->align_iframe || s->packet_number == 0){
00694 int PES_bytes_to_fill = s->packet_size - size - 10;
00695
00696 if (pts != AV_NOPTS_VALUE) {
00697 if (dts != pts)
00698 PES_bytes_to_fill -= 5 + 5;
00699 else
00700 PES_bytes_to_fill -= 5;
00701 }
00702
00703 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
00704 size = put_system_header(ctx, buf_ptr, 0);
00705 buf_ptr += size;
00706 size = buf_ptr - buffer;
00707 avio_write(ctx->pb, buffer, size);
00708
00709 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
00710 avio_wb16(ctx->pb, 0x03d4);
00711 avio_w8(ctx->pb, 0x00);
00712 for (i = 0; i < 979; i++)
00713 avio_w8(ctx->pb, 0x00);
00714
00715 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
00716 avio_wb16(ctx->pb, 0x03fa);
00717 avio_w8(ctx->pb, 0x01);
00718 for (i = 0; i < 1017; i++)
00719 avio_w8(ctx->pb, 0x00);
00720
00721 memset(buffer, 0, 128);
00722 buf_ptr = buffer;
00723 s->packet_number++;
00724 stream->align_iframe = 0;
00725 scr += s->packet_size*90000LL / (s->mux_rate*50LL);
00726 size = put_pack_header(ctx, buf_ptr, scr);
00727 s->last_scr= scr;
00728 buf_ptr += size;
00729
00730 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
00731 pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
00732 }
00733 }
00734 } else {
00735 if ((s->packet_number % s->system_header_freq) == 0) {
00736 size = put_system_header(ctx, buf_ptr, 0);
00737 buf_ptr += size;
00738 }
00739 }
00740 }
00741 size = buf_ptr - buffer;
00742 avio_write(ctx->pb, buffer, size);
00743
00744 packet_size = s->packet_size - size;
00745
00746 if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
00747
00748
00749 zero_trail_bytes += 20;
00750
00751 if ((s->is_vcd && stream->packet_number==0)
00752 || (s->is_svcd && s->packet_number==0)) {
00753
00754
00755
00756
00757
00758
00759 if (s->is_svcd)
00760 general_pack = 1;
00761 pad_packet_bytes = packet_size - zero_trail_bytes;
00762 }
00763
00764 packet_size -= pad_packet_bytes + zero_trail_bytes;
00765
00766 if (packet_size > 0) {
00767
00768
00769 packet_size -= 6;
00770
00771
00772 if (s->is_mpeg2) {
00773 header_len = 3;
00774 if (stream->packet_number==0)
00775 header_len += 3;
00776 header_len += 1;
00777 } else {
00778 header_len = 0;
00779 }
00780 if (pts != AV_NOPTS_VALUE) {
00781 if (dts != pts)
00782 header_len += 5 + 5;
00783 else
00784 header_len += 5;
00785 } else {
00786 if (!s->is_mpeg2)
00787 header_len++;
00788 }
00789
00790 payload_size = packet_size - header_len;
00791 if (id < 0xc0) {
00792 startcode = PRIVATE_STREAM_1;
00793 payload_size -= 1;
00794 if (id >= 0x40) {
00795 payload_size -= 3;
00796 if (id >= 0xa0)
00797 payload_size -= 3;
00798 }
00799 } else {
00800 startcode = 0x100 + id;
00801 }
00802
00803 stuffing_size = payload_size - av_fifo_size(stream->fifo);
00804
00805
00806 if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
00807 int timestamp_len=0;
00808 if(dts != pts)
00809 timestamp_len += 5;
00810 if(pts != AV_NOPTS_VALUE)
00811 timestamp_len += s->is_mpeg2 ? 5 : 4;
00812 pts=dts= AV_NOPTS_VALUE;
00813 header_len -= timestamp_len;
00814 if (s->is_dvd && stream->align_iframe) {
00815 pad_packet_bytes += timestamp_len;
00816 packet_size -= timestamp_len;
00817 } else {
00818 payload_size += timestamp_len;
00819 }
00820 stuffing_size += timestamp_len;
00821 if(payload_size > trailer_size)
00822 stuffing_size += payload_size - trailer_size;
00823 }
00824
00825 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
00826 packet_size += pad_packet_bytes;
00827 payload_size += pad_packet_bytes;
00828 if (stuffing_size < 0) {
00829 stuffing_size = pad_packet_bytes;
00830 } else {
00831 stuffing_size += pad_packet_bytes;
00832 }
00833 pad_packet_bytes = 0;
00834 }
00835
00836 if (stuffing_size < 0)
00837 stuffing_size = 0;
00838
00839 if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
00840 if (payload_size < av_fifo_size(stream->fifo))
00841 stuffing_size += payload_size % stream->lpcm_align;
00842 }
00843
00844 if (stuffing_size > 16) {
00845 pad_packet_bytes += stuffing_size;
00846 packet_size -= stuffing_size;
00847 payload_size -= stuffing_size;
00848 stuffing_size = 0;
00849 }
00850
00851 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
00852
00853 avio_wb32(ctx->pb, startcode);
00854
00855 avio_wb16(ctx->pb, packet_size);
00856
00857 if (!s->is_mpeg2)
00858 for(i=0;i<stuffing_size;i++)
00859 avio_w8(ctx->pb, 0xff);
00860
00861 if (s->is_mpeg2) {
00862 avio_w8(ctx->pb, 0x80);
00863
00864 pes_flags=0;
00865
00866 if (pts != AV_NOPTS_VALUE) {
00867 pes_flags |= 0x80;
00868 if (dts != pts)
00869 pes_flags |= 0x40;
00870 }
00871
00872
00873
00874
00875
00876 if (stream->packet_number == 0)
00877 pes_flags |= 0x01;
00878
00879 avio_w8(ctx->pb, pes_flags);
00880 avio_w8(ctx->pb, header_len - 3 + stuffing_size);
00881
00882 if (pes_flags & 0x80)
00883 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
00884 if (pes_flags & 0x40)
00885 put_timestamp(ctx->pb, 0x01, dts);
00886
00887 if (pes_flags & 0x01) {
00888 avio_w8(ctx->pb, 0x10);
00889
00890
00891 if ((id & 0xe0) == AUDIO_ID)
00892 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
00893 else
00894 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
00895 }
00896
00897 } else {
00898 if (pts != AV_NOPTS_VALUE) {
00899 if (dts != pts) {
00900 put_timestamp(ctx->pb, 0x03, pts);
00901 put_timestamp(ctx->pb, 0x01, dts);
00902 } else {
00903 put_timestamp(ctx->pb, 0x02, pts);
00904 }
00905 } else {
00906 avio_w8(ctx->pb, 0x0f);
00907 }
00908 }
00909
00910 if (s->is_mpeg2) {
00911
00912
00913 avio_w8(ctx->pb, 0xff);
00914
00915 for(i=0;i<stuffing_size;i++)
00916 avio_w8(ctx->pb, 0xff);
00917 }
00918
00919 if (startcode == PRIVATE_STREAM_1) {
00920 avio_w8(ctx->pb, id);
00921 if (id >= 0xa0) {
00922
00923 avio_w8(ctx->pb, 7);
00924 avio_wb16(ctx->pb, 4);
00925 avio_w8(ctx->pb, stream->lpcm_header[0]);
00926 avio_w8(ctx->pb, stream->lpcm_header[1]);
00927 avio_w8(ctx->pb, stream->lpcm_header[2]);
00928 } else if (id >= 0x40) {
00929
00930 avio_w8(ctx->pb, nb_frames);
00931 avio_wb16(ctx->pb, trailer_size+1);
00932 }
00933 }
00934
00935
00936 assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
00937 av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, (void*)avio_write);
00938 stream->bytes_to_iframe -= payload_size - stuffing_size;
00939 }else{
00940 payload_size=
00941 stuffing_size= 0;
00942 }
00943
00944 if (pad_packet_bytes > 0)
00945 put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
00946
00947 for(i=0;i<zero_trail_bytes;i++)
00948 avio_w8(ctx->pb, 0x00);
00949
00950 avio_flush(ctx->pb);
00951
00952 s->packet_number++;
00953
00954
00955
00956
00957 if (!general_pack)
00958 stream->packet_number++;
00959
00960 return payload_size - stuffing_size;
00961 }
00962
00963 static void put_vcd_padding_sector(AVFormatContext *ctx)
00964 {
00965
00966
00967
00968
00969
00970
00971 MpegMuxContext *s = ctx->priv_data;
00972 int i;
00973
00974 for(i=0;i<s->packet_size;i++)
00975 avio_w8(ctx->pb, 0);
00976
00977 s->vcd_padding_bytes_written += s->packet_size;
00978
00979 avio_flush(ctx->pb);
00980
00981
00982
00983
00984
00985 s->packet_number++;
00986 }
00987
00988 #if 0
00989 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
00990 {
00991 MpegMuxContext *s = ctx->priv_data;
00992 int64_t scr;
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004 scr = 36000 + s->packet_number * 1200;
01005
01006 return scr;
01007 }
01008 #endif
01009
01010 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
01011
01012 int i;
01013
01014 for(i=0; i<ctx->nb_streams; i++){
01015 AVStream *st = ctx->streams[i];
01016 StreamInfo *stream = st->priv_data;
01017 PacketDesc *pkt_desc;
01018
01019 while((pkt_desc= stream->predecode_packet)
01020 && scr > pkt_desc->dts){
01021 if(stream->buffer_index < pkt_desc->size ||
01022 stream->predecode_packet == stream->premux_packet){
01023 av_log(ctx, AV_LOG_ERROR,
01024 "buffer underflow i=%d bufi=%d size=%d\n",
01025 i, stream->buffer_index, pkt_desc->size);
01026 break;
01027 }
01028 stream->buffer_index -= pkt_desc->size;
01029
01030 stream->predecode_packet= pkt_desc->next;
01031 av_freep(&pkt_desc);
01032 }
01033 }
01034
01035 return 0;
01036 }
01037
01038 static int output_packet(AVFormatContext *ctx, int flush){
01039 MpegMuxContext *s = ctx->priv_data;
01040 AVStream *st;
01041 StreamInfo *stream;
01042 int i, avail_space=0, es_size, trailer_size;
01043 int best_i= -1;
01044 int best_score= INT_MIN;
01045 int ignore_constraints=0;
01046 int64_t scr= s->last_scr;
01047 PacketDesc *timestamp_packet;
01048 const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
01049
01050 retry:
01051 for(i=0; i<ctx->nb_streams; i++){
01052 AVStream *st = ctx->streams[i];
01053 StreamInfo *stream = st->priv_data;
01054 const int avail_data= av_fifo_size(stream->fifo);
01055 const int space= stream->max_buffer_size - stream->buffer_index;
01056 int rel_space= 1024LL*space / stream->max_buffer_size;
01057 PacketDesc *next_pkt= stream->premux_packet;
01058
01059
01060
01061 if(s->packet_size > avail_data && !flush
01062 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
01063 return 0;
01064 if(avail_data==0)
01065 continue;
01066 assert(avail_data>0);
01067
01068 if(space < s->packet_size && !ignore_constraints)
01069 continue;
01070
01071 if(next_pkt && next_pkt->dts - scr > max_delay)
01072 continue;
01073
01074 if(rel_space > best_score){
01075 best_score= rel_space;
01076 best_i = i;
01077 avail_space= space;
01078 }
01079 }
01080
01081 if(best_i < 0){
01082 int64_t best_dts= INT64_MAX;
01083
01084 for(i=0; i<ctx->nb_streams; i++){
01085 AVStream *st = ctx->streams[i];
01086 StreamInfo *stream = st->priv_data;
01087 PacketDesc *pkt_desc= stream->predecode_packet;
01088 if(pkt_desc && pkt_desc->dts < best_dts)
01089 best_dts= pkt_desc->dts;
01090 }
01091
01092 av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
01093 scr / 90000.0, best_dts / 90000.0);
01094 if(best_dts == INT64_MAX)
01095 return 0;
01096
01097 if(scr >= best_dts+1 && !ignore_constraints){
01098 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
01099 ignore_constraints= 1;
01100 }
01101 scr= FFMAX(best_dts+1, scr);
01102 if(remove_decoded_packets(ctx, scr) < 0)
01103 return -1;
01104 goto retry;
01105 }
01106
01107 assert(best_i >= 0);
01108
01109 st = ctx->streams[best_i];
01110 stream = st->priv_data;
01111
01112 assert(av_fifo_size(stream->fifo) > 0);
01113
01114 assert(avail_space >= s->packet_size || ignore_constraints);
01115
01116 timestamp_packet= stream->premux_packet;
01117 if(timestamp_packet->unwritten_size == timestamp_packet->size){
01118 trailer_size= 0;
01119 }else{
01120 trailer_size= timestamp_packet->unwritten_size;
01121 timestamp_packet= timestamp_packet->next;
01122 }
01123
01124 if(timestamp_packet){
01125
01126 es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
01127 }else{
01128 assert(av_fifo_size(stream->fifo) == trailer_size);
01129 es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
01130 }
01131
01132 if (s->is_vcd) {
01133
01134
01135 int vcd_pad_bytes;
01136
01137 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){
01138 put_vcd_padding_sector(ctx);
01139 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL);
01140 }
01141 }
01142
01143 stream->buffer_index += es_size;
01144 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL);
01145
01146 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
01147 es_size -= stream->premux_packet->unwritten_size;
01148 stream->premux_packet= stream->premux_packet->next;
01149 }
01150 if(es_size)
01151 stream->premux_packet->unwritten_size -= es_size;
01152
01153 if(remove_decoded_packets(ctx, s->last_scr) < 0)
01154 return -1;
01155
01156 return 1;
01157 }
01158
01159 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
01160 {
01161 MpegMuxContext *s = ctx->priv_data;
01162 int stream_index= pkt->stream_index;
01163 int size= pkt->size;
01164 uint8_t *buf= pkt->data;
01165 AVStream *st = ctx->streams[stream_index];
01166 StreamInfo *stream = st->priv_data;
01167 int64_t pts, dts;
01168 PacketDesc *pkt_desc;
01169 int preload;
01170 const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY);
01171
01172 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
01173
01174 pts= pkt->pts;
01175 dts= pkt->dts;
01176
01177 if(pts != AV_NOPTS_VALUE) pts += 2*preload;
01178 if(dts != AV_NOPTS_VALUE){
01179 if(!s->last_scr)
01180 s->last_scr= dts + preload;
01181 dts += 2*preload;
01182 }
01183
01184
01185 if (!stream->premux_packet)
01186 stream->next_packet = &stream->premux_packet;
01187 *stream->next_packet=
01188 pkt_desc= av_mallocz(sizeof(PacketDesc));
01189 pkt_desc->pts= pts;
01190 pkt_desc->dts= dts;
01191 pkt_desc->unwritten_size=
01192 pkt_desc->size= size;
01193 if(!stream->predecode_packet)
01194 stream->predecode_packet= pkt_desc;
01195 stream->next_packet= &pkt_desc->next;
01196
01197 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
01198 return -1;
01199
01200 if (s->is_dvd){
01201 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) {
01202 stream->bytes_to_iframe = av_fifo_size(stream->fifo);
01203 stream->align_iframe = 1;
01204 stream->vobu_start_pts = pts;
01205 }
01206 }
01207
01208 av_fifo_generic_write(stream->fifo, buf, size, NULL);
01209
01210 for(;;){
01211 int ret= output_packet(ctx, 0);
01212 if(ret<=0)
01213 return ret;
01214 }
01215 }
01216
01217 static int mpeg_mux_end(AVFormatContext *ctx)
01218 {
01219
01220 StreamInfo *stream;
01221 int i;
01222
01223 for(;;){
01224 int ret= output_packet(ctx, 1);
01225 if(ret<0)
01226 return ret;
01227 else if(ret==0)
01228 break;
01229 }
01230
01231
01232
01233
01234
01235
01236
01237 for(i=0;i<ctx->nb_streams;i++) {
01238 stream = ctx->streams[i]->priv_data;
01239
01240 assert(av_fifo_size(stream->fifo) == 0);
01241 av_fifo_free(stream->fifo);
01242 }
01243 return 0;
01244 }
01245
01246 #define OFFSET(x) offsetof(MpegMuxContext, x)
01247 #define E AV_OPT_FLAG_ENCODING_PARAM
01248 static const AVOption options[] = {
01249 { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, {0}, 0, INT_MAX, E },
01250 { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, {500000}, 0, INT_MAX, E},
01251 { NULL },
01252 };
01253
01254 #define MPEGENC_CLASS(flavor)\
01255 static const AVClass flavor ## _class = {\
01256 .class_name = #flavor " muxer",\
01257 .item_name = av_default_item_name,\
01258 .version = LIBAVUTIL_VERSION_INT,\
01259 .option = options,\
01260 };
01261
01262 #if CONFIG_MPEG1SYSTEM_MUXER
01263 MPEGENC_CLASS(mpeg)
01264 AVOutputFormat ff_mpeg1system_muxer = {
01265 .name = "mpeg",
01266 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 System format"),
01267 .mime_type = "video/mpeg",
01268 .extensions = "mpg,mpeg",
01269 .priv_data_size = sizeof(MpegMuxContext),
01270 .audio_codec = CODEC_ID_MP2,
01271 .video_codec = CODEC_ID_MPEG1VIDEO,
01272 .write_header = mpeg_mux_init,
01273 .write_packet = mpeg_mux_write_packet,
01274 .write_trailer = mpeg_mux_end,
01275 .priv_class = &mpeg_class,
01276 };
01277 #endif
01278 #if CONFIG_MPEG1VCD_MUXER
01279 MPEGENC_CLASS(vcd)
01280 AVOutputFormat ff_mpeg1vcd_muxer = {
01281 .name = "vcd",
01282 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"),
01283 .mime_type = "video/mpeg",
01284 .priv_data_size = sizeof(MpegMuxContext),
01285 .audio_codec = CODEC_ID_MP2,
01286 .video_codec = CODEC_ID_MPEG1VIDEO,
01287 .write_header = mpeg_mux_init,
01288 .write_packet = mpeg_mux_write_packet,
01289 .write_trailer = mpeg_mux_end,
01290 .priv_class = &vcd_class,
01291 };
01292 #endif
01293 #if CONFIG_MPEG2VOB_MUXER
01294 MPEGENC_CLASS(vob)
01295 AVOutputFormat ff_mpeg2vob_muxer = {
01296 .name = "vob",
01297 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
01298 .mime_type = "video/mpeg",
01299 .extensions = "vob",
01300 .priv_data_size = sizeof(MpegMuxContext),
01301 .audio_codec = CODEC_ID_MP2,
01302 .video_codec = CODEC_ID_MPEG2VIDEO,
01303 .write_header = mpeg_mux_init,
01304 .write_packet = mpeg_mux_write_packet,
01305 .write_trailer = mpeg_mux_end,
01306 .priv_class = &vob_class,
01307 };
01308 #endif
01309
01310
01311 #if CONFIG_MPEG2SVCD_MUXER
01312 MPEGENC_CLASS(svcd)
01313 AVOutputFormat ff_mpeg2svcd_muxer = {
01314 .name = "svcd",
01315 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
01316 .mime_type = "video/mpeg",
01317 .extensions = "vob",
01318 .priv_data_size = sizeof(MpegMuxContext),
01319 .audio_codec = CODEC_ID_MP2,
01320 .video_codec = CODEC_ID_MPEG2VIDEO,
01321 .write_header = mpeg_mux_init,
01322 .write_packet = mpeg_mux_write_packet,
01323 .write_trailer = mpeg_mux_end,
01324 .priv_class = &svcd_class,
01325 };
01326 #endif
01327
01328
01329 #if CONFIG_MPEG2DVD_MUXER
01330 MPEGENC_CLASS(dvd)
01331 AVOutputFormat ff_mpeg2dvd_muxer = {
01332 .name = "dvd",
01333 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"),
01334 .mime_type = "video/mpeg",
01335 .extensions = "dvd",
01336 .priv_data_size = sizeof(MpegMuxContext),
01337 .audio_codec = CODEC_ID_MP2,
01338 .video_codec = CODEC_ID_MPEG2VIDEO,
01339 .write_header = mpeg_mux_init,
01340 .write_packet = mpeg_mux_write_packet,
01341 .write_trailer = mpeg_mux_end,
01342 .priv_class = &dvd_class,
01343 };
01344 #endif