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