FFmpeg  4.2.2
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  uint8_t name[256];
58  uint8_t provider_name[256];
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 static void putbuf(uint8_t **q_ptr, const uint8_t *buf, size_t len)
268 {
269  memcpy(*q_ptr, buf, len);
270  *q_ptr += len;
271 }
272 
273 static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
274 {
275  uint8_t *q = *q_ptr;
276  *q++ = 0x05; /* MPEG-2 registration descriptor*/
277  *q++ = 4;
278  *q++ = tag;
279  *q++ = tag >> 8;
280  *q++ = tag >> 16;
281  *q++ = tag >> 24;
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  }
412  put_registration_descriptor(&q, MKTAG('B', 'S', 'S', 'D'));
413  if (st->codecpar->codec_id==AV_CODEC_ID_OPUS) {
414  /* 6 bytes registration descriptor, 4 bytes Opus audio descriptor */
415  if (q - data > SECTION_LENGTH - 6 - 4) {
416  err = 1;
417  break;
418  }
419 
420  put_registration_descriptor(&q, MKTAG('O', 'p', 'u', 's'));
421 
422  *q++ = 0x7f; /* DVB extension descriptor */
423  *q++ = 2;
424  *q++ = 0x80;
425 
426  if (st->codecpar->extradata && st->codecpar->extradata_size >= 19) {
427  if (st->codecpar->extradata[18] == 0 && st->codecpar->channels <= 2) {
428  /* RTP mapping family */
429  *q++ = st->codecpar->channels;
430  } else if (st->codecpar->extradata[18] == 1 && st->codecpar->channels <= 8 &&
431  st->codecpar->extradata_size >= 21 + st->codecpar->channels) {
432  static const uint8_t coupled_stream_counts[9] = {
433  1, 0, 1, 1, 2, 2, 2, 3, 3
434  };
435  static const uint8_t channel_map_a[8][8] = {
436  {0},
437  {0, 1},
438  {0, 2, 1},
439  {0, 1, 2, 3},
440  {0, 4, 1, 2, 3},
441  {0, 4, 1, 2, 3, 5},
442  {0, 4, 1, 2, 3, 5, 6},
443  {0, 6, 1, 2, 3, 4, 5, 7},
444  };
445  static const uint8_t channel_map_b[8][8] = {
446  {0},
447  {0, 1},
448  {0, 1, 2},
449  {0, 1, 2, 3},
450  {0, 1, 2, 3, 4},
451  {0, 1, 2, 3, 4, 5},
452  {0, 1, 2, 3, 4, 5, 6},
453  {0, 1, 2, 3, 4, 5, 6, 7},
454  };
455  /* Vorbis mapping family */
456 
457  if (st->codecpar->extradata[19] == st->codecpar->channels - coupled_stream_counts[st->codecpar->channels] &&
458  st->codecpar->extradata[20] == coupled_stream_counts[st->codecpar->channels] &&
459  memcmp(&st->codecpar->extradata[21], channel_map_a[st->codecpar->channels-1], st->codecpar->channels) == 0) {
460  *q++ = st->codecpar->channels;
461  } else if (st->codecpar->channels >= 2 && st->codecpar->extradata[19] == st->codecpar->channels &&
462  st->codecpar->extradata[20] == 0 &&
463  memcmp(&st->codecpar->extradata[21], channel_map_b[st->codecpar->channels-1], st->codecpar->channels) == 0) {
464  *q++ = st->codecpar->channels | 0x80;
465  } else {
466  /* Unsupported, could write an extended descriptor here */
467  av_log(s, AV_LOG_ERROR, "Unsupported Opus Vorbis-style channel mapping");
468  *q++ = 0xff;
469  }
470  } else {
471  /* Unsupported */
472  av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping for family %d", st->codecpar->extradata[18]);
473  *q++ = 0xff;
474  }
475  } else if (st->codecpar->channels <= 2) {
476  /* Assume RTP mapping family */
477  *q++ = st->codecpar->channels;
478  } else {
479  /* Unsupported */
480  av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping");
481  *q++ = 0xff;
482  }
483  }
484 
485  if (lang) {
486  char *p;
487  char *next = lang->value;
488  uint8_t *len_ptr;
489 
490  *q++ = 0x0a; /* ISO 639 language descriptor */
491  len_ptr = q++;
492  *len_ptr = 0;
493 
494  for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
495  if (q - data > SECTION_LENGTH - 4) {
496  err = 1;
497  break;
498  }
499  next = strchr(p, ',');
500  if (strlen(p) != 3 && (!next || next != p + 3))
501  continue; /* not a 3-letter code */
502 
503  *q++ = *p++;
504  *q++ = *p++;
505  *q++ = *p++;
506 
508  *q++ = 0x01;
510  *q++ = 0x02;
512  *q++ = 0x03;
513  else
514  *q++ = 0; /* undefined type */
515 
516  *len_ptr += 4;
517  }
518 
519  if (*len_ptr == 0)
520  q -= 2; /* no language codes were written */
521  }
522  break;
524  {
525  const char default_language[] = "und";
526  const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
527 
529  uint8_t *len_ptr;
530  int extradata_copied = 0;
531 
532  *q++ = 0x59; /* subtitling_descriptor */
533  len_ptr = q++;
534 
535  while (strlen(language) >= 3) {
536  if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
537  err = 1;
538  break;
539  }
540  *q++ = *language++;
541  *q++ = *language++;
542  *q++ = *language++;
543  /* Skip comma */
544  if (*language != '\0')
545  language++;
546 
547  if (st->codecpar->extradata_size - extradata_copied >= 5) {
548  *q++ = st->codecpar->extradata[extradata_copied + 4]; /* subtitling_type */
549  memcpy(q, st->codecpar->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
550  extradata_copied += 5;
551  q += 4;
552  } else {
553  /* subtitling_type:
554  * 0x10 - normal with no monitor aspect ratio criticality
555  * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
556  *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
557  if ((st->codecpar->extradata_size == 4) && (extradata_copied == 0)) {
558  /* support of old 4-byte extradata format */
559  memcpy(q, st->codecpar->extradata, 4); /* composition_page_id and ancillary_page_id */
560  extradata_copied += 4;
561  q += 4;
562  } else {
563  put16(&q, 1); /* composition_page_id */
564  put16(&q, 1); /* ancillary_page_id */
565  }
566  }
567  }
568 
569  *len_ptr = q - len_ptr - 1;
570  } else if (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
571  uint8_t *len_ptr = NULL;
572  int extradata_copied = 0;
573 
574  /* The descriptor tag. teletext_descriptor */
575  *q++ = 0x56;
576  len_ptr = q++;
577 
578  while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
579  *q++ = *language++;
580  *q++ = *language++;
581  *q++ = *language++;
582  /* Skip comma */
583  if (*language != '\0')
584  language++;
585 
586  if (st->codecpar->extradata_size - 1 > extradata_copied) {
587  memcpy(q, st->codecpar->extradata + extradata_copied, 2);
588  extradata_copied += 2;
589  q += 2;
590  } else {
591  /* The Teletext descriptor:
592  * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
593  * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
594  * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
595  *q++ = 0x08;
596  *q++ = 0x00;
597  }
598  }
599 
600  *len_ptr = q - len_ptr - 1;
601  }
602  }
603  break;
604  case AVMEDIA_TYPE_VIDEO:
605  if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
606  put_registration_descriptor(&q, MKTAG('d', 'r', 'a', 'c'));
607  } else if (stream_type == STREAM_TYPE_VIDEO_VC1) {
608  put_registration_descriptor(&q, MKTAG('V', 'C', '-', '1'));
609  } else if (stream_type == STREAM_TYPE_VIDEO_HEVC && s->strict_std_compliance <= FF_COMPLIANCE_NORMAL) {
610  put_registration_descriptor(&q, MKTAG('H', 'E', 'V', 'C'));
611  }
612  break;
613  case AVMEDIA_TYPE_DATA:
615  put_registration_descriptor(&q, MKTAG('K', 'L', 'V', 'A'));
616  } else if (st->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {
617  const char *tag = "ID3 ";
618  *q++ = 0x26; /* metadata descriptor */
619  *q++ = 13;
620  put16(&q, 0xffff); /* metadata application format */
621  putbuf(&q, tag, strlen(tag));
622  *q++ = 0xff; /* metadata format */
623  putbuf(&q, tag, strlen(tag));
624  *q++ = 0; /* metadata service ID */
625  *q++ = 0xF; /* metadata_locator_record_flag|MPEG_carriage_flags|reserved */
626  }
627  break;
628  }
629 
630  val = 0xf000 | (q - desc_length_ptr - 2);
631  desc_length_ptr[0] = val >> 8;
632  desc_length_ptr[1] = val;
633  }
634 
635  if (err)
636  av_log(s, AV_LOG_ERROR,
637  "The PMT section cannot fit stream %d and all following streams.\n"
638  "Try reducing the number of languages in the audio streams "
639  "or the total number of streams.\n", i);
640 
641  mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
642  data, q - data);
643  return 0;
644 }
645 
647 {
648  MpegTSWrite *ts = s->priv_data;
649  MpegTSService *service;
650  uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
651  int i, running_status, free_ca_mode, val;
652 
653  q = data;
654  put16(&q, ts->onid);
655  *q++ = 0xff;
656  for (i = 0; i < ts->nb_services; i++) {
657  service = ts->services[i];
658  put16(&q, service->sid);
659  *q++ = 0xfc | 0x00; /* currently no EIT info */
660  desc_list_len_ptr = q;
661  q += 2;
662  running_status = 4; /* running */
663  free_ca_mode = 0;
664 
665  /* write only one descriptor for the service name and provider */
666  *q++ = 0x48;
667  desc_len_ptr = q;
668  q++;
669  *q++ = ts->service_type;
670  putbuf(&q, service->provider_name, service->provider_name[0] + 1);
671  putbuf(&q, service->name, service->name[0] + 1);
672  desc_len_ptr[0] = q - desc_len_ptr - 1;
673 
674  /* fill descriptor length */
675  val = (running_status << 13) | (free_ca_mode << 12) |
676  (q - desc_list_len_ptr - 2);
677  desc_list_len_ptr[0] = val >> 8;
678  desc_list_len_ptr[1] = val;
679  }
680  mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, ts->tables_version, 0, 0,
681  data, q - data);
682 }
683 
684 /* This stores a string in buf with the correct encoding and also sets the
685  * first byte as the length. !str is accepted for an empty string.
686  * If the string is already encoded, invalid UTF-8 or has no multibyte sequence
687  * then we keep it as is, otherwise we signal UTF-8 encoding. */
688 static int encode_str8(uint8_t *buf, const char *str)
689 {
690  size_t str_len;
691  if (!str)
692  str = "";
693  str_len = strlen(str);
694  if (str[0] && (unsigned)str[0] >= 0x20) { /* Make sure the string is not already encoded. */
695  const uint8_t *q = str;
696  int has_multibyte = 0;
697  while (*q) {
698  uint32_t code;
699  GET_UTF8(code, *q++, goto invalid;) /* Is it valid UTF-8? */
700  has_multibyte |= (code > 127); /* Does it have multibyte UTF-8 chars in it? */
701  }
702  if (has_multibyte) { /* If we have multibyte chars and valid UTF-8, then encode as such! */
703  if (str_len > 254)
704  return AVERROR(EINVAL);
705  buf[0] = str_len + 1;
706  buf[1] = 0x15;
707  memcpy(&buf[2], str, str_len);
708  return 0;
709  }
710  }
711 invalid:
712  /* Otherwise let's just encode the string as is! */
713  if (str_len > 255)
714  return AVERROR(EINVAL);
715  buf[0] = str_len;
716  memcpy(&buf[1], str, str_len);
717  return 0;
718 }
719 
721  const char *provider_name,
722  const char *name)
723 {
724  MpegTSWrite *ts = s->priv_data;
725  MpegTSService *service;
726 
727  service = av_mallocz(sizeof(MpegTSService));
728  if (!service)
729  return NULL;
730  service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
731  service->sid = sid;
732  service->pcr_pid = 0x1fff;
733  if (encode_str8(service->provider_name, provider_name) < 0 ||
734  encode_str8(service->name, name) < 0) {
735  av_log(s, AV_LOG_ERROR, "Too long service or provider name\n");
736  goto fail;
737  }
738  if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
739  goto fail;
740 
741  return service;
742 fail:
743  av_free(service);
744  return NULL;
745 }
746 
747 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
748 {
749  return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
750  ts->first_pcr;
751 }
752 
754 {
755  MpegTSWrite *ts = s->priv_data;
756  if (ts->m2ts_mode) {
757  int64_t pcr = get_pcr(s->priv_data, s->pb);
758  uint32_t tp_extra_header = pcr % 0x3fffffff;
759  tp_extra_header = AV_RB32(&tp_extra_header);
760  avio_write(s->pb, (unsigned char *) &tp_extra_header,
761  sizeof(tp_extra_header));
762  }
763 }
764 
765 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
766 {
767  AVFormatContext *ctx = s->opaque;
769  avio_write(ctx->pb, packet, TS_PACKET_SIZE);
770 }
771 
773 {
774  MpegTSWrite *ts = s->priv_data;
775  MpegTSWriteStream *ts_st;
776  MpegTSService *service;
777  AVStream *st, *pcr_st = NULL;
778  AVDictionaryEntry *title, *provider;
779  int i, j;
780  const char *service_name;
781  const char *provider_name;
782  int *pids;
783  int ret;
784 
785  if (s->max_delay < 0) /* Not set by the caller */
786  s->max_delay = 0;
787 
788  // round up to a whole number of TS packets
789  ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
790 
791  ts->tsid = ts->transport_stream_id;
792  ts->onid = ts->original_network_id;
793  if (!s->nb_programs) {
794  /* allocate a single DVB service */
795  title = av_dict_get(s->metadata, "service_name", NULL, 0);
796  if (!title)
797  title = av_dict_get(s->metadata, "title", NULL, 0);
798  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
799  provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
800  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
801  service = mpegts_add_service(s, ts->service_id,
802  provider_name, service_name);
803 
804  if (!service)
805  return AVERROR(ENOMEM);
806 
808  service->pmt.opaque = s;
809  service->pmt.cc = 15;
810  service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
811  } else {
812  for (i = 0; i < s->nb_programs; i++) {
813  AVProgram *program = s->programs[i];
814  title = av_dict_get(program->metadata, "service_name", NULL, 0);
815  if (!title)
816  title = av_dict_get(program->metadata, "title", NULL, 0);
817  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
818  provider = av_dict_get(program->metadata, "service_provider", NULL, 0);
819  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
820  service = mpegts_add_service(s, program->id,
821  provider_name, service_name);
822 
823  if (!service)
824  return AVERROR(ENOMEM);
825 
827  service->pmt.opaque = s;
828  service->pmt.cc = 15;
829  service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
830  service->program = program;
831  }
832  }
833 
834  ts->pat.pid = PAT_PID;
835  /* Initialize at 15 so that it wraps and is equal to 0 for the
836  * first packet we write. */
837  ts->pat.cc = 15;
840  ts->pat.opaque = s;
841 
842  ts->sdt.pid = SDT_PID;
843  ts->sdt.cc = 15;
846  ts->sdt.opaque = s;
847 
848  pids = av_malloc_array(s->nb_streams, sizeof(*pids));
849  if (!pids) {
850  ret = AVERROR(ENOMEM);
851  goto fail;
852  }
853 
854  /* assign pids to each stream */
855  for (i = 0; i < s->nb_streams; i++) {
857  st = s->streams[i];
858 
859  ts_st = av_mallocz(sizeof(MpegTSWriteStream));
860  if (!ts_st) {
861  ret = AVERROR(ENOMEM);
862  goto fail;
863  }
864  st->priv_data = ts_st;
865 
866  ts_st->user_tb = st->time_base;
867  avpriv_set_pts_info(st, 33, 1, 90000);
868 
869  ts_st->payload = av_mallocz(ts->pes_payload_size);
870  if (!ts_st->payload) {
871  ret = AVERROR(ENOMEM);
872  goto fail;
873  }
874 
875  program = av_find_program_from_stream(s, NULL, i);
876  if (program) {
877  for (j = 0; j < ts->nb_services; j++) {
878  if (ts->services[j]->program == program) {
879  service = ts->services[j];
880  break;
881  }
882  }
883  }
884 
885  ts_st->service = service;
886  /* MPEG pid values < 16 are reserved. Applications which set st->id in
887  * this range are assigned a calculated pid. */
888  if (st->id < 16) {
889  ts_st->pid = ts->start_pid + i;
890  } else if (st->id < 0x1FFF) {
891  ts_st->pid = st->id;
892  } else {
893  av_log(s, AV_LOG_ERROR,
894  "Invalid stream id %d, must be less than 8191\n", st->id);
895  ret = AVERROR(EINVAL);
896  goto fail;
897  }
898  if (ts_st->pid == service->pmt.pid) {
899  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
900  ret = AVERROR(EINVAL);
901  goto fail;
902  }
903  for (j = 0; j < i; j++) {
904  if (pids[j] == ts_st->pid) {
905  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
906  ret = AVERROR(EINVAL);
907  goto fail;
908  }
909  }
910  pids[i] = ts_st->pid;
911  ts_st->payload_pts = AV_NOPTS_VALUE;
912  ts_st->payload_dts = AV_NOPTS_VALUE;
913  ts_st->first_pts_check = 1;
914  ts_st->cc = 15;
915  ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
916  /* update PCR pid by using the first video stream */
917  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
918  service->pcr_pid == 0x1fff) {
919  service->pcr_pid = ts_st->pid;
920  pcr_st = st;
921  }
922  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
923  st->codecpar->extradata_size > 0) {
924  AVStream *ast;
925  ts_st->amux = avformat_alloc_context();
926  if (!ts_st->amux) {
927  ret = AVERROR(ENOMEM);
928  goto fail;
929  }
930  ts_st->amux->oformat =
931  av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
932  NULL, NULL);
933  if (!ts_st->amux->oformat) {
934  ret = AVERROR(EINVAL);
935  goto fail;
936  }
937  if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
938  ret = AVERROR(ENOMEM);
939  goto fail;
940  }
941  ret = avcodec_parameters_copy(ast->codecpar, st->codecpar);
942  if (ret != 0)
943  goto fail;
944  ast->time_base = st->time_base;
945  ret = avformat_write_header(ts_st->amux, NULL);
946  if (ret < 0)
947  goto fail;
948  }
949  if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
951  }
952  }
953 
954  av_freep(&pids);
955 
956  /* if no video stream, use the first stream as PCR */
957  if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
958  pcr_st = s->streams[0];
959  ts_st = pcr_st->priv_data;
960  service->pcr_pid = ts_st->pid;
961  } else
962  ts_st = pcr_st->priv_data;
963 
964  if (ts->mux_rate > 1) {
965  service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period /
966  (TS_PACKET_SIZE * 8 * 1000);
967  ts->sdt_packet_period = (int64_t)ts->mux_rate * SDT_RETRANS_TIME /
968  (TS_PACKET_SIZE * 8 * 1000);
969  ts->pat_packet_period = (int64_t)ts->mux_rate * PAT_RETRANS_TIME /
970  (TS_PACKET_SIZE * 8 * 1000);
971 
972  if (ts->copyts < 1)
974  } else {
975  /* Arbitrary values, PAT/PMT will also be written on video key frames */
976  ts->sdt_packet_period = 200;
977  ts->pat_packet_period = 40;
978  if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
980  if (!frame_size) {
981  av_log(s, AV_LOG_WARNING, "frame size not set\n");
982  service->pcr_packet_period =
983  pcr_st->codecpar->sample_rate / (10 * 512);
984  } else {
985  service->pcr_packet_period =
986  pcr_st->codecpar->sample_rate / (10 * frame_size);
987  }
988  } else {
989  // max delta PCR 0.1s
990  // TODO: should be avg_frame_rate
991  service->pcr_packet_period =
992  ts_st->user_tb.den / (10 * ts_st->user_tb.num);
993  }
994  if (!service->pcr_packet_period)
995  service->pcr_packet_period = 1;
996  }
997 
1000  // The user specified a period, use only it
1001  if (ts->pat_period < INT_MAX/2) {
1002  ts->pat_packet_period = INT_MAX;
1003  }
1004  if (ts->sdt_period < INT_MAX/2) {
1005  ts->sdt_packet_period = INT_MAX;
1006  }
1007 
1008  // output a PCR as soon as possible
1009  service->pcr_packet_count = service->pcr_packet_period;
1010  ts->pat_packet_count = ts->pat_packet_period - 1;
1011  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1012 
1013  if (ts->mux_rate == 1)
1014  av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
1015  else
1016  av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
1018  "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
1019  service->pcr_packet_period,
1021 
1022  if (ts->m2ts_mode == -1) {
1023  if (av_match_ext(s->url, "m2ts")) {
1024  ts->m2ts_mode = 1;
1025  } else {
1026  ts->m2ts_mode = 0;
1027  }
1028  }
1029 
1030  return 0;
1031 
1032 fail:
1033  av_freep(&pids);
1034  return ret;
1035 }
1036 
1037 /* send SDT, PAT and PMT tables regularly */
1038 static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
1039 {
1040  MpegTSWrite *ts = s->priv_data;
1041  int i;
1042 
1043  if (++ts->sdt_packet_count == ts->sdt_packet_period ||
1044  (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
1045  (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
1046  ) {
1047  ts->sdt_packet_count = 0;
1048  if (dts != AV_NOPTS_VALUE)
1049  ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
1050  mpegts_write_sdt(s);
1051  }
1052  if (++ts->pat_packet_count == ts->pat_packet_period ||
1053  (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
1054  (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
1055  force_pat) {
1056  ts->pat_packet_count = 0;
1057  if (dts != AV_NOPTS_VALUE)
1058  ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
1059  mpegts_write_pat(s);
1060  for (i = 0; i < ts->nb_services; i++)
1061  mpegts_write_pmt(s, ts->services[i]);
1062  }
1063 }
1064 
1065 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
1066 {
1067  int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
1068 
1069  *buf++ = pcr_high >> 25;
1070  *buf++ = pcr_high >> 17;
1071  *buf++ = pcr_high >> 9;
1072  *buf++ = pcr_high >> 1;
1073  *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
1074  *buf++ = pcr_low;
1075 
1076  return 6;
1077 }
1078 
1079 /* Write a single null transport stream packet */
1081 {
1082  uint8_t *q;
1084 
1085  q = buf;
1086  *q++ = 0x47;
1087  *q++ = 0x00 | 0x1f;
1088  *q++ = 0xff;
1089  *q++ = 0x10;
1090  memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
1092  avio_write(s->pb, buf, TS_PACKET_SIZE);
1093 }
1094 
1095 /* Write a single transport stream packet with a PCR and no payload */
1097 {
1098  MpegTSWrite *ts = s->priv_data;
1099  MpegTSWriteStream *ts_st = st->priv_data;
1100  uint8_t *q;
1102 
1103  q = buf;
1104  *q++ = 0x47;
1105  *q++ = ts_st->pid >> 8;
1106  *q++ = ts_st->pid;
1107  *q++ = 0x20 | ts_st->cc; /* Adaptation only */
1108  /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
1109  *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
1110  *q++ = 0x10; /* Adaptation flags: PCR present */
1111  if (ts_st->discontinuity) {
1112  q[-1] |= 0x80;
1113  ts_st->discontinuity = 0;
1114  }
1115 
1116  /* PCR coded into 6 bytes */
1117  q += write_pcr_bits(q, get_pcr(ts, s->pb));
1118 
1119  /* stuffing bytes */
1120  memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
1122  avio_write(s->pb, buf, TS_PACKET_SIZE);
1123 }
1124 
1125 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
1126 {
1127  int val;
1128 
1129  val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
1130  *q++ = val;
1131  val = (((pts >> 15) & 0x7fff) << 1) | 1;
1132  *q++ = val >> 8;
1133  *q++ = val;
1134  val = (((pts) & 0x7fff) << 1) | 1;
1135  *q++ = val >> 8;
1136  *q++ = val;
1137 }
1138 
1139 /* Set an adaptation field flag in an MPEG-TS packet*/
1140 static void set_af_flag(uint8_t *pkt, int flag)
1141 {
1142  // expect at least one flag to set
1143  av_assert0(flag);
1144 
1145  if ((pkt[3] & 0x20) == 0) {
1146  // no AF yet, set adaptation field flag
1147  pkt[3] |= 0x20;
1148  // 1 byte length, no flags
1149  pkt[4] = 1;
1150  pkt[5] = 0;
1151  }
1152  pkt[5] |= flag;
1153 }
1154 
1155 /* Extend the adaptation field by size bytes */
1156 static void extend_af(uint8_t *pkt, int size)
1157 {
1158  // expect already existing adaptation field
1159  av_assert0(pkt[3] & 0x20);
1160  pkt[4] += size;
1161 }
1162 
1163 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
1165 {
1166  if (pkt[3] & 0x20)
1167  return pkt + 5 + pkt[4];
1168  else
1169  return pkt + 4;
1170 }
1171 
1172 /* Add a PES header to the front of the payload, and segment into an integer
1173  * number of TS packets. The final TS packet is padded using an oversized
1174  * adaptation header to exactly fill the last TS packet.
1175  * NOTE: 'payload' contains a complete PES payload. */
1177  const uint8_t *payload, int payload_size,
1178  int64_t pts, int64_t dts, int key, int stream_id)
1179 {
1180  MpegTSWriteStream *ts_st = st->priv_data;
1181  MpegTSWrite *ts = s->priv_data;
1183  uint8_t *q;
1184  int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
1185  int afc_len, stuffing_len;
1186  int64_t pcr = -1; /* avoid warning */
1187  int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
1188  int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
1189 
1190  av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
1192  force_pat = 1;
1193  }
1194 
1195  is_start = 1;
1196  while (payload_size > 0) {
1197  retransmit_si_info(s, force_pat, dts);
1198  force_pat = 0;
1199 
1200  write_pcr = 0;
1201  if (ts_st->pid == ts_st->service->pcr_pid) {
1202  if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
1203  ts_st->service->pcr_packet_count++;
1204  if (ts_st->service->pcr_packet_count >=
1205  ts_st->service->pcr_packet_period) {
1206  ts_st->service->pcr_packet_count = 0;
1207  write_pcr = 1;
1208  }
1209  }
1210 
1211  if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
1212  (dts - get_pcr(ts, s->pb) / 300) > delay) {
1213  /* pcr insert gets priority over null packet insert */
1214  if (write_pcr)
1215  mpegts_insert_pcr_only(s, st);
1216  else
1218  /* recalculate write_pcr and possibly retransmit si_info */
1219  continue;
1220  }
1221 
1222  /* prepare packet header */
1223  q = buf;
1224  *q++ = 0x47;
1225  val = ts_st->pid >> 8;
1226  if (is_start)
1227  val |= 0x40;
1228  *q++ = val;
1229  *q++ = ts_st->pid;
1230  ts_st->cc = ts_st->cc + 1 & 0xf;
1231  *q++ = 0x10 | ts_st->cc; // payload indicator + CC
1232  if (ts_st->discontinuity) {
1233  set_af_flag(buf, 0x80);
1234  q = get_ts_payload_start(buf);
1235  ts_st->discontinuity = 0;
1236  }
1237  if (key && is_start && pts != AV_NOPTS_VALUE) {
1238  // set Random Access for key frames
1239  if (ts_st->pid == ts_st->service->pcr_pid)
1240  write_pcr = 1;
1241  set_af_flag(buf, 0x40);
1242  q = get_ts_payload_start(buf);
1243  }
1244  if (write_pcr) {
1245  set_af_flag(buf, 0x10);
1246  q = get_ts_payload_start(buf);
1247  // add 11, pcr references the last byte of program clock reference base
1248  if (ts->mux_rate > 1)
1249  pcr = get_pcr(ts, s->pb);
1250  else
1251  pcr = (dts - delay) * 300;
1252  if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
1253  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
1254  extend_af(buf, write_pcr_bits(q, pcr));
1255  q = get_ts_payload_start(buf);
1256  }
1257  if (is_start) {
1258  int pes_extension = 0;
1259  int pes_header_stuffing_bytes = 0;
1260  /* write PES header */
1261  *q++ = 0x00;
1262  *q++ = 0x00;
1263  *q++ = 0x01;
1264  is_dvb_subtitle = 0;
1265  is_dvb_teletext = 0;
1266  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1267  if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)
1268  *q++ = 0xfd;
1269  else
1270  *q++ = 0xe0;
1271  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1272  (st->codecpar->codec_id == AV_CODEC_ID_MP2 ||
1273  st->codecpar->codec_id == AV_CODEC_ID_MP3 ||
1274  st->codecpar->codec_id == AV_CODEC_ID_AAC)) {
1275  *q++ = 0xc0;
1276  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1277  st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
1278  ts->m2ts_mode) {
1279  *q++ = 0xfd;
1280  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1282  *q++ = 0xbd;
1283  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
1284  *q++ = stream_id != -1 ? stream_id : 0xfc;
1285 
1286  if (stream_id == 0xbd) /* asynchronous KLV */
1287  pts = dts = AV_NOPTS_VALUE;
1288  } else {
1289  *q++ = 0xbd;
1292  is_dvb_subtitle = 1;
1293  } else if (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
1294  is_dvb_teletext = 1;
1295  }
1296  }
1297  }
1298  header_len = 0;
1299  flags = 0;
1300  if (pts != AV_NOPTS_VALUE) {
1301  header_len += 5;
1302  flags |= 0x80;
1303  }
1304  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1305  header_len += 5;
1306  flags |= 0x40;
1307  }
1308  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1310  /* set PES_extension_flag */
1311  pes_extension = 1;
1312  flags |= 0x01;
1313 
1314  /* One byte for PES2 extension flag +
1315  * one byte for extension length +
1316  * one byte for extension id */
1317  header_len += 3;
1318  }
1319  /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
1320  * otherwise it will not play sound on blu-ray
1321  */
1322  if (ts->m2ts_mode &&
1324  st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1325  /* set PES_extension_flag */
1326  pes_extension = 1;
1327  flags |= 0x01;
1328  header_len += 3;
1329  }
1330  if (is_dvb_teletext) {
1331  pes_header_stuffing_bytes = 0x24 - header_len;
1332  header_len = 0x24;
1333  }
1334  len = payload_size + header_len + 3;
1335  /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
1336  if (is_dvb_subtitle) {
1337  len += 3;
1338  payload_size++;
1339  }
1340  if (len > 0xffff)
1341  len = 0;
1343  len = 0;
1344  }
1345  *q++ = len >> 8;
1346  *q++ = len;
1347  val = 0x80;
1348  /* data alignment indicator is required for subtitle and data streams */
1350  val |= 0x04;
1351  *q++ = val;
1352  *q++ = flags;
1353  *q++ = header_len;
1354  if (pts != AV_NOPTS_VALUE) {
1355  write_pts(q, flags >> 6, pts);
1356  q += 5;
1357  }
1358  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1359  write_pts(q, 1, dts);
1360  q += 5;
1361  }
1362  if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
1363  flags = 0x01; /* set PES_extension_flag_2 */
1364  *q++ = flags;
1365  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1366  /* Set the stream ID extension flag bit to 0 and
1367  * write the extended stream ID. */
1368  *q++ = 0x00 | 0x60;
1369  }
1370  /* For Blu-ray AC3 Audio Setting extended flags */
1371  if (ts->m2ts_mode &&
1372  pes_extension &&
1373  st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1374  flags = 0x01; /* set PES_extension_flag_2 */
1375  *q++ = flags;
1376  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1377  *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1378  }
1379 
1380 
1381  if (is_dvb_subtitle) {
1382  /* First two fields of DVB subtitles PES data:
1383  * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
1384  * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
1385  *q++ = 0x20;
1386  *q++ = 0x00;
1387  }
1388  if (is_dvb_teletext) {
1389  memset(q, 0xff, pes_header_stuffing_bytes);
1390  q += pes_header_stuffing_bytes;
1391  }
1392  is_start = 0;
1393  }
1394  /* header size */
1395  header_len = q - buf;
1396  /* data len */
1397  len = TS_PACKET_SIZE - header_len;
1398  if (len > payload_size)
1399  len = payload_size;
1400  stuffing_len = TS_PACKET_SIZE - header_len - len;
1401  if (stuffing_len > 0) {
1402  /* add stuffing with AFC */
1403  if (buf[3] & 0x20) {
1404  /* stuffing already present: increase its size */
1405  afc_len = buf[4] + 1;
1406  memmove(buf + 4 + afc_len + stuffing_len,
1407  buf + 4 + afc_len,
1408  header_len - (4 + afc_len));
1409  buf[4] += stuffing_len;
1410  memset(buf + 4 + afc_len, 0xff, stuffing_len);
1411  } else {
1412  /* add stuffing */
1413  memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1414  buf[3] |= 0x20;
1415  buf[4] = stuffing_len - 1;
1416  if (stuffing_len >= 2) {
1417  buf[5] = 0x00;
1418  memset(buf + 6, 0xff, stuffing_len - 2);
1419  }
1420  }
1421  }
1422 
1423  if (is_dvb_subtitle && payload_size == len) {
1424  memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
1425  buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
1426  } else {
1427  memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1428  }
1429 
1430  payload += len;
1431  payload_size -= len;
1433  avio_write(s->pb, buf, TS_PACKET_SIZE);
1434  }
1435  ts_st->prev_payload_key = key;
1436 }
1437 
1439 {
1440  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1441  if (!st->nb_frames) {
1442  av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1443  "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1444  "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1445  return AVERROR_INVALIDDATA;
1446  }
1447  av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing, size %d", pkt->size);
1448  if (pkt->size)
1449  av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
1450  av_log(s, AV_LOG_WARNING, "\n");
1451  }
1452  return 0;
1453 }
1454 
1456 {
1457  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1458  if (!st->nb_frames) {
1459  av_log(s, AV_LOG_ERROR, "HEVC bitstream malformed, no startcode found\n");
1460  return AVERROR_PATCHWELCOME;
1461  }
1462  av_log(s, AV_LOG_WARNING, "HEVC bitstream error, startcode missing, size %d", pkt->size);
1463  if (pkt->size)
1464  av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
1465  av_log(s, AV_LOG_WARNING, "\n");
1466  }
1467  return 0;
1468 }
1469 
1470 /* Based on GStreamer's gst-plugins-base/ext/ogg/gstoggstream.c
1471  * Released under the LGPL v2.1+, written by
1472  * Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
1473  */
1475 {
1476  static const int durations[32] = {
1477  480, 960, 1920, 2880, /* Silk NB */
1478  480, 960, 1920, 2880, /* Silk MB */
1479  480, 960, 1920, 2880, /* Silk WB */
1480  480, 960, /* Hybrid SWB */
1481  480, 960, /* Hybrid FB */
1482  120, 240, 480, 960, /* CELT NB */
1483  120, 240, 480, 960, /* CELT NB */
1484  120, 240, 480, 960, /* CELT NB */
1485  120, 240, 480, 960, /* CELT NB */
1486  };
1487  int toc, frame_duration, nframes, duration;
1488 
1489  if (pkt->size < 1)
1490  return 0;
1491 
1492  toc = pkt->data[0];
1493 
1494  frame_duration = durations[toc >> 3];
1495  switch (toc & 3) {
1496  case 0:
1497  nframes = 1;
1498  break;
1499  case 1:
1500  nframes = 2;
1501  break;
1502  case 2:
1503  nframes = 2;
1504  break;
1505  case 3:
1506  if (pkt->size < 2)
1507  return 0;
1508  nframes = pkt->data[1] & 63;
1509  break;
1510  }
1511 
1512  duration = nframes * frame_duration;
1513  if (duration > 5760) {
1515  "Opus packet duration > 120 ms, invalid");
1516  return 0;
1517  }
1518 
1519  return duration;
1520 }
1521 
1523 {
1524  AVStream *st = s->streams[pkt->stream_index];
1525  int size = pkt->size;
1526  uint8_t *buf = pkt->data;
1527  uint8_t *data = NULL;
1528  MpegTSWrite *ts = s->priv_data;
1529  MpegTSWriteStream *ts_st = st->priv_data;
1530  const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1531  int64_t dts = pkt->dts, pts = pkt->pts;
1532  int opus_samples = 0;
1533  int side_data_size;
1534  uint8_t *side_data = NULL;
1535  int stream_id = -1;
1536 
1537  side_data = av_packet_get_side_data(pkt,
1539  &side_data_size);
1540  if (side_data)
1541  stream_id = side_data[0];
1542 
1543  if (ts->reemit_pat_pmt) {
1545  "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1546  ts->reemit_pat_pmt = 0;
1548  }
1549 
1550  if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1551  ts->pat_packet_count = ts->pat_packet_period - 1;
1552  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1554  }
1555 
1556  if (ts->copyts < 1) {
1557  if (pts != AV_NOPTS_VALUE)
1558  pts += delay;
1559  if (dts != AV_NOPTS_VALUE)
1560  dts += delay;
1561  }
1562 
1563  if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1564  av_log(s, AV_LOG_ERROR, "first pts value must be set\n");
1565  return AVERROR_INVALIDDATA;
1566  }
1567  ts_st->first_pts_check = 0;
1568 
1569  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1570  const uint8_t *p = buf, *buf_end = p + size;
1571  uint32_t state = -1;
1572  int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
1573  int ret = ff_check_h264_startcode(s, st, pkt);
1574  if (ret < 0)
1575  return ret;
1576 
1577  if (extradd && AV_RB24(st->codecpar->extradata) > 1)
1578  extradd = 0;
1579 
1580  do {
1581  p = avpriv_find_start_code(p, buf_end, &state);
1582  av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", state & 0x1f);
1583  if ((state & 0x1f) == 7)
1584  extradd = 0;
1585  } while (p < buf_end && (state & 0x1f) != 9 &&
1586  (state & 0x1f) != 5 && (state & 0x1f) != 1);
1587 
1588  if ((state & 0x1f) != 5)
1589  extradd = 0;
1590  if ((state & 0x1f) != 9) { // AUD NAL
1591  data = av_malloc(pkt->size + 6 + extradd);
1592  if (!data)
1593  return AVERROR(ENOMEM);
1594  memcpy(data + 6, st->codecpar->extradata, extradd);
1595  memcpy(data + 6 + extradd, pkt->data, pkt->size);
1596  AV_WB32(data, 0x00000001);
1597  data[4] = 0x09;
1598  data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1599  buf = data;
1600  size = pkt->size + 6 + extradd;
1601  }
1602  } else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
1603  if (pkt->size < 2) {
1604  av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1605  return AVERROR_INVALIDDATA;
1606  }
1607  if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1608  int ret;
1609  AVPacket pkt2;
1610 
1611  if (!ts_st->amux) {
1612  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1613  "and extradata missing\n");
1614  } else {
1615  av_init_packet(&pkt2);
1616  pkt2.data = pkt->data;
1617  pkt2.size = pkt->size;
1618  av_assert0(pkt->dts != AV_NOPTS_VALUE);
1619  pkt2.dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
1620 
1621  ret = avio_open_dyn_buf(&ts_st->amux->pb);
1622  if (ret < 0)
1623  return AVERROR(ENOMEM);
1624 
1625  ret = av_write_frame(ts_st->amux, &pkt2);
1626  if (ret < 0) {
1627  ffio_free_dyn_buf(&ts_st->amux->pb);
1628  return ret;
1629  }
1630  size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1631  ts_st->amux->pb = NULL;
1632  buf = data;
1633  }
1634  }
1635  } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
1636  const uint8_t *p = buf, *buf_end = p + size;
1637  uint32_t state = -1;
1638  int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
1639  int ret = check_hevc_startcode(s, st, pkt);
1640  if (ret < 0)
1641  return ret;
1642 
1643  if (extradd && AV_RB24(st->codecpar->extradata) > 1)
1644  extradd = 0;
1645 
1646  do {
1647  p = avpriv_find_start_code(p, buf_end, &state);
1648  av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", (state & 0x7e)>>1);
1649  if ((state & 0x7e) == 2*32)
1650  extradd = 0;
1651  } while (p < buf_end && (state & 0x7e) != 2*35 &&
1652  (state & 0x7e) >= 2*32);
1653 
1654  if ((state & 0x7e) < 2*16 && (state & 0x7e) >= 2*24)
1655  extradd = 0;
1656  if ((state & 0x7e) != 2*35) { // AUD NAL
1657  data = av_malloc(pkt->size + 7 + extradd);
1658  if (!data)
1659  return AVERROR(ENOMEM);
1660  memcpy(data + 7, st->codecpar->extradata, extradd);
1661  memcpy(data + 7 + extradd, pkt->data, pkt->size);
1662  AV_WB32(data, 0x00000001);
1663  data[4] = 2*35;
1664  data[5] = 1;
1665  data[6] = 0x50; // any slice type (0x4) + rbsp stop one bit
1666  buf = data;
1667  size = pkt->size + 7 + extradd;
1668  }
1669  } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
1670  if (pkt->size < 2) {
1671  av_log(s, AV_LOG_ERROR, "Opus packet too short\n");
1672  return AVERROR_INVALIDDATA;
1673  }
1674 
1675  /* Add Opus control header */
1676  if ((AV_RB16(pkt->data) >> 5) != 0x3ff) {
1677  uint8_t *side_data;
1678  int side_data_size;
1679  int i, n;
1680  int ctrl_header_size;
1681  int trim_start = 0, trim_end = 0;
1682 
1683  opus_samples = opus_get_packet_samples(s, pkt);
1684 
1685  side_data = av_packet_get_side_data(pkt,
1687  &side_data_size);
1688 
1689  if (side_data && side_data_size >= 10) {
1690  trim_end = AV_RL32(side_data + 4) * 48000 / st->codecpar->sample_rate;
1691  }
1692 
1693  ctrl_header_size = pkt->size + 2 + pkt->size / 255 + 1;
1694  if (ts_st->opus_pending_trim_start)
1695  ctrl_header_size += 2;
1696  if (trim_end)
1697  ctrl_header_size += 2;
1698 
1699  data = av_malloc(ctrl_header_size);
1700  if (!data)
1701  return AVERROR(ENOMEM);
1702 
1703  data[0] = 0x7f;
1704  data[1] = 0xe0;
1705  if (ts_st->opus_pending_trim_start)
1706  data[1] |= 0x10;
1707  if (trim_end)
1708  data[1] |= 0x08;
1709 
1710  n = pkt->size;
1711  i = 2;
1712  do {
1713  data[i] = FFMIN(n, 255);
1714  n -= 255;
1715  i++;
1716  } while (n >= 0);
1717 
1718  av_assert0(2 + pkt->size / 255 + 1 == i);
1719 
1720  if (ts_st->opus_pending_trim_start) {
1721  trim_start = FFMIN(ts_st->opus_pending_trim_start, opus_samples);
1722  AV_WB16(data + i, trim_start);
1723  i += 2;
1724  ts_st->opus_pending_trim_start -= trim_start;
1725  }
1726  if (trim_end) {
1727  trim_end = FFMIN(trim_end, opus_samples - trim_start);
1728  AV_WB16(data + i, trim_end);
1729  i += 2;
1730  }
1731 
1732  memcpy(data + i, pkt->data, pkt->size);
1733  buf = data;
1734  size = ctrl_header_size;
1735  } else {
1736  /* TODO: Can we get TS formatted data here? If so we will
1737  * need to count the samples of that too! */
1738  av_log(s, AV_LOG_WARNING, "Got MPEG-TS formatted Opus data, unhandled");
1739  }
1740  }
1741 
1742  if (pkt->dts != AV_NOPTS_VALUE) {
1743  int i;
1744  for(i=0; i<s->nb_streams; i++) {
1745  AVStream *st2 = s->streams[i];
1746  MpegTSWriteStream *ts_st2 = st2->priv_data;
1747  if ( ts_st2->payload_size
1748  && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
1749  mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
1750  ts_st2->payload_pts, ts_st2->payload_dts,
1751  ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
1752  ts_st2->payload_size = 0;
1753  }
1754  }
1755  }
1756 
1757  if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
1758  (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
1759  av_compare_ts(dts - ts_st->payload_dts, st->time_base,
1760  s->max_delay, AV_TIME_BASE_Q) >= 0) ||
1761  ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) {
1762  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1763  ts_st->payload_pts, ts_st->payload_dts,
1764  ts_st->payload_flags & AV_PKT_FLAG_KEY, stream_id);
1765  ts_st->payload_size = 0;
1766  ts_st->opus_queued_samples = 0;
1767  }
1768 
1769  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
1770  av_assert0(!ts_st->payload_size);
1771  // for video and subtitle, write a single pes packet
1772  mpegts_write_pes(s, st, buf, size, pts, dts,
1773  pkt->flags & AV_PKT_FLAG_KEY, stream_id);
1774  ts_st->opus_queued_samples = 0;
1775  av_free(data);
1776  return 0;
1777  }
1778 
1779  if (!ts_st->payload_size) {
1780  ts_st->payload_pts = pts;
1781  ts_st->payload_dts = dts;
1782  ts_st->payload_flags = pkt->flags;
1783  }
1784 
1785  memcpy(ts_st->payload + ts_st->payload_size, buf, size);
1786  ts_st->payload_size += size;
1787  ts_st->opus_queued_samples += opus_samples;
1788 
1789  av_free(data);
1790 
1791  return 0;
1792 }
1793 
1795 {
1796  int i;
1797 
1798  /* flush current packets */
1799  for (i = 0; i < s->nb_streams; i++) {
1800  AVStream *st = s->streams[i];
1801  MpegTSWriteStream *ts_st = st->priv_data;
1802  if (ts_st->payload_size > 0) {
1803  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1804  ts_st->payload_pts, ts_st->payload_dts,
1805  ts_st->payload_flags & AV_PKT_FLAG_KEY, -1);
1806  ts_st->payload_size = 0;
1807  ts_st->opus_queued_samples = 0;
1808  }
1809  }
1810 }
1811 
1813 {
1814  if (!pkt) {
1815  mpegts_write_flush(s);
1816  return 1;
1817  } else {
1818  return mpegts_write_packet_internal(s, pkt);
1819  }
1820 }
1821 
1823 {
1824  if (s->pb)
1825  mpegts_write_flush(s);
1826 
1827  return 0;
1828 }
1829 
1831 {
1832  MpegTSWrite *ts = s->priv_data;
1833  MpegTSService *service;
1834  int i;
1835 
1836  for (i = 0; i < s->nb_streams; i++) {
1837  AVStream *st = s->streams[i];
1838  MpegTSWriteStream *ts_st = st->priv_data;
1839  if (ts_st) {
1840  av_freep(&ts_st->payload);
1841  if (ts_st->amux) {
1842  avformat_free_context(ts_st->amux);
1843  ts_st->amux = NULL;
1844  }
1845  }
1846  }
1847 
1848  for (i = 0; i < ts->nb_services; i++) {
1849  service = ts->services[i];
1850  av_freep(&service);
1851  }
1852  av_freep(&ts->services);
1853 }
1854 
1856 {
1857  int ret = 1;
1858  AVStream *st = s->streams[pkt->stream_index];
1859 
1860  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1861  if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
1862  (AV_RB24(pkt->data) != 0x000001 ||
1863  (st->codecpar->extradata_size > 0 &&
1864  st->codecpar->extradata[0] == 1)))
1865  ret = ff_stream_add_bitstream_filter(st, "h264_mp4toannexb", NULL);
1866  } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
1867  if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
1868  (AV_RB24(pkt->data) != 0x000001 ||
1869  (st->codecpar->extradata_size > 0 &&
1870  st->codecpar->extradata[0] == 1)))
1871  ret = ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL);
1872  }
1873 
1874  return ret;
1875 }
1876 
1877 static const AVOption options[] = {
1878  { "mpegts_transport_stream_id", "Set transport_stream_id field.",
1879  offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
1880  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1881  { "mpegts_original_network_id", "Set original_network_id field.",
1882  offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
1883  { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1884  { "mpegts_service_id", "Set service_id field.",
1885  offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
1886  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1887  { "mpegts_service_type", "Set service_type field.",
1888  offsetof(MpegTSWrite, service_type), AV_OPT_TYPE_INT,
1889  { .i64 = 0x01 }, 0x01, 0xff, AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1890  { "digital_tv", "Digital Television.",
1891  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff,
1892  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1893  { "digital_radio", "Digital Radio.",
1894  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff,
1895  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1896  { "teletext", "Teletext.",
1897  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff,
1898  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1899  { "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
1901  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1902  { "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
1903  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff,
1904  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1905  { "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
1907  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1908  { "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
1910  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1911  { "hevc_digital_hdtv", "HEVC Digital Television Service.",
1912  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff,
1913  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1914  { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
1915  offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
1916  { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
1917  { "mpegts_start_pid", "Set the first pid.",
1918  offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
1919  { .i64 = 0x0100 }, 0x0010, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
1920  { "mpegts_m2ts_mode", "Enable m2ts mode.",
1921  offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
1922  { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1923  { "muxrate", NULL,
1924  offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
1925  { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1926  { "pes_payload_size", "Minimum PES packet payload in bytes",
1927  offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT,
1928  { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1929  { "mpegts_flags", "MPEG-TS muxing flags",
1930  offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX,
1931  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1932  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1933  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX,
1934  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1935  { "latm", "Use LATM packetization for AAC",
1936  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX,
1937  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1938  { "pat_pmt_at_frames", "Reemit PAT and PMT at each video frame",
1939  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_PAT_PMT_AT_FRAMES}, 0, INT_MAX,
1940  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1941  { "system_b", "Conform to System B (DVB) instead of System A (ATSC)",
1942  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_SYSTEM_B }, 0, INT_MAX,
1943  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1944  { "initial_discontinuity", "Mark initial packets as discontinuous",
1945  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_DISCONT }, 0, INT_MAX,
1946  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1947  // backward compatibility
1948  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1949  offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT,
1950  { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1951  { "mpegts_copyts", "don't offset dts/pts",
1952  offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_BOOL,
1953  { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1954  { "tables_version", "set PAT, PMT and SDT version",
1955  offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT,
1956  { .i64 = 0 }, 0, 31, AV_OPT_FLAG_ENCODING_PARAM },
1957  { "omit_video_pes_length", "Omit the PES packet length for video packets",
1958  offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
1959  { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
1960  { "pcr_period", "PCR retransmission time in milliseconds",
1961  offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
1962  { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1963  { "pat_period", "PAT/PMT retransmission time limit in seconds",
1964  offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
1965  { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1966  { "sdt_period", "SDT retransmission time limit in seconds",
1967  offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
1968  { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1969  { NULL },
1970 };
1971 
1972 static const AVClass mpegts_muxer_class = {
1973  .class_name = "MPEGTS muxer",
1974  .item_name = av_default_item_name,
1975  .option = options,
1976  .version = LIBAVUTIL_VERSION_INT,
1977 };
1978 
1980  .name = "mpegts",
1981  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
1982  .mime_type = "video/MP2T",
1983  .extensions = "ts,m2t,m2ts,mts",
1984  .priv_data_size = sizeof(MpegTSWrite),
1985  .audio_codec = AV_CODEC_ID_MP2,
1986  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1987  .init = mpegts_init,
1990  .deinit = mpegts_deinit,
1991  .check_bitstream = mpegts_check_bitstream,
1993  .priv_class = &mpegts_muxer_class,
1994 };
const char * name
Definition: avisynth_c.h:867
#define NULL
Definition: coverity.c:32
#define SDT_PID
Definition: mpegts.h:37
const char const char void * val
Definition: avisynth_c.h:863
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define PMT_TID
Definition: mpegts.h:41
int size
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:385
#define SECTION_LENGTH
Definition: mpegtsenc.c:126
int pat_packet_period
Definition: mpegtsenc.c:84
#define STREAM_TYPE_AUDIO_MPEG2
Definition: mpeg.h:52
uint8_t provider_name[256]
Definition: mpegtsenc.c:58
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:1413
AVOption.
Definition: opt.h:246
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:878
#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:4886
#define STREAM_TYPE_VIDEO_CAVS
Definition: mpeg.h:59
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
Definition: mpegtsenc.c:273
int64_t payload_dts
Definition: mpegtsenc.c:239
static int mpegts_init(AVFormatContext *s)
Definition: mpegtsenc.c:772
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1478
const char * b
Definition: vf_curves.c:116
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
int64_t last_sdt_ts
Definition: mpegtsenc.c:115
#define AV_RB24
Definition: intreadwrite.h:64
#define PCR_TIME_BASE
Definition: mpegtsenc.c:37
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:839
#define STREAM_TYPE_AUDIO_EAC3
Definition: mpegts.h:64
void * priv_data
Definition: avformat.h:896
#define AV_DISPOSITION_CLEAN_EFFECTS
stream without voice
Definition: avformat.h:841
const char * key
int version
Definition: avisynth_c.h:858
#define PCR_RETRANS_TIME
Definition: mpegtsenc.c:228
static AVPacket pkt
int64_t first_pcr
Definition: mpegtsenc.c:88
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:476
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1384
double pat_period
Definition: mpegtsenc.c:112
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1666
Format I/O context.
Definition: avformat.h:1358
#define MPEGTS_FLAG_DISCONT
Definition: mpegtsenc.c:108
int first_pts_check
first pts check needed
Definition: mpegtsenc.c:236
unsigned int nb_stream_indexes
Definition: avformat.h:1280
#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:203
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
#define AV_RB32
Definition: intreadwrite.h:130
void * opaque
Definition: mpegtsenc.c:51
#define DEFAULT_PES_PAYLOAD_SIZE
Definition: mpegtsenc.c:122
int id
Format-specific stream ID.
Definition: avformat.h:888
#define STREAM_TYPE_VIDEO_HEVC
Definition: mpeg.h:58
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4459
static int mpegts_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
Definition: mpegtsenc.c:1855
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
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:144
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1812
Public header for CRC hash function implementation.
static int encode_str8(uint8_t *buf, const char *str)
Definition: mpegtsenc.c:688
int initial_padding
Audio only.
Definition: avcodec.h:4086
const char data[16]
Definition: mxf.c:91
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:1080
uint8_t * data
Definition: avcodec.h:1477
uint32_t tag
Definition: movenc.c:1496
#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:38
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:294
static void mpegts_write_sdt(AVFormatContext *s)
Definition: mpegtsenc.c:646
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
#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:1156
unsigned int * stream_index
Definition: avformat.h:1279
#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)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
#define STREAM_TYPE_AUDIO_AAC
Definition: mpeg.h:55
MPEGTS stream ID as uint8_t, this is required to pass the stream ID information from the demuxer to t...
Definition: avcodec.h:1359
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:1176
AVProgram * av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
Find the programs which belong to a given stream.
Definition: utils.c:4169
static void mpegts_prefix_m2ts_header(AVFormatContext *s)
Definition: mpegtsenc.c:753
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2040
#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:1598
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:1125
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
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:186
static uint8_t * get_ts_payload_start(uint8_t *pkt)
Definition: mpegtsenc.c:1164
char * url
input or output URL.
Definition: avformat.h:1454
unsigned int nb_programs
Definition: avformat.h:1537
#define STREAM_TYPE_VIDEO_DIRAC
Definition: mpegts.h:59
static void set_af_flag(uint8_t *pkt, int flag)
Definition: mpegtsenc.c:1140
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:565
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
Definition: mpegtsenc.c:1096
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
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
#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:1522
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1275
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
Definition: mpegtsenc.c:1065
#define FFMAX(a, b)
Definition: common.h:94
static MpegTSService * mpegts_add_service(AVFormatContext *s, int sid, const char *provider_name, const char *name)
Definition: mpegtsenc.c:720
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:1746
#define fail()
Definition: checkasm.h:120
#define MPEGTS_FLAG_SYSTEM_B
Definition: mpegtsenc.c:107
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
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:3975
double sdt_period
Definition: mpegtsenc.c:113
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1414
#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:1438
static const AVOption options[]
Definition: mpegtsenc.c:1877
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:254
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:508
#define FFMIN(a, b)
Definition: common.h:96
#define STREAM_TYPE_VIDEO_H264
Definition: mpeg.h:57
static struct @313 state
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:505
#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:392
int discontinuity
Definition: mpegtsenc.c:49
static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Definition: mpegtsenc.c:1455
#define s(width, name)
Definition: cbs_vp9.c:257
AVOutputFormat ff_mpegts_muxer
Definition: mpegtsenc.c:1979
#define AV_RL32
Definition: intreadwrite.h:146
int n
Definition: avisynth_c.h:760
AVFormatContext * amux
Definition: mpegtsenc.c:242
AVDictionary * metadata
Definition: avformat.h:945
#define MPEGTS_FLAG_REEMIT_PAT_PMT
Definition: mpegtsenc.c:104
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
#define STREAM_TYPE_VIDEO_MPEG4
Definition: mpeg.h:56
static void mpegts_deinit(AVFormatContext *s)
Definition: mpegtsenc.c:1830
ff_const59 struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1377
int pmt_start_pid
Definition: mpegtsenc.c:97
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1443
int service_type
Definition: mpegtsenc.c:95
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:840
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:50
Stream structure.
Definition: avformat.h:881
#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:2215
#define DVB_PRIVATE_NETWORK_START
Definition: mpegtsenc.c:41
typedef void(RENAME(mix_any_func_type))
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
int sdt_packet_period
Definition: mpegtsenc.c:82
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES
Definition: mpegtsenc.c:106
const AVClass * av_class
Definition: mpegtsenc.c:77
#define STREAM_TYPE_PRIVATE_DATA
Definition: mpeg.h:54
void * buf
Definition: avisynth_c.h:766
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:1474
int original_network_id
Definition: mpegtsenc.c:93
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2631
ff_const59 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:51
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:1300
byte swapping routines
static int mpegts_write_end(AVFormatContext *s)
Definition: mpegtsenc.c:1822
#define STREAM_TYPE_AUDIO_AC3
Definition: mpeg.h:61
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4393
static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
Definition: mpegtsenc.c:1038
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:765
uint8_t * payload
Definition: mpegtsenc.c:241
AVDictionary * metadata
Definition: avformat.h:1281
static int64_t pts
static void mpegts_write_flush(AVFormatContext *s)
Definition: mpegtsenc.c:1794
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int m2ts_mode
Definition: mpegtsenc.c:99
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
int sample_rate
Audio only.
Definition: avcodec.h:4067
#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
#define flag(name)
Definition: cbs_av1.c:553
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
Definition: mpegtsenc.c:747
uint8_t name[256]
Definition: mpegtsenc.c:57
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:934
static void putbuf(uint8_t **q_ptr, const uint8_t *buf, size_t len)
Definition: mpegtsenc.c:267
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:932
int den
Denominator.
Definition: rational.h:60
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:470
#define av_free(p)
char * value
Definition: dict.h:87
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:5516
void * priv_data
Format private data.
Definition: avformat.h:1386
MpegTSSection pat
Definition: mpegtsenc.c:78
int reemit_pat_pmt
Definition: mpegtsenc.c:101
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition: avformat.h:471
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:3971
int channels
Audio only.
Definition: avcodec.h:4063
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1476
int pat_packet_count
Definition: mpegtsenc.c:83
static const AVClass mpegts_muxer_class
Definition: mpegtsenc.c:1972
#define av_freep(p)
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
#define av_malloc_array(a, b)
int opus_pending_trim_start
Definition: mpegtsenc.c:247
int stream_index
Definition: avcodec.h:1479
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:910
#define MKTAG(a, b, c, d)
Definition: common.h:366
#define STREAM_TYPE_AUDIO_MPEG1
Definition: mpeg.h:51
This structure stores compressed data.
Definition: avcodec.h:1454
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:1470
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define STREAM_TYPE_AUDIO_TRUEHD
Definition: mpegts.h:63
AVProgram ** programs
Definition: avformat.h:1538
#define SDT_TID
Definition: mpegts.h:43
int tables_version
Definition: mpegtsenc.c:111