mpegtsenc.c
Go to the documentation of this file.
1 /*
2  * MPEG2 transport stream (aka DVB) muxer
3  * Copyright (c) 2003 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 "libavutil/bswap.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mathematics.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "libavcodec/internal.h"
30 #include "avformat.h"
31 #include "internal.h"
32 #include "mpegts.h"
33 
34 #define PCR_TIME_BASE 27000000
35 
36 /* write DVB SI sections */
37 
38 /*********************************************/
39 /* mpegts section writer */
40 
41 typedef struct MpegTSSection {
42  int pid;
43  int cc;
44  void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
45  void *opaque;
47 
48 typedef struct MpegTSService {
49  MpegTSSection pmt; /* MPEG2 pmt table context */
50  int sid; /* service ID */
51  char *name;
53  int pcr_pid;
57 
58 typedef struct MpegTSWrite {
59  const AVClass *av_class;
60  MpegTSSection pat; /* MPEG2 pat table */
61  MpegTSSection sdt; /* MPEG2 sdt table context */
68  int onid;
69  int tsid;
70  int64_t first_pcr;
71  int mux_rate; ///< set to 1 when VBR
73 
77 
79  int start_pid;
80  int m2ts_mode;
81 
82  int reemit_pat_pmt; // backward compatibility
83 
84 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
85 #define MPEGTS_FLAG_AAC_LATM 0x02
86  int flags;
87  int copyts;
88 } MpegTSWrite;
89 
90 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
91 #define DEFAULT_PES_HEADER_FREQ 16
92 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
93 
94 static const AVOption options[] = {
95  { "mpegts_transport_stream_id", "Set transport_stream_id field.",
96  offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
97  { "mpegts_original_network_id", "Set original_network_id field.",
98  offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
99  { "mpegts_service_id", "Set service_id field.",
100  offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
101  { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
102  offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT, {.i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM},
103  { "mpegts_start_pid", "Set the first pid.",
104  offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT, {.i64 = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM},
105  {"mpegts_m2ts_mode", "Enable m2ts mode.",
106  offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_INT, {.i64 = -1 },
108  { "muxrate", NULL, offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
109  { "pes_payload_size", "Minimum PES packet payload in bytes",
110  offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT, {.i64 = DEFAULT_PES_PAYLOAD_SIZE}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
111  { "mpegts_flags", "MPEG-TS muxing flags", offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX,
112  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
113  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
114  0, AV_OPT_TYPE_CONST, {.i64 = MPEGTS_FLAG_REEMIT_PAT_PMT}, 0, INT_MAX,
115  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags"},
116  { "latm", "Use LATM packetization for AAC",
117  0, AV_OPT_TYPE_CONST, {.i64 = MPEGTS_FLAG_AAC_LATM}, 0, INT_MAX,
118  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags"},
119  // backward compatibility
120  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
121  offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
122  { "mpegts_copyts", "dont offset dts/pts",
123  offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
124  { NULL },
125 };
126 
127 static const AVClass mpegts_muxer_class = {
128  .class_name = "MPEGTS muxer",
129  .item_name = av_default_item_name,
130  .option = options,
131  .version = LIBAVUTIL_VERSION_INT,
132 };
133 
134 /* NOTE: 4 bytes must be left at the end for the crc32 */
136 {
137  unsigned int crc;
138  unsigned char packet[TS_PACKET_SIZE];
139  const unsigned char *buf_ptr;
140  unsigned char *q;
141  int first, b, len1, left;
142 
143  crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
144  buf[len - 4] = (crc >> 24) & 0xff;
145  buf[len - 3] = (crc >> 16) & 0xff;
146  buf[len - 2] = (crc >> 8) & 0xff;
147  buf[len - 1] = (crc) & 0xff;
148 
149  /* send each packet */
150  buf_ptr = buf;
151  while (len > 0) {
152  first = (buf == buf_ptr);
153  q = packet;
154  *q++ = 0x47;
155  b = (s->pid >> 8);
156  if (first)
157  b |= 0x40;
158  *q++ = b;
159  *q++ = s->pid;
160  s->cc = (s->cc + 1) & 0xf;
161  *q++ = 0x10 | s->cc;
162  if (first)
163  *q++ = 0; /* 0 offset */
164  len1 = TS_PACKET_SIZE - (q - packet);
165  if (len1 > len)
166  len1 = len;
167  memcpy(q, buf_ptr, len1);
168  q += len1;
169  /* add known padding data */
170  left = TS_PACKET_SIZE - (q - packet);
171  if (left > 0)
172  memset(q, 0xff, left);
173 
174  s->write_packet(s, packet);
175 
176  buf_ptr += len1;
177  len -= len1;
178  }
179 }
180 
181 static inline void put16(uint8_t **q_ptr, int val)
182 {
183  uint8_t *q;
184  q = *q_ptr;
185  *q++ = val >> 8;
186  *q++ = val;
187  *q_ptr = q;
188 }
189 
190 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
191  int version, int sec_num, int last_sec_num,
192  uint8_t *buf, int len)
193 {
194  uint8_t section[1024], *q;
195  unsigned int tot_len;
196  /* reserved_future_use field must be set to 1 for SDT */
197  unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
198 
199  tot_len = 3 + 5 + len + 4;
200  /* check if not too big */
201  if (tot_len > 1024)
202  return AVERROR_INVALIDDATA;
203 
204  q = section;
205  *q++ = tid;
206  put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
207  put16(&q, id);
208  *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
209  *q++ = sec_num;
210  *q++ = last_sec_num;
211  memcpy(q, buf, len);
212 
213  mpegts_write_section(s, section, tot_len);
214  return 0;
215 }
216 
217 /*********************************************/
218 /* mpegts writer */
219 
220 #define DEFAULT_PROVIDER_NAME "FFmpeg"
221 #define DEFAULT_SERVICE_NAME "Service01"
222 
223 /* we retransmit the SI info at this rate */
224 #define SDT_RETRANS_TIME 500
225 #define PAT_RETRANS_TIME 100
226 #define PCR_RETRANS_TIME 20
227 
228 typedef struct MpegTSWriteStream {
230  int pid; /* stream associated pid */
231  int cc;
233  int first_pts_check; ///< first pts check needed
235  int64_t payload_pts;
236  int64_t payload_dts;
241 
243 {
244  MpegTSWrite *ts = s->priv_data;
245  MpegTSService *service;
246  uint8_t data[1012], *q;
247  int i;
248 
249  q = data;
250  for(i = 0; i < ts->nb_services; i++) {
251  service = ts->services[i];
252  put16(&q, service->sid);
253  put16(&q, 0xe000 | service->pmt.pid);
254  }
255  mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
256  data, q - data);
257 }
258 
260 {
261  MpegTSWrite *ts = s->priv_data;
262  uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
263  int val, stream_type, i;
264 
265  q = data;
266  put16(&q, 0xe000 | service->pcr_pid);
267 
268  program_info_length_ptr = q;
269  q += 2; /* patched after */
270 
271  /* put program info here */
272 
273  val = 0xf000 | (q - program_info_length_ptr - 2);
274  program_info_length_ptr[0] = val >> 8;
275  program_info_length_ptr[1] = val;
276 
277  for(i = 0; i < s->nb_streams; i++) {
278  AVStream *st = s->streams[i];
279  MpegTSWriteStream *ts_st = st->priv_data;
280  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
281  switch(st->codec->codec_id) {
284  stream_type = STREAM_TYPE_VIDEO_MPEG2;
285  break;
286  case AV_CODEC_ID_MPEG4:
287  stream_type = STREAM_TYPE_VIDEO_MPEG4;
288  break;
289  case AV_CODEC_ID_H264:
290  stream_type = STREAM_TYPE_VIDEO_H264;
291  break;
292  case AV_CODEC_ID_CAVS:
293  stream_type = STREAM_TYPE_VIDEO_CAVS;
294  break;
295  case AV_CODEC_ID_DIRAC:
296  stream_type = STREAM_TYPE_VIDEO_DIRAC;
297  break;
298  case AV_CODEC_ID_MP2:
299  case AV_CODEC_ID_MP3:
300  stream_type = STREAM_TYPE_AUDIO_MPEG1;
301  break;
302  case AV_CODEC_ID_AAC:
304  break;
306  stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
307  break;
308  case AV_CODEC_ID_AC3:
309  stream_type = STREAM_TYPE_AUDIO_AC3;
310  break;
311  default:
312  stream_type = STREAM_TYPE_PRIVATE_DATA;
313  break;
314  }
315  *q++ = stream_type;
316  put16(&q, 0xe000 | ts_st->pid);
317  desc_length_ptr = q;
318  q += 2; /* patched after */
319 
320  /* write optional descriptors here */
321  switch(st->codec->codec_type) {
322  case AVMEDIA_TYPE_AUDIO:
323  if(st->codec->codec_id==AV_CODEC_ID_EAC3){
324  *q++=0x7a; // EAC3 descriptor see A038 DVB SI
325  *q++=1; // 1 byte, all flags sets to 0
326  *q++=0; // omit all fields...
327  }
328  if(st->codec->codec_id==AV_CODEC_ID_S302M){
329  *q++ = 0x05; /* MPEG-2 registration descriptor*/
330  *q++ = 4;
331  *q++ = 'B';
332  *q++ = 'S';
333  *q++ = 'S';
334  *q++ = 'D';
335  }
336 
337  if (lang) {
338  char *p;
339  char *next = lang->value;
340  uint8_t *len_ptr;
341 
342  *q++ = 0x0a; /* ISO 639 language descriptor */
343  len_ptr = q++;
344  *len_ptr = 0;
345 
346  for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
347  next = strchr(p, ',');
348  if (strlen(p) != 3 && (!next || next != p + 3))
349  continue; /* not a 3-letter code */
350 
351  *q++ = *p++;
352  *q++ = *p++;
353  *q++ = *p++;
354 
356  *q++ = 0x01;
358  *q++ = 0x02;
360  *q++ = 0x03;
361  else
362  *q++ = 0; /* undefined type */
363 
364  *len_ptr += 4;
365  }
366 
367  if (*len_ptr == 0)
368  q -= 2; /* no language codes were written */
369  }
370  break;
372  {
373  const char *language;
374  language = lang && strlen(lang->value)==3 ? lang->value : "eng";
375  *q++ = 0x59;
376  *q++ = 8;
377  *q++ = language[0];
378  *q++ = language[1];
379  *q++ = language[2];
380  *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
381  if(st->codec->extradata_size == 4) {
382  memcpy(q, st->codec->extradata, 4);
383  q += 4;
384  } else {
385  put16(&q, 1); /* page id */
386  put16(&q, 1); /* ancillary page id */
387  }
388  }
389  break;
390  case AVMEDIA_TYPE_VIDEO:
391  if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
392  *q++ = 0x05; /*MPEG-2 registration descriptor*/
393  *q++ = 4;
394  *q++ = 'd';
395  *q++ = 'r';
396  *q++ = 'a';
397  *q++ = 'c';
398  }
399  break;
400  }
401 
402  val = 0xf000 | (q - desc_length_ptr - 2);
403  desc_length_ptr[0] = val >> 8;
404  desc_length_ptr[1] = val;
405  }
406  mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
407  data, q - data);
408 }
409 
410 /* NOTE: str == NULL is accepted for an empty string */
411 static void putstr8(uint8_t **q_ptr, const char *str)
412 {
413  uint8_t *q;
414  int len;
415 
416  q = *q_ptr;
417  if (!str)
418  len = 0;
419  else
420  len = strlen(str);
421  *q++ = len;
422  memcpy(q, str, len);
423  q += len;
424  *q_ptr = q;
425 }
426 
428 {
429  MpegTSWrite *ts = s->priv_data;
430  MpegTSService *service;
431  uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
432  int i, running_status, free_ca_mode, val;
433 
434  q = data;
435  put16(&q, ts->onid);
436  *q++ = 0xff;
437  for(i = 0; i < ts->nb_services; i++) {
438  service = ts->services[i];
439  put16(&q, service->sid);
440  *q++ = 0xfc | 0x00; /* currently no EIT info */
441  desc_list_len_ptr = q;
442  q += 2;
443  running_status = 4; /* running */
444  free_ca_mode = 0;
445 
446  /* write only one descriptor for the service name and provider */
447  *q++ = 0x48;
448  desc_len_ptr = q;
449  q++;
450  *q++ = 0x01; /* digital television service */
451  putstr8(&q, service->provider_name);
452  putstr8(&q, service->name);
453  desc_len_ptr[0] = q - desc_len_ptr - 1;
454 
455  /* fill descriptor length */
456  val = (running_status << 13) | (free_ca_mode << 12) |
457  (q - desc_list_len_ptr - 2);
458  desc_list_len_ptr[0] = val >> 8;
459  desc_list_len_ptr[1] = val;
460  }
461  mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
462  data, q - data);
463 }
464 
466  int sid,
467  const char *provider_name,
468  const char *name)
469 {
470  MpegTSService *service;
471 
472  service = av_mallocz(sizeof(MpegTSService));
473  if (!service)
474  return NULL;
475  service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
476  service->sid = sid;
477  service->provider_name = av_strdup(provider_name);
478  service->name = av_strdup(name);
479  service->pcr_pid = 0x1fff;
480  dynarray_add(&ts->services, &ts->nb_services, service);
481  return service;
482 }
483 
484 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
485 {
486  return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
487  ts->first_pcr;
488 }
489 
491 {
492  MpegTSWrite *ts = s->priv_data;
493  if (ts->m2ts_mode) {
494  int64_t pcr = get_pcr(s->priv_data, s->pb);
495  uint32_t tp_extra_header = pcr % 0x3fffffff;
496  tp_extra_header = AV_RB32(&tp_extra_header);
497  avio_write(s->pb, (unsigned char *) &tp_extra_header,
498  sizeof(tp_extra_header));
499  }
500 }
501 
502 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
503 {
504  AVFormatContext *ctx = s->opaque;
506  avio_write(ctx->pb, packet, TS_PACKET_SIZE);
507 }
508 
510 {
511  MpegTSWrite *ts = s->priv_data;
512  MpegTSWriteStream *ts_st;
513  MpegTSService *service;
514  AVStream *st, *pcr_st = NULL;
515  AVDictionaryEntry *title, *provider;
516  int i, j;
517  const char *service_name;
518  const char *provider_name;
519  int *pids;
520  int ret;
521 
522  if (s->max_delay < 0) /* Not set by the caller */
523  s->max_delay = 0;
524 
525  // round up to a whole number of TS packets
526  ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
527 
528  ts->tsid = ts->transport_stream_id;
529  ts->onid = ts->original_network_id;
530  /* allocate a single DVB service */
531  title = av_dict_get(s->metadata, "service_name", NULL, 0);
532  if (!title)
533  title = av_dict_get(s->metadata, "title", NULL, 0);
534  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
535  provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
536  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
537  service = mpegts_add_service(ts, ts->service_id, provider_name, service_name);
539  service->pmt.opaque = s;
540  service->pmt.cc = 15;
541 
542  ts->pat.pid = PAT_PID;
543  ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
545  ts->pat.opaque = s;
546 
547  ts->sdt.pid = SDT_PID;
548  ts->sdt.cc = 15;
550  ts->sdt.opaque = s;
551 
552  pids = av_malloc(s->nb_streams * sizeof(*pids));
553  if (!pids)
554  return AVERROR(ENOMEM);
555 
556  /* assign pids to each stream */
557  for(i = 0;i < s->nb_streams; i++) {
558  st = s->streams[i];
559  avpriv_set_pts_info(st, 33, 1, 90000);
560  ts_st = av_mallocz(sizeof(MpegTSWriteStream));
561  if (!ts_st) {
562  ret = AVERROR(ENOMEM);
563  goto fail;
564  }
565  st->priv_data = ts_st;
566  ts_st->payload = av_mallocz(ts->pes_payload_size);
567  if (!ts_st->payload) {
568  ret = AVERROR(ENOMEM);
569  goto fail;
570  }
571  ts_st->service = service;
572  /* MPEG pid values < 16 are reserved. Applications which set st->id in
573  * this range are assigned a calculated pid. */
574  if (st->id < 16) {
575  ts_st->pid = ts->start_pid + i;
576  } else if (st->id < 0x1FFF) {
577  ts_st->pid = st->id;
578  } else {
579  av_log(s, AV_LOG_ERROR, "Invalid stream id %d, must be less than 8191\n", st->id);
580  ret = AVERROR(EINVAL);
581  goto fail;
582  }
583  if (ts_st->pid == service->pmt.pid) {
584  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
585  ret = AVERROR(EINVAL);
586  goto fail;
587  }
588  for (j = 0; j < i; j++)
589  if (pids[j] == ts_st->pid) {
590  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
591  ret = AVERROR(EINVAL);
592  goto fail;
593  }
594  pids[i] = ts_st->pid;
595  ts_st->payload_pts = AV_NOPTS_VALUE;
596  ts_st->payload_dts = AV_NOPTS_VALUE;
597  ts_st->first_pts_check = 1;
598  ts_st->cc = 15;
599  /* update PCR pid by using the first video stream */
600  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
601  service->pcr_pid == 0x1fff) {
602  service->pcr_pid = ts_st->pid;
603  pcr_st = st;
604  }
605  if (st->codec->codec_id == AV_CODEC_ID_AAC &&
606  st->codec->extradata_size > 0)
607  {
608  AVStream *ast;
609  ts_st->amux = avformat_alloc_context();
610  if (!ts_st->amux) {
611  ret = AVERROR(ENOMEM);
612  goto fail;
613  }
614  ts_st->amux->oformat = av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts", NULL, NULL);
615  if (!ts_st->amux->oformat) {
616  ret = AVERROR(EINVAL);
617  goto fail;
618  }
619  ast = avformat_new_stream(ts_st->amux, NULL);
620  ret = avcodec_copy_context(ast->codec, st->codec);
621  if (ret != 0)
622  goto fail;
623  ret = avformat_write_header(ts_st->amux, NULL);
624  if (ret < 0)
625  goto fail;
626  }
627  }
628 
629  av_free(pids);
630 
631  /* if no video stream, use the first stream as PCR */
632  if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
633  pcr_st = s->streams[0];
634  ts_st = pcr_st->priv_data;
635  service->pcr_pid = ts_st->pid;
636  }
637 
638  if (ts->mux_rate > 1) {
639  service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
640  (TS_PACKET_SIZE * 8 * 1000);
642  (TS_PACKET_SIZE * 8 * 1000);
644  (TS_PACKET_SIZE * 8 * 1000);
645 
646  if(ts->copyts < 1)
648  } else {
649  /* Arbitrary values, PAT/PMT will also be written on video key frames */
650  ts->sdt_packet_period = 200;
651  ts->pat_packet_period = 40;
652  if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
653  if (!pcr_st->codec->frame_size) {
654  av_log(s, AV_LOG_WARNING, "frame size not set\n");
655  service->pcr_packet_period =
656  pcr_st->codec->sample_rate/(10*512);
657  } else {
658  service->pcr_packet_period =
659  pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
660  }
661  } else {
662  // max delta PCR 0.1s
663  service->pcr_packet_period =
664  pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
665  }
666  if(!service->pcr_packet_period)
667  service->pcr_packet_period = 1;
668  }
669 
670  // output a PCR as soon as possible
671  service->pcr_packet_count = service->pcr_packet_period;
674 
675  if (ts->mux_rate == 1)
676  av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
677  else
678  av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
679  av_log(s, AV_LOG_VERBOSE, "pcr every %d pkts, "
680  "sdt every %d, pat/pmt every %d pkts\n",
681  service->pcr_packet_period,
683 
684  if (ts->m2ts_mode == -1) {
685  if (av_match_ext(s->filename, "m2ts")) {
686  ts->m2ts_mode = 1;
687  } else {
688  ts->m2ts_mode = 0;
689  }
690  }
691 
692  avio_flush(s->pb);
693 
694  return 0;
695 
696  fail:
697  av_free(pids);
698  for(i = 0;i < s->nb_streams; i++) {
699  MpegTSWriteStream *ts_st;
700  st = s->streams[i];
701  ts_st = st->priv_data;
702  if (ts_st) {
703  av_freep(&ts_st->payload);
704  if (ts_st->amux) {
705  avformat_free_context(ts_st->amux);
706  ts_st->amux = NULL;
707  }
708  }
709  av_freep(&st->priv_data);
710  }
711  return ret;
712 }
713 
714 /* send SDT, PAT and PMT tables regulary */
715 static void retransmit_si_info(AVFormatContext *s, int force_pat)
716 {
717  MpegTSWrite *ts = s->priv_data;
718  int i;
719 
720  if (++ts->sdt_packet_count == ts->sdt_packet_period) {
721  ts->sdt_packet_count = 0;
722  mpegts_write_sdt(s);
723  }
724  if (++ts->pat_packet_count == ts->pat_packet_period || force_pat) {
725  ts->pat_packet_count = 0;
726  mpegts_write_pat(s);
727  for(i = 0; i < ts->nb_services; i++) {
728  mpegts_write_pmt(s, ts->services[i]);
729  }
730  }
731 }
732 
733 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
734 {
735  int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
736 
737  *buf++ = pcr_high >> 25;
738  *buf++ = pcr_high >> 17;
739  *buf++ = pcr_high >> 9;
740  *buf++ = pcr_high >> 1;
741  *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
742  *buf++ = pcr_low;
743 
744  return 6;
745 }
746 
747 /* Write a single null transport stream packet */
749 {
750  uint8_t *q;
752 
753  q = buf;
754  *q++ = 0x47;
755  *q++ = 0x00 | 0x1f;
756  *q++ = 0xff;
757  *q++ = 0x10;
758  memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
760  avio_write(s->pb, buf, TS_PACKET_SIZE);
761 }
762 
763 /* Write a single transport stream packet with a PCR and no payload */
765 {
766  MpegTSWrite *ts = s->priv_data;
767  MpegTSWriteStream *ts_st = st->priv_data;
768  uint8_t *q;
770 
771  q = buf;
772  *q++ = 0x47;
773  *q++ = ts_st->pid >> 8;
774  *q++ = ts_st->pid;
775  *q++ = 0x20 | ts_st->cc; /* Adaptation only */
776  /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
777  *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
778  *q++ = 0x10; /* Adaptation flags: PCR present */
779 
780  /* PCR coded into 6 bytes */
781  q += write_pcr_bits(q, get_pcr(ts, s->pb));
782 
783  /* stuffing bytes */
784  memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
786  avio_write(s->pb, buf, TS_PACKET_SIZE);
787 }
788 
789 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
790 {
791  int val;
792 
793  val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
794  *q++ = val;
795  val = (((pts >> 15) & 0x7fff) << 1) | 1;
796  *q++ = val >> 8;
797  *q++ = val;
798  val = (((pts) & 0x7fff) << 1) | 1;
799  *q++ = val >> 8;
800  *q++ = val;
801 }
802 
803 /* Set an adaptation field flag in an MPEG-TS packet*/
804 static void set_af_flag(uint8_t *pkt, int flag)
805 {
806  // expect at least one flag to set
807  av_assert0(flag);
808 
809  if ((pkt[3] & 0x20) == 0) {
810  // no AF yet, set adaptation field flag
811  pkt[3] |= 0x20;
812  // 1 byte length, no flags
813  pkt[4] = 1;
814  pkt[5] = 0;
815  }
816  pkt[5] |= flag;
817 }
818 
819 /* Extend the adaptation field by size bytes */
820 static void extend_af(uint8_t *pkt, int size)
821 {
822  // expect already existing adaptation field
823  av_assert0(pkt[3] & 0x20);
824  pkt[4] += size;
825 }
826 
827 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
829 {
830  if (pkt[3] & 0x20)
831  return pkt + 5 + pkt[4];
832  else
833  return pkt + 4;
834 }
835 
836 /* Add a pes header to the front of payload, and segment into an integer number of
837  * ts packets. The final ts packet is padded using an over-sized adaptation header
838  * to exactly fill the last ts packet.
839  * NOTE: 'payload' contains a complete PES payload.
840  */
842  const uint8_t *payload, int payload_size,
843  int64_t pts, int64_t dts, int key)
844 {
845  MpegTSWriteStream *ts_st = st->priv_data;
846  MpegTSWrite *ts = s->priv_data;
848  uint8_t *q;
849  int val, is_start, len, header_len, write_pcr, private_code, flags;
850  int afc_len, stuffing_len;
851  int64_t pcr = -1; /* avoid warning */
852  int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
853  int force_pat = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
854 
855  is_start = 1;
856  while (payload_size > 0) {
857  retransmit_si_info(s, force_pat);
858  force_pat = 0;
859 
860  write_pcr = 0;
861  if (ts_st->pid == ts_st->service->pcr_pid) {
862  if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
863  ts_st->service->pcr_packet_count++;
864  if (ts_st->service->pcr_packet_count >=
865  ts_st->service->pcr_packet_period) {
866  ts_st->service->pcr_packet_count = 0;
867  write_pcr = 1;
868  }
869  }
870 
871  if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
872  (dts - get_pcr(ts, s->pb)/300) > delay) {
873  /* pcr insert gets priority over null packet insert */
874  if (write_pcr)
875  mpegts_insert_pcr_only(s, st);
876  else
878  continue; /* recalculate write_pcr and possibly retransmit si_info */
879  }
880 
881  /* prepare packet header */
882  q = buf;
883  *q++ = 0x47;
884  val = (ts_st->pid >> 8);
885  if (is_start)
886  val |= 0x40;
887  *q++ = val;
888  *q++ = ts_st->pid;
889  ts_st->cc = (ts_st->cc + 1) & 0xf;
890  *q++ = 0x10 | ts_st->cc; // payload indicator + CC
891  if (key && is_start && pts != AV_NOPTS_VALUE) {
892  // set Random Access for key frames
893  if (ts_st->pid == ts_st->service->pcr_pid)
894  write_pcr = 1;
895  set_af_flag(buf, 0x40);
896  q = get_ts_payload_start(buf);
897  }
898  if (write_pcr) {
899  set_af_flag(buf, 0x10);
900  q = get_ts_payload_start(buf);
901  // add 11, pcr references the last byte of program clock reference base
902  if (ts->mux_rate > 1)
903  pcr = get_pcr(ts, s->pb);
904  else
905  pcr = (dts - delay)*300;
906  if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
907  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
908  extend_af(buf, write_pcr_bits(q, pcr));
909  q = get_ts_payload_start(buf);
910  }
911  if (is_start) {
912  int pes_extension = 0;
913  /* write PES header */
914  *q++ = 0x00;
915  *q++ = 0x00;
916  *q++ = 0x01;
917  private_code = 0;
918  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
919  if (st->codec->codec_id == AV_CODEC_ID_DIRAC) {
920  *q++ = 0xfd;
921  } else
922  *q++ = 0xe0;
923  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
924  (st->codec->codec_id == AV_CODEC_ID_MP2 ||
925  st->codec->codec_id == AV_CODEC_ID_MP3 ||
926  st->codec->codec_id == AV_CODEC_ID_AAC)) {
927  *q++ = 0xc0;
928  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
929  st->codec->codec_id == AV_CODEC_ID_AC3 &&
930  ts->m2ts_mode) {
931  *q++ = 0xfd;
932  } else {
933  *q++ = 0xbd;
934  if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
935  private_code = 0x20;
936  }
937  }
938  header_len = 0;
939  flags = 0;
940  if (pts != AV_NOPTS_VALUE) {
941  header_len += 5;
942  flags |= 0x80;
943  }
944  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
945  header_len += 5;
946  flags |= 0x40;
947  }
948  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
949  st->codec->codec_id == AV_CODEC_ID_DIRAC) {
950  /* set PES_extension_flag */
951  pes_extension = 1;
952  flags |= 0x01;
953 
954  /*
955  * One byte for PES2 extension flag +
956  * one byte for extension length +
957  * one byte for extension id
958  */
959  header_len += 3;
960  }
961  /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
962  * otherwise it will not play sound on blu-ray
963  */
964  if (ts->m2ts_mode &&
966  st->codec->codec_id == AV_CODEC_ID_AC3) {
967  /* set PES_extension_flag */
968  pes_extension = 1;
969  flags |= 0x01;
970  header_len += 3;
971  }
972  len = payload_size + header_len + 3;
973  if (private_code != 0)
974  len++;
975  if (len > 0xffff)
976  len = 0;
977  *q++ = len >> 8;
978  *q++ = len;
979  val = 0x80;
980  /* data alignment indicator is required for subtitle data */
982  val |= 0x04;
983  *q++ = val;
984  *q++ = flags;
985  *q++ = header_len;
986  if (pts != AV_NOPTS_VALUE) {
987  write_pts(q, flags >> 6, pts);
988  q += 5;
989  }
990  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
991  write_pts(q, 1, dts);
992  q += 5;
993  }
994  if (pes_extension && st->codec->codec_id == AV_CODEC_ID_DIRAC) {
995  flags = 0x01; /* set PES_extension_flag_2 */
996  *q++ = flags;
997  *q++ = 0x80 | 0x01; /* marker bit + extension length */
998  /*
999  * Set the stream id extension flag bit to 0 and
1000  * write the extended stream id
1001  */
1002  *q++ = 0x00 | 0x60;
1003  }
1004  /* For Blu-ray AC3 Audio Setting extended flags */
1005  if (ts->m2ts_mode &&
1006  pes_extension &&
1007  st->codec->codec_id == AV_CODEC_ID_AC3) {
1008  flags = 0x01; /* set PES_extension_flag_2 */
1009  *q++ = flags;
1010  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1011  *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1012  }
1013 
1014 
1015  if (private_code != 0)
1016  *q++ = private_code;
1017  is_start = 0;
1018  }
1019  /* header size */
1020  header_len = q - buf;
1021  /* data len */
1022  len = TS_PACKET_SIZE - header_len;
1023  if (len > payload_size)
1024  len = payload_size;
1025  stuffing_len = TS_PACKET_SIZE - header_len - len;
1026  if (stuffing_len > 0) {
1027  /* add stuffing with AFC */
1028  if (buf[3] & 0x20) {
1029  /* stuffing already present: increase its size */
1030  afc_len = buf[4] + 1;
1031  memmove(buf + 4 + afc_len + stuffing_len,
1032  buf + 4 + afc_len,
1033  header_len - (4 + afc_len));
1034  buf[4] += stuffing_len;
1035  memset(buf + 4 + afc_len, 0xff, stuffing_len);
1036  } else {
1037  /* add stuffing */
1038  memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1039  buf[3] |= 0x20;
1040  buf[4] = stuffing_len - 1;
1041  if (stuffing_len >= 2) {
1042  buf[5] = 0x00;
1043  memset(buf + 6, 0xff, stuffing_len - 2);
1044  }
1045  }
1046  }
1047  memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1048  payload += len;
1049  payload_size -= len;
1051  avio_write(s->pb, buf, TS_PACKET_SIZE);
1052  }
1053  avio_flush(s->pb);
1054  ts_st->prev_payload_key = key;
1055 }
1056 
1058 {
1059  AVStream *st = s->streams[pkt->stream_index];
1060  int size = pkt->size;
1061  uint8_t *buf= pkt->data;
1062  uint8_t *data= NULL;
1063  MpegTSWrite *ts = s->priv_data;
1064  MpegTSWriteStream *ts_st = st->priv_data;
1065  const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
1066  int64_t dts = pkt->dts, pts = pkt->pts;
1067 
1068  if (ts->reemit_pat_pmt) {
1069  av_log(s, AV_LOG_WARNING, "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1070  ts->reemit_pat_pmt = 0;
1072  }
1073 
1074  if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1075  ts->pat_packet_count = ts->pat_packet_period - 1;
1076  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1078  }
1079 
1080  if(ts->copyts < 1){
1081  if (pts != AV_NOPTS_VALUE)
1082  pts += delay;
1083  if (dts != AV_NOPTS_VALUE)
1084  dts += delay;
1085  }
1086 
1087  if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1088  av_log(s, AV_LOG_ERROR, "first pts value must be set\n");
1089  return AVERROR_INVALIDDATA;
1090  }
1091  ts_st->first_pts_check = 0;
1092 
1093  if (st->codec->codec_id == AV_CODEC_ID_H264) {
1094  const uint8_t *p = buf, *buf_end = p+size;
1095  uint32_t state = -1;
1096 
1097  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
1098  if (!st->nb_frames) {
1099  av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1100  "no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)\n");
1101  return AVERROR(EINVAL);
1102  }
1103  av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing\n");
1104  }
1105 
1106  do {
1107  p = avpriv_find_start_code(p, buf_end, &state);
1108  av_dlog(s, "nal %d\n", state & 0x1f);
1109  } while (p < buf_end && (state & 0x1f) != 9 &&
1110  (state & 0x1f) != 5 && (state & 0x1f) != 1);
1111 
1112  if ((state & 0x1f) != 9) { // AUD NAL
1113  data = av_malloc(pkt->size+6);
1114  if (!data)
1115  return AVERROR(ENOMEM);
1116  memcpy(data+6, pkt->data, pkt->size);
1117  AV_WB32(data, 0x00000001);
1118  data[4] = 0x09;
1119  data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1120  buf = data;
1121  size = pkt->size+6;
1122  }
1123  } else if (st->codec->codec_id == AV_CODEC_ID_AAC) {
1124  if (pkt->size < 2) {
1125  av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1126  return AVERROR_INVALIDDATA;
1127  }
1128  if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1129  int ret;
1130  AVPacket pkt2;
1131 
1132  if (!ts_st->amux) {
1133  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1134  "and extradata missing\n");
1135  return AVERROR_INVALIDDATA;
1136  }
1137 
1138  av_init_packet(&pkt2);
1139  pkt2.data = pkt->data;
1140  pkt2.size = pkt->size;
1141  ret = avio_open_dyn_buf(&ts_st->amux->pb);
1142  if (ret < 0)
1143  return AVERROR(ENOMEM);
1144 
1145  ret = av_write_frame(ts_st->amux, &pkt2);
1146  if (ret < 0) {
1147  avio_close_dyn_buf(ts_st->amux->pb, &data);
1148  ts_st->amux->pb = NULL;
1149  av_free(data);
1150  return ret;
1151  }
1152  size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1153  ts_st->amux->pb = NULL;
1154  buf = data;
1155  }
1156  }
1157 
1158  if (pkt->dts != AV_NOPTS_VALUE) {
1159  int i;
1160  for(i=0; i<s->nb_streams; i++){
1161  AVStream *st2 = s->streams[i];
1162  MpegTSWriteStream *ts_st2 = st2->priv_data;
1163  if( ts_st2->payload_size
1164  && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)){
1165  mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
1166  ts_st2->payload_pts, ts_st2->payload_dts,
1167  ts_st2->payload_flags & AV_PKT_FLAG_KEY);
1168  ts_st2->payload_size = 0;
1169  }
1170  }
1171  }
1172 
1173  if (ts_st->payload_size && ts_st->payload_size + size > ts->pes_payload_size) {
1174  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1175  ts_st->payload_pts, ts_st->payload_dts,
1176  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1177  ts_st->payload_size = 0;
1178  }
1179 
1180  if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
1181  av_assert0(!ts_st->payload_size);
1182  // for video and subtitle, write a single pes packet
1183  mpegts_write_pes(s, st, buf, size, pts, dts, pkt->flags & AV_PKT_FLAG_KEY);
1184  av_free(data);
1185  return 0;
1186  }
1187 
1188  if (!ts_st->payload_size) {
1189  ts_st->payload_pts = pts;
1190  ts_st->payload_dts = dts;
1191  ts_st->payload_flags = pkt->flags;
1192  }
1193 
1194  memcpy(ts_st->payload + ts_st->payload_size, buf, size);
1195  ts_st->payload_size += size;
1196 
1197  av_free(data);
1198 
1199  return 0;
1200 }
1201 
1203 {
1204  int i;
1205 
1206  /* flush current packets */
1207  for(i = 0; i < s->nb_streams; i++) {
1208  AVStream *st = s->streams[i];
1209  MpegTSWriteStream *ts_st = st->priv_data;
1210  if (ts_st->payload_size > 0) {
1211  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1212  ts_st->payload_pts, ts_st->payload_dts,
1213  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1214  ts_st->payload_size = 0;
1215  }
1216  }
1217  avio_flush(s->pb);
1218 }
1219 
1221 {
1222  if (!pkt) {
1223  mpegts_write_flush(s);
1224  return 1;
1225  } else {
1226  return mpegts_write_packet_internal(s, pkt);
1227  }
1228 }
1229 
1231 {
1232  MpegTSWrite *ts = s->priv_data;
1233  MpegTSService *service;
1234  int i;
1235 
1236  mpegts_write_flush(s);
1237 
1238  for(i = 0; i < s->nb_streams; i++) {
1239  AVStream *st = s->streams[i];
1240  MpegTSWriteStream *ts_st = st->priv_data;
1241  av_freep(&ts_st->payload);
1242  if (ts_st->amux) {
1243  avformat_free_context(ts_st->amux);
1244  ts_st->amux = NULL;
1245  }
1246  }
1247 
1248  for(i = 0; i < ts->nb_services; i++) {
1249  service = ts->services[i];
1250  av_freep(&service->provider_name);
1251  av_freep(&service->name);
1252  av_free(service);
1253  }
1254  av_free(ts->services);
1255 
1256  return 0;
1257 }
1258 
1260  .name = "mpegts",
1261  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
1262  .mime_type = "video/x-mpegts",
1263  .extensions = "ts,m2t,m2ts,mts",
1264  .priv_data_size = sizeof(MpegTSWrite),
1265  .audio_codec = AV_CODEC_ID_MP2,
1266  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1271  .priv_class = &mpegts_muxer_class,
1272 };
const char * name
Definition: avisynth_c.h:675
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
#define SDT_PID
Definition: mpegts.h:37
static void retransmit_si_info(AVFormatContext *s, int force_pat)
Definition: mpegtsenc.c:715
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define PMT_TID
Definition: mpegts.h:41
int pat_packet_period
Definition: mpegtsenc.c:66
MpegTSSection pmt
Definition: mpegtsenc.c:49
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:275
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:988
AVOption.
Definition: opt.h:251
int pcr_packet_count
Definition: mpegtsenc.c:54
int sdt_packet_count
Definition: mpegtsenc.c:63
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:383
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:504
av_default_item_name
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.
#define STREAM_TYPE_VIDEO_CAVS
Definition: mpeg.h:57
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
int64_t payload_dts
Definition: mpegtsenc.c:236
char * name
Definition: mpegtsenc.c:51
int num
numerator
Definition: rational.h:44
title('Sinusoid at 1/4 the Spampling Rate')
#define PCR_TIME_BASE
Definition: mpegtsenc.c:34
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:618
void * priv_data
Definition: avformat.h:663
#define AV_DISPOSITION_CLEAN_EFFECTS
stream without voice
Definition: avformat.h:620
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)
int version
Definition: avisynth_c.h:666
#define PCR_RETRANS_TIME
Definition: mpegtsenc.c:226
int64_t first_pcr
Definition: mpegtsenc.c:70
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:361
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:976
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
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:944
char * provider_name
Definition: mpegtsenc.c:52
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
int first_pts_check
first pts check needed
Definition: mpegtsenc.c:233
#define SDT_RETRANS_TIME
Definition: mpegtsenc.c:224
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
int64_t payload_pts
Definition: mpegtsenc.c:235
uint8_t
AVOptions.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:261
#define AV_RB32
void * opaque
Definition: mpegtsenc.c:45
static AVPacket pkt
Definition: demuxing.c:56
#define DEFAULT_PES_PAYLOAD_SIZE
Definition: mpegtsenc.c:92
int id
Format-specific stream ID.
Definition: avformat.h:650
#define b
Definition: input.c:42
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
Definition: avformat.h:992
#define TS_PACKET_SIZE
Definition: mpegts.h:29
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1220
static void mpegts_insert_null_packet(AVFormatContext *s)
Definition: mpegtsenc.c:748
uint8_t * data
struct MpegTSService MpegTSService
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
static void mpegts_write_sdt(AVFormatContext *s)
Definition: mpegtsenc.c:427
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
static void extend_af(uint8_t *pkt, int size)
Definition: mpegtsenc.c:820
#define MPEGTS_FLAG_AAC_LATM
Definition: mpegtsenc.c:85
static int write_trailer(AVFormatContext *s)
#define PAT_TID
Definition: mpegts.h:40
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:281
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
struct AVOutputFormat * oformat
Definition: avformat.h:958
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define STREAM_TYPE_AUDIO_AAC
Definition: mpeg.h:54
struct MpegTSService * service
Definition: mpegtsenc.c:229
static void mpegts_prefix_m2ts_header(AVFormatContext *s)
Definition: mpegtsenc.c:490
AVDictionary * metadata
Definition: avformat.h:1092
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
static void put16(uint8_t **q_ptr, int val)
Definition: mpegtsenc.c:181
static int mpegts_write_section1(MpegTSSection *s, int tid, int id, int version, int sec_num, int last_sec_num, uint8_t *buf, int len)
Definition: mpegtsenc.c:190
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
Definition: mpegtsenc.c:789
#define AV_RB16
int pes_payload_size
Definition: mpegtsenc.c:72
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static uint8_t * get_ts_payload_start(uint8_t *pkt)
Definition: mpegtsenc.c:828
Spectrum Plot time data
#define STREAM_TYPE_VIDEO_DIRAC
Definition: mpegts.h:57
static void set_af_flag(uint8_t *pkt, int flag)
Definition: mpegtsenc.c:804
preferred ID for decoding MPEG audio layer 1, 2 or 3
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
Definition: mpegtsenc.c:764
simple assert() macros that are a bit more flexible than ISO C assert().
int nb_services
Definition: mpegtsenc.c:67
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
#define PAT_RETRANS_TIME
Definition: mpegtsenc.c:225
int mux_rate
set to 1 when VBR
Definition: mpegtsenc.c:71
static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1057
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
Definition: mpegtsenc.c:733
int size
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
#define STREAM_TYPE_AUDIO_AAC_LATM
Definition: mpegts.h:52
#define AV_LOG_VERBOSE
Definition: log.h:157
static const AVOption options[]
Definition: mpegtsenc.c:94
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
#define dynarray_add(tab, nb_ptr, elem)
char filename[1024]
input or output filename
Definition: avformat.h:994
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:118
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:196
static void putstr8(uint8_t **q_ptr, const char *str)
Definition: mpegtsenc.c:411
#define STREAM_TYPE_VIDEO_H264
Definition: mpeg.h:56
ret
Definition: avfilter.c:821
const char * name
Definition: avformat.h:378
#define DEFAULT_PROVIDER_NAME
Definition: mpegtsenc.c:220
AVOutputFormat ff_mpegts_muxer
Definition: mpegtsenc.c:1259
AVFormatContext * amux
Definition: mpegtsenc.c:239
AVDictionary * metadata
Definition: avformat.h:711
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
static int mpegts_write_header(AVFormatContext *s)
Definition: mpegtsenc.c:509
#define MPEGTS_FLAG_REEMIT_PAT_PMT
Definition: mpegtsenc.c:84
static void mpegts_write_pes(AVFormatContext *s, AVStream *st, const uint8_t *payload, int payload_size, int64_t pts, int64_t dts, int key)
Definition: mpegtsenc.c:841
preferred ID for MPEG-1/2 video decoding
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define STREAM_TYPE_VIDEO_MPEG4
Definition: mpeg.h:55
int pmt_start_pid
Definition: mpegtsenc.c:78
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:619
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:44
Stream structure.
Definition: avformat.h:643
int start_pid
Definition: mpegtsenc.c:79
int copyts
Definition: mpegtsenc.c:87
int frame_size
Number of samples per channel in an audio frame.
NULL
Definition: eval.c:55
#define av_bswap32
Definition: bfin/bswap.h:33
enum AVMediaType codec_type
typedef void(RENAME(mix_any_func_type))
enum AVCodecID codec_id
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:220
int sample_rate
samples per second
int sdt_packet_period
Definition: mpegtsenc.c:64
AVIOContext * pb
I/O context.
Definition: avformat.h:977
const AVClass * av_class
Definition: mpegtsenc.c:59
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
#define STREAM_TYPE_PRIVATE_DATA
Definition: mpeg.h:53
void * buf
Definition: avisynth_c.h:594
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
Describe the class of an AVClass context structure.
Definition: log.h:50
int original_network_id
Definition: mpegtsenc.c:75
synthesis window for stochastic i
int pcr_packet_period
Definition: mpegtsenc.c:55
int service_id
Definition: mpegtsenc.c:76
byte swapping routines
static int mpegts_write_end(AVFormatContext *s)
Definition: mpegtsenc.c:1230
#define STREAM_TYPE_AUDIO_AC3
Definition: mpeg.h:59
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
int transport_stream_id
Definition: mpegtsenc.c:74
MpegTSService ** services
Definition: mpegtsenc.c:62
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:502
uint8_t * payload
Definition: mpegtsenc.c:238
static uint32_t state
Definition: trasher.c:27
static void mpegts_write_flush(AVFormatContext *s)
Definition: mpegtsenc.c:1202
static int flags
Definition: cpu.c:23
int m2ts_mode
Definition: mpegtsenc.c:80
struct MpegTSWriteStream MpegTSWriteStream
#define DEFAULT_SERVICE_NAME
Definition: mpegtsenc.c:221
Main libavformat public API header.
common internal api header.
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
Definition: mpegtsenc.c:484
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:700
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:698
int den
denominator
Definition: rational.h:45
struct MpegTSWrite MpegTSWrite
static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
Definition: mpegtsenc.c:259
char * value
Definition: dict.h:82
int len
#define PAT_PID
Definition: mpegts.h:36
#define STREAM_TYPE_VIDEO_MPEG2
Definition: mpeg.h:49
void * priv_data
Format private data.
Definition: avformat.h:964
MpegTSSection pat
Definition: mpegtsenc.c:60
int reemit_pat_pmt
Definition: mpegtsenc.c:82
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
static void mpegts_write_pat(AVFormatContext *s)
Definition: mpegtsenc.c:242
MpegTSSection sdt
Definition: mpegtsenc.c:61
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int pat_packet_count
Definition: mpegtsenc.c:65
static const AVClass mpegts_muxer_class
Definition: mpegtsenc.c:127
struct MpegTSSection MpegTSSection
#define STREAM_TYPE_AUDIO_MPEG1
Definition: mpeg.h:50
This structure stores compressed data.
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
Definition: mpegtsenc.c:135
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
#define SDT_TID
Definition: mpegts.h:43
static MpegTSService * mpegts_add_service(MpegTSWrite *ts, int sid, const char *provider_name, const char *name)
Definition: mpegtsenc.c:465