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 /* The section length is 12 bits. The first 2 are set to 0, the remaining
126  * 10 bits should not exceed 1021. */
127 #define SECTION_LENGTH 1020
128 
129 /* NOTE: 4 bytes must be left at the end for the crc32 */
130 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
131 {
132  unsigned int crc;
133  unsigned char packet[TS_PACKET_SIZE];
134  const unsigned char *buf_ptr;
135  unsigned char *q;
136  int first, b, len1, left;
137 
138  crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
139  buf[len - 4] = (crc >> 24) & 0xff;
140  buf[len - 3] = (crc >> 16) & 0xff;
141  buf[len - 2] = (crc >> 8) & 0xff;
142  buf[len - 1] = (crc) & 0xff;
143 
144  /* send each packet */
145  buf_ptr = buf;
146  while (len > 0) {
147  first = (buf == buf_ptr);
148  q = packet;
149  *q++ = 0x47;
150  b = (s->pid >> 8);
151  if (first)
152  b |= 0x40;
153  *q++ = b;
154  *q++ = s->pid;
155  s->cc = (s->cc + 1) & 0xf;
156  *q++ = 0x10 | s->cc;
157  if (first)
158  *q++ = 0; /* 0 offset */
159  len1 = TS_PACKET_SIZE - (q - packet);
160  if (len1 > len)
161  len1 = len;
162  memcpy(q, buf_ptr, len1);
163  q += len1;
164  /* add known padding data */
165  left = TS_PACKET_SIZE - (q - packet);
166  if (left > 0)
167  memset(q, 0xff, left);
168 
169  s->write_packet(s, packet);
170 
171  buf_ptr += len1;
172  len -= len1;
173  }
174 }
175 
176 static inline void put16(uint8_t **q_ptr, int val)
177 {
178  uint8_t *q;
179  q = *q_ptr;
180  *q++ = val >> 8;
181  *q++ = val;
182  *q_ptr = q;
183 }
184 
185 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
186  int version, int sec_num, int last_sec_num,
187  uint8_t *buf, int len)
188 {
189  uint8_t section[1024], *q;
190  unsigned int tot_len;
191  /* reserved_future_use field must be set to 1 for SDT */
192  unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
193 
194  tot_len = 3 + 5 + len + 4;
195  /* check if not too big */
196  if (tot_len > 1024)
197  return -1;
198 
199  q = section;
200  *q++ = tid;
201  put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
202  put16(&q, id);
203  *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
204  *q++ = sec_num;
205  *q++ = last_sec_num;
206  memcpy(q, buf, len);
207 
208  mpegts_write_section(s, section, tot_len);
209  return 0;
210 }
211 
212 /*********************************************/
213 /* mpegts writer */
214 
215 #define DEFAULT_PROVIDER_NAME "Libav"
216 #define DEFAULT_SERVICE_NAME "Service01"
217 
218 /* we retransmit the SI info at this rate */
219 #define SDT_RETRANS_TIME 500
220 #define PAT_RETRANS_TIME 100
221 #define PCR_RETRANS_TIME 20
222 
223 typedef struct MpegTSWriteStream {
225  int pid; /* stream associated pid */
226  int cc;
229  int64_t payload_pts;
230  int64_t payload_dts;
235 
237 {
238  MpegTSWrite *ts = s->priv_data;
239  MpegTSService *service;
241  int i;
242 
243  q = data;
244  for(i = 0; i < ts->nb_services; i++) {
245  service = ts->services[i];
246  put16(&q, service->sid);
247  put16(&q, 0xe000 | service->pmt.pid);
248  }
249  mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
250  data, q - data);
251 }
252 
254 {
255  MpegTSWrite *ts = s->priv_data;
256  uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
257  int val, stream_type, i, err = 0;
258 
259  q = data;
260  put16(&q, 0xe000 | service->pcr_pid);
261 
262  program_info_length_ptr = q;
263  q += 2; /* patched after */
264 
265  /* put program info here */
266 
267  val = 0xf000 | (q - program_info_length_ptr - 2);
268  program_info_length_ptr[0] = val >> 8;
269  program_info_length_ptr[1] = val;
270 
271  for(i = 0; i < s->nb_streams; i++) {
272  AVStream *st = s->streams[i];
273  MpegTSWriteStream *ts_st = st->priv_data;
274  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
275 
276  if (q - data > SECTION_LENGTH - 3 - 2 - 6) {
277  err = 1;
278  break;
279  }
280  switch(st->codec->codec_id) {
283  stream_type = STREAM_TYPE_VIDEO_MPEG2;
284  break;
285  case AV_CODEC_ID_MPEG4:
286  stream_type = STREAM_TYPE_VIDEO_MPEG4;
287  break;
288  case AV_CODEC_ID_H264:
289  stream_type = STREAM_TYPE_VIDEO_H264;
290  break;
291  case AV_CODEC_ID_DIRAC:
292  stream_type = STREAM_TYPE_VIDEO_DIRAC;
293  break;
294  case AV_CODEC_ID_MP2:
295  case AV_CODEC_ID_MP3:
296  stream_type = STREAM_TYPE_AUDIO_MPEG1;
297  break;
298  case AV_CODEC_ID_AAC:
300  break;
302  stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
303  break;
304  case AV_CODEC_ID_AC3:
305  stream_type = STREAM_TYPE_AUDIO_AC3;
306  break;
307  default:
308  stream_type = STREAM_TYPE_PRIVATE_DATA;
309  break;
310  }
311  *q++ = stream_type;
312  put16(&q, 0xe000 | ts_st->pid);
313  desc_length_ptr = q;
314  q += 2; /* patched after */
315 
316  /* write optional descriptors here */
317  switch(st->codec->codec_type) {
318  case AVMEDIA_TYPE_AUDIO:
319  if (lang) {
320  char *p;
321  char *next = lang->value;
322  uint8_t *len_ptr;
323 
324  *q++ = 0x0a; /* ISO 639 language descriptor */
325  len_ptr = q++;
326  *len_ptr = 0;
327 
328  for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
329  if (q - data > SECTION_LENGTH - 4) {
330  err = 1;
331  break;
332  }
333  next = strchr(p, ',');
334  if (strlen(p) != 3 && (!next || next != p + 3))
335  continue; /* not a 3-letter code */
336 
337  *q++ = *p++;
338  *q++ = *p++;
339  *q++ = *p++;
340 
342  *q++ = 0x01;
344  *q++ = 0x02;
346  *q++ = 0x03;
347  else
348  *q++ = 0; /* undefined type */
349 
350  *len_ptr += 4;
351  }
352 
353  if (*len_ptr == 0)
354  q -= 2; /* no language codes were written */
355  }
356  break;
358  {
359  const char *language;
360  language = lang && strlen(lang->value)==3 ? lang->value : "eng";
361  *q++ = 0x59;
362  *q++ = 8;
363  *q++ = language[0];
364  *q++ = language[1];
365  *q++ = language[2];
366  *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
367 
368  if (q - data > SECTION_LENGTH - 4) {
369  err = 1;
370  break;
371  }
372  if(st->codec->extradata_size == 4) {
373  memcpy(q, st->codec->extradata, 4);
374  q += 4;
375  } else {
376  put16(&q, 1); /* page id */
377  put16(&q, 1); /* ancillary page id */
378  }
379  }
380  break;
381  case AVMEDIA_TYPE_VIDEO:
382  if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
383  *q++ = 0x05; /*MPEG-2 registration descriptor*/
384  *q++ = 4;
385  *q++ = 'd';
386  *q++ = 'r';
387  *q++ = 'a';
388  *q++ = 'c';
389  }
390  break;
391  }
392 
393  val = 0xf000 | (q - desc_length_ptr - 2);
394  desc_length_ptr[0] = val >> 8;
395  desc_length_ptr[1] = val;
396  }
397 
398  if (err)
399  av_log(s, AV_LOG_ERROR,
400  "The PMT section is too small for stream %d and following.\n"
401  "Try reducing the number of languages in the audio streams "
402  "or the total number of streams.\n",
403  i);
404 
405  mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
406  data, q - data);
407 }
408 
409 /* NOTE: str == NULL is accepted for an empty string */
410 static void putstr8(uint8_t **q_ptr, const char *str)
411 {
412  uint8_t *q;
413  int len;
414 
415  q = *q_ptr;
416  if (!str)
417  len = 0;
418  else
419  len = strlen(str);
420  *q++ = len;
421  memcpy(q, str, len);
422  q += len;
423  *q_ptr = q;
424 }
425 
427 {
428  MpegTSWrite *ts = s->priv_data;
429  MpegTSService *service;
430  uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
431  int i, running_status, free_ca_mode, val;
432 
433  q = data;
434  put16(&q, ts->onid);
435  *q++ = 0xff;
436  for(i = 0; i < ts->nb_services; i++) {
437  service = ts->services[i];
438  put16(&q, service->sid);
439  *q++ = 0xfc | 0x00; /* currently no EIT info */
440  desc_list_len_ptr = q;
441  q += 2;
442  running_status = 4; /* running */
443  free_ca_mode = 0;
444 
445  /* write only one descriptor for the service name and provider */
446  *q++ = 0x48;
447  desc_len_ptr = q;
448  q++;
449  *q++ = 0x01; /* digital television service */
450  putstr8(&q, service->provider_name);
451  putstr8(&q, service->name);
452  desc_len_ptr[0] = q - desc_len_ptr - 1;
453 
454  /* fill descriptor length */
455  val = (running_status << 13) | (free_ca_mode << 12) |
456  (q - desc_list_len_ptr - 2);
457  desc_list_len_ptr[0] = val >> 8;
458  desc_list_len_ptr[1] = val;
459  }
460  mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
461  data, q - data);
462 }
463 
465  int sid,
466  const char *provider_name,
467  const char *name)
468 {
469  MpegTSService *service;
470 
471  service = av_mallocz(sizeof(MpegTSService));
472  if (!service)
473  return NULL;
474  service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
475  service->sid = sid;
476  service->provider_name = av_strdup(provider_name);
477  service->name = av_strdup(name);
478  service->pcr_pid = 0x1fff;
479  dynarray_add(&ts->services, &ts->nb_services, service);
480  return service;
481 }
482 
483 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
484 {
485  AVFormatContext *ctx = s->opaque;
486  avio_write(ctx->pb, packet, TS_PACKET_SIZE);
487 }
488 
490 {
491  MpegTSWrite *ts = s->priv_data;
492  MpegTSWriteStream *ts_st;
493  MpegTSService *service;
494  AVStream *st, *pcr_st = NULL;
495  AVDictionaryEntry *title, *provider;
496  int i, j;
497  const char *service_name;
498  const char *provider_name;
499  int *pids;
500  int ret;
501 
502  if (s->max_delay < 0) /* Not set by the caller */
503  s->max_delay = 0;
504 
505  // round up to a whole number of TS packets
506  ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
507 
508  ts->tsid = ts->transport_stream_id;
509  ts->onid = ts->original_network_id;
510  /* allocate a single DVB service */
511  title = av_dict_get(s->metadata, "service_name", NULL, 0);
512  if (!title)
513  title = av_dict_get(s->metadata, "title", NULL, 0);
514  service_name = title ? title->value : DEFAULT_SERVICE_NAME;
515  provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
516  provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
517  service = mpegts_add_service(ts, ts->service_id, provider_name, service_name);
519  service->pmt.opaque = s;
520  service->pmt.cc = 15;
521 
522  ts->pat.pid = PAT_PID;
523  ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
525  ts->pat.opaque = s;
526 
527  ts->sdt.pid = SDT_PID;
528  ts->sdt.cc = 15;
530  ts->sdt.opaque = s;
531 
532  pids = av_malloc(s->nb_streams * sizeof(*pids));
533  if (!pids)
534  return AVERROR(ENOMEM);
535 
536  /* assign pids to each stream */
537  for(i = 0;i < s->nb_streams; i++) {
538  st = s->streams[i];
539  avpriv_set_pts_info(st, 33, 1, 90000);
540  ts_st = av_mallocz(sizeof(MpegTSWriteStream));
541  if (!ts_st) {
542  ret = AVERROR(ENOMEM);
543  goto fail;
544  }
545  st->priv_data = ts_st;
546  ts_st->payload = av_mallocz(ts->pes_payload_size);
547  if (!ts_st->payload) {
548  ret = AVERROR(ENOMEM);
549  goto fail;
550  }
551  ts_st->service = service;
552  /* MPEG pid values < 16 are reserved. Applications which set st->id in
553  * this range are assigned a calculated pid. */
554  if (st->id < 16) {
555  ts_st->pid = ts->start_pid + i;
556  } else if (st->id < 0x1FFF) {
557  ts_st->pid = st->id;
558  } else {
559  av_log(s, AV_LOG_ERROR, "Invalid stream id %d, must be less than 8191\n", st->id);
560  ret = AVERROR(EINVAL);
561  goto fail;
562  }
563  if (ts_st->pid == service->pmt.pid) {
564  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
565  ret = AVERROR(EINVAL);
566  goto fail;
567  }
568  for (j = 0; j < i; j++)
569  if (pids[j] == ts_st->pid) {
570  av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
571  ret = AVERROR(EINVAL);
572  goto fail;
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  {
588  AVStream *ast;
589  ts_st->amux = avformat_alloc_context();
590  if (!ts_st->amux) {
591  ret = AVERROR(ENOMEM);
592  goto fail;
593  }
594  ts_st->amux->oformat = av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts", NULL, NULL);
595  if (!ts_st->amux->oformat) {
596  ret = AVERROR(EINVAL);
597  goto fail;
598  }
599  ast = avformat_new_stream(ts_st->amux, NULL);
600  ret = avcodec_copy_context(ast->codec, st->codec);
601  if (ret != 0)
602  goto fail;
603  ret = avformat_write_header(ts_st->amux, NULL);
604  if (ret < 0)
605  goto fail;
606  }
607  }
608 
609  av_free(pids);
610 
611  /* if no video stream, use the first stream as PCR */
612  if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
613  pcr_st = s->streams[0];
614  ts_st = pcr_st->priv_data;
615  service->pcr_pid = ts_st->pid;
616  }
617 
618  if (ts->mux_rate > 1) {
619  service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
620  (TS_PACKET_SIZE * 8 * 1000);
622  (TS_PACKET_SIZE * 8 * 1000);
624  (TS_PACKET_SIZE * 8 * 1000);
625 
627  } else {
628  /* Arbitrary values, PAT/PMT could be written on key frames */
629  ts->sdt_packet_period = 200;
630  ts->pat_packet_period = 40;
631  if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
632  if (!pcr_st->codec->frame_size) {
633  av_log(s, AV_LOG_WARNING, "frame size not set\n");
634  service->pcr_packet_period =
635  pcr_st->codec->sample_rate/(10*512);
636  } else {
637  service->pcr_packet_period =
638  pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
639  }
640  } else {
641  // max delta PCR 0.1s
642  service->pcr_packet_period =
643  pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
644  }
645  }
646 
647  // output a PCR as soon as possible
648  service->pcr_packet_count = service->pcr_packet_period;
651 
652  if (ts->mux_rate == 1)
653  av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
654  else
655  av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
656  av_log(s, AV_LOG_VERBOSE, "pcr every %d pkts, "
657  "sdt every %d, pat/pmt every %d pkts\n",
658  service->pcr_packet_period,
660 
661  avio_flush(s->pb);
662 
663  return 0;
664 
665  fail:
666  av_free(pids);
667  for(i = 0;i < s->nb_streams; i++) {
668  MpegTSWriteStream *ts_st;
669  st = s->streams[i];
670  ts_st = st->priv_data;
671  if (ts_st) {
672  av_freep(&ts_st->payload);
673  if (ts_st->amux) {
674  avformat_free_context(ts_st->amux);
675  ts_st->amux = NULL;
676  }
677  }
678  av_freep(&st->priv_data);
679  }
680  return ret;
681 }
682 
683 /* send SDT, PAT and PMT tables regulary */
685 {
686  MpegTSWrite *ts = s->priv_data;
687  int i;
688 
689  if (++ts->sdt_packet_count == ts->sdt_packet_period) {
690  ts->sdt_packet_count = 0;
691  mpegts_write_sdt(s);
692  }
693  if (++ts->pat_packet_count == ts->pat_packet_period) {
694  ts->pat_packet_count = 0;
695  mpegts_write_pat(s);
696  for(i = 0; i < ts->nb_services; i++) {
697  mpegts_write_pmt(s, ts->services[i]);
698  }
699  }
700 }
701 
702 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
703 {
704  return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
705  ts->first_pcr;
706 }
707 
708 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
709 {
710  int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
711 
712  *buf++ = pcr_high >> 25;
713  *buf++ = pcr_high >> 17;
714  *buf++ = pcr_high >> 9;
715  *buf++ = pcr_high >> 1;
716  *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
717  *buf++ = pcr_low;
718 
719  return 6;
720 }
721 
722 /* Write a single null transport stream packet */
724 {
725  uint8_t *q;
726  uint8_t buf[TS_PACKET_SIZE];
727 
728  q = buf;
729  *q++ = 0x47;
730  *q++ = 0x00 | 0x1f;
731  *q++ = 0xff;
732  *q++ = 0x10;
733  memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
734  avio_write(s->pb, buf, TS_PACKET_SIZE);
735 }
736 
737 /* Write a single transport stream packet with a PCR and no payload */
739 {
740  MpegTSWrite *ts = s->priv_data;
741  MpegTSWriteStream *ts_st = st->priv_data;
742  uint8_t *q;
743  uint8_t buf[TS_PACKET_SIZE];
744 
745  q = buf;
746  *q++ = 0x47;
747  *q++ = ts_st->pid >> 8;
748  *q++ = ts_st->pid;
749  *q++ = 0x20 | ts_st->cc; /* Adaptation only */
750  /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
751  *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
752  *q++ = 0x10; /* Adaptation flags: PCR present */
753 
754  /* PCR coded into 6 bytes */
755  q += write_pcr_bits(q, get_pcr(ts, s->pb));
756 
757  /* stuffing bytes */
758  memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
759  avio_write(s->pb, buf, TS_PACKET_SIZE);
760 }
761 
762 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
763 {
764  int val;
765 
766  val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
767  *q++ = val;
768  val = (((pts >> 15) & 0x7fff) << 1) | 1;
769  *q++ = val >> 8;
770  *q++ = val;
771  val = (((pts) & 0x7fff) << 1) | 1;
772  *q++ = val >> 8;
773  *q++ = val;
774 }
775 
776 /* Set an adaptation field flag in an MPEG-TS packet*/
777 static void set_af_flag(uint8_t *pkt, int flag)
778 {
779  // expect at least one flag to set
780  assert(flag);
781 
782  if ((pkt[3] & 0x20) == 0) {
783  // no AF yet, set adaptation field flag
784  pkt[3] |= 0x20;
785  // 1 byte length, no flags
786  pkt[4] = 1;
787  pkt[5] = 0;
788  }
789  pkt[5] |= flag;
790 }
791 
792 /* Extend the adaptation field by size bytes */
793 static void extend_af(uint8_t *pkt, int size)
794 {
795  // expect already existing adaptation field
796  assert(pkt[3] & 0x20);
797  pkt[4] += size;
798 }
799 
800 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
802 {
803  if (pkt[3] & 0x20)
804  return pkt + 5 + pkt[4];
805  else
806  return pkt + 4;
807 }
808 
809 /* Add a pes header to the front of payload, and segment into an integer number of
810  * ts packets. The final ts packet is padded using an over-sized adaptation header
811  * to exactly fill the last ts packet.
812  * NOTE: 'payload' contains a complete PES payload.
813  */
815  const uint8_t *payload, int payload_size,
816  int64_t pts, int64_t dts, int key)
817 {
818  MpegTSWriteStream *ts_st = st->priv_data;
819  MpegTSWrite *ts = s->priv_data;
820  uint8_t buf[TS_PACKET_SIZE];
821  uint8_t *q;
822  int val, is_start, len, header_len, write_pcr, private_code, flags;
823  int afc_len, stuffing_len;
824  int64_t pcr = -1; /* avoid warning */
825  int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
826 
827  is_start = 1;
828  while (payload_size > 0) {
830 
831  write_pcr = 0;
832  if (ts_st->pid == ts_st->service->pcr_pid) {
833  if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
834  ts_st->service->pcr_packet_count++;
835  if (ts_st->service->pcr_packet_count >=
836  ts_st->service->pcr_packet_period) {
837  ts_st->service->pcr_packet_count = 0;
838  write_pcr = 1;
839  }
840  }
841 
842  if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
843  (dts - get_pcr(ts, s->pb)/300) > delay) {
844  /* pcr insert gets priority over null packet insert */
845  if (write_pcr)
846  mpegts_insert_pcr_only(s, st);
847  else
849  continue; /* recalculate write_pcr and possibly retransmit si_info */
850  }
851 
852  /* prepare packet header */
853  q = buf;
854  *q++ = 0x47;
855  val = (ts_st->pid >> 8);
856  if (is_start)
857  val |= 0x40;
858  *q++ = val;
859  *q++ = ts_st->pid;
860  ts_st->cc = (ts_st->cc + 1) & 0xf;
861  *q++ = 0x10 | ts_st->cc; // payload indicator + CC
862  if (key && is_start && pts != AV_NOPTS_VALUE) {
863  // set Random Access for key frames
864  if (ts_st->pid == ts_st->service->pcr_pid)
865  write_pcr = 1;
866  set_af_flag(buf, 0x40);
867  q = get_ts_payload_start(buf);
868  }
869  if (write_pcr) {
870  set_af_flag(buf, 0x10);
871  q = get_ts_payload_start(buf);
872  // add 11, pcr references the last byte of program clock reference base
873  if (ts->mux_rate > 1)
874  pcr = get_pcr(ts, s->pb);
875  else
876  pcr = (dts - delay)*300;
877  if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
878  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
879  extend_af(buf, write_pcr_bits(q, pcr));
880  q = get_ts_payload_start(buf);
881  }
882  if (is_start) {
883  int pes_extension = 0;
884  /* write PES header */
885  *q++ = 0x00;
886  *q++ = 0x00;
887  *q++ = 0x01;
888  private_code = 0;
889  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
890  if (st->codec->codec_id == AV_CODEC_ID_DIRAC) {
891  *q++ = 0xfd;
892  } else
893  *q++ = 0xe0;
894  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
895  (st->codec->codec_id == AV_CODEC_ID_MP2 ||
896  st->codec->codec_id == AV_CODEC_ID_MP3 ||
897  st->codec->codec_id == AV_CODEC_ID_AAC)) {
898  *q++ = 0xc0;
899  } else {
900  *q++ = 0xbd;
901  if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
902  private_code = 0x20;
903  }
904  }
905  header_len = 0;
906  flags = 0;
907  if (pts != AV_NOPTS_VALUE) {
908  header_len += 5;
909  flags |= 0x80;
910  }
911  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
912  header_len += 5;
913  flags |= 0x40;
914  }
915  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
916  st->codec->codec_id == AV_CODEC_ID_DIRAC) {
917  /* set PES_extension_flag */
918  pes_extension = 1;
919  flags |= 0x01;
920 
921  /*
922  * One byte for PES2 extension flag +
923  * one byte for extension length +
924  * one byte for extension id
925  */
926  header_len += 3;
927  }
928  len = payload_size + header_len + 3;
929  if (private_code != 0)
930  len++;
931  if (len > 0xffff)
932  len = 0;
933  *q++ = len >> 8;
934  *q++ = len;
935  val = 0x80;
936  /* data alignment indicator is required for subtitle data */
938  val |= 0x04;
939  *q++ = val;
940  *q++ = flags;
941  *q++ = header_len;
942  if (pts != AV_NOPTS_VALUE) {
943  write_pts(q, flags >> 6, pts);
944  q += 5;
945  }
946  if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
947  write_pts(q, 1, dts);
948  q += 5;
949  }
950  if (pes_extension && st->codec->codec_id == AV_CODEC_ID_DIRAC) {
951  flags = 0x01; /* set PES_extension_flag_2 */
952  *q++ = flags;
953  *q++ = 0x80 | 0x01; /* marker bit + extension length */
954  /*
955  * Set the stream id extension flag bit to 0 and
956  * write the extended stream id
957  */
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) {
1012  av_log(s, AV_LOG_WARNING, "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1013  ts->reemit_pat_pmt = 0;
1015  }
1016 
1017  if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1018  ts->pat_packet_count = ts->pat_packet_period - 1;
1019  ts->sdt_packet_count = ts->sdt_packet_period - 1;
1021  }
1022 
1023  if (pkt->pts != AV_NOPTS_VALUE)
1024  pts = pkt->pts + delay;
1025  if (pkt->dts != AV_NOPTS_VALUE)
1026  dts = pkt->dts + delay;
1027 
1028  if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1029  av_log(s, AV_LOG_ERROR, "first pts value must set\n");
1030  return AVERROR(EINVAL);
1031  }
1032  ts_st->first_pts_check = 0;
1033 
1034  if (st->codec->codec_id == AV_CODEC_ID_H264) {
1035  const uint8_t *p = buf, *buf_end = p+size;
1036  uint32_t state = -1;
1037 
1038  if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
1039  av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1040  "no startcode found, use -bsf h264_mp4toannexb\n");
1041  return AVERROR(EINVAL);
1042  }
1043 
1044  do {
1045  p = avpriv_mpv_find_start_code(p, buf_end, &state);
1046  av_dlog(s, "nal %d\n", state & 0x1f);
1047  } while (p < buf_end && (state & 0x1f) != 9 &&
1048  (state & 0x1f) != 5 && (state & 0x1f) != 1);
1049 
1050  if ((state & 0x1f) != 9) { // AUD NAL
1051  data = av_malloc(pkt->size+6);
1052  if (!data)
1053  return AVERROR(ENOMEM);
1054  memcpy(data+6, pkt->data, pkt->size);
1055  AV_WB32(data, 0x00000001);
1056  data[4] = 0x09;
1057  data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1058  buf = data;
1059  size = pkt->size+6;
1060  }
1061  } else if (st->codec->codec_id == AV_CODEC_ID_AAC) {
1062  if (pkt->size < 2) {
1063  av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1064  return AVERROR(EINVAL);
1065  }
1066  if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1067  int ret;
1068  AVPacket pkt2;
1069 
1070  if (!ts_st->amux) {
1071  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1072  "and extradata missing\n");
1073  return AVERROR(EINVAL);
1074  }
1075 
1076  av_init_packet(&pkt2);
1077  pkt2.data = pkt->data;
1078  pkt2.size = pkt->size;
1079  ret = avio_open_dyn_buf(&ts_st->amux->pb);
1080  if (ret < 0)
1081  return AVERROR(ENOMEM);
1082 
1083  ret = av_write_frame(ts_st->amux, &pkt2);
1084  if (ret < 0) {
1085  avio_close_dyn_buf(ts_st->amux->pb, &data);
1086  ts_st->amux->pb = NULL;
1087  av_free(data);
1088  return ret;
1089  }
1090  size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1091  ts_st->amux->pb = NULL;
1092  buf = data;
1093  }
1094  }
1095 
1096  if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
1097  // for video and subtitle, write a single pes packet
1098  mpegts_write_pes(s, st, buf, size, pts, dts, pkt->flags & AV_PKT_FLAG_KEY);
1099  av_free(data);
1100  return 0;
1101  }
1102 
1103  if (ts_st->payload_size + size > ts->pes_payload_size) {
1104  if (ts_st->payload_size) {
1105  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1106  ts_st->payload_pts, ts_st->payload_dts,
1107  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1108  ts_st->payload_size = 0;
1109  }
1110  if (size > ts->pes_payload_size) {
1111  mpegts_write_pes(s, st, buf, size, pts, dts,
1112  pkt->flags & AV_PKT_FLAG_KEY);
1113  av_free(data);
1114  return 0;
1115  }
1116  }
1117 
1118  if (!ts_st->payload_size) {
1119  ts_st->payload_pts = pts;
1120  ts_st->payload_dts = dts;
1121  ts_st->payload_flags = pkt->flags;
1122  }
1123 
1124  memcpy(ts_st->payload + ts_st->payload_size, buf, size);
1125  ts_st->payload_size += size;
1126 
1127  av_free(data);
1128 
1129  return 0;
1130 }
1131 
1133 {
1134  int i;
1135 
1136  /* flush current packets */
1137  for(i = 0; i < s->nb_streams; i++) {
1138  AVStream *st = s->streams[i];
1139  MpegTSWriteStream *ts_st = st->priv_data;
1140  if (ts_st->payload_size > 0) {
1141  mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1142  ts_st->payload_pts, ts_st->payload_dts,
1143  ts_st->payload_flags & AV_PKT_FLAG_KEY);
1144  ts_st->payload_size = 0;
1145  }
1146  }
1147  avio_flush(s->pb);
1148 }
1149 
1151 {
1152  if (!pkt) {
1153  mpegts_write_flush(s);
1154  return 1;
1155  } else {
1156  return mpegts_write_packet_internal(s, pkt);
1157  }
1158 }
1159 
1161 {
1162  MpegTSWrite *ts = s->priv_data;
1163  MpegTSService *service;
1164  int i;
1165 
1166  mpegts_write_flush(s);
1167 
1168  for(i = 0; i < s->nb_streams; i++) {
1169  AVStream *st = s->streams[i];
1170  MpegTSWriteStream *ts_st = st->priv_data;
1171  av_freep(&ts_st->payload);
1172  if (ts_st->amux) {
1173  avformat_free_context(ts_st->amux);
1174  ts_st->amux = NULL;
1175  }
1176  }
1177 
1178  for(i = 0; i < ts->nb_services; i++) {
1179  service = ts->services[i];
1180  av_freep(&service->provider_name);
1181  av_freep(&service->name);
1182  av_free(service);
1183  }
1184  av_free(ts->services);
1185 
1186  return 0;
1187 }
1188 
1190  .name = "mpegts",
1191  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
1192  .mime_type = "video/x-mpegts",
1193  .extensions = "ts,m2t",
1194  .priv_data_size = sizeof(MpegTSWrite),
1195  .audio_codec = AV_CODEC_ID_MP2,
1196  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1201  .priv_class = &mpegts_muxer_class,
1202 };
#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
#define SECTION_LENGTH
Definition: mpegtsenc.c:127
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:230
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:221
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:228
#define SDT_RETRANS_TIME
Definition: mpegtsenc.c:219
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:229
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:1150
const char data[16]
Definition: mxf.c:66
static void mpegts_insert_null_packet(AVFormatContext *s)
Definition: mpegtsenc.c:723
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:426
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:793
#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:224
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:176
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:185
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
Definition: mpegtsenc.c:762
#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:801
#define STREAM_TYPE_VIDEO_DIRAC
Definition: mpegts.h:56
static void set_af_flag(uint8_t *pkt, int flag)
Definition: mpegtsenc.c:777
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:738
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:220
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:1000
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
Definition: mpegtsenc.c:708
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:684
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:410
#define STREAM_TYPE_VIDEO_H264
Definition: mpeg.h:56
const char * name
Definition: avformat.h:376
#define DEFAULT_PROVIDER_NAME
Definition: mpegtsenc.c:215
AVOutputFormat ff_mpegts_muxer
Definition: mpegtsenc.c:1189
AVFormatContext * amux
Definition: mpegtsenc.c:233
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:489
#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:814
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:1160
#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:483
uint8_t * payload
Definition: mpegtsenc.c:232
static uint32_t state
Definition: trasher.c:27
static void mpegts_write_flush(AVFormatContext *s)
Definition: mpegtsenc.c:1132
struct MpegTSWriteStream MpegTSWriteStream
#define DEFAULT_SERVICE_NAME
Definition: mpegtsenc.c:216
Main libavformat public API header.
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
Definition: mpegtsenc.c:702
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:253
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:236
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:130
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:464