FFmpeg  4.4.9
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 {
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 */
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 
125 static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int buf_size,
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, buf_size);
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;
309  s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_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 
466  /* The system header is emitted, right after the pack header (which is at
467  * most 14 bytes), into the fixed 128-byte buffer used by flush_packet().
468  * Reject configurations whose system header would not fit. */
469  if (get_system_header_size(ctx) > 128 - 14) {
471  "Too many streams to fit the MPEG program stream system header\n");
472  return AVERROR(EINVAL);
473  }
474 
475  bitrate = 0;
476  audio_bitrate = 0;
477  video_bitrate = 0;
478  for (i = 0; i < ctx->nb_streams; i++) {
479  AVCPBProperties *props;
480  int codec_rate;
481  st = ctx->streams[i];
482  stream = (StreamInfo *)st->priv_data;
483 
485  if (props)
486  codec_rate = props->max_bitrate;
487  else
488  codec_rate = st->codecpar->bit_rate;
489 
490  if (!codec_rate)
491  codec_rate = (1 << 21) * 8 * 50 / ctx->nb_streams;
492 
493  bitrate += codec_rate;
494 
495  if ((stream->id & 0xe0) == AUDIO_ID)
496  audio_bitrate += codec_rate;
497  else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
498  video_bitrate += codec_rate;
499  }
500 
501  if (s->user_mux_rate) {
502  s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
503  } else {
504  /* we increase slightly the bitrate to take into account the
505  * headers. XXX: compute it exactly */
506  bitrate += bitrate / 20;
507  bitrate += 10000;
508  s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
509  if (s->mux_rate >= (1<<22)) {
510  av_log(ctx, AV_LOG_WARNING, "mux rate %d is too large\n", s->mux_rate);
511  s->mux_rate = (1<<22) - 1;
512  }
513  }
514 
515  if (s->is_vcd) {
516  int64_t overhead_rate;
517 
518  /* The VCD standard mandates that the mux_rate field is 3528
519  * (see standard p. IV-6).
520  * The value is actually "wrong", i.e. if you calculate
521  * it using the normal formula and the 75 sectors per second transfer
522  * rate you get a different value because the real pack size is 2324,
523  * not 2352. But the standard explicitly specifies that the mux_rate
524  * field in the header must have this value. */
525  // s->mux_rate = 2352 * 75 / 50; /* = 3528 */
526 
527  /* The VCD standard states that the muxed stream must be
528  * exactly 75 packs / second (the data rate of a single speed cdrom).
529  * Since the video bitrate (probably 1150000 bits/sec) will be below
530  * the theoretical maximum we have to add some padding packets
531  * to make up for the lower data rate.
532  * (cf. VCD standard p. IV-6 ) */
533 
534  /* Add the header overhead to the data rate.
535  * 2279 data bytes per audio pack, 2294 data bytes per video pack */
536  overhead_rate = audio_bitrate * 2294LL * (2324 - 2279);
537  overhead_rate += video_bitrate * 2279LL * (2324 - 2294);
538 
539  /* Add padding so that the full bitrate is 2324*75 bytes/sec */
540  s->vcd_padding_bitrate_num = (2324LL * 75 * 8 - bitrate) * 2279 * 2294 - overhead_rate;
541 #define VCD_PADDING_BITRATE_DEN (2279 * 2294)
542  }
543 
544  if (s->is_vcd || s->is_mpeg2)
545  /* every packet */
546  s->pack_header_freq = 1;
547  else
548  /* every 2 seconds */
549  s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
550 
551  /* the above seems to make pack_header_freq zero sometimes */
552  if (s->pack_header_freq == 0)
553  s->pack_header_freq = 1;
554 
555  if (s->is_mpeg2)
556  /* every 200 packets. Need to look at the spec. */
557  s->system_header_freq = s->pack_header_freq * 40;
558  else if (s->is_vcd)
559  /* the standard mandates that there are only two system headers
560  * in the whole file: one in the first packet of each stream.
561  * (see standard p. IV-7 and IV-8) */
562  s->system_header_freq = 0x7fffffff;
563  else
564  s->system_header_freq = s->pack_header_freq * 5;
565 
566  for (i = 0; i < ctx->nb_streams; i++) {
567  stream = ctx->streams[i]->priv_data;
568  stream->packet_number = 0;
569  }
570  s->system_header_size = get_system_header_size(ctx);
571  s->last_scr = AV_NOPTS_VALUE;
572  return 0;
573 }
574 
575 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
576 {
577  avio_w8(pb, (id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
578  avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
579  avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
580 }
581 
582 /* return the number of padding bytes that should be inserted into
583  * the multiplexed stream. */
585 {
587  int pad_bytes = 0;
588 
589  if (s->vcd_padding_bitrate_num > 0 && pts != AV_NOPTS_VALUE) {
590  int64_t full_pad_bytes;
591 
592  // FIXME: this is wrong
593  full_pad_bytes =
594  av_rescale(s->vcd_padding_bitrate_num, pts, 90000LL * 8 * VCD_PADDING_BITRATE_DEN);
595  pad_bytes = (int)(full_pad_bytes - s->vcd_padding_bytes_written);
596 
597  if (pad_bytes < 0)
598  /* might happen if we have already padded to a later timestamp. This
599  * can occur if another stream has already advanced further. */
600  pad_bytes = 0;
601  }
602 
603  return pad_bytes;
604 }
605 
606 /* Write an MPEG padding packet header. */
608  int packet_bytes)
609 {
611  int i;
612 
614  avio_wb16(pb, packet_bytes - 6);
615  if (!s->is_mpeg2) {
616  avio_w8(pb, 0x0f);
617  packet_bytes -= 7;
618  } else
619  packet_bytes -= 6;
620 
621  for (i = 0; i < packet_bytes; i++)
622  avio_w8(pb, 0xff);
623 }
624 
625 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
626 {
627  int nb_frames = 0;
628  PacketDesc *pkt_desc = stream->premux_packet;
629 
630  while (len > 0) {
631  if (pkt_desc->size == pkt_desc->unwritten_size)
632  nb_frames++;
633  len -= pkt_desc->unwritten_size;
634  pkt_desc = pkt_desc->next;
635  }
636 
637  return nb_frames;
638 }
639 
640 /* flush the packet on stream stream_index */
641 static int flush_packet(AVFormatContext *ctx, int stream_index,
642  int64_t pts, int64_t dts, int64_t scr, int trailer_size)
643 {
645  StreamInfo *stream = ctx->streams[stream_index]->priv_data;
646  uint8_t *buf_ptr;
647  int size, payload_size, startcode, id, stuffing_size, i, header_len;
648  int packet_size;
649  uint8_t buffer[128];
650  uint8_t *buf_end = buffer + sizeof(buffer);
651  int zero_trail_bytes = 0;
652  int pad_packet_bytes = 0;
653  int pes_flags;
654  /* "general" pack without data specific to one stream? */
655  int general_pack = 0;
656  int nb_frames;
657 
658  id = stream->id;
659 
660  av_log(ctx, AV_LOG_TRACE, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
661 
662  buf_ptr = buffer;
663 
664  if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
665  /* output pack and systems header if needed */
666  size = put_pack_header(ctx, buf_ptr, scr);
667  buf_ptr += size;
668  s->last_scr = scr;
669 
670  if (s->is_vcd) {
671  /* there is exactly one system header for each stream in a VCD MPEG,
672  * One in the very first video packet and one in the very first
673  * audio packet (see VCD standard p. IV-7 and IV-8). */
674 
675  if (stream->packet_number == 0) {
676  size = put_system_header(ctx, buf_ptr, buf_end - buf_ptr, id);
677  buf_ptr += size;
678  }
679  } else if (s->is_dvd) {
680  if (stream->align_iframe || s->packet_number == 0) {
681  int PES_bytes_to_fill = s->packet_size - size - 10;
682 
683  if (pts != AV_NOPTS_VALUE) {
684  if (dts != pts)
685  PES_bytes_to_fill -= 5 + 5;
686  else
687  PES_bytes_to_fill -= 5;
688  }
689 
690  if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
691  size = put_system_header(ctx, buf_ptr, buf_end - buf_ptr, 0);
692  buf_ptr += size;
693  size = buf_ptr - buffer;
695 
697  avio_wb16(ctx->pb, 0x03d4); // length
698  avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI
699  for (i = 0; i < 979; i++)
700  avio_w8(ctx->pb, 0x00);
701 
703  avio_wb16(ctx->pb, 0x03fa); // length
704  avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI
705  for (i = 0; i < 1017; i++)
706  avio_w8(ctx->pb, 0x00);
707 
708  memset(buffer, 0, 128);
709  buf_ptr = buffer;
710  s->packet_number++;
711  stream->align_iframe = 0;
712  // FIXME: rounding and first few bytes of each packet
713  scr += s->packet_size * 90000LL /
714  (s->mux_rate * 50LL);
715  size = put_pack_header(ctx, buf_ptr, scr);
716  s->last_scr = scr;
717  buf_ptr += size;
718  /* GOP Start */
719  } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
720  pad_packet_bytes = PES_bytes_to_fill -
721  stream->bytes_to_iframe;
722  }
723  }
724  } else {
725  if ((s->packet_number % s->system_header_freq) == 0) {
726  size = put_system_header(ctx, buf_ptr, buf_end - buf_ptr, 0);
727  buf_ptr += size;
728  }
729  }
730  }
731  size = buf_ptr - buffer;
733 
734  packet_size = s->packet_size - size;
735 
736  if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
737  /* The VCD standard demands that 20 zero bytes follow
738  * each audio pack (see standard p. IV-8). */
739  zero_trail_bytes += 20;
740 
741  if ((s->is_vcd && stream->packet_number == 0) ||
742  (s->is_svcd && s->packet_number == 0)) {
743  /* for VCD the first pack of each stream contains only the pack header,
744  * the system header and lots of padding (see VCD standard p. IV-6).
745  * In the case of an audio pack, 20 zero bytes are also added at
746  * the end. */
747  /* For SVCD we fill the very first pack to increase compatibility with
748  * some DVD players. Not mandated by the standard. */
749  if (s->is_svcd)
750  /* the system header refers to both streams and no stream data */
751  general_pack = 1;
752  pad_packet_bytes = packet_size - zero_trail_bytes;
753  }
754 
755  packet_size -= pad_packet_bytes + zero_trail_bytes;
756 
757  if (packet_size > 0) {
758  /* packet header size */
759  packet_size -= 6;
760 
761  /* packet header */
762  if (s->is_mpeg2) {
763  header_len = 3;
764  if (stream->packet_number == 0)
765  header_len += 3; /* PES extension */
766  header_len += 1; /* obligatory stuffing byte */
767  } else {
768  header_len = 0;
769  }
770  if (pts != AV_NOPTS_VALUE) {
771  if (dts != pts)
772  header_len += 5 + 5;
773  else
774  header_len += 5;
775  } else {
776  if (!s->is_mpeg2)
777  header_len++;
778  }
779 
780  payload_size = packet_size - header_len;
781  if (id < 0xc0) {
782  startcode = PRIVATE_STREAM_1;
783  payload_size -= 1;
784  if (id >= 0x40) {
785  payload_size -= 3;
786  if (id >= 0xa0)
787  payload_size -= 3;
788  }
789  } else {
790  startcode = 0x100 + id;
791  }
792 
793  stuffing_size = payload_size - av_fifo_size(stream->fifo);
794 
795  // first byte does not fit -> reset pts/dts + stuffing
796  if (payload_size <= trailer_size && pts != AV_NOPTS_VALUE) {
797  int timestamp_len = 0;
798  if (dts != pts)
799  timestamp_len += 5;
800  if (pts != AV_NOPTS_VALUE)
801  timestamp_len += s->is_mpeg2 ? 5 : 4;
802  pts =
803  dts = AV_NOPTS_VALUE;
804  header_len -= timestamp_len;
805  if (s->is_dvd && stream->align_iframe) {
806  pad_packet_bytes += timestamp_len;
807  packet_size -= timestamp_len;
808  } else {
809  payload_size += timestamp_len;
810  }
811  stuffing_size += timestamp_len;
812  if (payload_size > trailer_size)
813  stuffing_size += payload_size - trailer_size;
814  }
815 
816  // can't use padding, so use stuffing
817  if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
818  packet_size += pad_packet_bytes;
819  payload_size += pad_packet_bytes; // undo the previous adjustment
820  if (stuffing_size < 0)
821  stuffing_size = pad_packet_bytes;
822  else
823  stuffing_size += pad_packet_bytes;
824  pad_packet_bytes = 0;
825  }
826 
827  if (stuffing_size < 0)
828  stuffing_size = 0;
829 
830  if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
831  if (payload_size < av_fifo_size(stream->fifo))
832  stuffing_size += payload_size % stream->lpcm_align;
833  }
834 
835  if (stuffing_size > 16) { /* <=16 for MPEG-1, <=32 for MPEG-2 */
836  pad_packet_bytes += stuffing_size;
837  packet_size -= stuffing_size;
838  payload_size -= stuffing_size;
839  stuffing_size = 0;
840  }
841 
842  nb_frames = get_nb_frames(ctx, stream, payload_size - stuffing_size);
843 
844  avio_wb32(ctx->pb, startcode);
845 
846  avio_wb16(ctx->pb, packet_size);
847 
848  if (!s->is_mpeg2)
849  for (i = 0; i < stuffing_size; i++)
850  avio_w8(ctx->pb, 0xff);
851 
852  if (s->is_mpeg2) {
853  avio_w8(ctx->pb, 0x80); /* mpeg2 id */
854 
855  pes_flags = 0;
856 
857  if (pts != AV_NOPTS_VALUE) {
858  pes_flags |= 0x80;
859  if (dts != pts)
860  pes_flags |= 0x40;
861  }
862 
863  /* Both the MPEG-2 and the SVCD standards demand that the
864  * P-STD_buffer_size field be included in the first packet of
865  * every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
866  * and MPEG-2 standard 2.7.7) */
867  if (stream->packet_number == 0)
868  pes_flags |= 0x01;
869 
870  avio_w8(ctx->pb, pes_flags); /* flags */
871  avio_w8(ctx->pb, header_len - 3 + stuffing_size);
872 
873  if (pes_flags & 0x80) /* write pts */
874  put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
875  if (pes_flags & 0x40) /* write dts */
876  put_timestamp(ctx->pb, 0x01, dts);
877 
878  if (pes_flags & 0x01) { /* write pes extension */
879  avio_w8(ctx->pb, 0x10); /* flags */
880 
881  /* P-STD buffer info */
882  if ((id & 0xe0) == AUDIO_ID)
883  avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size / 128);
884  else
885  avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size / 1024);
886  }
887  } else {
888  if (pts != AV_NOPTS_VALUE) {
889  if (dts != pts) {
890  put_timestamp(ctx->pb, 0x03, pts);
891  put_timestamp(ctx->pb, 0x01, dts);
892  } else {
893  put_timestamp(ctx->pb, 0x02, pts);
894  }
895  } else {
896  avio_w8(ctx->pb, 0x0f);
897  }
898  }
899 
900  if (s->is_mpeg2) {
901  /* special stuffing byte that is always written
902  * to prevent accidental generation of start codes. */
903  avio_w8(ctx->pb, 0xff);
904 
905  for (i = 0; i < stuffing_size; i++)
906  avio_w8(ctx->pb, 0xff);
907  }
908 
909  if (startcode == PRIVATE_STREAM_1) {
910  avio_w8(ctx->pb, id);
911  if (id >= 0xa0) {
912  /* LPCM (XXX: check nb_frames) */
913  avio_w8(ctx->pb, 7);
914  avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
915  avio_w8(ctx->pb, stream->lpcm_header[0]);
916  avio_w8(ctx->pb, stream->lpcm_header[1]);
917  avio_w8(ctx->pb, stream->lpcm_header[2]);
918  } else if (id >= 0x40) {
919  /* AC-3 */
920  avio_w8(ctx->pb, nb_frames);
921  avio_wb16(ctx->pb, trailer_size + 1);
922  }
923  }
924 
925  /* output data */
926  av_assert0(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
927  av_fifo_generic_read(stream->fifo, ctx->pb,
928  payload_size - stuffing_size,
929  (void (*)(void*, void*, int))avio_write);
930  stream->bytes_to_iframe -= payload_size - stuffing_size;
931  } else {
932  payload_size =
933  stuffing_size = 0;
934  }
935 
936  if (pad_packet_bytes > 0)
937  put_padding_packet(ctx, ctx->pb, pad_packet_bytes);
938 
939  for (i = 0; i < zero_trail_bytes; i++)
940  avio_w8(ctx->pb, 0x00);
941 
943 
944  s->packet_number++;
945 
946  /* only increase the stream packet number if this pack actually contains
947  * something that is specific to this stream! I.e. a dedicated header
948  * or some data. */
949  if (!general_pack)
950  stream->packet_number++;
951 
952  return payload_size - stuffing_size;
953 }
954 
956 {
957  /* There are two ways to do this padding: writing a sector/pack
958  * of 0 values, or writing an MPEG padding pack. Both seem to
959  * work with most decoders, BUT the VCD standard only allows a 0-sector
960  * (see standard p. IV-4, IV-5).
961  * So a 0-sector it is... */
962 
964  int i;
965 
966  for (i = 0; i < s->packet_size; i++)
967  avio_w8(ctx->pb, 0);
968 
969  s->vcd_padding_bytes_written += s->packet_size;
970 
972 
973  /* increasing the packet number is correct. The SCR of the following packs
974  * is calculated from the packet_number and it has to include the padding
975  * sector (it represents the sector index, not the MPEG pack index)
976  * (see VCD standard p. IV-6) */
977  s->packet_number++;
978 }
979 
981 {
982  int i;
983 
984  for (i = 0; i < ctx->nb_streams; i++) {
985  AVStream *st = ctx->streams[i];
986  StreamInfo *stream = st->priv_data;
987  PacketDesc *pkt_desc;
988 
989  while ((pkt_desc = stream->predecode_packet) &&
990  scr > pkt_desc->dts) { // FIXME: > vs >=
991  if (stream->buffer_index < pkt_desc->size ||
992  stream->predecode_packet == stream->premux_packet) {
994  "buffer underflow st=%d bufi=%d size=%d\n",
995  i, stream->buffer_index, pkt_desc->size);
996  break;
997  }
998  stream->buffer_index -= pkt_desc->size;
999  stream->predecode_packet = pkt_desc->next;
1000  if (!stream->predecode_packet)
1001  stream->last_packet = NULL;
1002  av_freep(&pkt_desc);
1003  }
1004  }
1005 
1006  return 0;
1007 }
1008 
1010 {
1012  AVStream *st;
1013  StreamInfo *stream;
1014  int i, avail_space = 0, es_size, trailer_size;
1015  int best_i = -1;
1016  int best_score = INT_MIN;
1017  int ignore_constraints = 0;
1018  int ignore_delay = 0;
1019  int64_t scr = s->last_scr;
1020  PacketDesc *timestamp_packet;
1021  const int64_t max_delay = av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1022 
1023 retry:
1024  for (i = 0; i < ctx->nb_streams; i++) {
1025  AVStream *st = ctx->streams[i];
1026  StreamInfo *stream = st->priv_data;
1027  const int avail_data = av_fifo_size(stream->fifo);
1028  const int space = stream->max_buffer_size - stream->buffer_index;
1029  int rel_space = 1024LL * space / stream->max_buffer_size;
1030  PacketDesc *next_pkt = stream->premux_packet;
1031 
1032  /* for subtitle, a single PES packet must be generated,
1033  * so we flush after every single subtitle packet */
1034  if (s->packet_size > avail_data && !flush
1036  return 0;
1037  if (avail_data == 0)
1038  continue;
1039  av_assert0(avail_data > 0);
1040 
1041  if (space < s->packet_size && !ignore_constraints)
1042  continue;
1043 
1044  if (next_pkt && next_pkt->dts - scr > max_delay && !ignore_delay)
1045  continue;
1046  if ( stream->predecode_packet
1047  && stream->predecode_packet->size > stream->buffer_index)
1048  rel_space += 1<<28;
1049  if (rel_space > best_score) {
1050  best_score = rel_space;
1051  best_i = i;
1052  avail_space = space;
1053  }
1054  }
1055 
1056  if (best_i < 0) {
1057  int64_t best_dts = INT64_MAX;
1058  int has_premux = 0;
1059 
1060  for (i = 0; i < ctx->nb_streams; i++) {
1061  AVStream *st = ctx->streams[i];
1062  StreamInfo *stream = st->priv_data;
1063  PacketDesc *pkt_desc = stream->predecode_packet;
1064  if (pkt_desc && pkt_desc->dts < best_dts)
1065  best_dts = pkt_desc->dts;
1066  has_premux |= !!stream->premux_packet;
1067  }
1068 
1069  if (best_dts < INT64_MAX) {
1070  av_log(ctx, AV_LOG_TRACE, "bumping scr, scr:%f, dts:%f\n",
1071  scr / 90000.0, best_dts / 90000.0);
1072 
1073  if (scr >= best_dts + 1 && !ignore_constraints) {
1075  "packet too large, ignoring buffer limits to mux it\n");
1076  ignore_constraints = 1;
1077  }
1078  scr = FFMAX(best_dts + 1, scr);
1079  if (remove_decoded_packets(ctx, scr) < 0)
1080  return -1;
1081  } else if (has_premux && flush) {
1083  "delay too large, ignoring ...\n");
1084  ignore_delay = 1;
1085  ignore_constraints = 1;
1086  } else
1087  return 0;
1088 
1089  goto retry;
1090  }
1091 
1092  av_assert0(best_i >= 0);
1093 
1094  st = ctx->streams[best_i];
1095  stream = st->priv_data;
1096 
1097  av_assert0(av_fifo_size(stream->fifo) > 0);
1098 
1099  av_assert0(avail_space >= s->packet_size || ignore_constraints);
1100 
1101  timestamp_packet = stream->premux_packet;
1102  if (timestamp_packet->unwritten_size == timestamp_packet->size) {
1103  trailer_size = 0;
1104  } else {
1105  trailer_size = timestamp_packet->unwritten_size;
1106  timestamp_packet = timestamp_packet->next;
1107  }
1108 
1109  if (timestamp_packet) {
1110  av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f scr:%f stream:%d\n",
1111  timestamp_packet->dts / 90000.0,
1112  timestamp_packet->pts / 90000.0,
1113  scr / 90000.0, best_i);
1114  es_size = flush_packet(ctx, best_i, timestamp_packet->pts,
1115  timestamp_packet->dts, scr, trailer_size);
1116  } else {
1117  av_assert0(av_fifo_size(stream->fifo) == trailer_size);
1118  es_size = flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr,
1119  trailer_size);
1120  }
1121 
1122  if (s->is_vcd) {
1123  /* Write one or more padding sectors, if necessary, to reach
1124  * the constant overall bitrate. */
1125  int vcd_pad_bytes;
1126 
1127  // FIXME: pts cannot be correct here
1128  while ((vcd_pad_bytes = get_vcd_padding_size(ctx, stream->premux_packet->pts)) >= s->packet_size) {
1130  // FIXME: rounding and first few bytes of each packet
1131  s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1132  }
1133  }
1134 
1135  stream->buffer_index += es_size;
1136  // FIXME: rounding and first few bytes of each packet
1137  s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1138 
1139  while (stream->premux_packet &&
1140  stream->premux_packet->unwritten_size <= es_size) {
1141  es_size -= stream->premux_packet->unwritten_size;
1142  stream->premux_packet = stream->premux_packet->next;
1143  }
1144  if (es_size) {
1145  av_assert0(stream->premux_packet);
1146  stream->premux_packet->unwritten_size -= es_size;
1147  }
1148 
1149  if (remove_decoded_packets(ctx, s->last_scr) < 0)
1150  return -1;
1151 
1152  return 1;
1153 }
1154 
1156 {
1157  int stream_index = pkt->stream_index;
1158  int size = pkt->size;
1159  uint8_t *buf = pkt->data;
1161  AVStream *st = ctx->streams[stream_index];
1162  StreamInfo *stream = st->priv_data;
1163  int64_t pts, dts;
1164  PacketDesc *pkt_desc;
1165  int preload, ret;
1166  const int is_iframe = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1167  (pkt->flags & AV_PKT_FLAG_KEY);
1168 
1169  preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1170 
1171  pts = pkt->pts;
1172  dts = pkt->dts;
1173 
1174  if (s->last_scr == AV_NOPTS_VALUE) {
1175  if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {
1176  if (dts != AV_NOPTS_VALUE)
1177  s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);
1178  s->last_scr = 0;
1179  } else {
1180  s->last_scr = dts - preload;
1181  s->preload = 0;
1182  }
1183  preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1184  av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);
1185  }
1186 
1187  if (dts != AV_NOPTS_VALUE) dts += preload;
1188  if (pts != AV_NOPTS_VALUE) pts += preload;
1189 
1190  av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1191  dts / 90000.0, pts / 90000.0, pkt->flags,
1193 
1194  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD) {
1195  if (size < 3) {
1196  av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n", size);
1197  return AVERROR(EINVAL);
1198  }
1199 
1200  /* Skip first 3 bytes of packet data, which comprise PCM header
1201  and will be written fresh by this muxer. */
1202  buf += 3;
1203  size -= 3;
1204  }
1205 
1206  pkt_desc = av_mallocz(sizeof(PacketDesc));
1207  if (!pkt_desc)
1208  return AVERROR(ENOMEM);
1209  if (!stream->predecode_packet) {
1210  stream->predecode_packet = pkt_desc;
1211  } else
1212  stream->last_packet->next = pkt_desc;
1213  stream->last_packet = pkt_desc;
1214  if (!stream->premux_packet)
1215  stream->premux_packet = pkt_desc;
1216  pkt_desc->pts = pts;
1217  pkt_desc->dts = dts;
1218  pkt_desc->unwritten_size =
1219  pkt_desc->size = size;
1220 
1221  ret = av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size);
1222  if (ret < 0)
1223  return ret;
1224 
1225  if (s->is_dvd) {
1226  // min VOBU length 0.4 seconds (mpucoder)
1227  if (is_iframe &&
1228  (s->packet_number == 0 || pts != AV_NOPTS_VALUE &&
1229  (pts - stream->vobu_start_pts >= 36000))) {
1230  stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1231  stream->align_iframe = 1;
1232  stream->vobu_start_pts = pts;
1233  }
1234  }
1235 
1236  av_fifo_generic_write(stream->fifo, buf, size, NULL);
1237 
1238  for (;;) {
1239  int ret = output_packet(ctx, 0);
1240  if (ret <= 0)
1241  return ret;
1242  }
1243 }
1244 
1246 {
1247  StreamInfo *stream;
1248  int i;
1249 
1250  for (;;) {
1251  int ret = output_packet(ctx, 1);
1252  if (ret < 0)
1253  return ret;
1254  else if (ret == 0)
1255  break;
1256  }
1257 
1258  /* End header according to MPEG-1 systems standard. We do not write
1259  * it as it is usually not needed by decoders and because it
1260  * complicates MPEG stream concatenation. */
1261  // avio_wb32(ctx->pb, ISO_11172_END_CODE);
1262 
1263  for (i = 0; i < ctx->nb_streams; i++) {
1264  stream = ctx->streams[i]->priv_data;
1265 
1266  av_assert0(av_fifo_size(stream->fifo) == 0);
1267  }
1268  return 0;
1269 }
1270 
1272 {
1273  for (int i = 0; i < ctx->nb_streams; i++) {
1274  StreamInfo *stream = ctx->streams[i]->priv_data;
1275  if (!stream)
1276  continue;
1277  for (PacketDesc *pkt = stream->predecode_packet; pkt; ) {
1278  PacketDesc *tmp = pkt->next;
1279  av_free(pkt);
1280  pkt = tmp;
1281  }
1282  av_fifo_freep(&stream->fifo);
1283  }
1284 }
1285 
1286 #define OFFSET(x) offsetof(MpegMuxContext, x)
1287 #define E AV_OPT_FLAG_ENCODING_PARAM
1288 static const AVOption options[] = {
1289  { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, ((1<<22) - 1) * (8 * 50), E },
1290  { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX, E },
1291  { NULL },
1292 };
1293 
1294 #define MPEGENC_CLASS(flavor) \
1295 static const AVClass flavor ## _class = { \
1296  .class_name = #flavor " muxer", \
1297  .item_name = av_default_item_name, \
1298  .version = LIBAVUTIL_VERSION_INT, \
1299  .option = options, \
1300 };
1301 
1302 #if CONFIG_MPEG1SYSTEM_MUXER
1303 MPEGENC_CLASS(mpeg)
1305  .name = "mpeg",
1306  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
1307  .mime_type = "video/mpeg",
1308  .extensions = "mpg,mpeg",
1309  .priv_data_size = sizeof(MpegMuxContext),
1310  .audio_codec = AV_CODEC_ID_MP2,
1311  .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1316  .priv_class = &mpeg_class,
1317 };
1318 #endif
1319 
1320 #if CONFIG_MPEG1VCD_MUXER
1321 MPEGENC_CLASS(vcd)
1323  .name = "vcd",
1324  .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
1325  .mime_type = "video/mpeg",
1326  .priv_data_size = sizeof(MpegMuxContext),
1327  .audio_codec = AV_CODEC_ID_MP2,
1328  .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1333  .priv_class = &vcd_class,
1334 };
1335 #endif
1336 
1337 #if CONFIG_MPEG2VOB_MUXER
1338 MPEGENC_CLASS(vob)
1340  .name = "vob",
1341  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
1342  .mime_type = "video/mpeg",
1343  .extensions = "vob",
1344  .priv_data_size = sizeof(MpegMuxContext),
1345  .audio_codec = AV_CODEC_ID_MP2,
1346  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1351  .priv_class = &vob_class,
1352 };
1353 #endif
1354 
1355 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1356 #if CONFIG_MPEG2SVCD_MUXER
1357 MPEGENC_CLASS(svcd)
1359  .name = "svcd",
1360  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
1361  .mime_type = "video/mpeg",
1362  .extensions = "vob",
1363  .priv_data_size = sizeof(MpegMuxContext),
1364  .audio_codec = AV_CODEC_ID_MP2,
1365  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1370  .priv_class = &svcd_class,
1371 };
1372 #endif
1373 
1374 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1375 #if CONFIG_MPEG2DVD_MUXER
1376 MPEGENC_CLASS(dvd)
1378  .name = "dvd",
1379  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
1380  .mime_type = "video/mpeg",
1381  .extensions = "dvd",
1382  .priv_data_size = sizeof(MpegMuxContext),
1383  .audio_codec = AV_CODEC_ID_MP2,
1384  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1389  .priv_class = &dvd_class,
1390 };
1391 #endif
static void flush(AVCodecContext *avctx)
AVOutputFormat ff_mpeg1system_muxer
Macro definitions for various function/variable attributes.
#define av_cold
Definition: attributes.h:88
uint8_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Main libavformat public API header.
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:486
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:208
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:390
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:468
@ 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
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:230
#define s(width, name)
Definition: cbs_vp9.c:257
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:50
#define FFMAX(a, b)
Definition: common.h:103
#define CONFIG_MPEG1VCD_MUXER
Definition: config.h:2576
#define CONFIG_MPEG2VOB_MUXER
Definition: config.h:2581
#define CONFIG_MPEG2DVD_MUXER
Definition: config.h:2578
#define CONFIG_MPEG2SVCD_MUXER
Definition: config.h:2579
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
enum AVCodecID id
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:740
int
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
a very simple circular buffer FIFO implementation
@ AV_OPT_TYPE_INT
Definition: opt.h:225
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:497
@ AV_CODEC_ID_PCM_DVD
Definition: codec_id.h:332
@ AV_CODEC_ID_H264
Definition: codec_id.h:76
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:314
@ AV_CODEC_ID_MP1
Definition: codec_id.h:466
@ AV_CODEC_ID_MP2
Definition: codec_id.h:424
@ AV_CODEC_ID_DTS
Definition: codec_id.h:428
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:468
@ AV_CODEC_ID_AC3
Definition: codec_id.h:427
@ AV_CODEC_ID_MLP
Definition: codec_id.h:453
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:425
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:50
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
Definition: packet.h:145
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, size_t *size)
Get side information from stream.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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
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
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
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
int i
Definition: input.c:407
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
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:4944
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
Definition: fifo.c:63
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
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
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
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
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define AUDIO_ID
Definition: mpeg.h:41
#define LPCM_ID
Definition: mpeg.h:46
#define AC3_ID
Definition: mpeg.h:44
#define SYSTEM_HEADER_START_CODE
Definition: mpeg.h:29
static const int lpcm_freq_tab[4]
Definition: mpeg.h:63
#define PRIVATE_STREAM_2
Definition: mpeg.h:39
#define PRIVATE_STREAM_1
Definition: mpeg.h:37
#define H264_ID
Definition: mpeg.h:43
#define SUB_ID
Definition: mpeg.h:47
#define PADDING_STREAM
Definition: mpeg.h:38
#define DTS_ID
Definition: mpeg.h:45
#define VIDEO_ID
Definition: mpeg.h:42
#define PACK_START_CODE
Definition: mpeg.h:28
AVOutputFormat ff_mpeg1vcd_muxer
#define E
Definition: mpegenc.c:1287
static int get_system_header_size(AVFormatContext *ctx)
Definition: mpegenc.c:274
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb, int packet_bytes)
Definition: mpegenc.c:607
#define MPEGENC_CLASS(flavor)
Definition: mpegenc.c:1294
static const AVOption options[]
Definition: mpegenc.c:1288
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
Definition: mpegenc.c:625
static int mpeg_mux_end(AVFormatContext *ctx)
Definition: mpegenc.c:1245
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
Definition: mpegenc.c:980
AVOutputFormat ff_mpeg2vob_muxer
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
Definition: mpegenc.c:584
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: mpegenc.c:1155
AVOutputFormat ff_mpeg2svcd_muxer
static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int buf_size, int only_for_stream_id)
Definition: mpegenc.c:125
static av_cold int mpeg_mux_init(AVFormatContext *ctx)
Definition: mpegenc.c:297
static int output_packet(AVFormatContext *ctx, int flush)
Definition: mpegenc.c:1009
static void mpeg_mux_deinit(AVFormatContext *ctx)
Definition: mpegenc.c:1271
AVOutputFormat ff_mpeg2dvd_muxer
#define VCD_PADDING_BITRATE_DEN
static void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
Definition: mpegenc.c:575
#define OFFSET(x)
Definition: mpegenc.c:1286
static int put_pack_header(AVFormatContext *ctx, uint8_t *buf, int64_t timestamp)
Definition: mpegenc.c:91
static int flush_packet(AVFormatContext *ctx, int stream_index, int64_t pts, int64_t dts, int64_t scr, int trailer_size)
Definition: mpegenc.c:641
static void put_vcd_padding_sector(AVFormatContext *ctx)
Definition: mpegenc.c:955
AVOptions.
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:263
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:342
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
typedef void(RENAME(mix_any_func_type))
static char buffer[20]
Definition: seek.c:32
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:453
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:486
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: avcodec.h:459
Describe the class of an AVClass context structure.
Definition: log.h:67
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
unsigned int packet_size
Definition: avformat.h:1356
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1288
AVIOContext * pb
I/O context.
Definition: avformat.h:1274
ff_const59 struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1251
void * priv_data
Format private data.
Definition: avformat.h:1260
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1300
Bytestream IO Context.
Definition: avio.h:161
AVOption.
Definition: opt.h:248
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
void * priv_data
Definition: avformat.h:888
int audio_bound
Definition: mpegenc.c:72
int is_mpeg2
Definition: mpegenc.c:74
int64_t last_scr
Definition: mpegenc.c:78
int packet_number
Definition: mpegenc.c:65
int system_header_size
Definition: mpegenc.c:68
int64_t vcd_padding_bytes_written
Definition: mpegenc.c:81
int64_t vcd_padding_bitrate_num
Definition: mpegenc.c:80
int mux_rate
Definition: mpegenc.c:70
int video_bound
Definition: mpegenc.c:73
int system_header_freq
Definition: mpegenc.c:67
int pack_header_freq
Definition: mpegenc.c:66
int packet_size
Definition: mpegenc.c:64
int user_mux_rate
Definition: mpegenc.c:69
int unwritten_size
Definition: mpegenc.c:42
struct PacketDesc * next
Definition: mpegenc.c:43
int64_t pts
Definition: mpegenc.c:39
int size
Definition: mpegenc.c:41
int64_t dts
Definition: mpegenc.c:40
uint8_t * buf
Definition: put_bits.h:47
int64_t vobu_start_pts
Definition: mpegenc.c:59
uint8_t lpcm_header[3]
Definition: mpegenc.c:55
PacketDesc * premux_packet
Definition: mpegenc.c:53
int buffer_index
Definition: mpegenc.c:50
uint8_t id
Definition: mpegenc.c:48
int lpcm_align
Definition: mpegenc.c:56
int max_buffer_size
Definition: mpegenc.c:49
int align_iframe
Definition: mpegenc.c:58
AVFifoBuffer * fifo
Definition: mpegenc.c:47
PacketDesc * last_packet
Definition: mpegenc.c:52
PacketDesc * predecode_packet
Definition: mpegenc.c:51
int packet_number
Definition: mpegenc.c:54
int bytes_to_iframe
Definition: mpegenc.c:57
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
int64_t bitrate
Definition: h264_levels.c:131
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
static int64_t pts
int size
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
if(ret< 0)
Definition: vf_mcdeint.c:282
int len