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