Libav
mpegtsenc.c
Go to the documentation of this file.
1 /*
2  * MPEG-2 transport stream (aka DVB) muxer
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 
29 #include "libavcodec/internal.h"
30 
31 #include "avformat.h"
32 #include "avio_internal.h"
33 #include "internal.h"
34 #include "mpegts.h"
35 
36 #define PCR_TIME_BASE 27000000
37 
38 /* write DVB SI sections */
39 
40 #define DVB_PRIVATE_NETWORK_START 0xff01
41 
42 /*********************************************/
43 /* mpegts section writer */
44 
45 typedef struct MpegTSSection {
46  int pid;
47  int cc;
48  void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
49  void *opaque;
51 
52 typedef struct MpegTSService {
53  MpegTSSection pmt; /* MPEG-2 PMT table context */
54  int sid; /* service ID */
55  char *name;
57  int pcr_pid;
61 
62 typedef struct MpegTSWrite {
63  const AVClass *av_class;
64  MpegTSSection pat; /* MPEG-2 PAT table */
65  MpegTSSection sdt; /* MPEG-2 SDT table context */
72  int onid;
73  int tsid;
74  int64_t first_pcr;
75  int mux_rate;
77 
81 
83  int start_pid;
84  int m2ts_mode;
85 
86  int reemit_pat_pmt; // backward compatibility
87 
89 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
90 #define MPEGTS_FLAG_AAC_LATM 0x02
91 #define MPEGTS_FLAG_SYSTEM_B 0x04
92  int flags;
93 } MpegTSWrite;
94 
95 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
96 #define DEFAULT_PES_HEADER_FREQ 16
97 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
98 
99 /* The section length is 12 bits. The first 2 are set to 0, the remaining
100  * 10 bits should not exceed 1021. */
101 #define SECTION_LENGTH 1020
102 
103 /* NOTE: 4 bytes must be left at the end for the crc32 */
104 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
105 {
106  unsigned int crc;
107  unsigned char packet[TS_PACKET_SIZE];
108  const unsigned char *buf_ptr;
109  unsigned char *q;
110  int first, b, len1, left;
111 
113  -1, buf, len - 4));
114 
115  buf[len - 4] = (crc >> 24) & 0xff;
116  buf[len - 3] = (crc >> 16) & 0xff;
117  buf[len - 2] = (crc >> 8) & 0xff;
118  buf[len - 1] = crc & 0xff;
119 
120  /* send each packet */
121  buf_ptr = buf;
122  while (len > 0) {
123  first = buf == buf_ptr;
124  q = packet;
125  *q++ = 0x47;
126  b = s->pid >> 8;
127  if (first)
128  b |= 0x40;
129  *q++ = b;
130  *q++ = s->pid;
131  s->cc = s->cc + 1 & 0xf;
132  *q++ = 0x10 | s->cc;
133  if (first)
134  *q++ = 0; /* 0 offset */
135  len1 = TS_PACKET_SIZE - (q - packet);
136  if (len1 > len)
137  len1 = len;
138  memcpy(q, buf_ptr, len1);
139  q += len1;
140  /* add known padding data */
141  left = TS_PACKET_SIZE - (q - packet);
142  if (left > 0)
143  memset(q, 0xff, left);
144 
145  s->write_packet(s, packet);
146 
147  buf_ptr += len1;
148  len -= len1;
149  }
150 }
151 
152 static inline void put16(uint8_t **q_ptr, int val)
153 {
154  uint8_t *q;
155  q = *q_ptr;
156  *q++ = val >> 8;
157  *q++ = val;
158  *q_ptr = q;
159 }
160 
161 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
162  int version, int sec_num, int last_sec_num,
163  uint8_t *buf, int len)
164 {
165  uint8_t section[1024], *q;
166  unsigned int tot_len;
167  /* reserved_future_use field must be set to 1 for SDT */
168  unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
169 
170  tot_len = 3 + 5 + len + 4;
171  /* check if not too big */
172  if (tot_len > 1024)
173  return AVERROR_INVALIDDATA;
174 
175  q = section;
176  *q++ = tid;
177  put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
178  put16(&q, id);
179  *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
180  *q++ = sec_num;
181  *q++ = last_sec_num;
182  memcpy(q, buf, len);
183 
184  mpegts_write_section(s, section, tot_len);
185  return 0;
186 }
187 
188 /*********************************************/
189 /* mpegts writer */
190 
191 #define DEFAULT_PROVIDER_NAME "Libav"
192 #define DEFAULT_SERVICE_NAME "Service01"
193 
194 /* we retransmit the SI info at this rate */
195 #define SDT_RETRANS_TIME 500
196 #define PAT_RETRANS_TIME 100
197 #define PCR_RETRANS_TIME 20
198 
199 typedef struct MpegTSWriteStream {
201  int pid; /* stream associated pid */
202  int cc;
205  int64_t payload_pts;
206  int64_t payload_dts;
212 
214 {
215  MpegTSWrite *ts = s->priv_data;
216  MpegTSService *service;
218  int i;
219 
220  q = data;
221  for (i = 0; i < ts->nb_services; i++) {
222  service = ts->services[i];
223  put16(&q, service->sid);
224  put16(&q, 0xe000 | service->pmt.pid);
225  }
226  mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
227  data, q - data);
228 }
229 
231 {
232  MpegTSWrite *ts = s->priv_data;
233  uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
234  int val, stream_type, i, err = 0;
235 
236  q = data;
237  put16(&q, 0xe000 | service->pcr_pid);
238 
239  program_info_length_ptr = q;
240  q += 2; /* patched after */
241 
242  /* put program info here */
243 
244  val = 0xf000 | (q - program_info_length_ptr - 2);
245  program_info_length_ptr[0] = val >> 8;
246  program_info_length_ptr[1] = val;
247 
248  for (i = 0; i < s->nb_streams; i++) {
249  AVStream *st = s->streams[i];
250  MpegTSWriteStream *ts_st = st->priv_data;
251  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
252 
253  if (q - data > SECTION_LENGTH - 3 - 2 - 6) {
254  err = 1;
255  break;
256  }
257  switch (st->codecpar->codec_id) {
260  stream_type = STREAM_TYPE_VIDEO_MPEG2;
261  break;
262  case AV_CODEC_ID_MPEG4:
263  stream_type = STREAM_TYPE_VIDEO_MPEG4;
264  break;
265  case AV_CODEC_ID_H264:
266  stream_type = STREAM_TYPE_VIDEO_H264;
267  break;
268  case AV_CODEC_ID_HEVC:
269  stream_type = STREAM_TYPE_VIDEO_HEVC;
270  break;
271  case AV_CODEC_ID_CAVS:
272  stream_type = STREAM_TYPE_VIDEO_CAVS;
273  break;
274  case AV_CODEC_ID_DIRAC:
275  stream_type = STREAM_TYPE_VIDEO_DIRAC;
276  break;
277  case AV_CODEC_ID_MP2:
278  case AV_CODEC_ID_MP3:
279  stream_type = STREAM_TYPE_AUDIO_MPEG1;
280  break;
281  case AV_CODEC_ID_AAC:
282  stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
285  break;
287  stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
288  break;
289  case AV_CODEC_ID_AC3:
290  stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
293  break;
294  default:
295  stream_type = STREAM_TYPE_PRIVATE_DATA;
296  break;
297  }
298  *q++ = stream_type;
299  put16(&q, 0xe000 | ts_st->pid);
300  desc_length_ptr = q;
301  q += 2; /* patched after */
302 
303  /* write optional descriptors here */
304  switch (st->codecpar->codec_type) {
305  case AVMEDIA_TYPE_AUDIO:
307  *q++ = 0x6a; /* ETSI EN 300 468 AC-3 descriptor */
308  *q++ = 1;
309  *q++ = 0x00;
310  }
311 
312  if (lang) {
313  char *p;
314  char *next = lang->value;
315  uint8_t *len_ptr;
316 
317  *q++ = 0x0a; /* ISO 639 language descriptor */
318  len_ptr = q++;
319  *len_ptr = 0;
320 
321  for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
322  if (q - data > SECTION_LENGTH - 4) {
323  err = 1;
324  break;
325  }
326  next = strchr(p, ',');
327  if (strlen(p) != 3 && (!next || next != p + 3))
328  continue; /* not a 3-letter code */
329 
330  *q++ = *p++;
331  *q++ = *p++;
332  *q++ = *p++;
333 
335  *q++ = 0x01;
337  *q++ = 0x02;
339  *q++ = 0x03;
340  else
341  *q++ = 0; /* undefined type */
342 
343  *len_ptr += 4;
344  }
345 
346  if (*len_ptr == 0)
347  q -= 2; /* no language codes were written */
348  }
349  break;
351  {
352  const char *language;
353  language = lang && strlen(lang->value) == 3 ? lang->value : "eng";
354  *q++ = 0x59;
355  *q++ = 8;
356  *q++ = language[0];
357  *q++ = language[1];
358  *q++ = language[2];
359  *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
360 
361  if (q - data > SECTION_LENGTH - 4) {
362  err = 1;
363  break;
364  }
365 
366  if (st->codecpar->extradata_size == 4) {
367  memcpy(q, st->codecpar->extradata, 4);
368  q += 4;
369  } else {
370  put16(&q, 1); /* page id */
371  put16(&q, 1); /* ancillary page id */
372  }
373  }
374  break;
375  case AVMEDIA_TYPE_VIDEO:
376  if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
377  *q++ = 0x05; /*MPEG-2 registration descriptor*/
378  *q++ = 4;
379  *q++ = 'd';
380  *q++ = 'r';
381  *q++ = 'a';
382  *q++ = 'c';
383  }
384  break;
385  }
386 
387  val = 0xf000 | (q - desc_length_ptr - 2);
388  desc_length_ptr[0] = val >> 8;
389  desc_length_ptr[1] = val;
390  }
391 
392  if (err)
393  av_log(s, AV_LOG_ERROR,
394  "The PMT section cannot fit stream %d and all following streams.\n"
395  "Try reducing the number of languages in the audio streams "
396  "or the total number of streams.\n", i);
397 
398  mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
399  data, q - data);
400 }
401 
402 /* NOTE: str == NULL is accepted for an empty string */
403 static void putstr8(uint8_t **q_ptr, const char *str)
404 {
405  uint8_t *q;
406  int len;
407 
408  q = *q_ptr;
409  if (!str)
410  len = 0;
411  else
412  len = strlen(str);
413  *q++ = len;
414  memcpy(q, str, len);
415  q += len;
416  *q_ptr = q;
417 }
418 
420 {
421  MpegTSWrite *ts = s->priv_data;
422  MpegTSService *service;
423  uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
424  int i, running_status, free_ca_mode, val;
425 
426  q = data;
427  put16(&q, ts->onid);
428  *q++ = 0xff;
429  for (i = 0; i < ts->nb_services; i++) {
430  service = ts->services[i];
431  put16(&q, service->sid);
432  *q++ = 0xfc | 0x00; /* currently no EIT info */
433  desc_list_len_ptr = q;
434  q += 2;
435  running_status = 4; /* running */
436  free_ca_mode = 0;
437 
438  /* write only one descriptor for the service name and provider */
439  *q++ = 0x48;
440  desc_len_ptr = q;
441  q++;
442  *q++ = 0x01; /* digital television service */
443  putstr8(&q, service->provider_name);
444  putstr8(&q, service->name);
445  desc_len_ptr[0] = q - desc_len_ptr - 1;
446 
447  /* fill descriptor length */
448  val = (running_status << 13) | (free_ca_mode << 12) |
449  (q - desc_list_len_ptr - 2);
450  desc_list_len_ptr[0] = val >> 8;
451  desc_list_len_ptr[1] = val;
452  }
453  mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
454  data, q - data);
455 }
456 
458  const char *provider_name,
459  const char *name)
460 {
461  MpegTSService *service;
462 
463  service = av_mallocz(sizeof(MpegTSService));
464  if (!service)
465  return NULL;
466  service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
467  service->sid = sid;
468  service->pcr_pid = 0x1fff;
469  service->provider_name = av_strdup(provider_name);
470  service->name = av_strdup(name);
471  if (!service->provider_name || !service->name) {
472  av_free(service->provider_name);
473  av_free(service->name);
474  av_free(service);
475  return NULL;
476  }
477  dynarray_add(&ts->services, &ts->nb_services, service);
478  return service;
479 }
480 
481 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
482 {
483  return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
484  ts->first_pcr;
485 }
486 
488 {
489  MpegTSWrite *ts = s->priv_data;
490  if (ts->m2ts_mode) {
491  int64_t pcr = get_pcr(s->priv_data, s->pb);
492  uint32_t tp_extra_header = pcr % 0x3fffffff;
493  tp_extra_header = AV_RB32(&tp_extra_header);
494  avio_write(s->pb, (unsigned char *) &tp_extra_header,
495  sizeof(tp_extra_header));
496  }
497 }
498 
499 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
500 {
501  AVFormatContext *ctx = s->opaque;
503  avio_write(ctx->pb, packet, TS_PACKET_SIZE);
504 }
505 
507 {
508  MpegTSWrite *ts = s->priv_data;
509  MpegTSWriteStream *ts_st;
510  MpegTSService *service;
511  AVStream *st, *pcr_st = NULL;
512  AVDictionaryEntry *title, *provider;
513  int i, j;
514  const char *service_name;
515  const char *provider_name;
516  int *pids;
517  int ret;
518 
519  if (s->max_delay < 0) /* Not set by the caller */
520  s->max_delay = 0;
521 
522  // round up to a whole number of TS packets
523  ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
524 
525  ts->tsid = ts->transport_stream_id;
526  ts->onid = ts->original_network_id;
527  /* allocate a single DVB service */
528  title = av_dict_get(s->metadata, "service_name", NULL, 0);
529  if (!title)
530  title = av_dict_get(s->metadata, "title", NULL, 0);
531  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
532  provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
533  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
534  service = mpegts_add_service(ts, ts->service_id,
535  provider_name, service_name);
536 
537  if (!service)
538  return AVERROR(ENOMEM);
539 
541  service->pmt.opaque = s;
542  service->pmt.cc = 15;
543 
544  ts->pat.pid = PAT_PID;
545  /* Initialize at 15 so that it wraps and is equal to 0 for the
546  * first packet we write. */
547  ts->pat.cc = 15;
549  ts->pat.opaque = s;
550 
551  ts->sdt.pid = SDT_PID;
552  ts->sdt.cc = 15;
554  ts->sdt.opaque = s;
555 
556  pids = av_malloc(s->nb_streams * sizeof(*pids));
557  if (!pids) {
558  av_free(service);
559  return AVERROR(ENOMEM);
560  }
561 
562  /* assign pids to each stream */
563  for (i = 0; i < s->nb_streams; i++) {
564  st = s->streams[i];
565 
566  ts_st = av_mallocz(sizeof(MpegTSWriteStream));
567  if (!ts_st) {
568  ret = AVERROR(ENOMEM);
569  goto fail;
570  }
571  st->priv_data = ts_st;
572 
573  ts_st->user_tb = st->time_base;
574  avpriv_set_pts_info(st, 33, 1, 90000);
575 
576  ts_st->payload = av_mallocz(ts->pes_payload_size);
577  if (!ts_st->payload) {
578  ret = AVERROR(ENOMEM);
579  goto fail;
580  }
581  ts_st->service = service;
582  /* MPEG pid values < 16 are reserved. Applications which set st->id in
583  * this range are assigned a calculated pid. */
584  if (st->id < 16) {
585  ts_st->pid = ts->start_pid + i;
586  } else if (st->id < 0x1FFF) {
587  ts_st->pid = st->id;
588  } else {
589  av_log(s, AV_LOG_ERROR,
590  "Invalid stream id %d, must be less than 8191\n", st->id);
591  ret = AVERROR(EINVAL);
592  goto fail;
593  }
594  if (ts_st->pid == service->pmt.pid) {
595  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
596  ret = AVERROR(EINVAL);
597  goto fail;
598  }
599  for (j = 0; j < i; j++) {
600  if (pids[j] == ts_st->pid) {
601  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
602  ret = AVERROR(EINVAL);
603  goto fail;
604  }
605  }
606  pids[i] = ts_st->pid;
607  ts_st->payload_pts = AV_NOPTS_VALUE;
608  ts_st->payload_dts = AV_NOPTS_VALUE;
609  ts_st->first_pts_check = 1;
610  ts_st->cc = 15;
611  /* update PCR pid by using the first video stream */
612  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
613  service->pcr_pid == 0x1fff) {
614  service->pcr_pid = ts_st->pid;
615  pcr_st = st;
616  }
617  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
618  st->codecpar->extradata_size > 0) {
619  AVStream *ast;
620  ts_st->amux = avformat_alloc_context();
621  if (!ts_st->amux) {
622  ret = AVERROR(ENOMEM);
623  goto fail;
624  }
625  ts_st->amux->oformat =
626  av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
627  NULL, NULL);
628  if (!ts_st->amux->oformat) {
629  ret = AVERROR(EINVAL);
630  goto fail;
631  }
632  if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
633  ret = AVERROR(ENOMEM);
634  goto fail;
635  }
636  ret = avcodec_parameters_copy(ast->codecpar, st->codecpar);
637  if (ret != 0)
638  goto fail;
639  ast->time_base = st->time_base;
640  ret = avformat_write_header(ts_st->amux, NULL);
641  if (ret < 0)
642  goto fail;
643  }
644  }
645 
646  av_free(pids);
647 
648  /* if no video stream, use the first stream as PCR */
649  if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
650  pcr_st = s->streams[0];
651  ts_st = pcr_st->priv_data;
652  service->pcr_pid = ts_st->pid;
653  } else
654  ts_st = pcr_st->priv_data;
655 
656  if (ts->mux_rate > 1) {
657  service->pcr_packet_period = (ts->mux_rate * ts->pcr_period) /
658  (TS_PACKET_SIZE * 8 * 1000);
660  (TS_PACKET_SIZE * 8 * 1000);
662  (TS_PACKET_SIZE * 8 * 1000);
663 
665  } else {
666  /* Arbitrary values, PAT/PMT could be written on key frames */
667  ts->sdt_packet_period = 200;
668  ts->pat_packet_period = 40;
669  if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
671  if (!frame_size) {
672  av_log(s, AV_LOG_WARNING, "frame size not set\n");
673  service->pcr_packet_period =
674  pcr_st->codecpar->sample_rate / (10 * 512);
675  } else {
676  service->pcr_packet_period =
677  pcr_st->codecpar->sample_rate / (10 * frame_size);
678  }
679  } else {
680  // max delta PCR 0.1s
681  // TODO: should be avg_frame_rate
682  service->pcr_packet_period =
683  ts_st->user_tb.den / (10 * ts_st->user_tb.num);
684  }
685  }
686 
687  // output a PCR as soon as possible
688  service->pcr_packet_count = service->pcr_packet_period;
689  ts->pat_packet_count = ts->pat_packet_period - 1;
690  ts->sdt_packet_count = ts->sdt_packet_period - 1;
691 
692  if (ts->mux_rate == 1)
693  av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
694  else
695  av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
697  "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
698  service->pcr_packet_period,
700 
701  if (ts->m2ts_mode == -1) {
702  if (av_match_ext(s->filename, "m2ts")) {
703  ts->m2ts_mode = 1;
704  } else {
705  ts->m2ts_mode = 0;
706  }
707  }
708 
709  avio_flush(s->pb);
710 
711  return 0;
712 
713 fail:
714  av_free(service);
715  av_free(pids);
716  for (i = 0; i < s->nb_streams; i++) {
717  st = s->streams[i];
718  ts_st = st->priv_data;
719  if (ts_st) {
720  av_freep(&ts_st->payload);
721  if (ts_st->amux) {
722  avformat_free_context(ts_st->amux);
723  ts_st->amux = NULL;
724  }
725  }
726  av_freep(&st->priv_data);
727  }
728  return ret;
729 }
730 
731 /* send SDT, PAT and PMT tables regularly */
733 {
734  MpegTSWrite *ts = s->priv_data;
735  int i;
736 
737  if (++ts->sdt_packet_count == ts->sdt_packet_period) {
738  ts->sdt_packet_count = 0;
739  mpegts_write_sdt(s);
740  }
741  if (++ts->pat_packet_count == ts->pat_packet_period) {
742  ts->pat_packet_count = 0;
743  mpegts_write_pat(s);
744  for (i = 0; i < ts->nb_services; i++)
745  mpegts_write_pmt(s, ts->services[i]);
746  }
747 }
748 
749 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
750 {
751  int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
752 
753  *buf++ = pcr_high >> 25;
754  *buf++ = pcr_high >> 17;
755  *buf++ = pcr_high >> 9;
756  *buf++ = pcr_high >> 1;
757  *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
758  *buf++ = pcr_low;
759 
760  return 6;
761 }
762 
763 /* Write a single null transport stream packet */
765 {
766  uint8_t *q;
767  uint8_t buf[TS_PACKET_SIZE];
768 
769  q = buf;
770  *q++ = 0x47;
771  *q++ = 0x00 | 0x1f;
772  *q++ = 0xff;
773  *q++ = 0x10;
774  memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
776  avio_write(s->pb, buf, TS_PACKET_SIZE);
777 }
778 
779 /* Write a single transport stream packet with a PCR and no payload */
781 {
782  MpegTSWrite *ts = s->priv_data;
783  MpegTSWriteStream *ts_st = st->priv_data;
784  uint8_t *q;
785  uint8_t buf[TS_PACKET_SIZE];
786 
787  q = buf;
788  *q++ = 0x47;
789  *q++ = ts_st->pid >> 8;
790  *q++ = ts_st->pid;
791  *q++ = 0x20 | ts_st->cc; /* Adaptation only */
792  /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
793  *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
794  *q++ = 0x10; /* Adaptation flags: PCR present */
795 
796  /* PCR coded into 6 bytes */
797  q += write_pcr_bits(q, get_pcr(ts, s->pb));
798 
799  /* stuffing bytes */
800  memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
802  avio_write(s->pb, buf, TS_PACKET_SIZE);
803 }
804 
805 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
806 {
807  int val;
808 
809  val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
810  *q++ = val;
811  val = (((pts >> 15) & 0x7fff) << 1) | 1;
812  *q++ = val >> 8;
813  *q++ = val;
814  val = (((pts) & 0x7fff) << 1) | 1;
815  *q++ = val >> 8;
816  *q++ = val;
817 }
818 
819 /* Set an adaptation field flag in an MPEG-TS packet*/
820 static void set_af_flag(uint8_t *pkt, int flag)
821 {
822  // expect at least one flag to set
823  assert(flag);
824 
825  if ((pkt[3] & 0x20) == 0) {
826  // no AF yet, set adaptation field flag
827  pkt[3] |= 0x20;
828  // 1 byte length, no flags
829  pkt[4] = 1;
830  pkt[5] = 0;
831  }
832  pkt[5] |= flag;
833 }
834 
835 /* Extend the adaptation field by size bytes */
836 static void extend_af(uint8_t *pkt, int size)
837 {
838  // expect already existing adaptation field
839  assert(pkt[3] & 0x20);
840  pkt[4] += size;
841 }
842 
843 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
845 {
846  if (pkt[3] & 0x20)
847  return pkt + 5 + pkt[4];
848  else
849  return pkt + 4;
850 }
851 
852 /* Add a PES header to the front of the payload, and segment into an integer
853  * number of TS packets. The final TS packet is padded using an oversized
854  * adaptation header to exactly fill the last TS packet.
855  * NOTE: 'payload' contains a complete PES payload. */
857  const uint8_t *payload, int payload_size,
858  int64_t pts, int64_t dts, int key)
859 {
860  MpegTSWriteStream *ts_st = st->priv_data;
861  MpegTSWrite *ts = s->priv_data;
862  uint8_t buf[TS_PACKET_SIZE];
863  uint8_t *q;
864  int val, is_start, len, header_len, write_pcr, private_code, flags;
865  int afc_len, stuffing_len;
866  int64_t pcr = -1; /* avoid warning */
867  int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
868 
869  is_start = 1;
870  while (payload_size > 0) {
872 
873  write_pcr = 0;
874  if (ts_st->pid == ts_st->service->pcr_pid) {
875  if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
876  ts_st->service->pcr_packet_count++;
877  if (ts_st->service->pcr_packet_count >=
878  ts_st->service->pcr_packet_period) {
879  ts_st->service->pcr_packet_count = 0;
880  write_pcr = 1;
881  }
882  }
883 
884  if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
885  (dts - get_pcr(ts, s->pb) / 300) > delay) {
886  /* pcr insert gets priority over null packet insert */
887  if (write_pcr)
888  mpegts_insert_pcr_only(s, st);
889  else
891  /* recalculate write_pcr and possibly retransmit si_info */
892  continue;
893  }
894 
895  /* prepare packet header */
896  q = buf;
897  *q++ = 0x47;
898  val = ts_st->pid >> 8;
899  if (is_start)
900  val |= 0x40;
901  *q++ = val;
902  *q++ = ts_st->pid;
903  ts_st->cc = ts_st->cc + 1 & 0xf;
904  *q++ = 0x10 | ts_st->cc; // payload indicator + CC
905  if (key && is_start && pts != AV_NOPTS_VALUE) {
906  // set Random Access for key frames
907  if (ts_st->pid == ts_st->service->pcr_pid)
908  write_pcr = 1;
909  set_af_flag(buf, 0x40);
910  q = get_ts_payload_start(buf);
911  }
912  if (write_pcr) {
913  set_af_flag(buf, 0x10);
914  q = get_ts_payload_start(buf);
915  // add 11, pcr references the last byte of program clock reference base
916  if (ts->mux_rate > 1)
917  pcr = get_pcr(ts, s->pb);
918  else
919  pcr = (dts - delay) * 300;
920  if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
921  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
922  extend_af(buf, write_pcr_bits(q, pcr));
923  q = get_ts_payload_start(buf);
924  }
925  if (is_start) {
926  int pes_extension = 0;
927  /* write PES header */
928  *q++ = 0x00;
929  *q++ = 0x00;
930  *q++ = 0x01;
931  private_code = 0;
932  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
933  if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)
934  *q++ = 0xfd;
935  else
936  *q++ = 0xe0;
937  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
938  (st->codecpar->codec_id == AV_CODEC_ID_MP2 ||
940  st->codecpar->codec_id == AV_CODEC_ID_AAC)) {
941  *q++ = 0xc0;
942  } else {
943  *q++ = 0xbd;
945  private_code = 0x20;
946  }
947  header_len = 0;
948  flags = 0;
949  if (pts != AV_NOPTS_VALUE) {
950  header_len += 5;
951  flags |= 0x80;
952  }
953  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
954  header_len += 5;
955  flags |= 0x40;
956  }
957  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
959  /* set PES_extension_flag */
960  pes_extension = 1;
961  flags |= 0x01;
962 
963  /* One byte for PES2 extension flag +
964  * one byte for extension length +
965  * one byte for extension id */
966  header_len += 3;
967  }
968  len = payload_size + header_len + 3;
969  if (private_code != 0)
970  len++;
971  if (len > 0xffff)
972  len = 0;
973  *q++ = len >> 8;
974  *q++ = len;
975  val = 0x80;
976  /* data alignment indicator is required for subtitle data */
978  val |= 0x04;
979  *q++ = val;
980  *q++ = flags;
981  *q++ = header_len;
982  if (pts != AV_NOPTS_VALUE) {
983  write_pts(q, flags >> 6, pts);
984  q += 5;
985  }
986  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
987  write_pts(q, 1, dts);
988  q += 5;
989  }
990  if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
991  flags = 0x01; /* set PES_extension_flag_2 */
992  *q++ = flags;
993  *q++ = 0x80 | 0x01; /* marker bit + extension length */
994  /* Set the stream ID extension flag bit to 0 and
995  * write the extended stream ID. */
996  *q++ = 0x00 | 0x60;
997  }
998  if (private_code != 0)
999  *q++ = private_code;
1000  is_start = 0;
1001  }
1002  /* header size */
1003  header_len = q - buf;
1004  /* data len */
1005  len = TS_PACKET_SIZE - header_len;
1006  if (len > payload_size)
1007  len = payload_size;
1008  stuffing_len = TS_PACKET_SIZE - header_len - len;
1009  if (stuffing_len > 0) {
1010  /* add stuffing with AFC */
1011  if (buf[3] & 0x20) {
1012  /* stuffing already present: increase its size */
1013  afc_len = buf[4] + 1;
1014  memmove(buf + 4 + afc_len + stuffing_len,
1015  buf + 4 + afc_len,
1016  header_len - (4 + afc_len));
1017  buf[4] += stuffing_len;
1018  memset(buf + 4 + afc_len, 0xff, stuffing_len);
1019  } else {
1020  /* add stuffing */
1021  memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1022  buf[3] |= 0x20;
1023  buf[4] = stuffing_len - 1;
1024  if (stuffing_len >= 2) {
1025  buf[5] = 0x00;
1026  memset(buf + 6, 0xff, stuffing_len - 2);
1027  }
1028  }
1029  }
1030  memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1031  payload += len;
1032  payload_size -= len;
1034  avio_write(s->pb, buf, TS_PACKET_SIZE);
1035  }
1036  avio_flush(s->pb);
1037 }
1038 
1040 {
1041  AVStream *st = s->streams[pkt->stream_index];
1042  int size = pkt->size;
1043  uint8_t *buf = pkt->data;
1044  uint8_t *data = NULL;
1045  MpegTSWrite *ts = s->priv_data;
1046  MpegTSWriteStream *ts_st = st->priv_data;
1047  const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1048  int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE;
1049 
1050  if (ts->reemit_pat_pmt) {
1052  "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1053  ts->reemit_pat_pmt = 0;
1055  }
1056 
1057  if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1058  ts->pat_packet_count = ts->pat_packet_period - 1;
1059  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1061  }
1062 
1063  if (pkt->pts != AV_NOPTS_VALUE)
1064  pts = pkt->pts + delay;
1065  if (pkt->dts != AV_NOPTS_VALUE)
1066  dts = pkt->dts + delay;
1067 
1068  if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1069  av_log(s, AV_LOG_ERROR, "first pts value must set\n");
1070  return AVERROR_INVALIDDATA;
1071  }
1072  ts_st->first_pts_check = 0;
1073 
1074  if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1075  const uint8_t *p = buf, *buf_end = p + size;
1076  uint32_t state = -1;
1077 
1078  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
1079  av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1080  "no startcode found, use -bsf h264_mp4toannexb\n");
1081  return AVERROR_INVALIDDATA;
1082  }
1083 
1084  do {
1085  p = avpriv_find_start_code(p, buf_end, &state);
1086  av_log(s, AV_LOG_TRACE, "nal %d\n", state & 0x1f);
1087  } while (p < buf_end && (state & 0x1f) != 9 &&
1088  (state & 0x1f) != 5 && (state & 0x1f) != 1);
1089 
1090  if ((state & 0x1f) != 9) { // AUD NAL
1091  data = av_malloc(pkt->size + 6);
1092  if (!data)
1093  return AVERROR(ENOMEM);
1094  memcpy(data + 6, pkt->data, pkt->size);
1095  AV_WB32(data, 0x00000001);
1096  data[4] = 0x09;
1097  data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1098  buf = data;
1099  size = pkt->size + 6;
1100  }
1101  } else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
1102  if (pkt->size < 2) {
1103  av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1104  return AVERROR_INVALIDDATA;
1105  }
1106  if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1107  int ret;
1108  AVPacket pkt2;
1109 
1110  if (!ts_st->amux) {
1111  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1112  "and extradata missing\n");
1113  return AVERROR_INVALIDDATA;
1114  }
1115 
1116  av_init_packet(&pkt2);
1117  pkt2.data = pkt->data;
1118  pkt2.size = pkt->size;
1119 
1120  ret = avio_open_dyn_buf(&ts_st->amux->pb);
1121  if (ret < 0)
1122  return AVERROR(ENOMEM);
1123 
1124  ret = av_write_frame(ts_st->amux, &pkt2);
1125  if (ret < 0) {
1126  ffio_free_dyn_buf(&ts_st->amux->pb);
1127  return ret;
1128  }
1129  size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1130  ts_st->amux->pb = NULL;
1131  buf = data;
1132  }
1133  }
1134 
1135  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
1136  // for video and subtitle, write a single pes packet
1137  mpegts_write_pes(s, st, buf, size, pts, dts,
1138  pkt->flags & AV_PKT_FLAG_KEY);
1139  av_free(data);
1140  return 0;
1141  }
1142 
1143  if (ts_st->payload_size + size > ts->pes_payload_size ||
1144  (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
1145  av_compare_ts(dts - ts_st->payload_dts, st->time_base,
1146  s->max_delay, AV_TIME_BASE_Q) >= 0)) {
1147  if (ts_st->payload_size) {
1148  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1149  ts_st->payload_pts, ts_st->payload_dts,
1150  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1151  ts_st->payload_size = 0;
1152  }
1153  if (size > ts->pes_payload_size) {
1154  mpegts_write_pes(s, st, buf, size, pts, dts,
1155  pkt->flags & AV_PKT_FLAG_KEY);
1156  av_free(data);
1157  return 0;
1158  }
1159  }
1160 
1161  if (!ts_st->payload_size) {
1162  ts_st->payload_pts = pts;
1163  ts_st->payload_dts = dts;
1164  ts_st->payload_flags = pkt->flags;
1165  }
1166 
1167  memcpy(ts_st->payload + ts_st->payload_size, buf, size);
1168  ts_st->payload_size += size;
1169 
1170  av_free(data);
1171 
1172  return 0;
1173 }
1174 
1176 {
1177  int i;
1178 
1179  /* flush current packets */
1180  for (i = 0; i < s->nb_streams; i++) {
1181  AVStream *st = s->streams[i];
1182  MpegTSWriteStream *ts_st = st->priv_data;
1183  if (ts_st->payload_size > 0) {
1184  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1185  ts_st->payload_pts, ts_st->payload_dts,
1186  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1187  ts_st->payload_size = 0;
1188  }
1189  }
1190  avio_flush(s->pb);
1191 }
1192 
1194 {
1195  if (!pkt) {
1196  mpegts_write_flush(s);
1197  return 1;
1198  } else {
1199  return mpegts_write_packet_internal(s, pkt);
1200  }
1201 }
1202 
1204 {
1205  MpegTSWrite *ts = s->priv_data;
1206  MpegTSService *service;
1207  int i;
1208 
1209  if (s->pb)
1210  mpegts_write_flush(s);
1211 
1212  for (i = 0; i < s->nb_streams; i++) {
1213  AVStream *st = s->streams[i];
1214  MpegTSWriteStream *ts_st = st->priv_data;
1215  av_freep(&ts_st->payload);
1216  if (ts_st->amux) {
1217  avformat_free_context(ts_st->amux);
1218  ts_st->amux = NULL;
1219  }
1220  }
1221 
1222  for (i = 0; i < ts->nb_services; i++) {
1223  service = ts->services[i];
1224  av_freep(&service->provider_name);
1225  av_freep(&service->name);
1226  av_free(service);
1227  }
1228  av_free(ts->services);
1229 
1230  return 0;
1231 }
1232 
1233 static const AVOption options[] = {
1234  { "mpegts_transport_stream_id", "Set transport_stream_id field.",
1235  offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
1236  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1237  { "mpegts_original_network_id", "Set original_network_id field.",
1238  offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
1239  { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1240  { "mpegts_service_id", "Set service_id field.",
1241  offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
1242  { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1243  { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
1244  offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
1245  { .i64 = 0x1000 }, 0x1000, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
1246  { "mpegts_start_pid", "Set the first pid.",
1247  offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
1248  { .i64 = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
1249  {"mpegts_m2ts_mode", "Enable m2ts mode.",
1250  offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_INT,
1251  { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
1252  { "muxrate", NULL,
1253  offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
1254  { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1255  { "pes_payload_size", "Minimum PES packet payload in bytes",
1256  offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT,
1257  { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1258  { "mpegts_flags", "MPEG-TS muxing flags",
1259  offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX,
1260  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1261  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1262  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX,
1263  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1264  { "latm", "Use LATM packetization for AAC",
1265  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX,
1266  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1267  { "system_b", "Conform to System B (DVB) instead of System A (ATSC)",
1268  0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_SYSTEM_B }, 0, INT_MAX,
1269  AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1270  // backward compatibility
1271  { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1272  offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT,
1273  { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1274  { "pcr_period", "PCR retransmission time",
1275  offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
1276  { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1277  { NULL },
1278 };
1279 
1280 static const AVClass mpegts_muxer_class = {
1281  .class_name = "MPEGTS muxer",
1282  .item_name = av_default_item_name,
1283  .option = options,
1284  .version = LIBAVUTIL_VERSION_INT,
1285 };
1286 
1288  .name = "mpegts",
1289  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
1290  .mime_type = "video/MP2T",
1291  .extensions = "ts,m2t,m2ts,mts",
1292  .priv_data_size = sizeof(MpegTSWrite),
1293  .audio_codec = AV_CODEC_ID_MP2,
1294  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1299  .priv_class = &mpegts_muxer_class,
1300 };
#define SDT_PID
Definition: mpegts.h:37
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:104
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define PMT_TID
Definition: mpegts.h:41
int size
#define SECTION_LENGTH
Definition: mpegtsenc.c:101
int pat_packet_period
Definition: mpegtsenc.c:70
MpegTSSection pmt
Definition: mpegtsenc.c:53
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:312
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1155
AVOption.
Definition: opt.h:234
int pcr_packet_count
Definition: mpegtsenc.c:58
int sdt_packet_count
Definition: mpegtsenc.c:67
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:252
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:470
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
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:2986
#define STREAM_TYPE_VIDEO_CAVS
Definition: mpeg.h:58
static const uint8_t frame_size[4]
Definition: g723_1.h:219
int64_t payload_dts
Definition: mpegtsenc.c:206
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
char * name
Definition: mpegtsenc.c:55
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1347
int flag
Definition: cpu.c:35
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define PCR_TIME_BASE
Definition: mpegtsenc.c:36
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:685
void * priv_data
Definition: avformat.h:720
#define AV_DISPOSITION_CLEAN_EFFECTS
stream without voice
Definition: avformat.h:687
#define PCR_RETRANS_TIME
Definition: mpegtsenc.c:197
int64_t first_pcr
Definition: mpegtsenc.c:74
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:429
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1143
#define AV_WB32(p, val)
Definition: intreadwrite.h:246
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
char * provider_name
Definition: mpegtsenc.c:56
int first_pts_check
first pts check needed
Definition: mpegtsenc.c:204
#define SDT_RETRANS_TIME
Definition: mpegtsenc.c:195
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:39
Public dictionary API.
int64_t payload_pts
Definition: mpegtsenc.c:205
uint8_t
AVOptions.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:298
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
#define AV_RB32
Definition: intreadwrite.h:130
void * opaque
Definition: mpegtsenc.c:49
#define DEFAULT_PES_PAYLOAD_SIZE
Definition: mpegtsenc.c:97
int id
Format-specific stream ID.
Definition: avformat.h:712
#define b
Definition: input.c:52
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
#define TS_PACKET_SIZE
Definition: mpegts.h:29
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1193
const char data[16]
Definition: mxf.c:70
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:38
static void mpegts_insert_null_packet(AVFormatContext *s)
Definition: mpegtsenc.c:764
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
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:77
static void mpegts_write_sdt(AVFormatContext *s)
Definition: mpegtsenc.c:419
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:221
static void extend_af(uint8_t *pkt, int size)
Definition: mpegtsenc.c:836
#define MPEGTS_FLAG_AAC_LATM
Definition: mpegtsenc.c:90
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
#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:264
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:959
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
#define STREAM_TYPE_AUDIO_AAC
Definition: mpeg.h:55
struct MpegTSService * service
Definition: mpegtsenc.c:200
static void mpegts_prefix_m2ts_header(AVFormatContext *s)
Definition: mpegtsenc.c:487
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2819
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
static void put16(uint8_t **q_ptr, int val)
Definition: mpegtsenc.c:152
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:161
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
Definition: mpegtsenc.c:805
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
int pes_payload_size
Definition: mpegtsenc.c:76
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
static uint8_t * get_ts_payload_start(uint8_t *pkt)
Definition: mpegtsenc.c:844
#define STREAM_TYPE_VIDEO_DIRAC
Definition: mpegts.h:58
static void set_af_flag(uint8_t *pkt, int flag)
Definition: mpegtsenc.c:820
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:479
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
Definition: mpegtsenc.c:780
int nb_services
Definition: mpegtsenc.c:71
AVRational user_tb
Definition: mpegtsenc.c:210
#define PAT_RETRANS_TIME
Definition: mpegtsenc.c:196
int mux_rate
set to 1 when VBR
Definition: mpegtsenc.c:75
static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition: mpegtsenc.c:1039
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
Definition: mpegtsenc.c:749
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
Definition: utils.c:2515
#define fail()
Definition: checkasm.h:80
#define MPEGTS_FLAG_SYSTEM_B
Definition: mpegtsenc.c:91
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
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:104
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
static void retransmit_si_info(AVFormatContext *s)
Definition: mpegtsenc.c:732
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
#define STREAM_TYPE_AUDIO_AAC_LATM
Definition: mpegts.h:52
const char * name
Definition: qsvenc.c:44
static const AVOption options[]
Definition: mpegtsenc.c:1233
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:236
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:134
char filename[1024]
input or output filename
Definition: avformat.h:1016
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:86
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:241
static void putstr8(uint8_t **q_ptr, const char *str)
Definition: mpegtsenc.c:403
#define STREAM_TYPE_VIDEO_H264
Definition: mpeg.h:57
const char * name
Definition: avformat.h:450
#define DEFAULT_PROVIDER_NAME
Definition: mpegtsenc.c:191
AVFormatContext * ctx
Definition: movenc.c:48
AVOutputFormat ff_mpegts_muxer
Definition: mpegtsenc.c:1287
AVFormatContext * amux
Definition: mpegtsenc.c:209
AVDictionary * metadata
Definition: avformat.h:772
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:104
static int mpegts_write_header(AVFormatContext *s)
Definition: mpegtsenc.c:506
#define MPEGTS_FLAG_REEMIT_PAT_PMT
Definition: mpegtsenc.c:89
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:856
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:198
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define STREAM_TYPE_VIDEO_MPEG4
Definition: mpeg.h:56
int pmt_start_pid
Definition: mpegtsenc.c:82
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1183
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:686
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:48
Stream structure.
Definition: avformat.h:705
int start_pid
Definition: mpegtsenc.c:83
NULL
Definition: eval.c:55
#define av_bswap32
Definition: bswap.h:33
#define DVB_PRIVATE_NETWORK_START
Definition: mpegtsenc.c:40
version
Definition: ffv1enc.c:1091
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:247
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:219
int sdt_packet_period
Definition: mpegtsenc.c:68
AVIOContext * pb
I/O context.
Definition: avformat.h:982
av_default_item_name
Definition: dnxhdenc.c:55
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
const AVClass * av_class
Definition: mpegtsenc.c:63
#define STREAM_TYPE_PRIVATE_DATA
Definition: mpeg.h:54
Describe the class of an AVClass context structure.
Definition: log.h:34
int original_network_id
Definition: mpegtsenc.c:79
rational number numerator/denominator
Definition: rational.h:43
int pcr_packet_period
Definition: mpegtsenc.c:59
int service_id
Definition: mpegtsenc.c:80
byte swapping routines
static int mpegts_write_end(AVFormatContext *s)
Definition: mpegtsenc.c:1203
#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:2594
int transport_stream_id
Definition: mpegtsenc.c:78
MpegTSService ** services
Definition: mpegtsenc.c:66
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
Definition: mpegtsenc.c:499
uint8_t * payload
Definition: mpegtsenc.c:208
static int64_t pts
Global timestamp for the audio frames.
static void mpegts_write_flush(AVFormatContext *s)
Definition: mpegtsenc.c:1175
int m2ts_mode
Definition: mpegtsenc.c:84
const uint8_t * avpriv_find_start_code(const uint8_t *restrict p, const uint8_t *end, uint32_t *restrict state)
Definition: utils.c:2707
#define STREAM_TYPE_VIDEO_HEVC
Definition: mpegts.h:55
int sample_rate
Audio only.
Definition: avcodec.h:3564
#define DEFAULT_SERVICE_NAME
Definition: mpegtsenc.c:192
Main libavformat public API header.
static struct @174 state
common internal api header.
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
Definition: mpegtsenc.c:481
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:761
int pcr_period
Definition: mpegtsenc.c:88
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:31
int den
denominator
Definition: rational.h:45
static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
Definition: mpegtsenc.c:230
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:423
char * value
Definition: dict.h:74
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:968
MpegTSSection pat
Definition: mpegtsenc.c:64
int reemit_pat_pmt
Definition: mpegtsenc.c:86
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:373
static void mpegts_write_pat(AVFormatContext *s)
Definition: mpegtsenc.c:213
MpegTSSection sdt
Definition: mpegtsenc.c:65
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
int pat_packet_count
Definition: mpegtsenc.c:69
static const AVClass mpegts_muxer_class
Definition: mpegtsenc.c:1280
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
#define STREAM_TYPE_AUDIO_MPEG1
Definition: mpeg.h:51
This structure stores compressed data.
Definition: avcodec.h:1323
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
Definition: mpegtsenc.c:104
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:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
#define SDT_TID
Definition: mpegts.h:43
static MpegTSService * mpegts_add_service(MpegTSWrite *ts, int sid, const char *provider_name, const char *name)
Definition: mpegtsenc.c:457