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