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