Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpegenc.c @ d2062ff6

History | View | Annotate | Download (41.3 KB)

1 2abe5a01 Baptiste Coudurier
/*
2
 * MPEG1/2 muxer
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include "avformat.h"
23 5d385746 Baptiste Coudurier
#include "allformats.h"
24 2abe5a01 Baptiste Coudurier
#include "bitstream.h"
25
#include "fifo.h"
26
#include "mpeg.h"
27
28
#define MAX_PAYLOAD_SIZE 4096
29
//#define DEBUG_SEEK
30
31
#undef NDEBUG
32
#include <assert.h>
33
34
typedef struct PacketDesc {
35
    int64_t pts;
36
    int64_t dts;
37
    int size;
38
    int unwritten_size;
39
    int flags;
40
    struct PacketDesc *next;
41
} PacketDesc;
42
43
typedef struct {
44
    AVFifoBuffer fifo;
45
    uint8_t id;
46
    int max_buffer_size; /* in bytes */
47
    int buffer_index;
48
    PacketDesc *predecode_packet;
49
    PacketDesc *premux_packet;
50
    PacketDesc **next_packet;
51
    int packet_number;
52
    uint8_t lpcm_header[3];
53
    int lpcm_align;
54
    int bytes_to_iframe;
55
    int align_iframe;
56
    int64_t vobu_start_pts;
57
} StreamInfo;
58
59
typedef struct {
60
    int packet_size; /* required packet size */
61
    int packet_number;
62
    int pack_header_freq;     /* frequency (in packets^-1) at which we send pack headers */
63
    int system_header_freq;
64
    int system_header_size;
65
    int mux_rate; /* bitrate in units of 50 bytes/s */
66
    /* stream info */
67
    int audio_bound;
68
    int video_bound;
69
    int is_mpeg2;
70
    int is_vcd;
71
    int is_svcd;
72
    int is_dvd;
73
    int64_t last_scr; /* current system clock */
74
75
    double vcd_padding_bitrate; //FIXME floats
76
    int64_t vcd_padding_bytes_written;
77
78
} MpegMuxContext;
79
80
static int put_pack_header(AVFormatContext *ctx,
81
                           uint8_t *buf, int64_t timestamp)
82
{
83
    MpegMuxContext *s = ctx->priv_data;
84
    PutBitContext pb;
85
86
    init_put_bits(&pb, buf, 128);
87
88
    put_bits(&pb, 32, PACK_START_CODE);
89
    if (s->is_mpeg2) {
90
        put_bits(&pb, 2, 0x1);
91
    } else {
92
        put_bits(&pb, 4, 0x2);
93
    }
94
    put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
95
    put_bits(&pb, 1, 1);
96
    put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
97
    put_bits(&pb, 1, 1);
98
    put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
99
    put_bits(&pb, 1, 1);
100
    if (s->is_mpeg2) {
101
        /* clock extension */
102
        put_bits(&pb, 9, 0);
103
    }
104
    put_bits(&pb, 1, 1);
105
    put_bits(&pb, 22, s->mux_rate);
106
    put_bits(&pb, 1, 1);
107
    if (s->is_mpeg2) {
108
        put_bits(&pb, 1, 1);
109
        put_bits(&pb, 5, 0x1f); /* reserved */
110
        put_bits(&pb, 3, 0); /* stuffing length */
111
    }
112
    flush_put_bits(&pb);
113
    return pbBufPtr(&pb) - pb.buf;
114
}
115
116
static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
117
{
118
    MpegMuxContext *s = ctx->priv_data;
119
    int size, i, private_stream_coded, id;
120
    PutBitContext pb;
121
122
    init_put_bits(&pb, buf, 128);
123
124
    put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
125
    put_bits(&pb, 16, 0);
126
    put_bits(&pb, 1, 1);
127
128
    put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
129
    put_bits(&pb, 1, 1); /* marker */
130
    if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
131
        /* This header applies only to the video stream (see VCD standard p. IV-7)*/
132
        put_bits(&pb, 6, 0);
133
    } else
134
        put_bits(&pb, 6, s->audio_bound);
135
136
    if (s->is_vcd) {
137
        /* see VCD standard, p. IV-7*/
138
        put_bits(&pb, 1, 0);
139
        put_bits(&pb, 1, 1);
140
    } else {
141
        put_bits(&pb, 1, 0); /* variable bitrate*/
142
        put_bits(&pb, 1, 0); /* non constrainted bit stream */
143
    }
144
145
    if (s->is_vcd || s->is_dvd) {
146
        /* see VCD standard p IV-7 */
147
        put_bits(&pb, 1, 1); /* audio locked */
148
        put_bits(&pb, 1, 1); /* video locked */
149
    } else {
150
        put_bits(&pb, 1, 0); /* audio locked */
151
        put_bits(&pb, 1, 0); /* video locked */
152
    }
153
154
    put_bits(&pb, 1, 1); /* marker */
155
156
    if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
157
        /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
158
        put_bits(&pb, 5, 0);
159
    } else
160
        put_bits(&pb, 5, s->video_bound);
161
162
    if (s->is_dvd) {
163
        put_bits(&pb, 1, 0);    /* packet_rate_restriction_flag */
164
        put_bits(&pb, 7, 0x7f); /* reserved byte */
165
    } else
166
        put_bits(&pb, 8, 0xff); /* reserved byte */
167
168
    /* DVD-Video Stream_bound entries
169
    id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
170
    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)
171
    id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
172
    id (0xBF) private stream 2, NAV packs, set to 2x1024. */
173
    if (s->is_dvd) {
174
175
        int P_STD_max_video = 0;
176
        int P_STD_max_mpeg_audio = 0;
177
        int P_STD_max_mpeg_PS1 = 0;
178
179
        for(i=0;i<ctx->nb_streams;i++) {
180
            StreamInfo *stream = ctx->streams[i]->priv_data;
181
182
            id = stream->id;
183
            if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
184
                P_STD_max_mpeg_PS1 = stream->max_buffer_size;
185
            } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
186
                P_STD_max_mpeg_audio = stream->max_buffer_size;
187
            } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
188
                P_STD_max_video = stream->max_buffer_size;
189
            }
190
        }
191
192
        /* video */
193
        put_bits(&pb, 8, 0xb9); /* stream ID */
194
        put_bits(&pb, 2, 3);
195
        put_bits(&pb, 1, 1);
196
        put_bits(&pb, 13, P_STD_max_video / 1024);
197
198
        /* audio */
199
        if (P_STD_max_mpeg_audio == 0)
200
            P_STD_max_mpeg_audio = 4096;
201
        put_bits(&pb, 8, 0xb8); /* stream ID */
202
        put_bits(&pb, 2, 3);
203
        put_bits(&pb, 1, 0);
204
        put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
205
206
        /* private stream 1 */
207
        put_bits(&pb, 8, 0xbd); /* stream ID */
208
        put_bits(&pb, 2, 3);
209
        put_bits(&pb, 1, 0);
210
        put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
211
212
        /* private stream 2 */
213
        put_bits(&pb, 8, 0xbf); /* stream ID */
214
        put_bits(&pb, 2, 3);
215
        put_bits(&pb, 1, 1);
216
        put_bits(&pb, 13, 2);
217
    }
218
    else {
219
        /* audio stream info */
220
        private_stream_coded = 0;
221
        for(i=0;i<ctx->nb_streams;i++) {
222
            StreamInfo *stream = ctx->streams[i]->priv_data;
223
224
225
            /* For VCDs, only include the stream info for the stream
226
            that the pack which contains this system belongs to.
227
            (see VCD standard p. IV-7) */
228
            if ( !s->is_vcd || stream->id==only_for_stream_id
229
                || only_for_stream_id==0) {
230
231
                id = stream->id;
232
                if (id < 0xc0) {
233
                    /* special case for private streams (AC3 use that) */
234
                    if (private_stream_coded)
235
                        continue;
236
                    private_stream_coded = 1;
237
                    id = 0xbd;
238
                }
239
                put_bits(&pb, 8, id); /* stream ID */
240
                put_bits(&pb, 2, 3);
241
                if (id < 0xe0) {
242
                    /* audio */
243
                    put_bits(&pb, 1, 0);
244
                    put_bits(&pb, 13, stream->max_buffer_size / 128);
245
                } else {
246
                    /* video */
247
                    put_bits(&pb, 1, 1);
248
                    put_bits(&pb, 13, stream->max_buffer_size / 1024);
249
                }
250
            }
251
        }
252
    }
253
254
    flush_put_bits(&pb);
255
    size = pbBufPtr(&pb) - pb.buf;
256
    /* patch packet size */
257
    buf[4] = (size - 6) >> 8;
258
    buf[5] = (size - 6) & 0xff;
259
260
    return size;
261
}
262
263
static int get_system_header_size(AVFormatContext *ctx)
264
{
265
    int buf_index, i, private_stream_coded;
266
    StreamInfo *stream;
267
    MpegMuxContext *s = ctx->priv_data;
268
269
    if (s->is_dvd)
270
       return 18; // DVD-Video system headers are 18 bytes fixed length.
271
272
    buf_index = 12;
273
    private_stream_coded = 0;
274
    for(i=0;i<ctx->nb_streams;i++) {
275
        stream = ctx->streams[i]->priv_data;
276
        if (stream->id < 0xc0) {
277
            if (private_stream_coded)
278
                continue;
279
            private_stream_coded = 1;
280
        }
281
        buf_index += 3;
282
    }
283
    return buf_index;
284
}
285
286
static int mpeg_mux_init(AVFormatContext *ctx)
287
{
288
    MpegMuxContext *s = ctx->priv_data;
289
    int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
290
    AVStream *st;
291
    StreamInfo *stream;
292
    int audio_bitrate;
293
    int video_bitrate;
294
295
    s->packet_number = 0;
296 372a80d1 Aurelien Jacobs
    s->is_vcd =   (ENABLE_MPEG1VCD_MUXER  && ctx->oformat == &mpeg1vcd_muxer);
297
    s->is_svcd =  (ENABLE_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer);
298
    s->is_mpeg2 = (ENABLE_MPEG2VOB_MUXER  && ctx->oformat == &mpeg2vob_muxer ||
299
                   ENABLE_MPEG2DVD_MUXER  && ctx->oformat == &mpeg2dvd_muxer ||
300
                   ENABLE_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer);
301
    s->is_dvd =   (ENABLE_MPEG2DVD_MUXER  && ctx->oformat == &mpeg2dvd_muxer);
302 2abe5a01 Baptiste Coudurier
303
    if(ctx->packet_size)
304
        s->packet_size = ctx->packet_size;
305
    else
306
        s->packet_size = 2048;
307
308
    s->vcd_padding_bytes_written = 0;
309
    s->vcd_padding_bitrate=0;
310
311
    s->audio_bound = 0;
312
    s->video_bound = 0;
313
    mpa_id = AUDIO_ID;
314
    ac3_id = AC3_ID;
315
    dts_id = DTS_ID;
316
    mpv_id = VIDEO_ID;
317
    mps_id = SUB_ID;
318
    lpcm_id = LPCM_ID;
319
    for(i=0;i<ctx->nb_streams;i++) {
320
        st = ctx->streams[i];
321
        stream = av_mallocz(sizeof(StreamInfo));
322
        if (!stream)
323
            goto fail;
324
        st->priv_data = stream;
325
326
        av_set_pts_info(st, 64, 1, 90000);
327
328
        switch(st->codec->codec_type) {
329
        case CODEC_TYPE_AUDIO:
330
            if (st->codec->codec_id == CODEC_ID_AC3) {
331
                stream->id = ac3_id++;
332
            } else if (st->codec->codec_id == CODEC_ID_DTS) {
333
                stream->id = dts_id++;
334
            } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
335
                stream->id = lpcm_id++;
336
                for(j = 0; j < 4; j++) {
337
                    if (lpcm_freq_tab[j] == st->codec->sample_rate)
338
                        break;
339
                }
340
                if (j == 4)
341
                    goto fail;
342
                if (st->codec->channels > 8)
343
                    return -1;
344
                stream->lpcm_header[0] = 0x0c;
345
                stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
346
                stream->lpcm_header[2] = 0x80;
347
                stream->lpcm_align = st->codec->channels * 2;
348
            } else {
349
                stream->id = mpa_id++;
350
            }
351
352
            /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
353
               Right now it is also used for everything else.*/
354
            stream->max_buffer_size = 4 * 1024;
355
            s->audio_bound++;
356
            break;
357
        case CODEC_TYPE_VIDEO:
358
            stream->id = mpv_id++;
359
            if (st->codec->rc_buffer_size)
360
                stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
361
            else
362
                stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
363
#if 0
364
                /* see VCD standard, p. IV-7*/
365
                stream->max_buffer_size = 46 * 1024;
366
            else
367
                /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
368
                   Right now it is also used for everything else.*/
369
                stream->max_buffer_size = 230 * 1024;
370
#endif
371
            s->video_bound++;
372
            break;
373
        case CODEC_TYPE_SUBTITLE:
374
            stream->id = mps_id++;
375
            stream->max_buffer_size = 16 * 1024;
376
            break;
377
        default:
378
            return -1;
379
        }
380
        av_fifo_init(&stream->fifo, 16);
381
    }
382
    bitrate = 0;
383
    audio_bitrate = 0;
384
    video_bitrate = 0;
385
    for(i=0;i<ctx->nb_streams;i++) {
386
        int codec_rate;
387
        st = ctx->streams[i];
388
        stream = (StreamInfo*) st->priv_data;
389
390
        if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
391
            codec_rate= st->codec->rc_max_rate;
392
        else
393
            codec_rate= st->codec->bit_rate;
394
395
        if(!codec_rate)
396
            codec_rate= (1<<21)*8*50/ctx->nb_streams;
397
398
        bitrate += codec_rate;
399
400
        if (stream->id==AUDIO_ID)
401
            audio_bitrate += codec_rate;
402
        else if (stream->id==VIDEO_ID)
403
            video_bitrate += codec_rate;
404
    }
405
406
    if(ctx->mux_rate){
407
        s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
408
    } else {
409
        /* we increase slightly the bitrate to take into account the
410
           headers. XXX: compute it exactly */
411
        bitrate += bitrate*5/100;
412
        bitrate += 10000;
413
        s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
414
    }
415
416
    if (s->is_vcd) {
417
        double overhead_rate;
418
419
        /* The VCD standard mandates that the mux_rate field is 3528
420
           (see standard p. IV-6).
421
           The value is actually "wrong", i.e. if you calculate
422
           it using the normal formula and the 75 sectors per second transfer
423
           rate you get a different value because the real pack size is 2324,
424
           not 2352. But the standard explicitly specifies that the mux_rate
425
           field in the header must have this value.*/
426
//        s->mux_rate=2352 * 75 / 50;    /* = 3528*/
427
428
        /* The VCD standard states that the muxed stream must be
429
           exactly 75 packs / second (the data rate of a single speed cdrom).
430
           Since the video bitrate (probably 1150000 bits/sec) will be below
431
           the theoretical maximum we have to add some padding packets
432
           to make up for the lower data rate.
433
           (cf. VCD standard p. IV-6 )*/
434
435
        /* Add the header overhead to the data rate.
436
           2279 data bytes per audio pack, 2294 data bytes per video pack*/
437
        overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
438
        overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
439
        overhead_rate *= 8;
440
441
        /* Add padding so that the full bitrate is 2324*75 bytes/sec */
442
        s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
443
    }
444
445
    if (s->is_vcd || s->is_mpeg2)
446
        /* every packet */
447
        s->pack_header_freq = 1;
448
    else
449
        /* every 2 seconds */
450
        s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
451
452
    /* the above seems to make pack_header_freq zero sometimes */
453
    if (s->pack_header_freq == 0)
454
       s->pack_header_freq = 1;
455
456
    if (s->is_mpeg2)
457
        /* every 200 packets. Need to look at the spec.  */
458
        s->system_header_freq = s->pack_header_freq * 40;
459
    else if (s->is_vcd)
460
        /* the standard mandates that there are only two system headers
461
           in the whole file: one in the first packet of each stream.
462
           (see standard p. IV-7 and IV-8) */
463
        s->system_header_freq = 0x7fffffff;
464
    else
465
        s->system_header_freq = s->pack_header_freq * 5;
466
467
    for(i=0;i<ctx->nb_streams;i++) {
468
        stream = ctx->streams[i]->priv_data;
469
        stream->packet_number = 0;
470
    }
471
    s->system_header_size = get_system_header_size(ctx);
472
    s->last_scr = 0;
473
    return 0;
474
 fail:
475
    for(i=0;i<ctx->nb_streams;i++) {
476
        av_free(ctx->streams[i]->priv_data);
477
    }
478
    return AVERROR(ENOMEM);
479
}
480
481
static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
482
{
483
    put_byte(pb,
484
             (id << 4) |
485
             (((timestamp >> 30) & 0x07) << 1) |
486
             1);
487
    put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
488
    put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
489
}
490
491
492
/* return the number of padding bytes that should be inserted into
493
   the multiplexed stream.*/
494
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
495
{
496
    MpegMuxContext *s = ctx->priv_data;
497
    int pad_bytes = 0;
498
499
    if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
500
    {
501
        int64_t full_pad_bytes;
502
503
        full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
504
        pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
505
506
        if (pad_bytes<0)
507
            /* might happen if we have already padded to a later timestamp. This
508
               can occur if another stream has already advanced further.*/
509
            pad_bytes=0;
510
    }
511
512
    return pad_bytes;
513
}
514
515
516
#if 0 /* unused, remove? */
517
/* return the exact available payload size for the next packet for
518
   stream 'stream_index'. 'pts' and 'dts' are only used to know if
519
   timestamps are needed in the packet header. */
520
static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
521
                                   int64_t pts, int64_t dts)
522
{
523
    MpegMuxContext *s = ctx->priv_data;
524
    int buf_index;
525
    StreamInfo *stream;
526

527
    stream = ctx->streams[stream_index]->priv_data;
528

529
    buf_index = 0;
530
    if (((s->packet_number % s->pack_header_freq) == 0)) {
531
        /* pack header size */
532
        if (s->is_mpeg2)
533
            buf_index += 14;
534
        else
535
            buf_index += 12;
536

537
        if (s->is_vcd) {
538
            /* there is exactly one system header for each stream in a VCD MPEG,
539
               One in the very first video packet and one in the very first
540
               audio packet (see VCD standard p. IV-7 and IV-8).*/
541

542
            if (stream->packet_number==0)
543
                /* The system headers refer only to the stream they occur in,
544
                   so they have a constant size.*/
545
                buf_index += 15;
546

547
        } else {
548
            if ((s->packet_number % s->system_header_freq) == 0)
549
                buf_index += s->system_header_size;
550
        }
551
    }
552

553
    if ((s->is_vcd && stream->packet_number==0)
554
        || (s->is_svcd && s->packet_number==0))
555
        /* the first pack of each stream contains only the pack header,
556
           the system header and some padding (see VCD standard p. IV-6)
557
           Add the padding size, so that the actual payload becomes 0.*/
558
        buf_index += s->packet_size - buf_index;
559
    else {
560
        /* packet header size */
561
        buf_index += 6;
562
        if (s->is_mpeg2) {
563
            buf_index += 3;
564
            if (stream->packet_number==0)
565
                buf_index += 3; /* PES extension */
566
            buf_index += 1;    /* obligatory stuffing byte */
567
        }
568
        if (pts != AV_NOPTS_VALUE) {
569
            if (dts != pts)
570
                buf_index += 5 + 5;
571
            else
572
                buf_index += 5;
573

574
        } else {
575
            if (!s->is_mpeg2)
576
                buf_index++;
577
        }
578

579
        if (stream->id < 0xc0) {
580
            /* AC3/LPCM private data header */
581
            buf_index += 4;
582
            if (stream->id >= 0xa0) {
583
                int n;
584
                buf_index += 3;
585
                /* NOTE: we round the payload size to an integer number of
586
                   LPCM samples */
587
                n = (s->packet_size - buf_index) % stream->lpcm_align;
588
                if (n)
589
                    buf_index += (stream->lpcm_align - n);
590
            }
591
        }
592

593
        if (s->is_vcd && stream->id == AUDIO_ID)
594
            /* The VCD standard demands that 20 zero bytes follow
595
               each audio packet (see standard p. IV-8).*/
596
            buf_index+=20;
597
    }
598
    return s->packet_size - buf_index;
599
}
600
#endif
601
602
/* Write an MPEG padding packet header. */
603
static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
604
{
605
    MpegMuxContext *s = ctx->priv_data;
606
    int i;
607
608
    put_be32(pb, PADDING_STREAM);
609
    put_be16(pb, packet_bytes - 6);
610
    if (!s->is_mpeg2) {
611
        put_byte(pb, 0x0f);
612
        packet_bytes -= 7;
613
    } else
614
        packet_bytes -= 6;
615
616
    for(i=0;i<packet_bytes;i++)
617
        put_byte(pb, 0xff);
618
}
619
620
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
621
    int nb_frames=0;
622
    PacketDesc *pkt_desc= stream->premux_packet;
623
624
    while(len>0){
625
        if(pkt_desc->size == pkt_desc->unwritten_size)
626
            nb_frames++;
627
        len -= pkt_desc->unwritten_size;
628
        pkt_desc= pkt_desc->next;
629
    }
630
631
    return nb_frames;
632
}
633
634
/* flush the packet on stream stream_index */
635
static int flush_packet(AVFormatContext *ctx, int stream_index,
636
                         int64_t pts, int64_t dts, int64_t scr, int trailer_size)
637
{
638
    MpegMuxContext *s = ctx->priv_data;
639
    StreamInfo *stream = ctx->streams[stream_index]->priv_data;
640
    uint8_t *buf_ptr;
641
    int size, payload_size, startcode, id, stuffing_size, i, header_len;
642
    int packet_size;
643
    uint8_t buffer[128];
644
    int zero_trail_bytes = 0;
645
    int pad_packet_bytes = 0;
646
    int pes_flags;
647
    int general_pack = 0;  /*"general" pack without data specific to one stream?*/
648
    int nb_frames;
649
650
    id = stream->id;
651
652
#if 0
653
    printf("packet ID=%2x PTS=%0.3f\n",
654
           id, pts / 90000.0);
655
#endif
656
657
    buf_ptr = buffer;
658
659
    if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
660
        /* output pack and systems header if needed */
661
        size = put_pack_header(ctx, buf_ptr, scr);
662
        buf_ptr += size;
663
        s->last_scr= scr;
664
665
        if (s->is_vcd) {
666
            /* there is exactly one system header for each stream in a VCD MPEG,
667
               One in the very first video packet and one in the very first
668
               audio packet (see VCD standard p. IV-7 and IV-8).*/
669
670
            if (stream->packet_number==0) {
671
                size = put_system_header(ctx, buf_ptr, id);
672
                buf_ptr += size;
673
            }
674
        } else if (s->is_dvd) {
675
            if (stream->align_iframe || s->packet_number == 0){
676
                int PES_bytes_to_fill = s->packet_size - size - 10;
677
678
                if (pts != AV_NOPTS_VALUE) {
679
                    if (dts != pts)
680
                        PES_bytes_to_fill -= 5 + 5;
681
                    else
682
                        PES_bytes_to_fill -= 5;
683
                }
684
685
                if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
686
                    size = put_system_header(ctx, buf_ptr, 0);
687
                    buf_ptr += size;
688
                    size = buf_ptr - buffer;
689
                    put_buffer(&ctx->pb, buffer, size);
690
691
                    put_be32(&ctx->pb, PRIVATE_STREAM_2);
692
                    put_be16(&ctx->pb, 0x03d4);         // length
693
                    put_byte(&ctx->pb, 0x00);           // substream ID, 00=PCI
694
                    for (i = 0; i < 979; i++)
695
                        put_byte(&ctx->pb, 0x00);
696
697
                    put_be32(&ctx->pb, PRIVATE_STREAM_2);
698
                    put_be16(&ctx->pb, 0x03fa);         // length
699
                    put_byte(&ctx->pb, 0x01);           // substream ID, 01=DSI
700
                    for (i = 0; i < 1017; i++)
701
                        put_byte(&ctx->pb, 0x00);
702
703
                    memset(buffer, 0, 128);
704
                    buf_ptr = buffer;
705
                    s->packet_number++;
706
                    stream->align_iframe = 0;
707
                    scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
708
                    size = put_pack_header(ctx, buf_ptr, scr);
709
                    s->last_scr= scr;
710
                    buf_ptr += size;
711
                    /* GOP Start */
712
                } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
713
                    pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
714
                }
715
            }
716
        } else {
717
            if ((s->packet_number % s->system_header_freq) == 0) {
718
                size = put_system_header(ctx, buf_ptr, 0);
719
                buf_ptr += size;
720
            }
721
        }
722
    }
723
    size = buf_ptr - buffer;
724
    put_buffer(&ctx->pb, buffer, size);
725
726
    packet_size = s->packet_size - size;
727
728
    if (s->is_vcd && id == AUDIO_ID)
729
        /* The VCD standard demands that 20 zero bytes follow
730
           each audio pack (see standard p. IV-8).*/
731
        zero_trail_bytes += 20;
732
733
    if ((s->is_vcd && stream->packet_number==0)
734
        || (s->is_svcd && s->packet_number==0)) {
735
        /* for VCD the first pack of each stream contains only the pack header,
736
           the system header and lots of padding (see VCD standard p. IV-6).
737
           In the case of an audio pack, 20 zero bytes are also added at
738
           the end.*/
739
        /* For SVCD we fill the very first pack to increase compatibility with
740
           some DVD players. Not mandated by the standard.*/
741
        if (s->is_svcd)
742
            general_pack = 1;    /* the system header refers to both streams and no stream data*/
743
        pad_packet_bytes = packet_size - zero_trail_bytes;
744
    }
745
746
    packet_size -= pad_packet_bytes + zero_trail_bytes;
747
748
    if (packet_size > 0) {
749
750
        /* packet header size */
751
        packet_size -= 6;
752
753
        /* packet header */
754
        if (s->is_mpeg2) {
755
            header_len = 3;
756
            if (stream->packet_number==0)
757
                header_len += 3; /* PES extension */
758
            header_len += 1; /* obligatory stuffing byte */
759
        } else {
760
            header_len = 0;
761
        }
762
        if (pts != AV_NOPTS_VALUE) {
763
            if (dts != pts)
764
                header_len += 5 + 5;
765
            else
766
                header_len += 5;
767
        } else {
768
            if (!s->is_mpeg2)
769
                header_len++;
770
        }
771
772
        payload_size = packet_size - header_len;
773
        if (id < 0xc0) {
774
            startcode = PRIVATE_STREAM_1;
775
            payload_size -= 1;
776
            if (id >= 0x40) {
777
                payload_size -= 3;
778
                if (id >= 0xa0)
779
                    payload_size -= 3;
780
            }
781
        } else {
782
            startcode = 0x100 + id;
783
        }
784
785
        stuffing_size = payload_size - av_fifo_size(&stream->fifo);
786
787
        // first byte does not fit -> reset pts/dts + stuffing
788
        if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
789
            int timestamp_len=0;
790
            if(dts != pts)
791
                timestamp_len += 5;
792
            if(pts != AV_NOPTS_VALUE)
793
                timestamp_len += s->is_mpeg2 ? 5 : 4;
794
            pts=dts= AV_NOPTS_VALUE;
795
            header_len -= timestamp_len;
796
            if (s->is_dvd && stream->align_iframe) {
797
                pad_packet_bytes += timestamp_len;
798
                packet_size -= timestamp_len;
799
            } else {
800
                payload_size += timestamp_len;
801
            }
802
            stuffing_size += timestamp_len;
803
            if(payload_size > trailer_size)
804
                stuffing_size += payload_size - trailer_size;
805
        }
806
807
        if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
808
            packet_size += pad_packet_bytes;
809
            payload_size += pad_packet_bytes; // undo the previous adjustment
810
            if (stuffing_size < 0) {
811
                stuffing_size = pad_packet_bytes;
812
            } else {
813
                stuffing_size += pad_packet_bytes;
814
            }
815
            pad_packet_bytes = 0;
816
        }
817
818
        if (stuffing_size < 0)
819
            stuffing_size = 0;
820
        if (stuffing_size > 16) {    /*<=16 for MPEG-1, <=32 for MPEG-2*/
821
            pad_packet_bytes += stuffing_size;
822
            packet_size -= stuffing_size;
823
            payload_size -= stuffing_size;
824
            stuffing_size = 0;
825
        }
826
827
        nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
828
829
        put_be32(&ctx->pb, startcode);
830
831
        put_be16(&ctx->pb, packet_size);
832
833
        if (!s->is_mpeg2)
834
            for(i=0;i<stuffing_size;i++)
835
                put_byte(&ctx->pb, 0xff);
836
837
        if (s->is_mpeg2) {
838
            put_byte(&ctx->pb, 0x80); /* mpeg2 id */
839
840
            pes_flags=0;
841
842
            if (pts != AV_NOPTS_VALUE) {
843
                pes_flags |= 0x80;
844
                if (dts != pts)
845
                    pes_flags |= 0x40;
846
            }
847
848
            /* Both the MPEG-2 and the SVCD standards demand that the
849
               P-STD_buffer_size field be included in the first packet of
850
               every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
851
               and MPEG-2 standard 2.7.7) */
852
            if (stream->packet_number == 0)
853
                pes_flags |= 0x01;
854
855
            put_byte(&ctx->pb, pes_flags); /* flags */
856
            put_byte(&ctx->pb, header_len - 3 + stuffing_size);
857
858
            if (pes_flags & 0x80)  /*write pts*/
859
                put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
860
            if (pes_flags & 0x40)  /*write dts*/
861
                put_timestamp(&ctx->pb, 0x01, dts);
862
863
            if (pes_flags & 0x01) {  /*write pes extension*/
864
                put_byte(&ctx->pb, 0x10); /* flags */
865
866
                /* P-STD buffer info */
867
                if (id == AUDIO_ID)
868
                    put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
869
                else
870
                    put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
871
            }
872
873
        } else {
874
            if (pts != AV_NOPTS_VALUE) {
875
                if (dts != pts) {
876
                    put_timestamp(&ctx->pb, 0x03, pts);
877
                    put_timestamp(&ctx->pb, 0x01, dts);
878
                } else {
879
                    put_timestamp(&ctx->pb, 0x02, pts);
880
                }
881
            } else {
882
                put_byte(&ctx->pb, 0x0f);
883
            }
884
        }
885
886
        if (s->is_mpeg2) {
887
            /* special stuffing byte that is always written
888
               to prevent accidental generation of start codes. */
889
            put_byte(&ctx->pb, 0xff);
890
891
            for(i=0;i<stuffing_size;i++)
892
                put_byte(&ctx->pb, 0xff);
893
        }
894
895
        if (startcode == PRIVATE_STREAM_1) {
896
            put_byte(&ctx->pb, id);
897
            if (id >= 0xa0) {
898
                /* LPCM (XXX: check nb_frames) */
899
                put_byte(&ctx->pb, 7);
900
                put_be16(&ctx->pb, 4); /* skip 3 header bytes */
901
                put_byte(&ctx->pb, stream->lpcm_header[0]);
902
                put_byte(&ctx->pb, stream->lpcm_header[1]);
903
                put_byte(&ctx->pb, stream->lpcm_header[2]);
904
            } else if (id >= 0x40) {
905
                /* AC3 */
906
                put_byte(&ctx->pb, nb_frames);
907
                put_be16(&ctx->pb, trailer_size+1);
908
            }
909
        }
910
911
        /* output data */
912
        if(av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0)
913
            return -1;
914
        stream->bytes_to_iframe -= payload_size - stuffing_size;
915
    }else{
916
        payload_size=
917
        stuffing_size= 0;
918
    }
919
920
    if (pad_packet_bytes > 0)
921
        put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
922
923
    for(i=0;i<zero_trail_bytes;i++)
924
        put_byte(&ctx->pb, 0x00);
925
926
    put_flush_packet(&ctx->pb);
927
928
    s->packet_number++;
929
930
    /* only increase the stream packet number if this pack actually contains
931
       something that is specific to this stream! I.e. a dedicated header
932
       or some data.*/
933
    if (!general_pack)
934
        stream->packet_number++;
935
936
    return payload_size - stuffing_size;
937
}
938
939
static void put_vcd_padding_sector(AVFormatContext *ctx)
940
{
941
    /* There are two ways to do this padding: writing a sector/pack
942
       of 0 values, or writing an MPEG padding pack. Both seem to
943
       work with most decoders, BUT the VCD standard only allows a 0-sector
944
       (see standard p. IV-4, IV-5).
945
       So a 0-sector it is...*/
946
947
    MpegMuxContext *s = ctx->priv_data;
948
    int i;
949
950
    for(i=0;i<s->packet_size;i++)
951
        put_byte(&ctx->pb, 0);
952
953
    s->vcd_padding_bytes_written += s->packet_size;
954
955
    put_flush_packet(&ctx->pb);
956
957
    /* increasing the packet number is correct. The SCR of the following packs
958
       is calculated from the packet_number and it has to include the padding
959
       sector (it represents the sector index, not the MPEG pack index)
960
       (see VCD standard p. IV-6)*/
961
    s->packet_number++;
962
}
963
964
#if 0 /* unused, remove? */
965
static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
966
{
967
    MpegMuxContext *s = ctx->priv_data;
968
    int64_t scr;
969

970
        /* Since the data delivery rate is constant, SCR is computed
971
           using the formula C + i * 1200 where C is the start constant
972
           and i is the pack index.
973
           It is recommended that SCR 0 is at the beginning of the VCD front
974
           margin (a sequence of empty Form 2 sectors on the CD).
975
           It is recommended that the front margin is 30 sectors long, so
976
           we use C = 30*1200 = 36000
977
           (Note that even if the front margin is not 30 sectors the file
978
           will still be correct according to the standard. It just won't have
979
           the "recommended" value).*/
980
        scr = 36000 + s->packet_number * 1200;
981

982
    return scr;
983
}
984
#endif
985
986
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
987
//    MpegMuxContext *s = ctx->priv_data;
988
    int i;
989
990
    for(i=0; i<ctx->nb_streams; i++){
991
        AVStream *st = ctx->streams[i];
992
        StreamInfo *stream = st->priv_data;
993
        PacketDesc *pkt_desc;
994
995
        while((pkt_desc= stream->predecode_packet)
996
              && scr > pkt_desc->dts){ //FIXME > vs >=
997
            if(stream->buffer_index < pkt_desc->size ||
998
               stream->predecode_packet == stream->premux_packet){
999
                av_log(ctx, AV_LOG_ERROR,
1000
                       "buffer underflow i=%d bufi=%d size=%d\n",
1001
                       i, stream->buffer_index, pkt_desc->size);
1002
                break;
1003
            }
1004
            stream->buffer_index -= pkt_desc->size;
1005
1006
            stream->predecode_packet= pkt_desc->next;
1007
            av_freep(&pkt_desc);
1008
        }
1009
    }
1010
1011
    return 0;
1012
}
1013
1014
static int output_packet(AVFormatContext *ctx, int flush){
1015
    MpegMuxContext *s = ctx->priv_data;
1016
    AVStream *st;
1017
    StreamInfo *stream;
1018
    int i, avail_space, es_size, trailer_size;
1019
    int best_i= -1;
1020
    int best_score= INT_MIN;
1021
    int ignore_constraints=0;
1022
    int64_t scr= s->last_scr;
1023
    PacketDesc *timestamp_packet;
1024
    const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1025
1026
retry:
1027
    for(i=0; i<ctx->nb_streams; i++){
1028
        AVStream *st = ctx->streams[i];
1029
        StreamInfo *stream = st->priv_data;
1030
        const int avail_data=  av_fifo_size(&stream->fifo);
1031
        const int space= stream->max_buffer_size - stream->buffer_index;
1032
        int rel_space= 1024*space / stream->max_buffer_size;
1033
        PacketDesc *next_pkt= stream->premux_packet;
1034
1035
        /* for subtitle, a single PES packet must be generated,
1036
           so we flush after every single subtitle packet */
1037
        if(s->packet_size > avail_data && !flush
1038
           && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
1039
            return 0;
1040
        if(avail_data==0)
1041
            continue;
1042
        assert(avail_data>0);
1043
1044
        if(space < s->packet_size && !ignore_constraints)
1045
            continue;
1046
1047
        if(next_pkt && next_pkt->dts - scr > max_delay)
1048
            continue;
1049
1050
        if(rel_space > best_score){
1051
            best_score= rel_space;
1052
            best_i = i;
1053
            avail_space= space;
1054
        }
1055
    }
1056
1057
    if(best_i < 0){
1058
        int64_t best_dts= INT64_MAX;
1059
1060
        for(i=0; i<ctx->nb_streams; i++){
1061
            AVStream *st = ctx->streams[i];
1062
            StreamInfo *stream = st->priv_data;
1063
            PacketDesc *pkt_desc= stream->predecode_packet;
1064
            if(pkt_desc && pkt_desc->dts < best_dts)
1065
                best_dts= pkt_desc->dts;
1066
        }
1067
1068
#if 0
1069
        av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
1070
               scr/90000.0, best_dts/90000.0);
1071
#endif
1072
        if(best_dts == INT64_MAX)
1073
            return 0;
1074
1075
        if(scr >= best_dts+1 && !ignore_constraints){
1076
            av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
1077
            ignore_constraints= 1;
1078
        }
1079
        scr= FFMAX(best_dts+1, scr);
1080
        if(remove_decoded_packets(ctx, scr) < 0)
1081
            return -1;
1082
        goto retry;
1083
    }
1084
1085
    assert(best_i >= 0);
1086
1087
    st = ctx->streams[best_i];
1088
    stream = st->priv_data;
1089
1090
    assert(av_fifo_size(&stream->fifo) > 0);
1091
1092
    assert(avail_space >= s->packet_size || ignore_constraints);
1093
1094
    timestamp_packet= stream->premux_packet;
1095
    if(timestamp_packet->unwritten_size == timestamp_packet->size){
1096
        trailer_size= 0;
1097
    }else{
1098
        trailer_size= timestamp_packet->unwritten_size;
1099
        timestamp_packet= timestamp_packet->next;
1100
    }
1101
1102
    if(timestamp_packet){
1103
//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);
1104
        es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
1105
    }else{
1106
        assert(av_fifo_size(&stream->fifo) == trailer_size);
1107
        es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
1108
    }
1109
1110
    if (s->is_vcd) {
1111
        /* Write one or more padding sectors, if necessary, to reach
1112
           the constant overall bitrate.*/
1113
        int vcd_pad_bytes;
1114
1115
        while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1116
            put_vcd_padding_sector(ctx);
1117
            s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1118
        }
1119
    }
1120
1121
    stream->buffer_index += es_size;
1122
    s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1123
1124
    while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
1125
        es_size -= stream->premux_packet->unwritten_size;
1126
        stream->premux_packet= stream->premux_packet->next;
1127
    }
1128
    if(es_size)
1129
        stream->premux_packet->unwritten_size -= es_size;
1130
1131
    if(remove_decoded_packets(ctx, s->last_scr) < 0)
1132
        return -1;
1133
1134
    return 1;
1135
}
1136
1137
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1138
{
1139
    MpegMuxContext *s = ctx->priv_data;
1140
    int stream_index= pkt->stream_index;
1141
    int size= pkt->size;
1142
    uint8_t *buf= pkt->data;
1143
    AVStream *st = ctx->streams[stream_index];
1144
    StreamInfo *stream = st->priv_data;
1145
    int64_t pts, dts;
1146
    PacketDesc *pkt_desc;
1147
    const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
1148
    const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
1149
1150
    pts= pkt->pts;
1151
    dts= pkt->dts;
1152
1153
    if(pts != AV_NOPTS_VALUE) pts += preload;
1154
    if(dts != AV_NOPTS_VALUE) dts += preload;
1155
1156
//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);
1157
    if (!stream->premux_packet)
1158
        stream->next_packet = &stream->premux_packet;
1159
    *stream->next_packet=
1160
    pkt_desc= av_mallocz(sizeof(PacketDesc));
1161
    pkt_desc->pts= pts;
1162
    pkt_desc->dts= dts;
1163
    pkt_desc->unwritten_size=
1164
    pkt_desc->size= size;
1165
    if(!stream->predecode_packet)
1166
        stream->predecode_packet= pkt_desc;
1167
    stream->next_packet= &pkt_desc->next;
1168
1169
    av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
1170
1171
    if (s->is_dvd){
1172
        if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
1173
            stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
1174
            stream->align_iframe = 1;
1175
            stream->vobu_start_pts = pts;
1176
        }
1177
    }
1178
1179
    av_fifo_write(&stream->fifo, buf, size);
1180
1181
    for(;;){
1182
        int ret= output_packet(ctx, 0);
1183
        if(ret<=0)
1184
            return ret;
1185
    }
1186
}
1187
1188
static int mpeg_mux_end(AVFormatContext *ctx)
1189
{
1190
//    MpegMuxContext *s = ctx->priv_data;
1191
    StreamInfo *stream;
1192
    int i;
1193
1194
    for(;;){
1195
        int ret= output_packet(ctx, 1);
1196
        if(ret<0)
1197
            return ret;
1198
        else if(ret==0)
1199
            break;
1200
    }
1201
1202
    /* End header according to MPEG1 systems standard. We do not write
1203
       it as it is usually not needed by decoders and because it
1204
       complicates MPEG stream concatenation. */
1205
    //put_be32(&ctx->pb, ISO_11172_END_CODE);
1206
    //put_flush_packet(&ctx->pb);
1207
1208
    for(i=0;i<ctx->nb_streams;i++) {
1209
        stream = ctx->streams[i]->priv_data;
1210
1211
        assert(av_fifo_size(&stream->fifo) == 0);
1212
        av_fifo_free(&stream->fifo);
1213
    }
1214
    return 0;
1215
}
1216
1217
#ifdef CONFIG_MPEG1SYSTEM_MUXER
1218
AVOutputFormat mpeg1system_muxer = {
1219
    "mpeg",
1220
    "MPEG1 System format",
1221
    "video/mpeg",
1222
    "mpg,mpeg",
1223
    sizeof(MpegMuxContext),
1224
    CODEC_ID_MP2,
1225
    CODEC_ID_MPEG1VIDEO,
1226
    mpeg_mux_init,
1227
    mpeg_mux_write_packet,
1228
    mpeg_mux_end,
1229
};
1230
#endif
1231
#ifdef CONFIG_MPEG1VCD_MUXER
1232
AVOutputFormat mpeg1vcd_muxer = {
1233
    "vcd",
1234
    "MPEG1 System format (VCD)",
1235
    "video/mpeg",
1236
    NULL,
1237
    sizeof(MpegMuxContext),
1238
    CODEC_ID_MP2,
1239
    CODEC_ID_MPEG1VIDEO,
1240
    mpeg_mux_init,
1241
    mpeg_mux_write_packet,
1242
    mpeg_mux_end,
1243
};
1244
#endif
1245
#ifdef CONFIG_MPEG2VOB_MUXER
1246
AVOutputFormat mpeg2vob_muxer = {
1247
    "vob",
1248
    "MPEG2 PS format (VOB)",
1249
    "video/mpeg",
1250
    "vob",
1251
    sizeof(MpegMuxContext),
1252
    CODEC_ID_MP2,
1253
    CODEC_ID_MPEG2VIDEO,
1254
    mpeg_mux_init,
1255
    mpeg_mux_write_packet,
1256
    mpeg_mux_end,
1257
};
1258
#endif
1259
1260
/* Same as mpeg2vob_mux except that the pack size is 2324 */
1261
#ifdef CONFIG_MPEG2SVCD_MUXER
1262
AVOutputFormat mpeg2svcd_muxer = {
1263
    "svcd",
1264
    "MPEG2 PS format (VOB)",
1265
    "video/mpeg",
1266
    "vob",
1267
    sizeof(MpegMuxContext),
1268
    CODEC_ID_MP2,
1269
    CODEC_ID_MPEG2VIDEO,
1270
    mpeg_mux_init,
1271
    mpeg_mux_write_packet,
1272
    mpeg_mux_end,
1273
};
1274
#endif
1275
1276
/*  Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1277
#ifdef CONFIG_MPEG2DVD_MUXER
1278
AVOutputFormat mpeg2dvd_muxer = {
1279
    "dvd",
1280
    "MPEG2 PS format (DVD VOB)",
1281
    "video/mpeg",
1282
    "dvd",
1283
    sizeof(MpegMuxContext),
1284
    CODEC_ID_MP2,
1285
    CODEC_ID_MPEG2VIDEO,
1286
    mpeg_mux_init,
1287
    mpeg_mux_write_packet,
1288
    mpeg_mux_end,
1289
};
1290
#endif