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