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