Libav 0.7.1
|
00001 /* 00002 * MPEG1/2 muxer 00003 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; 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 "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; /* in bytes */ 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; /* required packet size */ 00059 int packet_number; 00060 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */ 00061 int system_header_freq; 00062 int system_header_size; 00063 int mux_rate; /* bitrate in units of 50 bytes/s */ 00064 /* stream info */ 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; /* current system clock */ 00072 00073 double vcd_padding_bitrate; //FIXME floats 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 /* clock extension */ 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); /* reserved */ 00113 put_bits(&pb, 3, 0); /* stuffing length */ 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); /* maximum bit rate of the multiplexed stream */ 00132 put_bits(&pb, 1, 1); /* marker */ 00133 if (s->is_vcd && only_for_stream_id==VIDEO_ID) { 00134 /* This header applies only to the video stream (see VCD standard p. IV-7)*/ 00135 put_bits(&pb, 6, 0); 00136 } else 00137 put_bits(&pb, 6, s->audio_bound); 00138 00139 if (s->is_vcd) { 00140 /* see VCD standard, p. IV-7*/ 00141 put_bits(&pb, 1, 0); 00142 put_bits(&pb, 1, 1); 00143 } else { 00144 put_bits(&pb, 1, 0); /* variable bitrate*/ 00145 put_bits(&pb, 1, 0); /* non constrainted bit stream */ 00146 } 00147 00148 if (s->is_vcd || s->is_dvd) { 00149 /* see VCD standard p IV-7 */ 00150 put_bits(&pb, 1, 1); /* audio locked */ 00151 put_bits(&pb, 1, 1); /* video locked */ 00152 } else { 00153 put_bits(&pb, 1, 0); /* audio locked */ 00154 put_bits(&pb, 1, 0); /* video locked */ 00155 } 00156 00157 put_bits(&pb, 1, 1); /* marker */ 00158 00159 if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) { 00160 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/ 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); /* packet_rate_restriction_flag */ 00167 put_bits(&pb, 7, 0x7f); /* reserved byte */ 00168 } else 00169 put_bits(&pb, 8, 0xff); /* reserved byte */ 00170 00171 /* DVD-Video Stream_bound entries 00172 id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1) 00173 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) 00174 id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1) 00175 id (0xBF) private stream 2, NAV packs, set to 2x1024. */ 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 /* video */ 00196 put_bits(&pb, 8, 0xb9); /* stream ID */ 00197 put_bits(&pb, 2, 3); 00198 put_bits(&pb, 1, 1); 00199 put_bits(&pb, 13, P_STD_max_video / 1024); 00200 00201 /* audio */ 00202 if (P_STD_max_mpeg_audio == 0) 00203 P_STD_max_mpeg_audio = 4096; 00204 put_bits(&pb, 8, 0xb8); /* stream ID */ 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 /* private stream 1 */ 00210 put_bits(&pb, 8, 0xbd); /* stream ID */ 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 /* private stream 2 */ 00216 put_bits(&pb, 8, 0xbf); /* stream ID */ 00217 put_bits(&pb, 2, 3); 00218 put_bits(&pb, 1, 1); 00219 put_bits(&pb, 13, 2); 00220 } 00221 else { 00222 /* audio stream info */ 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 /* For VCDs, only include the stream info for the stream 00229 that the pack which contains this system belongs to. 00230 (see VCD standard p. IV-7) */ 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 /* special case for private streams (AC-3 uses that) */ 00237 if (private_stream_coded) 00238 continue; 00239 private_stream_coded = 1; 00240 id = 0xbd; 00241 } 00242 put_bits(&pb, 8, id); /* stream ID */ 00243 put_bits(&pb, 2, 3); 00244 if (id < 0xe0) { 00245 /* audio */ 00246 put_bits(&pb, 1, 0); 00247 put_bits(&pb, 13, stream->max_buffer_size / 128); 00248 } else { 00249 /* video */ 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 /* patch packet size */ 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; // DVD-Video system headers are 18 bytes fixed length. 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 /* This value HAS to be used for VCD (see VCD standard, p. IV-7). 00361 Right now it is also used for everything else.*/ 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; //FIXME this is probably too small as default 00372 } 00373 #if 0 00374 /* see VCD standard, p. IV-7*/ 00375 stream->max_buffer_size = 46 * 1024; 00376 else 00377 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2). 00378 Right now it is also used for everything else.*/ 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 /* we increase slightly the bitrate to take into account the 00422 headers. XXX: compute it exactly */ 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 /* The VCD standard mandates that the mux_rate field is 3528 00432 (see standard p. IV-6). 00433 The value is actually "wrong", i.e. if you calculate 00434 it using the normal formula and the 75 sectors per second transfer 00435 rate you get a different value because the real pack size is 2324, 00436 not 2352. But the standard explicitly specifies that the mux_rate 00437 field in the header must have this value.*/ 00438 // s->mux_rate=2352 * 75 / 50; /* = 3528*/ 00439 00440 /* The VCD standard states that the muxed stream must be 00441 exactly 75 packs / second (the data rate of a single speed cdrom). 00442 Since the video bitrate (probably 1150000 bits/sec) will be below 00443 the theoretical maximum we have to add some padding packets 00444 to make up for the lower data rate. 00445 (cf. VCD standard p. IV-6 )*/ 00446 00447 /* Add the header overhead to the data rate. 00448 2279 data bytes per audio pack, 2294 data bytes per video pack*/ 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 /* Add padding so that the full bitrate is 2324*75 bytes/sec */ 00454 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate); 00455 } 00456 00457 if (s->is_vcd || s->is_mpeg2) 00458 /* every packet */ 00459 s->pack_header_freq = 1; 00460 else 00461 /* every 2 seconds */ 00462 s->pack_header_freq = 2 * bitrate / s->packet_size / 8; 00463 00464 /* the above seems to make pack_header_freq zero sometimes */ 00465 if (s->pack_header_freq == 0) 00466 s->pack_header_freq = 1; 00467 00468 if (s->is_mpeg2) 00469 /* every 200 packets. Need to look at the spec. */ 00470 s->system_header_freq = s->pack_header_freq * 40; 00471 else if (s->is_vcd) 00472 /* the standard mandates that there are only two system headers 00473 in the whole file: one in the first packet of each stream. 00474 (see standard p. IV-7 and IV-8) */ 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 /* return the number of padding bytes that should be inserted into 00505 the multiplexed stream.*/ 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); //FIXME this is wrong 00516 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written); 00517 00518 if (pad_bytes<0) 00519 /* might happen if we have already padded to a later timestamp. This 00520 can occur if another stream has already advanced further.*/ 00521 pad_bytes=0; 00522 } 00523 00524 return pad_bytes; 00525 } 00526 00527 00528 #if 0 /* unused, remove? */ 00529 /* return the exact available payload size for the next packet for 00530 stream 'stream_index'. 'pts' and 'dts' are only used to know if 00531 timestamps are needed in the packet header. */ 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 /* pack header size */ 00544 if (s->is_mpeg2) 00545 buf_index += 14; 00546 else 00547 buf_index += 12; 00548 00549 if (s->is_vcd) { 00550 /* there is exactly one system header for each stream in a VCD MPEG, 00551 One in the very first video packet and one in the very first 00552 audio packet (see VCD standard p. IV-7 and IV-8).*/ 00553 00554 if (stream->packet_number==0) 00555 /* The system headers refer only to the stream they occur in, 00556 so they have a constant size.*/ 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 /* the first pack of each stream contains only the pack header, 00568 the system header and some padding (see VCD standard p. IV-6) 00569 Add the padding size, so that the actual payload becomes 0.*/ 00570 buf_index += s->packet_size - buf_index; 00571 else { 00572 /* packet header size */ 00573 buf_index += 6; 00574 if (s->is_mpeg2) { 00575 buf_index += 3; 00576 if (stream->packet_number==0) 00577 buf_index += 3; /* PES extension */ 00578 buf_index += 1; /* obligatory stuffing byte */ 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 /* AC-3/LPCM private data header */ 00593 buf_index += 4; 00594 if (stream->id >= 0xa0) { 00595 int n; 00596 buf_index += 3; 00597 /* NOTE: we round the payload size to an integer number of 00598 LPCM samples */ 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 /* The VCD standard demands that 20 zero bytes follow 00607 each audio packet (see standard p. IV-8).*/ 00608 buf_index+=20; 00609 } 00610 return s->packet_size - buf_index; 00611 } 00612 #endif 00613 00614 /* Write an MPEG padding packet header. */ 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 /* flush the packet on stream stream_index */ 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; /*"general" pack without data specific to one stream?*/ 00660 int nb_frames; 00661 00662 id = stream->id; 00663 00664 av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0); 00665 00666 buf_ptr = buffer; 00667 00668 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) { 00669 /* output pack and systems header if needed */ 00670 size = put_pack_header(ctx, buf_ptr, scr); 00671 buf_ptr += size; 00672 s->last_scr= scr; 00673 00674 if (s->is_vcd) { 00675 /* there is exactly one system header for each stream in a VCD MPEG, 00676 One in the very first video packet and one in the very first 00677 audio packet (see VCD standard p. IV-7 and IV-8).*/ 00678 00679 if (stream->packet_number==0) { 00680 size = put_system_header(ctx, buf_ptr, id); 00681 buf_ptr += size; 00682 } 00683 } else if (s->is_dvd) { 00684 if (stream->align_iframe || s->packet_number == 0){ 00685 int PES_bytes_to_fill = s->packet_size - size - 10; 00686 00687 if (pts != AV_NOPTS_VALUE) { 00688 if (dts != pts) 00689 PES_bytes_to_fill -= 5 + 5; 00690 else 00691 PES_bytes_to_fill -= 5; 00692 } 00693 00694 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) { 00695 size = put_system_header(ctx, buf_ptr, 0); 00696 buf_ptr += size; 00697 size = buf_ptr - buffer; 00698 avio_write(ctx->pb, buffer, size); 00699 00700 avio_wb32(ctx->pb, PRIVATE_STREAM_2); 00701 avio_wb16(ctx->pb, 0x03d4); // length 00702 avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI 00703 for (i = 0; i < 979; i++) 00704 avio_w8(ctx->pb, 0x00); 00705 00706 avio_wb32(ctx->pb, PRIVATE_STREAM_2); 00707 avio_wb16(ctx->pb, 0x03fa); // length 00708 avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI 00709 for (i = 0; i < 1017; i++) 00710 avio_w8(ctx->pb, 0x00); 00711 00712 memset(buffer, 0, 128); 00713 buf_ptr = buffer; 00714 s->packet_number++; 00715 stream->align_iframe = 0; 00716 scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet 00717 size = put_pack_header(ctx, buf_ptr, scr); 00718 s->last_scr= scr; 00719 buf_ptr += size; 00720 /* GOP Start */ 00721 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) { 00722 pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe; 00723 } 00724 } 00725 } else { 00726 if ((s->packet_number % s->system_header_freq) == 0) { 00727 size = put_system_header(ctx, buf_ptr, 0); 00728 buf_ptr += size; 00729 } 00730 } 00731 } 00732 size = buf_ptr - buffer; 00733 avio_write(ctx->pb, buffer, size); 00734 00735 packet_size = s->packet_size - size; 00736 00737 if (s->is_vcd && (id & 0xe0) == AUDIO_ID) 00738 /* The VCD standard demands that 20 zero bytes follow 00739 each audio pack (see standard p. IV-8).*/ 00740 zero_trail_bytes += 20; 00741 00742 if ((s->is_vcd && stream->packet_number==0) 00743 || (s->is_svcd && s->packet_number==0)) { 00744 /* for VCD the first pack of each stream contains only the pack header, 00745 the system header and lots of padding (see VCD standard p. IV-6). 00746 In the case of an audio pack, 20 zero bytes are also added at 00747 the end.*/ 00748 /* For SVCD we fill the very first pack to increase compatibility with 00749 some DVD players. Not mandated by the standard.*/ 00750 if (s->is_svcd) 00751 general_pack = 1; /* the system header refers to both streams and no stream data*/ 00752 pad_packet_bytes = packet_size - zero_trail_bytes; 00753 } 00754 00755 packet_size -= pad_packet_bytes + zero_trail_bytes; 00756 00757 if (packet_size > 0) { 00758 00759 /* packet header size */ 00760 packet_size -= 6; 00761 00762 /* packet header */ 00763 if (s->is_mpeg2) { 00764 header_len = 3; 00765 if (stream->packet_number==0) 00766 header_len += 3; /* PES extension */ 00767 header_len += 1; /* obligatory stuffing byte */ 00768 } else { 00769 header_len = 0; 00770 } 00771 if (pts != AV_NOPTS_VALUE) { 00772 if (dts != pts) 00773 header_len += 5 + 5; 00774 else 00775 header_len += 5; 00776 } else { 00777 if (!s->is_mpeg2) 00778 header_len++; 00779 } 00780 00781 payload_size = packet_size - header_len; 00782 if (id < 0xc0) { 00783 startcode = PRIVATE_STREAM_1; 00784 payload_size -= 1; 00785 if (id >= 0x40) { 00786 payload_size -= 3; 00787 if (id >= 0xa0) 00788 payload_size -= 3; 00789 } 00790 } else { 00791 startcode = 0x100 + id; 00792 } 00793 00794 stuffing_size = payload_size - av_fifo_size(stream->fifo); 00795 00796 // first byte does not fit -> reset pts/dts + stuffing 00797 if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){ 00798 int timestamp_len=0; 00799 if(dts != pts) 00800 timestamp_len += 5; 00801 if(pts != AV_NOPTS_VALUE) 00802 timestamp_len += s->is_mpeg2 ? 5 : 4; 00803 pts=dts= AV_NOPTS_VALUE; 00804 header_len -= timestamp_len; 00805 if (s->is_dvd && stream->align_iframe) { 00806 pad_packet_bytes += timestamp_len; 00807 packet_size -= timestamp_len; 00808 } else { 00809 payload_size += timestamp_len; 00810 } 00811 stuffing_size += timestamp_len; 00812 if(payload_size > trailer_size) 00813 stuffing_size += payload_size - trailer_size; 00814 } 00815 00816 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing 00817 packet_size += pad_packet_bytes; 00818 payload_size += pad_packet_bytes; // undo the previous adjustment 00819 if (stuffing_size < 0) { 00820 stuffing_size = pad_packet_bytes; 00821 } else { 00822 stuffing_size += pad_packet_bytes; 00823 } 00824 pad_packet_bytes = 0; 00825 } 00826 00827 if (stuffing_size < 0) 00828 stuffing_size = 0; 00829 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/ 00830 pad_packet_bytes += stuffing_size; 00831 packet_size -= stuffing_size; 00832 payload_size -= stuffing_size; 00833 stuffing_size = 0; 00834 } 00835 00836 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size); 00837 00838 avio_wb32(ctx->pb, startcode); 00839 00840 avio_wb16(ctx->pb, packet_size); 00841 00842 if (!s->is_mpeg2) 00843 for(i=0;i<stuffing_size;i++) 00844 avio_w8(ctx->pb, 0xff); 00845 00846 if (s->is_mpeg2) { 00847 avio_w8(ctx->pb, 0x80); /* mpeg2 id */ 00848 00849 pes_flags=0; 00850 00851 if (pts != AV_NOPTS_VALUE) { 00852 pes_flags |= 0x80; 00853 if (dts != pts) 00854 pes_flags |= 0x40; 00855 } 00856 00857 /* Both the MPEG-2 and the SVCD standards demand that the 00858 P-STD_buffer_size field be included in the first packet of 00859 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2 00860 and MPEG-2 standard 2.7.7) */ 00861 if (stream->packet_number == 0) 00862 pes_flags |= 0x01; 00863 00864 avio_w8(ctx->pb, pes_flags); /* flags */ 00865 avio_w8(ctx->pb, header_len - 3 + stuffing_size); 00866 00867 if (pes_flags & 0x80) /*write pts*/ 00868 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts); 00869 if (pes_flags & 0x40) /*write dts*/ 00870 put_timestamp(ctx->pb, 0x01, dts); 00871 00872 if (pes_flags & 0x01) { /*write pes extension*/ 00873 avio_w8(ctx->pb, 0x10); /* flags */ 00874 00875 /* P-STD buffer info */ 00876 if ((id & 0xe0) == AUDIO_ID) 00877 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128); 00878 else 00879 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024); 00880 } 00881 00882 } else { 00883 if (pts != AV_NOPTS_VALUE) { 00884 if (dts != pts) { 00885 put_timestamp(ctx->pb, 0x03, pts); 00886 put_timestamp(ctx->pb, 0x01, dts); 00887 } else { 00888 put_timestamp(ctx->pb, 0x02, pts); 00889 } 00890 } else { 00891 avio_w8(ctx->pb, 0x0f); 00892 } 00893 } 00894 00895 if (s->is_mpeg2) { 00896 /* special stuffing byte that is always written 00897 to prevent accidental generation of start codes. */ 00898 avio_w8(ctx->pb, 0xff); 00899 00900 for(i=0;i<stuffing_size;i++) 00901 avio_w8(ctx->pb, 0xff); 00902 } 00903 00904 if (startcode == PRIVATE_STREAM_1) { 00905 avio_w8(ctx->pb, id); 00906 if (id >= 0xa0) { 00907 /* LPCM (XXX: check nb_frames) */ 00908 avio_w8(ctx->pb, 7); 00909 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */ 00910 avio_w8(ctx->pb, stream->lpcm_header[0]); 00911 avio_w8(ctx->pb, stream->lpcm_header[1]); 00912 avio_w8(ctx->pb, stream->lpcm_header[2]); 00913 } else if (id >= 0x40) { 00914 /* AC-3 */ 00915 avio_w8(ctx->pb, nb_frames); 00916 avio_wb16(ctx->pb, trailer_size+1); 00917 } 00918 } 00919 00920 /* output data */ 00921 assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo)); 00922 av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, &avio_write); 00923 stream->bytes_to_iframe -= payload_size - stuffing_size; 00924 }else{ 00925 payload_size= 00926 stuffing_size= 0; 00927 } 00928 00929 if (pad_packet_bytes > 0) 00930 put_padding_packet(ctx,ctx->pb, pad_packet_bytes); 00931 00932 for(i=0;i<zero_trail_bytes;i++) 00933 avio_w8(ctx->pb, 0x00); 00934 00935 avio_flush(ctx->pb); 00936 00937 s->packet_number++; 00938 00939 /* only increase the stream packet number if this pack actually contains 00940 something that is specific to this stream! I.e. a dedicated header 00941 or some data.*/ 00942 if (!general_pack) 00943 stream->packet_number++; 00944 00945 return payload_size - stuffing_size; 00946 } 00947 00948 static void put_vcd_padding_sector(AVFormatContext *ctx) 00949 { 00950 /* There are two ways to do this padding: writing a sector/pack 00951 of 0 values, or writing an MPEG padding pack. Both seem to 00952 work with most decoders, BUT the VCD standard only allows a 0-sector 00953 (see standard p. IV-4, IV-5). 00954 So a 0-sector it is...*/ 00955 00956 MpegMuxContext *s = ctx->priv_data; 00957 int i; 00958 00959 for(i=0;i<s->packet_size;i++) 00960 avio_w8(ctx->pb, 0); 00961 00962 s->vcd_padding_bytes_written += s->packet_size; 00963 00964 avio_flush(ctx->pb); 00965 00966 /* increasing the packet number is correct. The SCR of the following packs 00967 is calculated from the packet_number and it has to include the padding 00968 sector (it represents the sector index, not the MPEG pack index) 00969 (see VCD standard p. IV-6)*/ 00970 s->packet_number++; 00971 } 00972 00973 #if 0 /* unused, remove? */ 00974 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts) 00975 { 00976 MpegMuxContext *s = ctx->priv_data; 00977 int64_t scr; 00978 00979 /* Since the data delivery rate is constant, SCR is computed 00980 using the formula C + i * 1200 where C is the start constant 00981 and i is the pack index. 00982 It is recommended that SCR 0 is at the beginning of the VCD front 00983 margin (a sequence of empty Form 2 sectors on the CD). 00984 It is recommended that the front margin is 30 sectors long, so 00985 we use C = 30*1200 = 36000 00986 (Note that even if the front margin is not 30 sectors the file 00987 will still be correct according to the standard. It just won't have 00988 the "recommended" value).*/ 00989 scr = 36000 + s->packet_number * 1200; 00990 00991 return scr; 00992 } 00993 #endif 00994 00995 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){ 00996 // MpegMuxContext *s = ctx->priv_data; 00997 int i; 00998 00999 for(i=0; i<ctx->nb_streams; i++){ 01000 AVStream *st = ctx->streams[i]; 01001 StreamInfo *stream = st->priv_data; 01002 PacketDesc *pkt_desc; 01003 01004 while((pkt_desc= stream->predecode_packet) 01005 && scr > pkt_desc->dts){ //FIXME > vs >= 01006 if(stream->buffer_index < pkt_desc->size || 01007 stream->predecode_packet == stream->premux_packet){ 01008 av_log(ctx, AV_LOG_ERROR, 01009 "buffer underflow i=%d bufi=%d size=%d\n", 01010 i, stream->buffer_index, pkt_desc->size); 01011 break; 01012 } 01013 stream->buffer_index -= pkt_desc->size; 01014 01015 stream->predecode_packet= pkt_desc->next; 01016 av_freep(&pkt_desc); 01017 } 01018 } 01019 01020 return 0; 01021 } 01022 01023 static int output_packet(AVFormatContext *ctx, int flush){ 01024 MpegMuxContext *s = ctx->priv_data; 01025 AVStream *st; 01026 StreamInfo *stream; 01027 int i, avail_space=0, es_size, trailer_size; 01028 int best_i= -1; 01029 int best_score= INT_MIN; 01030 int ignore_constraints=0; 01031 int64_t scr= s->last_scr; 01032 PacketDesc *timestamp_packet; 01033 const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE); 01034 01035 retry: 01036 for(i=0; i<ctx->nb_streams; i++){ 01037 AVStream *st = ctx->streams[i]; 01038 StreamInfo *stream = st->priv_data; 01039 const int avail_data= av_fifo_size(stream->fifo); 01040 const int space= stream->max_buffer_size - stream->buffer_index; 01041 int rel_space= 1024*space / stream->max_buffer_size; 01042 PacketDesc *next_pkt= stream->premux_packet; 01043 01044 /* for subtitle, a single PES packet must be generated, 01045 so we flush after every single subtitle packet */ 01046 if(s->packet_size > avail_data && !flush 01047 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) 01048 return 0; 01049 if(avail_data==0) 01050 continue; 01051 assert(avail_data>0); 01052 01053 if(space < s->packet_size && !ignore_constraints) 01054 continue; 01055 01056 if(next_pkt && next_pkt->dts - scr > max_delay) 01057 continue; 01058 01059 if(rel_space > best_score){ 01060 best_score= rel_space; 01061 best_i = i; 01062 avail_space= space; 01063 } 01064 } 01065 01066 if(best_i < 0){ 01067 int64_t best_dts= INT64_MAX; 01068 01069 for(i=0; i<ctx->nb_streams; i++){ 01070 AVStream *st = ctx->streams[i]; 01071 StreamInfo *stream = st->priv_data; 01072 PacketDesc *pkt_desc= stream->predecode_packet; 01073 if(pkt_desc && pkt_desc->dts < best_dts) 01074 best_dts= pkt_desc->dts; 01075 } 01076 01077 av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n", 01078 scr / 90000.0, best_dts / 90000.0); 01079 if(best_dts == INT64_MAX) 01080 return 0; 01081 01082 if(scr >= best_dts+1 && !ignore_constraints){ 01083 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n"); 01084 ignore_constraints= 1; 01085 } 01086 scr= FFMAX(best_dts+1, scr); 01087 if(remove_decoded_packets(ctx, scr) < 0) 01088 return -1; 01089 goto retry; 01090 } 01091 01092 assert(best_i >= 0); 01093 01094 st = ctx->streams[best_i]; 01095 stream = st->priv_data; 01096 01097 assert(av_fifo_size(stream->fifo) > 0); 01098 01099 assert(avail_space >= s->packet_size || ignore_constraints); 01100 01101 timestamp_packet= stream->premux_packet; 01102 if(timestamp_packet->unwritten_size == timestamp_packet->size){ 01103 trailer_size= 0; 01104 }else{ 01105 trailer_size= timestamp_packet->unwritten_size; 01106 timestamp_packet= timestamp_packet->next; 01107 } 01108 01109 if(timestamp_packet){ 01110 //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); 01111 es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size); 01112 }else{ 01113 assert(av_fifo_size(stream->fifo) == trailer_size); 01114 es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size); 01115 } 01116 01117 if (s->is_vcd) { 01118 /* Write one or more padding sectors, if necessary, to reach 01119 the constant overall bitrate.*/ 01120 int vcd_pad_bytes; 01121 01122 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here 01123 put_vcd_padding_sector(ctx); 01124 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet 01125 } 01126 } 01127 01128 stream->buffer_index += es_size; 01129 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet 01130 01131 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){ 01132 es_size -= stream->premux_packet->unwritten_size; 01133 stream->premux_packet= stream->premux_packet->next; 01134 } 01135 if(es_size) 01136 stream->premux_packet->unwritten_size -= es_size; 01137 01138 if(remove_decoded_packets(ctx, s->last_scr) < 0) 01139 return -1; 01140 01141 return 1; 01142 } 01143 01144 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) 01145 { 01146 MpegMuxContext *s = ctx->priv_data; 01147 int stream_index= pkt->stream_index; 01148 int size= pkt->size; 01149 uint8_t *buf= pkt->data; 01150 AVStream *st = ctx->streams[stream_index]; 01151 StreamInfo *stream = st->priv_data; 01152 int64_t pts, dts; 01153 PacketDesc *pkt_desc; 01154 const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE); 01155 const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY); 01156 01157 pts= pkt->pts; 01158 dts= pkt->dts; 01159 01160 if(pts != AV_NOPTS_VALUE) pts += 2*preload; 01161 if(dts != AV_NOPTS_VALUE){ 01162 if(!s->last_scr) 01163 s->last_scr= dts + preload; 01164 dts += 2*preload; 01165 } 01166 01167 //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); 01168 if (!stream->premux_packet) 01169 stream->next_packet = &stream->premux_packet; 01170 *stream->next_packet= 01171 pkt_desc= av_mallocz(sizeof(PacketDesc)); 01172 pkt_desc->pts= pts; 01173 pkt_desc->dts= dts; 01174 pkt_desc->unwritten_size= 01175 pkt_desc->size= size; 01176 if(!stream->predecode_packet) 01177 stream->predecode_packet= pkt_desc; 01178 stream->next_packet= &pkt_desc->next; 01179 01180 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0) 01181 return -1; 01182 01183 if (s->is_dvd){ 01184 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder) 01185 stream->bytes_to_iframe = av_fifo_size(stream->fifo); 01186 stream->align_iframe = 1; 01187 stream->vobu_start_pts = pts; 01188 } 01189 } 01190 01191 av_fifo_generic_write(stream->fifo, buf, size, NULL); 01192 01193 for(;;){ 01194 int ret= output_packet(ctx, 0); 01195 if(ret<=0) 01196 return ret; 01197 } 01198 } 01199 01200 static int mpeg_mux_end(AVFormatContext *ctx) 01201 { 01202 // MpegMuxContext *s = ctx->priv_data; 01203 StreamInfo *stream; 01204 int i; 01205 01206 for(;;){ 01207 int ret= output_packet(ctx, 1); 01208 if(ret<0) 01209 return ret; 01210 else if(ret==0) 01211 break; 01212 } 01213 01214 /* End header according to MPEG1 systems standard. We do not write 01215 it as it is usually not needed by decoders and because it 01216 complicates MPEG stream concatenation. */ 01217 //avio_wb32(ctx->pb, ISO_11172_END_CODE); 01218 //avio_flush(ctx->pb); 01219 01220 for(i=0;i<ctx->nb_streams;i++) { 01221 stream = ctx->streams[i]->priv_data; 01222 01223 assert(av_fifo_size(stream->fifo) == 0); 01224 av_fifo_free(stream->fifo); 01225 } 01226 return 0; 01227 } 01228 01229 #if CONFIG_MPEG1SYSTEM_MUXER 01230 AVOutputFormat ff_mpeg1system_muxer = { 01231 "mpeg", 01232 NULL_IF_CONFIG_SMALL("MPEG-1 System format"), 01233 "video/mpeg", 01234 "mpg,mpeg", 01235 sizeof(MpegMuxContext), 01236 CODEC_ID_MP2, 01237 CODEC_ID_MPEG1VIDEO, 01238 mpeg_mux_init, 01239 mpeg_mux_write_packet, 01240 mpeg_mux_end, 01241 }; 01242 #endif 01243 #if CONFIG_MPEG1VCD_MUXER 01244 AVOutputFormat ff_mpeg1vcd_muxer = { 01245 "vcd", 01246 NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"), 01247 "video/mpeg", 01248 NULL, 01249 sizeof(MpegMuxContext), 01250 CODEC_ID_MP2, 01251 CODEC_ID_MPEG1VIDEO, 01252 mpeg_mux_init, 01253 mpeg_mux_write_packet, 01254 mpeg_mux_end, 01255 }; 01256 #endif 01257 #if CONFIG_MPEG2VOB_MUXER 01258 AVOutputFormat ff_mpeg2vob_muxer = { 01259 "vob", 01260 NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"), 01261 "video/mpeg", 01262 "vob", 01263 sizeof(MpegMuxContext), 01264 CODEC_ID_MP2, 01265 CODEC_ID_MPEG2VIDEO, 01266 mpeg_mux_init, 01267 mpeg_mux_write_packet, 01268 mpeg_mux_end, 01269 }; 01270 #endif 01271 01272 /* Same as mpeg2vob_mux except that the pack size is 2324 */ 01273 #if CONFIG_MPEG2SVCD_MUXER 01274 AVOutputFormat ff_mpeg2svcd_muxer = { 01275 "svcd", 01276 NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"), 01277 "video/mpeg", 01278 "vob", 01279 sizeof(MpegMuxContext), 01280 CODEC_ID_MP2, 01281 CODEC_ID_MPEG2VIDEO, 01282 mpeg_mux_init, 01283 mpeg_mux_write_packet, 01284 mpeg_mux_end, 01285 }; 01286 #endif 01287 01288 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */ 01289 #if CONFIG_MPEG2DVD_MUXER 01290 AVOutputFormat ff_mpeg2dvd_muxer = { 01291 "dvd", 01292 NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"), 01293 "video/mpeg", 01294 "dvd", 01295 sizeof(MpegMuxContext), 01296 CODEC_ID_MP2, 01297 CODEC_ID_MPEG2VIDEO, 01298 mpeg_mux_init, 01299 mpeg_mux_write_packet, 01300 mpeg_mux_end, 01301 }; 01302 #endif