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