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