FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpegtsenc.c
Go to the documentation of this file.
1 /*
2  * MPEG-2 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/avassert.h"
23 #include "libavutil/bswap.h"
24 #include "libavutil/crc.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
29 
30 #include "libavcodec/internal.h"
31 
32 #include "avformat.h"
33 #include "avio_internal.h"
34 #include "internal.h"
35 #include "mpegts.h"
36 
37 #define PCR_TIME_BASE 27000000
38 
39 /* write DVB SI sections */
40 
41 #define DVB_PRIVATE_NETWORK_START 0xff01
42 
43 /*********************************************/
44 /* mpegts section writer */
45 
46 typedef struct MpegTSSection {
47  int pid;
48  int cc;
50  void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
51  void *opaque;
53 
54 typedef struct MpegTSService {
55  MpegTSSection pmt; /* MPEG-2 PMT table context */
56  int sid; /* service ID */
57  char *name;
59  int pcr_pid;
64 
65 // service_type values as defined in ETSI 300 468
66 enum {
75 };
76 typedef struct MpegTSWrite {
77  const AVClass *av_class;
78  MpegTSSection pat; /* MPEG-2 PAT table */
79  MpegTSSection sdt; /* MPEG-2 SDT table context */
86  int onid;
87  int tsid;
88  int64_t first_pcr;
89  int mux_rate; ///< set to 1 when VBR
91 
96 
98  int start_pid;
99  int m2ts_mode;
100 
101  int reemit_pat_pmt; // backward compatibility
102 
104 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
105 #define MPEGTS_FLAG_AAC_LATM 0x02
106 #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
107 #define MPEGTS_FLAG_SYSTEM_B 0x08
108 #define MPEGTS_FLAG_DISCONT 0x10
109  int flags;
110  int copyts;
112  double pat_period;
113  double sdt_period;
114  int64_t last_pat_ts;
115  int64_t last_sdt_ts;
116 
118 } MpegTSWrite;
119 
120 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
121 #define DEFAULT_PES_HEADER_FREQ 16
122 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
123 
124 /* The section length is 12 bits. The first 2 are set to 0, the remaining
125  * 10 bits should not exceed 1021. */
126 #define SECTION_LENGTH 1020
127 
128 /* NOTE: 4 bytes must be left at the end for the crc32 */
130 {
131  unsigned int crc;
132  unsigned char packet[TS_PACKET_SIZE];
133  const unsigned char *buf_ptr;
134  unsigned char *q;
135  int first, b, len1, left;
136 
138  -1, buf, len - 4));
139 
140  buf[len - 4] = (crc >> 24) & 0xff;
141  buf[len - 3] = (crc >> 16) & 0xff;
142  buf[len - 2] = (crc >> 8) & 0xff;
143  buf[len - 1] = crc & 0xff;
144 
145  /* send each packet */
146  buf_ptr = buf;
147  while (len > 0) {
148  first = buf == buf_ptr;
149  q = packet;
150  *q++ = 0x47;
151  b = s->pid >> 8;
152  if (first)
153  b |= 0x40;
154  *q++ = b;
155  *q++ = s->pid;
156  s->cc = s->cc + 1 & 0xf;
157  *q++ = 0x10 | s->cc;
158  if (s->discontinuity) {
159  q[-1] |= 0x20;
160  *q++ = 1;
161  *q++ = 0x80;
162  s->discontinuity = 0;
163  }
164  if (first)
165  *q++ = 0; /* 0 offset */
166  len1 = TS_PACKET_SIZE - (q - packet);
167  if (len1 > len)
168  len1 = len;
169  memcpy(q, buf_ptr, len1);
170  q += len1;
171  /* add known padding data */
172  left = TS_PACKET_SIZE - (q - packet);
173  if (left > 0)
174  memset(q, 0xff, left);
175 
176  s->write_packet(s, packet);
177 
178  buf_ptr += len1;
179  len -= len1;
180  }
181 }
182 
183 static inline void put16(uint8_t **q_ptr, int val)
184 {
185  uint8_t *q;
186  q = *q_ptr;
187  *q++ = val >> 8;
188  *q++ = val;
189  *q_ptr = q;
190 }
191 
192 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
193  int version, int sec_num, int last_sec_num,
194  uint8_t *buf, int len)
195 {
196  uint8_t section[1024], *q;
197  unsigned int tot_len;
198  /* reserved_future_use field must be set to 1 for SDT */
199  unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
200 
201  tot_len = 3 + 5 + len + 4;
202  /* check if not too big */
203  if (tot_len > 1024)
204  return AVERROR_INVALIDDATA;
205 
206  q = section;
207  *q++ = tid;
208  put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
209  put16(&q, id);
210  *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
211  *q++ = sec_num;
212  *q++ = last_sec_num;
213  memcpy(q, buf, len);
214 
215  mpegts_write_section(s, section, tot_len);
216  return 0;
217 }
218 
219 /*********************************************/
220 /* mpegts writer */
221 
222 #define DEFAULT_PROVIDER_NAME "FFmpeg"
223 #define DEFAULT_SERVICE_NAME "Service01"
224 
225 /* we retransmit the SI info at this rate */
226 #define SDT_RETRANS_TIME 500
227 #define PAT_RETRANS_TIME 100
228 #define PCR_RETRANS_TIME 20
229 
230 typedef struct MpegTSWriteStream {
232  int pid; /* stream associated pid */
233  int cc;
236  int first_pts_check; ///< first pts check needed
238  int64_t payload_pts;
239  int64_t payload_dts;
244 
245  /* For Opus */
249 
251 {
252  MpegTSWrite *ts = s->priv_data;
253  MpegTSService *service;
255  int i;
256 
257  q = data;
258  for (i = 0; i < ts->nb_services; i++) {
259  service = ts->services[i];
260  put16(&q, service->sid);
261  put16(&q, 0xe000 | service->pmt.pid);
262  }
263  mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, ts->tables_version, 0, 0,
264  data, q - data);
265 }
266 
267 /* NOTE: !str is accepted for an empty string */
268 static void putstr8(uint8_t **q_ptr, const char *str, int write_len)
269 {
270  uint8_t *q;
271  int len;
272 
273  q = *q_ptr;
274  if (!str)
275  len = 0;
276  else
277  len = strlen(str);
278  if (write_len)
279  *q++ = len;
280  memcpy(q, str, len);
281  q += len;
282  *q_ptr = q;
283 }
284 
286 {
287  MpegTSWrite *ts = s->priv_data;
288  uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
289  int val, stream_type, i, err = 0;
290 
291  q = data;
292  put16(&q, 0xe000 | service->pcr_pid);
293 
294  program_info_length_ptr = q;
295  q += 2; /* patched after */
296 
297  /* put program info here */
298 
299  val = 0xf000 | (q - program_info_length_ptr - 2);
300  program_info_length_ptr[0] = val >> 8;
301  program_info_length_ptr[1] = val;
302 
303  for (i = 0; i < s->nb_streams; i++) {
304  AVStream *st = s->streams[i];
305  MpegTSWriteStream *ts_st = st->priv_data;
306  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
307 
308  if (s->nb_programs) {
309  int k, found = 0;
310  AVProgram *program = service->program;
311 
312  for (k = 0; k < program->nb_stream_indexes; k++)
313  if (program->stream_index[k] == i) {
314  found = 1;
315  break;
316  }
317 
318  if (!found)
319  continue;
320  }
321 
322  if (q - data > SECTION_LENGTH - 32) {
323  err = 1;
324  break;
325  }
326  switch (st->codecpar->codec_id) {
329  stream_type = STREAM_TYPE_VIDEO_MPEG2;
330  break;
331  case AV_CODEC_ID_MPEG4:
332  stream_type = STREAM_TYPE_VIDEO_MPEG4;
333  break;
334  case AV_CODEC_ID_H264:
335  stream_type = STREAM_TYPE_VIDEO_H264;
336  break;
337  case AV_CODEC_ID_HEVC:
338  stream_type = STREAM_TYPE_VIDEO_HEVC;
339  break;
340  case AV_CODEC_ID_CAVS:
341  stream_type = STREAM_TYPE_VIDEO_CAVS;
342  break;
343  case AV_CODEC_ID_DIRAC:
344  stream_type = STREAM_TYPE_VIDEO_DIRAC;
345  break;
346  case AV_CODEC_ID_VC1:
347  stream_type = STREAM_TYPE_VIDEO_VC1;
348  break;
349  case AV_CODEC_ID_MP2:
350  case AV_CODEC_ID_MP3:
351  if ( st->codecpar->sample_rate > 0
352  && st->codecpar->sample_rate < 32000) {
353  stream_type = STREAM_TYPE_AUDIO_MPEG2;
354  } else {
355  stream_type = STREAM_TYPE_AUDIO_MPEG1;
356  }
357  break;
358  case AV_CODEC_ID_AAC:
359  stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
362  break;
364  stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
365  break;
366  case AV_CODEC_ID_AC3:
367  stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
370  break;
371  case AV_CODEC_ID_EAC3:
372  stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
375  break;
376  case AV_CODEC_ID_DTS:
377  stream_type = STREAM_TYPE_AUDIO_DTS;
378  break;
379  case AV_CODEC_ID_TRUEHD:
380  stream_type = STREAM_TYPE_AUDIO_TRUEHD;
381  break;
382  case AV_CODEC_ID_OPUS:
383  stream_type = STREAM_TYPE_PRIVATE_DATA;
384  break;
386  stream_type = STREAM_TYPE_METADATA;
387  break;
388  default:
389  stream_type = STREAM_TYPE_PRIVATE_DATA;
390  break;
391  }
392 
393  *q++ = stream_type;
394  put16(&q, 0xe000 | ts_st->pid);
395  desc_length_ptr = q;
396  q += 2; /* patched after */
397 
398  /* write optional descriptors here */
399  switch (st->codecpar->codec_type) {
400  case AVMEDIA_TYPE_AUDIO:
402  *q++=0x6a; // AC3 descriptor see A038 DVB SI
403  *q++=1; // 1 byte, all flags sets to 0
404  *q++=0; // omit all fields...
405  }
407  *q++=0x7a; // EAC3 descriptor see A038 DVB SI
408  *q++=1; // 1 byte, all flags sets to 0
409  *q++=0; // omit all fields...
410  }
411  if (st->codecpar->codec_id==AV_CODEC_ID_S302M) {
412  *q++ = 0x05; /* MPEG-2 registration descriptor*/
413  *q++ = 4;
414  *q++ = 'B';
415  *q++ = 'S';
416  *q++ = 'S';
417  *q++ = 'D';
418  }
419  if (st->codecpar->codec_id==AV_CODEC_ID_OPUS) {
420  /* 6 bytes registration descriptor, 4 bytes Opus audio descriptor */
421  if (q - data > SECTION_LENGTH - 6 - 4) {
422  err = 1;
423  break;
424  }
425 
426  *q++ = 0x05; /* MPEG-2 registration descriptor*/
427  *q++ = 4;
428  *q++ = 'O';
429  *q++ = 'p';
430  *q++ = 'u';
431  *q++ = 's';
432 
433  *q++ = 0x7f; /* DVB extension descriptor */
434  *q++ = 2;
435  *q++ = 0x80;
436 
437  if (st->codecpar->extradata && st->codecpar->extradata_size >= 19) {
438  if (st->codecpar->extradata[18] == 0 && st->codecpar->channels <= 2) {
439  /* RTP mapping family */
440  *q++ = st->codecpar->channels;
441  } else if (st->codecpar->extradata[18] == 1 && st->codecpar->channels <= 8 &&
442  st->codecpar->extradata_size >= 21 + st->codecpar->channels) {
443  static const uint8_t coupled_stream_counts[9] = {
444  1, 0, 1, 1, 2, 2, 2, 3, 3
445  };
446  static const uint8_t channel_map_a[8][8] = {
447  {0},
448  {0, 1},
449  {0, 2, 1},
450  {0, 1, 2, 3},
451  {0, 4, 1, 2, 3},
452  {0, 4, 1, 2, 3, 5},
453  {0, 4, 1, 2, 3, 5, 6},
454  {0, 6, 1, 2, 3, 4, 5, 7},
455  };
456  static const uint8_t channel_map_b[8][8] = {
457  {0},
458  {0, 1},
459  {0, 1, 2},
460  {0, 1, 2, 3},
461  {0, 1, 2, 3, 4},
462  {0, 1, 2, 3, 4, 5},
463  {0, 1, 2, 3, 4, 5, 6},
464  {0, 1, 2, 3, 4, 5, 6, 7},
465  };
466  /* Vorbis mapping family */
467 
468  if (st->codecpar->extradata[19] == st->codecpar->channels - coupled_stream_counts[st->codecpar->channels] &&
469  st->codecpar->extradata[20] == coupled_stream_counts[st->codecpar->channels] &&
470  memcmp(&st->codecpar->extradata[21], channel_map_a[st->codecpar->channels-1], st->codecpar->channels) == 0) {
471  *q++ = st->codecpar->channels;
472  } else if (st->codecpar->channels >= 2 && st->codecpar->extradata[19] == st->codecpar->channels &&
473  st->codecpar->extradata[20] == 0 &&
474  memcmp(&st->codecpar->extradata[21], channel_map_b[st->codecpar->channels-1], st->codecpar->channels) == 0) {
475  *q++ = st->codecpar->channels | 0x80;
476  } else {
477  /* Unsupported, could write an extended descriptor here */
478  av_log(s, AV_LOG_ERROR, "Unsupported Opus Vorbis-style channel mapping");
479  *q++ = 0xff;
480  }
481  } else {
482  /* Unsupported */
483  av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping for family %d", st->codecpar->extradata[18]);
484  *q++ = 0xff;
485  }
486  } else if (st->codecpar->channels <= 2) {
487  /* Assume RTP mapping family */
488  *q++ = st->codecpar->channels;
489  } else {
490  /* Unsupported */
491  av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping");
492  *q++ = 0xff;
493  }
494  }
495 
496  if (lang) {
497  char *p;
498  char *next = lang->value;
499  uint8_t *len_ptr;
500 
501  *q++ = 0x0a; /* ISO 639 language descriptor */
502  len_ptr = q++;
503  *len_ptr = 0;
504 
505  for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
506  if (q - data > SECTION_LENGTH - 4) {
507  err = 1;
508  break;
509  }
510  next = strchr(p, ',');
511  if (strlen(p) != 3 && (!next || next != p + 3))
512  continue; /* not a 3-letter code */
513 
514  *q++ = *p++;
515  *q++ = *p++;
516  *q++ = *p++;
517 
519  *q++ = 0x01;
521  *q++ = 0x02;
523  *q++ = 0x03;
524  else
525  *q++ = 0; /* undefined type */
526 
527  *len_ptr += 4;
528  }
529 
530  if (*len_ptr == 0)
531  q -= 2; /* no language codes were written */
532  }
533  break;
535  {
536  const char default_language[] = "und";
537  const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
538 
540  uint8_t *len_ptr;
541  int extradata_copied = 0;
542 
543  *q++ = 0x59; /* subtitling_descriptor */
544  len_ptr = q++;
545 
546  while (strlen(language) >= 3) {
547  if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
548  err = 1;
549  break;
550  }
551  *q++ = *language++;
552  *q++ = *language++;
553  *q++ = *language++;
554  /* Skip comma */
555  if (*language != '\0')
556  language++;
557 
558  if (st->codecpar->extradata_size - extradata_copied >= 5) {
559  *q++ = st->codecpar->extradata[extradata_copied + 4]; /* subtitling_type */
560  memcpy(q, st->codecpar->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
561  extradata_copied += 5;
562  q += 4;
563  } else {
564  /* subtitling_type:
565  * 0x10 - normal with no monitor aspect ratio criticality
566  * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
567  *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
568  if ((st->codecpar->extradata_size == 4) && (extradata_copied == 0)) {
569  /* support of old 4-byte extradata format */
570  memcpy(q, st->codecpar->extradata, 4); /* composition_page_id and ancillary_page_id */
571  extradata_copied += 4;
572  q += 4;
573  } else {
574  put16(&q, 1); /* composition_page_id */
575  put16(&q, 1); /* ancillary_page_id */
576  }
577  }
578  }
579 
580  *len_ptr = q - len_ptr - 1;
581  } else if (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
582  uint8_t *len_ptr = NULL;
583  int extradata_copied = 0;
584 
585  /* The descriptor tag. teletext_descriptor */
586  *q++ = 0x56;
587  len_ptr = q++;
588 
589  while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
590  *q++ = *language++;
591  *q++ = *language++;
592  *q++ = *language++;
593  /* Skip comma */
594  if (*language != '\0')
595  language++;
596 
597  if (st->codecpar->extradata_size - 1 > extradata_copied) {
598  memcpy(q, st->codecpar->extradata + extradata_copied, 2);
599  extradata_copied += 2;
600  q += 2;
601  } else {
602  /* The Teletext descriptor:
603  * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
604  * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
605  * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
606  *q++ = 0x08;
607  *q++ = 0x00;
608  }
609  }
610 
611  *len_ptr = q - len_ptr - 1;
612  }
613  }
614  break;
615  case AVMEDIA_TYPE_VIDEO:
616  if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
617  *q++ = 0x05; /*MPEG-2 registration descriptor*/
618  *q++ = 4;
619  *q++ = 'd';
620  *q++ = 'r';
621  *q++ = 'a';
622  *q++ = 'c';
623  } else if (stream_type == STREAM_TYPE_VIDEO_VC1) {
624  *q++ = 0x05; /*MPEG-2 registration descriptor*/
625  *q++ = 4;
626  *q++ = 'V';
627  *q++ = 'C';
628  *q++ = '-';
629  *q++ = '1';
630  }
631  break;
632  case AVMEDIA_TYPE_DATA:
634  *q++ = 0x05; /* MPEG-2 registration descriptor */
635  *q++ = 4;
636  *q++ = 'K';
637  *q++ = 'L';
638  *q++ = 'V';
639  *q++ = 'A';
640  } else if (st->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {
641  const char *tag = "ID3 ";
642  *q++ = 0x26; /* metadata descriptor */
643  *q++ = 13;
644  put16(&q, 0xffff); /* metadata application format */
645  putstr8(&q, tag, 0);
646  *q++ = 0xff; /* metadata format */
647  putstr8(&q, tag, 0);
648  *q++ = 0; /* metadata service ID */
649  *q++ = 0xF; /* metadata_locator_record_flag|MPEG_carriage_flags|reserved */
650  }
651  break;
652  }
653 
654  val = 0xf000 | (q - desc_length_ptr - 2);
655  desc_length_ptr[0] = val >> 8;
656  desc_length_ptr[1] = val;
657  }
658 
659  if (err)
660  av_log(s, AV_LOG_ERROR,
661  "The PMT section cannot fit stream %d and all following streams.\n"
662  "Try reducing the number of languages in the audio streams "
663  "or the total number of streams.\n", i);
664 
665  mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
666  data, q - data);
667  return 0;
668 }
669 
671 {
672  MpegTSWrite *ts = s->priv_data;
673  MpegTSService *service;
674  uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
675  int i, running_status, free_ca_mode, val;
676 
677  q = data;
678  put16(&q, ts->onid);
679  *q++ = 0xff;
680  for (i = 0; i < ts->nb_services; i++) {
681  service = ts->services[i];
682  put16(&q, service->sid);
683  *q++ = 0xfc | 0x00; /* currently no EIT info */
684  desc_list_len_ptr = q;
685  q += 2;
686  running_status = 4; /* running */
687  free_ca_mode = 0;
688 
689  /* write only one descriptor for the service name and provider */
690  *q++ = 0x48;
691  desc_len_ptr = q;
692  q++;
693  *q++ = ts->service_type;
694  putstr8(&q, service->provider_name, 1);
695  putstr8(&q, service->name, 1);
696  desc_len_ptr[0] = q - desc_len_ptr - 1;
697 
698  /* fill descriptor length */
699  val = (running_status << 13) | (free_ca_mode << 12) |
700  (q - desc_list_len_ptr - 2);
701  desc_list_len_ptr[0] = val >> 8;
702  desc_list_len_ptr[1] = val;
703  }
704  mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, ts->tables_version, 0, 0,
705  data, q - data);
706 }
707 
709  const char *provider_name,
710  const char *name)
711 {
712  MpegTSService *service;
713 
714  service = av_mallocz(sizeof(MpegTSService));
715  if (!service)
716  return NULL;
717  service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
718  service->sid = sid;
719  service->pcr_pid = 0x1fff;
720  service->provider_name = av_strdup(provider_name);
721  service->name = av_strdup(name);
722  if (!service->provider_name || !service->name)
723  goto fail;
724  if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
725  goto fail;
726 
727  return service;
728 fail:
729  av_freep(&service->provider_name);
730  av_freep(&service->name);
731  av_free(service);
732  return NULL;
733 }
734 
735 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
736 {
737  return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
738  ts->first_pcr;
739 }
740 
742 {
743  MpegTSWrite *ts = s->priv_data;
744  if (ts->m2ts_mode) {
745  int64_t pcr = get_pcr(s->priv_data, s->pb);
746  uint32_t tp_extra_header = pcr % 0x3fffffff;
747  tp_extra_header = AV_RB32(&tp_extra_header);
748  avio_write(s->pb, (unsigned char *) &tp_extra_header,
749  sizeof(tp_extra_header));
750  }
751 }
752 
753 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
754 {
755  AVFormatContext *ctx = s->opaque;
757  avio_write(ctx->pb, packet, TS_PACKET_SIZE);
758 }
759 
761 {
762  MpegTSWrite *ts = s->priv_data;
763  MpegTSWriteStream *ts_st;
764  MpegTSService *service;
765  AVStream *st, *pcr_st = NULL;
766  AVDictionaryEntry *title, *provider;
767  int i, j;
768  const char *service_name;
769  const char *provider_name;
770  int *pids;
771  int ret;
772 
773  if (s->max_delay < 0) /* Not set by the caller */
774  s->max_delay = 0;
775 
776  // round up to a whole number of TS packets
777  ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
778 
779  ts->tsid = ts->transport_stream_id;
780  ts->onid = ts->original_network_id;
781  if (!s->nb_programs) {
782  /* allocate a single DVB service */
783  title = av_dict_get(s->metadata, "service_name", NULL, 0);
784  if (!title)
785  title = av_dict_get(s->metadata, "title", NULL, 0);
786  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
787  provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
788  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
789  service = mpegts_add_service(ts, ts->service_id,
790  provider_name, service_name);
791 
792  if (!service)
793  return AVERROR(ENOMEM);
794 
796  service->pmt.opaque = s;
797  service->pmt.cc = 15;
798  service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
799  } else {
800  for (i = 0; i < s->nb_programs; i++) {
801  AVProgram *program = s->programs[i];
802  title = av_dict_get(program->metadata, "service_name", NULL, 0);
803  if (!title)
804  title = av_dict_get(program->metadata, "title", NULL, 0);
805  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
806  provider = av_dict_get(program->metadata, "service_provider", NULL, 0);
807  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
808  service = mpegts_add_service(ts, program->id,
809  provider_name, service_name);
810 
811  if (!service)
812  return AVERROR(ENOMEM);
813 
815  service->pmt.opaque = s;
816  service->pmt.cc = 15;
817  service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
818  service->program = program;
819  }
820  }
821 
822  ts->pat.pid = PAT_PID;
823  /* Initialize at 15 so that it wraps and is equal to 0 for the
824  * first packet we write. */
825  ts->pat.cc = 15;
828  ts->pat.opaque = s;
829 
830  ts->sdt.pid = SDT_PID;
831  ts->sdt.cc = 15;
834  ts->sdt.opaque = s;
835 
836  pids = av_malloc_array(s->nb_streams, sizeof(*pids));
837  if (!pids) {
838  ret = AVERROR(ENOMEM);
839  goto fail;
840  }
841 
842  /* assign pids to each stream */
843  for (i = 0; i < s->nb_streams; i++) {
845  st = s->streams[i];
846 
847  ts_st = av_mallocz(sizeof(MpegTSWriteStream));
848  if (!ts_st) {
849  ret = AVERROR(ENOMEM);
850  goto fail;
851  }
852  st->priv_data = ts_st;
853 
854  ts_st->user_tb = st->time_base;
855  avpriv_set_pts_info(st, 33, 1, 90000);
856 
857  ts_st->payload = av_mallocz(ts->pes_payload_size);
858  if (!ts_st->payload) {
859  ret = AVERROR(ENOMEM);
860  goto fail;
861  }
862 
863  program = av_find_program_from_stream(s, NULL, i);
864  if (program) {
865  for (j = 0; j < ts->nb_services; j++) {
866  if (ts->services[j]->program == program) {
867  service = ts->services[j];
868  break;
869  }
870  }
871  }
872 
873  ts_st->service = service;
874  /* MPEG pid values < 16 are reserved. Applications which set st->id in
875  * this range are assigned a calculated pid. */
876  if (st->id < 16) {
877  ts_st->pid = ts->start_pid + i;
878  } else if (st->id < 0x1FFF) {
879  ts_st->pid = st->id;
880  } else {
881  av_log(s, AV_LOG_ERROR,
882  "Invalid stream id %d, must be less than 8191\n", st->id);
883  ret = AVERROR(EINVAL);
884  goto fail;
885  }
886  if (ts_st->pid == service->pmt.pid) {
887  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
888  ret = AVERROR(EINVAL);
889  goto fail;
890  }
891  for (j = 0; j < i; j++) {
892  if (pids[j] == ts_st->pid) {
893  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
894  ret = AVERROR(EINVAL);
895  goto fail;
896  }
897  }
898  pids[i] = ts_st->pid;
899  ts_st->payload_pts = AV_NOPTS_VALUE;
900  ts_st->payload_dts = AV_NOPTS_VALUE;
901  ts_st->first_pts_check = 1;
902  ts_st->cc = 15;
903  ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
904  /* update PCR pid by using the first video stream */
905  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
906  service->pcr_pid == 0x1fff) {
907  service->pcr_pid = ts_st->pid;
908  pcr_st = st;
909  }
910  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
911  st->codecpar->extradata_size > 0) {
912  AVStream *ast;
913  ts_st->amux = avformat_alloc_context();
914  if (!ts_st->amux) {
915  ret = AVERROR(ENOMEM);
916  goto fail;
917  }
918  ts_st->amux->oformat =
919  av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
920  NULL, NULL);
921  if (!ts_st->amux->oformat) {
922  ret = AVERROR(EINVAL);
923  goto fail;
924  }
925  if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
926  ret = AVERROR(ENOMEM);
927  goto fail;
928  }
929  ret = avcodec_parameters_copy(ast->codecpar, st->codecpar);
930  if (ret != 0)
931  goto fail;
932  ast->time_base = st->time_base;
933  ret = avformat_write_header(ts_st->amux, NULL);
934  if (ret < 0)
935  goto fail;
936  }
937  if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
939  }
940  }
941 
942  av_freep(&pids);
943 
944  /* if no video stream, use the first stream as PCR */
945  if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
946  pcr_st = s->streams[0];
947  ts_st = pcr_st->priv_data;
948  service->pcr_pid = ts_st->pid;
949  } else
950  ts_st = pcr_st->priv_data;
951 
952  if (ts->mux_rate > 1) {
953  service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period /
954  (TS_PACKET_SIZE * 8 * 1000);
955  ts->sdt_packet_period = (int64_t)ts->mux_rate * SDT_RETRANS_TIME /
956  (TS_PACKET_SIZE * 8 * 1000);
957  ts->pat_packet_period = (int64_t)ts->mux_rate * PAT_RETRANS_TIME /
958  (TS_PACKET_SIZE * 8 * 1000);
959 
960  if (ts->copyts < 1)
962  } else {
963  /* Arbitrary values, PAT/PMT will also be written on video key frames */
964  ts->sdt_packet_period = 200;
965  ts->pat_packet_period = 40;
966  if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
968  if (!frame_size) {
969  av_log(s, AV_LOG_WARNING, "frame size not set\n");
970  service->pcr_packet_period =
971  pcr_st->codecpar->sample_rate / (10 * 512);
972  } else {
973  service->pcr_packet_period =
974  pcr_st->codecpar->sample_rate / (10 * frame_size);
975  }
976  } else {
977  // max delta PCR 0.1s
978  // TODO: should be avg_frame_rate
979  service->pcr_packet_period =
980  ts_st->user_tb.den / (10 * ts_st->user_tb.num);
981  }
982  if (!service->pcr_packet_period)
983  service->pcr_packet_period = 1;
984  }
985 
988  // The user specified a period, use only it
989  if (ts->pat_period < INT_MAX/2) {
990  ts->pat_packet_period = INT_MAX;
991  }
992  if (ts->sdt_period < INT_MAX/2) {
993  ts->sdt_packet_period = INT_MAX;
994  }
995 
996  // output a PCR as soon as possible
997  service->pcr_packet_count = service->pcr_packet_period;
998  ts->pat_packet_count = ts->pat_packet_period - 1;
999  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1000 
1001  if (ts->mux_rate == 1)
1002  av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
1003  else
1004  av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
1006  "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
1007  service->pcr_packet_period,
1009 
1010  if (ts->m2ts_mode == -1) {
1011  if (av_match_ext(s->filename, "m2ts")) {
1012  ts->m2ts_mode = 1;
1013  } else {
1014  ts->m2ts_mode = 0;
1015  }
1016  }
1017 
1018  return 0;
1019 
1020 fail:
1021  av_freep(&pids);
1022  return ret;
1023 }
1024 
1025 /* send SDT, PAT and PMT tables regularly */
1026 static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
1027 {
1028  MpegTSWrite *ts = s->priv_data;
1029  int i;
1030 
1031  if (++ts->sdt_packet_count == ts->sdt_packet_period ||
1032  (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
1033  (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
1034  ) {
1035  ts->sdt_packet_count = 0;
1036  if (dts != AV_NOPTS_VALUE)
1037  ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
1038  mpegts_write_sdt(s);
1039  }
1040  if (++ts->pat_packet_count == ts->pat_packet_period ||
1041  (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
1042  (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
1043  force_pat) {
1044  ts->pat_packet_count = 0;
1045  if (dts != AV_NOPTS_VALUE)
1046  ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
1047  mpegts_write_pat(s);
1048  for (i = 0; i < ts->nb_services; i++)
1049  mpegts_write_pmt(s, ts->services[i]);
1050  }
1051 }
1052 
1053 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
1054 {
1055  int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
1056 
1057  *buf++ = pcr_high >> 25;
1058  *buf++ = pcr_high >> 17;
1059  *buf++ = pcr_high >> 9;
1060  *buf++ = pcr_high >> 1;
1061  *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
1062  *buf++ = pcr_low;
1063 
1064  return 6;
1065 }
1066 
1067 /* Write a single null transport stream packet */
1069 {
1070  uint8_t *q;
1072 
1073  q = buf;
1074  *q++ = 0x47;
1075  *q++ = 0x00 | 0x1f;
1076  *q++ = 0xff;
1077  *q++ = 0x10;
1078  memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
1080  avio_write(s->pb, buf, TS_PACKET_SIZE);
1081 }
1082 
1083 /* Write a single transport stream packet with a PCR and no payload */
1085 {
1086  MpegTSWrite *ts = s->priv_data;
1087  MpegTSWriteStream *ts_st = st->priv_data;
1088  uint8_t *q;
1090 
1091  q = buf;
1092  *q++ = 0x47;
1093  *q++ = ts_st->pid >> 8;
1094  *q++ = ts_st->pid;
1095  *q++ = 0x20 | ts_st->cc; /* Adaptation only */
1096  /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
1097  *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
1098  *q++ = 0x10; /* Adaptation flags: PCR present */
1099  if (ts_st->discontinuity) {
1100  q[-1] |= 0x80;
1101  ts_st->discontinuity = 0;
1102  }
1103 
1104  /* PCR coded into 6 bytes */
1105  q += write_pcr_bits(q, get_pcr(ts, s->pb));
1106 
1107  /* stuffing bytes */
1108  memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
1110  avio_write(s->pb, buf, TS_PACKET_SIZE);
1111 }
1112 
1113 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
1114 {
1115  int val;
1116 
1117  val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
1118  *q++ = val;
1119  val = (((pts >> 15) & 0x7fff) << 1) | 1;
1120  *q++ = val >> 8;
1121  *q++ = val;
1122  val = (((pts) & 0x7fff) << 1) | 1;
1123  *q++ = val >> 8;
1124  *q++ = val;
1125 }
1126 
1127 /* Set an adaptation field flag in an MPEG-TS packet*/
1128 static void set_af_flag(uint8_t *pkt, int flag)
1129 {
1130  // expect at least one flag to set
1131  av_assert0(flag);
1132 
1133  if ((pkt[3] & 0x20) == 0) {
1134  // no AF yet, set adaptation field flag
1135  pkt[3] |= 0x20;
1136  // 1 byte length, no flags
1137  pkt[4] = 1;
1138  pkt[5] = 0;
1139  }
1140  pkt[5] |= flag;
1141 }
1142 
1143 /* Extend the adaptation field by size bytes */
1144 static void extend_af(uint8_t *pkt, int size)
1145 {
1146  // expect already existing adaptation field
1147  av_assert0(pkt[3] & 0x20);
1148  pkt[4] += size;
1149 }
1150 
1151 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
1153 {
1154  if (pkt[3] & 0x20)
1155  return pkt + 5 + pkt[4];
1156  else
1157  return pkt + 4;
1158 }
1159 
1160 /* Add a PES header to the front of the payload, and segment into an integer
1161  * number of TS packets. The final TS packet is padded using an oversized
1162  * adaptation header to exactly fill the last TS packet.
1163  * NOTE: 'payload' contains a complete PES payload. */
1165  const uint8_t *payload, int payload_size,
1166  int64_t pts, int64_t dts, int key, int stream_id)
1167 {
1168  MpegTSWriteStream *ts_st = st->priv_data;
1169  MpegTSWrite *ts = s->priv_data;
1171  uint8_t *q;
1172  int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
1173  int afc_len, stuffing_len;
1174  int64_t pcr = -1; /* avoid warning */
1175  int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
1176  int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
1177 
1178  av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
1180  force_pat = 1;
1181  }
1182 
1183  is_start = 1;
1184  while (payload_size > 0) {
1185  retransmit_si_info(s, force_pat, dts);
1186  force_pat = 0;
1187 
1188  write_pcr = 0;
1189  if (ts_st->pid == ts_st->service->pcr_pid) {
1190  if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
1191  ts_st->service->pcr_packet_count++;
1192  if (ts_st->service->pcr_packet_count >=
1193  ts_st->service->pcr_packet_period) {
1194  ts_st->service->pcr_packet_count = 0;
1195  write_pcr = 1;
1196  }
1197  }
1198 
1199  if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
1200  (dts - get_pcr(ts, s->pb) / 300) > delay) {
1201  /* pcr insert gets priority over null packet insert */
1202  if (write_pcr)
1203  mpegts_insert_pcr_only(s, st);
1204  else
1206  /* recalculate write_pcr and possibly retransmit si_info */
1207  continue;
1208  }
1209 
1210  /* prepare packet header */
1211  q = buf;
1212  *q++ = 0x47;
1213  val = ts_st->pid >> 8;
1214  if (is_start)
1215  val |= 0x40;
1216  *q++ = val;
1217  *q++ = ts_st->pid;
1218  ts_st->cc = ts_st->cc + 1 & 0xf;
1219  *q++ = 0x10 | ts_st->cc; // payload indicator + CC
1220  if (ts_st->discontinuity) {
1221  set_af_flag(buf, 0x80);
1222  q = get_ts_payload_start(buf);
1223  ts_st->discontinuity = 0;
1224  }
1225  if (key && is_start && pts != AV_NOPTS_VALUE) {
1226  // set Random Access for key frames
1227  if (ts_st->pid == ts_st->service->pcr_pid)
1228  write_pcr = 1;
1229  set_af_flag(buf, 0x40);
1230  q = get_ts_payload_start(buf);
1231  }
1232  if (write_pcr) {
1233  set_af_flag(buf, 0x10);
1234  q = get_ts_payload_start(buf);
1235  // add 11, pcr references the last byte of program clock reference base
1236  if (ts->mux_rate > 1)
1237  pcr = get_pcr(ts, s->pb);
1238  else
1239  pcr = (dts - delay) * 300;
1240  if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
1241  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
1242  extend_af(buf, write_pcr_bits(q, pcr));
1243  q = get_ts_payload_start(buf);
1244  }
1245  if (is_start) {
1246  int pes_extension = 0;
1247  int pes_header_stuffing_bytes = 0;
1248  /* write PES header */
1249  *q++ = 0x00;
1250  *q++ = 0x00;
1251  *q++ = 0x01;
1252  is_dvb_subtitle = 0;
1253  is_dvb_teletext = 0;
1254  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1255  if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)
1256  *q++ = 0xfd;
1257  else
1258  *q++ = 0xe0;
1259  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1260  (st->codecpar->codec_id == AV_CODEC_ID_MP2 ||
1261  st->codecpar->codec_id == AV_CODEC_ID_MP3 ||
1262  st->codecpar->codec_id == AV_CODEC_ID_AAC)) {
1263  *q++ = 0xc0;
1264  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1265  st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
1266  ts->m2ts_mode) {
1267  *q++ = 0xfd;
1268  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1270  *q++ = 0xbd;
1271  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
1272  *q++ = stream_id != -1 ? stream_id : 0xfc;
1273 
1274  if (stream_id == 0xbd) /* asynchronous KLV */
1275  pts = dts = AV_NOPTS_VALUE;
1276  } else {
1277  *q++ = 0xbd;
1280  is_dvb_subtitle = 1;
1281  } else if (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
1282  is_dvb_teletext = 1;
1283  }
1284  }
1285  }
1286  header_len = 0;
1287  flags = 0;
1288  if (pts != AV_NOPTS_VALUE) {
1289  header_len += 5;
1290  flags |= 0x80;
1291  }
1292  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1293  header_len += 5;
1294  flags |= 0x40;
1295  }
1296  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1298  /* set PES_extension_flag */
1299  pes_extension = 1;
1300  flags |= 0x01;
1301 
1302  /* One byte for PES2 extension flag +
1303  * one byte for extension length +
1304  * one byte for extension id */
1305  header_len += 3;
1306  }
1307  /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
1308  * otherwise it will not play sound on blu-ray
1309  */
1310  if (ts->m2ts_mode &&
1312  st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1313  /* set PES_extension_flag */
1314  pes_extension = 1;
1315  flags |= 0x01;
1316  header_len += 3;
1317  }
1318  if (is_dvb_teletext) {
1319  pes_header_stuffing_bytes = 0x24 - header_len;
1320  header_len = 0x24;
1321  }
1322  len = payload_size + header_len + 3;
1323  /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
1324  if (is_dvb_subtitle) {
1325  len += 3;
1326  payload_size++;
1327  }
1328  if (len > 0xffff)
1329  len = 0;
1331  len = 0;
1332  }
1333  *q++ = len >> 8;
1334  *q++ = len;
1335  val = 0x80;
1336  /* data alignment indicator is required for subtitle and data streams */
1338  val |= 0x04;
1339  *q++ = val;
1340  *q++ = flags;
1341  *q++ = header_len;
1342  if (pts != AV_NOPTS_VALUE) {
1343  write_pts(q, flags >> 6, pts);
1344  q += 5;
1345  }
1346  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1347  write_pts(q, 1, dts);
1348  q += 5;
1349  }
1350  if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
1351  flags = 0x01; /* set PES_extension_flag_2 */
1352  *q++ = flags;
1353  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1354  /* Set the stream ID extension flag bit to 0 and
1355  * write the extended stream ID. */
1356  *q++ = 0x00 | 0x60;
1357  }
1358  /* For Blu-ray AC3 Audio Setting extended flags */
1359  if (ts->m2ts_mode &&
1360  pes_extension &&
1361  st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1362  flags = 0x01; /* set PES_extension_flag_2 */
1363  *q++ = flags;
1364  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1365  *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1366  }
1367 
1368 
1369  if (is_dvb_subtitle) {
1370  /* First two fields of DVB subtitles PES data:
1371  * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
1372  * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
1373  *q++ = 0x20;
1374  *q++ = 0x00;
1375  }
1376  if (is_dvb_teletext) {
1377  memset(q, 0xff, pes_header_stuffing_bytes);
1378  q += pes_header_stuffing_bytes;
1379  }
1380  is_start = 0;
1381  }
1382  /* header size */
1383  header_len = q - buf;
1384  /* data len */
1385  len = TS_PACKET_SIZE - header_len;
1386  if (len > payload_size)
1387  len = payload_size;
1388  stuffing_len = TS_PACKET_SIZE - header_len - len;
1389  if (stuffing_len > 0) {
1390  /* add stuffing with AFC */
1391  if (buf[3] & 0x20) {
1392  /* stuffing already present: increase its size */
1393  afc_len = buf[4] + 1;
1394  memmove(buf + 4 + afc_len + stuffing_len,
1395  buf + 4 + afc_len,
1396  header_len - (4 + afc_len));
1397  buf[4] += stuffing_len;
1398  memset(buf + 4 + afc_len, 0xff, stuffing_len);
1399  } else {
1400  /* add stuffing */
1401  memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1402  buf[3] |= 0x20;
1403  buf[4] = stuffing_len - 1;
1404  if (stuffing_len >= 2) {
1405  buf[5] = 0x00;
1406  memset(buf + 6, 0xff, stuffing_len - 2);
1407  }
1408  }
1409  }
1410 
1411  if (is_dvb_subtitle && payload_size == len) {
1412  memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
1413  buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
1414  } else {
1415  memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1416  }
1417 
1418  payload += len;
1419  payload_size -= len;
1421  avio_write(s->pb, buf, TS_PACKET_SIZE);
1422  }
1423  ts_st->prev_payload_key = key;
1424 }
1425 
1427 {
1428  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1429  if (!st->nb_frames) {
1430  av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1431  "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1432  "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1433  return AVERROR_INVALIDDATA;
1434  }
1435  av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing, size %d", pkt->size);
1436  if (pkt->size) av_log(s, AV_LOG_WARNING, " data %08X", AV_RB32(pkt->data));
1437  av_log(s, AV_LOG_WARNING, "\n");
1438  }
1439  return 0;
1440 }
1441 
1443 {
1444  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1445  if (!st->nb_frames) {
1446  av_log(s, AV_LOG_ERROR, "HEVC bitstream malformed, no startcode found\n");
1447  return AVERROR_PATCHWELCOME;
1448  }
1449  av_log(s, AV_LOG_WARNING, "HEVC bitstream error, startcode missing, size %d", pkt->size);
1450  if (pkt->size) av_log(s, AV_LOG_WARNING, " data %08X", AV_RB32(pkt->data));
1451  av_log(s, AV_LOG_WARNING, "\n");
1452  }
1453  return 0;
1454 }
1455 
1456 /* Based on GStreamer's gst-plugins-base/ext/ogg/gstoggstream.c
1457  * Released under the LGPL v2.1+, written by
1458  * Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
1459  */
1461 {
1462  static const int durations[32] = {
1463  480, 960, 1920, 2880, /* Silk NB */
1464  480, 960, 1920, 2880, /* Silk MB */
1465  480, 960, 1920, 2880, /* Silk WB */
1466  480, 960, /* Hybrid SWB */
1467  480, 960, /* Hybrid FB */
1468  120, 240, 480, 960, /* CELT NB */
1469  120, 240, 480, 960, /* CELT NB */
1470  120, 240, 480, 960, /* CELT NB */
1471  120, 240, 480, 960, /* CELT NB */
1472  };
1473  int toc, frame_duration, nframes, duration;
1474 
1475  if (pkt->size < 1)
1476  return 0;
1477 
1478  toc = pkt->data[0];
1479 
1480  frame_duration = durations[toc >> 3];
1481  switch (toc & 3) {
1482  case 0:
1483  nframes = 1;
1484  break;
1485  case 1:
1486  nframes = 2;
1487  break;
1488  case 2:
1489  nframes = 2;
1490  break;
1491  case 3:
1492  if (pkt->size < 2)
1493  return 0;
1494  nframes = pkt->data[1] & 63;
1495  break;
1496  }
1497 
1498  duration = nframes * frame_duration;
1499  if (duration > 5760) {
1501  "Opus packet duration > 120 ms, invalid");
1502  return 0;
1503  }
1504 
1505  return duration;
1506 }
1507 
1509 {
1510  AVStream *st = s->streams[pkt->stream_index];
1511  int size = pkt->size;
1512  uint8_t *buf = pkt->data;
1513  uint8_t *data = NULL;
1514  MpegTSWrite *ts = s->priv_data;
1515  MpegTSWriteStream *ts_st = st->priv_data;
1516  const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1517  int64_t dts = pkt->dts, pts = pkt->pts;
1518  int opus_samples = 0;
1519  int side_data_size;
1520  char *side_data = NULL;
1521  int stream_id = -1;
1522 
1523  side_data = av_packet_get_side_data(pkt,
1525  &side_data_size);
1526  if (side_data)
1527  stream_id = side_data[0];
1528 
1529  if (ts->reemit_pat_pmt) {
1531  "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1532  ts->reemit_pat_pmt = 0;
1534  }
1535 
1536  if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1537  ts->pat_packet_count = ts->pat_packet_period - 1;
1538  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1540  }
1541 
1542  if (ts->copyts < 1) {
1543  if (pts != AV_NOPTS_VALUE)
1544  pts += delay;
1545  if (dts != AV_NOPTS_VALUE)
1546  dts += delay;
1547  }
1548 
1549  if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1550  av_log(s, AV_LOG_ERROR, "first pts value must be set\n");
1551  return AVERROR_INVALIDDATA;
1552  }
1553  ts_st->first_pts_check = 0;
1554 
1555  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1556  const uint8_t *p = buf, *buf_end = p + size;
1557  uint32_t state = -1;
1558  int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
1559  int ret = ff_check_h264_startcode(s, st, pkt);
1560  if (ret < 0)
1561  return ret;
1562 
1563  if (extradd && AV_RB24(st->codecpar->extradata) > 1)
1564  extradd = 0;
1565 
1566  do {
1567  p = avpriv_find_start_code(p, buf_end, &state);
1568  av_log(s, AV_LOG_TRACE, "nal %d\n", state & 0x1f);
1569  if ((state & 0x1f) == 7)
1570  extradd = 0;
1571  } while (p < buf_end && (state & 0x1f) != 9 &&
1572  (state & 0x1f) != 5 && (state & 0x1f) != 1);
1573 
1574  if ((state & 0x1f) != 5)
1575  extradd = 0;
1576  if ((state & 0x1f) != 9) { // AUD NAL
1577  data = av_malloc(pkt->size + 6 + extradd);
1578  if (!data)
1579  return AVERROR(ENOMEM);
1580  memcpy(data + 6, st->codecpar->extradata, extradd);
1581  memcpy(data + 6 + extradd, pkt->data, pkt->size);
1582  AV_WB32(data, 0x00000001);
1583  data[4] = 0x09;
1584  data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1585  buf = data;
1586  size = pkt->size + 6 + extradd;
1587  }
1588  } else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
1589  if (pkt->size < 2) {
1590  av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1591  return AVERROR_INVALIDDATA;
1592  }
1593  if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1594  int ret;
1595  AVPacket pkt2;
1596 
1597  if (!ts_st->amux) {
1598  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1599  "and extradata missing\n");
1600  } else {
1601  av_init_packet(&pkt2);
1602  pkt2.data = pkt->data;
1603  pkt2.size = pkt->size;
1604  av_assert0(pkt->dts != AV_NOPTS_VALUE);
1605  pkt2.dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
1606 
1607  ret = avio_open_dyn_buf(&ts_st->amux->pb);
1608  if (ret < 0)
1609  return AVERROR(ENOMEM);
1610 
1611  ret = av_write_frame(ts_st->amux, &pkt2);
1612  if (ret < 0) {
1613  ffio_free_dyn_buf(&ts_st->amux->pb);
1614  return ret;
1615  }
1616  size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1617  ts_st->amux->pb = NULL;
1618  buf = data;
1619  }
1620  }
1621  } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
1622  int ret = check_hevc_startcode(s, st, pkt);
1623  if (ret < 0)
1624  return ret;
1625  } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
1626  if (pkt->size < 2) {
1627  av_log(s, AV_LOG_ERROR, "Opus packet too short\n");
1628  return AVERROR_INVALIDDATA;
1629  }
1630 
1631  /* Add Opus control header */
1632  if ((AV_RB16(pkt->data) >> 5) != 0x3ff) {
1633  uint8_t *side_data;
1634  int side_data_size;
1635  int i, n;
1636  int ctrl_header_size;
1637  int trim_start = 0, trim_end = 0;
1638 
1639  opus_samples = opus_get_packet_samples(s, pkt);
1640 
1641  side_data = av_packet_get_side_data(pkt,
1643  &side_data_size);
1644 
1645  if (side_data && side_data_size >= 10) {
1646  trim_end = AV_RL32(side_data + 4) * 48000 / st->codecpar->sample_rate;
1647  }
1648 
1649  ctrl_header_size = pkt->size + 2 + pkt->size / 255 + 1;
1650  if (ts_st->opus_pending_trim_start)
1651  ctrl_header_size += 2;
1652  if (trim_end)
1653  ctrl_header_size += 2;
1654 
1655  data = av_malloc(ctrl_header_size);
1656  if (!data)
1657  return AVERROR(ENOMEM);
1658 
1659  data[0] = 0x7f;
1660  data[1] = 0xe0;
1661  if (ts_st->opus_pending_trim_start)
1662  data[1] |= 0x10;
1663  if (trim_end)
1664  data[1] |= 0x08;
1665 
1666  n = pkt->size;
1667  i = 2;
1668  do {
1669  data[i] = FFMIN(n, 255);
1670  n -= 255;
1671  i++;
1672  } while (n >= 0);
1673 
1674  av_assert0(2 + pkt->size / 255 + 1 == i);
1675 
1676  if (ts_st->opus_pending_trim_start) {
1677  trim_start = FFMIN(ts_st->opus_pending_trim_start, opus_samples);
1678  AV_WB16(data + i, trim_start);
1679  i += 2;
1680  ts_st->opus_pending_trim_start -= trim_start;
1681  }
1682  if (trim_end) {
1683  trim_end = FFMIN(trim_end, opus_samples - trim_start);
1684  AV_WB16(data + i, trim_end);
1685  i += 2;
1686  }
1687 
1688  memcpy(data + i, pkt->data, pkt->size);
1689  buf = data;
1690  size = ctrl_header_size;
1691  } else {
1692  /* TODO: Can we get TS formatted data here? If so we will
1693  * need to count the samples of that too! */
1694  av_log(s, AV_LOG_WARNING, "Got MPEG-TS formatted Opus data, unhandled");
1695  }
1696  }
1697 
1698  if (pkt->dts != AV_NOPTS_VALUE) {
1699  int i;
1700  for(i=0; i<s->nb_streams; i++) {
1701  AVStream *st2 = s->streams[i];
1702  MpegTSWriteStream *ts_st2 = st2->priv_data;
1703  if ( ts_st2->payload_size
1704  && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
1705  mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
1706  ts_st2->payload_pts, ts_st2->payload_dts,
1707  ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
1708  ts_st2->payload_size = 0;
1709  }
1710  }
1711  }
1712 
1713  if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
1714  (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
1715  av_compare_ts(dts - ts_st->payload_dts, st->time_base,
1716  s->max_delay, AV_TIME_BASE_Q) >= 0) ||
1717  ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) {
1718  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1719  ts_st->payload_pts, ts_st->payload_dts,
1720  ts_st->payload_flags & AV_PKT_FLAG_KEY, stream_id);
1721  ts_st->payload_size = 0;
1722  ts_st->opus_queued_samples = 0;
1723  }
1724 
1725  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
1726  av_assert0(!ts_st->payload_size);
1727  // for video and subtitle, write a single pes packet
1728  mpegts_write_pes(s, st, buf, size, pts, dts,
1729  pkt->flags & AV_PKT_FLAG_KEY, stream_id);
1730  ts_st->opus_queued_samples = 0;
1731  av_free(data);
1732  return 0;
1733  }
1734 
1735  if (!ts_st->payload_size) {
1736  ts_st->payload_pts = pts;
1737  ts_st->payload_dts = dts;
1738  ts_st->payload_flags = pkt->flags;
1739  }
1740 
1741  memcpy(ts_st->payload + ts_st->payload_size, buf, size);
1742  ts_st->payload_size += size;
1743  ts_st->opus_queued_samples += opus_samples;
1744 
1745  av_free(data);
1746 
1747  return 0;
1748 }
1749 
1751 {
1752  int i;
1753 
1754  /* flush current packets */
1755  for (i = 0; i < s->nb_streams; i++) {
1756  AVStream *st = s->streams[i];
1757  MpegTSWriteStream *ts_st = st->priv_data;
1758  if (ts_st->payload_size > 0) {
1759  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1760  ts_st->payload_pts, ts_st->payload_dts,
1761  ts_st->payload_flags & AV_PKT_FLAG_KEY, -1);
1762  ts_st->payload_size = 0;
1763  ts_st->opus_queued_samples = 0;
1764  }
1765  }
1766 }
1767 
1769 {
1770  if (!pkt) {
1771  mpegts_write_flush(s);
1772  return 1;
1773  } else {
1774  return mpegts_write_packet_internal(s, pkt);
1775  }
1776 }
1777 
1779 {
1780  if (s->pb)
1781  mpegts_write_flush(s);
1782 
1783  return 0;
1784 }
1785 
1787 {
1788  MpegTSWrite *ts = s->priv_data;
1789  MpegTSService *service;
1790  int i;
1791 
1792  for (i = 0; i < s->nb_streams; i++) {
1793  AVStream *st = s->streams[i];
1794  MpegTSWriteStream *ts_st = st->priv_data;
1795  if (ts_st) {
1796  av_freep(&ts_st->payload);
1797  if (ts_st->amux) {
1798  avformat_free_context(ts_st->amux);
1799  ts_st->amux = NULL;
1800  }
1801  }
1802  }
1803 
1804  for (i = 0; i < ts->nb_services; i++) {
1805  service = ts->services[i];
1806  av_freep(&service->provider_name);
1807  av_freep(&service->name);
1808  av_freep(&service);
1809  }
1810  av_freep(&ts->services);
1811 }
1812 
1814 {
1815  int ret = 1;
1816  AVStream *st = s->streams[pkt->stream_index];
1817 
1818  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1819  if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
1820  (AV_RB24(pkt->data) != 0x000001 ||
1821  (st->codecpar->extradata_size > 0 &&
1822  st->codecpar->extradata[0] == 1)))
1823  ret = ff_stream_add_bitstream_filter(st, "h264_mp4toannexb", NULL);
1824  } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
1825  if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
1826  (AV_RB24(pkt->data) != 0x000001 ||
1827  (st->codecpar->extradata_size > 0 &&
1828  st->codecpar->extradata[0] == 1)))
1829  ret = ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL);
1830  }
1831 
1832  return ret;
1833 }
1834 
1835 static const AVOption options[] = {
1836  { "mpegts_transport_stream_id", "Set transport_stream_id field.",
1837  offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
1838  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1839  { "mpegts_original_network_id", "Set original_network_id field.",
1840  offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
1841  { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1842  { "mpegts_service_id", "Set service_id field.",
1843  offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
1844  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1845  { "mpegts_service_type", "Set service_type field.",
1846  offsetof(MpegTSWrite, service_type), AV_OPT_TYPE_INT,
1847  { .i64 = 0x01 }, 0x01, 0xff, AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1848  { "digital_tv", "Digital Television.",
1849  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff,
1850  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1851  { "digital_radio", "Digital Radio.",
1852  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff,
1853  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1854  { "teletext", "Teletext.",
1855  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff,
1856  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1857  { "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
1859  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1860  { "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
1861  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff,
1862  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1863  { "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
1865  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1866  { "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
1868  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1869  { "hevc_digital_hdtv", "HEVC Digital Television Service.",
1870  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff,
1871  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1872  { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
1873  offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
1874  { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
1875  { "mpegts_start_pid", "Set the first pid.",
1876  offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
1877  { .i64 = 0x0100 }, 0x0010, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
1878  { "mpegts_m2ts_mode", "Enable m2ts mode.",
1879  offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
1880  { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1881  { "muxrate", NULL,
1882  offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
1883  { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1884  { "pes_payload_size", "Minimum PES packet payload in bytes",
1885  offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT,
1886  { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1887  { "mpegts_flags", "MPEG-TS muxing flags",
1888  offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX,
1889  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1890  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1891  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX,
1892  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1893  { "latm", "Use LATM packetization for AAC",
1894  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX,
1895  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1896  { "pat_pmt_at_frames", "Reemit PAT and PMT at each video frame",
1897  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_PAT_PMT_AT_FRAMES}, 0, INT_MAX,
1898  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1899  { "system_b", "Conform to System B (DVB) instead of System A (ATSC)",
1900  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_SYSTEM_B }, 0, INT_MAX,
1901  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1902  { "initial_discontinuity", "Mark initial packets as discontinuous",
1903  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_DISCONT }, 0, INT_MAX,
1904  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1905  // backward compatibility
1906  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1907  offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT,
1908  { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1909  { "mpegts_copyts", "don't offset dts/pts",
1910  offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_BOOL,
1911  { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1912  { "tables_version", "set PAT, PMT and SDT version",
1913  offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT,
1914  { .i64 = 0 }, 0, 31, AV_OPT_FLAG_ENCODING_PARAM },
1915  { "omit_video_pes_length", "Omit the PES packet length for video packets",
1916  offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
1917  { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
1918  { "pcr_period", "PCR retransmission time",
1919  offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
1920  { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1921  { "pat_period", "PAT/PMT retransmission time limit in seconds",
1922  offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
1923  { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1924  { "sdt_period", "SDT retransmission time limit in seconds",
1925  offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
1926  { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1927  { NULL },
1928 };
1929 
1930 static const AVClass mpegts_muxer_class = {
1931  .class_name = "MPEGTS muxer",
1932  .item_name = av_default_item_name,
1933  .option = options,
1934  .version = LIBAVUTIL_VERSION_INT,
1935 };
1936 
1938  .name = "mpegts",
1939  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
1940  .mime_type = "video/MP2T",
1941  .extensions = "ts,m2t,m2ts,mts",
1942  .priv_data_size = sizeof(MpegTSWrite),
1943  .audio_codec = AV_CODEC_ID_MP2,
1944  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1945  .init = mpegts_init,
1948  .deinit = mpegts_deinit,
1949  .check_bitstream = mpegts_check_bitstream,
1951  .priv_class = &mpegts_muxer_class,
1952 };
#define NULL
Definition: coverity.c:32
#define SDT_PID
Definition: mpegts.h:37
const char const char void * val
Definition: avisynth_c.h:771
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define PMT_TID
Definition: mpegts.h:41
#define SECTION_LENGTH
Definition: mpegtsenc.c:126
int pat_packet_period
Definition: mpegtsenc.c:84
#define STREAM_TYPE_AUDIO_MPEG2
Definition: mpeg.h:52
MpegTSSection pmt
Definition: mpegtsenc.c:55
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1280
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int pcr_packet_count
Definition: mpegtsenc.c:60
int sdt_packet_count
Definition: mpegtsenc.c:81
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:919
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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:4581
#define STREAM_TYPE_VIDEO_CAVS
Definition: mpeg.h:58
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int64_t payload_dts
Definition: mpegtsenc.c:239
static int mpegts_init(AVFormatContext *s)
Definition: mpegtsenc.c:760
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3981
char * name
Definition: mpegtsenc.c:57
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1602
const char * b
Definition: vf_curves.c:113
int flag
Definition: cpu.c:34
int64_t last_sdt_ts
Definition: mpegtsenc.c:115
#define PCR_TIME_BASE
Definition: mpegtsenc.c:37
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:849
#define STREAM_TYPE_AUDIO_EAC3
Definition: mpegts.h:64
void * priv_data
Definition: avformat.h:904
#define AV_DISPOSITION_CLEAN_EFFECTS
stream without voice
Definition: avformat.h:851
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
#define PCR_RETRANS_TIME
Definition: mpegtsenc.c:228
static AVPacket pkt
int64_t first_pcr
Definition: mpegtsenc.c:88
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:495
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1268
double pat_period
Definition: mpegtsenc.c:112
Format I/O context.
Definition: avformat.h:1338
#define MPEGTS_FLAG_DISCONT
Definition: mpegtsenc.c:108
char * provider_name
Definition: mpegtsenc.c:58
int first_pts_check
first pts check needed
Definition: mpegtsenc.c:236
unsigned int nb_stream_indexes
Definition: avformat.h:1270
#define SDT_RETRANS_TIME
Definition: mpegtsenc.c:226
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:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
Definition: mpegtsenc.c:285
int64_t payload_pts
Definition: mpegtsenc.c:238
uint8_t
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:197
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
void * opaque
Definition: mpegtsenc.c:51
#define DEFAULT_PES_PAYLOAD_SIZE
Definition: mpegtsenc.c:122
int id
Format-specific stream ID.
Definition: avformat.h:896
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4214
static int mpegts_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
Definition: mpegtsenc.c:1813
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1406
int64_t duration
Definition: movenc.c:63
#define TS_PACKET_SIZE
Definition: mpegts.h:29
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:132
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1768
Public header for CRC hash function implementation.
int initial_padding
Audio only.
Definition: avcodec.h:4110
int omit_video_pes_length
Definition: mpegtsenc.c:117
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
static void mpegts_insert_null_packet(AVFormatContext *s)
Definition: mpegtsenc.c:1068
uint8_t * data
Definition: avcodec.h:1601
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1412
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
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.
Definition: format.c:85
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:310
static void mpegts_write_sdt(AVFormatContext *s)
Definition: mpegtsenc.c:670
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
#define STREAM_TYPE_VIDEO_VC1
Definition: mpegts.h:58
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
static void extend_af(uint8_t *pkt, int size)
Definition: mpegtsenc.c:1144
unsigned int * stream_index
Definition: avformat.h:1269
#define MPEGTS_FLAG_AAC_LATM
Definition: mpegtsenc.c:105
#define av_log(a,...)
#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:276
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1357
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1633
#define STREAM_TYPE_AUDIO_AAC
Definition: mpeg.h:55
MPEGTS stream ID, this is required to pass the stream ID information from the demuxer to the correspo...
Definition: avcodec.h:1532
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
struct MpegTSService * service
Definition: mpegtsenc.c:231
#define STREAM_TYPE_AUDIO_DTS
Definition: mpegts.h:62
static void mpegts_write_pes(AVFormatContext *s, AVStream *st, const uint8_t *payload, int payload_size, int64_t pts, int64_t dts, int key, int stream_id)
Definition: mpegtsenc.c:1164
AVProgram * av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
Find the programs which belong to a given stream.
Definition: utils.c:3938
static void mpegts_prefix_m2ts_header(AVFormatContext *s)
Definition: mpegtsenc.c:741
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:4148
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1554
AVProgram * program
Definition: mpegtsenc.c:62
static void put16(uint8_t **q_ptr, int val)
Definition: mpegtsenc.c:183
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:192
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
Definition: mpegtsenc.c:1113
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
int pes_payload_size
Definition: mpegtsenc.c:90
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
static uint8_t * get_ts_payload_start(uint8_t *pkt)
Definition: mpegtsenc.c:1152
unsigned int nb_programs
Definition: avformat.h:1493
#define STREAM_TYPE_VIDEO_DIRAC
Definition: mpegts.h:59
static void set_af_flag(uint8_t *pkt, int flag)
Definition: mpegtsenc.c:1128
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:517
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3977
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
Definition: mpegtsenc.c:1084
simple assert() macros that are a bit more flexible than ISO C assert().
int nb_services
Definition: mpegtsenc.c:85
AVRational user_tb
Definition: mpegtsenc.c:243
#define PAT_RETRANS_TIME
Definition: mpegtsenc.c:227
int mux_rate
set to 1 when VBR
Definition: mpegtsenc.c:89
static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1508
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1265
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
Definition: mpegtsenc.c:1053
#define FFMAX(a, b)
Definition: common.h:94
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
Definition: utils.c:3746
#define fail()
Definition: checkasm.h:84
#define MPEGTS_FLAG_SYSTEM_B
Definition: mpegtsenc.c:107
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1607
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
Definition: mathematics.c:147
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3999
double sdt_period
Definition: mpegtsenc.c:113
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1394
#define STREAM_TYPE_AUDIO_AAC_LATM
Definition: mpegts.h:52
int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Check presence of H264 startcode.
Definition: mpegtsenc.c:1426
static const AVOption options[]
Definition: mpegtsenc.c:1835
char filename[1024]
input or output filename
Definition: avformat.h:1414
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:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:248
av_warn_unused_result 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:521
#define FFMIN(a, b)
Definition: common.h:96
#define STREAM_TYPE_VIDEO_H264
Definition: mpeg.h:57
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
static void putstr8(uint8_t **q_ptr, const char *str, int write_len)
Definition: mpegtsenc.c:268
const char * name
Definition: avformat.h:524
#define DEFAULT_PROVIDER_NAME
Definition: mpegtsenc.c:222
AVFormatContext * ctx
Definition: movenc.c:48
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:357
int discontinuity
Definition: mpegtsenc.c:49
static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Definition: mpegtsenc.c:1442
AVOutputFormat ff_mpegts_muxer
Definition: mpegtsenc.c:1937
int n
Definition: avisynth_c.h:684
AVFormatContext * amux
Definition: mpegtsenc.c:242
AVDictionary * metadata
Definition: avformat.h:958
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...
Definition: format.c:98
#define MPEGTS_FLAG_REEMIT_PAT_PMT
Definition: mpegtsenc.c:104
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:196
#define STREAM_TYPE_VIDEO_MPEG4
Definition: mpeg.h:56
static void mpegts_deinit(AVFormatContext *s)
Definition: mpegtsenc.c:1786
int pmt_start_pid
Definition: mpegtsenc.c:97
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1308
int service_type
Definition: mpegtsenc.c:95
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:850
Stream structure.
Definition: avformat.h:889
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int start_pid
Definition: mpegtsenc.c:98
#define STREAM_TYPE_METADATA
Definition: mpegts.h:54
#define av_bswap32
Definition: bswap.h:33
int frame_size
Definition: mxfenc.c:1820
#define DVB_PRIVATE_NETWORK_START
Definition: mpegtsenc.c:41
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:267
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:254
int sdt_packet_period
Definition: mpegtsenc.c:82
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES
Definition: mpegtsenc.c:106
const AVClass * av_class
Definition: mpegtsenc.c:77
static struct @246 state
#define STREAM_TYPE_PRIVATE_DATA
Definition: mpeg.h:54
void * buf
Definition: avisynth_c.h:690
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: ffmpeg.c:650
Describe the class of an AVClass context structure.
Definition: log.h:67
static int opus_get_packet_samples(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1460
int original_network_id
Definition: mpegtsenc.c:93
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int pcr_packet_period
Definition: mpegtsenc.c:61
int service_id
Definition: mpegtsenc.c:94
Recommmends skipping the specified number of samples.
Definition: avcodec.h:1473
byte swapping routines
static int mpegts_write_end(AVFormatContext *s)
Definition: mpegtsenc.c:1778
#define STREAM_TYPE_AUDIO_AC3
Definition: mpeg.h:60
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4150
static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
Definition: mpegtsenc.c:1026
int transport_stream_id
Definition: mpegtsenc.c:92
MpegTSService ** services
Definition: mpegtsenc.c:80
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:753
uint8_t * payload
Definition: mpegtsenc.c:241
AVDictionary * metadata
Definition: avformat.h:1271
static int64_t pts
Global timestamp for the audio frames.
static void mpegts_write_flush(AVFormatContext *s)
Definition: mpegtsenc.c:1750
int m2ts_mode
Definition: mpegtsenc.c:99
#define STREAM_TYPE_VIDEO_HEVC
Definition: mpegts.h:56
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:343
int sample_rate
Audio only.
Definition: avcodec.h:4091
#define DEFAULT_SERVICE_NAME
Definition: mpegtsenc.c:223
Main libavformat public API header.
common internal api header.
int64_t last_pat_ts
Definition: mpegtsenc.c:114
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
Definition: mpegtsenc.c:735
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:947
int pcr_period
Definition: mpegtsenc.c:103
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:945
int den
Denominator.
Definition: rational.h:60
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:489
#define av_free(p)
char * value
Definition: dict.h:87
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:50
int len
#define PAT_PID
Definition: mpegts.h:36
#define STREAM_TYPE_VIDEO_MPEG2
Definition: mpeg.h:50
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:5168
void * priv_data
Format private data.
Definition: avformat.h:1366
MpegTSSection pat
Definition: mpegtsenc.c:78
int reemit_pat_pmt
Definition: mpegtsenc.c:101
static void mpegts_write_pat(AVFormatContext *s)
Definition: mpegtsenc.c:250
MpegTSSection sdt
Definition: mpegtsenc.c:79
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3995
int channels
Audio only.
Definition: avcodec.h:4087
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1600
int pat_packet_count
Definition: mpegtsenc.c:83
static const AVClass mpegts_muxer_class
Definition: mpegtsenc.c:1930
#define av_freep(p)
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:339
AVCodecParameters * codecpar
Definition: avformat.h:1241
#define av_malloc_array(a, b)
int opus_pending_trim_start
Definition: mpegtsenc.c:247
int stream_index
Definition: avcodec.h:1603
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
#define STREAM_TYPE_AUDIO_MPEG1
Definition: mpeg.h:51
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1578
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
Definition: mpegtsenc.c:129
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1594
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
#define STREAM_TYPE_AUDIO_TRUEHD
Definition: mpegts.h:63
AVProgram ** programs
Definition: avformat.h:1494
#define SDT_TID
Definition: mpegts.h:43
int tables_version
Definition: mpegtsenc.c:111
const char * name
Definition: opengl_enc.c:103
static MpegTSService * mpegts_add_service(MpegTSWrite *ts, int sid, const char *provider_name, const char *name)
Definition: mpegtsenc.c:708