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