FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpegtsenc.c
Go to the documentation of this file.
1 /*
2  * MPEG2 transport stream (aka DVB) muxer
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/avassert.h"
23 #include "libavutil/bswap.h"
24 #include "libavutil/crc.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
29 
30 #include "libavcodec/internal.h"
31 
32 #include "avformat.h"
33 #include "avio_internal.h"
34 #include "internal.h"
35 #include "mpegts.h"
36 
37 #define PCR_TIME_BASE 27000000
38 
39 /* write DVB SI sections */
40 
41 /*********************************************/
42 /* mpegts section writer */
43 
44 typedef struct MpegTSSection {
45  int pid;
46  int cc;
47  void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
48  void *opaque;
50 
51 typedef struct MpegTSService {
52  MpegTSSection pmt; /* MPEG2 pmt table context */
53  int sid; /* service ID */
54  char *name;
56  int pcr_pid;
60 
61 // service_type values as defined in ETSI 300 468
62 enum {
70 };
71 typedef struct MpegTSWrite {
72  const AVClass *av_class;
73  MpegTSSection pat; /* MPEG2 pat table */
74  MpegTSSection sdt; /* MPEG2 sdt table context */
81  int onid;
82  int tsid;
83  int64_t first_pcr;
84  int mux_rate; ///< set to 1 when VBR
86 
91 
93  int start_pid;
94  int m2ts_mode;
95 
96  int reemit_pat_pmt; // backward compatibility
97 
99 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
100 #define MPEGTS_FLAG_AAC_LATM 0x02
101 #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
102  int flags;
103  int copyts;
105  float pat_period;
106  float sdt_period;
107  int64_t last_pat_ts;
108  int64_t last_sdt_ts;
109 
111 } MpegTSWrite;
112 
113 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
114 #define DEFAULT_PES_HEADER_FREQ 16
115 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
116 
117 /* The section length is 12 bits. The first 2 are set to 0, the remaining
118  * 10 bits should not exceed 1021. */
119 #define SECTION_LENGTH 1020
120 
121 /* NOTE: 4 bytes must be left at the end for the crc32 */
123 {
124  unsigned int crc;
125  unsigned char packet[TS_PACKET_SIZE];
126  const unsigned char *buf_ptr;
127  unsigned char *q;
128  int first, b, len1, left;
129 
131  -1, buf, len - 4));
132 
133  buf[len - 4] = (crc >> 24) & 0xff;
134  buf[len - 3] = (crc >> 16) & 0xff;
135  buf[len - 2] = (crc >> 8) & 0xff;
136  buf[len - 1] = crc & 0xff;
137 
138  /* send each packet */
139  buf_ptr = buf;
140  while (len > 0) {
141  first = buf == buf_ptr;
142  q = packet;
143  *q++ = 0x47;
144  b = s->pid >> 8;
145  if (first)
146  b |= 0x40;
147  *q++ = b;
148  *q++ = s->pid;
149  s->cc = s->cc + 1 & 0xf;
150  *q++ = 0x10 | s->cc;
151  if (first)
152  *q++ = 0; /* 0 offset */
153  len1 = TS_PACKET_SIZE - (q - packet);
154  if (len1 > len)
155  len1 = len;
156  memcpy(q, buf_ptr, len1);
157  q += len1;
158  /* add known padding data */
159  left = TS_PACKET_SIZE - (q - packet);
160  if (left > 0)
161  memset(q, 0xff, left);
162 
163  s->write_packet(s, packet);
164 
165  buf_ptr += len1;
166  len -= len1;
167  }
168 }
169 
170 static inline void put16(uint8_t **q_ptr, int val)
171 {
172  uint8_t *q;
173  q = *q_ptr;
174  *q++ = val >> 8;
175  *q++ = val;
176  *q_ptr = q;
177 }
178 
179 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
180  int version, int sec_num, int last_sec_num,
181  uint8_t *buf, int len)
182 {
183  uint8_t section[1024], *q;
184  unsigned int tot_len;
185  /* reserved_future_use field must be set to 1 for SDT */
186  unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
187 
188  tot_len = 3 + 5 + len + 4;
189  /* check if not too big */
190  if (tot_len > 1024)
191  return AVERROR_INVALIDDATA;
192 
193  q = section;
194  *q++ = tid;
195  put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
196  put16(&q, id);
197  *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
198  *q++ = sec_num;
199  *q++ = last_sec_num;
200  memcpy(q, buf, len);
201 
202  mpegts_write_section(s, section, tot_len);
203  return 0;
204 }
205 
206 /*********************************************/
207 /* mpegts writer */
208 
209 #define DEFAULT_PROVIDER_NAME "FFmpeg"
210 #define DEFAULT_SERVICE_NAME "Service01"
211 
212 /* we retransmit the SI info at this rate */
213 #define SDT_RETRANS_TIME 500
214 #define PAT_RETRANS_TIME 100
215 #define PCR_RETRANS_TIME 20
216 
217 typedef struct MpegTSWriteStream {
219  int pid; /* stream associated pid */
220  int cc;
222  int first_pts_check; ///< first pts check needed
224  int64_t payload_pts;
225  int64_t payload_dts;
231 
233 {
234  MpegTSWrite *ts = s->priv_data;
235  MpegTSService *service;
237  int i;
238 
239  q = data;
240  for (i = 0; i < ts->nb_services; i++) {
241  service = ts->services[i];
242  put16(&q, service->sid);
243  put16(&q, 0xe000 | service->pmt.pid);
244  }
245  mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, ts->tables_version, 0, 0,
246  data, q - data);
247 }
248 
250 {
251  MpegTSWrite *ts = s->priv_data;
252  uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
253  int val, stream_type, i, err = 0;
254 
255  q = data;
256  put16(&q, 0xe000 | service->pcr_pid);
257 
258  program_info_length_ptr = q;
259  q += 2; /* patched after */
260 
261  /* put program info here */
262 
263  val = 0xf000 | (q - program_info_length_ptr - 2);
264  program_info_length_ptr[0] = val >> 8;
265  program_info_length_ptr[1] = val;
266 
267  for (i = 0; i < s->nb_streams; i++) {
268  AVStream *st = s->streams[i];
269  MpegTSWriteStream *ts_st = st->priv_data;
270  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
271 
272  if (q - data > SECTION_LENGTH - 32) {
273  err = 1;
274  break;
275  }
276  switch (st->codec->codec_id) {
279  stream_type = STREAM_TYPE_VIDEO_MPEG2;
280  break;
281  case AV_CODEC_ID_MPEG4:
282  stream_type = STREAM_TYPE_VIDEO_MPEG4;
283  break;
284  case AV_CODEC_ID_H264:
285  stream_type = STREAM_TYPE_VIDEO_H264;
286  break;
287  case AV_CODEC_ID_HEVC:
288  stream_type = STREAM_TYPE_VIDEO_HEVC;
289  break;
290  case AV_CODEC_ID_CAVS:
291  stream_type = STREAM_TYPE_VIDEO_CAVS;
292  break;
293  case AV_CODEC_ID_DIRAC:
294  stream_type = STREAM_TYPE_VIDEO_DIRAC;
295  break;
296  case AV_CODEC_ID_MP2:
297  case AV_CODEC_ID_MP3:
298  stream_type = STREAM_TYPE_AUDIO_MPEG1;
299  break;
300  case AV_CODEC_ID_AAC:
301  stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
304  break;
306  stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
307  break;
308  case AV_CODEC_ID_AC3:
309  stream_type = STREAM_TYPE_AUDIO_AC3;
310  break;
311  case AV_CODEC_ID_DTS:
312  stream_type = STREAM_TYPE_AUDIO_DTS;
313  break;
314  case AV_CODEC_ID_TRUEHD:
315  stream_type = STREAM_TYPE_AUDIO_TRUEHD;
316  break;
317  default:
318  stream_type = STREAM_TYPE_PRIVATE_DATA;
319  break;
320  }
321 
322  *q++ = stream_type;
323  put16(&q, 0xe000 | ts_st->pid);
324  desc_length_ptr = q;
325  q += 2; /* patched after */
326 
327  /* write optional descriptors here */
328  switch (st->codec->codec_type) {
329  case AVMEDIA_TYPE_AUDIO:
330  if (st->codec->codec_id==AV_CODEC_ID_EAC3) {
331  *q++=0x7a; // EAC3 descriptor see A038 DVB SI
332  *q++=1; // 1 byte, all flags sets to 0
333  *q++=0; // omit all fields...
334  }
335  if (st->codec->codec_id==AV_CODEC_ID_S302M) {
336  *q++ = 0x05; /* MPEG-2 registration descriptor*/
337  *q++ = 4;
338  *q++ = 'B';
339  *q++ = 'S';
340  *q++ = 'S';
341  *q++ = 'D';
342  }
343 
344  if (lang) {
345  char *p;
346  char *next = lang->value;
347  uint8_t *len_ptr;
348 
349  *q++ = 0x0a; /* ISO 639 language descriptor */
350  len_ptr = q++;
351  *len_ptr = 0;
352 
353  for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
354  if (q - data > SECTION_LENGTH - 4) {
355  err = 1;
356  break;
357  }
358  next = strchr(p, ',');
359  if (strlen(p) != 3 && (!next || next != p + 3))
360  continue; /* not a 3-letter code */
361 
362  *q++ = *p++;
363  *q++ = *p++;
364  *q++ = *p++;
365 
367  *q++ = 0x01;
369  *q++ = 0x02;
371  *q++ = 0x03;
372  else
373  *q++ = 0; /* undefined type */
374 
375  *len_ptr += 4;
376  }
377 
378  if (*len_ptr == 0)
379  q -= 2; /* no language codes were written */
380  }
381  break;
383  {
384  const char default_language[] = "und";
385  const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
386 
388  uint8_t *len_ptr;
389  int extradata_copied = 0;
390 
391  *q++ = 0x59; /* subtitling_descriptor */
392  len_ptr = q++;
393 
394  while (strlen(language) >= 3) {
395  if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
396  err = 1;
397  break;
398  }
399  *q++ = *language++;
400  *q++ = *language++;
401  *q++ = *language++;
402  /* Skip comma */
403  if (*language != '\0')
404  language++;
405 
406  if (st->codec->extradata_size - extradata_copied >= 5) {
407  *q++ = st->codec->extradata[extradata_copied + 4]; /* subtitling_type */
408  memcpy(q, st->codec->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
409  extradata_copied += 5;
410  q += 4;
411  } else {
412  /* subtitling_type:
413  * 0x10 - normal with no monitor aspect ratio criticality
414  * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
415  *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
416  if ((st->codec->extradata_size == 4) && (extradata_copied == 0)) {
417  /* support of old 4-byte extradata format */
418  memcpy(q, st->codec->extradata, 4); /* composition_page_id and ancillary_page_id */
419  extradata_copied += 4;
420  q += 4;
421  } else {
422  put16(&q, 1); /* composition_page_id */
423  put16(&q, 1); /* ancillary_page_id */
424  }
425  }
426  }
427 
428  *len_ptr = q - len_ptr - 1;
429  } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
430  uint8_t *len_ptr = NULL;
431  int extradata_copied = 0;
432 
433  /* The descriptor tag. teletext_descriptor */
434  *q++ = 0x56;
435  len_ptr = q++;
436 
437  while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
438  *q++ = *language++;
439  *q++ = *language++;
440  *q++ = *language++;
441  /* Skip comma */
442  if (*language != '\0')
443  language++;
444 
445  if (st->codec->extradata_size - 1 > extradata_copied) {
446  memcpy(q, st->codec->extradata + extradata_copied, 2);
447  extradata_copied += 2;
448  q += 2;
449  } else {
450  /* The Teletext descriptor:
451  * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
452  * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
453  * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
454  *q++ = 0x08;
455  *q++ = 0x00;
456  }
457  }
458 
459  *len_ptr = q - len_ptr - 1;
460  }
461  }
462  break;
463  case AVMEDIA_TYPE_VIDEO:
464  if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
465  *q++ = 0x05; /*MPEG-2 registration descriptor*/
466  *q++ = 4;
467  *q++ = 'd';
468  *q++ = 'r';
469  *q++ = 'a';
470  *q++ = 'c';
471  }
472  break;
473  case AVMEDIA_TYPE_DATA:
474  if (st->codec->codec_id == AV_CODEC_ID_SMPTE_KLV) {
475  *q++ = 0x05; /* MPEG-2 registration descriptor */
476  *q++ = 4;
477  *q++ = 'K';
478  *q++ = 'L';
479  *q++ = 'V';
480  *q++ = 'A';
481  }
482  break;
483  }
484 
485  val = 0xf000 | (q - desc_length_ptr - 2);
486  desc_length_ptr[0] = val >> 8;
487  desc_length_ptr[1] = val;
488  }
489 
490  if (err)
491  av_log(s, AV_LOG_ERROR,
492  "The PMT section cannot fit stream %d and all following streams.\n"
493  "Try reducing the number of languages in the audio streams "
494  "or the total number of streams.\n", i);
495 
496  mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
497  data, q - data);
498  return 0;
499 }
500 
501 /* NOTE: !str is accepted for an empty string */
502 static void putstr8(uint8_t **q_ptr, const char *str)
503 {
504  uint8_t *q;
505  int len;
506 
507  q = *q_ptr;
508  if (!str)
509  len = 0;
510  else
511  len = strlen(str);
512  *q++ = len;
513  memcpy(q, str, len);
514  q += len;
515  *q_ptr = q;
516 }
517 
519 {
520  MpegTSWrite *ts = s->priv_data;
521  MpegTSService *service;
522  uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
523  int i, running_status, free_ca_mode, val;
524 
525  q = data;
526  put16(&q, ts->onid);
527  *q++ = 0xff;
528  for (i = 0; i < ts->nb_services; i++) {
529  service = ts->services[i];
530  put16(&q, service->sid);
531  *q++ = 0xfc | 0x00; /* currently no EIT info */
532  desc_list_len_ptr = q;
533  q += 2;
534  running_status = 4; /* running */
535  free_ca_mode = 0;
536 
537  /* write only one descriptor for the service name and provider */
538  *q++ = 0x48;
539  desc_len_ptr = q;
540  q++;
541  *q++ = ts->service_type;
542  putstr8(&q, service->provider_name);
543  putstr8(&q, service->name);
544  desc_len_ptr[0] = q - desc_len_ptr - 1;
545 
546  /* fill descriptor length */
547  val = (running_status << 13) | (free_ca_mode << 12) |
548  (q - desc_list_len_ptr - 2);
549  desc_list_len_ptr[0] = val >> 8;
550  desc_list_len_ptr[1] = val;
551  }
552  mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, ts->tables_version, 0, 0,
553  data, q - data);
554 }
555 
557  const char *provider_name,
558  const char *name)
559 {
560  MpegTSService *service;
561 
562  service = av_mallocz(sizeof(MpegTSService));
563  if (!service)
564  return NULL;
565  service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
566  service->sid = sid;
567  service->pcr_pid = 0x1fff;
568  service->provider_name = av_strdup(provider_name);
569  service->name = av_strdup(name);
570  if (!service->provider_name || !service->name)
571  goto fail;
572  if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
573  goto fail;
574 
575  return service;
576 fail:
577  av_freep(&service->provider_name);
578  av_freep(&service->name);
579  av_free(service);
580  return NULL;
581 }
582 
583 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
584 {
585  return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
586  ts->first_pcr;
587 }
588 
590 {
591  MpegTSWrite *ts = s->priv_data;
592  if (ts->m2ts_mode) {
593  int64_t pcr = get_pcr(s->priv_data, s->pb);
594  uint32_t tp_extra_header = pcr % 0x3fffffff;
595  tp_extra_header = AV_RB32(&tp_extra_header);
596  avio_write(s->pb, (unsigned char *) &tp_extra_header,
597  sizeof(tp_extra_header));
598  }
599 }
600 
601 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
602 {
603  AVFormatContext *ctx = s->opaque;
605  avio_write(ctx->pb, packet, TS_PACKET_SIZE);
606 }
607 
609 {
610  MpegTSWrite *ts = s->priv_data;
611  MpegTSWriteStream *ts_st;
612  MpegTSService *service;
613  AVStream *st, *pcr_st = NULL;
614  AVDictionaryEntry *title, *provider;
615  int i, j;
616  const char *service_name;
617  const char *provider_name;
618  int *pids;
619  int ret;
620 
621  if (s->max_delay < 0) /* Not set by the caller */
622  s->max_delay = 0;
623 
624  // round up to a whole number of TS packets
625  ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
626 
627  ts->tsid = ts->transport_stream_id;
628  ts->onid = ts->original_network_id;
629  /* allocate a single DVB service */
630  title = av_dict_get(s->metadata, "service_name", NULL, 0);
631  if (!title)
632  title = av_dict_get(s->metadata, "title", NULL, 0);
633  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
634  provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
635  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
636  service = mpegts_add_service(ts, ts->service_id,
637  provider_name, service_name);
638 
639  if (!service)
640  return AVERROR(ENOMEM);
641 
643  service->pmt.opaque = s;
644  service->pmt.cc = 15;
645 
646  ts->pat.pid = PAT_PID;
647  /* Initialize at 15 so that it wraps and is equal to 0 for the
648  * first packet we write. */
649  ts->pat.cc = 15;
651  ts->pat.opaque = s;
652 
653  ts->sdt.pid = SDT_PID;
654  ts->sdt.cc = 15;
656  ts->sdt.opaque = s;
657 
658  pids = av_malloc_array(s->nb_streams, sizeof(*pids));
659  if (!pids) {
660  ret = AVERROR(ENOMEM);
661  goto fail;
662  }
663 
664  /* assign pids to each stream */
665  for (i = 0; i < s->nb_streams; i++) {
666  st = s->streams[i];
667 
668  ts_st = av_mallocz(sizeof(MpegTSWriteStream));
669  if (!ts_st) {
670  ret = AVERROR(ENOMEM);
671  goto fail;
672  }
673  st->priv_data = ts_st;
674 
675  ts_st->user_tb = st->time_base;
676  avpriv_set_pts_info(st, 33, 1, 90000);
677 
678  ts_st->payload = av_mallocz(ts->pes_payload_size);
679  if (!ts_st->payload) {
680  ret = AVERROR(ENOMEM);
681  goto fail;
682  }
683  ts_st->service = service;
684  /* MPEG pid values < 16 are reserved. Applications which set st->id in
685  * this range are assigned a calculated pid. */
686  if (st->id < 16) {
687  ts_st->pid = ts->start_pid + i;
688  } else if (st->id < 0x1FFF) {
689  ts_st->pid = st->id;
690  } else {
691  av_log(s, AV_LOG_ERROR,
692  "Invalid stream id %d, must be less than 8191\n", st->id);
693  ret = AVERROR(EINVAL);
694  goto fail;
695  }
696  if (ts_st->pid == service->pmt.pid) {
697  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
698  ret = AVERROR(EINVAL);
699  goto fail;
700  }
701  for (j = 0; j < i; j++) {
702  if (pids[j] == ts_st->pid) {
703  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
704  ret = AVERROR(EINVAL);
705  goto fail;
706  }
707  }
708  pids[i] = ts_st->pid;
709  ts_st->payload_pts = AV_NOPTS_VALUE;
710  ts_st->payload_dts = AV_NOPTS_VALUE;
711  ts_st->first_pts_check = 1;
712  ts_st->cc = 15;
713  /* update PCR pid by using the first video stream */
714  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
715  service->pcr_pid == 0x1fff) {
716  service->pcr_pid = ts_st->pid;
717  pcr_st = st;
718  }
719  if (st->codec->codec_id == AV_CODEC_ID_AAC &&
720  st->codec->extradata_size > 0) {
721  AVStream *ast;
722  ts_st->amux = avformat_alloc_context();
723  if (!ts_st->amux) {
724  ret = AVERROR(ENOMEM);
725  goto fail;
726  }
727  ts_st->amux->oformat =
728  av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
729  NULL, NULL);
730  if (!ts_st->amux->oformat) {
731  ret = AVERROR(EINVAL);
732  goto fail;
733  }
734  if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
735  ret = AVERROR(ENOMEM);
736  goto fail;
737  }
738  ret = avcodec_copy_context(ast->codec, st->codec);
739  if (ret != 0)
740  goto fail;
741  ast->time_base = st->time_base;
742  ret = avformat_write_header(ts_st->amux, NULL);
743  if (ret < 0)
744  goto fail;
745  }
746  }
747 
748  av_freep(&pids);
749 
750  /* if no video stream, use the first stream as PCR */
751  if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
752  pcr_st = s->streams[0];
753  ts_st = pcr_st->priv_data;
754  service->pcr_pid = ts_st->pid;
755  } else
756  ts_st = pcr_st->priv_data;
757 
758  if (ts->mux_rate > 1) {
759  service->pcr_packet_period = (ts->mux_rate * ts->pcr_period) /
760  (TS_PACKET_SIZE * 8 * 1000);
762  (TS_PACKET_SIZE * 8 * 1000);
764  (TS_PACKET_SIZE * 8 * 1000);
765 
766  if (ts->copyts < 1)
768  } else {
769  /* Arbitrary values, PAT/PMT will also be written on video key frames */
770  ts->sdt_packet_period = 200;
771  ts->pat_packet_period = 40;
772  if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
773  if (!pcr_st->codec->frame_size) {
774  av_log(s, AV_LOG_WARNING, "frame size not set\n");
775  service->pcr_packet_period =
776  pcr_st->codec->sample_rate / (10 * 512);
777  } else {
778  service->pcr_packet_period =
779  pcr_st->codec->sample_rate / (10 * pcr_st->codec->frame_size);
780  }
781  } else {
782  // max delta PCR 0.1s
783  // TODO: should be avg_frame_rate
784  service->pcr_packet_period =
785  ts_st->user_tb.den / (10 * ts_st->user_tb.num);
786  }
787  if (!service->pcr_packet_period)
788  service->pcr_packet_period = 1;
789  }
790 
793  // The user specified a period, use only it
794  if (ts->pat_period < INT_MAX/2) {
795  ts->pat_packet_period = INT_MAX;
796  }
797  if (ts->sdt_period < INT_MAX/2) {
798  ts->sdt_packet_period = INT_MAX;
799  }
800 
801  // output a PCR as soon as possible
802  service->pcr_packet_count = service->pcr_packet_period;
803  ts->pat_packet_count = ts->pat_packet_period - 1;
804  ts->sdt_packet_count = ts->sdt_packet_period - 1;
805 
806  if (ts->mux_rate == 1)
807  av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
808  else
809  av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
811  "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
812  service->pcr_packet_period,
814 
815  if (ts->m2ts_mode == -1) {
816  if (av_match_ext(s->filename, "m2ts")) {
817  ts->m2ts_mode = 1;
818  } else {
819  ts->m2ts_mode = 0;
820  }
821  }
822 
823  return 0;
824 
825 fail:
826  av_freep(&pids);
827  for (i = 0; i < s->nb_streams; i++) {
828  st = s->streams[i];
829  ts_st = st->priv_data;
830  if (ts_st) {
831  av_freep(&ts_st->payload);
832  if (ts_st->amux) {
833  avformat_free_context(ts_st->amux);
834  ts_st->amux = NULL;
835  }
836  }
837  av_freep(&st->priv_data);
838  }
839 
840  for (i = 0; i < ts->nb_services; i++) {
841  service = ts->services[i];
842  av_freep(&service->provider_name);
843  av_freep(&service->name);
844  av_freep(&service);
845  }
846  av_freep(&ts->services);
847  return ret;
848 }
849 
850 /* send SDT, PAT and PMT tables regulary */
851 static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
852 {
853  MpegTSWrite *ts = s->priv_data;
854  int i;
855 
856  if (++ts->sdt_packet_count == ts->sdt_packet_period ||
857  (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
858  (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
859  ) {
860  ts->sdt_packet_count = 0;
861  if (dts != AV_NOPTS_VALUE)
862  ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
863  mpegts_write_sdt(s);
864  }
865  if (++ts->pat_packet_count == ts->pat_packet_period ||
866  (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
867  (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
868  force_pat) {
869  ts->pat_packet_count = 0;
870  if (dts != AV_NOPTS_VALUE)
871  ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
872  mpegts_write_pat(s);
873  for (i = 0; i < ts->nb_services; i++)
874  mpegts_write_pmt(s, ts->services[i]);
875  }
876 }
877 
878 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
879 {
880  int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
881 
882  *buf++ = pcr_high >> 25;
883  *buf++ = pcr_high >> 17;
884  *buf++ = pcr_high >> 9;
885  *buf++ = pcr_high >> 1;
886  *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
887  *buf++ = pcr_low;
888 
889  return 6;
890 }
891 
892 /* Write a single null transport stream packet */
894 {
895  uint8_t *q;
897 
898  q = buf;
899  *q++ = 0x47;
900  *q++ = 0x00 | 0x1f;
901  *q++ = 0xff;
902  *q++ = 0x10;
903  memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
905  avio_write(s->pb, buf, TS_PACKET_SIZE);
906 }
907 
908 /* Write a single transport stream packet with a PCR and no payload */
910 {
911  MpegTSWrite *ts = s->priv_data;
912  MpegTSWriteStream *ts_st = st->priv_data;
913  uint8_t *q;
915 
916  q = buf;
917  *q++ = 0x47;
918  *q++ = ts_st->pid >> 8;
919  *q++ = ts_st->pid;
920  *q++ = 0x20 | ts_st->cc; /* Adaptation only */
921  /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
922  *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
923  *q++ = 0x10; /* Adaptation flags: PCR present */
924 
925  /* PCR coded into 6 bytes */
926  q += write_pcr_bits(q, get_pcr(ts, s->pb));
927 
928  /* stuffing bytes */
929  memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
931  avio_write(s->pb, buf, TS_PACKET_SIZE);
932 }
933 
934 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
935 {
936  int val;
937 
938  val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
939  *q++ = val;
940  val = (((pts >> 15) & 0x7fff) << 1) | 1;
941  *q++ = val >> 8;
942  *q++ = val;
943  val = (((pts) & 0x7fff) << 1) | 1;
944  *q++ = val >> 8;
945  *q++ = val;
946 }
947 
948 /* Set an adaptation field flag in an MPEG-TS packet*/
949 static void set_af_flag(uint8_t *pkt, int flag)
950 {
951  // expect at least one flag to set
952  av_assert0(flag);
953 
954  if ((pkt[3] & 0x20) == 0) {
955  // no AF yet, set adaptation field flag
956  pkt[3] |= 0x20;
957  // 1 byte length, no flags
958  pkt[4] = 1;
959  pkt[5] = 0;
960  }
961  pkt[5] |= flag;
962 }
963 
964 /* Extend the adaptation field by size bytes */
965 static void extend_af(uint8_t *pkt, int size)
966 {
967  // expect already existing adaptation field
968  av_assert0(pkt[3] & 0x20);
969  pkt[4] += size;
970 }
971 
972 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
974 {
975  if (pkt[3] & 0x20)
976  return pkt + 5 + pkt[4];
977  else
978  return pkt + 4;
979 }
980 
981 /* Add a PES header to the front of the payload, and segment into an integer
982  * number of TS packets. The final TS packet is padded using an oversized
983  * adaptation header to exactly fill the last TS packet.
984  * NOTE: 'payload' contains a complete PES payload. */
986  const uint8_t *payload, int payload_size,
987  int64_t pts, int64_t dts, int key)
988 {
989  MpegTSWriteStream *ts_st = st->priv_data;
990  MpegTSWrite *ts = s->priv_data;
992  uint8_t *q;
993  int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
994  int afc_len, stuffing_len;
995  int64_t pcr = -1; /* avoid warning */
996  int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
997  int force_pat = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
998 
999  av_assert0(ts_st->payload != buf || st->codec->codec_type != AVMEDIA_TYPE_VIDEO);
1001  force_pat = 1;
1002  }
1003 
1004  is_start = 1;
1005  while (payload_size > 0) {
1006  retransmit_si_info(s, force_pat, dts);
1007  force_pat = 0;
1008 
1009  write_pcr = 0;
1010  if (ts_st->pid == ts_st->service->pcr_pid) {
1011  if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
1012  ts_st->service->pcr_packet_count++;
1013  if (ts_st->service->pcr_packet_count >=
1014  ts_st->service->pcr_packet_period) {
1015  ts_st->service->pcr_packet_count = 0;
1016  write_pcr = 1;
1017  }
1018  }
1019 
1020  if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
1021  (dts - get_pcr(ts, s->pb) / 300) > delay) {
1022  /* pcr insert gets priority over null packet insert */
1023  if (write_pcr)
1024  mpegts_insert_pcr_only(s, st);
1025  else
1027  /* recalculate write_pcr and possibly retransmit si_info */
1028  continue;
1029  }
1030 
1031  /* prepare packet header */
1032  q = buf;
1033  *q++ = 0x47;
1034  val = ts_st->pid >> 8;
1035  if (is_start)
1036  val |= 0x40;
1037  *q++ = val;
1038  *q++ = ts_st->pid;
1039  ts_st->cc = ts_st->cc + 1 & 0xf;
1040  *q++ = 0x10 | ts_st->cc; // payload indicator + CC
1041  if (key && is_start && pts != AV_NOPTS_VALUE) {
1042  // set Random Access for key frames
1043  if (ts_st->pid == ts_st->service->pcr_pid)
1044  write_pcr = 1;
1045  set_af_flag(buf, 0x40);
1046  q = get_ts_payload_start(buf);
1047  }
1048  if (write_pcr) {
1049  set_af_flag(buf, 0x10);
1050  q = get_ts_payload_start(buf);
1051  // add 11, pcr references the last byte of program clock reference base
1052  if (ts->mux_rate > 1)
1053  pcr = get_pcr(ts, s->pb);
1054  else
1055  pcr = (dts - delay) * 300;
1056  if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
1057  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
1058  extend_af(buf, write_pcr_bits(q, pcr));
1059  q = get_ts_payload_start(buf);
1060  }
1061  if (is_start) {
1062  int pes_extension = 0;
1063  int pes_header_stuffing_bytes = 0;
1064  /* write PES header */
1065  *q++ = 0x00;
1066  *q++ = 0x00;
1067  *q++ = 0x01;
1068  is_dvb_subtitle = 0;
1069  is_dvb_teletext = 0;
1070  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1071  if (st->codec->codec_id == AV_CODEC_ID_DIRAC)
1072  *q++ = 0xfd;
1073  else
1074  *q++ = 0xe0;
1075  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1076  (st->codec->codec_id == AV_CODEC_ID_MP2 ||
1077  st->codec->codec_id == AV_CODEC_ID_MP3 ||
1078  st->codec->codec_id == AV_CODEC_ID_AAC)) {
1079  *q++ = 0xc0;
1080  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1081  st->codec->codec_id == AV_CODEC_ID_AC3 &&
1082  ts->m2ts_mode) {
1083  *q++ = 0xfd;
1084  } else {
1085  *q++ = 0xbd;
1086  if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1087  if (st->codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
1088  is_dvb_subtitle = 1;
1089  } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
1090  is_dvb_teletext = 1;
1091  }
1092  }
1093  }
1094  header_len = 0;
1095  flags = 0;
1096  if (pts != AV_NOPTS_VALUE) {
1097  header_len += 5;
1098  flags |= 0x80;
1099  }
1100  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1101  header_len += 5;
1102  flags |= 0x40;
1103  }
1104  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
1105  st->codec->codec_id == AV_CODEC_ID_DIRAC) {
1106  /* set PES_extension_flag */
1107  pes_extension = 1;
1108  flags |= 0x01;
1109 
1110  /* One byte for PES2 extension flag +
1111  * one byte for extension length +
1112  * one byte for extension id */
1113  header_len += 3;
1114  }
1115  /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
1116  * otherwise it will not play sound on blu-ray
1117  */
1118  if (ts->m2ts_mode &&
1120  st->codec->codec_id == AV_CODEC_ID_AC3) {
1121  /* set PES_extension_flag */
1122  pes_extension = 1;
1123  flags |= 0x01;
1124  header_len += 3;
1125  }
1126  if (is_dvb_teletext) {
1127  pes_header_stuffing_bytes = 0x24 - header_len;
1128  header_len = 0x24;
1129  }
1130  len = payload_size + header_len + 3;
1131  /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
1132  if (is_dvb_subtitle) {
1133  len += 3;
1134  payload_size++;
1135  }
1136  if (len > 0xffff)
1137  len = 0;
1139  len = 0;
1140  }
1141  *q++ = len >> 8;
1142  *q++ = len;
1143  val = 0x80;
1144  /* data alignment indicator is required for subtitle and data streams */
1146  val |= 0x04;
1147  *q++ = val;
1148  *q++ = flags;
1149  *q++ = header_len;
1150  if (pts != AV_NOPTS_VALUE) {
1151  write_pts(q, flags >> 6, pts);
1152  q += 5;
1153  }
1154  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1155  write_pts(q, 1, dts);
1156  q += 5;
1157  }
1158  if (pes_extension && st->codec->codec_id == AV_CODEC_ID_DIRAC) {
1159  flags = 0x01; /* set PES_extension_flag_2 */
1160  *q++ = flags;
1161  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1162  /* Set the stream ID extension flag bit to 0 and
1163  * write the extended stream ID. */
1164  *q++ = 0x00 | 0x60;
1165  }
1166  /* For Blu-ray AC3 Audio Setting extended flags */
1167  if (ts->m2ts_mode &&
1168  pes_extension &&
1169  st->codec->codec_id == AV_CODEC_ID_AC3) {
1170  flags = 0x01; /* set PES_extension_flag_2 */
1171  *q++ = flags;
1172  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1173  *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1174  }
1175 
1176 
1177  if (is_dvb_subtitle) {
1178  /* First two fields of DVB subtitles PES data:
1179  * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
1180  * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
1181  *q++ = 0x20;
1182  *q++ = 0x00;
1183  }
1184  if (is_dvb_teletext) {
1185  memset(q, 0xff, pes_header_stuffing_bytes);
1186  q += pes_header_stuffing_bytes;
1187  }
1188  is_start = 0;
1189  }
1190  /* header size */
1191  header_len = q - buf;
1192  /* data len */
1193  len = TS_PACKET_SIZE - header_len;
1194  if (len > payload_size)
1195  len = payload_size;
1196  stuffing_len = TS_PACKET_SIZE - header_len - len;
1197  if (stuffing_len > 0) {
1198  /* add stuffing with AFC */
1199  if (buf[3] & 0x20) {
1200  /* stuffing already present: increase its size */
1201  afc_len = buf[4] + 1;
1202  memmove(buf + 4 + afc_len + stuffing_len,
1203  buf + 4 + afc_len,
1204  header_len - (4 + afc_len));
1205  buf[4] += stuffing_len;
1206  memset(buf + 4 + afc_len, 0xff, stuffing_len);
1207  } else {
1208  /* add stuffing */
1209  memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1210  buf[3] |= 0x20;
1211  buf[4] = stuffing_len - 1;
1212  if (stuffing_len >= 2) {
1213  buf[5] = 0x00;
1214  memset(buf + 6, 0xff, stuffing_len - 2);
1215  }
1216  }
1217  }
1218 
1219  if (is_dvb_subtitle && payload_size == len) {
1220  memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
1221  buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
1222  } else {
1223  memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1224  }
1225 
1226  payload += len;
1227  payload_size -= len;
1229  avio_write(s->pb, buf, TS_PACKET_SIZE);
1230  }
1231  ts_st->prev_payload_key = key;
1232 }
1233 
1235 {
1236  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1237  if (!st->nb_frames) {
1238  av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1239  "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1240  "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1241  return AVERROR_INVALIDDATA;
1242  }
1243  av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing, size %d", pkt->size);
1244  if (pkt->size) av_log(s, AV_LOG_WARNING, " data %08X", AV_RB32(pkt->data));
1245  av_log(s, AV_LOG_WARNING, "\n");
1246  }
1247  return 0;
1248 }
1249 
1251 {
1252  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1253  if (!st->nb_frames) {
1254  av_log(s, AV_LOG_ERROR, "HEVC bitstream malformed, no startcode found\n");
1255  return AVERROR_PATCHWELCOME;
1256  }
1257  av_log(s, AV_LOG_WARNING, "HEVC bitstream error, startcode missing, size %d", pkt->size);
1258  if (pkt->size) av_log(s, AV_LOG_WARNING, " data %08X", AV_RB32(pkt->data));
1259  av_log(s, AV_LOG_WARNING, "\n");
1260  }
1261  return 0;
1262 }
1263 
1265 {
1266  AVStream *st = s->streams[pkt->stream_index];
1267  int size = pkt->size;
1268  uint8_t *buf = pkt->data;
1269  uint8_t *data = NULL;
1270  MpegTSWrite *ts = s->priv_data;
1271  MpegTSWriteStream *ts_st = st->priv_data;
1272  const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1273  int64_t dts = pkt->dts, pts = pkt->pts;
1274 
1275  if (ts->reemit_pat_pmt) {
1277  "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1278  ts->reemit_pat_pmt = 0;
1280  }
1281 
1282  if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1283  ts->pat_packet_count = ts->pat_packet_period - 1;
1284  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1286  }
1287 
1288  if (ts->copyts < 1) {
1289  if (pts != AV_NOPTS_VALUE)
1290  pts += delay;
1291  if (dts != AV_NOPTS_VALUE)
1292  dts += delay;
1293  }
1294 
1295  if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1296  av_log(s, AV_LOG_ERROR, "first pts value must be set\n");
1297  return AVERROR_INVALIDDATA;
1298  }
1299  ts_st->first_pts_check = 0;
1300 
1301  if (st->codec->codec_id == AV_CODEC_ID_H264) {
1302  const uint8_t *p = buf, *buf_end = p + size;
1303  uint32_t state = -1;
1304  int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codec->extradata_size : 0;
1305  int ret = ff_check_h264_startcode(s, st, pkt);
1306  if (ret < 0)
1307  return ret;
1308 
1309  if (extradd && AV_RB24(st->codec->extradata) > 1)
1310  extradd = 0;
1311 
1312  do {
1313  p = avpriv_find_start_code(p, buf_end, &state);
1314  av_log(s, AV_LOG_TRACE, "nal %d\n", state & 0x1f);
1315  if ((state & 0x1f) == 7)
1316  extradd = 0;
1317  } while (p < buf_end && (state & 0x1f) != 9 &&
1318  (state & 0x1f) != 5 && (state & 0x1f) != 1);
1319 
1320  if ((state & 0x1f) != 5)
1321  extradd = 0;
1322  if ((state & 0x1f) != 9) { // AUD NAL
1323  data = av_malloc(pkt->size + 6 + extradd);
1324  if (!data)
1325  return AVERROR(ENOMEM);
1326  memcpy(data + 6, st->codec->extradata, extradd);
1327  memcpy(data + 6 + extradd, pkt->data, pkt->size);
1328  AV_WB32(data, 0x00000001);
1329  data[4] = 0x09;
1330  data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1331  buf = data;
1332  size = pkt->size + 6 + extradd;
1333  }
1334  } else if (st->codec->codec_id == AV_CODEC_ID_AAC) {
1335  if (pkt->size < 2) {
1336  av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1337  return AVERROR_INVALIDDATA;
1338  }
1339  if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1340  int ret;
1341  AVPacket pkt2;
1342 
1343  if (!ts_st->amux) {
1344  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1345  "and extradata missing\n");
1346  if (!st->nb_frames)
1347  return AVERROR_INVALIDDATA;
1348  } else {
1349  av_init_packet(&pkt2);
1350  pkt2.data = pkt->data;
1351  pkt2.size = pkt->size;
1352  av_assert0(pkt->dts != AV_NOPTS_VALUE);
1353  pkt2.dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
1354 
1355  ret = avio_open_dyn_buf(&ts_st->amux->pb);
1356  if (ret < 0)
1357  return AVERROR(ENOMEM);
1358 
1359  ret = av_write_frame(ts_st->amux, &pkt2);
1360  if (ret < 0) {
1361  ffio_free_dyn_buf(&ts_st->amux->pb);
1362  return ret;
1363  }
1364  size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1365  ts_st->amux->pb = NULL;
1366  buf = data;
1367  }
1368  }
1369  } else if (st->codec->codec_id == AV_CODEC_ID_HEVC) {
1370  int ret = check_hevc_startcode(s, st, pkt);
1371  if (ret < 0)
1372  return ret;
1373  }
1374 
1375  if (pkt->dts != AV_NOPTS_VALUE) {
1376  int i;
1377  for(i=0; i<s->nb_streams; i++) {
1378  AVStream *st2 = s->streams[i];
1379  MpegTSWriteStream *ts_st2 = st2->priv_data;
1380  if ( ts_st2->payload_size
1381  && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
1382  mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
1383  ts_st2->payload_pts, ts_st2->payload_dts,
1384  ts_st2->payload_flags & AV_PKT_FLAG_KEY);
1385  ts_st2->payload_size = 0;
1386  }
1387  }
1388  }
1389 
1390  if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
1391  (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
1392  av_compare_ts(dts - ts_st->payload_dts, st->time_base,
1393  s->max_delay, AV_TIME_BASE_Q) >= 0))) {
1394  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1395  ts_st->payload_pts, ts_st->payload_dts,
1396  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1397  ts_st->payload_size = 0;
1398  }
1399 
1400  if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
1401  av_assert0(!ts_st->payload_size);
1402  // for video and subtitle, write a single pes packet
1403  mpegts_write_pes(s, st, buf, size, pts, dts,
1404  pkt->flags & AV_PKT_FLAG_KEY);
1405  av_free(data);
1406  return 0;
1407  }
1408 
1409  if (!ts_st->payload_size) {
1410  ts_st->payload_pts = pts;
1411  ts_st->payload_dts = dts;
1412  ts_st->payload_flags = pkt->flags;
1413  }
1414 
1415  memcpy(ts_st->payload + ts_st->payload_size, buf, size);
1416  ts_st->payload_size += size;
1417 
1418  av_free(data);
1419 
1420  return 0;
1421 }
1422 
1424 {
1425  int i;
1426 
1427  /* flush current packets */
1428  for (i = 0; i < s->nb_streams; i++) {
1429  AVStream *st = s->streams[i];
1430  MpegTSWriteStream *ts_st = st->priv_data;
1431  if (ts_st->payload_size > 0) {
1432  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1433  ts_st->payload_pts, ts_st->payload_dts,
1434  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1435  ts_st->payload_size = 0;
1436  }
1437  }
1438 }
1439 
1441 {
1442  if (!pkt) {
1443  mpegts_write_flush(s);
1444  return 1;
1445  } else {
1446  return mpegts_write_packet_internal(s, pkt);
1447  }
1448 }
1449 
1451 {
1452  MpegTSWrite *ts = s->priv_data;
1453  MpegTSService *service;
1454  int i;
1455 
1456  if (s->pb)
1457  mpegts_write_flush(s);
1458 
1459  for (i = 0; i < s->nb_streams; i++) {
1460  AVStream *st = s->streams[i];
1461  MpegTSWriteStream *ts_st = st->priv_data;
1462  av_freep(&ts_st->payload);
1463  if (ts_st->amux) {
1464  avformat_free_context(ts_st->amux);
1465  ts_st->amux = NULL;
1466  }
1467  }
1468 
1469  for (i = 0; i < ts->nb_services; i++) {
1470  service = ts->services[i];
1471  av_freep(&service->provider_name);
1472  av_freep(&service->name);
1473  av_freep(&service);
1474  }
1475  av_freep(&ts->services);
1476 
1477  return 0;
1478 }
1479 
1480 static const AVOption options[] = {
1481  { "mpegts_transport_stream_id", "Set transport_stream_id field.",
1482  offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
1483  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1484  { "mpegts_original_network_id", "Set original_network_id field.",
1485  offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
1486  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1487  { "mpegts_service_id", "Set service_id field.",
1488  offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
1489  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1490  { "mpegts_service_type", "Set service_type field.",
1491  offsetof(MpegTSWrite, service_type), AV_OPT_TYPE_INT,
1492  { .i64 = 0x01 }, 0x01, 0xff, AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1493  { "digital_tv", "Digital Television.",
1494  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff,
1495  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1496  { "digital_radio", "Digital Radio.",
1497  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff,
1498  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1499  { "teletext", "Teletext.",
1500  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff,
1501  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1502  { "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
1504  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1505  { "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
1506  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff,
1507  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1508  { "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
1510  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1511  { "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
1513  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1514  { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
1515  offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
1516  { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
1517  { "mpegts_start_pid", "Set the first pid.",
1518  offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
1519  { .i64 = 0x0100 }, 0x0020, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
1520  { "mpegts_m2ts_mode", "Enable m2ts mode.",
1521  offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_INT,
1522  { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1523  { "muxrate", NULL,
1524  offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
1525  { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1526  { "pes_payload_size", "Minimum PES packet payload in bytes",
1527  offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT,
1528  { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1529  { "mpegts_flags", "MPEG-TS muxing flags",
1530  offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX,
1531  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1532  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1533  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX,
1534  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1535  { "latm", "Use LATM packetization for AAC",
1536  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX,
1537  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1538  { "pat_pmt_at_frames", "Reemit PAT and PMT at each video frame",
1539  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_PAT_PMT_AT_FRAMES}, 0, INT_MAX,
1540  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1541  // backward compatibility
1542  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1543  offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT,
1544  { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1545  { "mpegts_copyts", "don't offset dts/pts",
1546  offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_INT,
1547  { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1548  { "tables_version", "set PAT, PMT and SDT version",
1549  offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT,
1550  { .i64 = 0 }, 0, 31, AV_OPT_FLAG_ENCODING_PARAM },
1551  { "omit_video_pes_length", "Omit the PES packet length for video packets",
1552  offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_INT,
1553  { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
1554  { "pcr_period", "PCR retransmission time",
1555  offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
1556  { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1557  { "pat_period", "PAT/PMT retransmission time limit in seconds",
1558  offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_FLOAT,
1559  { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1560  { "sdt_period", "SDT retransmission time limit in seconds",
1561  offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_FLOAT,
1562  { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1563  { NULL },
1564 };
1565 
1566 static const AVClass mpegts_muxer_class = {
1567  .class_name = "MPEGTS muxer",
1568  .item_name = av_default_item_name,
1569  .option = options,
1570  .version = LIBAVUTIL_VERSION_INT,
1571 };
1572 
1574  .name = "mpegts",
1575  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
1576  .mime_type = "video/MP2T",
1577  .extensions = "ts,m2t,m2ts,mts",
1578  .priv_data_size = sizeof(MpegTSWrite),
1579  .audio_codec = AV_CODEC_ID_MP2,
1580  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1585  .priv_class = &mpegts_muxer_class,
1586 };
#define NULL
Definition: coverity.c:32
#define SDT_PID
Definition: mpegts.h:37
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define PMT_TID
Definition: mpegts.h:41
#define SECTION_LENGTH
Definition: mpegtsenc.c:119
int pat_packet_period
Definition: mpegtsenc.c:79
MpegTSSection pmt
Definition: mpegtsenc.c:52
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1146
AVOption.
Definition: opt.h:255
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int pcr_packet_count
Definition: mpegtsenc.c:57
int sdt_packet_count
Definition: mpegtsenc.c:76
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:447
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:691
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
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:4083
#define STREAM_TYPE_VIDEO_CAVS
Definition: mpeg.h:58
int64_t payload_dts
Definition: mpegtsenc.c:225
char * name
Definition: mpegtsenc.c:54
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1424
const char * b
Definition: vf_curves.c:109
int64_t last_sdt_ts
Definition: mpegtsenc.c:108
#define PCR_TIME_BASE
Definition: mpegtsenc.c:37
float pat_period
Definition: mpegtsenc.c:105
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:810
void * priv_data
Definition: avformat.h:862
#define AV_DISPOSITION_CLEAN_EFFECTS
stream without voice
Definition: avformat.h:812
int version
Definition: avisynth_c.h:629
#define PCR_RETRANS_TIME
Definition: mpegtsenc.c:215
static AVPacket pkt
int64_t first_pcr
Definition: mpegtsenc.c:83
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:481
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1134
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:182
Format I/O context.
Definition: avformat.h:1273
char * provider_name
Definition: mpegtsenc.c:55
int first_pts_check
first pts check needed
Definition: mpegtsenc.c:222
#define SDT_RETRANS_TIME
Definition: mpegtsenc.c:213
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
Definition: mpegtsenc.c:249
int64_t payload_pts
Definition: mpegtsenc.c:224
uint8_t
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:195
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
void * opaque
Definition: mpegtsenc.c:48
#define DEFAULT_PES_PAYLOAD_SIZE
Definition: mpegtsenc.c:115
int id
Format-specific stream ID.
Definition: avformat.h:849
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:310
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1617
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
#define TS_PACKET_SIZE
Definition: mpegts.h:29
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:111
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1440
int omit_video_pes_length
Definition: mpegtsenc.c:110
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:39
static void mpegts_insert_null_packet(AVFormatContext *s)
Definition: mpegtsenc.c:893
uint8_t * data
Definition: avcodec.h:1423
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: format.c:81
static void mpegts_write_sdt(AVFormatContext *s)
Definition: mpegtsenc.c:518
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:178
static void extend_af(uint8_t *pkt, int size)
Definition: mpegtsenc.c:965
#define MPEGTS_FLAG_AAC_LATM
Definition: mpegtsenc.c:100
#define av_log(a,...)
#define PAT_TID
Definition: mpegts.h:40
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:285
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
int flag
Definition: checkasm.c:76
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1292
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
#define STREAM_TYPE_AUDIO_AAC
Definition: mpeg.h:55
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
struct MpegTSService * service
Definition: mpegtsenc.c:218
#define STREAM_TYPE_AUDIO_DTS
Definition: mpegts.h:61
static void mpegts_prefix_m2ts_header(AVFormatContext *s)
Definition: mpegtsenc.c:589
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1485
static void put16(uint8_t **q_ptr, int val)
Definition: mpegtsenc.c:170
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:179
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
Definition: mpegtsenc.c:934
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
int pes_payload_size
Definition: mpegtsenc.c:85
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
static uint8_t * get_ts_payload_start(uint8_t *pkt)
Definition: mpegtsenc.c:973
#define STREAM_TYPE_VIDEO_DIRAC
Definition: mpegts.h:58
static void set_af_flag(uint8_t *pkt, int flag)
Definition: mpegtsenc.c:949
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:425
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
Definition: mpegtsenc.c:909
simple assert() macros that are a bit more flexible than ISO C assert().
int nb_services
Definition: mpegtsenc.c:80
AVRational user_tb
Definition: mpegtsenc.c:229
#define PAT_RETRANS_TIME
Definition: mpegtsenc.c:214
int mux_rate
set to 1 when VBR
Definition: mpegtsenc.c:84
static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1264
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
Definition: mpegtsenc.c:878
#define FFMAX(a, b)
Definition: common.h:79
#define fail()
Definition: checkasm.h:57
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1429
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:145
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1329
#define STREAM_TYPE_AUDIO_AAC_LATM
Definition: mpegts.h:52
int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Check presence of H264 startcode.
Definition: mpegtsenc.c:1234
static const AVOption options[]
Definition: mpegtsenc.c:1480
char filename[1024]
input or output filename
Definition: avformat.h:1349
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:127
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
static void putstr8(uint8_t **q_ptr, const char *str)
Definition: mpegtsenc.c:502
#define STREAM_TYPE_VIDEO_H264
Definition: mpeg.h:57
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:513
#define DEFAULT_PROVIDER_NAME
Definition: mpegtsenc.c:209
static struct @197 state
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:356
static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Definition: mpegtsenc.c:1250
AVOutputFormat ff_mpegts_muxer
Definition: mpegtsenc.c:1573
AVFormatContext * amux
Definition: mpegtsenc.c:228
AVDictionary * metadata
Definition: avformat.h:916
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:94
static int mpegts_write_header(AVFormatContext *s)
Definition: mpegtsenc.c:608
#define MPEGTS_FLAG_REEMIT_PAT_PMT
Definition: mpegtsenc.c:99
static void mpegts_write_pes(AVFormatContext *s, AVStream *st, const uint8_t *payload, int payload_size, int64_t pts, int64_t dts, int key)
Definition: mpegtsenc.c:985
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
#define STREAM_TYPE_VIDEO_MPEG4
Definition: mpeg.h:56
int pmt_start_pid
Definition: mpegtsenc.c:92
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1174
int service_type
Definition: mpegtsenc.c:90
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:811
Stream structure.
Definition: avformat.h:842
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int start_pid
Definition: mpegtsenc.c:93
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2282
#define av_bswap32
Definition: bswap.h:33
enum AVMediaType codec_type
Definition: avcodec.h:1510
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
enum AVCodecID codec_id
Definition: avcodec.h:1519
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
int sample_rate
samples per second
Definition: avcodec.h:2262
int sdt_packet_period
Definition: mpegtsenc.c:77
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES
Definition: mpegtsenc.c:101
const AVClass * av_class
Definition: mpegtsenc.c:72
#define STREAM_TYPE_PRIVATE_DATA
Definition: mpeg.h:54
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1618
Describe the class of an AVClass context structure.
Definition: log.h:67
int original_network_id
Definition: mpegtsenc.c:88
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
rational number numerator/denominator
Definition: rational.h:43
int pcr_packet_period
Definition: mpegtsenc.c:58
int service_id
Definition: mpegtsenc.c:89
byte swapping routines
static int mpegts_write_end(AVFormatContext *s)
Definition: mpegtsenc.c:1450
float sdt_period
Definition: mpegtsenc.c:106
#define STREAM_TYPE_AUDIO_AC3
Definition: mpeg.h:60
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3686
static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
Definition: mpegtsenc.c:851
int transport_stream_id
Definition: mpegtsenc.c:87
MpegTSService ** services
Definition: mpegtsenc.c:75
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:601
uint8_t * payload
Definition: mpegtsenc.c:227
static int64_t pts
Global timestamp for the audio frames.
static void mpegts_write_flush(AVFormatContext *s)
Definition: mpegtsenc.c:1423
static int flags
Definition: cpu.c:47
int m2ts_mode
Definition: mpegtsenc.c:94
#define STREAM_TYPE_VIDEO_HEVC
Definition: mpegts.h:55
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:342
#define DEFAULT_SERVICE_NAME
Definition: mpegtsenc.c:210
Main libavformat public API header.
common internal api header.
int64_t last_pat_ts
Definition: mpegtsenc.c:107
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
Definition: mpegtsenc.c:583
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:905
int pcr_period
Definition: mpegtsenc.c:98
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:903
int den
denominator
Definition: rational.h:45
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:475
#define av_free(p)
char * value
Definition: dict.h:88
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:47
int len
#define PAT_PID
Definition: mpegts.h:36
#define STREAM_TYPE_VIDEO_MPEG2
Definition: mpeg.h:50
void * priv_data
Format private data.
Definition: avformat.h:1301
MpegTSSection pat
Definition: mpegtsenc.c:73
int reemit_pat_pmt
Definition: mpegtsenc.c:96
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
static void mpegts_write_pat(AVFormatContext *s)
Definition: mpegtsenc.c:232
MpegTSSection sdt
Definition: mpegtsenc.c:74
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1422
int pat_packet_count
Definition: mpegtsenc.c:78
static const AVClass mpegts_muxer_class
Definition: mpegtsenc.c:1566
#define av_freep(p)
#define av_malloc_array(a, b)
int stream_index
Definition: avcodec.h:1425
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:884
#define STREAM_TYPE_AUDIO_MPEG1
Definition: mpeg.h:51
This structure stores compressed data.
Definition: avcodec.h:1400
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
Definition: mpegtsenc.c:122
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
#define STREAM_TYPE_AUDIO_TRUEHD
Definition: mpegts.h:62
#define SDT_TID
Definition: mpegts.h:43
int tables_version
Definition: mpegtsenc.c:104
const char * name
Definition: opengl_enc.c:103
static MpegTSService * mpegts_add_service(MpegTSWrite *ts, int sid, const char *provider_name, const char *name)
Definition: mpegtsenc.c:556