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 == AV_CODEC_ID_AC3) {
00348 stream->id = ac3_id++;
00349 } else if (st->codec->codec_id == AV_CODEC_ID_DTS) {
00350 stream->id = dts_id++;
00351 } else if (st->codec->codec_id == AV_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 s->video_bound++;
00383 break;
00384 case AVMEDIA_TYPE_SUBTITLE:
00385 stream->id = mps_id++;
00386 stream->max_buffer_size = 16 * 1024;
00387 break;
00388 default:
00389 return -1;
00390 }
00391 stream->fifo= av_fifo_alloc(16);
00392 if (!stream->fifo)
00393 goto fail;
00394 }
00395 bitrate = 0;
00396 audio_bitrate = 0;
00397 video_bitrate = 0;
00398 for(i=0;i<ctx->nb_streams;i++) {
00399 int codec_rate;
00400 st = ctx->streams[i];
00401 stream = (StreamInfo*) st->priv_data;
00402
00403 if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
00404 codec_rate= st->codec->rc_max_rate;
00405 else
00406 codec_rate= st->codec->bit_rate;
00407
00408 if(!codec_rate)
00409 codec_rate= (1<<21)*8*50/ctx->nb_streams;
00410
00411 bitrate += codec_rate;
00412
00413 if ((stream->id & 0xe0) == AUDIO_ID)
00414 audio_bitrate += codec_rate;
00415 else if (stream->id==VIDEO_ID)
00416 video_bitrate += codec_rate;
00417 }
00418
00419 if (s->user_mux_rate) {
00420 s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
00421 } else {
00422
00423
00424 bitrate += bitrate / 20;
00425 bitrate += 10000;
00426 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
00427 }
00428
00429 if (s->is_vcd) {
00430 double overhead_rate;
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
00451 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
00452 overhead_rate *= 8;
00453
00454
00455 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
00456 }
00457
00458 if (s->is_vcd || s->is_mpeg2)
00459
00460 s->pack_header_freq = 1;
00461 else
00462
00463 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
00464
00465
00466 if (s->pack_header_freq == 0)
00467 s->pack_header_freq = 1;
00468
00469 if (s->is_mpeg2)
00470
00471 s->system_header_freq = s->pack_header_freq * 40;
00472 else if (s->is_vcd)
00473
00474
00475
00476 s->system_header_freq = 0x7fffffff;
00477 else
00478 s->system_header_freq = s->pack_header_freq * 5;
00479
00480 for(i=0;i<ctx->nb_streams;i++) {
00481 stream = ctx->streams[i]->priv_data;
00482 stream->packet_number = 0;
00483 }
00484 s->system_header_size = get_system_header_size(ctx);
00485 s->last_scr = 0;
00486 return 0;
00487 fail:
00488 for(i=0;i<ctx->nb_streams;i++) {
00489 av_free(ctx->streams[i]->priv_data);
00490 }
00491 return AVERROR(ENOMEM);
00492 }
00493
00494 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
00495 {
00496 avio_w8(pb,
00497 (id << 4) |
00498 (((timestamp >> 30) & 0x07) << 1) |
00499 1);
00500 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
00501 avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
00502 }
00503
00504
00505
00506
00507 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
00508 {
00509 MpegMuxContext *s = ctx->priv_data;
00510 int pad_bytes = 0;
00511
00512 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
00513 {
00514 int64_t full_pad_bytes;
00515
00516 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0);
00517 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
00518
00519 if (pad_bytes<0)
00520
00521
00522 pad_bytes=0;
00523 }
00524
00525 return pad_bytes;
00526 }
00527
00528
00529
00530 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
00531 {
00532 MpegMuxContext *s = ctx->priv_data;
00533 int i;
00534
00535 avio_wb32(pb, PADDING_STREAM);
00536 avio_wb16(pb, packet_bytes - 6);
00537 if (!s->is_mpeg2) {
00538 avio_w8(pb, 0x0f);
00539 packet_bytes -= 7;
00540 } else
00541 packet_bytes -= 6;
00542
00543 for(i=0;i<packet_bytes;i++)
00544 avio_w8(pb, 0xff);
00545 }
00546
00547 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
00548 int nb_frames=0;
00549 PacketDesc *pkt_desc= stream->premux_packet;
00550
00551 while(len>0){
00552 if(pkt_desc->size == pkt_desc->unwritten_size)
00553 nb_frames++;
00554 len -= pkt_desc->unwritten_size;
00555 pkt_desc= pkt_desc->next;
00556 }
00557
00558 return nb_frames;
00559 }
00560
00561
00562 static int flush_packet(AVFormatContext *ctx, int stream_index,
00563 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
00564 {
00565 MpegMuxContext *s = ctx->priv_data;
00566 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
00567 uint8_t *buf_ptr;
00568 int size, payload_size, startcode, id, stuffing_size, i, header_len;
00569 int packet_size;
00570 uint8_t buffer[128];
00571 int zero_trail_bytes = 0;
00572 int pad_packet_bytes = 0;
00573 int pes_flags;
00574 int general_pack = 0;
00575 int nb_frames;
00576
00577 id = stream->id;
00578
00579 av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
00580
00581 buf_ptr = buffer;
00582
00583 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
00584
00585 size = put_pack_header(ctx, buf_ptr, scr);
00586 buf_ptr += size;
00587 s->last_scr= scr;
00588
00589 if (s->is_vcd) {
00590
00591
00592
00593
00594 if (stream->packet_number==0) {
00595 size = put_system_header(ctx, buf_ptr, id);
00596 buf_ptr += size;
00597 }
00598 } else if (s->is_dvd) {
00599 if (stream->align_iframe || s->packet_number == 0){
00600 int PES_bytes_to_fill = s->packet_size - size - 10;
00601
00602 if (pts != AV_NOPTS_VALUE) {
00603 if (dts != pts)
00604 PES_bytes_to_fill -= 5 + 5;
00605 else
00606 PES_bytes_to_fill -= 5;
00607 }
00608
00609 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
00610 size = put_system_header(ctx, buf_ptr, 0);
00611 buf_ptr += size;
00612 size = buf_ptr - buffer;
00613 avio_write(ctx->pb, buffer, size);
00614
00615 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
00616 avio_wb16(ctx->pb, 0x03d4);
00617 avio_w8(ctx->pb, 0x00);
00618 for (i = 0; i < 979; i++)
00619 avio_w8(ctx->pb, 0x00);
00620
00621 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
00622 avio_wb16(ctx->pb, 0x03fa);
00623 avio_w8(ctx->pb, 0x01);
00624 for (i = 0; i < 1017; i++)
00625 avio_w8(ctx->pb, 0x00);
00626
00627 memset(buffer, 0, 128);
00628 buf_ptr = buffer;
00629 s->packet_number++;
00630 stream->align_iframe = 0;
00631 scr += s->packet_size*90000LL / (s->mux_rate*50LL);
00632 size = put_pack_header(ctx, buf_ptr, scr);
00633 s->last_scr= scr;
00634 buf_ptr += size;
00635
00636 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
00637 pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
00638 }
00639 }
00640 } else {
00641 if ((s->packet_number % s->system_header_freq) == 0) {
00642 size = put_system_header(ctx, buf_ptr, 0);
00643 buf_ptr += size;
00644 }
00645 }
00646 }
00647 size = buf_ptr - buffer;
00648 avio_write(ctx->pb, buffer, size);
00649
00650 packet_size = s->packet_size - size;
00651
00652 if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
00653
00654
00655 zero_trail_bytes += 20;
00656
00657 if ((s->is_vcd && stream->packet_number==0)
00658 || (s->is_svcd && s->packet_number==0)) {
00659
00660
00661
00662
00663
00664
00665 if (s->is_svcd)
00666 general_pack = 1;
00667 pad_packet_bytes = packet_size - zero_trail_bytes;
00668 }
00669
00670 packet_size -= pad_packet_bytes + zero_trail_bytes;
00671
00672 if (packet_size > 0) {
00673
00674
00675 packet_size -= 6;
00676
00677
00678 if (s->is_mpeg2) {
00679 header_len = 3;
00680 if (stream->packet_number==0)
00681 header_len += 3;
00682 header_len += 1;
00683 } else {
00684 header_len = 0;
00685 }
00686 if (pts != AV_NOPTS_VALUE) {
00687 if (dts != pts)
00688 header_len += 5 + 5;
00689 else
00690 header_len += 5;
00691 } else {
00692 if (!s->is_mpeg2)
00693 header_len++;
00694 }
00695
00696 payload_size = packet_size - header_len;
00697 if (id < 0xc0) {
00698 startcode = PRIVATE_STREAM_1;
00699 payload_size -= 1;
00700 if (id >= 0x40) {
00701 payload_size -= 3;
00702 if (id >= 0xa0)
00703 payload_size -= 3;
00704 }
00705 } else {
00706 startcode = 0x100 + id;
00707 }
00708
00709 stuffing_size = payload_size - av_fifo_size(stream->fifo);
00710
00711
00712 if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
00713 int timestamp_len=0;
00714 if(dts != pts)
00715 timestamp_len += 5;
00716 if(pts != AV_NOPTS_VALUE)
00717 timestamp_len += s->is_mpeg2 ? 5 : 4;
00718 pts=dts= AV_NOPTS_VALUE;
00719 header_len -= timestamp_len;
00720 if (s->is_dvd && stream->align_iframe) {
00721 pad_packet_bytes += timestamp_len;
00722 packet_size -= timestamp_len;
00723 } else {
00724 payload_size += timestamp_len;
00725 }
00726 stuffing_size += timestamp_len;
00727 if(payload_size > trailer_size)
00728 stuffing_size += payload_size - trailer_size;
00729 }
00730
00731 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
00732 packet_size += pad_packet_bytes;
00733 payload_size += pad_packet_bytes;
00734 if (stuffing_size < 0) {
00735 stuffing_size = pad_packet_bytes;
00736 } else {
00737 stuffing_size += pad_packet_bytes;
00738 }
00739 pad_packet_bytes = 0;
00740 }
00741
00742 if (stuffing_size < 0)
00743 stuffing_size = 0;
00744
00745 if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
00746 if (payload_size < av_fifo_size(stream->fifo))
00747 stuffing_size += payload_size % stream->lpcm_align;
00748 }
00749
00750 if (stuffing_size > 16) {
00751 pad_packet_bytes += stuffing_size;
00752 packet_size -= stuffing_size;
00753 payload_size -= stuffing_size;
00754 stuffing_size = 0;
00755 }
00756
00757 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
00758
00759 avio_wb32(ctx->pb, startcode);
00760
00761 avio_wb16(ctx->pb, packet_size);
00762
00763 if (!s->is_mpeg2)
00764 for(i=0;i<stuffing_size;i++)
00765 avio_w8(ctx->pb, 0xff);
00766
00767 if (s->is_mpeg2) {
00768 avio_w8(ctx->pb, 0x80);
00769
00770 pes_flags=0;
00771
00772 if (pts != AV_NOPTS_VALUE) {
00773 pes_flags |= 0x80;
00774 if (dts != pts)
00775 pes_flags |= 0x40;
00776 }
00777
00778
00779
00780
00781
00782 if (stream->packet_number == 0)
00783 pes_flags |= 0x01;
00784
00785 avio_w8(ctx->pb, pes_flags);
00786 avio_w8(ctx->pb, header_len - 3 + stuffing_size);
00787
00788 if (pes_flags & 0x80)
00789 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
00790 if (pes_flags & 0x40)
00791 put_timestamp(ctx->pb, 0x01, dts);
00792
00793 if (pes_flags & 0x01) {
00794 avio_w8(ctx->pb, 0x10);
00795
00796
00797 if ((id & 0xe0) == AUDIO_ID)
00798 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
00799 else
00800 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
00801 }
00802
00803 } else {
00804 if (pts != AV_NOPTS_VALUE) {
00805 if (dts != pts) {
00806 put_timestamp(ctx->pb, 0x03, pts);
00807 put_timestamp(ctx->pb, 0x01, dts);
00808 } else {
00809 put_timestamp(ctx->pb, 0x02, pts);
00810 }
00811 } else {
00812 avio_w8(ctx->pb, 0x0f);
00813 }
00814 }
00815
00816 if (s->is_mpeg2) {
00817
00818
00819 avio_w8(ctx->pb, 0xff);
00820
00821 for(i=0;i<stuffing_size;i++)
00822 avio_w8(ctx->pb, 0xff);
00823 }
00824
00825 if (startcode == PRIVATE_STREAM_1) {
00826 avio_w8(ctx->pb, id);
00827 if (id >= 0xa0) {
00828
00829 avio_w8(ctx->pb, 7);
00830 avio_wb16(ctx->pb, 4);
00831 avio_w8(ctx->pb, stream->lpcm_header[0]);
00832 avio_w8(ctx->pb, stream->lpcm_header[1]);
00833 avio_w8(ctx->pb, stream->lpcm_header[2]);
00834 } else if (id >= 0x40) {
00835
00836 avio_w8(ctx->pb, nb_frames);
00837 avio_wb16(ctx->pb, trailer_size+1);
00838 }
00839 }
00840
00841
00842 assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
00843 av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, (void*)avio_write);
00844 stream->bytes_to_iframe -= payload_size - stuffing_size;
00845 }else{
00846 payload_size=
00847 stuffing_size= 0;
00848 }
00849
00850 if (pad_packet_bytes > 0)
00851 put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
00852
00853 for(i=0;i<zero_trail_bytes;i++)
00854 avio_w8(ctx->pb, 0x00);
00855
00856 avio_flush(ctx->pb);
00857
00858 s->packet_number++;
00859
00860
00861
00862
00863 if (!general_pack)
00864 stream->packet_number++;
00865
00866 return payload_size - stuffing_size;
00867 }
00868
00869 static void put_vcd_padding_sector(AVFormatContext *ctx)
00870 {
00871
00872
00873
00874
00875
00876
00877 MpegMuxContext *s = ctx->priv_data;
00878 int i;
00879
00880 for(i=0;i<s->packet_size;i++)
00881 avio_w8(ctx->pb, 0);
00882
00883 s->vcd_padding_bytes_written += s->packet_size;
00884
00885 avio_flush(ctx->pb);
00886
00887
00888
00889
00890
00891 s->packet_number++;
00892 }
00893
00894 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
00895
00896 int i;
00897
00898 for(i=0; i<ctx->nb_streams; i++){
00899 AVStream *st = ctx->streams[i];
00900 StreamInfo *stream = st->priv_data;
00901 PacketDesc *pkt_desc;
00902
00903 while((pkt_desc= stream->predecode_packet)
00904 && scr > pkt_desc->dts){
00905 if(stream->buffer_index < pkt_desc->size ||
00906 stream->predecode_packet == stream->premux_packet){
00907 av_log(ctx, AV_LOG_ERROR,
00908 "buffer underflow i=%d bufi=%d size=%d\n",
00909 i, stream->buffer_index, pkt_desc->size);
00910 break;
00911 }
00912 stream->buffer_index -= pkt_desc->size;
00913
00914 stream->predecode_packet= pkt_desc->next;
00915 av_freep(&pkt_desc);
00916 }
00917 }
00918
00919 return 0;
00920 }
00921
00922 static int output_packet(AVFormatContext *ctx, int flush){
00923 MpegMuxContext *s = ctx->priv_data;
00924 AVStream *st;
00925 StreamInfo *stream;
00926 int i, avail_space=0, es_size, trailer_size;
00927 int best_i= -1;
00928 int best_score= INT_MIN;
00929 int ignore_constraints=0;
00930 int64_t scr= s->last_scr;
00931 PacketDesc *timestamp_packet;
00932 const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
00933
00934 retry:
00935 for(i=0; i<ctx->nb_streams; i++){
00936 AVStream *st = ctx->streams[i];
00937 StreamInfo *stream = st->priv_data;
00938 const int avail_data= av_fifo_size(stream->fifo);
00939 const int space= stream->max_buffer_size - stream->buffer_index;
00940 int rel_space= 1024LL*space / stream->max_buffer_size;
00941 PacketDesc *next_pkt= stream->premux_packet;
00942
00943
00944
00945 if(s->packet_size > avail_data && !flush
00946 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
00947 return 0;
00948 if(avail_data==0)
00949 continue;
00950 assert(avail_data>0);
00951
00952 if(space < s->packet_size && !ignore_constraints)
00953 continue;
00954
00955 if(next_pkt && next_pkt->dts - scr > max_delay)
00956 continue;
00957
00958 if(rel_space > best_score){
00959 best_score= rel_space;
00960 best_i = i;
00961 avail_space= space;
00962 }
00963 }
00964
00965 if(best_i < 0){
00966 int64_t best_dts= INT64_MAX;
00967
00968 for(i=0; i<ctx->nb_streams; i++){
00969 AVStream *st = ctx->streams[i];
00970 StreamInfo *stream = st->priv_data;
00971 PacketDesc *pkt_desc= stream->predecode_packet;
00972 if(pkt_desc && pkt_desc->dts < best_dts)
00973 best_dts= pkt_desc->dts;
00974 }
00975
00976 av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
00977 scr / 90000.0, best_dts / 90000.0);
00978 if(best_dts == INT64_MAX)
00979 return 0;
00980
00981 if(scr >= best_dts+1 && !ignore_constraints){
00982 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
00983 ignore_constraints= 1;
00984 }
00985 scr= FFMAX(best_dts+1, scr);
00986 if(remove_decoded_packets(ctx, scr) < 0)
00987 return -1;
00988 goto retry;
00989 }
00990
00991 assert(best_i >= 0);
00992
00993 st = ctx->streams[best_i];
00994 stream = st->priv_data;
00995
00996 assert(av_fifo_size(stream->fifo) > 0);
00997
00998 assert(avail_space >= s->packet_size || ignore_constraints);
00999
01000 timestamp_packet= stream->premux_packet;
01001 if(timestamp_packet->unwritten_size == timestamp_packet->size){
01002 trailer_size= 0;
01003 }else{
01004 trailer_size= timestamp_packet->unwritten_size;
01005 timestamp_packet= timestamp_packet->next;
01006 }
01007
01008 if(timestamp_packet){
01009
01010 es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
01011 }else{
01012 assert(av_fifo_size(stream->fifo) == trailer_size);
01013 es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
01014 }
01015
01016 if (s->is_vcd) {
01017
01018
01019 int vcd_pad_bytes;
01020
01021 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){
01022 put_vcd_padding_sector(ctx);
01023 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL);
01024 }
01025 }
01026
01027 stream->buffer_index += es_size;
01028 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL);
01029
01030 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
01031 es_size -= stream->premux_packet->unwritten_size;
01032 stream->premux_packet= stream->premux_packet->next;
01033 }
01034 if(es_size)
01035 stream->premux_packet->unwritten_size -= es_size;
01036
01037 if(remove_decoded_packets(ctx, s->last_scr) < 0)
01038 return -1;
01039
01040 return 1;
01041 }
01042
01043 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
01044 {
01045 MpegMuxContext *s = ctx->priv_data;
01046 int stream_index= pkt->stream_index;
01047 int size= pkt->size;
01048 uint8_t *buf= pkt->data;
01049 AVStream *st = ctx->streams[stream_index];
01050 StreamInfo *stream = st->priv_data;
01051 int64_t pts, dts;
01052 PacketDesc *pkt_desc;
01053 int preload;
01054 const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY);
01055
01056 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
01057
01058 pts= pkt->pts;
01059 dts= pkt->dts;
01060
01061 if(pts != AV_NOPTS_VALUE) pts += 2*preload;
01062 if(dts != AV_NOPTS_VALUE){
01063 if(!s->last_scr)
01064 s->last_scr= dts + preload;
01065 dts += 2*preload;
01066 }
01067
01068
01069 if (!stream->premux_packet)
01070 stream->next_packet = &stream->premux_packet;
01071 *stream->next_packet=
01072 pkt_desc= av_mallocz(sizeof(PacketDesc));
01073 pkt_desc->pts= pts;
01074 pkt_desc->dts= dts;
01075 pkt_desc->unwritten_size=
01076 pkt_desc->size= size;
01077 if(!stream->predecode_packet)
01078 stream->predecode_packet= pkt_desc;
01079 stream->next_packet= &pkt_desc->next;
01080
01081 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
01082 return -1;
01083
01084 if (s->is_dvd){
01085 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) {
01086 stream->bytes_to_iframe = av_fifo_size(stream->fifo);
01087 stream->align_iframe = 1;
01088 stream->vobu_start_pts = pts;
01089 }
01090 }
01091
01092 av_fifo_generic_write(stream->fifo, buf, size, NULL);
01093
01094 for(;;){
01095 int ret= output_packet(ctx, 0);
01096 if(ret<=0)
01097 return ret;
01098 }
01099 }
01100
01101 static int mpeg_mux_end(AVFormatContext *ctx)
01102 {
01103
01104 StreamInfo *stream;
01105 int i;
01106
01107 for(;;){
01108 int ret= output_packet(ctx, 1);
01109 if(ret<0)
01110 return ret;
01111 else if(ret==0)
01112 break;
01113 }
01114
01115
01116
01117
01118
01119
01120
01121 for(i=0;i<ctx->nb_streams;i++) {
01122 stream = ctx->streams[i]->priv_data;
01123
01124 assert(av_fifo_size(stream->fifo) == 0);
01125 av_fifo_free(stream->fifo);
01126 }
01127 return 0;
01128 }
01129
01130 #define OFFSET(x) offsetof(MpegMuxContext, x)
01131 #define E AV_OPT_FLAG_ENCODING_PARAM
01132 static const AVOption options[] = {
01133 { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
01134 { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX, E},
01135 { NULL },
01136 };
01137
01138 #define MPEGENC_CLASS(flavor)\
01139 static const AVClass flavor ## _class = {\
01140 .class_name = #flavor " muxer",\
01141 .item_name = av_default_item_name,\
01142 .version = LIBAVUTIL_VERSION_INT,\
01143 .option = options,\
01144 };
01145
01146 #if CONFIG_MPEG1SYSTEM_MUXER
01147 MPEGENC_CLASS(mpeg)
01148 AVOutputFormat ff_mpeg1system_muxer = {
01149 .name = "mpeg",
01150 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
01151 .mime_type = "video/mpeg",
01152 .extensions = "mpg,mpeg",
01153 .priv_data_size = sizeof(MpegMuxContext),
01154 .audio_codec = AV_CODEC_ID_MP2,
01155 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
01156 .write_header = mpeg_mux_init,
01157 .write_packet = mpeg_mux_write_packet,
01158 .write_trailer = mpeg_mux_end,
01159 .priv_class = &mpeg_class,
01160 };
01161 #endif
01162 #if CONFIG_MPEG1VCD_MUXER
01163 MPEGENC_CLASS(vcd)
01164 AVOutputFormat ff_mpeg1vcd_muxer = {
01165 .name = "vcd",
01166 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
01167 .mime_type = "video/mpeg",
01168 .priv_data_size = sizeof(MpegMuxContext),
01169 .audio_codec = AV_CODEC_ID_MP2,
01170 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
01171 .write_header = mpeg_mux_init,
01172 .write_packet = mpeg_mux_write_packet,
01173 .write_trailer = mpeg_mux_end,
01174 .priv_class = &vcd_class,
01175 };
01176 #endif
01177 #if CONFIG_MPEG2VOB_MUXER
01178 MPEGENC_CLASS(vob)
01179 AVOutputFormat ff_mpeg2vob_muxer = {
01180 .name = "vob",
01181 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
01182 .mime_type = "video/mpeg",
01183 .extensions = "vob",
01184 .priv_data_size = sizeof(MpegMuxContext),
01185 .audio_codec = AV_CODEC_ID_MP2,
01186 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
01187 .write_header = mpeg_mux_init,
01188 .write_packet = mpeg_mux_write_packet,
01189 .write_trailer = mpeg_mux_end,
01190 .priv_class = &vob_class,
01191 };
01192 #endif
01193
01194
01195 #if CONFIG_MPEG2SVCD_MUXER
01196 MPEGENC_CLASS(svcd)
01197 AVOutputFormat ff_mpeg2svcd_muxer = {
01198 .name = "svcd",
01199 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
01200 .mime_type = "video/mpeg",
01201 .extensions = "vob",
01202 .priv_data_size = sizeof(MpegMuxContext),
01203 .audio_codec = AV_CODEC_ID_MP2,
01204 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
01205 .write_header = mpeg_mux_init,
01206 .write_packet = mpeg_mux_write_packet,
01207 .write_trailer = mpeg_mux_end,
01208 .priv_class = &svcd_class,
01209 };
01210 #endif
01211
01212
01213 #if CONFIG_MPEG2DVD_MUXER
01214 MPEGENC_CLASS(dvd)
01215 AVOutputFormat ff_mpeg2dvd_muxer = {
01216 .name = "dvd",
01217 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
01218 .mime_type = "video/mpeg",
01219 .extensions = "dvd",
01220 .priv_data_size = sizeof(MpegMuxContext),
01221 .audio_codec = AV_CODEC_ID_MP2,
01222 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
01223 .write_header = mpeg_mux_init,
01224 .write_packet = mpeg_mux_write_packet,
01225 .write_trailer = mpeg_mux_end,
01226 .priv_class = &dvd_class,
01227 };
01228 #endif