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