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