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