Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpegenc.c @ b7f2fdde

History | View | Annotate | Download (42.2 KB)

1
/*
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 "libavutil/fifo.h"
23
#include "libavcodec/put_bits.h"
24
#include "avformat.h"
25
#include "mpeg.h"
26

    
27
#define MAX_PAYLOAD_SIZE 4096
28
//#define DEBUG_SEEK
29

    
30
#undef NDEBUG
31
#include <assert.h>
32

    
33
typedef struct PacketDesc {
34
    int64_t pts;
35
    int64_t dts;
36
    int size;
37
    int unwritten_size;
38
    int flags;
39
    struct PacketDesc *next;
40
} PacketDesc;
41

    
42
typedef struct {
43
    AVFifoBuffer *fifo;
44
    uint8_t id;
45
    int max_buffer_size; /* in bytes */
46
    int buffer_index;
47
    PacketDesc *predecode_packet;
48
    PacketDesc *premux_packet;
49
    PacketDesc **next_packet;
50
    int packet_number;
51
    uint8_t lpcm_header[3];
52
    int lpcm_align;
53
    int bytes_to_iframe;
54
    int align_iframe;
55
    int64_t vobu_start_pts;
56
} StreamInfo;
57

    
58
typedef struct {
59
    int packet_size; /* required packet size */
60
    int packet_number;
61
    int pack_header_freq;     /* frequency (in packets^-1) at which we send pack headers */
62
    int system_header_freq;
63
    int system_header_size;
64
    int mux_rate; /* bitrate in units of 50 bytes/s */
65
    /* stream info */
66
    int audio_bound;
67
    int video_bound;
68
    int is_mpeg2;
69
    int is_vcd;
70
    int is_svcd;
71
    int is_dvd;
72
    int64_t last_scr; /* current system clock */
73

    
74
    double vcd_padding_bitrate; //FIXME floats
75
    int64_t vcd_padding_bytes_written;
76

    
77
} MpegMuxContext;
78

    
79
extern AVOutputFormat ff_mpeg1vcd_muxer;
80
extern AVOutputFormat ff_mpeg2dvd_muxer;
81
extern AVOutputFormat ff_mpeg2svcd_muxer;
82
extern AVOutputFormat ff_mpeg2vob_muxer;
83

    
84
static int put_pack_header(AVFormatContext *ctx,
85
                           uint8_t *buf, int64_t timestamp)
86
{
87
    MpegMuxContext *s = ctx->priv_data;
88
    PutBitContext pb;
89

    
90
    init_put_bits(&pb, buf, 128);
91

    
92
    put_bits32(&pb, PACK_START_CODE);
93
    if (s->is_mpeg2) {
94
        put_bits(&pb, 2, 0x1);
95
    } else {
96
        put_bits(&pb, 4, 0x2);
97
    }
98
    put_bits(&pb, 3,  (uint32_t)((timestamp >> 30) & 0x07));
99
    put_bits(&pb, 1, 1);
100
    put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
101
    put_bits(&pb, 1, 1);
102
    put_bits(&pb, 15, (uint32_t)((timestamp      ) & 0x7fff));
103
    put_bits(&pb, 1, 1);
104
    if (s->is_mpeg2) {
105
        /* clock extension */
106
        put_bits(&pb, 9, 0);
107
    }
108
    put_bits(&pb, 1, 1);
109
    put_bits(&pb, 22, s->mux_rate);
110
    put_bits(&pb, 1, 1);
111
    if (s->is_mpeg2) {
112
        put_bits(&pb, 1, 1);
113
        put_bits(&pb, 5, 0x1f); /* reserved */
114
        put_bits(&pb, 3, 0); /* stuffing length */
115
    }
116
    flush_put_bits(&pb);
117
    return put_bits_ptr(&pb) - pb.buf;
118
}
119

    
120
static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
121
{
122
    MpegMuxContext *s = ctx->priv_data;
123
    int size, i, private_stream_coded, id;
124
    PutBitContext pb;
125

    
126
    init_put_bits(&pb, buf, 128);
127

    
128
    put_bits32(&pb, SYSTEM_HEADER_START_CODE);
129
    put_bits(&pb, 16, 0);
130
    put_bits(&pb, 1, 1);
131

    
132
    put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
133
    put_bits(&pb, 1, 1); /* marker */
134
    if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
135
        /* This header applies only to the video stream (see VCD standard p. IV-7)*/
136
        put_bits(&pb, 6, 0);
137
    } else
138
        put_bits(&pb, 6, s->audio_bound);
139

    
140
    if (s->is_vcd) {
141
        /* see VCD standard, p. IV-7*/
142
        put_bits(&pb, 1, 0);
143
        put_bits(&pb, 1, 1);
144
    } else {
145
        put_bits(&pb, 1, 0); /* variable bitrate*/
146
        put_bits(&pb, 1, 0); /* non constrainted bit stream */
147
    }
148

    
149
    if (s->is_vcd || s->is_dvd) {
150
        /* see VCD standard p IV-7 */
151
        put_bits(&pb, 1, 1); /* audio locked */
152
        put_bits(&pb, 1, 1); /* video locked */
153
    } else {
154
        put_bits(&pb, 1, 0); /* audio locked */
155
        put_bits(&pb, 1, 0); /* video locked */
156
    }
157

    
158
    put_bits(&pb, 1, 1); /* marker */
159

    
160
    if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
161
        /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
162
        put_bits(&pb, 5, 0);
163
    } else
164
        put_bits(&pb, 5, s->video_bound);
165

    
166
    if (s->is_dvd) {
167
        put_bits(&pb, 1, 0);    /* packet_rate_restriction_flag */
168
        put_bits(&pb, 7, 0x7f); /* reserved byte */
169
    } else
170
        put_bits(&pb, 8, 0xff); /* reserved byte */
171

    
172
    /* DVD-Video Stream_bound entries
173
    id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
174
    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)
175
    id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
176
    id (0xBF) private stream 2, NAV packs, set to 2x1024. */
177
    if (s->is_dvd) {
178

    
179
        int P_STD_max_video = 0;
180
        int P_STD_max_mpeg_audio = 0;
181
        int P_STD_max_mpeg_PS1 = 0;
182

    
183
        for(i=0;i<ctx->nb_streams;i++) {
184
            StreamInfo *stream = ctx->streams[i]->priv_data;
185

    
186
            id = stream->id;
187
            if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
188
                P_STD_max_mpeg_PS1 = stream->max_buffer_size;
189
            } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
190
                P_STD_max_mpeg_audio = stream->max_buffer_size;
191
            } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
192
                P_STD_max_video = stream->max_buffer_size;
193
            }
194
        }
195

    
196
        /* video */
197
        put_bits(&pb, 8, 0xb9); /* stream ID */
198
        put_bits(&pb, 2, 3);
199
        put_bits(&pb, 1, 1);
200
        put_bits(&pb, 13, P_STD_max_video / 1024);
201

    
202
        /* audio */
203
        if (P_STD_max_mpeg_audio == 0)
204
            P_STD_max_mpeg_audio = 4096;
205
        put_bits(&pb, 8, 0xb8); /* stream ID */
206
        put_bits(&pb, 2, 3);
207
        put_bits(&pb, 1, 0);
208
        put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
209

    
210
        /* private stream 1 */
211
        put_bits(&pb, 8, 0xbd); /* stream ID */
212
        put_bits(&pb, 2, 3);
213
        put_bits(&pb, 1, 0);
214
        put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
215

    
216
        /* private stream 2 */
217
        put_bits(&pb, 8, 0xbf); /* stream ID */
218
        put_bits(&pb, 2, 3);
219
        put_bits(&pb, 1, 1);
220
        put_bits(&pb, 13, 2);
221
    }
222
    else {
223
        /* audio stream info */
224
        private_stream_coded = 0;
225
        for(i=0;i<ctx->nb_streams;i++) {
226
            StreamInfo *stream = ctx->streams[i]->priv_data;
227

    
228

    
229
            /* For VCDs, only include the stream info for the stream
230
            that the pack which contains this system belongs to.
231
            (see VCD standard p. IV-7) */
232
            if ( !s->is_vcd || stream->id==only_for_stream_id
233
                || only_for_stream_id==0) {
234

    
235
                id = stream->id;
236
                if (id < 0xc0) {
237
                    /* special case for private streams (AC-3 uses that) */
238
                    if (private_stream_coded)
239
                        continue;
240
                    private_stream_coded = 1;
241
                    id = 0xbd;
242
                }
243
                put_bits(&pb, 8, id); /* stream ID */
244
                put_bits(&pb, 2, 3);
245
                if (id < 0xe0) {
246
                    /* audio */
247
                    put_bits(&pb, 1, 0);
248
                    put_bits(&pb, 13, stream->max_buffer_size / 128);
249
                } else {
250
                    /* video */
251
                    put_bits(&pb, 1, 1);
252
                    put_bits(&pb, 13, stream->max_buffer_size / 1024);
253
                }
254
            }
255
        }
256
    }
257

    
258
    flush_put_bits(&pb);
259
    size = put_bits_ptr(&pb) - pb.buf;
260
    /* patch packet size */
261
    buf[4] = (size - 6) >> 8;
262
    buf[5] = (size - 6) & 0xff;
263

    
264
    return size;
265
}
266

    
267
static int get_system_header_size(AVFormatContext *ctx)
268
{
269
    int buf_index, i, private_stream_coded;
270
    StreamInfo *stream;
271
    MpegMuxContext *s = ctx->priv_data;
272

    
273
    if (s->is_dvd)
274
       return 18; // DVD-Video system headers are 18 bytes fixed length.
275

    
276
    buf_index = 12;
277
    private_stream_coded = 0;
278
    for(i=0;i<ctx->nb_streams;i++) {
279
        stream = ctx->streams[i]->priv_data;
280
        if (stream->id < 0xc0) {
281
            if (private_stream_coded)
282
                continue;
283
            private_stream_coded = 1;
284
        }
285
        buf_index += 3;
286
    }
287
    return buf_index;
288
}
289

    
290
static int mpeg_mux_init(AVFormatContext *ctx)
291
{
292
    MpegMuxContext *s = ctx->priv_data;
293
    int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
294
    AVStream *st;
295
    StreamInfo *stream;
296
    int audio_bitrate;
297
    int video_bitrate;
298

    
299
    s->packet_number = 0;
300
    s->is_vcd =    (CONFIG_MPEG1VCD_MUXER  && ctx->oformat == &ff_mpeg1vcd_muxer);
301
    s->is_svcd =   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
302
    s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER  && ctx->oformat == &ff_mpeg2vob_muxer) ||
303
                   (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer) ||
304
                   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
305
    s->is_dvd =    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer);
306

    
307
    if(ctx->packet_size) {
308
        if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
309
            av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
310
                   ctx->packet_size);
311
            goto fail;
312
        }
313
        s->packet_size = ctx->packet_size;
314
    } else
315
        s->packet_size = 2048;
316

    
317
    s->vcd_padding_bytes_written = 0;
318
    s->vcd_padding_bitrate=0;
319

    
320
    s->audio_bound = 0;
321
    s->video_bound = 0;
322
    mpa_id = AUDIO_ID;
323
    ac3_id = AC3_ID;
324
    dts_id = DTS_ID;
325
    mpv_id = VIDEO_ID;
326
    mps_id = SUB_ID;
327
    lpcm_id = LPCM_ID;
328
    for(i=0;i<ctx->nb_streams;i++) {
329
        st = ctx->streams[i];
330
        stream = av_mallocz(sizeof(StreamInfo));
331
        if (!stream)
332
            goto fail;
333
        st->priv_data = stream;
334

    
335
        av_set_pts_info(st, 64, 1, 90000);
336

    
337
        switch(st->codec->codec_type) {
338
        case AVMEDIA_TYPE_AUDIO:
339
            if        (st->codec->codec_id == CODEC_ID_AC3) {
340
                stream->id = ac3_id++;
341
            } else if (st->codec->codec_id == CODEC_ID_DTS) {
342
                stream->id = dts_id++;
343
            } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
344
                stream->id = lpcm_id++;
345
                for(j = 0; j < 4; j++) {
346
                    if (lpcm_freq_tab[j] == st->codec->sample_rate)
347
                        break;
348
                }
349
                if (j == 4)
350
                    goto fail;
351
                if (st->codec->channels > 8)
352
                    return -1;
353
                stream->lpcm_header[0] = 0x0c;
354
                stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
355
                stream->lpcm_header[2] = 0x80;
356
                stream->lpcm_align = st->codec->channels * 2;
357
            } else {
358
                stream->id = mpa_id++;
359
            }
360

    
361
            /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
362
               Right now it is also used for everything else.*/
363
            stream->max_buffer_size = 4 * 1024;
364
            s->audio_bound++;
365
            break;
366
        case AVMEDIA_TYPE_VIDEO:
367
            stream->id = mpv_id++;
368
            if (st->codec->rc_buffer_size)
369
                stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
370
            else {
371
                av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n");
372
                stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
373
            }
374
#if 0
375
                /* see VCD standard, p. IV-7*/
376
                stream->max_buffer_size = 46 * 1024;
377
            else
378
                /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
379
                   Right now it is also used for everything else.*/
380
                stream->max_buffer_size = 230 * 1024;
381
#endif
382
            s->video_bound++;
383
            break;
384
        case AVMEDIA_TYPE_SUBTITLE:
385
            stream->id = mps_id++;
386
            stream->max_buffer_size = 16 * 1024;
387
            break;
388
        default:
389
            return -1;
390
        }
391
        stream->fifo= av_fifo_alloc(16);
392
        if (!stream->fifo)
393
            goto fail;
394
    }
395
    bitrate = 0;
396
    audio_bitrate = 0;
397
    video_bitrate = 0;
398
    for(i=0;i<ctx->nb_streams;i++) {
399
        int codec_rate;
400
        st = ctx->streams[i];
401
        stream = (StreamInfo*) st->priv_data;
402

    
403
        if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
404
            codec_rate= st->codec->rc_max_rate;
405
        else
406
            codec_rate= st->codec->bit_rate;
407

    
408
        if(!codec_rate)
409
            codec_rate= (1<<21)*8*50/ctx->nb_streams;
410

    
411
        bitrate += codec_rate;
412

    
413
        if ((stream->id & 0xe0) == AUDIO_ID)
414
            audio_bitrate += codec_rate;
415
        else if (stream->id==VIDEO_ID)
416
            video_bitrate += codec_rate;
417
    }
418

    
419
    if(ctx->mux_rate){
420
        s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
421
    } else {
422
        /* we increase slightly the bitrate to take into account the
423
           headers. XXX: compute it exactly */
424
        bitrate += bitrate*5/100;
425
        bitrate += 10000;
426
        s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
427
    }
428

    
429
    if (s->is_vcd) {
430
        double overhead_rate;
431

    
432
        /* The VCD standard mandates that the mux_rate field is 3528
433
           (see standard p. IV-6).
434
           The value is actually "wrong", i.e. if you calculate
435
           it using the normal formula and the 75 sectors per second transfer
436
           rate you get a different value because the real pack size is 2324,
437
           not 2352. But the standard explicitly specifies that the mux_rate
438
           field in the header must have this value.*/
439
//        s->mux_rate=2352 * 75 / 50;    /* = 3528*/
440

    
441
        /* The VCD standard states that the muxed stream must be
442
           exactly 75 packs / second (the data rate of a single speed cdrom).
443
           Since the video bitrate (probably 1150000 bits/sec) will be below
444
           the theoretical maximum we have to add some padding packets
445
           to make up for the lower data rate.
446
           (cf. VCD standard p. IV-6 )*/
447

    
448
        /* Add the header overhead to the data rate.
449
           2279 data bytes per audio pack, 2294 data bytes per video pack*/
450
        overhead_rate  = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
451
        overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
452
        overhead_rate *= 8;
453

    
454
        /* Add padding so that the full bitrate is 2324*75 bytes/sec */
455
        s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
456
    }
457

    
458
    if (s->is_vcd || s->is_mpeg2)
459
        /* every packet */
460
        s->pack_header_freq = 1;
461
    else
462
        /* every 2 seconds */
463
        s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
464

    
465
    /* the above seems to make pack_header_freq zero sometimes */
466
    if (s->pack_header_freq == 0)
467
       s->pack_header_freq = 1;
468

    
469
    if (s->is_mpeg2)
470
        /* every 200 packets. Need to look at the spec.  */
471
        s->system_header_freq = s->pack_header_freq * 40;
472
    else if (s->is_vcd)
473
        /* the standard mandates that there are only two system headers
474
           in the whole file: one in the first packet of each stream.
475
           (see standard p. IV-7 and IV-8) */
476
        s->system_header_freq = 0x7fffffff;
477
    else
478
        s->system_header_freq = s->pack_header_freq * 5;
479

    
480
    for(i=0;i<ctx->nb_streams;i++) {
481
        stream = ctx->streams[i]->priv_data;
482
        stream->packet_number = 0;
483
    }
484
    s->system_header_size = get_system_header_size(ctx);
485
    s->last_scr = 0;
486
    return 0;
487
 fail:
488
    for(i=0;i<ctx->nb_streams;i++) {
489
        av_free(ctx->streams[i]->priv_data);
490
    }
491
    return AVERROR(ENOMEM);
492
}
493

    
494
static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
495
{
496
    avio_w8(pb,
497
             (id << 4) |
498
             (((timestamp >> 30) & 0x07) << 1) |
499
             1);
500
    avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
501
    avio_wb16(pb, (uint16_t)((((timestamp      ) & 0x7fff) << 1) | 1));
502
}
503

    
504

    
505
/* return the number of padding bytes that should be inserted into
506
   the multiplexed stream.*/
507
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
508
{
509
    MpegMuxContext *s = ctx->priv_data;
510
    int pad_bytes = 0;
511

    
512
    if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
513
    {
514
        int64_t full_pad_bytes;
515

    
516
        full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
517
        pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
518

    
519
        if (pad_bytes<0)
520
            /* might happen if we have already padded to a later timestamp. This
521
               can occur if another stream has already advanced further.*/
522
            pad_bytes=0;
523
    }
524

    
525
    return pad_bytes;
526
}
527

    
528

    
529
#if 0 /* unused, remove? */
530
/* return the exact available payload size for the next packet for
531
   stream 'stream_index'. 'pts' and 'dts' are only used to know if
532
   timestamps are needed in the packet header. */
533
static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
534
                                   int64_t pts, int64_t dts)
535
{
536
    MpegMuxContext *s = ctx->priv_data;
537
    int buf_index;
538
    StreamInfo *stream;
539

540
    stream = ctx->streams[stream_index]->priv_data;
541

542
    buf_index = 0;
543
    if (((s->packet_number % s->pack_header_freq) == 0)) {
544
        /* pack header size */
545
        if (s->is_mpeg2)
546
            buf_index += 14;
547
        else
548
            buf_index += 12;
549

550
        if (s->is_vcd) {
551
            /* there is exactly one system header for each stream in a VCD MPEG,
552
               One in the very first video packet and one in the very first
553
               audio packet (see VCD standard p. IV-7 and IV-8).*/
554

555
            if (stream->packet_number==0)
556
                /* The system headers refer only to the stream they occur in,
557
                   so they have a constant size.*/
558
                buf_index += 15;
559

560
        } else {
561
            if ((s->packet_number % s->system_header_freq) == 0)
562
                buf_index += s->system_header_size;
563
        }
564
    }
565

566
    if ((s->is_vcd && stream->packet_number==0)
567
        || (s->is_svcd && s->packet_number==0))
568
        /* the first pack of each stream contains only the pack header,
569
           the system header and some padding (see VCD standard p. IV-6)
570
           Add the padding size, so that the actual payload becomes 0.*/
571
        buf_index += s->packet_size - buf_index;
572
    else {
573
        /* packet header size */
574
        buf_index += 6;
575
        if (s->is_mpeg2) {
576
            buf_index += 3;
577
            if (stream->packet_number==0)
578
                buf_index += 3; /* PES extension */
579
            buf_index += 1;    /* obligatory stuffing byte */
580
        }
581
        if (pts != AV_NOPTS_VALUE) {
582
            if (dts != pts)
583
                buf_index += 5 + 5;
584
            else
585
                buf_index += 5;
586

587
        } else {
588
            if (!s->is_mpeg2)
589
                buf_index++;
590
        }
591

592
        if (stream->id < 0xc0) {
593
            /* AC-3/LPCM private data header */
594
            buf_index += 4;
595
            if (stream->id >= 0xa0) {
596
                int n;
597
                buf_index += 3;
598
                /* NOTE: we round the payload size to an integer number of
599
                   LPCM samples */
600
                n = (s->packet_size - buf_index) % stream->lpcm_align;
601
                if (n)
602
                    buf_index += (stream->lpcm_align - n);
603
            }
604
        }
605

606
        if (s->is_vcd && (stream->id & 0xe0) == AUDIO_ID)
607
            /* The VCD standard demands that 20 zero bytes follow
608
               each audio packet (see standard p. IV-8).*/
609
            buf_index+=20;
610
    }
611
    return s->packet_size - buf_index;
612
}
613
#endif
614

    
615
/* Write an MPEG padding packet header. */
616
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
617
{
618
    MpegMuxContext *s = ctx->priv_data;
619
    int i;
620

    
621
    avio_wb32(pb, PADDING_STREAM);
622
    avio_wb16(pb, packet_bytes - 6);
623
    if (!s->is_mpeg2) {
624
        avio_w8(pb, 0x0f);
625
        packet_bytes -= 7;
626
    } else
627
        packet_bytes -= 6;
628

    
629
    for(i=0;i<packet_bytes;i++)
630
        avio_w8(pb, 0xff);
631
}
632

    
633
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
634
    int nb_frames=0;
635
    PacketDesc *pkt_desc= stream->premux_packet;
636

    
637
    while(len>0){
638
        if(pkt_desc->size == pkt_desc->unwritten_size)
639
            nb_frames++;
640
        len -= pkt_desc->unwritten_size;
641
        pkt_desc= pkt_desc->next;
642
    }
643

    
644
    return nb_frames;
645
}
646

    
647
/* flush the packet on stream stream_index */
648
static int flush_packet(AVFormatContext *ctx, int stream_index,
649
                         int64_t pts, int64_t dts, int64_t scr, int trailer_size)
650
{
651
    MpegMuxContext *s = ctx->priv_data;
652
    StreamInfo *stream = ctx->streams[stream_index]->priv_data;
653
    uint8_t *buf_ptr;
654
    int size, payload_size, startcode, id, stuffing_size, i, header_len;
655
    int packet_size;
656
    uint8_t buffer[128];
657
    int zero_trail_bytes = 0;
658
    int pad_packet_bytes = 0;
659
    int pes_flags;
660
    int general_pack = 0;  /*"general" pack without data specific to one stream?*/
661
    int nb_frames;
662

    
663
    id = stream->id;
664

    
665
#if 0
666
    printf("packet ID=%2x PTS=%0.3f\n",
667
           id, pts / 90000.0);
668
#endif
669

    
670
    buf_ptr = buffer;
671

    
672
    if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
673
        /* output pack and systems header if needed */
674
        size = put_pack_header(ctx, buf_ptr, scr);
675
        buf_ptr += size;
676
        s->last_scr= scr;
677

    
678
        if (s->is_vcd) {
679
            /* there is exactly one system header for each stream in a VCD MPEG,
680
               One in the very first video packet and one in the very first
681
               audio packet (see VCD standard p. IV-7 and IV-8).*/
682

    
683
            if (stream->packet_number==0) {
684
                size = put_system_header(ctx, buf_ptr, id);
685
                buf_ptr += size;
686
            }
687
        } else if (s->is_dvd) {
688
            if (stream->align_iframe || s->packet_number == 0){
689
                int PES_bytes_to_fill = s->packet_size - size - 10;
690

    
691
                if (pts != AV_NOPTS_VALUE) {
692
                    if (dts != pts)
693
                        PES_bytes_to_fill -= 5 + 5;
694
                    else
695
                        PES_bytes_to_fill -= 5;
696
                }
697

    
698
                if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
699
                    size = put_system_header(ctx, buf_ptr, 0);
700
                    buf_ptr += size;
701
                    size = buf_ptr - buffer;
702
                    avio_write(ctx->pb, buffer, size);
703

    
704
                    avio_wb32(ctx->pb, PRIVATE_STREAM_2);
705
                    avio_wb16(ctx->pb, 0x03d4);         // length
706
                    avio_w8(ctx->pb, 0x00);           // substream ID, 00=PCI
707
                    for (i = 0; i < 979; i++)
708
                        avio_w8(ctx->pb, 0x00);
709

    
710
                    avio_wb32(ctx->pb, PRIVATE_STREAM_2);
711
                    avio_wb16(ctx->pb, 0x03fa);         // length
712
                    avio_w8(ctx->pb, 0x01);           // substream ID, 01=DSI
713
                    for (i = 0; i < 1017; i++)
714
                        avio_w8(ctx->pb, 0x00);
715

    
716
                    memset(buffer, 0, 128);
717
                    buf_ptr = buffer;
718
                    s->packet_number++;
719
                    stream->align_iframe = 0;
720
                    scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
721
                    size = put_pack_header(ctx, buf_ptr, scr);
722
                    s->last_scr= scr;
723
                    buf_ptr += size;
724
                    /* GOP Start */
725
                } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
726
                    pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
727
                }
728
            }
729
        } else {
730
            if ((s->packet_number % s->system_header_freq) == 0) {
731
                size = put_system_header(ctx, buf_ptr, 0);
732
                buf_ptr += size;
733
            }
734
        }
735
    }
736
    size = buf_ptr - buffer;
737
    avio_write(ctx->pb, buffer, size);
738

    
739
    packet_size = s->packet_size - size;
740

    
741
    if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
742
        /* The VCD standard demands that 20 zero bytes follow
743
           each audio pack (see standard p. IV-8).*/
744
        zero_trail_bytes += 20;
745

    
746
    if ((s->is_vcd && stream->packet_number==0)
747
        || (s->is_svcd && s->packet_number==0)) {
748
        /* for VCD the first pack of each stream contains only the pack header,
749
           the system header and lots of padding (see VCD standard p. IV-6).
750
           In the case of an audio pack, 20 zero bytes are also added at
751
           the end.*/
752
        /* For SVCD we fill the very first pack to increase compatibility with
753
           some DVD players. Not mandated by the standard.*/
754
        if (s->is_svcd)
755
            general_pack = 1;    /* the system header refers to both streams and no stream data*/
756
        pad_packet_bytes = packet_size - zero_trail_bytes;
757
    }
758

    
759
    packet_size -= pad_packet_bytes + zero_trail_bytes;
760

    
761
    if (packet_size > 0) {
762

    
763
        /* packet header size */
764
        packet_size -= 6;
765

    
766
        /* packet header */
767
        if (s->is_mpeg2) {
768
            header_len = 3;
769
            if (stream->packet_number==0)
770
                header_len += 3; /* PES extension */
771
            header_len += 1; /* obligatory stuffing byte */
772
        } else {
773
            header_len = 0;
774
        }
775
        if (pts != AV_NOPTS_VALUE) {
776
            if (dts != pts)
777
                header_len += 5 + 5;
778
            else
779
                header_len += 5;
780
        } else {
781
            if (!s->is_mpeg2)
782
                header_len++;
783
        }
784

    
785
        payload_size = packet_size - header_len;
786
        if (id < 0xc0) {
787
            startcode = PRIVATE_STREAM_1;
788
            payload_size -= 1;
789
            if (id >= 0x40) {
790
                payload_size -= 3;
791
                if (id >= 0xa0)
792
                    payload_size -= 3;
793
            }
794
        } else {
795
            startcode = 0x100 + id;
796
        }
797

    
798
        stuffing_size = payload_size - av_fifo_size(stream->fifo);
799

    
800
        // first byte does not fit -> reset pts/dts + stuffing
801
        if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
802
            int timestamp_len=0;
803
            if(dts != pts)
804
                timestamp_len += 5;
805
            if(pts != AV_NOPTS_VALUE)
806
                timestamp_len += s->is_mpeg2 ? 5 : 4;
807
            pts=dts= AV_NOPTS_VALUE;
808
            header_len -= timestamp_len;
809
            if (s->is_dvd && stream->align_iframe) {
810
                pad_packet_bytes += timestamp_len;
811
                packet_size  -= timestamp_len;
812
            } else {
813
                payload_size += timestamp_len;
814
            }
815
            stuffing_size += timestamp_len;
816
            if(payload_size > trailer_size)
817
                stuffing_size += payload_size - trailer_size;
818
        }
819

    
820
        if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
821
            packet_size += pad_packet_bytes;
822
            payload_size += pad_packet_bytes; // undo the previous adjustment
823
            if (stuffing_size < 0) {
824
                stuffing_size  = pad_packet_bytes;
825
            } else {
826
                stuffing_size += pad_packet_bytes;
827
            }
828
            pad_packet_bytes = 0;
829
        }
830

    
831
        if (stuffing_size < 0)
832
            stuffing_size = 0;
833
        if (stuffing_size > 16) {    /*<=16 for MPEG-1, <=32 for MPEG-2*/
834
            pad_packet_bytes += stuffing_size;
835
            packet_size      -= stuffing_size;
836
            payload_size     -= stuffing_size;
837
            stuffing_size = 0;
838
        }
839

    
840
        nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
841

    
842
        avio_wb32(ctx->pb, startcode);
843

    
844
        avio_wb16(ctx->pb, packet_size);
845

    
846
        if (!s->is_mpeg2)
847
            for(i=0;i<stuffing_size;i++)
848
                avio_w8(ctx->pb, 0xff);
849

    
850
        if (s->is_mpeg2) {
851
            avio_w8(ctx->pb, 0x80); /* mpeg2 id */
852

    
853
            pes_flags=0;
854

    
855
            if (pts != AV_NOPTS_VALUE) {
856
                pes_flags |= 0x80;
857
                if (dts != pts)
858
                    pes_flags |= 0x40;
859
            }
860

    
861
            /* Both the MPEG-2 and the SVCD standards demand that the
862
               P-STD_buffer_size field be included in the first packet of
863
               every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
864
               and MPEG-2 standard 2.7.7) */
865
            if (stream->packet_number == 0)
866
                pes_flags |= 0x01;
867

    
868
            avio_w8(ctx->pb, pes_flags); /* flags */
869
            avio_w8(ctx->pb, header_len - 3 + stuffing_size);
870

    
871
            if (pes_flags & 0x80)  /*write pts*/
872
                put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
873
            if (pes_flags & 0x40)  /*write dts*/
874
                put_timestamp(ctx->pb, 0x01, dts);
875

    
876
            if (pes_flags & 0x01) {  /*write pes extension*/
877
                avio_w8(ctx->pb, 0x10); /* flags */
878

    
879
                /* P-STD buffer info */
880
                if ((id & 0xe0) == AUDIO_ID)
881
                    avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
882
                else
883
                    avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
884
            }
885

    
886
        } else {
887
            if (pts != AV_NOPTS_VALUE) {
888
                if (dts != pts) {
889
                    put_timestamp(ctx->pb, 0x03, pts);
890
                    put_timestamp(ctx->pb, 0x01, dts);
891
                } else {
892
                    put_timestamp(ctx->pb, 0x02, pts);
893
                }
894
            } else {
895
                avio_w8(ctx->pb, 0x0f);
896
            }
897
        }
898

    
899
        if (s->is_mpeg2) {
900
            /* special stuffing byte that is always written
901
               to prevent accidental generation of start codes. */
902
            avio_w8(ctx->pb, 0xff);
903

    
904
            for(i=0;i<stuffing_size;i++)
905
                avio_w8(ctx->pb, 0xff);
906
        }
907

    
908
        if (startcode == PRIVATE_STREAM_1) {
909
            avio_w8(ctx->pb, id);
910
            if (id >= 0xa0) {
911
                /* LPCM (XXX: check nb_frames) */
912
                avio_w8(ctx->pb, 7);
913
                avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
914
                avio_w8(ctx->pb, stream->lpcm_header[0]);
915
                avio_w8(ctx->pb, stream->lpcm_header[1]);
916
                avio_w8(ctx->pb, stream->lpcm_header[2]);
917
            } else if (id >= 0x40) {
918
                /* AC-3 */
919
                avio_w8(ctx->pb, nb_frames);
920
                avio_wb16(ctx->pb, trailer_size+1);
921
            }
922
        }
923

    
924
        /* output data */
925
        assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
926
        av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, &avio_write);
927
        stream->bytes_to_iframe -= payload_size - stuffing_size;
928
    }else{
929
        payload_size=
930
        stuffing_size= 0;
931
    }
932

    
933
    if (pad_packet_bytes > 0)
934
        put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
935

    
936
    for(i=0;i<zero_trail_bytes;i++)
937
        avio_w8(ctx->pb, 0x00);
938

    
939
    avio_flush(ctx->pb);
940

    
941
    s->packet_number++;
942

    
943
    /* only increase the stream packet number if this pack actually contains
944
       something that is specific to this stream! I.e. a dedicated header
945
       or some data.*/
946
    if (!general_pack)
947
        stream->packet_number++;
948

    
949
    return payload_size - stuffing_size;
950
}
951

    
952
static void put_vcd_padding_sector(AVFormatContext *ctx)
953
{
954
    /* There are two ways to do this padding: writing a sector/pack
955
       of 0 values, or writing an MPEG padding pack. Both seem to
956
       work with most decoders, BUT the VCD standard only allows a 0-sector
957
       (see standard p. IV-4, IV-5).
958
       So a 0-sector it is...*/
959

    
960
    MpegMuxContext *s = ctx->priv_data;
961
    int i;
962

    
963
    for(i=0;i<s->packet_size;i++)
964
        avio_w8(ctx->pb, 0);
965

    
966
    s->vcd_padding_bytes_written += s->packet_size;
967

    
968
    avio_flush(ctx->pb);
969

    
970
    /* increasing the packet number is correct. The SCR of the following packs
971
       is calculated from the packet_number and it has to include the padding
972
       sector (it represents the sector index, not the MPEG pack index)
973
       (see VCD standard p. IV-6)*/
974
    s->packet_number++;
975
}
976

    
977
#if 0 /* unused, remove? */
978
static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
979
{
980
    MpegMuxContext *s = ctx->priv_data;
981
    int64_t scr;
982

983
        /* Since the data delivery rate is constant, SCR is computed
984
           using the formula C + i * 1200 where C is the start constant
985
           and i is the pack index.
986
           It is recommended that SCR 0 is at the beginning of the VCD front
987
           margin (a sequence of empty Form 2 sectors on the CD).
988
           It is recommended that the front margin is 30 sectors long, so
989
           we use C = 30*1200 = 36000
990
           (Note that even if the front margin is not 30 sectors the file
991
           will still be correct according to the standard. It just won't have
992
           the "recommended" value).*/
993
        scr = 36000 + s->packet_number * 1200;
994

995
    return scr;
996
}
997
#endif
998

    
999
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
1000
//    MpegMuxContext *s = ctx->priv_data;
1001
    int i;
1002

    
1003
    for(i=0; i<ctx->nb_streams; i++){
1004
        AVStream *st = ctx->streams[i];
1005
        StreamInfo *stream = st->priv_data;
1006
        PacketDesc *pkt_desc;
1007

    
1008
        while((pkt_desc= stream->predecode_packet)
1009
              && scr > pkt_desc->dts){ //FIXME > vs >=
1010
            if(stream->buffer_index < pkt_desc->size ||
1011
               stream->predecode_packet == stream->premux_packet){
1012
                av_log(ctx, AV_LOG_ERROR,
1013
                       "buffer underflow i=%d bufi=%d size=%d\n",
1014
                       i, stream->buffer_index, pkt_desc->size);
1015
                break;
1016
            }
1017
            stream->buffer_index -= pkt_desc->size;
1018

    
1019
            stream->predecode_packet= pkt_desc->next;
1020
            av_freep(&pkt_desc);
1021
        }
1022
    }
1023

    
1024
    return 0;
1025
}
1026

    
1027
static int output_packet(AVFormatContext *ctx, int flush){
1028
    MpegMuxContext *s = ctx->priv_data;
1029
    AVStream *st;
1030
    StreamInfo *stream;
1031
    int i, avail_space=0, es_size, trailer_size;
1032
    int best_i= -1;
1033
    int best_score= INT_MIN;
1034
    int ignore_constraints=0;
1035
    int64_t scr= s->last_scr;
1036
    PacketDesc *timestamp_packet;
1037
    const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1038

    
1039
retry:
1040
    for(i=0; i<ctx->nb_streams; i++){
1041
        AVStream *st = ctx->streams[i];
1042
        StreamInfo *stream = st->priv_data;
1043
        const int avail_data=  av_fifo_size(stream->fifo);
1044
        const int space= stream->max_buffer_size - stream->buffer_index;
1045
        int rel_space= 1024*space / stream->max_buffer_size;
1046
        PacketDesc *next_pkt= stream->premux_packet;
1047

    
1048
        /* for subtitle, a single PES packet must be generated,
1049
           so we flush after every single subtitle packet */
1050
        if(s->packet_size > avail_data && !flush
1051
           && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
1052
            return 0;
1053
        if(avail_data==0)
1054
            continue;
1055
        assert(avail_data>0);
1056

    
1057
        if(space < s->packet_size && !ignore_constraints)
1058
            continue;
1059

    
1060
        if(next_pkt && next_pkt->dts - scr > max_delay)
1061
            continue;
1062

    
1063
        if(rel_space > best_score){
1064
            best_score= rel_space;
1065
            best_i = i;
1066
            avail_space= space;
1067
        }
1068
    }
1069

    
1070
    if(best_i < 0){
1071
        int64_t best_dts= INT64_MAX;
1072

    
1073
        for(i=0; i<ctx->nb_streams; i++){
1074
            AVStream *st = ctx->streams[i];
1075
            StreamInfo *stream = st->priv_data;
1076
            PacketDesc *pkt_desc= stream->predecode_packet;
1077
            if(pkt_desc && pkt_desc->dts < best_dts)
1078
                best_dts= pkt_desc->dts;
1079
        }
1080

    
1081
#if 0
1082
        av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
1083
               scr/90000.0, best_dts/90000.0);
1084
#endif
1085
        if(best_dts == INT64_MAX)
1086
            return 0;
1087

    
1088
        if(scr >= best_dts+1 && !ignore_constraints){
1089
            av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
1090
            ignore_constraints= 1;
1091
        }
1092
        scr= FFMAX(best_dts+1, scr);
1093
        if(remove_decoded_packets(ctx, scr) < 0)
1094
            return -1;
1095
        goto retry;
1096
    }
1097

    
1098
    assert(best_i >= 0);
1099

    
1100
    st = ctx->streams[best_i];
1101
    stream = st->priv_data;
1102

    
1103
    assert(av_fifo_size(stream->fifo) > 0);
1104

    
1105
    assert(avail_space >= s->packet_size || ignore_constraints);
1106

    
1107
    timestamp_packet= stream->premux_packet;
1108
    if(timestamp_packet->unwritten_size == timestamp_packet->size){
1109
        trailer_size= 0;
1110
    }else{
1111
        trailer_size= timestamp_packet->unwritten_size;
1112
        timestamp_packet= timestamp_packet->next;
1113
    }
1114

    
1115
    if(timestamp_packet){
1116
//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);
1117
        es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
1118
    }else{
1119
        assert(av_fifo_size(stream->fifo) == trailer_size);
1120
        es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
1121
    }
1122

    
1123
    if (s->is_vcd) {
1124
        /* Write one or more padding sectors, if necessary, to reach
1125
           the constant overall bitrate.*/
1126
        int vcd_pad_bytes;
1127

    
1128
        while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1129
            put_vcd_padding_sector(ctx);
1130
            s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1131
        }
1132
    }
1133

    
1134
    stream->buffer_index += es_size;
1135
    s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1136

    
1137
    while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
1138
        es_size -= stream->premux_packet->unwritten_size;
1139
        stream->premux_packet= stream->premux_packet->next;
1140
    }
1141
    if(es_size)
1142
        stream->premux_packet->unwritten_size -= es_size;
1143

    
1144
    if(remove_decoded_packets(ctx, s->last_scr) < 0)
1145
        return -1;
1146

    
1147
    return 1;
1148
}
1149

    
1150
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1151
{
1152
    MpegMuxContext *s = ctx->priv_data;
1153
    int stream_index= pkt->stream_index;
1154
    int size= pkt->size;
1155
    uint8_t *buf= pkt->data;
1156
    AVStream *st = ctx->streams[stream_index];
1157
    StreamInfo *stream = st->priv_data;
1158
    int64_t pts, dts;
1159
    PacketDesc *pkt_desc;
1160
    const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
1161
    const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY);
1162

    
1163
    pts= pkt->pts;
1164
    dts= pkt->dts;
1165

    
1166
    if(pts != AV_NOPTS_VALUE) pts += 2*preload;
1167
    if(dts != AV_NOPTS_VALUE){
1168
        if(!s->last_scr)
1169
            s->last_scr= dts + preload;
1170
        dts += 2*preload;
1171
    }
1172

    
1173
//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);
1174
    if (!stream->premux_packet)
1175
        stream->next_packet = &stream->premux_packet;
1176
    *stream->next_packet=
1177
    pkt_desc= av_mallocz(sizeof(PacketDesc));
1178
    pkt_desc->pts= pts;
1179
    pkt_desc->dts= dts;
1180
    pkt_desc->unwritten_size=
1181
    pkt_desc->size= size;
1182
    if(!stream->predecode_packet)
1183
        stream->predecode_packet= pkt_desc;
1184
    stream->next_packet= &pkt_desc->next;
1185

    
1186
    if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
1187
        return -1;
1188

    
1189
    if (s->is_dvd){
1190
        if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
1191
            stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1192
            stream->align_iframe = 1;
1193
            stream->vobu_start_pts = pts;
1194
        }
1195
    }
1196

    
1197
    av_fifo_generic_write(stream->fifo, buf, size, NULL);
1198

    
1199
    for(;;){
1200
        int ret= output_packet(ctx, 0);
1201
        if(ret<=0)
1202
            return ret;
1203
    }
1204
}
1205

    
1206
static int mpeg_mux_end(AVFormatContext *ctx)
1207
{
1208
//    MpegMuxContext *s = ctx->priv_data;
1209
    StreamInfo *stream;
1210
    int i;
1211

    
1212
    for(;;){
1213
        int ret= output_packet(ctx, 1);
1214
        if(ret<0)
1215
            return ret;
1216
        else if(ret==0)
1217
            break;
1218
    }
1219

    
1220
    /* End header according to MPEG1 systems standard. We do not write
1221
       it as it is usually not needed by decoders and because it
1222
       complicates MPEG stream concatenation. */
1223
    //avio_wb32(ctx->pb, ISO_11172_END_CODE);
1224
    //avio_flush(ctx->pb);
1225

    
1226
    for(i=0;i<ctx->nb_streams;i++) {
1227
        stream = ctx->streams[i]->priv_data;
1228

    
1229
        assert(av_fifo_size(stream->fifo) == 0);
1230
        av_fifo_free(stream->fifo);
1231
    }
1232
    return 0;
1233
}
1234

    
1235
#if CONFIG_MPEG1SYSTEM_MUXER
1236
AVOutputFormat ff_mpeg1system_muxer = {
1237
    "mpeg",
1238
    NULL_IF_CONFIG_SMALL("MPEG-1 System format"),
1239
    "video/mpeg",
1240
    "mpg,mpeg",
1241
    sizeof(MpegMuxContext),
1242
    CODEC_ID_MP2,
1243
    CODEC_ID_MPEG1VIDEO,
1244
    mpeg_mux_init,
1245
    mpeg_mux_write_packet,
1246
    mpeg_mux_end,
1247
};
1248
#endif
1249
#if CONFIG_MPEG1VCD_MUXER
1250
AVOutputFormat ff_mpeg1vcd_muxer = {
1251
    "vcd",
1252
    NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"),
1253
    "video/mpeg",
1254
    NULL,
1255
    sizeof(MpegMuxContext),
1256
    CODEC_ID_MP2,
1257
    CODEC_ID_MPEG1VIDEO,
1258
    mpeg_mux_init,
1259
    mpeg_mux_write_packet,
1260
    mpeg_mux_end,
1261
};
1262
#endif
1263
#if CONFIG_MPEG2VOB_MUXER
1264
AVOutputFormat ff_mpeg2vob_muxer = {
1265
    "vob",
1266
    NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
1267
    "video/mpeg",
1268
    "vob",
1269
    sizeof(MpegMuxContext),
1270
    CODEC_ID_MP2,
1271
    CODEC_ID_MPEG2VIDEO,
1272
    mpeg_mux_init,
1273
    mpeg_mux_write_packet,
1274
    mpeg_mux_end,
1275
};
1276
#endif
1277

    
1278
/* Same as mpeg2vob_mux except that the pack size is 2324 */
1279
#if CONFIG_MPEG2SVCD_MUXER
1280
AVOutputFormat ff_mpeg2svcd_muxer = {
1281
    "svcd",
1282
    NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
1283
    "video/mpeg",
1284
    "vob",
1285
    sizeof(MpegMuxContext),
1286
    CODEC_ID_MP2,
1287
    CODEC_ID_MPEG2VIDEO,
1288
    mpeg_mux_init,
1289
    mpeg_mux_write_packet,
1290
    mpeg_mux_end,
1291
};
1292
#endif
1293

    
1294
/*  Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1295
#if CONFIG_MPEG2DVD_MUXER
1296
AVOutputFormat ff_mpeg2dvd_muxer = {
1297
    "dvd",
1298
    NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"),
1299
    "video/mpeg",
1300
    "dvd",
1301
    sizeof(MpegMuxContext),
1302
    CODEC_ID_MP2,
1303
    CODEC_ID_MPEG2VIDEO,
1304
    mpeg_mux_init,
1305
    mpeg_mux_write_packet,
1306
    mpeg_mux_end,
1307
};
1308
#endif