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