]> git.sesse.net Git - ffmpeg/blob - libavformat/mpegenc.c
aacdec: Lower the number of frames required to detect ADTS
[ffmpeg] / libavformat / mpegenc.c
1 /*
2  * MPEG1/2 muxer
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <stdint.h>
23
24 #include "libavutil/attributes.h"
25 #include "libavutil/fifo.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
29 #include "libavcodec/put_bits.h"
30 #include "avformat.h"
31 #include "internal.h"
32 #include "mpeg.h"
33
34 #define MAX_PAYLOAD_SIZE 4096
35
36 #undef NDEBUG
37 #include <assert.h>
38
39 typedef struct PacketDesc {
40     int64_t pts;
41     int64_t dts;
42     int size;
43     int unwritten_size;
44     int flags;
45     struct PacketDesc *next;
46 } PacketDesc;
47
48 typedef struct {
49     AVFifoBuffer *fifo;
50     uint8_t id;
51     int max_buffer_size; /* in bytes */
52     int buffer_index;
53     PacketDesc *predecode_packet;
54     PacketDesc *premux_packet;
55     PacketDesc **next_packet;
56     int packet_number;
57     uint8_t lpcm_header[3];
58     int lpcm_align;
59     int bytes_to_iframe;
60     int align_iframe;
61     int64_t vobu_start_pts;
62 } StreamInfo;
63
64 typedef struct {
65     const AVClass *class;
66     int packet_size; /* required packet size */
67     int packet_number;
68     int pack_header_freq;     /* frequency (in packets^-1) at which we send pack headers */
69     int system_header_freq;
70     int system_header_size;
71     int mux_rate; /* bitrate in units of 50 bytes/s */
72     /* stream info */
73     int audio_bound;
74     int video_bound;
75     int is_mpeg2;
76     int is_vcd;
77     int is_svcd;
78     int is_dvd;
79     int64_t last_scr; /* current system clock */
80
81     double vcd_padding_bitrate; //FIXME floats
82     int64_t vcd_padding_bytes_written;
83
84     int preload;
85 } MpegMuxContext;
86
87 extern AVOutputFormat ff_mpeg1vcd_muxer;
88 extern AVOutputFormat ff_mpeg2dvd_muxer;
89 extern AVOutputFormat ff_mpeg2svcd_muxer;
90 extern AVOutputFormat ff_mpeg2vob_muxer;
91
92 static int put_pack_header(AVFormatContext *ctx,
93                            uint8_t *buf, int64_t timestamp)
94 {
95     MpegMuxContext *s = ctx->priv_data;
96     PutBitContext pb;
97
98     init_put_bits(&pb, buf, 128);
99
100     put_bits32(&pb, PACK_START_CODE);
101     if (s->is_mpeg2) {
102         put_bits(&pb, 2, 0x1);
103     } else {
104         put_bits(&pb, 4, 0x2);
105     }
106     put_bits(&pb, 3,  (uint32_t)((timestamp >> 30) & 0x07));
107     put_bits(&pb, 1, 1);
108     put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
109     put_bits(&pb, 1, 1);
110     put_bits(&pb, 15, (uint32_t)((timestamp      ) & 0x7fff));
111     put_bits(&pb, 1, 1);
112     if (s->is_mpeg2) {
113         /* clock extension */
114         put_bits(&pb, 9, 0);
115     }
116     put_bits(&pb, 1, 1);
117     put_bits(&pb, 22, s->mux_rate);
118     put_bits(&pb, 1, 1);
119     if (s->is_mpeg2) {
120         put_bits(&pb, 1, 1);
121         put_bits(&pb, 5, 0x1f); /* reserved */
122         put_bits(&pb, 3, 0); /* stuffing length */
123     }
124     flush_put_bits(&pb);
125     return put_bits_ptr(&pb) - pb.buf;
126 }
127
128 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
129 {
130     MpegMuxContext *s = ctx->priv_data;
131     int size, i, private_stream_coded, id;
132     PutBitContext pb;
133
134     init_put_bits(&pb, buf, 128);
135
136     put_bits32(&pb, SYSTEM_HEADER_START_CODE);
137     put_bits(&pb, 16, 0);
138     put_bits(&pb, 1, 1);
139
140     put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
141     put_bits(&pb, 1, 1); /* marker */
142     if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
143         /* This header applies only to the video stream (see VCD standard p. IV-7)*/
144         put_bits(&pb, 6, 0);
145     } else
146         put_bits(&pb, 6, s->audio_bound);
147
148     if (s->is_vcd) {
149         /* see VCD standard, p. IV-7*/
150         put_bits(&pb, 1, 0);
151         put_bits(&pb, 1, 1);
152     } else {
153         put_bits(&pb, 1, 0); /* variable bitrate*/
154         put_bits(&pb, 1, 0); /* non constrainted bit stream */
155     }
156
157     if (s->is_vcd || s->is_dvd) {
158         /* see VCD standard p IV-7 */
159         put_bits(&pb, 1, 1); /* audio locked */
160         put_bits(&pb, 1, 1); /* video locked */
161     } else {
162         put_bits(&pb, 1, 0); /* audio locked */
163         put_bits(&pb, 1, 0); /* video locked */
164     }
165
166     put_bits(&pb, 1, 1); /* marker */
167
168     if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
169         /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
170         put_bits(&pb, 5, 0);
171     } else
172         put_bits(&pb, 5, s->video_bound);
173
174     if (s->is_dvd) {
175         put_bits(&pb, 1, 0);    /* packet_rate_restriction_flag */
176         put_bits(&pb, 7, 0x7f); /* reserved byte */
177     } else
178         put_bits(&pb, 8, 0xff); /* reserved byte */
179
180     /* DVD-Video Stream_bound entries
181     id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
182     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)
183     id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
184     id (0xBF) private stream 2, NAV packs, set to 2x1024. */
185     if (s->is_dvd) {
186
187         int P_STD_max_video = 0;
188         int P_STD_max_mpeg_audio = 0;
189         int P_STD_max_mpeg_PS1 = 0;
190
191         for(i=0;i<ctx->nb_streams;i++) {
192             StreamInfo *stream = ctx->streams[i]->priv_data;
193
194             id = stream->id;
195             if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
196                 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
197             } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
198                 P_STD_max_mpeg_audio = stream->max_buffer_size;
199             } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
200                 P_STD_max_video = stream->max_buffer_size;
201             }
202         }
203
204         /* video */
205         put_bits(&pb, 8, 0xb9); /* stream ID */
206         put_bits(&pb, 2, 3);
207         put_bits(&pb, 1, 1);
208         put_bits(&pb, 13, P_STD_max_video / 1024);
209
210         /* audio */
211         if (P_STD_max_mpeg_audio == 0)
212             P_STD_max_mpeg_audio = 4096;
213         put_bits(&pb, 8, 0xb8); /* stream ID */
214         put_bits(&pb, 2, 3);
215         put_bits(&pb, 1, 0);
216         put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
217
218         /* private stream 1 */
219         put_bits(&pb, 8, 0xbd); /* stream ID */
220         put_bits(&pb, 2, 3);
221         put_bits(&pb, 1, 0);
222         put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
223
224         /* private stream 2 */
225         put_bits(&pb, 8, 0xbf); /* stream ID */
226         put_bits(&pb, 2, 3);
227         put_bits(&pb, 1, 1);
228         put_bits(&pb, 13, 2);
229     }
230     else {
231         /* audio stream info */
232         private_stream_coded = 0;
233         for(i=0;i<ctx->nb_streams;i++) {
234             StreamInfo *stream = ctx->streams[i]->priv_data;
235
236
237             /* For VCDs, only include the stream info for the stream
238             that the pack which contains this system belongs to.
239             (see VCD standard p. IV-7) */
240             if ( !s->is_vcd || stream->id==only_for_stream_id
241                 || only_for_stream_id==0) {
242
243                 id = stream->id;
244                 if (id < 0xc0) {
245                     /* special case for private streams (AC-3 uses that) */
246                     if (private_stream_coded)
247                         continue;
248                     private_stream_coded = 1;
249                     id = 0xbd;
250                 }
251                 put_bits(&pb, 8, id); /* stream ID */
252                 put_bits(&pb, 2, 3);
253                 if (id < 0xe0) {
254                     /* audio */
255                     put_bits(&pb, 1, 0);
256                     put_bits(&pb, 13, stream->max_buffer_size / 128);
257                 } else {
258                     /* video */
259                     put_bits(&pb, 1, 1);
260                     put_bits(&pb, 13, stream->max_buffer_size / 1024);
261                 }
262             }
263         }
264     }
265
266     flush_put_bits(&pb);
267     size = put_bits_ptr(&pb) - pb.buf;
268     /* patch packet size */
269     buf[4] = (size - 6) >> 8;
270     buf[5] = (size - 6) & 0xff;
271
272     return size;
273 }
274
275 static int get_system_header_size(AVFormatContext *ctx)
276 {
277     int buf_index, i, private_stream_coded;
278     StreamInfo *stream;
279     MpegMuxContext *s = ctx->priv_data;
280
281     if (s->is_dvd)
282        return 18; // DVD-Video system headers are 18 bytes fixed length.
283
284     buf_index = 12;
285     private_stream_coded = 0;
286     for(i=0;i<ctx->nb_streams;i++) {
287         stream = ctx->streams[i]->priv_data;
288         if (stream->id < 0xc0) {
289             if (private_stream_coded)
290                 continue;
291             private_stream_coded = 1;
292         }
293         buf_index += 3;
294     }
295     return buf_index;
296 }
297
298 static av_cold int mpeg_mux_init(AVFormatContext *ctx)
299 {
300     MpegMuxContext *s = ctx->priv_data;
301     int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
302     AVStream *st;
303     StreamInfo *stream;
304     int audio_bitrate;
305     int video_bitrate;
306
307     s->packet_number = 0;
308     s->is_vcd =    (CONFIG_MPEG1VCD_MUXER  && ctx->oformat == &ff_mpeg1vcd_muxer);
309     s->is_svcd =   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
310     s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER  && ctx->oformat == &ff_mpeg2vob_muxer) ||
311                    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer) ||
312                    (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
313     s->is_dvd =    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer);
314
315     if(ctx->packet_size) {
316         if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
317             av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
318                    ctx->packet_size);
319             goto fail;
320         }
321         s->packet_size = ctx->packet_size;
322     } else
323         s->packet_size = 2048;
324     if (ctx->max_delay < 0) /* Not set by the caller */
325         ctx->max_delay = 0;
326
327     s->vcd_padding_bytes_written = 0;
328     s->vcd_padding_bitrate=0;
329
330     s->audio_bound = 0;
331     s->video_bound = 0;
332     mpa_id = AUDIO_ID;
333     ac3_id = AC3_ID;
334     dts_id = DTS_ID;
335     mpv_id = VIDEO_ID;
336     mps_id = SUB_ID;
337     lpcm_id = LPCM_ID;
338     for(i=0;i<ctx->nb_streams;i++) {
339         st = ctx->streams[i];
340         stream = av_mallocz(sizeof(StreamInfo));
341         if (!stream)
342             goto fail;
343         st->priv_data = stream;
344
345         avpriv_set_pts_info(st, 64, 1, 90000);
346
347         switch(st->codec->codec_type) {
348         case AVMEDIA_TYPE_AUDIO:
349             if        (st->codec->codec_id == AV_CODEC_ID_AC3) {
350                 stream->id = ac3_id++;
351             } else if (st->codec->codec_id == AV_CODEC_ID_DTS) {
352                 stream->id = dts_id++;
353             } else if (st->codec->codec_id == AV_CODEC_ID_PCM_S16BE) {
354                 stream->id = lpcm_id++;
355                 for(j = 0; j < 4; j++) {
356                     if (lpcm_freq_tab[j] == st->codec->sample_rate)
357                         break;
358                 }
359                 if (j == 4)
360                     goto fail;
361                 if (st->codec->channels > 8)
362                     return -1;
363                 stream->lpcm_header[0] = 0x0c;
364                 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
365                 stream->lpcm_header[2] = 0x80;
366                 stream->lpcm_align = st->codec->channels * 2;
367             } else {
368                 stream->id = mpa_id++;
369             }
370
371             /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
372                Right now it is also used for everything else.*/
373             stream->max_buffer_size = 4 * 1024;
374             s->audio_bound++;
375             break;
376         case AVMEDIA_TYPE_VIDEO:
377             stream->id = mpv_id++;
378             if (st->codec->rc_buffer_size)
379                 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
380             else {
381                 av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n");
382                 stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
383             }
384             s->video_bound++;
385             break;
386         case AVMEDIA_TYPE_SUBTITLE:
387             stream->id = mps_id++;
388             stream->max_buffer_size = 16 * 1024;
389             break;
390         default:
391             return -1;
392         }
393         stream->fifo= av_fifo_alloc(16);
394         if (!stream->fifo)
395             goto fail;
396     }
397     bitrate = 0;
398     audio_bitrate = 0;
399     video_bitrate = 0;
400     for(i=0;i<ctx->nb_streams;i++) {
401         int codec_rate;
402         st = ctx->streams[i];
403         stream = (StreamInfo*) st->priv_data;
404
405         if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
406             codec_rate= st->codec->rc_max_rate;
407         else
408             codec_rate= st->codec->bit_rate;
409
410         if(!codec_rate)
411             codec_rate= (1<<21)*8*50/ctx->nb_streams;
412
413         bitrate += codec_rate;
414
415         if ((stream->id & 0xe0) == AUDIO_ID)
416             audio_bitrate += codec_rate;
417         else if (stream->id==VIDEO_ID)
418             video_bitrate += codec_rate;
419     }
420
421     if (!s->mux_rate) {
422         /* we increase slightly the bitrate to take into account the
423            headers. XXX: compute it exactly */
424         bitrate += bitrate / 20;
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 /* Write an MPEG padding packet header. */
530 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
531 {
532     MpegMuxContext *s = ctx->priv_data;
533     int i;
534
535     avio_wb32(pb, PADDING_STREAM);
536     avio_wb16(pb, packet_bytes - 6);
537     if (!s->is_mpeg2) {
538         avio_w8(pb, 0x0f);
539         packet_bytes -= 7;
540     } else
541         packet_bytes -= 6;
542
543     for(i=0;i<packet_bytes;i++)
544         avio_w8(pb, 0xff);
545 }
546
547 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
548     int nb_frames=0;
549     PacketDesc *pkt_desc= stream->premux_packet;
550
551     while(len>0){
552         if(pkt_desc->size == pkt_desc->unwritten_size)
553             nb_frames++;
554         len -= pkt_desc->unwritten_size;
555         pkt_desc= pkt_desc->next;
556     }
557
558     return nb_frames;
559 }
560
561 /* flush the packet on stream stream_index */
562 static int flush_packet(AVFormatContext *ctx, int stream_index,
563                          int64_t pts, int64_t dts, int64_t scr, int trailer_size)
564 {
565     MpegMuxContext *s = ctx->priv_data;
566     StreamInfo *stream = ctx->streams[stream_index]->priv_data;
567     uint8_t *buf_ptr;
568     int size, payload_size, startcode, id, stuffing_size, i, header_len;
569     int packet_size;
570     uint8_t buffer[128];
571     int zero_trail_bytes = 0;
572     int pad_packet_bytes = 0;
573     int pes_flags;
574     int general_pack = 0;  /*"general" pack without data specific to one stream?*/
575     int nb_frames;
576
577     id = stream->id;
578
579     av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
580
581     buf_ptr = buffer;
582
583     if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
584         /* output pack and systems header if needed */
585         size = put_pack_header(ctx, buf_ptr, scr);
586         buf_ptr += size;
587         s->last_scr= scr;
588
589         if (s->is_vcd) {
590             /* there is exactly one system header for each stream in a VCD MPEG,
591                One in the very first video packet and one in the very first
592                audio packet (see VCD standard p. IV-7 and IV-8).*/
593
594             if (stream->packet_number==0) {
595                 size = put_system_header(ctx, buf_ptr, id);
596                 buf_ptr += size;
597             }
598         } else if (s->is_dvd) {
599             if (stream->align_iframe || s->packet_number == 0){
600                 int PES_bytes_to_fill = s->packet_size - size - 10;
601
602                 if (pts != AV_NOPTS_VALUE) {
603                     if (dts != pts)
604                         PES_bytes_to_fill -= 5 + 5;
605                     else
606                         PES_bytes_to_fill -= 5;
607                 }
608
609                 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
610                     size = put_system_header(ctx, buf_ptr, 0);
611                     buf_ptr += size;
612                     size = buf_ptr - buffer;
613                     avio_write(ctx->pb, buffer, size);
614
615                     avio_wb32(ctx->pb, PRIVATE_STREAM_2);
616                     avio_wb16(ctx->pb, 0x03d4);         // length
617                     avio_w8(ctx->pb, 0x00);           // substream ID, 00=PCI
618                     for (i = 0; i < 979; i++)
619                         avio_w8(ctx->pb, 0x00);
620
621                     avio_wb32(ctx->pb, PRIVATE_STREAM_2);
622                     avio_wb16(ctx->pb, 0x03fa);         // length
623                     avio_w8(ctx->pb, 0x01);           // substream ID, 01=DSI
624                     for (i = 0; i < 1017; i++)
625                         avio_w8(ctx->pb, 0x00);
626
627                     memset(buffer, 0, 128);
628                     buf_ptr = buffer;
629                     s->packet_number++;
630                     stream->align_iframe = 0;
631                     scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
632                     size = put_pack_header(ctx, buf_ptr, scr);
633                     s->last_scr= scr;
634                     buf_ptr += size;
635                     /* GOP Start */
636                 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
637                     pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
638                 }
639             }
640         } else {
641             if ((s->packet_number % s->system_header_freq) == 0) {
642                 size = put_system_header(ctx, buf_ptr, 0);
643                 buf_ptr += size;
644             }
645         }
646     }
647     size = buf_ptr - buffer;
648     avio_write(ctx->pb, buffer, size);
649
650     packet_size = s->packet_size - size;
651
652     if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
653         /* The VCD standard demands that 20 zero bytes follow
654            each audio pack (see standard p. IV-8).*/
655         zero_trail_bytes += 20;
656
657     if ((s->is_vcd && stream->packet_number==0)
658         || (s->is_svcd && s->packet_number==0)) {
659         /* for VCD the first pack of each stream contains only the pack header,
660            the system header and lots of padding (see VCD standard p. IV-6).
661            In the case of an audio pack, 20 zero bytes are also added at
662            the end.*/
663         /* For SVCD we fill the very first pack to increase compatibility with
664            some DVD players. Not mandated by the standard.*/
665         if (s->is_svcd)
666             general_pack = 1;    /* the system header refers to both streams and no stream data*/
667         pad_packet_bytes = packet_size - zero_trail_bytes;
668     }
669
670     packet_size -= pad_packet_bytes + zero_trail_bytes;
671
672     if (packet_size > 0) {
673
674         /* packet header size */
675         packet_size -= 6;
676
677         /* packet header */
678         if (s->is_mpeg2) {
679             header_len = 3;
680             if (stream->packet_number==0)
681                 header_len += 3; /* PES extension */
682             header_len += 1; /* obligatory stuffing byte */
683         } else {
684             header_len = 0;
685         }
686         if (pts != AV_NOPTS_VALUE) {
687             if (dts != pts)
688                 header_len += 5 + 5;
689             else
690                 header_len += 5;
691         } else {
692             if (!s->is_mpeg2)
693                 header_len++;
694         }
695
696         payload_size = packet_size - header_len;
697         if (id < 0xc0) {
698             startcode = PRIVATE_STREAM_1;
699             payload_size -= 1;
700             if (id >= 0x40) {
701                 payload_size -= 3;
702                 if (id >= 0xa0)
703                     payload_size -= 3;
704             }
705         } else {
706             startcode = 0x100 + id;
707         }
708
709         stuffing_size = payload_size - av_fifo_size(stream->fifo);
710
711         // first byte does not fit -> reset pts/dts + stuffing
712         if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
713             int timestamp_len=0;
714             if(dts != pts)
715                 timestamp_len += 5;
716             if(pts != AV_NOPTS_VALUE)
717                 timestamp_len += s->is_mpeg2 ? 5 : 4;
718             pts=dts= AV_NOPTS_VALUE;
719             header_len -= timestamp_len;
720             if (s->is_dvd && stream->align_iframe) {
721                 pad_packet_bytes += timestamp_len;
722                 packet_size  -= timestamp_len;
723             } else {
724                 payload_size += timestamp_len;
725             }
726             stuffing_size += timestamp_len;
727             if(payload_size > trailer_size)
728                 stuffing_size += payload_size - trailer_size;
729         }
730
731         if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
732             packet_size += pad_packet_bytes;
733             payload_size += pad_packet_bytes; // undo the previous adjustment
734             if (stuffing_size < 0) {
735                 stuffing_size  = pad_packet_bytes;
736             } else {
737                 stuffing_size += pad_packet_bytes;
738             }
739             pad_packet_bytes = 0;
740         }
741
742         if (stuffing_size < 0)
743             stuffing_size = 0;
744
745         if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
746             if (payload_size < av_fifo_size(stream->fifo))
747                 stuffing_size += payload_size % stream->lpcm_align;
748         }
749
750         if (stuffing_size > 16) {    /*<=16 for MPEG-1, <=32 for MPEG-2*/
751             pad_packet_bytes += stuffing_size;
752             packet_size      -= stuffing_size;
753             payload_size     -= stuffing_size;
754             stuffing_size = 0;
755         }
756
757         nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
758
759         avio_wb32(ctx->pb, startcode);
760
761         avio_wb16(ctx->pb, packet_size);
762
763         if (!s->is_mpeg2)
764             for(i=0;i<stuffing_size;i++)
765                 avio_w8(ctx->pb, 0xff);
766
767         if (s->is_mpeg2) {
768             avio_w8(ctx->pb, 0x80); /* mpeg2 id */
769
770             pes_flags=0;
771
772             if (pts != AV_NOPTS_VALUE) {
773                 pes_flags |= 0x80;
774                 if (dts != pts)
775                     pes_flags |= 0x40;
776             }
777
778             /* Both the MPEG-2 and the SVCD standards demand that the
779                P-STD_buffer_size field be included in the first packet of
780                every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
781                and MPEG-2 standard 2.7.7) */
782             if (stream->packet_number == 0)
783                 pes_flags |= 0x01;
784
785             avio_w8(ctx->pb, pes_flags); /* flags */
786             avio_w8(ctx->pb, header_len - 3 + stuffing_size);
787
788             if (pes_flags & 0x80)  /*write pts*/
789                 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
790             if (pes_flags & 0x40)  /*write dts*/
791                 put_timestamp(ctx->pb, 0x01, dts);
792
793             if (pes_flags & 0x01) {  /*write pes extension*/
794                 avio_w8(ctx->pb, 0x10); /* flags */
795
796                 /* P-STD buffer info */
797                 if ((id & 0xe0) == AUDIO_ID)
798                     avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
799                 else
800                     avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
801             }
802
803         } else {
804             if (pts != AV_NOPTS_VALUE) {
805                 if (dts != pts) {
806                     put_timestamp(ctx->pb, 0x03, pts);
807                     put_timestamp(ctx->pb, 0x01, dts);
808                 } else {
809                     put_timestamp(ctx->pb, 0x02, pts);
810                 }
811             } else {
812                 avio_w8(ctx->pb, 0x0f);
813             }
814         }
815
816         if (s->is_mpeg2) {
817             /* special stuffing byte that is always written
818                to prevent accidental generation of start codes. */
819             avio_w8(ctx->pb, 0xff);
820
821             for(i=0;i<stuffing_size;i++)
822                 avio_w8(ctx->pb, 0xff);
823         }
824
825         if (startcode == PRIVATE_STREAM_1) {
826             avio_w8(ctx->pb, id);
827             if (id >= 0xa0) {
828                 /* LPCM (XXX: check nb_frames) */
829                 avio_w8(ctx->pb, 7);
830                 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
831                 avio_w8(ctx->pb, stream->lpcm_header[0]);
832                 avio_w8(ctx->pb, stream->lpcm_header[1]);
833                 avio_w8(ctx->pb, stream->lpcm_header[2]);
834             } else if (id >= 0x40) {
835                 /* AC-3 */
836                 avio_w8(ctx->pb, nb_frames);
837                 avio_wb16(ctx->pb, trailer_size+1);
838             }
839         }
840
841         /* output data */
842         assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
843         av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, &avio_write);
844         stream->bytes_to_iframe -= payload_size - stuffing_size;
845     }else{
846         payload_size=
847         stuffing_size= 0;
848     }
849
850     if (pad_packet_bytes > 0)
851         put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
852
853     for(i=0;i<zero_trail_bytes;i++)
854         avio_w8(ctx->pb, 0x00);
855
856     avio_flush(ctx->pb);
857
858     s->packet_number++;
859
860     /* only increase the stream packet number if this pack actually contains
861        something that is specific to this stream! I.e. a dedicated header
862        or some data.*/
863     if (!general_pack)
864         stream->packet_number++;
865
866     return payload_size - stuffing_size;
867 }
868
869 static void put_vcd_padding_sector(AVFormatContext *ctx)
870 {
871     /* There are two ways to do this padding: writing a sector/pack
872        of 0 values, or writing an MPEG padding pack. Both seem to
873        work with most decoders, BUT the VCD standard only allows a 0-sector
874        (see standard p. IV-4, IV-5).
875        So a 0-sector it is...*/
876
877     MpegMuxContext *s = ctx->priv_data;
878     int i;
879
880     for(i=0;i<s->packet_size;i++)
881         avio_w8(ctx->pb, 0);
882
883     s->vcd_padding_bytes_written += s->packet_size;
884
885     avio_flush(ctx->pb);
886
887     /* increasing the packet number is correct. The SCR of the following packs
888        is calculated from the packet_number and it has to include the padding
889        sector (it represents the sector index, not the MPEG pack index)
890        (see VCD standard p. IV-6)*/
891     s->packet_number++;
892 }
893
894 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
895 //    MpegMuxContext *s = ctx->priv_data;
896     int i;
897
898     for(i=0; i<ctx->nb_streams; i++){
899         AVStream *st = ctx->streams[i];
900         StreamInfo *stream = st->priv_data;
901         PacketDesc *pkt_desc;
902
903         while((pkt_desc= stream->predecode_packet)
904               && scr > pkt_desc->dts){ //FIXME > vs >=
905             if(stream->buffer_index < pkt_desc->size ||
906                stream->predecode_packet == stream->premux_packet){
907                 av_log(ctx, AV_LOG_ERROR,
908                        "buffer underflow i=%d bufi=%d size=%d\n",
909                        i, stream->buffer_index, pkt_desc->size);
910                 break;
911             }
912             stream->buffer_index -= pkt_desc->size;
913
914             stream->predecode_packet= pkt_desc->next;
915             av_freep(&pkt_desc);
916         }
917     }
918
919     return 0;
920 }
921
922 static int output_packet(AVFormatContext *ctx, int flush){
923     MpegMuxContext *s = ctx->priv_data;
924     AVStream *st;
925     StreamInfo *stream;
926     int i, avail_space=0, es_size, trailer_size;
927     int best_i= -1;
928     int best_score= INT_MIN;
929     int ignore_constraints=0;
930     int64_t scr= s->last_scr;
931     PacketDesc *timestamp_packet;
932     const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
933
934 retry:
935     for(i=0; i<ctx->nb_streams; i++){
936         AVStream *st = ctx->streams[i];
937         StreamInfo *stream = st->priv_data;
938         const int avail_data=  av_fifo_size(stream->fifo);
939         const int space= stream->max_buffer_size - stream->buffer_index;
940         int rel_space= 1024*space / stream->max_buffer_size;
941         PacketDesc *next_pkt= stream->premux_packet;
942
943         /* for subtitle, a single PES packet must be generated,
944            so we flush after every single subtitle packet */
945         if(s->packet_size > avail_data && !flush
946            && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
947             return 0;
948         if(avail_data==0)
949             continue;
950         assert(avail_data>0);
951
952         if(space < s->packet_size && !ignore_constraints)
953             continue;
954
955         if(next_pkt && next_pkt->dts - scr > max_delay)
956             continue;
957
958         if(rel_space > best_score){
959             best_score= rel_space;
960             best_i = i;
961             avail_space= space;
962         }
963     }
964
965     if(best_i < 0){
966         int64_t best_dts= INT64_MAX;
967
968         for(i=0; i<ctx->nb_streams; i++){
969             AVStream *st = ctx->streams[i];
970             StreamInfo *stream = st->priv_data;
971             PacketDesc *pkt_desc= stream->predecode_packet;
972             if(pkt_desc && pkt_desc->dts < best_dts)
973                 best_dts= pkt_desc->dts;
974         }
975
976         av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
977                 scr / 90000.0, best_dts / 90000.0);
978         if(best_dts == INT64_MAX)
979             return 0;
980
981         if(scr >= best_dts+1 && !ignore_constraints){
982             av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
983             ignore_constraints= 1;
984         }
985         scr= FFMAX(best_dts+1, scr);
986         if(remove_decoded_packets(ctx, scr) < 0)
987             return -1;
988         goto retry;
989     }
990
991     assert(best_i >= 0);
992
993     st = ctx->streams[best_i];
994     stream = st->priv_data;
995
996     assert(av_fifo_size(stream->fifo) > 0);
997
998     assert(avail_space >= s->packet_size || ignore_constraints);
999
1000     timestamp_packet= stream->premux_packet;
1001     if(timestamp_packet->unwritten_size == timestamp_packet->size){
1002         trailer_size= 0;
1003     }else{
1004         trailer_size= timestamp_packet->unwritten_size;
1005         timestamp_packet= timestamp_packet->next;
1006     }
1007
1008     if(timestamp_packet){
1009         av_dlog(ctx, "dts:%f pts:%f scr:%f stream:%d\n",
1010                 timestamp_packet->dts / 90000.0,
1011                 timestamp_packet->pts / 90000.0,
1012                 scr / 90000.0, best_i);
1013         es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
1014     }else{
1015         assert(av_fifo_size(stream->fifo) == trailer_size);
1016         es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
1017     }
1018
1019     if (s->is_vcd) {
1020         /* Write one or more padding sectors, if necessary, to reach
1021            the constant overall bitrate.*/
1022         int vcd_pad_bytes;
1023
1024         while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1025             put_vcd_padding_sector(ctx);
1026             s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1027         }
1028     }
1029
1030     stream->buffer_index += es_size;
1031     s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1032
1033     while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
1034         es_size -= stream->premux_packet->unwritten_size;
1035         stream->premux_packet= stream->premux_packet->next;
1036     }
1037     if(es_size)
1038         stream->premux_packet->unwritten_size -= es_size;
1039
1040     if(remove_decoded_packets(ctx, s->last_scr) < 0)
1041         return -1;
1042
1043     return 1;
1044 }
1045
1046 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1047 {
1048     MpegMuxContext *s = ctx->priv_data;
1049     int stream_index= pkt->stream_index;
1050     int size= pkt->size;
1051     uint8_t *buf= pkt->data;
1052     AVStream *st = ctx->streams[stream_index];
1053     StreamInfo *stream = st->priv_data;
1054     int64_t pts, dts;
1055     PacketDesc *pkt_desc;
1056     int preload;
1057     const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY);
1058
1059     preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1060
1061     pts= pkt->pts;
1062     dts= pkt->dts;
1063
1064     if(pts != AV_NOPTS_VALUE) pts += 2*preload;
1065     if(dts != AV_NOPTS_VALUE){
1066         if(!s->last_scr)
1067             s->last_scr= dts + preload;
1068         dts += 2*preload;
1069     }
1070
1071     av_dlog(ctx, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1072             dts / 90000.0, pts / 90000.0, pkt->flags,
1073             pkt->stream_index, pts != AV_NOPTS_VALUE);
1074     if (!stream->premux_packet)
1075         stream->next_packet = &stream->premux_packet;
1076     *stream->next_packet=
1077     pkt_desc= av_mallocz(sizeof(PacketDesc));
1078     pkt_desc->pts= pts;
1079     pkt_desc->dts= dts;
1080     pkt_desc->unwritten_size=
1081     pkt_desc->size= size;
1082     if(!stream->predecode_packet)
1083         stream->predecode_packet= pkt_desc;
1084     stream->next_packet= &pkt_desc->next;
1085
1086     if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
1087         return -1;
1088
1089     if (s->is_dvd){
1090         if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
1091             stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1092             stream->align_iframe = 1;
1093             stream->vobu_start_pts = pts;
1094         }
1095     }
1096
1097     av_fifo_generic_write(stream->fifo, buf, size, NULL);
1098
1099     for(;;){
1100         int ret= output_packet(ctx, 0);
1101         if(ret<=0)
1102             return ret;
1103     }
1104 }
1105
1106 static int mpeg_mux_end(AVFormatContext *ctx)
1107 {
1108 //    MpegMuxContext *s = ctx->priv_data;
1109     StreamInfo *stream;
1110     int i;
1111
1112     for(;;){
1113         int ret= output_packet(ctx, 1);
1114         if(ret<0)
1115             return ret;
1116         else if(ret==0)
1117             break;
1118     }
1119
1120     /* End header according to MPEG1 systems standard. We do not write
1121        it as it is usually not needed by decoders and because it
1122        complicates MPEG stream concatenation. */
1123     //avio_wb32(ctx->pb, ISO_11172_END_CODE);
1124     //avio_flush(ctx->pb);
1125
1126     for(i=0;i<ctx->nb_streams;i++) {
1127         stream = ctx->streams[i]->priv_data;
1128
1129         assert(av_fifo_size(stream->fifo) == 0);
1130         av_fifo_free(stream->fifo);
1131     }
1132     return 0;
1133 }
1134
1135 #define OFFSET(x) offsetof(MpegMuxContext, x)
1136 #define E AV_OPT_FLAG_ENCODING_PARAM
1137 static const AVOption options[] = {
1138     { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
1139     { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload),  AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX, E},
1140     { NULL },
1141 };
1142
1143 #define MPEGENC_CLASS(flavor)\
1144 static const AVClass flavor ## _class = {\
1145     .class_name = #flavor " muxer",\
1146     .item_name  = av_default_item_name,\
1147     .version    = LIBAVUTIL_VERSION_INT,\
1148     .option     = options,\
1149 };
1150
1151 #if CONFIG_MPEG1SYSTEM_MUXER
1152 MPEGENC_CLASS(mpeg)
1153 AVOutputFormat ff_mpeg1system_muxer = {
1154     .name              = "mpeg",
1155     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
1156     .mime_type         = "video/mpeg",
1157     .extensions        = "mpg,mpeg",
1158     .priv_data_size    = sizeof(MpegMuxContext),
1159     .audio_codec       = AV_CODEC_ID_MP2,
1160     .video_codec       = AV_CODEC_ID_MPEG1VIDEO,
1161     .write_header      = mpeg_mux_init,
1162     .write_packet      = mpeg_mux_write_packet,
1163     .write_trailer     = mpeg_mux_end,
1164     .priv_class        = &mpeg_class,
1165 };
1166 #endif
1167 #if CONFIG_MPEG1VCD_MUXER
1168 MPEGENC_CLASS(vcd)
1169 AVOutputFormat ff_mpeg1vcd_muxer = {
1170     .name              = "vcd",
1171     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
1172     .mime_type         = "video/mpeg",
1173     .priv_data_size    = sizeof(MpegMuxContext),
1174     .audio_codec       = AV_CODEC_ID_MP2,
1175     .video_codec       = AV_CODEC_ID_MPEG1VIDEO,
1176     .write_header      = mpeg_mux_init,
1177     .write_packet      = mpeg_mux_write_packet,
1178     .write_trailer     = mpeg_mux_end,
1179     .priv_class        = &vcd_class,
1180 };
1181 #endif
1182 #if CONFIG_MPEG2VOB_MUXER
1183 MPEGENC_CLASS(vob)
1184 AVOutputFormat ff_mpeg2vob_muxer = {
1185     .name              = "vob",
1186     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
1187     .mime_type         = "video/mpeg",
1188     .extensions        = "vob",
1189     .priv_data_size    = sizeof(MpegMuxContext),
1190     .audio_codec       = AV_CODEC_ID_MP2,
1191     .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
1192     .write_header      = mpeg_mux_init,
1193     .write_packet      = mpeg_mux_write_packet,
1194     .write_trailer     = mpeg_mux_end,
1195     .priv_class        = &vob_class,
1196 };
1197 #endif
1198
1199 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1200 #if CONFIG_MPEG2SVCD_MUXER
1201 MPEGENC_CLASS(svcd)
1202 AVOutputFormat ff_mpeg2svcd_muxer = {
1203     .name              = "svcd",
1204     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
1205     .mime_type         = "video/mpeg",
1206     .extensions        = "vob",
1207     .priv_data_size    = sizeof(MpegMuxContext),
1208     .audio_codec       = AV_CODEC_ID_MP2,
1209     .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
1210     .write_header      = mpeg_mux_init,
1211     .write_packet      = mpeg_mux_write_packet,
1212     .write_trailer     = mpeg_mux_end,
1213     .priv_class        = &svcd_class,
1214 };
1215 #endif
1216
1217 /*  Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1218 #if CONFIG_MPEG2DVD_MUXER
1219 MPEGENC_CLASS(dvd)
1220 AVOutputFormat ff_mpeg2dvd_muxer = {
1221     .name              = "dvd",
1222     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
1223     .mime_type         = "video/mpeg",
1224     .extensions        = "dvd",
1225     .priv_data_size    = sizeof(MpegMuxContext),
1226     .audio_codec       = AV_CODEC_ID_MP2,
1227     .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
1228     .write_header      = mpeg_mux_init,
1229     .write_packet      = mpeg_mux_write_packet,
1230     .write_trailer     = mpeg_mux_end,
1231     .priv_class        = &dvd_class,
1232 };
1233 #endif