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/bswap.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mathematics.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "libavcodec/internal.h"
30 #include "avformat.h"
31 #include "internal.h"
32 #include "mpegts.h"
33 
34 #define PCR_TIME_BASE 27000000
35 
36 /* write DVB SI sections */
37 
38 /*********************************************/
39 /* mpegts section writer */
40 
41 typedef struct MpegTSSection {
42  int pid;
43  int cc;
44  void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
45  void *opaque;
47 
48 typedef struct MpegTSService {
49  MpegTSSection pmt; /* MPEG2 pmt table context */
50  int sid; /* service ID */
51  char *name;
53  int pcr_pid;
57 
58 typedef struct MpegTSWrite {
59  const AVClass *av_class;
60  MpegTSSection pat; /* MPEG2 pat table */
61  MpegTSSection sdt; /* MPEG2 sdt table context */
68  int onid;
69  int tsid;
70  int64_t first_pcr;
71  int mux_rate; ///< set to 1 when VBR
73 
77 
79  int start_pid;
80  int m2ts_mode;
81 
82  int reemit_pat_pmt; // backward compatibility
83 
85 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
86 #define MPEGTS_FLAG_AAC_LATM 0x02
87  int flags;
88  int copyts;
90 
92 } MpegTSWrite;
93 
94 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
95 #define DEFAULT_PES_HEADER_FREQ 16
96 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
97 
98 /* NOTE: 4 bytes must be left at the end for the crc32 */
100 {
101  unsigned int crc;
102  unsigned char packet[TS_PACKET_SIZE];
103  const unsigned char *buf_ptr;
104  unsigned char *q;
105  int first, b, len1, left;
106 
107  crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
108  buf[len - 4] = (crc >> 24) & 0xff;
109  buf[len - 3] = (crc >> 16) & 0xff;
110  buf[len - 2] = (crc >> 8) & 0xff;
111  buf[len - 1] = (crc) & 0xff;
112 
113  /* send each packet */
114  buf_ptr = buf;
115  while (len > 0) {
116  first = (buf == buf_ptr);
117  q = packet;
118  *q++ = 0x47;
119  b = (s->pid >> 8);
120  if (first)
121  b |= 0x40;
122  *q++ = b;
123  *q++ = s->pid;
124  s->cc = (s->cc + 1) & 0xf;
125  *q++ = 0x10 | s->cc;
126  if (first)
127  *q++ = 0; /* 0 offset */
128  len1 = TS_PACKET_SIZE - (q - packet);
129  if (len1 > len)
130  len1 = len;
131  memcpy(q, buf_ptr, len1);
132  q += len1;
133  /* add known padding data */
134  left = TS_PACKET_SIZE - (q - packet);
135  if (left > 0)
136  memset(q, 0xff, left);
137 
138  s->write_packet(s, packet);
139 
140  buf_ptr += len1;
141  len -= len1;
142  }
143 }
144 
145 static inline void put16(uint8_t **q_ptr, int val)
146 {
147  uint8_t *q;
148  q = *q_ptr;
149  *q++ = val >> 8;
150  *q++ = val;
151  *q_ptr = q;
152 }
153 
154 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
155  int version, int sec_num, int last_sec_num,
156  uint8_t *buf, int len)
157 {
158  uint8_t section[1024], *q;
159  unsigned int tot_len;
160  /* reserved_future_use field must be set to 1 for SDT */
161  unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
162 
163  tot_len = 3 + 5 + len + 4;
164  /* check if not too big */
165  if (tot_len > 1024)
166  return AVERROR_INVALIDDATA;
167 
168  q = section;
169  *q++ = tid;
170  put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
171  put16(&q, id);
172  *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
173  *q++ = sec_num;
174  *q++ = last_sec_num;
175  memcpy(q, buf, len);
176 
177  mpegts_write_section(s, section, tot_len);
178  return 0;
179 }
180 
181 /*********************************************/
182 /* mpegts writer */
183 
184 #define DEFAULT_PROVIDER_NAME "FFmpeg"
185 #define DEFAULT_SERVICE_NAME "Service01"
186 
187 /* we retransmit the SI info at this rate */
188 #define SDT_RETRANS_TIME 500
189 #define PAT_RETRANS_TIME 100
190 #define PCR_RETRANS_TIME 20
191 
192 typedef struct MpegTSWriteStream {
194  int pid; /* stream associated pid */
195  int cc;
197  int first_pts_check; ///< first pts check needed
199  int64_t payload_pts;
200  int64_t payload_dts;
206 
208 {
209  MpegTSWrite *ts = s->priv_data;
210  MpegTSService *service;
211  uint8_t data[1012], *q;
212  int i;
213 
214  q = data;
215  for(i = 0; i < ts->nb_services; i++) {
216  service = ts->services[i];
217  put16(&q, service->sid);
218  put16(&q, 0xe000 | service->pmt.pid);
219  }
220  mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, ts->tables_version, 0, 0,
221  data, q - data);
222 }
223 
225 {
226  MpegTSWrite *ts = s->priv_data;
227  uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
228  int val, stream_type, i;
229 
230  q = data;
231  put16(&q, 0xe000 | service->pcr_pid);
232 
233  program_info_length_ptr = q;
234  q += 2; /* patched after */
235 
236  /* put program info here */
237 
238  val = 0xf000 | (q - program_info_length_ptr - 2);
239  program_info_length_ptr[0] = val >> 8;
240  program_info_length_ptr[1] = val;
241 
242  for(i = 0; i < s->nb_streams; i++) {
243  AVStream *st = s->streams[i];
244  MpegTSWriteStream *ts_st = st->priv_data;
245  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
246  switch(st->codec->codec_id) {
249  stream_type = STREAM_TYPE_VIDEO_MPEG2;
250  break;
251  case AV_CODEC_ID_MPEG4:
252  stream_type = STREAM_TYPE_VIDEO_MPEG4;
253  break;
254  case AV_CODEC_ID_H264:
255  stream_type = STREAM_TYPE_VIDEO_H264;
256  break;
257  case AV_CODEC_ID_HEVC:
258  stream_type = STREAM_TYPE_VIDEO_HEVC;
259  break;
260  case AV_CODEC_ID_CAVS:
261  stream_type = STREAM_TYPE_VIDEO_CAVS;
262  break;
263  case AV_CODEC_ID_DIRAC:
264  stream_type = STREAM_TYPE_VIDEO_DIRAC;
265  break;
266  case AV_CODEC_ID_MP2:
267  case AV_CODEC_ID_MP3:
268  stream_type = STREAM_TYPE_AUDIO_MPEG1;
269  break;
270  case AV_CODEC_ID_AAC:
272  break;
274  stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
275  break;
276  case AV_CODEC_ID_AC3:
277  stream_type = STREAM_TYPE_AUDIO_AC3;
278  break;
279  case AV_CODEC_ID_DTS:
280  stream_type = STREAM_TYPE_AUDIO_DTS;
281  break;
282  case AV_CODEC_ID_TRUEHD:
283  stream_type = STREAM_TYPE_AUDIO_TRUEHD;
284  break;
285  default:
286  stream_type = STREAM_TYPE_PRIVATE_DATA;
287  break;
288  }
289 
290  if (q - data > sizeof(data) - 32)
291  return AVERROR(EINVAL);
292 
293  *q++ = stream_type;
294  put16(&q, 0xe000 | ts_st->pid);
295  desc_length_ptr = q;
296  q += 2; /* patched after */
297 
298  /* write optional descriptors here */
299  switch(st->codec->codec_type) {
300  case AVMEDIA_TYPE_AUDIO:
301  if(st->codec->codec_id==AV_CODEC_ID_EAC3){
302  *q++=0x7a; // EAC3 descriptor see A038 DVB SI
303  *q++=1; // 1 byte, all flags sets to 0
304  *q++=0; // omit all fields...
305  }
306  if(st->codec->codec_id==AV_CODEC_ID_S302M){
307  *q++ = 0x05; /* MPEG-2 registration descriptor*/
308  *q++ = 4;
309  *q++ = 'B';
310  *q++ = 'S';
311  *q++ = 'S';
312  *q++ = 'D';
313  }
314 
315  if (lang) {
316  char *p;
317  char *next = lang->value;
318  uint8_t *len_ptr;
319 
320  *q++ = 0x0a; /* ISO 639 language descriptor */
321  len_ptr = q++;
322  *len_ptr = 0;
323 
324  for (p = lang->value; next && *len_ptr < 255 / 4 * 4 && q - data < sizeof(data) - 4; p = next + 1) {
325  next = strchr(p, ',');
326  if (strlen(p) != 3 && (!next || next != p + 3))
327  continue; /* not a 3-letter code */
328 
329  *q++ = *p++;
330  *q++ = *p++;
331  *q++ = *p++;
332 
334  *q++ = 0x01;
336  *q++ = 0x02;
338  *q++ = 0x03;
339  else
340  *q++ = 0; /* undefined type */
341 
342  *len_ptr += 4;
343  }
344 
345  if (*len_ptr == 0)
346  q -= 2; /* no language codes were written */
347  }
348  break;
350  {
351  const char default_language[] = "und";
352  const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
353 
355  uint8_t *len_ptr;
356  int extradata_copied = 0;
357 
358  *q++ = 0x59; /* subtitling_descriptor */
359  len_ptr = q++;
360 
361  while (strlen(language) >= 3 && (sizeof(data) - (q - data)) >= 8) { /* 8 bytes per DVB subtitle substream data */
362  *q++ = *language++;
363  *q++ = *language++;
364  *q++ = *language++;
365  /* Skip comma */
366  if (*language != '\0')
367  language++;
368 
369  if (st->codec->extradata_size - extradata_copied >= 5) {
370  *q++ = st->codec->extradata[extradata_copied + 4]; /* subtitling_type */
371  memcpy(q, st->codec->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
372  extradata_copied += 5;
373  q += 4;
374  } else {
375  /* subtitling_type:
376  * 0x10 - normal with no monitor aspect ratio criticality
377  * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
378  *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
379  if ((st->codec->extradata_size == 4) && (extradata_copied == 0)) {
380  /* support of old 4-byte extradata format */
381  memcpy(q, st->codec->extradata, 4); /* composition_page_id and ancillary_page_id */
382  extradata_copied += 4;
383  q += 4;
384  } else {
385  put16(&q, 1); /* composition_page_id */
386  put16(&q, 1); /* ancillary_page_id */
387  }
388  }
389  }
390 
391  *len_ptr = q - len_ptr - 1;
392  } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
393  uint8_t *len_ptr = NULL;
394  int extradata_copied = 0;
395 
396  /* The descriptor tag. teletext_descriptor */
397  *q++ = 0x56;
398  len_ptr = q++;
399 
400  while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
401  *q++ = *language++;
402  *q++ = *language++;
403  *q++ = *language++;
404  /* Skip comma */
405  if (*language != '\0')
406  language++;
407 
408  if (st->codec->extradata_size - 1 > extradata_copied) {
409  memcpy(q, st->codec->extradata + extradata_copied, 2);
410  extradata_copied += 2;
411  q += 2;
412  } else {
413  /* The Teletext descriptor:
414  * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
415  * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
416  * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
417  *q++ = 0x08;
418  *q++ = 0x00;
419  }
420  }
421 
422  *len_ptr = q - len_ptr - 1;
423  }
424  }
425  break;
426  case AVMEDIA_TYPE_VIDEO:
427  if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
428  *q++ = 0x05; /*MPEG-2 registration descriptor*/
429  *q++ = 4;
430  *q++ = 'd';
431  *q++ = 'r';
432  *q++ = 'a';
433  *q++ = 'c';
434  }
435  break;
436  case AVMEDIA_TYPE_DATA:
437  if (st->codec->codec_id == AV_CODEC_ID_SMPTE_KLV) {
438  *q++ = 0x05; /* MPEG-2 registration descriptor */
439  *q++ = 4;
440  *q++ = 'K';
441  *q++ = 'L';
442  *q++ = 'V';
443  *q++ = 'A';
444  }
445  break;
446  }
447 
448  val = 0xf000 | (q - desc_length_ptr - 2);
449  desc_length_ptr[0] = val >> 8;
450  desc_length_ptr[1] = val;
451  }
452  mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
453  data, q - data);
454  return 0;
455 }
456 
457 /* NOTE: str == NULL is accepted for an empty string */
458 static void putstr8(uint8_t **q_ptr, const char *str)
459 {
460  uint8_t *q;
461  int len;
462 
463  q = *q_ptr;
464  if (!str)
465  len = 0;
466  else
467  len = strlen(str);
468  *q++ = len;
469  memcpy(q, str, len);
470  q += len;
471  *q_ptr = q;
472 }
473 
475 {
476  MpegTSWrite *ts = s->priv_data;
477  MpegTSService *service;
478  uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
479  int i, running_status, free_ca_mode, val;
480 
481  q = data;
482  put16(&q, ts->onid);
483  *q++ = 0xff;
484  for(i = 0; i < ts->nb_services; i++) {
485  service = ts->services[i];
486  put16(&q, service->sid);
487  *q++ = 0xfc | 0x00; /* currently no EIT info */
488  desc_list_len_ptr = q;
489  q += 2;
490  running_status = 4; /* running */
491  free_ca_mode = 0;
492 
493  /* write only one descriptor for the service name and provider */
494  *q++ = 0x48;
495  desc_len_ptr = q;
496  q++;
497  *q++ = 0x01; /* digital television service */
498  putstr8(&q, service->provider_name);
499  putstr8(&q, service->name);
500  desc_len_ptr[0] = q - desc_len_ptr - 1;
501 
502  /* fill descriptor length */
503  val = (running_status << 13) | (free_ca_mode << 12) |
504  (q - desc_list_len_ptr - 2);
505  desc_list_len_ptr[0] = val >> 8;
506  desc_list_len_ptr[1] = val;
507  }
508  mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, ts->tables_version, 0, 0,
509  data, q - data);
510 }
511 
513  int sid,
514  const char *provider_name,
515  const char *name)
516 {
517  MpegTSService *service;
518 
519  service = av_mallocz(sizeof(MpegTSService));
520  if (!service)
521  return NULL;
522  service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
523  service->sid = sid;
524  service->provider_name = av_strdup(provider_name);
525  service->name = av_strdup(name);
526  service->pcr_pid = 0x1fff;
527  dynarray_add(&ts->services, &ts->nb_services, service);
528  return service;
529 }
530 
531 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
532 {
533  return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
534  ts->first_pcr;
535 }
536 
538 {
539  MpegTSWrite *ts = s->priv_data;
540  if (ts->m2ts_mode) {
541  int64_t pcr = get_pcr(s->priv_data, s->pb);
542  uint32_t tp_extra_header = pcr % 0x3fffffff;
543  tp_extra_header = AV_RB32(&tp_extra_header);
544  avio_write(s->pb, (unsigned char *) &tp_extra_header,
545  sizeof(tp_extra_header));
546  }
547 }
548 
549 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
550 {
551  AVFormatContext *ctx = s->opaque;
553  avio_write(ctx->pb, packet, TS_PACKET_SIZE);
554 }
555 
557 {
558  MpegTSWrite *ts = s->priv_data;
559  MpegTSWriteStream *ts_st;
560  MpegTSService *service;
561  AVStream *st, *pcr_st = NULL;
562  AVDictionaryEntry *title, *provider;
563  int i, j;
564  const char *service_name;
565  const char *provider_name;
566  int *pids;
567  int ret;
568 
569  if (s->max_delay < 0) /* Not set by the caller */
570  s->max_delay = 0;
571 
572  // round up to a whole number of TS packets
573  ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
574 
575  ts->tsid = ts->transport_stream_id;
576  ts->onid = ts->original_network_id;
577  /* allocate a single DVB service */
578  title = av_dict_get(s->metadata, "service_name", NULL, 0);
579  if (!title)
580  title = av_dict_get(s->metadata, "title", NULL, 0);
581  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
582  provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
583  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
584  service = mpegts_add_service(ts, ts->service_id, provider_name, service_name);
586  service->pmt.opaque = s;
587  service->pmt.cc = 15;
588 
589  ts->pat.pid = PAT_PID;
590  ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
592  ts->pat.opaque = s;
593 
594  ts->sdt.pid = SDT_PID;
595  ts->sdt.cc = 15;
597  ts->sdt.opaque = s;
598 
599  pids = av_malloc_array(s->nb_streams, sizeof(*pids));
600  if (!pids)
601  return AVERROR(ENOMEM);
602 
603  /* assign pids to each stream */
604  for(i = 0;i < s->nb_streams; i++) {
605  st = s->streams[i];
606 
607  ts_st = av_mallocz(sizeof(MpegTSWriteStream));
608  if (!ts_st) {
609  ret = AVERROR(ENOMEM);
610  goto fail;
611  }
612  st->priv_data = ts_st;
613 
614  ts_st->user_tb = st->time_base;
615  avpriv_set_pts_info(st, 33, 1, 90000);
616 
617  ts_st->payload = av_mallocz(ts->pes_payload_size);
618  if (!ts_st->payload) {
619  ret = AVERROR(ENOMEM);
620  goto fail;
621  }
622  ts_st->service = service;
623  /* MPEG pid values < 16 are reserved. Applications which set st->id in
624  * this range are assigned a calculated pid. */
625  if (st->id < 16) {
626  ts_st->pid = ts->start_pid + i;
627  } else if (st->id < 0x1FFF) {
628  ts_st->pid = st->id;
629  } else {
630  av_log(s, AV_LOG_ERROR, "Invalid stream id %d, must be less than 8191\n", st->id);
631  ret = AVERROR(EINVAL);
632  goto fail;
633  }
634  if (ts_st->pid == service->pmt.pid) {
635  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
636  ret = AVERROR(EINVAL);
637  goto fail;
638  }
639  for (j = 0; j < i; j++)
640  if (pids[j] == ts_st->pid) {
641  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
642  ret = AVERROR(EINVAL);
643  goto fail;
644  }
645  pids[i] = ts_st->pid;
646  ts_st->payload_pts = AV_NOPTS_VALUE;
647  ts_st->payload_dts = AV_NOPTS_VALUE;
648  ts_st->first_pts_check = 1;
649  ts_st->cc = 15;
650  /* update PCR pid by using the first video stream */
651  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
652  service->pcr_pid == 0x1fff) {
653  service->pcr_pid = ts_st->pid;
654  pcr_st = st;
655  }
656  if (st->codec->codec_id == AV_CODEC_ID_AAC &&
657  st->codec->extradata_size > 0)
658  {
659  AVStream *ast;
660  ts_st->amux = avformat_alloc_context();
661  if (!ts_st->amux) {
662  ret = AVERROR(ENOMEM);
663  goto fail;
664  }
665  ts_st->amux->oformat = av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts", NULL, NULL);
666  if (!ts_st->amux->oformat) {
667  ret = AVERROR(EINVAL);
668  goto fail;
669  }
670  ast = avformat_new_stream(ts_st->amux, NULL);
671  if (!ast) {
672  ret = AVERROR(ENOMEM);
673  goto fail;
674  }
675  ret = avcodec_copy_context(ast->codec, st->codec);
676  if (ret != 0)
677  goto fail;
678  ret = avformat_write_header(ts_st->amux, NULL);
679  if (ret < 0)
680  goto fail;
681  }
682  }
683 
684  av_free(pids);
685 
686  /* if no video stream, use the first stream as PCR */
687  if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
688  pcr_st = s->streams[0];
689  ts_st = pcr_st->priv_data;
690  service->pcr_pid = ts_st->pid;
691  } else
692  ts_st = pcr_st->priv_data;
693 
694  if (ts->mux_rate > 1) {
695  service->pcr_packet_period = (ts->mux_rate * ts->pcr_period) /
696  (TS_PACKET_SIZE * 8 * 1000);
698  (TS_PACKET_SIZE * 8 * 1000);
700  (TS_PACKET_SIZE * 8 * 1000);
701 
702  if(ts->copyts < 1)
704  } else {
705  /* Arbitrary values, PAT/PMT will also be written on video key frames */
706  ts->sdt_packet_period = 200;
707  ts->pat_packet_period = 40;
708  if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
709  if (!pcr_st->codec->frame_size) {
710  av_log(s, AV_LOG_WARNING, "frame size not set\n");
711  service->pcr_packet_period =
712  pcr_st->codec->sample_rate/(10*512);
713  } else {
714  service->pcr_packet_period =
715  pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
716  }
717  } else {
718  // max delta PCR 0.1s
719  // TODO: should be avg_frame_rate
720  service->pcr_packet_period =
721  ts_st->user_tb.den / (10 * ts_st->user_tb.num);
722  }
723  if(!service->pcr_packet_period)
724  service->pcr_packet_period = 1;
725  }
726 
727  // output a PCR as soon as possible
728  service->pcr_packet_count = service->pcr_packet_period;
731 
732  if (ts->mux_rate == 1)
733  av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
734  else
735  av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
736  av_log(s, AV_LOG_VERBOSE, "pcr every %d pkts, "
737  "sdt every %d, pat/pmt every %d pkts\n",
738  service->pcr_packet_period,
740 
741  if (ts->m2ts_mode == -1) {
742  if (av_match_ext(s->filename, "m2ts")) {
743  ts->m2ts_mode = 1;
744  } else {
745  ts->m2ts_mode = 0;
746  }
747  }
748 
749  avio_flush(s->pb);
750 
751  return 0;
752 
753  fail:
754  av_free(pids);
755  for(i = 0;i < s->nb_streams; i++) {
756  MpegTSWriteStream *ts_st;
757  st = s->streams[i];
758  ts_st = st->priv_data;
759  if (ts_st) {
760  av_freep(&ts_st->payload);
761  if (ts_st->amux) {
762  avformat_free_context(ts_st->amux);
763  ts_st->amux = NULL;
764  }
765  }
766  av_freep(&st->priv_data);
767  }
768  return ret;
769 }
770 
771 /* send SDT, PAT and PMT tables regulary */
772 static void retransmit_si_info(AVFormatContext *s, int force_pat)
773 {
774  MpegTSWrite *ts = s->priv_data;
775  int i;
776 
777  if (++ts->sdt_packet_count == ts->sdt_packet_period) {
778  ts->sdt_packet_count = 0;
779  mpegts_write_sdt(s);
780  }
781  if (++ts->pat_packet_count == ts->pat_packet_period || force_pat) {
782  ts->pat_packet_count = 0;
783  mpegts_write_pat(s);
784  for(i = 0; i < ts->nb_services; i++) {
785  mpegts_write_pmt(s, ts->services[i]);
786  }
787  }
788 }
789 
790 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
791 {
792  int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
793 
794  *buf++ = pcr_high >> 25;
795  *buf++ = pcr_high >> 17;
796  *buf++ = pcr_high >> 9;
797  *buf++ = pcr_high >> 1;
798  *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
799  *buf++ = pcr_low;
800 
801  return 6;
802 }
803 
804 /* Write a single null transport stream packet */
806 {
807  uint8_t *q;
809 
810  q = buf;
811  *q++ = 0x47;
812  *q++ = 0x00 | 0x1f;
813  *q++ = 0xff;
814  *q++ = 0x10;
815  memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
817  avio_write(s->pb, buf, TS_PACKET_SIZE);
818 }
819 
820 /* Write a single transport stream packet with a PCR and no payload */
822 {
823  MpegTSWrite *ts = s->priv_data;
824  MpegTSWriteStream *ts_st = st->priv_data;
825  uint8_t *q;
827 
828  q = buf;
829  *q++ = 0x47;
830  *q++ = ts_st->pid >> 8;
831  *q++ = ts_st->pid;
832  *q++ = 0x20 | ts_st->cc; /* Adaptation only */
833  /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
834  *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
835  *q++ = 0x10; /* Adaptation flags: PCR present */
836 
837  /* PCR coded into 6 bytes */
838  q += write_pcr_bits(q, get_pcr(ts, s->pb));
839 
840  /* stuffing bytes */
841  memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
843  avio_write(s->pb, buf, TS_PACKET_SIZE);
844 }
845 
846 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
847 {
848  int val;
849 
850  val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
851  *q++ = val;
852  val = (((pts >> 15) & 0x7fff) << 1) | 1;
853  *q++ = val >> 8;
854  *q++ = val;
855  val = (((pts) & 0x7fff) << 1) | 1;
856  *q++ = val >> 8;
857  *q++ = val;
858 }
859 
860 /* Set an adaptation field flag in an MPEG-TS packet*/
861 static void set_af_flag(uint8_t *pkt, int flag)
862 {
863  // expect at least one flag to set
864  av_assert0(flag);
865 
866  if ((pkt[3] & 0x20) == 0) {
867  // no AF yet, set adaptation field flag
868  pkt[3] |= 0x20;
869  // 1 byte length, no flags
870  pkt[4] = 1;
871  pkt[5] = 0;
872  }
873  pkt[5] |= flag;
874 }
875 
876 /* Extend the adaptation field by size bytes */
877 static void extend_af(uint8_t *pkt, int size)
878 {
879  // expect already existing adaptation field
880  av_assert0(pkt[3] & 0x20);
881  pkt[4] += size;
882 }
883 
884 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
886 {
887  if (pkt[3] & 0x20)
888  return pkt + 5 + pkt[4];
889  else
890  return pkt + 4;
891 }
892 
893 /* Add a pes header to the front of payload, and segment into an integer number of
894  * ts packets. The final ts packet is padded using an over-sized adaptation header
895  * to exactly fill the last ts packet.
896  * NOTE: 'payload' contains a complete PES payload.
897  */
899  const uint8_t *payload, int payload_size,
900  int64_t pts, int64_t dts, int key)
901 {
902  MpegTSWriteStream *ts_st = st->priv_data;
903  MpegTSWrite *ts = s->priv_data;
905  uint8_t *q;
906  int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
907  int afc_len, stuffing_len;
908  int64_t pcr = -1; /* avoid warning */
909  int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
910  int force_pat = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
911 
912  is_start = 1;
913  while (payload_size > 0) {
914  retransmit_si_info(s, force_pat);
915  force_pat = 0;
916 
917  write_pcr = 0;
918  if (ts_st->pid == ts_st->service->pcr_pid) {
919  if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
920  ts_st->service->pcr_packet_count++;
921  if (ts_st->service->pcr_packet_count >=
922  ts_st->service->pcr_packet_period) {
923  ts_st->service->pcr_packet_count = 0;
924  write_pcr = 1;
925  }
926  }
927 
928  if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
929  (dts - get_pcr(ts, s->pb)/300) > delay) {
930  /* pcr insert gets priority over null packet insert */
931  if (write_pcr)
932  mpegts_insert_pcr_only(s, st);
933  else
935  continue; /* recalculate write_pcr and possibly retransmit si_info */
936  }
937 
938  /* prepare packet header */
939  q = buf;
940  *q++ = 0x47;
941  val = (ts_st->pid >> 8);
942  if (is_start)
943  val |= 0x40;
944  *q++ = val;
945  *q++ = ts_st->pid;
946  ts_st->cc = (ts_st->cc + 1) & 0xf;
947  *q++ = 0x10 | ts_st->cc; // payload indicator + CC
948  if (key && is_start && pts != AV_NOPTS_VALUE) {
949  // set Random Access for key frames
950  if (ts_st->pid == ts_st->service->pcr_pid)
951  write_pcr = 1;
952  set_af_flag(buf, 0x40);
953  q = get_ts_payload_start(buf);
954  }
955  if (write_pcr) {
956  set_af_flag(buf, 0x10);
957  q = get_ts_payload_start(buf);
958  // add 11, pcr references the last byte of program clock reference base
959  if (ts->mux_rate > 1)
960  pcr = get_pcr(ts, s->pb);
961  else
962  pcr = (dts - delay)*300;
963  if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
964  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
965  extend_af(buf, write_pcr_bits(q, pcr));
966  q = get_ts_payload_start(buf);
967  }
968  if (is_start) {
969  int pes_extension = 0;
970  int pes_header_stuffing_bytes = 0;
971  /* write PES header */
972  *q++ = 0x00;
973  *q++ = 0x00;
974  *q++ = 0x01;
975  is_dvb_subtitle = 0;
976  is_dvb_teletext = 0;
977  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
978  if (st->codec->codec_id == AV_CODEC_ID_DIRAC) {
979  *q++ = 0xfd;
980  } else
981  *q++ = 0xe0;
982  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
983  (st->codec->codec_id == AV_CODEC_ID_MP2 ||
984  st->codec->codec_id == AV_CODEC_ID_MP3 ||
985  st->codec->codec_id == AV_CODEC_ID_AAC)) {
986  *q++ = 0xc0;
987  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
988  st->codec->codec_id == AV_CODEC_ID_AC3 &&
989  ts->m2ts_mode) {
990  *q++ = 0xfd;
991  } else {
992  *q++ = 0xbd;
995  is_dvb_subtitle = 1;
996  } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
997  is_dvb_teletext = 1;
998  }
999  }
1000  }
1001  header_len = 0;
1002  flags = 0;
1003  if (pts != AV_NOPTS_VALUE) {
1004  header_len += 5;
1005  flags |= 0x80;
1006  }
1007  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1008  header_len += 5;
1009  flags |= 0x40;
1010  }
1011  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
1012  st->codec->codec_id == AV_CODEC_ID_DIRAC) {
1013  /* set PES_extension_flag */
1014  pes_extension = 1;
1015  flags |= 0x01;
1016 
1017  /*
1018  * One byte for PES2 extension flag +
1019  * one byte for extension length +
1020  * one byte for extension id
1021  */
1022  header_len += 3;
1023  }
1024  /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
1025  * otherwise it will not play sound on blu-ray
1026  */
1027  if (ts->m2ts_mode &&
1029  st->codec->codec_id == AV_CODEC_ID_AC3) {
1030  /* set PES_extension_flag */
1031  pes_extension = 1;
1032  flags |= 0x01;
1033  header_len += 3;
1034  }
1035  if (is_dvb_teletext) {
1036  pes_header_stuffing_bytes = 0x24 - header_len;
1037  header_len = 0x24;
1038  }
1039  len = payload_size + header_len + 3;
1040  /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
1041  if (is_dvb_subtitle) {
1042  len += 3;
1043  payload_size++;
1044  }
1045  if (len > 0xffff)
1046  len = 0;
1048  len = 0;
1049  }
1050  *q++ = len >> 8;
1051  *q++ = len;
1052  val = 0x80;
1053  /* data alignment indicator is required for subtitle and data streams */
1055  val |= 0x04;
1056  *q++ = val;
1057  *q++ = flags;
1058  *q++ = header_len;
1059  if (pts != AV_NOPTS_VALUE) {
1060  write_pts(q, flags >> 6, pts);
1061  q += 5;
1062  }
1063  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1064  write_pts(q, 1, dts);
1065  q += 5;
1066  }
1067  if (pes_extension && st->codec->codec_id == AV_CODEC_ID_DIRAC) {
1068  flags = 0x01; /* set PES_extension_flag_2 */
1069  *q++ = flags;
1070  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1071  /*
1072  * Set the stream id extension flag bit to 0 and
1073  * write the extended stream id
1074  */
1075  *q++ = 0x00 | 0x60;
1076  }
1077  /* For Blu-ray AC3 Audio Setting extended flags */
1078  if (ts->m2ts_mode &&
1079  pes_extension &&
1080  st->codec->codec_id == AV_CODEC_ID_AC3) {
1081  flags = 0x01; /* set PES_extension_flag_2 */
1082  *q++ = flags;
1083  *q++ = 0x80 | 0x01; /* marker bit + extension length */
1084  *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1085  }
1086 
1087 
1088  if (is_dvb_subtitle) {
1089  /* First two fields of DVB subtitles PES data:
1090  * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
1091  * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
1092  *q++ = 0x20;
1093  *q++ = 0x00;
1094  }
1095  if (is_dvb_teletext) {
1096  memset(q, 0xff, pes_header_stuffing_bytes);
1097  q += pes_header_stuffing_bytes;
1098  }
1099  is_start = 0;
1100  }
1101  /* header size */
1102  header_len = q - buf;
1103  /* data len */
1104  len = TS_PACKET_SIZE - header_len;
1105  if (len > payload_size)
1106  len = payload_size;
1107  stuffing_len = TS_PACKET_SIZE - header_len - len;
1108  if (stuffing_len > 0) {
1109  /* add stuffing with AFC */
1110  if (buf[3] & 0x20) {
1111  /* stuffing already present: increase its size */
1112  afc_len = buf[4] + 1;
1113  memmove(buf + 4 + afc_len + stuffing_len,
1114  buf + 4 + afc_len,
1115  header_len - (4 + afc_len));
1116  buf[4] += stuffing_len;
1117  memset(buf + 4 + afc_len, 0xff, stuffing_len);
1118  } else {
1119  /* add stuffing */
1120  memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1121  buf[3] |= 0x20;
1122  buf[4] = stuffing_len - 1;
1123  if (stuffing_len >= 2) {
1124  buf[5] = 0x00;
1125  memset(buf + 6, 0xff, stuffing_len - 2);
1126  }
1127  }
1128  }
1129 
1130  if (is_dvb_subtitle && payload_size == len) {
1131  memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
1132  buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
1133  } else {
1134  memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1135  }
1136 
1137  payload += len;
1138  payload_size -= len;
1140  avio_write(s->pb, buf, TS_PACKET_SIZE);
1141  }
1142  avio_flush(s->pb);
1143  ts_st->prev_payload_key = key;
1144 }
1145 
1147 {
1148  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
1149  if (!st->nb_frames) {
1150  av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1151  "no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)\n");
1152  return AVERROR(EINVAL);
1153  }
1154  av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing\n");
1155  }
1156  return 0;
1157 }
1158 
1160 {
1161  AVStream *st = s->streams[pkt->stream_index];
1162  int size = pkt->size;
1163  uint8_t *buf= pkt->data;
1164  uint8_t *data= NULL;
1165  MpegTSWrite *ts = s->priv_data;
1166  MpegTSWriteStream *ts_st = st->priv_data;
1167  const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
1168  int64_t dts = pkt->dts, pts = pkt->pts;
1169 
1170  if (ts->reemit_pat_pmt) {
1171  av_log(s, AV_LOG_WARNING, "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1172  ts->reemit_pat_pmt = 0;
1174  }
1175 
1176  if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1177  ts->pat_packet_count = ts->pat_packet_period - 1;
1178  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1180  }
1181 
1182  if(ts->copyts < 1){
1183  if (pts != AV_NOPTS_VALUE)
1184  pts += delay;
1185  if (dts != AV_NOPTS_VALUE)
1186  dts += delay;
1187  }
1188 
1189  if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1190  av_log(s, AV_LOG_ERROR, "first pts value must be set\n");
1191  return AVERROR_INVALIDDATA;
1192  }
1193  ts_st->first_pts_check = 0;
1194 
1195  if (st->codec->codec_id == AV_CODEC_ID_H264) {
1196  const uint8_t *p = buf, *buf_end = p+size;
1197  uint32_t state = -1;
1198  int ret = ff_check_h264_startcode(s, st, pkt);
1199  if (ret < 0)
1200  return ret;
1201 
1202  do {
1203  p = avpriv_find_start_code(p, buf_end, &state);
1204  av_dlog(s, "nal %d\n", state & 0x1f);
1205  } while (p < buf_end && (state & 0x1f) != 9 &&
1206  (state & 0x1f) != 5 && (state & 0x1f) != 1);
1207 
1208  if ((state & 0x1f) != 9) { // AUD NAL
1209  data = av_malloc(pkt->size+6);
1210  if (!data)
1211  return AVERROR(ENOMEM);
1212  memcpy(data+6, pkt->data, pkt->size);
1213  AV_WB32(data, 0x00000001);
1214  data[4] = 0x09;
1215  data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1216  buf = data;
1217  size = pkt->size+6;
1218  }
1219  } else if (st->codec->codec_id == AV_CODEC_ID_AAC) {
1220  if (pkt->size < 2) {
1221  av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1222  return AVERROR_INVALIDDATA;
1223  }
1224  if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1225  int ret;
1226  AVPacket pkt2;
1227 
1228  if (!ts_st->amux) {
1229  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1230  "and extradata missing\n");
1231  return AVERROR_INVALIDDATA;
1232  }
1233 
1234  av_init_packet(&pkt2);
1235  pkt2.data = pkt->data;
1236  pkt2.size = pkt->size;
1237  av_assert0(pkt->dts != AV_NOPTS_VALUE);
1238  pkt2.dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
1239  ret = avio_open_dyn_buf(&ts_st->amux->pb);
1240  if (ret < 0)
1241  return AVERROR(ENOMEM);
1242 
1243  ret = av_write_frame(ts_st->amux, &pkt2);
1244  if (ret < 0) {
1245  avio_close_dyn_buf(ts_st->amux->pb, &data);
1246  ts_st->amux->pb = NULL;
1247  av_free(data);
1248  return ret;
1249  }
1250  size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1251  ts_st->amux->pb = NULL;
1252  buf = data;
1253  }
1254  }
1255 
1256  if (pkt->dts != AV_NOPTS_VALUE) {
1257  int i;
1258  for(i=0; i<s->nb_streams; i++){
1259  AVStream *st2 = s->streams[i];
1260  MpegTSWriteStream *ts_st2 = st2->priv_data;
1261  if( ts_st2->payload_size
1262  && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)){
1263  mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
1264  ts_st2->payload_pts, ts_st2->payload_dts,
1265  ts_st2->payload_flags & AV_PKT_FLAG_KEY);
1266  ts_st2->payload_size = 0;
1267  }
1268  }
1269  }
1270 
1271  if (ts_st->payload_size && ts_st->payload_size + size > ts->pes_payload_size) {
1272  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1273  ts_st->payload_pts, ts_st->payload_dts,
1274  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1275  ts_st->payload_size = 0;
1276  }
1277 
1278  if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
1279  av_assert0(!ts_st->payload_size);
1280  // for video and subtitle, write a single pes packet
1281  mpegts_write_pes(s, st, buf, size, pts, dts, pkt->flags & AV_PKT_FLAG_KEY);
1282  av_free(data);
1283  return 0;
1284  }
1285 
1286  if (!ts_st->payload_size) {
1287  ts_st->payload_pts = pts;
1288  ts_st->payload_dts = dts;
1289  ts_st->payload_flags = pkt->flags;
1290  }
1291 
1292  memcpy(ts_st->payload + ts_st->payload_size, buf, size);
1293  ts_st->payload_size += size;
1294 
1295  av_free(data);
1296 
1297  return 0;
1298 }
1299 
1301 {
1302  int i;
1303 
1304  /* flush current packets */
1305  for(i = 0; i < s->nb_streams; i++) {
1306  AVStream *st = s->streams[i];
1307  MpegTSWriteStream *ts_st = st->priv_data;
1308  if (ts_st->payload_size > 0) {
1309  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1310  ts_st->payload_pts, ts_st->payload_dts,
1311  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1312  ts_st->payload_size = 0;
1313  }
1314  }
1315  avio_flush(s->pb);
1316 }
1317 
1319 {
1320  if (!pkt) {
1321  mpegts_write_flush(s);
1322  return 1;
1323  } else {
1324  return mpegts_write_packet_internal(s, pkt);
1325  }
1326 }
1327 
1329 {
1330  MpegTSWrite *ts = s->priv_data;
1331  MpegTSService *service;
1332  int i;
1333 
1334  mpegts_write_flush(s);
1335 
1336  for(i = 0; i < s->nb_streams; i++) {
1337  AVStream *st = s->streams[i];
1338  MpegTSWriteStream *ts_st = st->priv_data;
1339  av_freep(&ts_st->payload);
1340  if (ts_st->amux) {
1341  avformat_free_context(ts_st->amux);
1342  ts_st->amux = NULL;
1343  }
1344  }
1345 
1346  for(i = 0; i < ts->nb_services; i++) {
1347  service = ts->services[i];
1348  av_freep(&service->provider_name);
1349  av_freep(&service->name);
1350  av_free(service);
1351  }
1352  av_free(ts->services);
1353 
1354  return 0;
1355 }
1356 
1357 static const AVOption options[] = {
1358  { "mpegts_transport_stream_id", "Set transport_stream_id field.",
1359  offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
1360  { "mpegts_original_network_id", "Set original_network_id field.",
1361  offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
1362  { "mpegts_service_id", "Set service_id field.",
1363  offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
1364  { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
1365  offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT, {.i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM},
1366  { "mpegts_start_pid", "Set the first pid.",
1367  offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT, {.i64 = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM},
1368  {"mpegts_m2ts_mode", "Enable m2ts mode.",
1369  offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_INT, {.i64 = -1 },
1371  { "muxrate", NULL, offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
1372  { "pes_payload_size", "Minimum PES packet payload in bytes",
1373  offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT, {.i64 = DEFAULT_PES_PAYLOAD_SIZE}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
1374  { "mpegts_flags", "MPEG-TS muxing flags", offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX,
1375  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1376  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1377  0, AV_OPT_TYPE_CONST, {.i64 = MPEGTS_FLAG_REEMIT_PAT_PMT}, 0, INT_MAX,
1378  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags"},
1379  { "latm", "Use LATM packetization for AAC",
1380  0, AV_OPT_TYPE_CONST, {.i64 = MPEGTS_FLAG_AAC_LATM}, 0, INT_MAX,
1381  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags"},
1382  // backward compatibility
1383  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1384  offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
1385  { "mpegts_copyts", "don't offset dts/pts",
1386  offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
1387  { "tables_version", "set PAT, PMT and SDT version",
1388  offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT, {.i64=0}, 0, 31, AV_OPT_FLAG_ENCODING_PARAM},
1389  { "omit_video_pes_length", "Omit the PES packet length for video packets",
1390  offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
1391  { "pcr_period", "PCR retransmission time",
1392  offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT, { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1393  { NULL },
1394 };
1395 
1396 static const AVClass mpegts_muxer_class = {
1397  .class_name = "MPEGTS muxer",
1398  .item_name = av_default_item_name,
1399  .option = options,
1400  .version = LIBAVUTIL_VERSION_INT,
1401 };
1402 
1404  .name = "mpegts",
1405  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
1406  .mime_type = "video/MP2T",
1407  .extensions = "ts,m2t,m2ts,mts",
1408  .priv_data_size = sizeof(MpegTSWrite),
1409  .audio_codec = AV_CODEC_ID_MP2,
1410  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1415  .priv_class = &mpegts_muxer_class,
1416 };