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