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