FFmpeg
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 
31 #include "libavcodec/bytestream.h"
32 #include "libavcodec/defs.h"
33 #include "libavcodec/h264.h"
34 #include "libavcodec/startcode.h"
35 
36 #include "avformat.h"
37 #include "avio_internal.h"
38 #include "internal.h"
39 #include "mpegts.h"
40 #include "mux.h"
41 
42 #define PCR_TIME_BASE 27000000
43 
44 /* write DVB SI sections */
45 
46 #define DVB_PRIVATE_NETWORK_START 0xff01
47 
48 /*********************************************/
49 /* mpegts section writer */
50 
51 typedef struct MpegTSSection {
52  int pid;
53  int cc;
55  void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
56  void *opaque;
58 
59 typedef struct MpegTSService {
60  MpegTSSection pmt; /* MPEG-2 PMT table context */
61  int sid; /* service ID */
62  uint8_t name[256];
63  uint8_t provider_name[256];
64  int pcr_pid;
67 
68 // service_type values as defined in ETSI 300 468
69 enum {
78 };
79 typedef struct MpegTSWrite {
80  const AVClass *av_class;
81  MpegTSSection pat; /* MPEG-2 PAT table */
82  MpegTSSection sdt; /* MPEG-2 SDT table context */
83  MpegTSSection nit; /* MPEG-2 NIT table context */
86  int64_t sdt_period; /* SDT period in PCR time base */
87  int64_t pat_period; /* PAT/PMT period in PCR time base */
88  int64_t nit_period; /* NIT period in PCR time base */
90  int64_t first_pcr;
92  int64_t next_pcr;
93  int mux_rate; ///< set to 1 when VBR
95  int64_t total_size;
96 
101 
109 
111 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
112 #define MPEGTS_FLAG_AAC_LATM 0x02
113 #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
114 #define MPEGTS_FLAG_SYSTEM_B 0x08
115 #define MPEGTS_FLAG_DISCONT 0x10
116 #define MPEGTS_FLAG_NIT 0x20
117 #define MPEGTS_FLAG_OMIT_RAI 0x40
118  int flags;
119  int copyts;
121  int64_t pat_period_us;
122  int64_t sdt_period_us;
123  int64_t nit_period_us;
124  int64_t last_pat_ts;
125  int64_t last_sdt_ts;
126  int64_t last_nit_ts;
127 
128  uint8_t provider_name[256];
129 
131 } MpegTSWrite;
132 
133 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
134 #define DEFAULT_PES_HEADER_FREQ 16
135 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
136 
137 /* The section length is 12 bits. The first 2 are set to 0, the remaining
138  * 10 bits should not exceed 1021. */
139 #define SECTION_LENGTH 1020
140 
141 /* NOTE: 4 bytes must be left at the end for the crc32 */
142 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
143 {
144  unsigned int crc;
145  unsigned char packet[TS_PACKET_SIZE];
146  const unsigned char *buf_ptr;
147  unsigned char *q;
148  int first, b, len1, left;
149 
151  -1, buf, len - 4));
152 
153  buf[len - 4] = (crc >> 24) & 0xff;
154  buf[len - 3] = (crc >> 16) & 0xff;
155  buf[len - 2] = (crc >> 8) & 0xff;
156  buf[len - 1] = crc & 0xff;
157 
158  /* send each packet */
159  buf_ptr = buf;
160  while (len > 0) {
161  first = buf == buf_ptr;
162  q = packet;
163  *q++ = 0x47;
164  b = s->pid >> 8;
165  if (first)
166  b |= 0x40;
167  *q++ = b;
168  *q++ = s->pid;
169  s->cc = s->cc + 1 & 0xf;
170  *q++ = 0x10 | s->cc;
171  if (s->discontinuity) {
172  q[-1] |= 0x20;
173  *q++ = 1;
174  *q++ = 0x80;
175  s->discontinuity = 0;
176  }
177  if (first)
178  *q++ = 0; /* 0 offset */
179  len1 = TS_PACKET_SIZE - (q - packet);
180  if (len1 > len)
181  len1 = len;
182  memcpy(q, buf_ptr, len1);
183  q += len1;
184  /* add known padding data */
185  left = TS_PACKET_SIZE - (q - packet);
186  if (left > 0)
187  memset(q, 0xff, left);
188 
189  s->write_packet(s, packet);
190 
191  buf_ptr += len1;
192  len -= len1;
193  }
194 }
195 
196 static inline void put16(uint8_t **q_ptr, int val)
197 {
198  uint8_t *q;
199  q = *q_ptr;
200  *q++ = val >> 8;
201  *q++ = val;
202  *q_ptr = q;
203 }
204 
205 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
206  int version, int sec_num, int last_sec_num,
207  uint8_t *buf, int len)
208 {
209  uint8_t section[1024], *q;
210  unsigned int tot_len;
211  /* reserved_future_use field must be set to 1 for SDT and NIT */
212  unsigned int flags = (tid == SDT_TID || tid == NIT_TID) ? 0xf000 : 0xb000;
213 
214  tot_len = 3 + 5 + len + 4;
215  /* check if not too big */
216  if (tot_len > 1024)
217  return AVERROR_INVALIDDATA;
218 
219  q = section;
220  *q++ = tid;
221  put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
222  put16(&q, id);
223  *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
224  *q++ = sec_num;
225  *q++ = last_sec_num;
226  memcpy(q, buf, len);
227 
228  mpegts_write_section(s, section, tot_len);
229  return 0;
230 }
231 
232 /*********************************************/
233 /* mpegts writer */
234 
235 #define DEFAULT_PROVIDER_NAME "FFmpeg"
236 #define DEFAULT_SERVICE_NAME "Service"
237 
238 /* we retransmit the SI info at this rate */
239 #define SDT_RETRANS_TIME 500
240 #define PAT_RETRANS_TIME 100
241 #define PCR_RETRANS_TIME 20
242 #define NIT_RETRANS_TIME 500
243 
244 typedef struct MpegTSWriteStream {
245  int pid; /* stream associated pid */
246  int cc;
249  int first_timestamp_checked; ///< first pts/dts check needed
251  int64_t payload_pts;
252  int64_t payload_dts;
254  uint8_t *payload;
257 
258  int64_t pcr_period; /* PCR period in PCR time base */
259  int64_t last_pcr;
260 
261  /* For Opus */
264 
267 
269 {
270  MpegTSWrite *ts = s->priv_data;
271  MpegTSService *service;
272  uint8_t data[SECTION_LENGTH], *q;
273  int i;
274 
275  q = data;
276  if (ts->flags & MPEGTS_FLAG_NIT) {
277  put16(&q, 0x0000);
278  put16(&q, NIT_PID);
279  }
280  for (i = 0; i < ts->nb_services; i++) {
281  service = ts->services[i];
282  put16(&q, service->sid);
283  put16(&q, 0xe000 | service->pmt.pid);
284  }
286  data, q - data);
287 }
288 
289 static void putbuf(uint8_t **q_ptr, const uint8_t *buf, size_t len)
290 {
291  memcpy(*q_ptr, buf, len);
292  *q_ptr += len;
293 }
294 
295 static int put_arib_caption_descriptor(AVFormatContext *s, uint8_t **q_ptr,
296  AVCodecParameters *codecpar)
297 {
298  uint8_t stream_identifier;
299  uint16_t data_component_id;
300  uint8_t *q = *q_ptr;
301 
302  switch (codecpar->profile) {
304  stream_identifier = 0x30;
305  data_component_id = 0x0008;
306  break;
308  stream_identifier = 0x87;
309  data_component_id = 0x0012;
310  break;
311  default:
313  "Unset/unknown ARIB caption profile %d utilized!\n",
314  codecpar->profile);
315  return AVERROR_INVALIDDATA;
316  }
317 
318  // stream_identifier_descriptor
319  *q++ = 0x52; // descriptor_tag
320  *q++ = 1; // descriptor_length
321  *q++ = stream_identifier; // component_tag: stream_identifier
322 
323  // data_component_descriptor, defined in ARIB STD-B10, part 2, 6.2.20
324  *q++ = 0xFD; // descriptor_tag: ARIB data coding type descriptor
325  *q++ = 3; // descriptor_length
326  put16(&q, data_component_id); // data_component_id
327  // additional_arib_caption_info: defined in ARIB STD-B24, fascicle 1, Part 3, 9.6.1
328  // Here we utilize a pre-defined set of values defined in ARIB TR-B14,
329  // Fascicle 2, 4.2.8.5 for PMT usage, with the reserved bits in the middle
330  // set to 1 (as that is what every broadcaster seems to be doing in
331  // production).
332  *q++ = 0x3D; // DMF('0011'), Reserved('11'), Timing('01')
333 
334  *q_ptr = q;
335 
336  return 0;
337 }
338 
339 static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
340 {
341  uint8_t *q = *q_ptr;
343  *q++ = 4;
344  *q++ = tag;
345  *q++ = tag >> 8;
346  *q++ = tag >> 16;
347  *q++ = tag >> 24;
348  *q_ptr = q;
349 }
350 
352 {
353  MpegTSWrite *ts = s->priv_data;
354  MpegTSWriteStream *ts_st = st->priv_data;
355  int stream_type;
356 
357  switch (st->codecpar->codec_id) {
360  stream_type = STREAM_TYPE_VIDEO_MPEG2;
361  break;
362  case AV_CODEC_ID_MPEG4:
363  stream_type = STREAM_TYPE_VIDEO_MPEG4;
364  break;
365  case AV_CODEC_ID_H264:
366  stream_type = STREAM_TYPE_VIDEO_H264;
367  break;
368  case AV_CODEC_ID_HEVC:
369  stream_type = STREAM_TYPE_VIDEO_HEVC;
370  break;
371  case AV_CODEC_ID_CAVS:
372  stream_type = STREAM_TYPE_VIDEO_CAVS;
373  break;
374  case AV_CODEC_ID_AVS2:
375  stream_type = STREAM_TYPE_VIDEO_AVS2;
376  break;
377  case AV_CODEC_ID_AVS3:
378  stream_type = STREAM_TYPE_VIDEO_AVS3;
379  break;
380  case AV_CODEC_ID_DIRAC:
381  stream_type = STREAM_TYPE_VIDEO_DIRAC;
382  break;
383  case AV_CODEC_ID_VC1:
384  stream_type = STREAM_TYPE_VIDEO_VC1;
385  break;
386  case AV_CODEC_ID_MP2:
387  case AV_CODEC_ID_MP3:
388  if ( st->codecpar->sample_rate > 0
389  && st->codecpar->sample_rate < 32000) {
390  stream_type = STREAM_TYPE_AUDIO_MPEG2;
391  } else {
392  stream_type = STREAM_TYPE_AUDIO_MPEG1;
393  }
394  break;
395  case AV_CODEC_ID_AAC:
396  stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
399  break;
401  stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
402  break;
403  case AV_CODEC_ID_AC3:
404  stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
407  break;
408  case AV_CODEC_ID_EAC3:
409  stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
412  break;
413  case AV_CODEC_ID_DTS:
414  stream_type = STREAM_TYPE_AUDIO_DTS;
415  break;
416  case AV_CODEC_ID_TRUEHD:
417  stream_type = STREAM_TYPE_AUDIO_TRUEHD;
418  break;
419  case AV_CODEC_ID_OPUS:
420  stream_type = STREAM_TYPE_PRIVATE_DATA;
421  break;
423  stream_type = STREAM_TYPE_METADATA;
424  break;
426  stream_type = STREAM_TYPE_PRIVATE_DATA;
427  break;
431  stream_type = STREAM_TYPE_PRIVATE_DATA;
432  break;
434  if (st->codecpar->profile == AV_PROFILE_KLVA_SYNC) {
435  stream_type = STREAM_TYPE_METADATA;
436  } else {
437  stream_type = STREAM_TYPE_PRIVATE_DATA;
438  }
439  break;
440  default:
442  "Stream %d, codec %s, is muxed as a private data stream "
443  "and may not be recognized upon reading.\n", st->index,
445  stream_type = STREAM_TYPE_PRIVATE_DATA;
446  break;
447  }
448 
449  return stream_type;
450 }
451 
453 {
454  int stream_type;
455  MpegTSWriteStream *ts_st = st->priv_data;
456 
457  switch (st->codecpar->codec_id) {
459  stream_type = STREAM_TYPE_VIDEO_MPEG2;
460  break;
461  case AV_CODEC_ID_H264:
462  stream_type = STREAM_TYPE_VIDEO_H264;
463  break;
464  case AV_CODEC_ID_VC1:
465  stream_type = STREAM_TYPE_VIDEO_VC1;
466  break;
467  case AV_CODEC_ID_HEVC:
468  stream_type = STREAM_TYPE_VIDEO_HEVC;
469  break;
471  stream_type = 0x80;
472  break;
473  case AV_CODEC_ID_AC3:
474  stream_type = 0x81;
475  break;
476  case AV_CODEC_ID_DTS:
477  stream_type = (st->codecpar->ch_layout.nb_channels > 6) ? 0x85 : 0x82;
478  break;
479  case AV_CODEC_ID_TRUEHD:
480  stream_type = 0x83;
481  break;
482  case AV_CODEC_ID_EAC3:
483  stream_type = 0x84;
484  break;
486  stream_type = 0x90;
487  break;
489  stream_type = 0x92;
490  break;
491  default:
493  "Stream %d, codec %s, is muxed as a private data stream "
494  "and may not be recognized upon reading.\n", st->index,
496  stream_type = STREAM_TYPE_PRIVATE_DATA;
497  break;
498  }
499 
500  return stream_type;
501 }
502 
504 {
505  MpegTSWrite *ts = s->priv_data;
506  uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
507  int val, stream_type, i, err = 0;
508 
509  q = data;
510  put16(&q, 0xe000 | service->pcr_pid);
511 
512  program_info_length_ptr = q;
513  q += 2; /* patched after */
514 
515  /* put program info here */
516  if (ts->m2ts_mode) {
517  put_registration_descriptor(&q, MKTAG('H', 'D', 'M', 'V'));
518  *q++ = 0x88; // descriptor_tag - hdmv_copy_control_descriptor
519  *q++ = 0x04; // descriptor_length
520  put16(&q, 0x0fff); // CA_System_ID
521  *q++ = 0xfc; // private_data_byte
522  *q++ = 0xfc; // private_data_byte
523  }
524 
525  val = 0xf000 | (q - program_info_length_ptr - 2);
526  program_info_length_ptr[0] = val >> 8;
527  program_info_length_ptr[1] = val;
528 
529  for (i = 0; i < s->nb_streams; i++) {
530  AVStream *st = s->streams[i];
531  MpegTSWriteStream *ts_st = st->priv_data;
532  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
533  const char default_language[] = "und";
534  const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
535  enum AVCodecID codec_id = st->codecpar->codec_id;
536 
537  if (s->nb_programs) {
538  int k, found = 0;
539  AVProgram *program = service->program;
540 
541  for (k = 0; k < program->nb_stream_indexes; k++)
542  if (program->stream_index[k] == i) {
543  found = 1;
544  break;
545  }
546 
547  if (!found)
548  continue;
549  }
550 
551  if (q - data > SECTION_LENGTH - 32) {
552  err = 1;
553  break;
554  }
555 
556  stream_type = ts->m2ts_mode ? get_m2ts_stream_type(s, st) : get_dvb_stream_type(s, st);
557 
558  *q++ = stream_type;
559  put16(&q, 0xe000 | ts_st->pid);
560  desc_length_ptr = q;
561  q += 2; /* patched after */
562 
563  /* write optional descriptors here */
564  switch (st->codecpar->codec_type) {
565  case AVMEDIA_TYPE_AUDIO:
566  if (codec_id == AV_CODEC_ID_AC3)
567  put_registration_descriptor(&q, MKTAG('A', 'C', '-', '3'));
568  if (codec_id == AV_CODEC_ID_EAC3)
569  put_registration_descriptor(&q, MKTAG('E', 'A', 'C', '3'));
570  if (ts->flags & MPEGTS_FLAG_SYSTEM_B) {
571  if (codec_id == AV_CODEC_ID_AC3) {
572  DVBAC3Descriptor *dvb_ac3_desc = ts_st->dvb_ac3_desc;
573 
574  *q++=0x6a; // AC3 descriptor see A038 DVB SI
575  if (dvb_ac3_desc) {
576  int len = 1 +
577  !!(dvb_ac3_desc->component_type_flag) +
578  !!(dvb_ac3_desc->bsid_flag) +
579  !!(dvb_ac3_desc->mainid_flag) +
580  !!(dvb_ac3_desc->asvc_flag);
581 
582  *q++ = len;
583  *q++ = dvb_ac3_desc->component_type_flag << 7 | dvb_ac3_desc->bsid_flag << 6 |
584  dvb_ac3_desc->mainid_flag << 5 | dvb_ac3_desc->asvc_flag << 4;
585 
586  if (dvb_ac3_desc->component_type_flag) *q++ = dvb_ac3_desc->component_type;
587  if (dvb_ac3_desc->bsid_flag) *q++ = dvb_ac3_desc->bsid;
588  if (dvb_ac3_desc->mainid_flag) *q++ = dvb_ac3_desc->mainid;
589  if (dvb_ac3_desc->asvc_flag) *q++ = dvb_ac3_desc->asvc;
590  } else {
591  *q++=1; // 1 byte, all flags sets to 0
592  *q++=0; // omit all fields...
593  }
594  } else if (codec_id == AV_CODEC_ID_EAC3) {
595  *q++=0x7a; // EAC3 descriptor see A038 DVB SI
596  *q++=1; // 1 byte, all flags sets to 0
597  *q++=0; // omit all fields...
598  }
599  }
601  put_registration_descriptor(&q, MKTAG('B', 'S', 'S', 'D'));
602  if (codec_id == AV_CODEC_ID_OPUS) {
603  int ch = st->codecpar->ch_layout.nb_channels;
604 
605  /* 6 bytes registration descriptor, 4 bytes Opus audio descriptor */
606  if (q - data > SECTION_LENGTH - 6 - 4) {
607  err = 1;
608  break;
609  }
610 
611  put_registration_descriptor(&q, MKTAG('O', 'p', 'u', 's'));
612 
613  *q++ = 0x7f; /* DVB extension descriptor */
614  *q++ = 2;
615  *q++ = 0x80;
616 
617  if (st->codecpar->extradata && st->codecpar->extradata_size >= 19) {
618  if (st->codecpar->extradata[18] == 0 && ch <= 2) {
619  /* RTP mapping family */
620  *q++ = ch;
621  } else if (st->codecpar->extradata[18] == 1 && ch <= 8 &&
622  st->codecpar->extradata_size >= 21 + ch) {
623  static const uint8_t coupled_stream_counts[9] = {
624  1, 0, 1, 1, 2, 2, 2, 3, 3
625  };
626  static const uint8_t channel_map_a[8][8] = {
627  {0},
628  {0, 1},
629  {0, 2, 1},
630  {0, 1, 2, 3},
631  {0, 4, 1, 2, 3},
632  {0, 4, 1, 2, 3, 5},
633  {0, 4, 1, 2, 3, 5, 6},
634  {0, 6, 1, 2, 3, 4, 5, 7},
635  };
636  static const uint8_t channel_map_b[8][8] = {
637  {0},
638  {0, 1},
639  {0, 1, 2},
640  {0, 1, 2, 3},
641  {0, 1, 2, 3, 4},
642  {0, 1, 2, 3, 4, 5},
643  {0, 1, 2, 3, 4, 5, 6},
644  {0, 1, 2, 3, 4, 5, 6, 7},
645  };
646  /* Vorbis mapping family */
647 
648  if (st->codecpar->extradata[19] == ch - coupled_stream_counts[ch] &&
649  st->codecpar->extradata[20] == coupled_stream_counts[ch] &&
650  memcmp(&st->codecpar->extradata[21], channel_map_a[ch - 1], ch) == 0) {
651  *q++ = ch;
652  } else if (ch >= 2 && st->codecpar->extradata[19] == ch &&
653  st->codecpar->extradata[20] == 0 &&
654  memcmp(&st->codecpar->extradata[21], channel_map_b[ch - 1], ch) == 0) {
655  *q++ = ch | 0x80;
656  } else {
657  /* Unsupported, could write an extended descriptor here */
658  av_log(s, AV_LOG_ERROR, "Unsupported Opus Vorbis-style channel mapping");
659  *q++ = 0xff;
660  }
661  } else {
662  /* Unsupported */
663  av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping for family %d", st->codecpar->extradata[18]);
664  *q++ = 0xff;
665  }
666  } else if (ch <= 2) {
667  /* Assume RTP mapping family */
668  *q++ = ch;
669  } else {
670  /* Unsupported */
671  av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping");
672  *q++ = 0xff;
673  }
674  }
675 
676  if (language != default_language ||
680  const char *p, *next;
681  uint8_t *len_ptr;
682 
684  len_ptr = q++;
685  *len_ptr = 0;
686 
687  for (p = next = language; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
688  if (q - data > SECTION_LENGTH - 4) {
689  err = 1;
690  break;
691  }
692  next = strchr(p, ',');
693  if (strlen(p) != 3 && (!next || next != p + 3))
694  continue; /* not a 3-letter code */
695 
696  *q++ = *p++;
697  *q++ = *p++;
698  *q++ = *p++;
699 
701  *q++ = 0x01;
703  *q++ = 0x02;
705  *q++ = 0x03;
706  else
707  *q++ = 0; /* undefined type */
708 
709  *len_ptr += 4;
710  }
711 
712  if (*len_ptr == 0)
713  q -= 2; /* no language codes were written */
714  }
715  break;
718  uint8_t *len_ptr;
719  int extradata_copied = 0;
720 
721  *q++ = 0x59; /* subtitling_descriptor */
722  len_ptr = q++;
723 
724  while (strlen(language) >= 3) {
725  if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
726  err = 1;
727  break;
728  }
729  *q++ = *language++;
730  *q++ = *language++;
731  *q++ = *language++;
732  /* Skip comma */
733  if (*language != '\0')
734  language++;
735 
736  if (st->codecpar->extradata_size - extradata_copied >= 5) {
737  *q++ = st->codecpar->extradata[extradata_copied + 4]; /* subtitling_type */
738  memcpy(q, st->codecpar->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
739  extradata_copied += 5;
740  q += 4;
741  } else {
742  /* subtitling_type:
743  * 0x10 - normal with no monitor aspect ratio criticality
744  * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
745  *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
746  if ((st->codecpar->extradata_size == 4) && (extradata_copied == 0)) {
747  /* support of old 4-byte extradata format */
748  memcpy(q, st->codecpar->extradata, 4); /* composition_page_id and ancillary_page_id */
749  extradata_copied += 4;
750  q += 4;
751  } else {
752  put16(&q, 1); /* composition_page_id */
753  put16(&q, 1); /* ancillary_page_id */
754  }
755  }
756  }
757 
758  *len_ptr = q - len_ptr - 1;
759  } else if (codec_id == AV_CODEC_ID_DVB_TELETEXT) {
760  uint8_t *len_ptr = NULL;
761  int extradata_copied = 0;
762 
763  /* The descriptor tag. teletext_descriptor */
764  *q++ = 0x56;
765  len_ptr = q++;
766 
767  while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
768  *q++ = *language++;
769  *q++ = *language++;
770  *q++ = *language++;
771  /* Skip comma */
772  if (*language != '\0')
773  language++;
774 
775  if (st->codecpar->extradata_size - 1 > extradata_copied) {
776  memcpy(q, st->codecpar->extradata + extradata_copied, 2);
777  extradata_copied += 2;
778  q += 2;
779  } else {
780  /* The Teletext descriptor:
781  * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
782  * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
783  * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
784  *q++ = 0x08;
785  *q++ = 0x00;
786  }
787  }
788 
789  *len_ptr = q - len_ptr - 1;
790  } else if (codec_id == AV_CODEC_ID_ARIB_CAPTION) {
791  if (put_arib_caption_descriptor(s, &q, st->codecpar) < 0)
792  break;
793  }
794  break;
795  case AVMEDIA_TYPE_VIDEO:
796  if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
797  put_registration_descriptor(&q, MKTAG('d', 'r', 'a', 'c'));
798  } else if (stream_type == STREAM_TYPE_VIDEO_VC1) {
799  put_registration_descriptor(&q, MKTAG('V', 'C', '-', '1'));
800  } else if (stream_type == STREAM_TYPE_VIDEO_HEVC && s->strict_std_compliance <= FF_COMPLIANCE_NORMAL) {
801  put_registration_descriptor(&q, MKTAG('H', 'E', 'V', 'C'));
802  } else if (stream_type == STREAM_TYPE_VIDEO_CAVS || stream_type == STREAM_TYPE_VIDEO_AVS2 ||
803  stream_type == STREAM_TYPE_VIDEO_AVS3) {
804  put_registration_descriptor(&q, MKTAG('A', 'V', 'S', 'V'));
805  }
806  break;
807  case AVMEDIA_TYPE_DATA:
809  put_registration_descriptor(&q, MKTAG('K', 'L', 'V', 'A'));
810  } else if (codec_id == AV_CODEC_ID_SMPTE_2038) {
811  put_registration_descriptor(&q, MKTAG('V', 'A', 'N', 'C'));
812  } else if (codec_id == AV_CODEC_ID_TIMED_ID3) {
813  const char *tag = "ID3 ";
814  *q++ = METADATA_DESCRIPTOR;
815  *q++ = 13;
816  put16(&q, 0xffff); /* metadata application format */
817  putbuf(&q, tag, strlen(tag));
818  *q++ = 0xff; /* metadata format */
819  putbuf(&q, tag, strlen(tag));
820  *q++ = 0; /* metadata service ID */
821  *q++ = 0xF; /* metadata_locator_record_flag|MPEG_carriage_flags|reserved */
822  }
823  break;
824  }
825 
826  val = 0xf000 | (q - desc_length_ptr - 2);
827  desc_length_ptr[0] = val >> 8;
828  desc_length_ptr[1] = val;
829  }
830 
831  if (err)
833  "The PMT section cannot fit stream %d and all following streams.\n"
834  "Try reducing the number of languages in the audio streams "
835  "or the total number of streams.\n", i);
836 
837  mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
838  data, q - data);
839  return 0;
840 }
841 
843 {
844  MpegTSWrite *ts = s->priv_data;
845  MpegTSService *service;
846  uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
847  int i, running_status, free_ca_mode, val;
848 
849  q = data;
850  put16(&q, ts->original_network_id);
851  *q++ = 0xff;
852  for (i = 0; i < ts->nb_services; i++) {
853  service = ts->services[i];
854  put16(&q, service->sid);
855  *q++ = 0xfc | 0x00; /* currently no EIT info */
856  desc_list_len_ptr = q;
857  q += 2;
858  running_status = 4; /* running */
859  free_ca_mode = 0;
860 
861  /* write only one descriptor for the service name and provider */
862  *q++ = 0x48;
863  desc_len_ptr = q;
864  q++;
865  *q++ = ts->service_type;
866  putbuf(&q, service->provider_name, service->provider_name[0] + 1);
867  putbuf(&q, service->name, service->name[0] + 1);
868  desc_len_ptr[0] = q - desc_len_ptr - 1;
869 
870  /* fill descriptor length */
871  val = (running_status << 13) | (free_ca_mode << 12) |
872  (q - desc_list_len_ptr - 2);
873  desc_list_len_ptr[0] = val >> 8;
874  desc_list_len_ptr[1] = val;
875  }
877  data, q - data);
878 }
879 
881 {
882  MpegTSWrite *ts = s->priv_data;
883  uint8_t data[SECTION_LENGTH], *q, *desc_len_ptr, *loop_len_ptr;
884 
885  q = data;
886 
887  //network_descriptors_length
888  put16(&q, 0xf000 | (ts->provider_name[0] + 2));
889 
890  //network_name_descriptor
891  *q++ = 0x40;
892  putbuf(&q, ts->provider_name, ts->provider_name[0] + 1);
893 
894  //transport_stream_loop_length
895  loop_len_ptr = q;
896  q += 2;
897 
898  put16(&q, ts->transport_stream_id);
899  put16(&q, ts->original_network_id);
900 
901  //transport_descriptors_length
902  desc_len_ptr = q;
903  q += 2;
904 
905  //service_list_descriptor
906  *q++ = 0x41;
907  *q++ = 3 * ts->nb_services;
908  for (int i = 0; i < ts->nb_services; i++) {
909  put16(&q, ts->services[i]->sid);
910  *q++ = ts->service_type;
911  }
912 
913  //calculate lengths
914  put16(&desc_len_ptr, 0xf000 | q - (desc_len_ptr + 2));
915  put16(&loop_len_ptr, 0xf000 | q - (loop_len_ptr + 2));
916 
918  data, q - data);
919 }
920 
921 /* This stores a string in buf with the correct encoding and also sets the
922  * first byte as the length. !str is accepted for an empty string.
923  * If the string is already encoded, invalid UTF-8 or has no multibyte sequence
924  * then we keep it as is, otherwise we signal UTF-8 encoding. */
925 static int encode_str8(uint8_t *buf, const char *str)
926 {
927  size_t str_len;
928  if (!str)
929  str = "";
930  str_len = strlen(str);
931  if (str[0] && (unsigned)str[0] >= 0x20) { /* Make sure the string is not already encoded. */
932  const uint8_t *q = str;
933  int has_multibyte = 0;
934  while (*q) {
935  uint32_t code;
936  GET_UTF8(code, *q++, goto invalid;) /* Is it valid UTF-8? */
937  has_multibyte |= (code > 127); /* Does it have multibyte UTF-8 chars in it? */
938  }
939  if (has_multibyte) { /* If we have multibyte chars and valid UTF-8, then encode as such! */
940  if (str_len > 254)
941  return AVERROR(EINVAL);
942  buf[0] = str_len + 1;
943  buf[1] = 0x15;
944  memcpy(&buf[2], str, str_len);
945  return 0;
946  }
947  }
948 invalid:
949  /* Otherwise let's just encode the string as is! */
950  if (str_len > 255)
951  return AVERROR(EINVAL);
952  buf[0] = str_len;
953  memcpy(&buf[1], str, str_len);
954  return 0;
955 }
956 
957 static int64_t get_pcr(const MpegTSWrite *ts)
958 {
959  return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
960  ts->first_pcr;
961 }
962 
963 static void write_packet(AVFormatContext *s, const uint8_t *packet)
964 {
965  MpegTSWrite *ts = s->priv_data;
966  if (ts->m2ts_mode) {
967  int64_t pcr = get_pcr(s->priv_data);
968  uint32_t tp_extra_header = pcr % 0x3fffffff;
969  tp_extra_header = AV_RB32(&tp_extra_header);
970  avio_write(s->pb, (unsigned char *) &tp_extra_header,
971  sizeof(tp_extra_header));
972  }
974  ts->total_size += TS_PACKET_SIZE;
975 }
976 
977 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
978 {
979  AVFormatContext *ctx = s->opaque;
981 }
982 
984  const AVDictionary *metadata,
986 {
987  MpegTSWrite *ts = s->priv_data;
988  MpegTSService *service;
989  AVDictionaryEntry *title, *provider;
990  char default_service_name[32];
991  const char *service_name;
992  const char *provider_name;
993 
994  title = av_dict_get(metadata, "service_name", NULL, 0);
995  if (!title)
996  title = av_dict_get(metadata, "title", NULL, 0);
997  snprintf(default_service_name, sizeof(default_service_name), "%s%02d", DEFAULT_SERVICE_NAME, ts->nb_services + 1);
998  service_name = title ? title->value : default_service_name;
999  provider = av_dict_get(metadata, "service_provider", NULL, 0);
1000  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
1001 
1002  service = av_mallocz(sizeof(MpegTSService));
1003  if (!service)
1004  return NULL;
1005  service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
1006  service->sid = sid;
1007  service->pcr_pid = 0x1fff;
1008  if (encode_str8(service->provider_name, provider_name) < 0 ||
1009  encode_str8(service->name, service_name) < 0) {
1010  av_log(s, AV_LOG_ERROR, "Too long service or provider name\n");
1011  goto fail;
1012  }
1013  if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
1014  goto fail;
1015 
1017  service->pmt.opaque = s;
1018  service->pmt.cc = 15;
1019  service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
1020  service->program = program;
1021 
1022  return service;
1023 fail:
1024  av_free(service);
1025  return NULL;
1026 }
1027 
1029 {
1030  MpegTSWrite *ts = s->priv_data;
1031  MpegTSWriteStream *ts_st = pcr_st->priv_data;
1032 
1033  if (ts->mux_rate > 1 || ts->pcr_period_ms >= 0) {
1034  int pcr_period_ms = ts->pcr_period_ms == -1 ? PCR_RETRANS_TIME : ts->pcr_period_ms;
1035  ts_st->pcr_period = av_rescale(pcr_period_ms, PCR_TIME_BASE, 1000);
1036  } else {
1037  /* By default, for VBR we select the highest multiple of frame duration which is less than 100 ms. */
1038  int64_t frame_period = 0;
1039  if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1041  if (!frame_size) {
1042  av_log(s, AV_LOG_WARNING, "frame size not set\n");
1043  frame_size = 512;
1044  }
1046  } else if (pcr_st->avg_frame_rate.num) {
1047  frame_period = av_rescale_rnd(pcr_st->avg_frame_rate.den, PCR_TIME_BASE, pcr_st->avg_frame_rate.num, AV_ROUND_UP);
1048  }
1049  if (frame_period > 0 && frame_period <= PCR_TIME_BASE / 10)
1050  ts_st->pcr_period = frame_period * (PCR_TIME_BASE / 10 / frame_period);
1051  else
1052  ts_st->pcr_period = 1;
1053  }
1054 
1055  // output a PCR as soon as possible
1056  ts_st->last_pcr = ts->first_pcr - ts_st->pcr_period;
1057 }
1058 
1060 {
1061  MpegTSWrite *ts = s->priv_data;
1062 
1063  for (int i = 0; i < ts->nb_services; i++) {
1064  MpegTSService *service = ts->services[i];
1065  AVStream *pcr_st = NULL;
1066  AVProgram *program = service->program;
1067  int nb_streams = program ? program->nb_stream_indexes : s->nb_streams;
1068 
1069  for (int j = 0; j < nb_streams; j++) {
1070  AVStream *st = s->streams[program ? program->stream_index[j] : j];
1071  if (!pcr_st ||
1073  {
1074  pcr_st = st;
1075  }
1076  }
1077 
1078  if (pcr_st) {
1079  MpegTSWriteStream *ts_st = pcr_st->priv_data;
1080  service->pcr_pid = ts_st->pid;
1082  av_log(s, AV_LOG_VERBOSE, "service %i using PCR in pid=%i, pcr_period=%"PRId64"ms\n",
1083  service->sid, service->pcr_pid, av_rescale(ts_st->pcr_period, 1000, PCR_TIME_BASE));
1084  }
1085  }
1086 }
1087 
1089 {
1090  MpegTSWrite *ts = s->priv_data;
1091  AVDictionaryEntry *provider;
1092  const char *provider_name;
1093  int i, j;
1094  int ret;
1095 
1096  if (ts->m2ts_mode == -1) {
1097  if (av_match_ext(s->url, "m2ts")) {
1098  ts->m2ts_mode = 1;
1099  } else {
1100  ts->m2ts_mode = 0;
1101  }
1102  }
1103 
1108 
1109  if (ts->m2ts_mode) {
1111  if (s->nb_programs > 1) {
1112  av_log(s, AV_LOG_ERROR, "Only one program is allowed in m2ts mode!\n");
1113  return AVERROR(EINVAL);
1114  }
1115  }
1116 
1117  if (s->max_delay < 0) /* Not set by the caller */
1118  s->max_delay = 0;
1119 
1120  // round up to a whole number of TS packets
1121  ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
1122 
1123  if (!s->nb_programs) {
1124  /* allocate a single DVB service */
1125  if (!mpegts_add_service(s, ts->service_id, s->metadata, NULL))
1126  return AVERROR(ENOMEM);
1127  } else {
1128  for (i = 0; i < s->nb_programs; i++) {
1129  AVProgram *program = s->programs[i];
1130  if (!mpegts_add_service(s, program->id, program->metadata, program))
1131  return AVERROR(ENOMEM);
1132  }
1133  }
1134 
1135  ts->pat.pid = PAT_PID;
1136  /* Initialize at 15 so that it wraps and is equal to 0 for the
1137  * first packet we write. */
1138  ts->pat.cc = 15;
1141  ts->pat.opaque = s;
1142 
1143  ts->sdt.pid = SDT_PID;
1144  ts->sdt.cc = 15;
1147  ts->sdt.opaque = s;
1148 
1149  ts->nit.pid = NIT_PID;
1150  ts->nit.cc = 15;
1153  ts->nit.opaque = s;
1154 
1155  ts->pkt = ffformatcontext(s)->pkt;
1156 
1157  /* assign pids to each stream */
1158  for (i = 0; i < s->nb_streams; i++) {
1159  AVStream *st = s->streams[i];
1160  MpegTSWriteStream *ts_st;
1161 
1162  ts_st = av_mallocz(sizeof(MpegTSWriteStream));
1163  if (!ts_st) {
1164  return AVERROR(ENOMEM);
1165  }
1166  st->priv_data = ts_st;
1167 
1168  avpriv_set_pts_info(st, 33, 1, 90000);
1169 
1170  ts_st->payload = av_mallocz(ts->pes_payload_size);
1171  if (!ts_st->payload) {
1172  return AVERROR(ENOMEM);
1173  }
1174 
1175  /* MPEG pid values < 16 are reserved. Applications which set st->id in
1176  * this range are assigned a calculated pid. */
1177  if (st->id < 16) {
1178  if (ts->m2ts_mode) {
1179  switch (st->codecpar->codec_type) {
1180  case AVMEDIA_TYPE_VIDEO:
1181  ts_st->pid = ts->m2ts_video_pid++;
1182  break;
1183  case AVMEDIA_TYPE_AUDIO:
1184  ts_st->pid = ts->m2ts_audio_pid++;
1185  break;
1186  case AVMEDIA_TYPE_SUBTITLE:
1187  switch (st->codecpar->codec_id) {
1189  ts_st->pid = ts->m2ts_pgssub_pid++;
1190  break;
1192  ts_st->pid = ts->m2ts_textsub_pid++;
1193  break;
1194  }
1195  break;
1196  }
1197  if (ts->m2ts_video_pid > M2TS_VIDEO_PID + 1 ||
1198  ts->m2ts_audio_pid > M2TS_AUDIO_START_PID + 32 ||
1200  ts->m2ts_textsub_pid > M2TS_TEXTSUB_PID + 1 ||
1201  ts_st->pid < 16) {
1202  av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index);
1203  return AVERROR(EINVAL);
1204  }
1205  } else {
1206  ts_st->pid = ts->start_pid + i;
1207  }
1208  } else {
1209  ts_st->pid = st->id;
1210  }
1211  if (ts_st->pid >= 0x1FFF) {
1213  "Invalid stream id %d, must be less than 8191\n", st->id);
1214  return AVERROR(EINVAL);
1215  }
1216  for (j = 0; j < ts->nb_services; j++) {
1217  if (ts->services[j]->pmt.pid > LAST_OTHER_PID) {
1219  "Invalid PMT PID %d, must be less than %d\n", ts->services[j]->pmt.pid, LAST_OTHER_PID + 1);
1220  return AVERROR(EINVAL);
1221  }
1222  if (ts_st->pid == ts->services[j]->pmt.pid) {
1223  av_log(s, AV_LOG_ERROR, "PID %d cannot be both elementary and PMT PID\n", ts_st->pid);
1224  return AVERROR(EINVAL);
1225  }
1226  }
1227  for (j = 0; j < i; j++) {
1228  MpegTSWriteStream *ts_st_prev = s->streams[j]->priv_data;
1229  if (ts_st_prev->pid == ts_st->pid) {
1230  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
1231  return AVERROR(EINVAL);
1232  }
1233  }
1234  ts_st->payload_pts = AV_NOPTS_VALUE;
1235  ts_st->payload_dts = AV_NOPTS_VALUE;
1236  ts_st->cc = 15;
1237  ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
1238  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1239  st->codecpar->extradata_size > 0) {
1240  AVStream *ast;
1241  ts_st->amux = avformat_alloc_context();
1242  if (!ts_st->amux) {
1243  return AVERROR(ENOMEM);
1244  }
1245  ts_st->amux->oformat =
1246  av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
1247  NULL, NULL);
1248  if (!ts_st->amux->oformat) {
1249  return AVERROR(EINVAL);
1250  }
1251  if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
1252  return AVERROR(ENOMEM);
1253  }
1255  if (ret != 0)
1256  return ret;
1257  ast->time_base = st->time_base;
1258  ret = avformat_write_header(ts_st->amux, NULL);
1259  if (ret < 0)
1260  return ret;
1261  }
1262  if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
1264  }
1265  }
1266 
1267  if (ts->copyts < 1)
1268  ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
1269 
1271 
1278 
1279  /* assign provider name */
1280  provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
1281  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
1282  if (encode_str8(ts->provider_name, provider_name) < 0) {
1283  av_log(s, AV_LOG_ERROR, "Too long provider name\n");
1284  return AVERROR(EINVAL);
1285  }
1286 
1287  if (ts->mux_rate == 1)
1288  av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
1289  else
1290  av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
1292  "sdt every %"PRId64" ms, pat/pmt every %"PRId64" ms",
1293  av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE),
1294  av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
1295  if (ts->flags & MPEGTS_FLAG_NIT)
1296  av_log(s, AV_LOG_VERBOSE, ", nit every %"PRId64" ms", av_rescale(ts->nit_period, 1000, PCR_TIME_BASE));
1297  av_log(s, AV_LOG_VERBOSE, "\n");
1298 
1299  return 0;
1300 }
1301 
1302 /* send SDT, NIT, PAT and PMT tables regularly */
1303 static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int force_nit, int64_t pcr)
1304 {
1305  MpegTSWrite *ts = s->priv_data;
1306  int i;
1307 
1308  if ((pcr != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
1309  (pcr != AV_NOPTS_VALUE && pcr - ts->last_sdt_ts >= ts->sdt_period) ||
1310  force_sdt
1311  ) {
1312  if (pcr != AV_NOPTS_VALUE)
1313  ts->last_sdt_ts = FFMAX(pcr, ts->last_sdt_ts);
1315  }
1316  if ((pcr != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
1317  (pcr != AV_NOPTS_VALUE && pcr - ts->last_pat_ts >= ts->pat_period) ||
1318  force_pat) {
1319  if (pcr != AV_NOPTS_VALUE)
1320  ts->last_pat_ts = FFMAX(pcr, ts->last_pat_ts);
1322  for (i = 0; i < ts->nb_services; i++)
1323  mpegts_write_pmt(s, ts->services[i]);
1324  }
1325  if ((pcr != AV_NOPTS_VALUE && ts->last_nit_ts == AV_NOPTS_VALUE) ||
1326  (pcr != AV_NOPTS_VALUE && pcr - ts->last_nit_ts >= ts->nit_period) ||
1327  force_nit
1328  ) {
1329  if (pcr != AV_NOPTS_VALUE)
1330  ts->last_nit_ts = FFMAX(pcr, ts->last_nit_ts);
1331  if (ts->flags & MPEGTS_FLAG_NIT)
1333  }
1334 }
1335 
1336 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
1337 {
1338  int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
1339 
1340  *buf++ = pcr_high >> 25;
1341  *buf++ = pcr_high >> 17;
1342  *buf++ = pcr_high >> 9;
1343  *buf++ = pcr_high >> 1;
1344  *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
1345  *buf++ = pcr_low;
1346 
1347  return 6;
1348 }
1349 
1350 /* Write a single null transport stream packet */
1352 {
1353  uint8_t *q;
1354  uint8_t buf[TS_PACKET_SIZE];
1355 
1356  q = buf;
1357  *q++ = 0x47;
1358  *q++ = 0x00 | 0x1f;
1359  *q++ = 0xff;
1360  *q++ = 0x10;
1361  memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
1362  write_packet(s, buf);
1363 }
1364 
1365 /* Write a single transport stream packet with a PCR and no payload */
1367 {
1368  MpegTSWrite *ts = s->priv_data;
1369  MpegTSWriteStream *ts_st = st->priv_data;
1370  uint8_t *q;
1371  uint8_t buf[TS_PACKET_SIZE];
1372 
1373  q = buf;
1374  *q++ = 0x47;
1375  *q++ = ts_st->pid >> 8;
1376  *q++ = ts_st->pid;
1377  *q++ = 0x20 | ts_st->cc; /* Adaptation only */
1378  /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
1379  *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
1380  *q++ = 0x10; /* Adaptation flags: PCR present */
1381  if (ts_st->discontinuity) {
1382  q[-1] |= 0x80;
1383  ts_st->discontinuity = 0;
1384  }
1385 
1386  /* PCR coded into 6 bytes */
1387  q += write_pcr_bits(q, get_pcr(ts));
1388 
1389  /* stuffing bytes */
1390  memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
1391  write_packet(s, buf);
1392 }
1393 
1394 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
1395 {
1396  int val;
1397 
1398  val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
1399  *q++ = val;
1400  val = (((pts >> 15) & 0x7fff) << 1) | 1;
1401  *q++ = val >> 8;
1402  *q++ = val;
1403  val = (((pts) & 0x7fff) << 1) | 1;
1404  *q++ = val >> 8;
1405  *q++ = val;
1406 }
1407 
1408 /* Set an adaptation field flag in an MPEG-TS packet*/
1409 static void set_af_flag(uint8_t *pkt, int flag)
1410 {
1411  // expect at least one flag to set
1412  av_assert0(flag);
1413 
1414  if ((pkt[3] & 0x20) == 0) {
1415  // no AF yet, set adaptation field flag
1416  pkt[3] |= 0x20;
1417  // 1 byte length, no flags
1418  pkt[4] = 1;
1419  pkt[5] = 0;
1420  }
1421  pkt[5] |= flag;
1422 }
1423 
1424 /* Extend the adaptation field by size bytes */
1425 static void extend_af(uint8_t *pkt, int size)
1426 {
1427  // expect already existing adaptation field
1428  av_assert0(pkt[3] & 0x20);
1429  pkt[4] += size;
1430 }
1431 
1432 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
1433 static uint8_t *get_ts_payload_start(uint8_t *pkt)
1434 {
1435  if (pkt[3] & 0x20)
1436  return pkt + 5 + pkt[4];
1437  else
1438  return pkt + 4;
1439 }
1440 
1441 static int get_pes_stream_id(AVFormatContext *s, AVStream *st, int stream_id, int *async)
1442 {
1443  MpegTSWrite *ts = s->priv_data;
1444  *async = 0;
1445  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1446  if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)
1448  else
1449  return STREAM_ID_VIDEO_STREAM_0;
1450  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1451  (st->codecpar->codec_id == AV_CODEC_ID_MP2 ||
1452  st->codecpar->codec_id == AV_CODEC_ID_MP3 ||
1453  st->codecpar->codec_id == AV_CODEC_ID_AAC)) {
1454  return STREAM_ID_AUDIO_STREAM_0;
1455  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1456  st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
1457  ts->m2ts_mode) {
1459  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1462  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
1463  if (st->codecpar->codec_id == AV_CODEC_ID_SMPTE_KLV &&
1464  stream_id == STREAM_ID_PRIVATE_STREAM_1) /* asynchronous KLV */
1465  *async = 1;
1466  return stream_id != -1 ? stream_id : STREAM_ID_METADATA_STREAM;
1467  } else {
1469  }
1470 }
1471 
1472 /* Add a PES header to the front of the payload, and segment into an integer
1473  * number of TS packets. The final TS packet is padded using an oversized
1474  * adaptation header to exactly fill the last TS packet.
1475  * NOTE: 'payload' contains a complete PES payload. */
1477  const uint8_t *payload, int payload_size,
1478  int64_t pts, int64_t dts, int key, int stream_id)
1479 {
1480  MpegTSWriteStream *ts_st = st->priv_data;
1481  MpegTSWrite *ts = s->priv_data;
1482  uint8_t buf[TS_PACKET_SIZE];
1483  uint8_t *q;
1484  int val, is_start, len, header_len, write_pcr, flags;
1485  int afc_len, stuffing_len;
1486  int is_dvb_subtitle = (st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE);
1487  int is_dvb_teletext = (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT);
1488  int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
1489  int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
1490  int force_sdt = 0;
1491  int force_nit = 0;
1492 
1493  av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
1495  force_pat = 1;
1496  }
1497 
1498  if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1499  force_pat = 1;
1500  force_sdt = 1;
1501  force_nit = 1;
1503  }
1504 
1505  is_start = 1;
1506  while (payload_size > 0) {
1507  int64_t pcr = AV_NOPTS_VALUE;
1508  if (ts->mux_rate > 1)
1509  pcr = get_pcr(ts);
1510  else if (dts != AV_NOPTS_VALUE)
1511  pcr = (dts - delay) * 300;
1512 
1513  retransmit_si_info(s, force_pat, force_sdt, force_nit, pcr);
1514  force_pat = 0;
1515  force_sdt = 0;
1516  force_nit = 0;
1517 
1518  write_pcr = 0;
1519  if (ts->mux_rate > 1) {
1520  /* Send PCR packets for all PCR streams if needed */
1521  pcr = get_pcr(ts);
1522  if (pcr >= ts->next_pcr) {
1523  int64_t next_pcr = INT64_MAX;
1524  for (int i = 0; i < s->nb_streams; i++) {
1525  /* Make the current stream the last, because for that we
1526  * can insert the pcr into the payload later */
1527  int st2_index = i < st->index ? i : (i + 1 == s->nb_streams ? st->index : i + 1);
1528  AVStream *st2 = s->streams[st2_index];
1529  MpegTSWriteStream *ts_st2 = st2->priv_data;
1530  if (ts_st2->pcr_period) {
1531  if (pcr - ts_st2->last_pcr >= ts_st2->pcr_period) {
1532  ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period);
1533  if (st2 != st) {
1534  mpegts_insert_pcr_only(s, st2);
1535  pcr = get_pcr(ts);
1536  } else {
1537  write_pcr = 1;
1538  }
1539  }
1540  next_pcr = FFMIN(next_pcr, ts_st2->last_pcr + ts_st2->pcr_period);
1541  }
1542  }
1543  ts->next_pcr = next_pcr;
1544  }
1545  if (dts != AV_NOPTS_VALUE && (dts - pcr / 300) > delay) {
1546  /* pcr insert gets priority over null packet insert */
1547  if (write_pcr)
1549  else
1551  /* recalculate write_pcr and possibly retransmit si_info */
1552  continue;
1553  }
1554  } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) {
1555  if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) {
1556  ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period);
1557  write_pcr = 1;
1558  }
1559  }
1560 
1561  /* prepare packet header */
1562  q = buf;
1563  *q++ = 0x47;
1564  val = ts_st->pid >> 8;
1565  if (ts->m2ts_mode && st->codecpar->codec_id == AV_CODEC_ID_AC3)
1566  val |= 0x20;
1567  if (is_start)
1568  val |= 0x40;
1569  *q++ = val;
1570  *q++ = ts_st->pid;
1571  ts_st->cc = ts_st->cc + 1 & 0xf;
1572  *q++ = 0x10 | ts_st->cc; // payload indicator + CC
1573  if (ts_st->discontinuity) {
1574  set_af_flag(buf, 0x80);
1575  q = get_ts_payload_start(buf);
1576  ts_st->discontinuity = 0;
1577  }
1578  if (!(ts->flags & MPEGTS_FLAG_OMIT_RAI) &&
1579  key && is_start && pts != AV_NOPTS_VALUE &&
1580  !is_dvb_teletext /* adaptation+payload forbidden for teletext (ETSI EN 300 472 V1.3.1 4.1) */) {
1581  // set Random Access for key frames
1582  if (ts_st->pcr_period)
1583  write_pcr = 1;
1584  set_af_flag(buf, 0x40);
1585  q = get_ts_payload_start(buf);
1586  }
1587  if (write_pcr) {
1588  set_af_flag(buf, 0x10);
1589  q = get_ts_payload_start(buf);
1590  // add 11, pcr references the last byte of program clock reference base
1591  if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
1592  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
1593  extend_af(buf, write_pcr_bits(q, pcr));
1594  q = get_ts_payload_start(buf);
1595  }
1596  if (is_start) {
1597  int pes_extension = 0;
1598  int pes_header_stuffing_bytes = 0;
1599  int async;
1600  /* write PES header */
1601  *q++ = 0x00;
1602  *q++ = 0x00;
1603  *q++ = 0x01;
1604  *q++ = stream_id = get_pes_stream_id(s, st, stream_id, &async);
1605  if (async)
1606  pts = dts = AV_NOPTS_VALUE;
1607 
1608  header_len = 0;
1609 
1610  if (stream_id != STREAM_ID_PROGRAM_STREAM_MAP &&
1611  stream_id != STREAM_ID_PADDING_STREAM &&
1612  stream_id != STREAM_ID_PRIVATE_STREAM_2 &&
1613  stream_id != STREAM_ID_ECM_STREAM &&
1614  stream_id != STREAM_ID_EMM_STREAM &&
1615  stream_id != STREAM_ID_PROGRAM_STREAM_DIRECTORY &&
1616  stream_id != STREAM_ID_DSMCC_STREAM &&
1617  stream_id != STREAM_ID_TYPE_E_STREAM) {
1618 
1619  flags = 0;
1620  if (pts != AV_NOPTS_VALUE) {
1621  header_len += 5;
1622  flags |= 0x80;
1623  }
1624  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1625  header_len += 5;
1626  flags |= 0x40;
1627  }
1628  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1630  /* set PES_extension_flag */
1631  pes_extension = 1;
1632  flags |= 0x01;
1633 
1634  /* One byte for PES2 extension flag +
1635  * one byte for extension length +
1636  * one byte for extension id */
1637  header_len += 3;
1638  }
1639  /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
1640  * otherwise it will not play sound on blu-ray
1641  */
1642  if (ts->m2ts_mode &&
1644  st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1645  /* set PES_extension_flag */
1646  pes_extension = 1;
1647  flags |= 0x01;
1648  header_len += 3;
1649  }
1650  if (is_dvb_teletext) {
1651  pes_header_stuffing_bytes = 0x24 - header_len;
1652  header_len = 0x24;
1653  }
1654  len = payload_size + header_len + 3;
1655  /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
1656  if (is_dvb_subtitle) {
1657  len += 3;
1658  payload_size++;
1659  }
1660  if (len > 0xffff)
1661  len = 0;
1663  len = 0;
1664  }
1665  *q++ = len >> 8;
1666  *q++ = len;
1667  val = 0x80;
1668  /* data alignment indicator is required for subtitle and data streams */
1670  val |= 0x04;
1671  *q++ = val;
1672  *q++ = flags;
1673  *q++ = header_len;
1674  if (pts != AV_NOPTS_VALUE) {
1675  write_pts(q, flags >> 6, pts);
1676  q += 5;
1677  }
1678  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1679  write_pts(q, 1, dts);
1680  q += 5;
1681  }
1682  if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
1683  flags = 0x01; /* set PES_extension_flag_2 */
1684  *q++ = flags;
1685  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1686  /* Set the stream ID extension flag bit to 0 and
1687  * write the extended stream ID. */
1688  *q++ = 0x00 | 0x60;
1689  }
1690  /* For Blu-ray AC3 Audio Setting extended flags */
1691  if (ts->m2ts_mode &&
1692  pes_extension &&
1693  st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1694  flags = 0x01; /* set PES_extension_flag_2 */
1695  *q++ = flags;
1696  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1697  *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1698  }
1699 
1700 
1701  if (is_dvb_subtitle) {
1702  /* First two fields of DVB subtitles PES data:
1703  * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
1704  * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
1705  *q++ = 0x20;
1706  *q++ = 0x00;
1707  }
1708  if (is_dvb_teletext) {
1709  memset(q, 0xff, pes_header_stuffing_bytes);
1710  q += pes_header_stuffing_bytes;
1711  }
1712  } else {
1713  len = payload_size;
1714  *q++ = len >> 8;
1715  *q++ = len;
1716  }
1717  is_start = 0;
1718  }
1719  /* header size */
1720  header_len = q - buf;
1721  /* data len */
1722  len = TS_PACKET_SIZE - header_len;
1723  if (len > payload_size)
1724  len = payload_size;
1725  stuffing_len = TS_PACKET_SIZE - header_len - len;
1726  if (stuffing_len > 0) {
1727  /* add stuffing with AFC */
1728  if (buf[3] & 0x20) {
1729  /* stuffing already present: increase its size */
1730  afc_len = buf[4] + 1;
1731  memmove(buf + 4 + afc_len + stuffing_len,
1732  buf + 4 + afc_len,
1733  header_len - (4 + afc_len));
1734  buf[4] += stuffing_len;
1735  memset(buf + 4 + afc_len, 0xff, stuffing_len);
1736  } else {
1737  /* add stuffing */
1738  memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1739  buf[3] |= 0x20;
1740  buf[4] = stuffing_len - 1;
1741  if (stuffing_len >= 2) {
1742  buf[5] = 0x00;
1743  memset(buf + 6, 0xff, stuffing_len - 2);
1744  }
1745  }
1746  }
1747 
1748  if (is_dvb_subtitle && payload_size == len) {
1749  memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
1750  buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
1751  } else {
1752  memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1753  }
1754 
1755  payload += len;
1756  payload_size -= len;
1757  write_packet(s, buf);
1758  }
1759  ts_st->prev_payload_key = key;
1760 }
1761 
1763 {
1764  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1765  if (!st->nb_frames) {
1766  av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1767  "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1768  "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1769  return AVERROR_INVALIDDATA;
1770  }
1771  av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing, size %d", pkt->size);
1772  if (pkt->size)
1773  av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
1774  av_log(s, AV_LOG_WARNING, "\n");
1775  }
1776  return 0;
1777 }
1778 
1780 {
1781  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1782  if (!st->nb_frames) {
1783  av_log(s, AV_LOG_ERROR, "HEVC bitstream malformed, no startcode found\n");
1784  return AVERROR_PATCHWELCOME;
1785  }
1786  av_log(s, AV_LOG_WARNING, "HEVC bitstream error, startcode missing, size %d", pkt->size);
1787  if (pkt->size)
1788  av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
1789  av_log(s, AV_LOG_WARNING, "\n");
1790  }
1791  return 0;
1792 }
1793 
1794 /* Based on GStreamer's gst-plugins-base/ext/ogg/gstoggstream.c
1795  * Released under the LGPL v2.1+, written by
1796  * Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
1797  */
1799 {
1800  static const int durations[32] = {
1801  480, 960, 1920, 2880, /* Silk NB */
1802  480, 960, 1920, 2880, /* Silk MB */
1803  480, 960, 1920, 2880, /* Silk WB */
1804  480, 960, /* Hybrid SWB */
1805  480, 960, /* Hybrid FB */
1806  120, 240, 480, 960, /* CELT NB */
1807  120, 240, 480, 960, /* CELT NB */
1808  120, 240, 480, 960, /* CELT NB */
1809  120, 240, 480, 960, /* CELT NB */
1810  };
1811  int toc, frame_duration, nframes, duration;
1812 
1813  if (pkt->size < 1)
1814  return 0;
1815 
1816  toc = pkt->data[0];
1817 
1818  frame_duration = durations[toc >> 3];
1819  switch (toc & 3) {
1820  case 0:
1821  nframes = 1;
1822  break;
1823  case 1:
1824  nframes = 2;
1825  break;
1826  case 2:
1827  nframes = 2;
1828  break;
1829  case 3:
1830  if (pkt->size < 2)
1831  return 0;
1832  nframes = pkt->data[1] & 63;
1833  break;
1834  }
1835 
1836  duration = nframes * frame_duration;
1837  if (duration > 5760) {
1839  "Opus packet duration > 120 ms, invalid");
1840  return 0;
1841  }
1842 
1843  return duration;
1844 }
1845 
1847 {
1848  AVStream *st = s->streams[pkt->stream_index];
1849  int size = pkt->size;
1850  const uint8_t *buf = pkt->data;
1851  uint8_t *data = NULL;
1852  MpegTSWrite *ts = s->priv_data;
1853  MpegTSWriteStream *ts_st = st->priv_data;
1854  const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1855  const int64_t max_audio_delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) / 2;
1856  int64_t dts = pkt->dts, pts = pkt->pts;
1857  int opus_samples = 0;
1858  size_t side_data_size;
1859  uint8_t *side_data = NULL;
1860  int stream_id = -1;
1861 
1862  side_data = av_packet_get_side_data(pkt,
1864  &side_data_size);
1865  if (side_data)
1866  stream_id = side_data[0];
1867 
1868  if (!ts->first_dts_checked && dts != AV_NOPTS_VALUE) {
1869  ts->first_pcr += dts * 300;
1870  ts->first_dts_checked = 1;
1871  }
1872 
1873  if (ts->copyts < 1) {
1874  if (pts != AV_NOPTS_VALUE)
1875  pts += delay;
1876  if (dts != AV_NOPTS_VALUE)
1877  dts += delay;
1878  }
1879 
1880  if (!ts_st->first_timestamp_checked && (pts == AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)) {
1881  av_log(s, AV_LOG_ERROR, "first pts and dts value must be set\n");
1882  return AVERROR_INVALIDDATA;
1883  }
1884  ts_st->first_timestamp_checked = 1;
1885 
1886  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1887  const uint8_t *p = buf, *buf_end = p + size;
1888  const uint8_t *found_aud = NULL, *found_aud_end = NULL;
1889  uint32_t state = -1;
1890  int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
1891  int ret = ff_check_h264_startcode(s, st, pkt);
1892  if (ret < 0)
1893  return ret;
1894 
1895  if (extradd && AV_RB24(st->codecpar->extradata) > 1)
1896  extradd = 0;
1897 
1898  /* Ensure that all pictures are prefixed with an AUD, and that
1899  * IDR pictures are also prefixed with SPS and PPS. SPS and PPS
1900  * are assumed to be available in 'extradata' if not found in-band. */
1901  do {
1902  p = avpriv_find_start_code(p, buf_end, &state);
1903  av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", state & 0x1f);
1904  if ((state & 0x1f) == H264_NAL_SPS)
1905  extradd = 0;
1906  if ((state & 0x1f) == H264_NAL_AUD) {
1907  found_aud = p - 4; // start of the 0x000001 start code.
1908  found_aud_end = p + 1; // first byte past the AUD.
1909  if (found_aud < buf)
1910  found_aud = buf;
1911  if (buf_end < found_aud_end)
1912  found_aud_end = buf_end;
1913  }
1914  } while (p < buf_end
1915  && (state & 0x1f) != H264_NAL_IDR_SLICE
1916  && (state & 0x1f) != H264_NAL_SLICE
1917  && (extradd > 0 || !found_aud));
1918  if ((state & 0x1f) != H264_NAL_IDR_SLICE)
1919  extradd = 0;
1920 
1921  if (!found_aud) {
1922  /* Prefix 'buf' with the missing AUD, and extradata if needed. */
1923  data = av_malloc(pkt->size + 6 + extradd);
1924  if (!data)
1925  return AVERROR(ENOMEM);
1926  memcpy(data + 6, st->codecpar->extradata, extradd);
1927  memcpy(data + 6 + extradd, pkt->data, pkt->size);
1928  AV_WB32(data, 0x00000001);
1929  data[4] = H264_NAL_AUD;
1930  data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1931  buf = data;
1932  size = pkt->size + 6 + extradd;
1933  } else if (extradd != 0) {
1934  /* Move the AUD up to the beginning of the frame, where the H.264
1935  * spec requires it to appear. Emit the extradata after it. */
1936  PutByteContext pb;
1937  const int new_pkt_size = pkt->size + 1 + extradd;
1938  data = av_malloc(new_pkt_size);
1939  if (!data)
1940  return AVERROR(ENOMEM);
1941  bytestream2_init_writer(&pb, data, new_pkt_size);
1942  bytestream2_put_byte(&pb, 0x00);
1943  bytestream2_put_buffer(&pb, found_aud, found_aud_end - found_aud);
1944  bytestream2_put_buffer(&pb, st->codecpar->extradata, extradd);
1945  bytestream2_put_buffer(&pb, pkt->data, found_aud - pkt->data);
1946  bytestream2_put_buffer(&pb, found_aud_end, buf_end - found_aud_end);
1947  av_assert0(new_pkt_size == bytestream2_tell_p(&pb));
1948  buf = data;
1949  size = new_pkt_size;
1950  }
1951  } else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
1952  if (pkt->size < 2) {
1953  av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1954  return AVERROR_INVALIDDATA;
1955  }
1956  if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1957  int ret;
1958  AVPacket *pkt2 = ts->pkt;
1959 
1960  if (!ts_st->amux) {
1961  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1962  "and extradata missing\n");
1963  } else {
1964  av_packet_unref(pkt2);
1965  pkt2->data = pkt->data;
1966  pkt2->size = pkt->size;
1968  pkt2->dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
1969 
1970  ret = avio_open_dyn_buf(&ts_st->amux->pb);
1971  if (ret < 0)
1972  return ret;
1973 
1974  ret = av_write_frame(ts_st->amux, pkt2);
1975  if (ret < 0) {
1976  ffio_free_dyn_buf(&ts_st->amux->pb);
1977  return ret;
1978  }
1979  size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1980  ts_st->amux->pb = NULL;
1981  buf = data;
1982  }
1983  }
1984  } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
1985  const uint8_t *p = buf, *buf_end = p + size;
1986  uint32_t state = -1;
1987  int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
1988  int ret = check_hevc_startcode(s, st, pkt);
1989  if (ret < 0)
1990  return ret;
1991 
1992  if (extradd && AV_RB24(st->codecpar->extradata) > 1)
1993  extradd = 0;
1994 
1995  do {
1996  p = avpriv_find_start_code(p, buf_end, &state);
1997  av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", (state & 0x7e)>>1);
1998  if ((state & 0x7e) == 2*32)
1999  extradd = 0;
2000  } while (p < buf_end && (state & 0x7e) != 2*35 &&
2001  (state & 0x7e) >= 2*32);
2002 
2003  if ((state & 0x7e) < 2*16 || (state & 0x7e) >= 2*24)
2004  extradd = 0;
2005  if ((state & 0x7e) != 2*35) { // AUD NAL
2006  data = av_malloc(pkt->size + 7 + extradd);
2007  if (!data)
2008  return AVERROR(ENOMEM);
2009  memcpy(data + 7, st->codecpar->extradata, extradd);
2010  memcpy(data + 7 + extradd, pkt->data, pkt->size);
2011  AV_WB32(data, 0x00000001);
2012  data[4] = 2*35;
2013  data[5] = 1;
2014  data[6] = 0x50; // any slice type (0x4) + rbsp stop one bit
2015  buf = data;
2016  size = pkt->size + 7 + extradd;
2017  }
2018  } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
2019  if (pkt->size < 2) {
2020  av_log(s, AV_LOG_ERROR, "Opus packet too short\n");
2021  return AVERROR_INVALIDDATA;
2022  }
2023 
2024  /* Add Opus control header */
2025  if ((AV_RB16(pkt->data) >> 5) != 0x3ff) {
2026  uint8_t *side_data;
2027  size_t side_data_size;
2028  int i, n;
2029  int ctrl_header_size;
2030  int trim_start = 0, trim_end = 0;
2031 
2032  opus_samples = opus_get_packet_samples(s, pkt);
2033 
2034  side_data = av_packet_get_side_data(pkt,
2036  &side_data_size);
2037 
2038  if (side_data && side_data_size >= 10) {
2039  trim_end = AV_RL32(side_data + 4) * 48000 / st->codecpar->sample_rate;
2040  }
2041 
2042  ctrl_header_size = pkt->size + 2 + pkt->size / 255 + 1;
2043  if (ts_st->opus_pending_trim_start)
2044  ctrl_header_size += 2;
2045  if (trim_end)
2046  ctrl_header_size += 2;
2047 
2048  data = av_malloc(ctrl_header_size);
2049  if (!data)
2050  return AVERROR(ENOMEM);
2051 
2052  data[0] = 0x7f;
2053  data[1] = 0xe0;
2054  if (ts_st->opus_pending_trim_start)
2055  data[1] |= 0x10;
2056  if (trim_end)
2057  data[1] |= 0x08;
2058 
2059  n = pkt->size;
2060  i = 2;
2061  do {
2062  data[i] = FFMIN(n, 255);
2063  n -= 255;
2064  i++;
2065  } while (n >= 0);
2066 
2067  av_assert0(2 + pkt->size / 255 + 1 == i);
2068 
2069  if (ts_st->opus_pending_trim_start) {
2070  trim_start = FFMIN(ts_st->opus_pending_trim_start, opus_samples);
2071  AV_WB16(data + i, trim_start);
2072  i += 2;
2073  ts_st->opus_pending_trim_start -= trim_start;
2074  }
2075  if (trim_end) {
2076  trim_end = FFMIN(trim_end, opus_samples - trim_start);
2077  AV_WB16(data + i, trim_end);
2078  i += 2;
2079  }
2080 
2081  memcpy(data + i, pkt->data, pkt->size);
2082  buf = data;
2083  size = ctrl_header_size;
2084  } else {
2085  /* TODO: Can we get TS formatted data here? If so we will
2086  * need to count the samples of that too! */
2087  av_log(s, AV_LOG_WARNING, "Got MPEG-TS formatted Opus data, unhandled");
2088  }
2089  } else if (st->codecpar->codec_id == AV_CODEC_ID_AC3 && !ts_st->dvb_ac3_desc) {
2090  AC3HeaderInfo *hdr = NULL;
2091 
2092  if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) >= 0) {
2093  uint8_t number_of_channels_flag;
2094  uint8_t service_type_flag;
2095  uint8_t full_service_flag = 1;
2096  DVBAC3Descriptor *dvb_ac3_desc;
2097 
2098  dvb_ac3_desc = av_mallocz(sizeof(*dvb_ac3_desc));
2099  if (!dvb_ac3_desc) {
2100  av_free(hdr);
2101  return AVERROR(ENOMEM);
2102  }
2103 
2104  service_type_flag = hdr->bitstream_mode;
2105  switch (hdr->channel_mode) {
2106  case AC3_CHMODE_DUALMONO:
2107  number_of_channels_flag = 1;
2108  break;
2109  case AC3_CHMODE_MONO:
2110  number_of_channels_flag = 0;
2111  break;
2112  case AC3_CHMODE_STEREO:
2113  if (hdr->dolby_surround_mode == AC3_DSURMOD_ON)
2114  number_of_channels_flag = 3;
2115  else
2116  number_of_channels_flag = 2;
2117  break;
2118  case AC3_CHMODE_3F:
2119  case AC3_CHMODE_2F1R:
2120  case AC3_CHMODE_3F1R:
2121  case AC3_CHMODE_2F2R:
2122  case AC3_CHMODE_3F2R:
2123  number_of_channels_flag = 4;
2124  break;
2125  default: /* reserved */
2126  number_of_channels_flag = 7;
2127  break;
2128  }
2129 
2130  if (service_type_flag == 1 || service_type_flag == 4 ||
2131  (service_type_flag == 7 && !number_of_channels_flag))
2132  full_service_flag = 0;
2133 
2134  dvb_ac3_desc->component_type_flag = 1;
2135  dvb_ac3_desc->component_type = (full_service_flag << 6) |
2136  ((service_type_flag & 0x7) << 3) |
2137  (number_of_channels_flag & 0x7);
2138  dvb_ac3_desc->bsid_flag = 1;
2139  dvb_ac3_desc->bsid = hdr->bitstream_id;
2140  dvb_ac3_desc->mainid_flag = 0;
2141  dvb_ac3_desc->asvc_flag = 0;
2142 
2143  ts_st->dvb_ac3_desc = dvb_ac3_desc;
2144  }
2145  av_free(hdr);
2146  } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_BLURAY && ts->m2ts_mode) {
2147  mpegts_write_pes(s, st, buf, size, pts, dts,
2148  pkt->flags & AV_PKT_FLAG_KEY, stream_id);
2149  return 0;
2150  }
2151 
2152  if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
2153  (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
2154  dts - ts_st->payload_dts >= max_audio_delay) ||
2155  ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) {
2156  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
2157  ts_st->payload_pts, ts_st->payload_dts,
2158  ts_st->payload_flags & AV_PKT_FLAG_KEY, stream_id);
2159  ts_st->payload_size = 0;
2160  ts_st->opus_queued_samples = 0;
2161  }
2162 
2164  av_assert0(!ts_st->payload_size);
2165  // for video and subtitle, write a single pes packet
2166  mpegts_write_pes(s, st, buf, size, pts, dts,
2167  pkt->flags & AV_PKT_FLAG_KEY, stream_id);
2168  ts_st->opus_queued_samples = 0;
2169  av_free(data);
2170  return 0;
2171  }
2172 
2173  if (!ts_st->payload_size) {
2174  ts_st->payload_pts = pts;
2175  ts_st->payload_dts = dts;
2176  ts_st->payload_flags = pkt->flags;
2177  }
2178 
2179  memcpy(ts_st->payload + ts_st->payload_size, buf, size);
2180  ts_st->payload_size += size;
2181  ts_st->opus_queued_samples += opus_samples;
2182 
2183  av_free(data);
2184 
2185  return 0;
2186 }
2187 
2189 {
2190  MpegTSWrite *ts = s->priv_data;
2191  int i;
2192 
2193  /* flush current packets */
2194  for (i = 0; i < s->nb_streams; i++) {
2195  AVStream *st = s->streams[i];
2196  MpegTSWriteStream *ts_st = st->priv_data;
2197  if (ts_st->payload_size > 0) {
2198  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
2199  ts_st->payload_pts, ts_st->payload_dts,
2200  ts_st->payload_flags & AV_PKT_FLAG_KEY, -1);
2201  ts_st->payload_size = 0;
2202  ts_st->opus_queued_samples = 0;
2203  }
2204  }
2205 
2206  if (ts->m2ts_mode) {
2207  int packets = (avio_tell(s->pb) / (TS_PACKET_SIZE + 4)) % 32;
2208  while (packets++ < 32)
2210  }
2211 }
2212 
2214 {
2215  if (!pkt) {
2217  return 1;
2218  } else {
2220  }
2221 }
2222 
2224 {
2225  if (s->pb)
2227 
2228  return 0;
2229 }
2230 
2232 {
2233  MpegTSWrite *ts = s->priv_data;
2234  MpegTSService *service;
2235  int i;
2236 
2237  for (i = 0; i < s->nb_streams; i++) {
2238  AVStream *st = s->streams[i];
2239  MpegTSWriteStream *ts_st = st->priv_data;
2240  if (ts_st) {
2241  av_freep(&ts_st->dvb_ac3_desc);
2242  av_freep(&ts_st->payload);
2243  if (ts_st->amux) {
2244  avformat_free_context(ts_st->amux);
2245  ts_st->amux = NULL;
2246  }
2247  }
2248  }
2249 
2250  for (i = 0; i < ts->nb_services; i++) {
2251  service = ts->services[i];
2252  av_freep(&service);
2253  }
2254  av_freep(&ts->services);
2255 }
2256 
2258  const AVPacket *pkt)
2259 {
2260  int ret = 1;
2261 
2262  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
2263  if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
2264  (AV_RB24(pkt->data) != 0x000001 ||
2265  (st->codecpar->extradata_size > 0 &&
2266  st->codecpar->extradata[0] == 1)))
2267  ret = ff_stream_add_bitstream_filter(st, "h264_mp4toannexb", NULL);
2268  } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
2269  if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
2270  (AV_RB24(pkt->data) != 0x000001 ||
2271  (st->codecpar->extradata_size > 0 &&
2272  st->codecpar->extradata[0] == 1)))
2273  ret = ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL);
2274  }
2275 
2276  return ret;
2277 }
2278 
2279 #define OFFSET(x) offsetof(MpegTSWrite, x)
2280 #define ENC AV_OPT_FLAG_ENCODING_PARAM
2281 static const AVOption options[] = {
2282  { "mpegts_transport_stream_id", "Set transport_stream_id field.",
2283  OFFSET(transport_stream_id), AV_OPT_TYPE_INT, { .i64 = 0x0001 }, 0x0001, 0xffff, ENC },
2284  { "mpegts_original_network_id", "Set original_network_id field.",
2285  OFFSET(original_network_id), AV_OPT_TYPE_INT, { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, ENC },
2286  { "mpegts_service_id", "Set service_id field.",
2287  OFFSET(service_id), AV_OPT_TYPE_INT, { .i64 = 0x0001 }, 0x0001, 0xffff, ENC },
2288  { "mpegts_service_type", "Set service_type field.",
2289  OFFSET(service_type), AV_OPT_TYPE_INT, { .i64 = 0x01 }, 0x01, 0xff, ENC, "mpegts_service_type" },
2290  { "digital_tv", "Digital Television.",
2291  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff, ENC, "mpegts_service_type" },
2292  { "digital_radio", "Digital Radio.",
2293  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff, ENC, "mpegts_service_type" },
2294  { "teletext", "Teletext.",
2295  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff, ENC, "mpegts_service_type" },
2296  { "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
2297  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO }, 0x01, 0xff, ENC, "mpegts_service_type" },
2298  { "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
2299  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
2300  { "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
2301  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
2302  { "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
2303  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
2304  { "hevc_digital_hdtv", "HEVC Digital Television Service.",
2305  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
2306  { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
2307  OFFSET(pmt_start_pid), AV_OPT_TYPE_INT, { .i64 = 0x1000 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
2308  { "mpegts_start_pid", "Set the first pid.",
2309  OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = 0x0100 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
2310  { "mpegts_m2ts_mode", "Enable m2ts mode.", OFFSET(m2ts_mode), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC },
2311  { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
2312  { "pes_payload_size", "Minimum PES packet payload in bytes",
2313  OFFSET(pes_payload_size), AV_OPT_TYPE_INT, { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, ENC },
2314  { "mpegts_flags", "MPEG-TS muxing flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, ENC, "mpegts_flags" },
2315  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
2316  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX, ENC, "mpegts_flags" },
2317  { "latm", "Use LATM packetization for AAC",
2318  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX, ENC, "mpegts_flags" },
2319  { "pat_pmt_at_frames", "Reemit PAT and PMT at each video frame",
2320  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_PAT_PMT_AT_FRAMES}, 0, INT_MAX, ENC, "mpegts_flags" },
2321  { "system_b", "Conform to System B (DVB) instead of System A (ATSC)",
2322  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_SYSTEM_B }, 0, INT_MAX, ENC, "mpegts_flags" },
2323  { "initial_discontinuity", "Mark initial packets as discontinuous",
2324  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_DISCONT }, 0, INT_MAX, ENC, "mpegts_flags" },
2325  { "nit", "Enable NIT transmission",
2326  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_NIT}, 0, INT_MAX, ENC, "mpegts_flags" },
2327  { "omit_rai", "Disable writing of random access indicator",
2328  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_OMIT_RAI }, 0, INT_MAX, ENC, "mpegts_flags" },
2329  { "mpegts_copyts", "don't offset dts/pts", OFFSET(copyts), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC },
2330  { "tables_version", "set PAT, PMT, SDT and NIT version", OFFSET(tables_version), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 31, ENC },
2331  { "omit_video_pes_length", "Omit the PES packet length for video packets",
2332  OFFSET(omit_video_pes_length), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC },
2333  { "pcr_period", "PCR retransmission time in milliseconds",
2334  OFFSET(pcr_period_ms), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ENC },
2335  { "pat_period", "PAT/PMT retransmission time limit in seconds",
2336  OFFSET(pat_period_us), AV_OPT_TYPE_DURATION, { .i64 = PAT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
2337  { "sdt_period", "SDT retransmission time limit in seconds",
2338  OFFSET(sdt_period_us), AV_OPT_TYPE_DURATION, { .i64 = SDT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
2339  { "nit_period", "NIT retransmission time limit in seconds",
2340  OFFSET(nit_period_us), AV_OPT_TYPE_DURATION, { .i64 = NIT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
2341  { NULL },
2342 };
2343 
2344 static const AVClass mpegts_muxer_class = {
2345  .class_name = "MPEGTS muxer",
2346  .item_name = av_default_item_name,
2347  .option = options,
2348  .version = LIBAVUTIL_VERSION_INT,
2349 };
2350 
2352  .p.name = "mpegts",
2353  .p.long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
2354  .p.mime_type = "video/MP2T",
2355  .p.extensions = "ts,m2t,m2ts,mts",
2356  .priv_data_size = sizeof(MpegTSWrite),
2357  .p.audio_codec = AV_CODEC_ID_MP2,
2358  .p.video_codec = AV_CODEC_ID_MPEG2VIDEO,
2359  .init = mpegts_init,
2360  .write_packet = mpegts_write_packet,
2361  .write_trailer = mpegts_write_end,
2362  .deinit = mpegts_deinit,
2363  .check_bitstream = mpegts_check_bitstream,
2365  .p.flags = AVFMT_ALLOW_FLUSH | AVFMT_VARIABLE_FPS | AVFMT_NODIMENSIONS,
2366 #else
2368 #endif
2369  .flags_internal = FF_FMT_ALLOW_FLUSH,
2370  .p.priv_class = &mpegts_muxer_class,
2371 };
AC3_CHMODE_3F
@ AC3_CHMODE_3F
Definition: ac3defs.h:58
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
DEFAULT_PROVIDER_NAME
#define DEFAULT_PROVIDER_NAME
Definition: mpegtsenc.c:235
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
MpegTSService::pcr_pid
int pcr_pid
Definition: mpegtsenc.c:64
AV_PROFILE_KLVA_SYNC
#define AV_PROFILE_KLVA_SYNC
Definition: defs.h:189
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:445
program
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C program
Definition: undefined.txt:6
MpegTSWrite::last_sdt_ts
int64_t last_sdt_ts
Definition: mpegtsenc.c:125
AC3HeaderInfo::dolby_surround_mode
int dolby_surround_mode
Definition: ac3_parser_internal.h:51
AVOutputFormat::name
const char * name
Definition: avformat.h:511
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
MPEGTS_FLAG_AAC_LATM
#define MPEGTS_FLAG_AAC_LATM
Definition: mpegtsenc.c:112
opt.h
AV_CODEC_ID_PCM_BLURAY
@ AV_CODEC_ID_PCM_BLURAY
Definition: codec_id.h:354
PAT_PID
#define PAT_PID
Definition: mpegts.h:37
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
mpegts.h
MpegTSService::provider_name
uint8_t provider_name[256]
Definition: mpegtsenc.c:63
AVFMT_NODIMENSIONS
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition: avformat.h:484
section_write_packet
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:977
STREAM_TYPE_AUDIO_AAC
#define STREAM_TYPE_AUDIO_AAC
Definition: mpeg.h:55
MpegTSWriteStream::discontinuity
int discontinuity
Definition: mpegtsenc.c:247
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
MpegTSWriteStream::pid
int pid
Definition: mpegtsenc.c:245
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:194
STREAM_TYPE_VIDEO_VC1
#define STREAM_TYPE_VIDEO_VC1
Definition: mpegts.h:134
STREAM_TYPE_PRIVATE_DATA
#define STREAM_TYPE_PRIVATE_DATA
Definition: mpeg.h:54
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVStream::priv_data
void * priv_data
Definition: avformat.h:866
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:483
MpegTSWriteStream::payload_dts
int64_t payload_dts
Definition: mpegtsenc.c:252
mpegts_write_flush
static void mpegts_write_flush(AVFormatContext *s)
Definition: mpegtsenc.c:2188
STREAM_ID_EMM_STREAM
#define STREAM_ID_EMM_STREAM
Definition: mpegts.h:150
MpegTSWrite::pmt_start_pid
int pmt_start_pid
Definition: mpegtsenc.c:102
MpegTSService::program
AVProgram * program
Definition: mpegtsenc.c:65
MpegTSWriteStream
Definition: mpegtsenc.c:244
STREAM_ID_PADDING_STREAM
#define STREAM_ID_PADDING_STREAM
Definition: mpegts.h:145
MpegTSWriteStream::first_timestamp_checked
int first_timestamp_checked
first pts/dts check needed
Definition: mpegtsenc.c:249
AV_CODEC_ID_DIRAC
@ AV_CODEC_ID_DIRAC
Definition: codec_id.h:168
STREAM_ID_PROGRAM_STREAM_MAP
#define STREAM_ID_PROGRAM_STREAM_MAP
Definition: mpegts.h:143
MpegTSWrite::m2ts_mode
int m2ts_mode
Definition: mpegtsenc.c:104
M2TS_AUDIO_START_PID
#define M2TS_AUDIO_START_PID
Definition: mpegts.h:72
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
set_af_flag
static void set_af_flag(uint8_t *pkt, int flag)
Definition: mpegtsenc.c:1409
NIT_TID
#define NIT_TID
Definition: mpegts.h:85
MpegTSWrite::pcr_period_ms
int pcr_period_ms
Definition: mpegtsenc.c:110
SECTION_LENGTH
#define SECTION_LENGTH
Definition: mpegtsenc.c:139
STREAM_TYPE_AUDIO_MPEG1
#define STREAM_TYPE_AUDIO_MPEG1
Definition: mpeg.h:51
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1183
mpegts_deinit
static void mpegts_deinit(AVFormatContext *s)
Definition: mpegtsenc.c:2231
AVPacket::data
uint8_t * data
Definition: packet.h:491
AV_CODEC_ID_DVB_TELETEXT
@ AV_CODEC_ID_DVB_TELETEXT
Definition: codec_id.h:557
NIT_PID
#define NIT_PID
Definition: mpegts.h:42
AVOption
AVOption.
Definition: opt.h:251
b
#define b
Definition: input.c:41
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:930
get_pcr
static int64_t get_pcr(const MpegTSWrite *ts)
Definition: mpegtsenc.c:957
mpegts_check_bitstream
static int mpegts_check_bitstream(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition: mpegtsenc.c:2257
bytestream2_tell_p
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:197
AV_CODEC_ID_AVS2
@ AV_CODEC_ID_AVS2
Definition: codec_id.h:246
data
const char data[16]
Definition: mxf.c:148
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:239
DVBAC3Descriptor::component_type
uint8_t component_type
Definition: mpegts.h:208
NIT_RETRANS_TIME
#define NIT_RETRANS_TIME
Definition: mpegtsenc.c:242
MpegTSWrite::nit_period_us
int64_t nit_period_us
Definition: mpegtsenc.c:123
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
MpegTSWriteStream::payload_pts
int64_t payload_pts
Definition: mpegtsenc.c:251
MPEGTS_FLAG_PAT_PMT_AT_FRAMES
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES
Definition: mpegtsenc.c:113
mathematics.h
AVDictionary
Definition: dict.c:34
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
FIRST_OTHER_PID
#define FIRST_OTHER_PID
Definition: mpegts.h:61
AV_PROFILE_ARIB_PROFILE_C
#define AV_PROFILE_ARIB_PROFILE_C
Definition: defs.h:187
MpegTSSection::opaque
void * opaque
Definition: mpegtsenc.c:56
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
AV_CODEC_ID_HDMV_PGS_SUBTITLE
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h:556
opus_get_packet_samples
static int opus_get_packet_samples(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1798
MpegTSWrite::nit_period
int64_t nit_period
Definition: mpegtsenc.c:88
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:486
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:546
mpegts_write_end
static int mpegts_write_end(AVFormatContext *s)
Definition: mpegtsenc.c:2223
MpegTSWrite::av_class
const AVClass * av_class
Definition: mpegtsenc.c:80
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:36
STREAM_TYPE_AUDIO_DTS
#define STREAM_TYPE_AUDIO_DTS
Definition: mpegts.h:138
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
STREAM_TYPE_AUDIO_MPEG2
#define STREAM_TYPE_AUDIO_MPEG2
Definition: mpeg.h:52
put_registration_descriptor
static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
Definition: mpegtsenc.c:339
MpegTSSection::pid
int pid
Definition: mpegtsenc.c:52
crc.h
mpegts_write_packet
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:2213
AV_PROFILE_ARIB_PROFILE_A
#define AV_PROFILE_ARIB_PROFILE_A
Definition: defs.h:186
OFFSET
#define OFFSET(x)
Definition: mpegtsenc.c:2279
METADATA_DESCRIPTOR
#define METADATA_DESCRIPTOR
Definition: mpegts.h:164
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:761
REGISTRATION_DESCRIPTOR
#define REGISTRATION_DESCRIPTOR
Definition: mpegts.h:159
mpegts_write_pes
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:1476
FF_FMT_ALLOW_FLUSH
#define FF_FMT_ALLOW_FLUSH
Definition: mux.h:30
STREAM_TYPE_VIDEO_AVS2
#define STREAM_TYPE_VIDEO_AVS2
Definition: mpegts.h:132
STREAM_TYPE_VIDEO_AVS3
#define STREAM_TYPE_VIDEO_AVS3
Definition: mpegts.h:133
fail
#define fail()
Definition: checkasm.h:138
STREAM_ID_DSMCC_STREAM
#define STREAM_ID_DSMCC_STREAM
Definition: mpegts.h:151
MpegTSWriteStream::last_pcr
int64_t last_pcr
Definition: mpegtsenc.c:259
get_m2ts_stream_type
static int get_m2ts_stream_type(AVFormatContext *s, AVStream *st)
Definition: mpegtsenc.c:452
AC3HeaderInfo
Definition: ac3_parser_internal.h:34
AC3_CHMODE_3F1R
@ AC3_CHMODE_3F1R
Definition: ac3defs.h:60
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:513
get_dvb_stream_type
static int get_dvb_stream_type(AVFormatContext *s, AVStream *st)
Definition: mpegtsenc.c:351
val
static double val(void *priv, double ch)
Definition: aeval.c:78
MpegTSSection::write_packet
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:55
DVBAC3Descriptor::asvc_flag
uint8_t asvc_flag
Definition: mpegts.h:206
AC3HeaderInfo::channel_mode
uint8_t channel_mode
Definition: ac3_parser_internal.h:43
DEFAULT_SERVICE_NAME
#define DEFAULT_SERVICE_NAME
Definition: mpegtsenc.c:236
pts
static int64_t pts
Definition: transcode_aac.c:643
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:134
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: h264.h:35
SDT_PID
#define SDT_PID
Definition: mpegts.h:43
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:443
MpegTSWrite::pat_period
int64_t pat_period
Definition: mpegtsenc.c:87
MpegTSWrite::provider_name
uint8_t provider_name[256]
Definition: mpegtsenc.c:128
AVRational::num
int num
Numerator.
Definition: rational.h:59
MPEGTS_SERVICE_TYPE_TELETEXT
@ MPEGTS_SERVICE_TYPE_TELETEXT
Definition: mpegtsenc.c:72
STREAM_ID_METADATA_STREAM
#define STREAM_ID_METADATA_STREAM
Definition: mpegts.h:153
MpegTSWrite::total_size
int64_t total_size
Definition: mpegtsenc.c:95
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:551
GET_UTF8
#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:470
AV_DISPOSITION_CLEAN_EFFECTS
#define AV_DISPOSITION_CLEAN_EFFECTS
The audio stream contains music and sound effects without voice.
Definition: avformat.h:764
STREAM_TYPE_AUDIO_EAC3
#define STREAM_TYPE_AUDIO_EAC3
Definition: mpegts.h:140
MpegTSWrite::last_nit_ts
int64_t last_nit_ts
Definition: mpegtsenc.c:126
avio_close_dyn_buf
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1552
first
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
Definition: rate_distortion.txt:12
avassert.h
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
MpegTSWrite::next_pcr
int64_t next_pcr
Definition: mpegtsenc.c:92
select_pcr_streams
static void select_pcr_streams(AVFormatContext *s)
Definition: mpegtsenc.c:1059
STREAM_ID_EXTENDED_STREAM_ID
#define STREAM_ID_EXTENDED_STREAM_ID
Definition: mpegts.h:154
MpegTSWrite::original_network_id
int original_network_id
Definition: mpegtsenc.c:98
duration
int64_t duration
Definition: movenc.c:64
MpegTSWrite::start_pid
int start_pid
Definition: mpegtsenc.c:103
bytestream2_init_writer
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
write_pcr_bits
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
Definition: mpegtsenc.c:1336
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1507
av_dict_get
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:62
AV_CODEC_ID_S302M
@ AV_CODEC_ID_S302M
Definition: codec_id.h:356
STREAM_TYPE_AUDIO_AC3
#define STREAM_TYPE_AUDIO_AC3
Definition: mpeg.h:61
STREAM_TYPE_VIDEO_MPEG4
#define STREAM_TYPE_VIDEO_MPEG4
Definition: mpeg.h:56
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
MPEGTS_FLAG_SYSTEM_B
#define MPEGTS_FLAG_SYSTEM_B
Definition: mpegtsenc.c:114
MpegTSService::name
uint8_t name[256]
Definition: mpegtsenc.c:62
frame_size
int frame_size
Definition: mxfenc.c:2311
bytestream2_put_buffer
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:286
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:442
av_match_ext
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:41
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
options
static const AVOption options[]
Definition: mpegtsenc.c:2281
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
STREAM_ID_TYPE_E_STREAM
#define STREAM_ID_TYPE_E_STREAM
Definition: mpegts.h:152
nb_streams
static int nb_streams
Definition: ffprobe.c:328
av_rescale_q
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
PMT_TID
#define PMT_TID
Definition: mpegts.h:81
mpegts_init
static int mpegts_init(AVFormatContext *s)
Definition: mpegtsenc.c:1088
MpegTSWriteStream::cc
int cc
Definition: mpegtsenc.c:246
extend_af
static void extend_af(uint8_t *pkt, int size)
Definition: mpegtsenc.c:1425
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:389
LAST_OTHER_PID
#define LAST_OTHER_PID
Definition: mpegts.h:62
key
const char * key
Definition: hwcontext_opencl.c:174
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
DVBAC3Descriptor::component_type_flag
uint8_t component_type_flag
Definition: mpegts.h:203
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
MpegTSWrite::sdt_period
int64_t sdt_period
Definition: mpegtsenc.c:86
avformat_write_header
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:477
AV_CODEC_ID_ARIB_CAPTION
@ AV_CODEC_ID_ARIB_CAPTION
Definition: codec_id.h:575
MpegTSWrite::first_dts_checked
int first_dts_checked
Definition: mpegtsenc.c:91
retransmit_si_info
static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int force_nit, int64_t pcr)
Definition: mpegtsenc.c:1303
MpegTSWrite::m2ts_textsub_pid
int m2ts_textsub_pid
Definition: mpegtsenc.c:108
mpegts_insert_null_packet
static void mpegts_insert_null_packet(AVFormatContext *s)
Definition: mpegtsenc.c:1351
AV_CODEC_ID_AVS3
@ AV_CODEC_ID_AVS3
Definition: codec_id.h:248
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
M2TS_VIDEO_PID
#define M2TS_VIDEO_PID
Definition: mpegts.h:71
PCR_TIME_BASE
#define PCR_TIME_BASE
Definition: mpegtsenc.c:42
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:880
NULL
#define NULL
Definition: coverity.c:32
mpegts_write_section
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
Definition: mpegtsenc.c:142
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
MPEGTS_SERVICE_TYPE_DIGITAL_RADIO
@ MPEGTS_SERVICE_TYPE_DIGITAL_RADIO
Definition: mpegtsenc.c:71
AV_CODEC_ID_TIMED_ID3
@ AV_CODEC_ID_TIMED_ID3
Definition: codec_id.h:589
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:403
MpegTSWrite::nb_services
int nb_services
Definition: mpegtsenc.c:89
STREAM_ID_PRIVATE_STREAM_1
#define STREAM_ID_PRIVATE_STREAM_1
Definition: mpegts.h:144
M2TS_TEXTSUB_PID
#define M2TS_TEXTSUB_PID
Definition: mpegts.h:74
MpegTSWrite::tables_version
int tables_version
Definition: mpegtsenc.c:120
MpegTSWrite::nit
MpegTSSection nit
Definition: mpegtsenc.c:83
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1157
M2TS_PGSSUB_START_PID
#define M2TS_PGSSUB_START_PID
Definition: mpegts.h:73
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:921
avpriv_find_start_code
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
FFOutputFormat
Definition: mux.h:32
MpegTSService::sid
int sid
Definition: mpegtsenc.c:61
AV_CODEC_ID_SMPTE_KLV
@ AV_CODEC_ID_SMPTE_KLV
Definition: codec_id.h:587
av_write_frame
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:1223
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:206
MpegTSWrite::m2ts_pgssub_pid
int m2ts_pgssub_pid
Definition: mpegtsenc.c:107
MPEGTS_FLAG_REEMIT_PAT_PMT
#define MPEGTS_FLAG_REEMIT_PAT_PMT
Definition: mpegtsenc.c:111
MpegTSWrite::sdt_period_us
int64_t sdt_period_us
Definition: mpegtsenc.c:122
MpegTSWrite::service_type
int service_type
Definition: mpegtsenc.c:100
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:171
H264_NAL_AUD
@ H264_NAL_AUD
Definition: h264.h:43
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:902
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:482
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:417
enable_pcr_generation_for_stream
static void enable_pcr_generation_for_stream(AVFormatContext *s, AVStream *pcr_st)
Definition: mpegtsenc.c:1028
SDT_TID
#define SDT_TID
Definition: mpegts.h:87
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:444
M2TS_PMT_PID
#define M2TS_PMT_PID
Definition: mpegts.h:69
MpegTSWrite::copyts
int copyts
Definition: mpegtsenc.c:119
PutByteContext
Definition: bytestream.h:37
MpegTSWriteStream::amux
AVFormatContext * amux
Definition: mpegtsenc.c:255
startcode.h
av_rescale_rnd
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
ac3_parser_internal.h
AC3_CHMODE_STEREO
@ AC3_CHMODE_STEREO
Definition: ac3defs.h:57
AVPacket::size
int size
Definition: packet.h:492
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:166
STREAM_TYPE_VIDEO_HEVC
#define STREAM_TYPE_VIDEO_HEVC
Definition: mpeg.h:58
MpegTSWriteStream::data_st_warning
int data_st_warning
Definition: mpegtsenc.c:256
DVBAC3Descriptor::bsid_flag
uint8_t bsid_flag
Definition: mpegts.h:204
mpegts_write_packet_internal
static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1846
av_bswap32
#define av_bswap32
Definition: bswap.h:28
MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV
@ MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV
Definition: mpegtsenc.c:74
MpegTSWriteStream::opus_queued_samples
int opus_queued_samples
Definition: mpegtsenc.c:262
AV_CODEC_ID_DTS
@ AV_CODEC_ID_DTS
Definition: codec_id.h:446
size
int size
Definition: twinvq_data.h:10344
mpegts_write_pat
static void mpegts_write_pat(AVFormatContext *s)
Definition: mpegtsenc.c:268
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV
@ MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV
Definition: mpegtsenc.c:76
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AV_RB32
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:96
section
Definition: ffprobe.c:216
STREAM_ID_PRIVATE_STREAM_2
#define STREAM_ID_PRIVATE_STREAM_2
Definition: mpegts.h:146
av_get_audio_frame_duration2
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:812
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:115
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:502
ff_mpegts_muxer
const FFOutputFormat ff_mpegts_muxer
Definition: mpegtsenc.c:2351
mpegts_add_service
static MpegTSService * mpegts_add_service(AVFormatContext *s, int sid, const AVDictionary *metadata, AVProgram *program)
Definition: mpegtsenc.c:983
MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV
@ MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV
Definition: mpegtsenc.c:77
MpegTSWrite::last_pat_ts
int64_t last_pat_ts
Definition: mpegtsenc.c:124
AV_DISPOSITION_HEARING_IMPAIRED
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition: avformat.h:756
MpegTSWrite::m2ts_video_pid
int m2ts_video_pid
Definition: mpegtsenc.c:105
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:490
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:248
FF_COMPLIANCE_NORMAL
#define FF_COMPLIANCE_NORMAL
Definition: defs.h:60
STREAM_TYPE_VIDEO_MPEG2
#define STREAM_TYPE_VIDEO_MPEG2
Definition: mpeg.h:50
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
MpegTSWrite::sdt
MpegTSSection sdt
Definition: mpegtsenc.c:82
PCR_RETRANS_TIME
#define PCR_RETRANS_TIME
Definition: mpegtsenc.c:241
MpegTSWrite::services
MpegTSService ** services
Definition: mpegtsenc.c:84
write_packet
static void write_packet(AVFormatContext *s, const uint8_t *packet)
Definition: mpegtsenc.c:963
AC3HeaderInfo::bitstream_mode
uint8_t bitstream_mode
Definition: ac3_parser_internal.h:42
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:497
MpegTSWriteStream::prev_payload_key
int prev_payload_key
Definition: mpegtsenc.c:250
MpegTSWrite::pes_payload_size
int pes_payload_size
Definition: mpegtsenc.c:94
version
version
Definition: libkvazaar.c:321
STREAM_TYPE_AUDIO_AAC_LATM
#define STREAM_TYPE_AUDIO_AAC_LATM
Definition: mpegts.h:126
flag
#define flag(name)
Definition: cbs_av1.c:466
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:417
STREAM_TYPE_VIDEO_DIRAC
#define STREAM_TYPE_VIDEO_DIRAC
Definition: mpegts.h:135
ISO_639_LANGUAGE_DESCRIPTOR
#define ISO_639_LANGUAGE_DESCRIPTOR
Definition: mpegts.h:160
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:484
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
avio_internal.h
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: avpacket.c:252
put16
static void put16(uint8_t **q_ptr, int val)
Definition: mpegtsenc.c:196
SDT_RETRANS_TIME
#define SDT_RETRANS_TIME
Definition: mpegtsenc.c:239
mpegts_write_section1
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:205
AV_CODEC_ID_SMPTE_2038
@ AV_CODEC_ID_SMPTE_2038
Definition: codec_id.h:591
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
PAT_TID
#define PAT_TID
Definition: mpegts.h:79
AV_CODEC_ID_CAVS
@ AV_CODEC_ID_CAVS
Definition: codec_id.h:139
AC3_CHMODE_DUALMONO
@ AC3_CHMODE_DUALMONO
Definition: ac3defs.h:55
avpriv_ac3_parse_header
int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf, size_t size)
Definition: ac3_parser.c:265
packet
enum AVPacketSideDataType packet
Definition: decode.c:1425
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
MpegTSWrite::m2ts_audio_pid
int m2ts_audio_pid
Definition: mpegtsenc.c:106
else
else
Definition: snow.txt:125
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
MPEGTS_FLAG_NIT
#define MPEGTS_FLAG_NIT
Definition: mpegtsenc.c:116
putbuf
static void putbuf(uint8_t **q_ptr, const uint8_t *buf, size_t len)
Definition: mpegtsenc.c:289
write_pts
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
Definition: mpegtsenc.c:1394
STREAM_TYPE_AUDIO_TRUEHD
#define STREAM_TYPE_AUDIO_TRUEHD
Definition: mpegts.h:139
ENC
#define ENC
Definition: mpegtsenc.c:2280
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
AV_PKT_DATA_MPEGTS_STREAM_ID
@ AV_PKT_DATA_MPEGTS_STREAM_ID
MPEGTS stream ID as uint8_t, this is required to pass the stream ID information from the demuxer to t...
Definition: packet.h:216
AVProgram
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1039
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: codec_id.h:122
MPEGTS_FLAG_DISCONT
#define MPEGTS_FLAG_DISCONT
Definition: mpegtsenc.c:115
len
int len
Definition: vorbis_enc_data.h:426
DVBAC3Descriptor::asvc
uint8_t asvc
Definition: mpegts.h:211
av_rescale
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
MpegTSWrite
Definition: mpegtsenc.c:79
MpegTSWriteStream::opus_pending_trim_start
int opus_pending_trim_start
Definition: mpegtsenc.c:263
AV_CRC_32_IEEE
@ AV_CRC_32_IEEE
Definition: crc.h:52
MpegTSWrite::service_id
int service_id
Definition: mpegtsenc.c:99
FF_API_ALLOW_FLUSH
#define FF_API_ALLOW_FLUSH
Definition: version_major.h:50
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO
@ MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO
Definition: mpegtsenc.c:73
PAT_RETRANS_TIME
#define PAT_RETRANS_TIME
Definition: mpegtsenc.c:240
AC3_CHMODE_MONO
@ AC3_CHMODE_MONO
Definition: ac3defs.h:56
mpegts_write_pmt
static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
Definition: mpegtsenc.c:503
AC3_CHMODE_3F2R
@ AC3_CHMODE_3F2R
Definition: ac3defs.h:62
MpegTSWriteStream::payload_size
int payload_size
Definition: mpegtsenc.c:248
AC3_CHMODE_2F1R
@ AC3_CHMODE_2F1R
Definition: ac3defs.h:59
MpegTSWrite::flags
int flags
Definition: mpegtsenc.c:118
MpegTSWrite::first_pcr
int64_t first_pcr
Definition: mpegtsenc.c:90
language
Undefined Behavior In the C language
Definition: undefined.txt:3
MpegTSWriteStream::dvb_ac3_desc
DVBAC3Descriptor * dvb_ac3_desc
Definition: mpegtsenc.c:265
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:910
AV_DISPOSITION_VISUAL_IMPAIRED
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition: avformat.h:760
STREAM_ID_VIDEO_STREAM_0
#define STREAM_ID_VIDEO_STREAM_0
Definition: mpegts.h:148
tag
uint32_t tag
Definition: movenc.c:1737
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1580
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:853
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:841
bswap.h
AVClass::class_name
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:71
AVFormatContext::oformat
const struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1134
AC3_CHMODE_2F2R
@ AC3_CHMODE_2F2R
Definition: ac3defs.h:61
MpegTSSection
Definition: mpegtsenc.c:51
avformat.h
dict.h
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
MpegTSWrite::pkt
AVPacket * pkt
Definition: mpegtsenc.c:85
av_dynarray_add_nofree
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:313
AC3_DSURMOD_ON
@ AC3_DSURMOD_ON
Definition: ac3defs.h:69
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:847
MpegTSWrite::pat
MpegTSSection pat
Definition: mpegtsenc.c:81
STREAM_TYPE_VIDEO_H264
#define STREAM_TYPE_VIDEO_H264
Definition: mpeg.h:57
mpegts_write_sdt
static void mpegts_write_sdt(AVFormatContext *s)
Definition: mpegtsenc.c:842
av_crc
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
AVRational::den
int den
Denominator.
Definition: rational.h:60
mpegts_write_nit
static void mpegts_write_nit(AVFormatContext *s)
Definition: mpegtsenc.c:880
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
defs.h
MpegTSSection::cc
int cc
Definition: mpegtsenc.c:53
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:102
STREAM_TYPE_VIDEO_CAVS
#define STREAM_TYPE_VIDEO_CAVS
Definition: mpeg.h:59
MpegTSWriteStream::payload
uint8_t * payload
Definition: mpegtsenc.c:254
AV_PKT_DATA_SKIP_SAMPLES
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:157
MpegTSWriteStream::pcr_period
int64_t pcr_period
Definition: mpegtsenc.c:258
check_hevc_startcode
static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Definition: mpegtsenc.c:1779
AVPacket::stream_index
int stream_index
Definition: packet.h:493
state
static struct @362 state
DVBAC3Descriptor
Definition: mpegts.h:202
MpegTSService::pmt
MpegTSSection pmt
Definition: mpegtsenc.c:60
mpegts_muxer_class
static const AVClass mpegts_muxer_class
Definition: mpegtsenc.c:2344
av_log_once
void av_log_once(void *avcl, int initial_level, int subsequent_level, int *state, const char *fmt,...)
Definition: log.c:417
MpegTSWriteStream::payload_flags
int payload_flags
Definition: mpegtsenc.c:253
AC3HeaderInfo::bitstream_id
uint8_t bitstream_id
Definition: ac3_parser_internal.h:41
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: h264.h:39
av_guess_format
const 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:79
STREAM_ID_AUDIO_STREAM_0
#define STREAM_ID_AUDIO_STREAM_0
Definition: mpegts.h:147
DVBAC3Descriptor::mainid
uint8_t mainid
Definition: mpegts.h:210
FFFormatContext::pkt
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition: internal.h:140
MpegTSService
Definition: mpegtsenc.c:59
STREAM_TYPE_METADATA
#define STREAM_TYPE_METADATA
Definition: mpegts.h:128
H264_NAL_SPS
@ H264_NAL_SPS
Definition: h264.h:41
MpegTSWrite::transport_stream_id
int transport_stream_id
Definition: mpegtsenc.c:97
STREAM_ID_ECM_STREAM
#define STREAM_ID_ECM_STREAM
Definition: mpegts.h:149
DVBAC3Descriptor::bsid
uint8_t bsid
Definition: mpegts.h:209
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVDictionaryEntry
Definition: dict.h:89
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
STREAM_ID_PROGRAM_STREAM_DIRECTORY
#define STREAM_ID_PROGRAM_STREAM_DIRECTORY
Definition: mpegts.h:155
MpegTSWrite::pat_period_us
int64_t pat_period_us
Definition: mpegtsenc.c:121
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
get_pes_stream_id
static int get_pes_stream_id(AVFormatContext *s, AVStream *st, int stream_id, int *async)
Definition: mpegtsenc.c:1441
AV_OPT_TYPE_FLAGS
@ AV_OPT_TYPE_FLAGS
Definition: opt.h:224
bytestream.h
h264.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
MpegTSWrite::omit_video_pes_length
int omit_video_pes_length
Definition: mpegtsenc.c:130
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CODEC_ID_AAC_LATM
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:491
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
get_ts_payload_start
static uint8_t * get_ts_payload_start(uint8_t *pkt)
Definition: mpegtsenc.c:1433
AV_CODEC_ID_HDMV_TEXT_SUBTITLE
@ AV_CODEC_ID_HDMV_TEXT_SUBTITLE
Definition: codec_id.h:573
TS_PACKET_SIZE
#define TS_PACKET_SIZE
Definition: mpegts.h:29
mpegts_insert_pcr_only
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
Definition: mpegtsenc.c:1366
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
DVB_PRIVATE_NETWORK_START
#define DVB_PRIVATE_NETWORK_START
Definition: mpegtsenc.c:46
AVDictionaryEntry::value
char * value
Definition: dict.h:91
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV
@ MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV
Definition: mpegtsenc.c:75
AV_RB24
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:97
DEFAULT_PES_PAYLOAD_SIZE
#define DEFAULT_PES_PAYLOAD_SIZE
Definition: mpegtsenc.c:135
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
snprintf
#define snprintf
Definition: snprintf.h:34
AVCodecParameters::initial_padding
int initial_padding
Audio only.
Definition: codec_par.h:190
MPEGTS_SERVICE_TYPE_DIGITAL_TV
@ MPEGTS_SERVICE_TYPE_DIGITAL_TV
Definition: mpegtsenc.c:70
ff_stream_add_bitstream_filter
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: mux.c:1347
DVBAC3Descriptor::mainid_flag
uint8_t mainid_flag
Definition: mpegts.h:205
MpegTSSection::discontinuity
int discontinuity
Definition: mpegtsenc.c:54
MPEGTS_FLAG_OMIT_RAI
#define MPEGTS_FLAG_OMIT_RAI
Definition: mpegtsenc.c:117
encode_str8
static int encode_str8(uint8_t *buf, const char *str)
Definition: mpegtsenc.c:925
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:106
MpegTSWrite::mux_rate
int mux_rate
set to 1 when VBR
Definition: mpegtsenc.c:93
AV_RB16
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:98
ff_check_h264_startcode
int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Check presence of H264 startcode.
Definition: mpegtsenc.c:1762
put_arib_caption_descriptor
static int put_arib_caption_descriptor(AVFormatContext *s, uint8_t **q_ptr, AVCodecParameters *codecpar)
Definition: mpegtsenc.c:295
mux.h