00001 /* 00002 * MPEG1/2 muxer 00003 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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; /* in bytes */ 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; /* required packet size */ 00064 int packet_number; 00065 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */ 00066 int system_header_freq; 00067 int system_header_size; 00068 int mux_rate; /* bitrate in units of 50 bytes/s */ 00069 /* stream info */ 00070 int audio_bound; 00071 int video_bound; 00072 int is_mpeg2; 00073 int is_vcd; 00074 int is_svcd; 00075 int is_dvd; 00076 int64_t last_scr; /* current system clock */ 00077 00078 double vcd_padding_bitrate; //FIXME floats 00079 int64_t vcd_padding_bytes_written; 00080 00081 int preload; 00082 } MpegMuxContext; 00083 00084 extern AVOutputFormat ff_mpeg1vcd_muxer; 00085 extern AVOutputFormat ff_mpeg2dvd_muxer; 00086 extern AVOutputFormat ff_mpeg2svcd_muxer; 00087 extern AVOutputFormat ff_mpeg2vob_muxer; 00088 00089 static int put_pack_header(AVFormatContext *ctx, 00090 uint8_t *buf, int64_t timestamp) 00091 { 00092 MpegMuxContext *s = ctx->priv_data; 00093 PutBitContext pb; 00094 00095 init_put_bits(&pb, buf, 128); 00096 00097 put_bits32(&pb, PACK_START_CODE); 00098 if (s->is_mpeg2) { 00099 put_bits(&pb, 2, 0x1); 00100 } else { 00101 put_bits(&pb, 4, 0x2); 00102 } 00103 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07)); 00104 put_bits(&pb, 1, 1); 00105 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff)); 00106 put_bits(&pb, 1, 1); 00107 put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff)); 00108 put_bits(&pb, 1, 1); 00109 if (s->is_mpeg2) { 00110 /* clock extension */ 00111 put_bits(&pb, 9, 0); 00112 } 00113 put_bits(&pb, 1, 1); 00114 put_bits(&pb, 22, s->mux_rate); 00115 put_bits(&pb, 1, 1); 00116 if (s->is_mpeg2) { 00117 put_bits(&pb, 1, 1); 00118 put_bits(&pb, 5, 0x1f); /* reserved */ 00119 put_bits(&pb, 3, 0); /* stuffing length */ 00120 } 00121 flush_put_bits(&pb); 00122 return put_bits_ptr(&pb) - pb.buf; 00123 } 00124 00125 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id) 00126 { 00127 MpegMuxContext *s = ctx->priv_data; 00128 int size, i, private_stream_coded, id; 00129 PutBitContext pb; 00130 00131 init_put_bits(&pb, buf, 128); 00132 00133 put_bits32(&pb, SYSTEM_HEADER_START_CODE); 00134 put_bits(&pb, 16, 0); 00135 put_bits(&pb, 1, 1); 00136 00137 put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */ 00138 put_bits(&pb, 1, 1); /* marker */ 00139 if (s->is_vcd && only_for_stream_id==VIDEO_ID) { 00140 /* This header applies only to the video stream (see VCD standard p. IV-7)*/ 00141 put_bits(&pb, 6, 0); 00142 } else 00143 put_bits(&pb, 6, s->audio_bound); 00144 00145 if (s->is_vcd) { 00146 /* see VCD standard, p. IV-7*/ 00147 put_bits(&pb, 1, 0); 00148 put_bits(&pb, 1, 1); 00149 } else { 00150 put_bits(&pb, 1, 0); /* variable bitrate*/ 00151 put_bits(&pb, 1, 0); /* non constrainted bit stream */ 00152 } 00153 00154 if (s->is_vcd || s->is_dvd) { 00155 /* see VCD standard p IV-7 */ 00156 put_bits(&pb, 1, 1); /* audio locked */ 00157 put_bits(&pb, 1, 1); /* video locked */ 00158 } else { 00159 put_bits(&pb, 1, 0); /* audio locked */ 00160 put_bits(&pb, 1, 0); /* video locked */ 00161 } 00162 00163 put_bits(&pb, 1, 1); /* marker */ 00164 00165 if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) { 00166 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/ 00167 put_bits(&pb, 5, 0); 00168 } else 00169 put_bits(&pb, 5, s->video_bound); 00170 00171 if (s->is_dvd) { 00172 put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */ 00173 put_bits(&pb, 7, 0x7f); /* reserved byte */ 00174 } else 00175 put_bits(&pb, 8, 0xff); /* reserved byte */ 00176 00177 /* DVD-Video Stream_bound entries 00178 id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1) 00179 id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0) 00180 id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1) 00181 id (0xBF) private stream 2, NAV packs, set to 2x1024. */ 00182 if (s->is_dvd) { 00183 00184 int P_STD_max_video = 0; 00185 int P_STD_max_mpeg_audio = 0; 00186 int P_STD_max_mpeg_PS1 = 0; 00187 00188 for(i=0;i<ctx->nb_streams;i++) { 00189 StreamInfo *stream = ctx->streams[i]->priv_data; 00190 00191 id = stream->id; 00192 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) { 00193 P_STD_max_mpeg_PS1 = stream->max_buffer_size; 00194 } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) { 00195 P_STD_max_mpeg_audio = stream->max_buffer_size; 00196 } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) { 00197 P_STD_max_video = stream->max_buffer_size; 00198 } 00199 } 00200 00201 /* video */ 00202 put_bits(&pb, 8, 0xb9); /* stream ID */ 00203 put_bits(&pb, 2, 3); 00204 put_bits(&pb, 1, 1); 00205 put_bits(&pb, 13, P_STD_max_video / 1024); 00206 00207 /* audio */ 00208 if (P_STD_max_mpeg_audio == 0) 00209 P_STD_max_mpeg_audio = 4096; 00210 put_bits(&pb, 8, 0xb8); /* stream ID */ 00211 put_bits(&pb, 2, 3); 00212 put_bits(&pb, 1, 0); 00213 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128); 00214 00215 /* private stream 1 */ 00216 put_bits(&pb, 8, 0xbd); /* stream ID */ 00217 put_bits(&pb, 2, 3); 00218 put_bits(&pb, 1, 0); 00219 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128); 00220 00221 /* private stream 2 */ 00222 put_bits(&pb, 8, 0xbf); /* stream ID */ 00223 put_bits(&pb, 2, 3); 00224 put_bits(&pb, 1, 1); 00225 put_bits(&pb, 13, 2); 00226 } 00227 else { 00228 /* audio stream info */ 00229 private_stream_coded = 0; 00230 for(i=0;i<ctx->nb_streams;i++) { 00231 StreamInfo *stream = ctx->streams[i]->priv_data; 00232 00233 00234 /* For VCDs, only include the stream info for the stream 00235 that the pack which contains this system belongs to. 00236 (see VCD standard p. IV-7) */ 00237 if ( !s->is_vcd || stream->id==only_for_stream_id 00238 || only_for_stream_id==0) { 00239 00240 id = stream->id; 00241 if (id < 0xc0) { 00242 /* special case for private streams (AC-3 uses that) */ 00243 if (private_stream_coded) 00244 continue; 00245 private_stream_coded = 1; 00246 id = 0xbd; 00247 } 00248 put_bits(&pb, 8, id); /* stream ID */ 00249 put_bits(&pb, 2, 3); 00250 if (id < 0xe0) { 00251 /* audio */ 00252 put_bits(&pb, 1, 0); 00253 put_bits(&pb, 13, stream->max_buffer_size / 128); 00254 } else { 00255 /* video */ 00256 put_bits(&pb, 1, 1); 00257 put_bits(&pb, 13, stream->max_buffer_size / 1024); 00258 } 00259 } 00260 } 00261 } 00262 00263 flush_put_bits(&pb); 00264 size = put_bits_ptr(&pb) - pb.buf; 00265 /* patch packet size */ 00266 buf[4] = (size - 6) >> 8; 00267 buf[5] = (size - 6) & 0xff; 00268 00269 return size; 00270 } 00271 00272 static int get_system_header_size(AVFormatContext *ctx) 00273 { 00274 int buf_index, i, private_stream_coded; 00275 StreamInfo *stream; 00276 MpegMuxContext *s = ctx->priv_data; 00277 00278 if (s->is_dvd) 00279 return 18; // DVD-Video system headers are 18 bytes fixed length. 00280 00281 buf_index = 12; 00282 private_stream_coded = 0; 00283 for(i=0;i<ctx->nb_streams;i++) { 00284 stream = ctx->streams[i]->priv_data; 00285 if (stream->id < 0xc0) { 00286 if (private_stream_coded) 00287 continue; 00288 private_stream_coded = 1; 00289 } 00290 buf_index += 3; 00291 } 00292 return buf_index; 00293 } 00294 00295 static int mpeg_mux_init(AVFormatContext *ctx) 00296 { 00297 MpegMuxContext *s = ctx->priv_data; 00298 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j; 00299 AVStream *st; 00300 StreamInfo *stream; 00301 int audio_bitrate; 00302 int video_bitrate; 00303 00304 s->packet_number = 0; 00305 s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer); 00306 s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer); 00307 s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer) || 00308 (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer) || 00309 (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer)); 00310 s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer); 00311 00312 if(ctx->packet_size) { 00313 if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) { 00314 av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n", 00315 ctx->packet_size); 00316 goto fail; 00317 } 00318 s->packet_size = ctx->packet_size; 00319 } else 00320 s->packet_size = 2048; 00321 00322 s->vcd_padding_bytes_written = 0; 00323 s->vcd_padding_bitrate=0; 00324 00325 s->audio_bound = 0; 00326 s->video_bound = 0; 00327 mpa_id = AUDIO_ID; 00328 ac3_id = AC3_ID; 00329 dts_id = DTS_ID; 00330 mpv_id = VIDEO_ID; 00331 mps_id = SUB_ID; 00332 lpcm_id = LPCM_ID; 00333 for(i=0;i<ctx->nb_streams;i++) { 00334 st = ctx->streams[i]; 00335 stream = av_mallocz(sizeof(StreamInfo)); 00336 if (!stream) 00337 goto fail; 00338 st->priv_data = stream; 00339 00340 avpriv_set_pts_info(st, 64, 1, 90000); 00341 00342 switch(st->codec->codec_type) { 00343 case AVMEDIA_TYPE_AUDIO: 00344 if (st->codec->codec_id == CODEC_ID_AC3) { 00345 stream->id = ac3_id++; 00346 } else if (st->codec->codec_id == CODEC_ID_DTS) { 00347 stream->id = dts_id++; 00348 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) { 00349 stream->id = lpcm_id++; 00350 for(j = 0; j < 4; j++) { 00351 if (lpcm_freq_tab[j] == st->codec->sample_rate) 00352 break; 00353 } 00354 if (j == 4) 00355 goto fail; 00356 if (st->codec->channels > 8) 00357 return -1; 00358 stream->lpcm_header[0] = 0x0c; 00359 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4); 00360 stream->lpcm_header[2] = 0x80; 00361 stream->lpcm_align = st->codec->channels * 2; 00362 } else { 00363 stream->id = mpa_id++; 00364 } 00365 00366 /* This value HAS to be used for VCD (see VCD standard, p. IV-7). 00367 Right now it is also used for everything else.*/ 00368 stream->max_buffer_size = 4 * 1024; 00369 s->audio_bound++; 00370 break; 00371 case AVMEDIA_TYPE_VIDEO: 00372 stream->id = mpv_id++; 00373 if (st->codec->rc_buffer_size) 00374 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8; 00375 else { 00376 av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n"); 00377 stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default 00378 } 00379 #if 0 00380 /* see VCD standard, p. IV-7*/ 00381 stream->max_buffer_size = 46 * 1024; 00382 else 00383 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2). 00384 Right now it is also used for everything else.*/ 00385 stream->max_buffer_size = 230 * 1024; 00386 #endif 00387 s->video_bound++; 00388 break; 00389 case AVMEDIA_TYPE_SUBTITLE: 00390 stream->id = mps_id++; 00391 stream->max_buffer_size = 16 * 1024; 00392 break; 00393 default: 00394 return -1; 00395 } 00396 stream->fifo= av_fifo_alloc(16); 00397 if (!stream->fifo) 00398 goto fail; 00399 } 00400 bitrate = 0; 00401 audio_bitrate = 0; 00402 video_bitrate = 0; 00403 for(i=0;i<ctx->nb_streams;i++) { 00404 int codec_rate; 00405 st = ctx->streams[i]; 00406 stream = (StreamInfo*) st->priv_data; 00407 00408 if(st->codec->rc_max_rate || stream->id==VIDEO_ID) 00409 codec_rate= st->codec->rc_max_rate; 00410 else 00411 codec_rate= st->codec->bit_rate; 00412 00413 if(!codec_rate) 00414 codec_rate= (1<<21)*8*50/ctx->nb_streams; 00415 00416 bitrate += codec_rate; 00417 00418 if ((stream->id & 0xe0) == AUDIO_ID) 00419 audio_bitrate += codec_rate; 00420 else if (stream->id==VIDEO_ID) 00421 video_bitrate += codec_rate; 00422 } 00423 00424 #if FF_API_MUXRATE 00425 if(ctx->mux_rate){ 00426 s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50); 00427 } else 00428 #endif 00429 if (!s->mux_rate) { 00430 /* we increase slightly the bitrate to take into account the 00431 headers. XXX: compute it exactly */ 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 /* The VCD standard mandates that the mux_rate field is 3528 00441 (see standard p. IV-6). 00442 The value is actually "wrong", i.e. if you calculate 00443 it using the normal formula and the 75 sectors per second transfer 00444 rate you get a different value because the real pack size is 2324, 00445 not 2352. But the standard explicitly specifies that the mux_rate 00446 field in the header must have this value.*/ 00447 // s->mux_rate=2352 * 75 / 50; /* = 3528*/ 00448 00449 /* The VCD standard states that the muxed stream must be 00450 exactly 75 packs / second (the data rate of a single speed cdrom). 00451 Since the video bitrate (probably 1150000 bits/sec) will be below 00452 the theoretical maximum we have to add some padding packets 00453 to make up for the lower data rate. 00454 (cf. VCD standard p. IV-6 )*/ 00455 00456 /* Add the header overhead to the data rate. 00457 2279 data bytes per audio pack, 2294 data bytes per video pack*/ 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 /* Add padding so that the full bitrate is 2324*75 bytes/sec */ 00463 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate); 00464 } 00465 00466 if (s->is_vcd || s->is_mpeg2) 00467 /* every packet */ 00468 s->pack_header_freq = 1; 00469 else 00470 /* every 2 seconds */ 00471 s->pack_header_freq = 2 * bitrate / s->packet_size / 8; 00472 00473 /* the above seems to make pack_header_freq zero sometimes */ 00474 if (s->pack_header_freq == 0) 00475 s->pack_header_freq = 1; 00476 00477 if (s->is_mpeg2) 00478 /* every 200 packets. Need to look at the spec. */ 00479 s->system_header_freq = s->pack_header_freq * 40; 00480 else if (s->is_vcd) 00481 /* the standard mandates that there are only two system headers 00482 in the whole file: one in the first packet of each stream. 00483 (see standard p. IV-7 and IV-8) */ 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 /* return the number of padding bytes that should be inserted into 00514 the multiplexed stream.*/ 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); //FIXME this is wrong 00525 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written); 00526 00527 if (pad_bytes<0) 00528 /* might happen if we have already padded to a later timestamp. This 00529 can occur if another stream has already advanced further.*/ 00530 pad_bytes=0; 00531 } 00532 00533 return pad_bytes; 00534 } 00535 00536 00537 #if 0 /* unused, remove? */ 00538 /* return the exact available payload size for the next packet for 00539 stream 'stream_index'. 'pts' and 'dts' are only used to know if 00540 timestamps are needed in the packet header. */ 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 /* pack header size */ 00553 if (s->is_mpeg2) 00554 buf_index += 14; 00555 else 00556 buf_index += 12; 00557 00558 if (s->is_vcd) { 00559 /* there is exactly one system header for each stream in a VCD MPEG, 00560 One in the very first video packet and one in the very first 00561 audio packet (see VCD standard p. IV-7 and IV-8).*/ 00562 00563 if (stream->packet_number==0) 00564 /* The system headers refer only to the stream they occur in, 00565 so they have a constant size.*/ 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 /* the first pack of each stream contains only the pack header, 00577 the system header and some padding (see VCD standard p. IV-6) 00578 Add the padding size, so that the actual payload becomes 0.*/ 00579 buf_index += s->packet_size - buf_index; 00580 else { 00581 /* packet header size */ 00582 buf_index += 6; 00583 if (s->is_mpeg2) { 00584 buf_index += 3; 00585 if (stream->packet_number==0) 00586 buf_index += 3; /* PES extension */ 00587 buf_index += 1; /* obligatory stuffing byte */ 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 /* AC-3/LPCM private data header */ 00602 buf_index += 4; 00603 if (stream->id >= 0xa0) { 00604 int n; 00605 buf_index += 3; 00606 /* NOTE: we round the payload size to an integer number of 00607 LPCM samples */ 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 /* The VCD standard demands that 20 zero bytes follow 00616 each audio packet (see standard p. IV-8).*/ 00617 buf_index+=20; 00618 } 00619 return s->packet_size - buf_index; 00620 } 00621 #endif 00622 00623 /* Write an MPEG padding packet header. */ 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 /* flush the packet on stream stream_index */ 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; /*"general" pack without data specific to one stream?*/ 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 /* output pack and systems header if needed */ 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 /* there is exactly one system header for each stream in a VCD MPEG, 00685 One in the very first video packet and one in the very first 00686 audio packet (see VCD standard p. IV-7 and IV-8).*/ 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); // length 00711 avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI 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); // length 00717 avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI 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); //FIXME rounding and first few bytes of each packet 00726 size = put_pack_header(ctx, buf_ptr, scr); 00727 s->last_scr= scr; 00728 buf_ptr += size; 00729 /* GOP Start */ 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 /* The VCD standard demands that 20 zero bytes follow 00748 each audio pack (see standard p. IV-8).*/ 00749 zero_trail_bytes += 20; 00750 00751 if ((s->is_vcd && stream->packet_number==0) 00752 || (s->is_svcd && s->packet_number==0)) { 00753 /* for VCD the first pack of each stream contains only the pack header, 00754 the system header and lots of padding (see VCD standard p. IV-6). 00755 In the case of an audio pack, 20 zero bytes are also added at 00756 the end.*/ 00757 /* For SVCD we fill the very first pack to increase compatibility with 00758 some DVD players. Not mandated by the standard.*/ 00759 if (s->is_svcd) 00760 general_pack = 1; /* the system header refers to both streams and no stream data*/ 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 /* packet header size */ 00769 packet_size -= 6; 00770 00771 /* packet header */ 00772 if (s->is_mpeg2) { 00773 header_len = 3; 00774 if (stream->packet_number==0) 00775 header_len += 3; /* PES extension */ 00776 header_len += 1; /* obligatory stuffing byte */ 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 // first byte does not fit -> reset pts/dts + stuffing 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) { // can't use padding, so use stuffing 00826 packet_size += pad_packet_bytes; 00827 payload_size += pad_packet_bytes; // undo the previous adjustment 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 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/ 00839 pad_packet_bytes += stuffing_size; 00840 packet_size -= stuffing_size; 00841 payload_size -= stuffing_size; 00842 stuffing_size = 0; 00843 } 00844 00845 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size); 00846 00847 avio_wb32(ctx->pb, startcode); 00848 00849 avio_wb16(ctx->pb, packet_size); 00850 00851 if (!s->is_mpeg2) 00852 for(i=0;i<stuffing_size;i++) 00853 avio_w8(ctx->pb, 0xff); 00854 00855 if (s->is_mpeg2) { 00856 avio_w8(ctx->pb, 0x80); /* mpeg2 id */ 00857 00858 pes_flags=0; 00859 00860 if (pts != AV_NOPTS_VALUE) { 00861 pes_flags |= 0x80; 00862 if (dts != pts) 00863 pes_flags |= 0x40; 00864 } 00865 00866 /* Both the MPEG-2 and the SVCD standards demand that the 00867 P-STD_buffer_size field be included in the first packet of 00868 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2 00869 and MPEG-2 standard 2.7.7) */ 00870 if (stream->packet_number == 0) 00871 pes_flags |= 0x01; 00872 00873 avio_w8(ctx->pb, pes_flags); /* flags */ 00874 avio_w8(ctx->pb, header_len - 3 + stuffing_size); 00875 00876 if (pes_flags & 0x80) /*write pts*/ 00877 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts); 00878 if (pes_flags & 0x40) /*write dts*/ 00879 put_timestamp(ctx->pb, 0x01, dts); 00880 00881 if (pes_flags & 0x01) { /*write pes extension*/ 00882 avio_w8(ctx->pb, 0x10); /* flags */ 00883 00884 /* P-STD buffer info */ 00885 if ((id & 0xe0) == AUDIO_ID) 00886 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128); 00887 else 00888 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024); 00889 } 00890 00891 } else { 00892 if (pts != AV_NOPTS_VALUE) { 00893 if (dts != pts) { 00894 put_timestamp(ctx->pb, 0x03, pts); 00895 put_timestamp(ctx->pb, 0x01, dts); 00896 } else { 00897 put_timestamp(ctx->pb, 0x02, pts); 00898 } 00899 } else { 00900 avio_w8(ctx->pb, 0x0f); 00901 } 00902 } 00903 00904 if (s->is_mpeg2) { 00905 /* special stuffing byte that is always written 00906 to prevent accidental generation of start codes. */ 00907 avio_w8(ctx->pb, 0xff); 00908 00909 for(i=0;i<stuffing_size;i++) 00910 avio_w8(ctx->pb, 0xff); 00911 } 00912 00913 if (startcode == PRIVATE_STREAM_1) { 00914 avio_w8(ctx->pb, id); 00915 if (id >= 0xa0) { 00916 /* LPCM (XXX: check nb_frames) */ 00917 avio_w8(ctx->pb, 7); 00918 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */ 00919 avio_w8(ctx->pb, stream->lpcm_header[0]); 00920 avio_w8(ctx->pb, stream->lpcm_header[1]); 00921 avio_w8(ctx->pb, stream->lpcm_header[2]); 00922 } else if (id >= 0x40) { 00923 /* AC-3 */ 00924 avio_w8(ctx->pb, nb_frames); 00925 avio_wb16(ctx->pb, trailer_size+1); 00926 } 00927 } 00928 00929 /* output data */ 00930 assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo)); 00931 av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, (void*)avio_write); 00932 stream->bytes_to_iframe -= payload_size - stuffing_size; 00933 }else{ 00934 payload_size= 00935 stuffing_size= 0; 00936 } 00937 00938 if (pad_packet_bytes > 0) 00939 put_padding_packet(ctx,ctx->pb, pad_packet_bytes); 00940 00941 for(i=0;i<zero_trail_bytes;i++) 00942 avio_w8(ctx->pb, 0x00); 00943 00944 avio_flush(ctx->pb); 00945 00946 s->packet_number++; 00947 00948 /* only increase the stream packet number if this pack actually contains 00949 something that is specific to this stream! I.e. a dedicated header 00950 or some data.*/ 00951 if (!general_pack) 00952 stream->packet_number++; 00953 00954 return payload_size - stuffing_size; 00955 } 00956 00957 static void put_vcd_padding_sector(AVFormatContext *ctx) 00958 { 00959 /* There are two ways to do this padding: writing a sector/pack 00960 of 0 values, or writing an MPEG padding pack. Both seem to 00961 work with most decoders, BUT the VCD standard only allows a 0-sector 00962 (see standard p. IV-4, IV-5). 00963 So a 0-sector it is...*/ 00964 00965 MpegMuxContext *s = ctx->priv_data; 00966 int i; 00967 00968 for(i=0;i<s->packet_size;i++) 00969 avio_w8(ctx->pb, 0); 00970 00971 s->vcd_padding_bytes_written += s->packet_size; 00972 00973 avio_flush(ctx->pb); 00974 00975 /* increasing the packet number is correct. The SCR of the following packs 00976 is calculated from the packet_number and it has to include the padding 00977 sector (it represents the sector index, not the MPEG pack index) 00978 (see VCD standard p. IV-6)*/ 00979 s->packet_number++; 00980 } 00981 00982 #if 0 /* unused, remove? */ 00983 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts) 00984 { 00985 MpegMuxContext *s = ctx->priv_data; 00986 int64_t scr; 00987 00988 /* Since the data delivery rate is constant, SCR is computed 00989 using the formula C + i * 1200 where C is the start constant 00990 and i is the pack index. 00991 It is recommended that SCR 0 is at the beginning of the VCD front 00992 margin (a sequence of empty Form 2 sectors on the CD). 00993 It is recommended that the front margin is 30 sectors long, so 00994 we use C = 30*1200 = 36000 00995 (Note that even if the front margin is not 30 sectors the file 00996 will still be correct according to the standard. It just won't have 00997 the "recommended" value).*/ 00998 scr = 36000 + s->packet_number * 1200; 00999 01000 return scr; 01001 } 01002 #endif 01003 01004 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){ 01005 // MpegMuxContext *s = ctx->priv_data; 01006 int i; 01007 01008 for(i=0; i<ctx->nb_streams; i++){ 01009 AVStream *st = ctx->streams[i]; 01010 StreamInfo *stream = st->priv_data; 01011 PacketDesc *pkt_desc; 01012 01013 while((pkt_desc= stream->predecode_packet) 01014 && scr > pkt_desc->dts){ //FIXME > vs >= 01015 if(stream->buffer_index < pkt_desc->size || 01016 stream->predecode_packet == stream->premux_packet){ 01017 av_log(ctx, AV_LOG_ERROR, 01018 "buffer underflow i=%d bufi=%d size=%d\n", 01019 i, stream->buffer_index, pkt_desc->size); 01020 break; 01021 } 01022 stream->buffer_index -= pkt_desc->size; 01023 01024 stream->predecode_packet= pkt_desc->next; 01025 av_freep(&pkt_desc); 01026 } 01027 } 01028 01029 return 0; 01030 } 01031 01032 static int output_packet(AVFormatContext *ctx, int flush){ 01033 MpegMuxContext *s = ctx->priv_data; 01034 AVStream *st; 01035 StreamInfo *stream; 01036 int i, avail_space=0, es_size, trailer_size; 01037 int best_i= -1; 01038 int best_score= INT_MIN; 01039 int ignore_constraints=0; 01040 int64_t scr= s->last_scr; 01041 PacketDesc *timestamp_packet; 01042 const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE); 01043 01044 retry: 01045 for(i=0; i<ctx->nb_streams; i++){ 01046 AVStream *st = ctx->streams[i]; 01047 StreamInfo *stream = st->priv_data; 01048 const int avail_data= av_fifo_size(stream->fifo); 01049 const int space= stream->max_buffer_size - stream->buffer_index; 01050 int rel_space= 1024LL*space / stream->max_buffer_size; 01051 PacketDesc *next_pkt= stream->premux_packet; 01052 01053 /* for subtitle, a single PES packet must be generated, 01054 so we flush after every single subtitle packet */ 01055 if(s->packet_size > avail_data && !flush 01056 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) 01057 return 0; 01058 if(avail_data==0) 01059 continue; 01060 assert(avail_data>0); 01061 01062 if(space < s->packet_size && !ignore_constraints) 01063 continue; 01064 01065 if(next_pkt && next_pkt->dts - scr > max_delay) 01066 continue; 01067 01068 if(rel_space > best_score){ 01069 best_score= rel_space; 01070 best_i = i; 01071 avail_space= space; 01072 } 01073 } 01074 01075 if(best_i < 0){ 01076 int64_t best_dts= INT64_MAX; 01077 01078 for(i=0; i<ctx->nb_streams; i++){ 01079 AVStream *st = ctx->streams[i]; 01080 StreamInfo *stream = st->priv_data; 01081 PacketDesc *pkt_desc= stream->predecode_packet; 01082 if(pkt_desc && pkt_desc->dts < best_dts) 01083 best_dts= pkt_desc->dts; 01084 } 01085 01086 av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n", 01087 scr / 90000.0, best_dts / 90000.0); 01088 if(best_dts == INT64_MAX) 01089 return 0; 01090 01091 if(scr >= best_dts+1 && !ignore_constraints){ 01092 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n"); 01093 ignore_constraints= 1; 01094 } 01095 scr= FFMAX(best_dts+1, scr); 01096 if(remove_decoded_packets(ctx, scr) < 0) 01097 return -1; 01098 goto retry; 01099 } 01100 01101 assert(best_i >= 0); 01102 01103 st = ctx->streams[best_i]; 01104 stream = st->priv_data; 01105 01106 assert(av_fifo_size(stream->fifo) > 0); 01107 01108 assert(avail_space >= s->packet_size || ignore_constraints); 01109 01110 timestamp_packet= stream->premux_packet; 01111 if(timestamp_packet->unwritten_size == timestamp_packet->size){ 01112 trailer_size= 0; 01113 }else{ 01114 trailer_size= timestamp_packet->unwritten_size; 01115 timestamp_packet= timestamp_packet->next; 01116 } 01117 01118 if(timestamp_packet){ 01119 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f scr:%f stream:%d\n", timestamp_packet->dts/90000.0, timestamp_packet->pts/90000.0, scr/90000.0, best_i); 01120 es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size); 01121 }else{ 01122 assert(av_fifo_size(stream->fifo) == trailer_size); 01123 es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size); 01124 } 01125 01126 if (s->is_vcd) { 01127 /* Write one or more padding sectors, if necessary, to reach 01128 the constant overall bitrate.*/ 01129 int vcd_pad_bytes; 01130 01131 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here 01132 put_vcd_padding_sector(ctx); 01133 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet 01134 } 01135 } 01136 01137 stream->buffer_index += es_size; 01138 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet 01139 01140 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){ 01141 es_size -= stream->premux_packet->unwritten_size; 01142 stream->premux_packet= stream->premux_packet->next; 01143 } 01144 if(es_size) 01145 stream->premux_packet->unwritten_size -= es_size; 01146 01147 if(remove_decoded_packets(ctx, s->last_scr) < 0) 01148 return -1; 01149 01150 return 1; 01151 } 01152 01153 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) 01154 { 01155 MpegMuxContext *s = ctx->priv_data; 01156 int stream_index= pkt->stream_index; 01157 int size= pkt->size; 01158 uint8_t *buf= pkt->data; 01159 AVStream *st = ctx->streams[stream_index]; 01160 StreamInfo *stream = st->priv_data; 01161 int64_t pts, dts; 01162 PacketDesc *pkt_desc; 01163 int preload; 01164 const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY); 01165 01166 #if FF_API_PRELOAD 01167 if (ctx->preload) 01168 s->preload = ctx->preload; 01169 #endif 01170 preload = av_rescale(s->preload, 90000, AV_TIME_BASE); 01171 01172 pts= pkt->pts; 01173 dts= pkt->dts; 01174 01175 if(pts != AV_NOPTS_VALUE) pts += 2*preload; 01176 if(dts != AV_NOPTS_VALUE){ 01177 if(!s->last_scr) 01178 s->last_scr= dts + preload; 01179 dts += 2*preload; 01180 } 01181 01182 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE); 01183 if (!stream->premux_packet) 01184 stream->next_packet = &stream->premux_packet; 01185 *stream->next_packet= 01186 pkt_desc= av_mallocz(sizeof(PacketDesc)); 01187 pkt_desc->pts= pts; 01188 pkt_desc->dts= dts; 01189 pkt_desc->unwritten_size= 01190 pkt_desc->size= size; 01191 if(!stream->predecode_packet) 01192 stream->predecode_packet= pkt_desc; 01193 stream->next_packet= &pkt_desc->next; 01194 01195 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0) 01196 return -1; 01197 01198 if (s->is_dvd){ 01199 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder) 01200 stream->bytes_to_iframe = av_fifo_size(stream->fifo); 01201 stream->align_iframe = 1; 01202 stream->vobu_start_pts = pts; 01203 } 01204 } 01205 01206 av_fifo_generic_write(stream->fifo, buf, size, NULL); 01207 01208 for(;;){ 01209 int ret= output_packet(ctx, 0); 01210 if(ret<=0) 01211 return ret; 01212 } 01213 } 01214 01215 static int mpeg_mux_end(AVFormatContext *ctx) 01216 { 01217 // MpegMuxContext *s = ctx->priv_data; 01218 StreamInfo *stream; 01219 int i; 01220 01221 for(;;){ 01222 int ret= output_packet(ctx, 1); 01223 if(ret<0) 01224 return ret; 01225 else if(ret==0) 01226 break; 01227 } 01228 01229 /* End header according to MPEG1 systems standard. We do not write 01230 it as it is usually not needed by decoders and because it 01231 complicates MPEG stream concatenation. */ 01232 //avio_wb32(ctx->pb, ISO_11172_END_CODE); 01233 //avio_flush(ctx->pb); 01234 01235 for(i=0;i<ctx->nb_streams;i++) { 01236 stream = ctx->streams[i]->priv_data; 01237 01238 assert(av_fifo_size(stream->fifo) == 0); 01239 av_fifo_free(stream->fifo); 01240 } 01241 return 0; 01242 } 01243 01244 #define OFFSET(x) offsetof(MpegMuxContext, x) 01245 #define E AV_OPT_FLAG_ENCODING_PARAM 01246 static const AVOption options[] = { 01247 { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, {0}, 0, INT_MAX, E }, 01248 { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, {500000}, 0, INT_MAX, E}, 01249 { NULL }, 01250 }; 01251 01252 #define MPEGENC_CLASS(flavor)\ 01253 static const AVClass flavor ## _class = {\ 01254 .class_name = #flavor " muxer",\ 01255 .item_name = av_default_item_name,\ 01256 .version = LIBAVUTIL_VERSION_INT,\ 01257 .option = options,\ 01258 }; 01259 01260 #if CONFIG_MPEG1SYSTEM_MUXER 01261 MPEGENC_CLASS(mpeg) 01262 AVOutputFormat ff_mpeg1system_muxer = { 01263 .name = "mpeg", 01264 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 System format"), 01265 .mime_type = "video/mpeg", 01266 .extensions = "mpg,mpeg", 01267 .priv_data_size = sizeof(MpegMuxContext), 01268 .audio_codec = CODEC_ID_MP2, 01269 .video_codec = CODEC_ID_MPEG1VIDEO, 01270 .write_header = mpeg_mux_init, 01271 .write_packet = mpeg_mux_write_packet, 01272 .write_trailer = mpeg_mux_end, 01273 .priv_class = &mpeg_class, 01274 }; 01275 #endif 01276 #if CONFIG_MPEG1VCD_MUXER 01277 MPEGENC_CLASS(vcd) 01278 AVOutputFormat ff_mpeg1vcd_muxer = { 01279 .name = "vcd", 01280 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"), 01281 .mime_type = "video/mpeg", 01282 .priv_data_size = sizeof(MpegMuxContext), 01283 .audio_codec = CODEC_ID_MP2, 01284 .video_codec = CODEC_ID_MPEG1VIDEO, 01285 .write_header = mpeg_mux_init, 01286 .write_packet = mpeg_mux_write_packet, 01287 .write_trailer = mpeg_mux_end, 01288 .priv_class = &vcd_class, 01289 }; 01290 #endif 01291 #if CONFIG_MPEG2VOB_MUXER 01292 MPEGENC_CLASS(vob) 01293 AVOutputFormat ff_mpeg2vob_muxer = { 01294 .name = "vob", 01295 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"), 01296 .mime_type = "video/mpeg", 01297 .extensions = "vob", 01298 .priv_data_size = sizeof(MpegMuxContext), 01299 .audio_codec = CODEC_ID_MP2, 01300 .video_codec = CODEC_ID_MPEG2VIDEO, 01301 .write_header = mpeg_mux_init, 01302 .write_packet = mpeg_mux_write_packet, 01303 .write_trailer = mpeg_mux_end, 01304 .priv_class = &vob_class, 01305 }; 01306 #endif 01307 01308 /* Same as mpeg2vob_mux except that the pack size is 2324 */ 01309 #if CONFIG_MPEG2SVCD_MUXER 01310 MPEGENC_CLASS(svcd) 01311 AVOutputFormat ff_mpeg2svcd_muxer = { 01312 .name = "svcd", 01313 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"), 01314 .mime_type = "video/mpeg", 01315 .extensions = "vob", 01316 .priv_data_size = sizeof(MpegMuxContext), 01317 .audio_codec = CODEC_ID_MP2, 01318 .video_codec = CODEC_ID_MPEG2VIDEO, 01319 .write_header = mpeg_mux_init, 01320 .write_packet = mpeg_mux_write_packet, 01321 .write_trailer = mpeg_mux_end, 01322 .priv_class = &svcd_class, 01323 }; 01324 #endif 01325 01326 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */ 01327 #if CONFIG_MPEG2DVD_MUXER 01328 MPEGENC_CLASS(dvd) 01329 AVOutputFormat ff_mpeg2dvd_muxer = { 01330 .name = "dvd", 01331 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"), 01332 .mime_type = "video/mpeg", 01333 .extensions = "dvd", 01334 .priv_data_size = sizeof(MpegMuxContext), 01335 .audio_codec = CODEC_ID_MP2, 01336 .video_codec = CODEC_ID_MPEG2VIDEO, 01337 .write_header = mpeg_mux_init, 01338 .write_packet = mpeg_mux_write_packet, 01339 .write_trailer = mpeg_mux_end, 01340 .priv_class = &dvd_class, 01341 }; 01342 #endif