FFmpeg
mpegenc.c
Go to the documentation of this file.
1 /*
2  * MPEG-1/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 "avio_internal.h"
34 #include "internal.h"
35 #include "mpeg.h"
36 
37 #define MAX_PAYLOAD_SIZE 4096
38 
39 typedef struct PacketDesc {
40  int64_t pts;
41  int64_t dts;
42  int size;
44  struct PacketDesc *next;
45 } PacketDesc;
46 
47 typedef struct StreamInfo {
49  uint8_t id;
50  int max_buffer_size; /* in bytes */
52  PacketDesc *predecode_packet; /* start of packet queue */
53  PacketDesc *last_packet; /* end of packet queue */
56  uint8_t lpcm_header[3];
60  int64_t vobu_start_pts;
61 } StreamInfo;
62 
63 typedef struct MpegMuxContext {
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 
83 
84  int preload;
86 
91 
92 static int put_pack_header(AVFormatContext *ctx, uint8_t *buf,
93  int64_t timestamp)
94 {
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 
126 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,
127  int only_for_stream_id)
128 {
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); /* nonconstrained bitstream */
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;
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 {
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  return AVERROR(EINVAL);
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 = AV_TIME_BASE*7/10;
326 
327  s->vcd_padding_bytes_written = 0;
328  s->vcd_padding_bitrate_num = 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  AVCPBProperties *props;
343 
344  st = ctx->streams[i];
345  stream = av_mallocz(sizeof(StreamInfo));
346  if (!stream)
347  return AVERROR(ENOMEM);
348  st->priv_data = stream;
349 
350  avpriv_set_pts_info(st, 64, 1, 90000);
351 
352  switch (st->codecpar->codec_type) {
353  case AVMEDIA_TYPE_AUDIO:
354  if (!s->is_mpeg2 &&
355  (st->codecpar->codec_id == AV_CODEC_ID_AC3 ||
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->codecpar->codec_id == AV_CODEC_ID_AC3) {
365  stream->id = ac3_id++;
366  } else if (st->codecpar->codec_id == AV_CODEC_ID_DTS) {
367  stream->id = dts_id++;
368  } else if (st->codecpar->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->codecpar->sample_rate)
372  break;
373  }
374  if (j == 4) {
375  int sr;
376  av_log(ctx, AV_LOG_ERROR, "Invalid sampling rate for PCM stream.\n");
377  av_log(ctx, AV_LOG_INFO, "Allowed sampling rates:");
378  for (sr = 0; sr < 4; sr++)
379  av_log(ctx, AV_LOG_INFO, " %d", lpcm_freq_tab[sr]);
380  av_log(ctx, AV_LOG_INFO, "\n");
381  return AVERROR(EINVAL);
382  }
383  if (st->codecpar->channels > 8) {
384  av_log(ctx, AV_LOG_ERROR, "At most 8 channels allowed for LPCM streams.\n");
385  return AVERROR(EINVAL);
386  }
387  stream->lpcm_header[0] = 0x0c;
388  stream->lpcm_header[1] = (st->codecpar->channels - 1) | (j << 4);
389  stream->lpcm_header[2] = 0x80;
390  stream->lpcm_align = st->codecpar->channels * 2;
391  } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD) {
392  int freq;
393 
394  switch (st->codecpar->sample_rate) {
395  case 48000: freq = 0; break;
396  case 96000: freq = 1; break;
397  case 44100: freq = 2; break;
398  case 32000: freq = 3; break;
399  default:
400  av_log(ctx, AV_LOG_ERROR, "Unsupported sample rate.\n");
401  return AVERROR(EINVAL);
402  }
403 
404  stream->lpcm_header[0] = 0x0c;
405  stream->lpcm_header[1] = (freq << 4) |
406  (((st->codecpar->bits_per_coded_sample - 16) / 4) << 6) |
407  st->codecpar->channels - 1;
408  stream->lpcm_header[2] = 0x80;
409  stream->id = lpcm_id++;
410  stream->lpcm_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample / 8;
411  } else if (st->codecpar->codec_id == AV_CODEC_ID_MLP ||
413  av_log(ctx, AV_LOG_ERROR, "Support for muxing audio codec %s not implemented.\n",
415  return AVERROR_PATCHWELCOME;
416  } else if (st->codecpar->codec_id != AV_CODEC_ID_MP1 &&
419  av_log(ctx, AV_LOG_ERROR, "Unsupported audio codec. Must be one of mp1, mp2, mp3, 16-bit pcm_dvd, pcm_s16be, ac3 or dts.\n");
420  return AVERROR(EINVAL);
421  } else {
422  stream->id = mpa_id++;
423  }
424 
425  /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
426  * Right now it is also used for everything else. */
427  stream->max_buffer_size = 4 * 1024;
428  s->audio_bound++;
429  break;
430  case AVMEDIA_TYPE_VIDEO:
431  if (st->codecpar->codec_id == AV_CODEC_ID_H264)
432  stream->id = h264_id++;
433  else
434  stream->id = mpv_id++;
435 
437  if (props && props->buffer_size)
438  stream->max_buffer_size = 6 * 1024 + props->buffer_size / 8;
439  else {
441  "VBV buffer size not set, using default size of 230KB\n"
442  "If you want the mpeg file to be compliant to some specification\n"
443  "Like DVD, VCD or others, make sure you set the correct buffer size\n");
444  // FIXME: this is probably too small as default
445  stream->max_buffer_size = 230 * 1024;
446  }
447  if (stream->max_buffer_size > 1024 * 8191) {
448  av_log(ctx, AV_LOG_WARNING, "buffer size %d, too large\n", stream->max_buffer_size);
449  stream->max_buffer_size = 1024 * 8191;
450  }
451  s->video_bound++;
452  break;
454  stream->id = mps_id++;
455  stream->max_buffer_size = 16 * 1024;
456  break;
457  default:
458  av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
460  return AVERROR(EINVAL);
461  }
462  stream->fifo = av_fifo_alloc(16);
463  if (!stream->fifo)
464  return AVERROR(ENOMEM);
465  }
466  bitrate = 0;
467  audio_bitrate = 0;
468  video_bitrate = 0;
469  for (i = 0; i < ctx->nb_streams; i++) {
470  AVCPBProperties *props;
471  int codec_rate;
472  st = ctx->streams[i];
473  stream = (StreamInfo *)st->priv_data;
474 
476  if (props)
477  codec_rate = props->max_bitrate;
478  else
479  codec_rate = st->codecpar->bit_rate;
480 
481  if (!codec_rate)
482  codec_rate = (1 << 21) * 8 * 50 / ctx->nb_streams;
483 
484  bitrate += codec_rate;
485 
486  if ((stream->id & 0xe0) == AUDIO_ID)
487  audio_bitrate += codec_rate;
488  else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
489  video_bitrate += codec_rate;
490  }
491 
492  if (s->user_mux_rate) {
493  s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
494  } else {
495  /* we increase slightly the bitrate to take into account the
496  * headers. XXX: compute it exactly */
497  bitrate += bitrate / 20;
498  bitrate += 10000;
499  s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
500  if (s->mux_rate >= (1<<22)) {
501  av_log(ctx, AV_LOG_WARNING, "mux rate %d is too large\n", s->mux_rate);
502  s->mux_rate = (1<<22) - 1;
503  }
504  }
505 
506  if (s->is_vcd) {
507  int64_t overhead_rate;
508 
509  /* The VCD standard mandates that the mux_rate field is 3528
510  * (see standard p. IV-6).
511  * The value is actually "wrong", i.e. if you calculate
512  * it using the normal formula and the 75 sectors per second transfer
513  * rate you get a different value because the real pack size is 2324,
514  * not 2352. But the standard explicitly specifies that the mux_rate
515  * field in the header must have this value. */
516  // s->mux_rate = 2352 * 75 / 50; /* = 3528 */
517 
518  /* The VCD standard states that the muxed stream must be
519  * exactly 75 packs / second (the data rate of a single speed cdrom).
520  * Since the video bitrate (probably 1150000 bits/sec) will be below
521  * the theoretical maximum we have to add some padding packets
522  * to make up for the lower data rate.
523  * (cf. VCD standard p. IV-6 ) */
524 
525  /* Add the header overhead to the data rate.
526  * 2279 data bytes per audio pack, 2294 data bytes per video pack */
527  overhead_rate = audio_bitrate * 2294LL * (2324 - 2279);
528  overhead_rate += video_bitrate * 2279LL * (2324 - 2294);
529 
530  /* Add padding so that the full bitrate is 2324*75 bytes/sec */
531  s->vcd_padding_bitrate_num = (2324LL * 75 * 8 - bitrate) * 2279 * 2294 - overhead_rate;
532 #define VCD_PADDING_BITRATE_DEN (2279 * 2294)
533  }
534 
535  if (s->is_vcd || s->is_mpeg2)
536  /* every packet */
537  s->pack_header_freq = 1;
538  else
539  /* every 2 seconds */
540  s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
541 
542  /* the above seems to make pack_header_freq zero sometimes */
543  if (s->pack_header_freq == 0)
544  s->pack_header_freq = 1;
545 
546  if (s->is_mpeg2)
547  /* every 200 packets. Need to look at the spec. */
548  s->system_header_freq = s->pack_header_freq * 40;
549  else if (s->is_vcd)
550  /* the standard mandates that there are only two system headers
551  * in the whole file: one in the first packet of each stream.
552  * (see standard p. IV-7 and IV-8) */
553  s->system_header_freq = 0x7fffffff;
554  else
555  s->system_header_freq = s->pack_header_freq * 5;
556 
557  for (i = 0; i < ctx->nb_streams; i++) {
558  stream = ctx->streams[i]->priv_data;
559  stream->packet_number = 0;
560  }
561  s->system_header_size = get_system_header_size(ctx);
562  s->last_scr = AV_NOPTS_VALUE;
563  return 0;
564 }
565 
566 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
567 {
568  avio_w8(pb, (id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
569  avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
570  avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
571 }
572 
573 /* return the number of padding bytes that should be inserted into
574  * the multiplexed stream. */
576 {
578  int pad_bytes = 0;
579 
580  if (s->vcd_padding_bitrate_num > 0 && pts != AV_NOPTS_VALUE) {
581  int64_t full_pad_bytes;
582 
583  // FIXME: this is wrong
584  full_pad_bytes =
585  av_rescale(s->vcd_padding_bitrate_num, pts, 90000LL * 8 * VCD_PADDING_BITRATE_DEN);
586  pad_bytes = (int)(full_pad_bytes - s->vcd_padding_bytes_written);
587 
588  if (pad_bytes < 0)
589  /* might happen if we have already padded to a later timestamp. This
590  * can occur if another stream has already advanced further. */
591  pad_bytes = 0;
592  }
593 
594  return pad_bytes;
595 }
596 
597 /* Write an MPEG padding packet header. */
599  int packet_bytes)
600 {
602 
604  avio_wb16(pb, packet_bytes - 6);
605  if (!s->is_mpeg2) {
606  avio_w8(pb, 0x0f);
607  packet_bytes -= 7;
608  } else
609  packet_bytes -= 6;
610 
611  ffio_fill(pb, 0xff, packet_bytes);
612 }
613 
614 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
615 {
616  int nb_frames = 0;
617  PacketDesc *pkt_desc = stream->premux_packet;
618 
619  while (len > 0) {
620  if (pkt_desc->size == pkt_desc->unwritten_size)
621  nb_frames++;
622  len -= pkt_desc->unwritten_size;
623  pkt_desc = pkt_desc->next;
624  }
625 
626  return nb_frames;
627 }
628 
629 /* flush the packet on stream stream_index */
630 static int flush_packet(AVFormatContext *ctx, int stream_index,
631  int64_t pts, int64_t dts, int64_t scr, int trailer_size)
632 {
634  StreamInfo *stream = ctx->streams[stream_index]->priv_data;
635  uint8_t *buf_ptr;
636  int size, payload_size, startcode, id, stuffing_size, header_len;
637  int packet_size;
638  uint8_t buffer[128];
639  int zero_trail_bytes = 0;
640  int pad_packet_bytes = 0;
641  int pes_flags;
642  /* "general" pack without data specific to one stream? */
643  int general_pack = 0;
644  int nb_frames;
645 
646  id = stream->id;
647 
648  av_log(ctx, AV_LOG_TRACE, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
649 
650  buf_ptr = buffer;
651 
652  if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
653  /* output pack and systems header if needed */
654  size = put_pack_header(ctx, buf_ptr, scr);
655  buf_ptr += size;
656  s->last_scr = scr;
657 
658  if (s->is_vcd) {
659  /* there is exactly one system header for each stream in a VCD MPEG,
660  * One in the very first video packet and one in the very first
661  * audio packet (see VCD standard p. IV-7 and IV-8). */
662 
663  if (stream->packet_number == 0) {
664  size = put_system_header(ctx, buf_ptr, id);
665  buf_ptr += size;
666  }
667  } else if (s->is_dvd) {
668  if (stream->align_iframe || s->packet_number == 0) {
669  int PES_bytes_to_fill = s->packet_size - size - 10;
670 
671  if (pts != AV_NOPTS_VALUE) {
672  if (dts != pts)
673  PES_bytes_to_fill -= 5 + 5;
674  else
675  PES_bytes_to_fill -= 5;
676  }
677 
678  if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
679  size = put_system_header(ctx, buf_ptr, 0);
680  buf_ptr += size;
681  size = buf_ptr - buffer;
683 
685  avio_wb16(ctx->pb, 0x03d4); // length
686  avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI
687  ffio_fill(ctx->pb, 0x00, 979);
688 
690  avio_wb16(ctx->pb, 0x03fa); // length
691  avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI
692  ffio_fill(ctx->pb, 0x00, 1017);
693 
694  memset(buffer, 0, 128);
695  buf_ptr = buffer;
696  s->packet_number++;
697  stream->align_iframe = 0;
698  // FIXME: rounding and first few bytes of each packet
699  scr += s->packet_size * 90000LL /
700  (s->mux_rate * 50LL);
701  size = put_pack_header(ctx, buf_ptr, scr);
702  s->last_scr = scr;
703  buf_ptr += size;
704  /* GOP Start */
705  } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
706  pad_packet_bytes = PES_bytes_to_fill -
707  stream->bytes_to_iframe;
708  }
709  }
710  } else {
711  if ((s->packet_number % s->system_header_freq) == 0) {
712  size = put_system_header(ctx, buf_ptr, 0);
713  buf_ptr += size;
714  }
715  }
716  }
717  size = buf_ptr - buffer;
719 
720  packet_size = s->packet_size - size;
721 
722  if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
723  /* The VCD standard demands that 20 zero bytes follow
724  * each audio pack (see standard p. IV-8). */
725  zero_trail_bytes += 20;
726 
727  if ((s->is_vcd && stream->packet_number == 0) ||
728  (s->is_svcd && s->packet_number == 0)) {
729  /* for VCD the first pack of each stream contains only the pack header,
730  * the system header and lots of padding (see VCD standard p. IV-6).
731  * In the case of an audio pack, 20 zero bytes are also added at
732  * the end. */
733  /* For SVCD we fill the very first pack to increase compatibility with
734  * some DVD players. Not mandated by the standard. */
735  if (s->is_svcd)
736  /* the system header refers to both streams and no stream data */
737  general_pack = 1;
738  pad_packet_bytes = packet_size - zero_trail_bytes;
739  }
740 
741  packet_size -= pad_packet_bytes + zero_trail_bytes;
742 
743  if (packet_size > 0) {
744  /* packet header size */
745  packet_size -= 6;
746 
747  /* packet header */
748  if (s->is_mpeg2) {
749  header_len = 3;
750  if (stream->packet_number == 0)
751  header_len += 3; /* PES extension */
752  header_len += 1; /* obligatory stuffing byte */
753  } else {
754  header_len = 0;
755  }
756  if (pts != AV_NOPTS_VALUE) {
757  if (dts != pts)
758  header_len += 5 + 5;
759  else
760  header_len += 5;
761  } else {
762  if (!s->is_mpeg2)
763  header_len++;
764  }
765 
766  payload_size = packet_size - header_len;
767  if (id < 0xc0) {
768  startcode = PRIVATE_STREAM_1;
769  payload_size -= 1;
770  if (id >= 0x40) {
771  payload_size -= 3;
772  if (id >= 0xa0)
773  payload_size -= 3;
774  }
775  } else {
776  startcode = 0x100 + id;
777  }
778 
779  stuffing_size = payload_size - av_fifo_size(stream->fifo);
780 
781  // first byte does not fit -> reset pts/dts + stuffing
782  if (payload_size <= trailer_size && pts != AV_NOPTS_VALUE) {
783  int timestamp_len = 0;
784  if (dts != pts)
785  timestamp_len += 5;
786  if (pts != AV_NOPTS_VALUE)
787  timestamp_len += s->is_mpeg2 ? 5 : 4;
788  pts =
789  dts = AV_NOPTS_VALUE;
790  header_len -= timestamp_len;
791  if (s->is_dvd && stream->align_iframe) {
792  pad_packet_bytes += timestamp_len;
793  packet_size -= timestamp_len;
794  } else {
795  payload_size += timestamp_len;
796  }
797  stuffing_size += timestamp_len;
798  if (payload_size > trailer_size)
799  stuffing_size += payload_size - trailer_size;
800  }
801 
802  // can't use padding, so use stuffing
803  if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
804  packet_size += pad_packet_bytes;
805  payload_size += pad_packet_bytes; // undo the previous adjustment
806  if (stuffing_size < 0)
807  stuffing_size = pad_packet_bytes;
808  else
809  stuffing_size += pad_packet_bytes;
810  pad_packet_bytes = 0;
811  }
812 
813  if (stuffing_size < 0)
814  stuffing_size = 0;
815 
816  if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
817  if (payload_size < av_fifo_size(stream->fifo))
818  stuffing_size += payload_size % stream->lpcm_align;
819  }
820 
821  if (stuffing_size > 16) { /* <=16 for MPEG-1, <=32 for MPEG-2 */
822  pad_packet_bytes += stuffing_size;
823  packet_size -= stuffing_size;
824  payload_size -= stuffing_size;
825  stuffing_size = 0;
826  }
827 
828  nb_frames = get_nb_frames(ctx, stream, payload_size - stuffing_size);
829 
830  avio_wb32(ctx->pb, startcode);
831 
832  avio_wb16(ctx->pb, packet_size);
833 
834  if (!s->is_mpeg2)
835  ffio_fill(ctx->pb, 0xff, stuffing_size);
836 
837  if (s->is_mpeg2) {
838  avio_w8(ctx->pb, 0x80); /* mpeg2 id */
839 
840  pes_flags = 0;
841 
842  if (pts != AV_NOPTS_VALUE) {
843  pes_flags |= 0x80;
844  if (dts != pts)
845  pes_flags |= 0x40;
846  }
847 
848  /* Both the MPEG-2 and the SVCD standards demand that the
849  * P-STD_buffer_size field be included in the first packet of
850  * every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
851  * and MPEG-2 standard 2.7.7) */
852  if (stream->packet_number == 0)
853  pes_flags |= 0x01;
854 
855  avio_w8(ctx->pb, pes_flags); /* flags */
856  avio_w8(ctx->pb, header_len - 3 + stuffing_size);
857 
858  if (pes_flags & 0x80) /* write pts */
859  put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
860  if (pes_flags & 0x40) /* write dts */
861  put_timestamp(ctx->pb, 0x01, dts);
862 
863  if (pes_flags & 0x01) { /* write pes extension */
864  avio_w8(ctx->pb, 0x10); /* flags */
865 
866  /* P-STD buffer info */
867  if ((id & 0xe0) == AUDIO_ID)
868  avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size / 128);
869  else
870  avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size / 1024);
871  }
872  } else {
873  if (pts != AV_NOPTS_VALUE) {
874  if (dts != pts) {
875  put_timestamp(ctx->pb, 0x03, pts);
876  put_timestamp(ctx->pb, 0x01, dts);
877  } else {
878  put_timestamp(ctx->pb, 0x02, pts);
879  }
880  } else {
881  avio_w8(ctx->pb, 0x0f);
882  }
883  }
884 
885  if (s->is_mpeg2) {
886  /* special stuffing byte that is always written
887  * to prevent accidental generation of start codes. */
888  avio_w8(ctx->pb, 0xff);
889 
890  ffio_fill(ctx->pb, 0xff, stuffing_size);
891  }
892 
893  if (startcode == PRIVATE_STREAM_1) {
894  avio_w8(ctx->pb, id);
895  if (id >= 0xa0) {
896  /* LPCM (XXX: check nb_frames) */
897  avio_w8(ctx->pb, 7);
898  avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
899  avio_w8(ctx->pb, stream->lpcm_header[0]);
900  avio_w8(ctx->pb, stream->lpcm_header[1]);
901  avio_w8(ctx->pb, stream->lpcm_header[2]);
902  } else if (id >= 0x40) {
903  /* AC-3 */
904  avio_w8(ctx->pb, nb_frames);
905  avio_wb16(ctx->pb, trailer_size + 1);
906  }
907  }
908 
909  /* output data */
910  av_assert0(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
911  av_fifo_generic_read(stream->fifo, ctx->pb,
912  payload_size - stuffing_size,
913  (void (*)(void*, void*, int))avio_write);
914  stream->bytes_to_iframe -= payload_size - stuffing_size;
915  } else {
916  payload_size =
917  stuffing_size = 0;
918  }
919 
920  if (pad_packet_bytes > 0)
921  put_padding_packet(ctx, ctx->pb, pad_packet_bytes);
922 
923  ffio_fill(ctx->pb, 0x00, zero_trail_bytes);
924 
926 
927  s->packet_number++;
928 
929  /* only increase the stream packet number if this pack actually contains
930  * something that is specific to this stream! I.e. a dedicated header
931  * or some data. */
932  if (!general_pack)
933  stream->packet_number++;
934 
935  return payload_size - stuffing_size;
936 }
937 
939 {
940  /* There are two ways to do this padding: writing a sector/pack
941  * of 0 values, or writing an MPEG padding pack. Both seem to
942  * work with most decoders, BUT the VCD standard only allows a 0-sector
943  * (see standard p. IV-4, IV-5).
944  * So a 0-sector it is... */
945 
947 
948  ffio_fill(ctx->pb, 0, s->packet_size);
949 
950  s->vcd_padding_bytes_written += s->packet_size;
951 
953 
954  /* increasing the packet number is correct. The SCR of the following packs
955  * is calculated from the packet_number and it has to include the padding
956  * sector (it represents the sector index, not the MPEG pack index)
957  * (see VCD standard p. IV-6) */
958  s->packet_number++;
959 }
960 
961 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
962 {
963  int i;
964 
965  for (i = 0; i < ctx->nb_streams; i++) {
966  AVStream *st = ctx->streams[i];
967  StreamInfo *stream = st->priv_data;
968  PacketDesc *pkt_desc;
969 
970  while ((pkt_desc = stream->predecode_packet) &&
971  scr > pkt_desc->dts) { // FIXME: > vs >=
972  if (stream->buffer_index < pkt_desc->size ||
973  stream->predecode_packet == stream->premux_packet) {
975  "buffer underflow st=%d bufi=%d size=%d\n",
976  i, stream->buffer_index, pkt_desc->size);
977  break;
978  }
979  stream->buffer_index -= pkt_desc->size;
980  stream->predecode_packet = pkt_desc->next;
981  if (!stream->predecode_packet)
982  stream->last_packet = NULL;
983  av_freep(&pkt_desc);
984  }
985  }
986 
987  return 0;
988 }
989 
991 {
993  AVStream *st;
994  StreamInfo *stream;
995  int i, avail_space = 0, es_size, trailer_size;
996  int best_i = -1;
997  int best_score = INT_MIN;
998  int ignore_constraints = 0;
999  int ignore_delay = 0;
1000  int64_t scr = s->last_scr;
1001  PacketDesc *timestamp_packet;
1002  const int64_t max_delay = av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1003 
1004 retry:
1005  for (i = 0; i < ctx->nb_streams; i++) {
1006  AVStream *st = ctx->streams[i];
1007  StreamInfo *stream = st->priv_data;
1008  const int avail_data = av_fifo_size(stream->fifo);
1009  const int space = stream->max_buffer_size - stream->buffer_index;
1010  int rel_space = 1024LL * space / stream->max_buffer_size;
1011  PacketDesc *next_pkt = stream->premux_packet;
1012 
1013  /* for subtitle, a single PES packet must be generated,
1014  * so we flush after every single subtitle packet */
1015  if (s->packet_size > avail_data && !flush
1017  return 0;
1018  if (avail_data == 0)
1019  continue;
1020  av_assert0(avail_data > 0);
1021 
1022  if (space < s->packet_size && !ignore_constraints)
1023  continue;
1024 
1025  if (next_pkt && next_pkt->dts - scr > max_delay && !ignore_delay)
1026  continue;
1027  if ( stream->predecode_packet
1028  && stream->predecode_packet->size > stream->buffer_index)
1029  rel_space += 1<<28;
1030  if (rel_space > best_score) {
1031  best_score = rel_space;
1032  best_i = i;
1033  avail_space = space;
1034  }
1035  }
1036 
1037  if (best_i < 0) {
1038  int64_t best_dts = INT64_MAX;
1039  int has_premux = 0;
1040 
1041  for (i = 0; i < ctx->nb_streams; i++) {
1042  AVStream *st = ctx->streams[i];
1043  StreamInfo *stream = st->priv_data;
1044  PacketDesc *pkt_desc = stream->predecode_packet;
1045  if (pkt_desc && pkt_desc->dts < best_dts)
1046  best_dts = pkt_desc->dts;
1047  has_premux |= !!stream->premux_packet;
1048  }
1049 
1050  if (best_dts < INT64_MAX) {
1051  av_log(ctx, AV_LOG_TRACE, "bumping scr, scr:%f, dts:%f\n",
1052  scr / 90000.0, best_dts / 90000.0);
1053 
1054  if (scr >= best_dts + 1 && !ignore_constraints) {
1056  "packet too large, ignoring buffer limits to mux it\n");
1057  ignore_constraints = 1;
1058  }
1059  scr = FFMAX(best_dts + 1, scr);
1060  if (remove_decoded_packets(ctx, scr) < 0)
1061  return -1;
1062  } else if (has_premux && flush) {
1064  "delay too large, ignoring ...\n");
1065  ignore_delay = 1;
1066  ignore_constraints = 1;
1067  } else
1068  return 0;
1069 
1070  goto retry;
1071  }
1072 
1073  av_assert0(best_i >= 0);
1074 
1075  st = ctx->streams[best_i];
1076  stream = st->priv_data;
1077 
1078  av_assert0(av_fifo_size(stream->fifo) > 0);
1079 
1080  av_assert0(avail_space >= s->packet_size || ignore_constraints);
1081 
1082  timestamp_packet = stream->premux_packet;
1083  if (timestamp_packet->unwritten_size == timestamp_packet->size) {
1084  trailer_size = 0;
1085  } else {
1086  trailer_size = timestamp_packet->unwritten_size;
1087  timestamp_packet = timestamp_packet->next;
1088  }
1089 
1090  if (timestamp_packet) {
1091  av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f scr:%f stream:%d\n",
1092  timestamp_packet->dts / 90000.0,
1093  timestamp_packet->pts / 90000.0,
1094  scr / 90000.0, best_i);
1095  es_size = flush_packet(ctx, best_i, timestamp_packet->pts,
1096  timestamp_packet->dts, scr, trailer_size);
1097  } else {
1098  av_assert0(av_fifo_size(stream->fifo) == trailer_size);
1099  es_size = flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr,
1100  trailer_size);
1101  }
1102 
1103  if (s->is_vcd) {
1104  /* Write one or more padding sectors, if necessary, to reach
1105  * the constant overall bitrate. */
1106  int vcd_pad_bytes;
1107 
1108  // FIXME: pts cannot be correct here
1109  while ((vcd_pad_bytes = get_vcd_padding_size(ctx, stream->premux_packet->pts)) >= s->packet_size) {
1111  // FIXME: rounding and first few bytes of each packet
1112  s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1113  }
1114  }
1115 
1116  stream->buffer_index += es_size;
1117  // FIXME: rounding and first few bytes of each packet
1118  s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1119 
1120  while (stream->premux_packet &&
1121  stream->premux_packet->unwritten_size <= es_size) {
1122  es_size -= stream->premux_packet->unwritten_size;
1123  stream->premux_packet = stream->premux_packet->next;
1124  }
1125  if (es_size) {
1126  av_assert0(stream->premux_packet);
1127  stream->premux_packet->unwritten_size -= es_size;
1128  }
1129 
1130  if (remove_decoded_packets(ctx, s->last_scr) < 0)
1131  return -1;
1132 
1133  return 1;
1134 }
1135 
1137 {
1138  int stream_index = pkt->stream_index;
1139  int size = pkt->size;
1140  uint8_t *buf = pkt->data;
1142  AVStream *st = ctx->streams[stream_index];
1143  StreamInfo *stream = st->priv_data;
1144  int64_t pts, dts;
1145  PacketDesc *pkt_desc;
1146  int preload, ret;
1147  const int is_iframe = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1148  (pkt->flags & AV_PKT_FLAG_KEY);
1149 
1150  preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1151 
1152  pts = pkt->pts;
1153  dts = pkt->dts;
1154 
1155  if (s->last_scr == AV_NOPTS_VALUE) {
1156  if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {
1157  if (dts != AV_NOPTS_VALUE)
1158  s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);
1159  s->last_scr = 0;
1160  } else {
1161  s->last_scr = dts - preload;
1162  s->preload = 0;
1163  }
1164  preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1165  av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);
1166  }
1167 
1168  if (dts != AV_NOPTS_VALUE) dts += preload;
1169  if (pts != AV_NOPTS_VALUE) pts += preload;
1170 
1171  av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1172  dts / 90000.0, pts / 90000.0, pkt->flags,
1174 
1175  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD) {
1176  if (size < 3) {
1177  av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n", size);
1178  return AVERROR(EINVAL);
1179  }
1180 
1181  /* Skip first 3 bytes of packet data, which comprise PCM header
1182  and will be written fresh by this muxer. */
1183  buf += 3;
1184  size -= 3;
1185  }
1186 
1187  pkt_desc = av_mallocz(sizeof(PacketDesc));
1188  if (!pkt_desc)
1189  return AVERROR(ENOMEM);
1190  if (!stream->predecode_packet) {
1191  stream->predecode_packet = pkt_desc;
1192  } else
1193  stream->last_packet->next = pkt_desc;
1194  stream->last_packet = pkt_desc;
1195  if (!stream->premux_packet)
1196  stream->premux_packet = pkt_desc;
1197  pkt_desc->pts = pts;
1198  pkt_desc->dts = dts;
1199  pkt_desc->unwritten_size =
1200  pkt_desc->size = size;
1201 
1202  ret = av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size);
1203  if (ret < 0)
1204  return ret;
1205 
1206  if (s->is_dvd) {
1207  // min VOBU length 0.4 seconds (mpucoder)
1208  if (is_iframe &&
1209  (s->packet_number == 0 || pts != AV_NOPTS_VALUE &&
1210  (pts - stream->vobu_start_pts >= 36000))) {
1211  stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1212  stream->align_iframe = 1;
1213  stream->vobu_start_pts = pts;
1214  }
1215  }
1216 
1217  av_fifo_generic_write(stream->fifo, buf, size, NULL);
1218 
1219  for (;;) {
1220  int ret = output_packet(ctx, 0);
1221  if (ret <= 0)
1222  return ret;
1223  }
1224 }
1225 
1227 {
1228  StreamInfo *stream;
1229  int i;
1230 
1231  for (;;) {
1232  int ret = output_packet(ctx, 1);
1233  if (ret < 0)
1234  return ret;
1235  else if (ret == 0)
1236  break;
1237  }
1238 
1239  /* End header according to MPEG-1 systems standard. We do not write
1240  * it as it is usually not needed by decoders and because it
1241  * complicates MPEG stream concatenation. */
1242  // avio_wb32(ctx->pb, ISO_11172_END_CODE);
1243 
1244  for (i = 0; i < ctx->nb_streams; i++) {
1245  stream = ctx->streams[i]->priv_data;
1246 
1247  av_assert0(av_fifo_size(stream->fifo) == 0);
1248  }
1249  return 0;
1250 }
1251 
1253 {
1254  for (int i = 0; i < ctx->nb_streams; i++) {
1255  StreamInfo *stream = ctx->streams[i]->priv_data;
1256  if (!stream)
1257  continue;
1258  for (PacketDesc *pkt = stream->predecode_packet; pkt; ) {
1259  PacketDesc *tmp = pkt->next;
1260  av_free(pkt);
1261  pkt = tmp;
1262  }
1263  av_fifo_freep(&stream->fifo);
1264  }
1265 }
1266 
1267 #define OFFSET(x) offsetof(MpegMuxContext, x)
1268 #define E AV_OPT_FLAG_ENCODING_PARAM
1269 static const AVOption options[] = {
1270  { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, ((1<<22) - 1) * (8 * 50), E },
1271  { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX, E },
1272  { NULL },
1273 };
1274 
1275 static const AVClass mpeg_class = {
1276  .class_name = "mpeg/(s)vcd/vob/dvd muxer",
1277  .item_name = av_default_item_name,
1278  .version = LIBAVUTIL_VERSION_INT,
1279  .option = options,
1280 };
1281 
1282 #if CONFIG_MPEG1SYSTEM_MUXER
1284  .name = "mpeg",
1285  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
1286  .mime_type = "video/mpeg",
1287  .extensions = "mpg,mpeg",
1288  .priv_data_size = sizeof(MpegMuxContext),
1289  .audio_codec = AV_CODEC_ID_MP2,
1290  .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1295  .priv_class = &mpeg_class,
1296 };
1297 #endif
1298 
1299 #if CONFIG_MPEG1VCD_MUXER
1301  .name = "vcd",
1302  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
1303  .mime_type = "video/mpeg",
1304  .priv_data_size = sizeof(MpegMuxContext),
1305  .audio_codec = AV_CODEC_ID_MP2,
1306  .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1311  .priv_class = &mpeg_class,
1312 };
1313 #endif
1314 
1315 #if CONFIG_MPEG2VOB_MUXER
1317  .name = "vob",
1318  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
1319  .mime_type = "video/mpeg",
1320  .extensions = "vob",
1321  .priv_data_size = sizeof(MpegMuxContext),
1322  .audio_codec = AV_CODEC_ID_MP2,
1323  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1328  .priv_class = &mpeg_class,
1329 };
1330 #endif
1331 
1332 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1333 #if CONFIG_MPEG2SVCD_MUXER
1335  .name = "svcd",
1336  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
1337  .mime_type = "video/mpeg",
1338  .extensions = "vob",
1339  .priv_data_size = sizeof(MpegMuxContext),
1340  .audio_codec = AV_CODEC_ID_MP2,
1341  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1346  .priv_class = &mpeg_class,
1347 };
1348 #endif
1349 
1350 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1351 #if CONFIG_MPEG2DVD_MUXER
1353  .name = "dvd",
1354  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
1355  .mime_type = "video/mpeg",
1356  .extensions = "dvd",
1357  .priv_data_size = sizeof(MpegMuxContext),
1358  .audio_codec = AV_CODEC_ID_MP2,
1359  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1364  .priv_class = &mpeg_class,
1365 };
1366 #endif
PacketDesc::pts
int64_t pts
Definition: mpegenc.c:40
StreamInfo::vobu_start_pts
int64_t vobu_start_pts
Definition: mpegenc.c:60
StreamInfo::last_packet
PacketDesc * last_packet
Definition: mpegenc.c:53
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
StreamInfo::lpcm_align
int lpcm_align
Definition: mpegenc.c:57
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:426
SYSTEM_HEADER_START_CODE
#define SYSTEM_HEADER_START_CODE
Definition: mpeg.h:29
AVOutputFormat::name
const char * name
Definition: avformat.h:504
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
StreamInfo::align_iframe
int align_iframe
Definition: mpegenc.c:59
put_bits32
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:290
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
space
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated space
Definition: undefined.txt:4
MpegMuxContext::last_scr
int64_t last_scr
Definition: mpegenc.c:79
get_system_header_size
static int get_system_header_size(AVFormatContext *ctx)
Definition: mpegenc.c:275
VCD_PADDING_BITRATE_DEN
#define VCD_PADDING_BITRATE_DEN
av_fifo_generic_write
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:122
AVStream::priv_data
void * priv_data
Definition: avformat.h:951
ff_mpeg1vcd_muxer
const AVOutputFormat ff_mpeg1vcd_muxer
MpegMuxContext::preload
int preload
Definition: mpegenc.c:84
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
mpeg_mux_init
static av_cold int mpeg_mux_init(AVFormatContext *ctx)
Definition: mpegenc.c:298
remove_decoded_packets
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
Definition: mpegenc.c:961
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1268
deinit
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:50
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
flush_packet
static int flush_packet(AVFormatContext *ctx, int stream_index, int64_t pts, int64_t dts, int64_t scr, int trailer_size)
Definition: mpegenc.c:630
AUDIO_ID
#define AUDIO_ID
Definition: mpeg.h:41
StreamInfo::lpcm_header
uint8_t lpcm_header[3]
Definition: mpegenc.c:56
mathematics.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
av_fifo_generic_read
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:213
StreamInfo::fifo
AVFifoBuffer * fifo
Definition: mpegenc.c:48
get_nb_frames
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
Definition: mpegenc.c:614
PacketDesc::next
struct PacketDesc * next
Definition: mpegenc.c:44
MpegMuxContext::system_header_freq
int system_header_freq
Definition: mpegenc.c:68
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:467
StreamInfo::packet_number
int packet_number
Definition: mpegenc.c:55
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:428
MpegMuxContext
Definition: mpegenc.c:63
AVFifoBuffer
Definition: fifo.h:31
avio_write_marker
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:490
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
fifo.h
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:315
MpegMuxContext::is_dvd
int is_dvd
Definition: mpegenc.c:78
output_packet
static int output_packet(AVFormatContext *ctx, int flush)
Definition: mpegenc.c:990
ff_mpeg2svcd_muxer
const AVOutputFormat ff_mpeg2svcd_muxer
ff_mpeg1system_muxer
const AVOutputFormat ff_mpeg1system_muxer
MpegMuxContext::audio_bound
int audio_bound
Definition: mpegenc.c:73
pts
static int64_t pts
Definition: transcode_aac.c:653
PacketDesc
Definition: mpegenc.c:39
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:424
StreamInfo::max_buffer_size
int max_buffer_size
Definition: mpegenc.c:50
StreamInfo::id
uint8_t id
Definition: mpegenc.c:49
get_vcd_padding_size
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
Definition: mpegenc.c:575
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
StreamInfo::predecode_packet
PacketDesc * predecode_packet
Definition: mpegenc.c:52
s
#define s(width, name)
Definition: cbs_vp9.c:257
MpegMuxContext::is_mpeg2
int is_mpeg2
Definition: mpegenc.c:75
H264_ID
#define H264_ID
Definition: mpeg.h:43
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:423
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
lpcm_freq_tab
static const int lpcm_freq_tab[4]
Definition: mpeg.h:63
MpegMuxContext::mux_rate
int mux_rate
Definition: mpegenc.c:71
MpegMuxContext::user_mux_rate
int user_mux_rate
Definition: mpegenc.c:70
AV_CODEC_ID_PCM_DVD
@ AV_CODEC_ID_PCM_DVD
Definition: codec_id.h:333
AVCPBProperties
This structure describes the bitrate properties of an encoded bitstream.
Definition: defs.h:104
av_fifo_realloc2
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
PutBitContext
Definition: put_bits.h:49
MpegMuxContext::is_svcd
int is_svcd
Definition: mpegenc.c:77
PacketDesc::size
int size
Definition: mpegenc.c:42
MpegMuxContext::pack_header_freq
int pack_header_freq
Definition: mpegenc.c:67
if
if(ret)
Definition: filter_design.txt:179
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
PutBitContext::buf
uint8_t * buf
Definition: put_bits.h:52
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
AC3_ID
#define AC3_ID
Definition: mpeg.h:44
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1242
MpegMuxContext::system_header_size
int system_header_size
Definition: mpegenc.c:69
LPCM_ID
#define LPCM_ID
Definition: mpeg.h:46
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:210
StreamInfo::bytes_to_iframe
int bytes_to_iframe
Definition: mpegenc.c:58
put_padding_packet
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb, int packet_bytes)
Definition: mpegenc.c:598
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:218
PACK_START_CODE
#define PACK_START_CODE
Definition: mpeg.h:28
put_timestamp
static void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
Definition: mpegenc.c:566
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:51
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1256
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
StreamInfo
Definition: mpegenc.c:47
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:325
PRIVATE_STREAM_1
#define PRIVATE_STREAM_1
Definition: mpeg.h:37
put_vcd_padding_sector
static void put_vcd_padding_sector(AVFormatContext *ctx)
Definition: mpegenc.c:938
AV_CODEC_ID_DTS
@ AV_CODEC_ID_DTS
Definition: codec_id.h:427
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
mpeg_class
static const AVClass mpeg_class
Definition: mpegenc.c:1275
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:372
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:394
mpeg.h
attributes.h
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
bitrate
int64_t bitrate
Definition: h264_levels.c:131
StreamInfo::buffer_index
int buffer_index
Definition: mpegenc.c:51
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:727
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:443
E
#define E
Definition: mpegenc.c:1268
PRIVATE_STREAM_2
#define PRIVATE_STREAM_2
Definition: mpeg.h:39
StreamInfo::premux_packet
PacketDesc * premux_packet
Definition: mpegenc.c:54
log.h
AVOutputFormat
Definition: avformat.h:503
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
avio_internal.h
mpeg_mux_end
static int mpeg_mux_end(AVFormatContext *ctx)
Definition: mpegenc.c:1226
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
AVCPBProperties::max_bitrate
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: defs.h:109
AVFormatContext::max_delay
int max_delay
Definition: avformat.h:1312
DTS_ID
#define DTS_ID
Definition: mpeg.h:45
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
AV_PKT_DATA_CPB_PROPERTIES
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
Definition: packet.h:145
len
int len
Definition: vorbis_enc_data.h:426
PacketDesc::dts
int64_t dts
Definition: mpegenc.c:41
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:128
MpegMuxContext::vcd_padding_bytes_written
int64_t vcd_padding_bytes_written
Definition: mpegenc.c:82
MpegMuxContext::is_vcd
int is_vcd
Definition: mpegenc.c:76
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
AVFormatContext::oformat
const struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1219
AVCPBProperties::buffer_size
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: defs.h:125
avformat.h
av_stream_get_side_data
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, size_t *size)
Get side information from stream.
Definition: utils.c:1722
VIDEO_ID
#define VIDEO_ID
Definition: mpeg.h:42
ff_mpeg2vob_muxer
const AVOutputFormat ff_mpeg2vob_muxer
av_get_media_type_string
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:71
MpegMuxContext::video_bound
int video_bound
Definition: mpegenc.c:74
put_bits_ptr
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:369
PADDING_STREAM
#define PADDING_STREAM
Definition: mpeg.h:38
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
OFFSET
#define OFFSET(x)
Definition: mpegenc.c:1267
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
SUB_ID
#define SUB_ID
Definition: mpeg.h:47
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:1196
AVPacket::stream_index
int stream_index
Definition: packet.h:375
AVFormatContext::packet_size
unsigned int packet_size
Definition: avformat.h:1311
MpegMuxContext::packet_size
int packet_size
Definition: mpegenc.c:65
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
put_system_header
static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int only_for_stream_id)
Definition: mpegenc.c:126
av_fifo_size
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:77
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:142
MpegMuxContext::vcd_padding_bitrate_num
int64_t vcd_padding_bitrate_num
Definition: mpegenc.c:81
av_fifo_freep
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
Definition: fifo.c:63
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_fifo_alloc
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:472
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
MpegMuxContext::packet_number
int packet_number
Definition: mpegenc.c:66
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:347
int
int
Definition: ffmpeg_filter.c:153
put_bits.h
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:52
options
static const AVOption options[]
Definition: mpegenc.c:1269
AV_CODEC_ID_MP1
@ AV_CODEC_ID_MP1
Definition: codec_id.h:465
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1228
mpeg_mux_deinit
static void mpeg_mux_deinit(AVFormatContext *ctx)
Definition: mpegenc.c:1252
mpeg_mux_write_packet
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: mpegenc.c:1136
ff_mpeg2dvd_muxer
const AVOutputFormat ff_mpeg2dvd_muxer
AV_CODEC_ID_MLP
@ AV_CODEC_ID_MLP
Definition: codec_id.h:452
PacketDesc::unwritten_size
int unwritten_size
Definition: mpegenc.c:43
put_pack_header
static int put_pack_header(AVFormatContext *ctx, uint8_t *buf, int64_t timestamp)
Definition: mpegenc.c:92
AVIO_DATA_MARKER_FLUSH_POINT
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition: avio.h:146