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