FFmpeg
Loading...
Searching...
No Matches
mpegtsenc.c
Go to the documentation of this file.
1/*
2 * MPEG-2 transport stream (aka DVB) muxer
3 * Copyright (c) 2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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/avassert.h"
23#include "libavutil/bswap.h"
24#include "libavutil/crc.h"
25#include "libavutil/dict.h"
28#include "libavutil/mem.h"
29#include "libavutil/opt.h"
30
33#include "libavcodec/defs.h"
34#include "libavcodec/h264.h"
36#include "libavcodec/vvc.h"
38
39#include "avformat.h"
40#include "avio_internal.h"
41#include "internal.h"
42#include "mpegts.h"
43#include "mux.h"
44
45#define PCR_TIME_BASE 27000000
46
47/* write DVB SI sections */
48
49#define DVB_PRIVATE_NETWORK_START 0xff01
50
51/*********************************************/
52/* mpegts section writer */
53
54typedef struct MpegTSSection {
55 int pid;
56 int cc;
58 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
59 void *opaque;
62
63typedef struct MpegTSService {
64 MpegTSSection pmt; /* MPEG-2 PMT table context */
65 int sid; /* service ID */
66 uint8_t name[256];
67 uint8_t provider_name[256];
71
72// service_type values as defined in ETSI 300 468
73enum {
82};
83typedef struct MpegTSWrite {
85 MpegTSSection pat; /* MPEG-2 PAT table */
86 MpegTSSection sdt; /* MPEG-2 SDT table context */
87 MpegTSSection nit; /* MPEG-2 NIT table context */
90 int64_t sdt_period; /* SDT period in PCR time base */
91 int64_t pat_period; /* PAT/PMT period in PCR time base */
92 int64_t nit_period; /* NIT period in PCR time base */
97 int mux_rate; ///< set to 1 when VBR
100
105
113
115#define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
116#define MPEGTS_FLAG_AAC_LATM 0x02
117#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
118#define MPEGTS_FLAG_SYSTEM_B 0x08
119#define MPEGTS_FLAG_DISCONT 0x10
120#define MPEGTS_FLAG_NIT 0x20
121#define MPEGTS_FLAG_OMIT_RAI 0x40
122 int flags;
131
132 uint8_t provider_name[256];
133
135
136 int pcr_pid; ///< user-specified separate PCR PID (-1 = use video PID)
137 int64_t pcr_stream_pcr_period; ///< PCR period for the dedicated stream
138 int64_t pcr_stream_last_pcr; ///< last PCR sent on the dedicated PID
140
141/* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
142#define DEFAULT_PES_HEADER_FREQ 16
143#define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
144
145/* The section length is 12 bits. The first 2 are set to 0, the remaining
146 * 10 bits should not exceed 1021. */
147#define SECTION_LENGTH 1020
148
149/* NOTE: 4 bytes must be left at the end for the crc32 */
150static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
151{
152 unsigned int crc;
153 unsigned char packet[TS_PACKET_SIZE];
154 const unsigned char *buf_ptr;
155 unsigned char *q;
156 int first, b, len1, left;
157
159 -1, buf, len - 4));
160
161 buf[len - 4] = (crc >> 24) & 0xff;
162 buf[len - 3] = (crc >> 16) & 0xff;
163 buf[len - 2] = (crc >> 8) & 0xff;
164 buf[len - 1] = crc & 0xff;
165
166 /* send each packet */
167 buf_ptr = buf;
168 while (len > 0) {
169 first = buf == buf_ptr;
170 q = packet;
171 *q++ = SYNC_BYTE;
172 b = s->pid >> 8;
173 if (first)
174 b |= 0x40;
175 *q++ = b;
176 *q++ = s->pid;
177 s->cc = (s->cc + 1) & 0xf;
178 *q++ = 0x10 | s->cc;
179 if (s->discontinuity) {
180 q[-1] |= 0x20;
181 *q++ = 1;
182 *q++ = 0x80;
183 s->discontinuity = 0;
184 }
185 if (first)
186 *q++ = 0; /* 0 offset */
187 len1 = TS_PACKET_SIZE - (q - packet);
188 if (len1 > len)
189 len1 = len;
190 memcpy(q, buf_ptr, len1);
191 q += len1;
192 /* add known padding data */
193 left = TS_PACKET_SIZE - (q - packet);
194 if (left > 0)
195 memset(q, STUFFING_BYTE, left);
196
197 s->write_packet(s, packet);
198
199 buf_ptr += len1;
200 len -= len1;
201 }
202}
203
204static inline void put16(uint8_t **q_ptr, int val)
205{
206 uint8_t *q;
207 q = *q_ptr;
208 *q++ = val >> 8;
209 *q++ = val;
210 *q_ptr = q;
211}
212
213static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
214 int version, int sec_num, int last_sec_num,
215 uint8_t *buf, int len)
216{
217 uint8_t section[1024], *q;
218 unsigned int tot_len;
219 /* reserved_future_use field must be set to 1 for SDT and NIT */
220 unsigned int flags = (tid == SDT_TID || tid == NIT_TID) ? 0xf000 : 0xb000;
221
222 tot_len = 3 + 5 + len + 4;
223 /* check if not too big */
224 if (tot_len > 1024)
225 return AVERROR_INVALIDDATA;
226
227 q = section;
228 *q++ = tid;
229 put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
230 put16(&q, id);
231 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
232 *q++ = sec_num;
233 *q++ = last_sec_num;
234 memcpy(q, buf, len);
235
236 mpegts_write_section(s, section, tot_len);
237 return 0;
238}
239
240/*********************************************/
241/* mpegts writer */
242
243#define DEFAULT_PROVIDER_NAME "FFmpeg"
244#define DEFAULT_SERVICE_NAME "Service"
245
246/* we retransmit the SI info at this rate */
247#define SDT_RETRANS_TIME 500
248#define PAT_RETRANS_TIME 100
249#define PCR_RETRANS_TIME 20
250#define NIT_RETRANS_TIME 500
251
275
277{
278 MpegTSWrite *ts = s->priv_data;
279 MpegTSService *service;
280 uint8_t data[SECTION_LENGTH], *q;
281 int i;
282
283 q = data;
284 if (ts->flags & MPEGTS_FLAG_NIT) {
285 put16(&q, 0x0000);
286 put16(&q, NIT_PID);
287 }
288 for (i = 0; i < ts->nb_services; i++) {
289 service = ts->services[i];
290 put16(&q, service->sid);
291 put16(&q, 0xe000 | service->pmt.pid);
292 }
294 data, q - data);
295}
296
297static void putbuf(uint8_t **q_ptr, const uint8_t *buf, size_t len)
298{
299 memcpy(*q_ptr, buf, len);
300 *q_ptr += len;
301}
302
303static int put_arib_caption_descriptor(AVFormatContext *s, uint8_t **q_ptr,
304 AVCodecParameters *codecpar)
305{
306 uint8_t stream_identifier;
307 uint16_t data_component_id;
308 uint8_t *q = *q_ptr;
309
310 switch (codecpar->profile) {
312 stream_identifier = 0x30;
313 data_component_id = 0x0008;
314 break;
316 stream_identifier = 0x87;
317 data_component_id = 0x0012;
318 break;
319 default:
321 "Unset/unknown ARIB caption profile %d utilized!\n",
322 codecpar->profile);
323 return AVERROR_INVALIDDATA;
324 }
325
326 // stream_identifier_descriptor
327 *q++ = STREAM_IDENTIFIER_DESCRIPTOR; // descriptor_tag
328 *q++ = 1; // descriptor_length
329 *q++ = stream_identifier; // component_tag: stream_identifier
330
331 // data_component_descriptor, defined in ARIB STD-B10, part 2, 6.2.20
332 *q++ = DATA_COMPONENT_DESCRIPTOR; // descriptor_tag: ARIB data coding type descriptor
333 *q++ = 3; // descriptor_length
334 put16(&q, data_component_id); // data_component_id
335 // additional_arib_caption_info: defined in ARIB STD-B24, fascicle 1, Part 3, 9.6.1
336 // Here we utilize a pre-defined set of values defined in ARIB TR-B14,
337 // Fascicle 2, 4.2.8.5 for PMT usage, with the reserved bits in the middle
338 // set to 1 (as that is what every broadcaster seems to be doing in
339 // production).
340 *q++ = 0x3D; // DMF('0011'), Reserved('11'), Timing('01')
341
342 *q_ptr = q;
343
344 return 0;
345}
346
347static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
348{
349 uint8_t *q = *q_ptr;
351 *q++ = 4;
352 *q++ = tag;
353 *q++ = tag >> 8;
354 *q++ = tag >> 16;
355 *q++ = tag >> 24;
356 *q_ptr = q;
357}
358
360{
361 MpegTSWrite *ts = s->priv_data;
362 MpegTSWriteStream *ts_st = st->priv_data;
363 int stream_type;
364
365 switch (st->codecpar->codec_id) {
368 stream_type = STREAM_TYPE_VIDEO_MPEG2;
369 break;
371 stream_type = STREAM_TYPE_VIDEO_MPEG4;
372 break;
373 case AV_CODEC_ID_H264:
374 stream_type = STREAM_TYPE_VIDEO_H264;
375 break;
376 case AV_CODEC_ID_HEVC:
377 stream_type = STREAM_TYPE_VIDEO_HEVC;
378 break;
379 case AV_CODEC_ID_VVC:
380 stream_type = STREAM_TYPE_VIDEO_VVC;
381 break;
382 case AV_CODEC_ID_CAVS:
383 stream_type = STREAM_TYPE_VIDEO_CAVS;
384 break;
385 case AV_CODEC_ID_AVS2:
386 stream_type = STREAM_TYPE_VIDEO_AVS2;
387 break;
388 case AV_CODEC_ID_AVS3:
389 stream_type = STREAM_TYPE_VIDEO_AVS3;
390 break;
392 stream_type = STREAM_TYPE_VIDEO_DIRAC;
393 break;
394 case AV_CODEC_ID_VC1:
395 stream_type = STREAM_TYPE_VIDEO_VC1;
396 break;
397 case AV_CODEC_ID_MP2:
398 case AV_CODEC_ID_MP3:
399 if ( st->codecpar->sample_rate > 0
400 && st->codecpar->sample_rate < 32000) {
401 stream_type = STREAM_TYPE_AUDIO_MPEG2;
402 } else {
403 stream_type = STREAM_TYPE_AUDIO_MPEG1;
404 }
405 break;
406 case AV_CODEC_ID_AAC:
407 stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
410 break;
412 stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
413 break;
414 case AV_CODEC_ID_AC3:
415 stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
418 break;
419 case AV_CODEC_ID_EAC3:
420 stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
423 break;
424 case AV_CODEC_ID_DTS:
425 stream_type = STREAM_TYPE_BLURAY_AUDIO_DTS; // should be STREAM_TYPE_PRIVATE_DATA (ETSI TS 101 154), needs a DTS_descriptor() (ETSI EN 300 468)
426 break;
428 stream_type = STREAM_TYPE_BLURAY_AUDIO_TRUEHD; // should be STREAM_TYPE_PRIVATE_DATA (ETSI TS 101 154), needs a DTS-HD_descriptor() (ETSI EN 300 468)
429 break;
430 case AV_CODEC_ID_OPUS:
431 stream_type = STREAM_TYPE_PRIVATE_DATA;
432 break;
434 stream_type = STREAM_TYPE_METADATA;
435 break;
437 stream_type = STREAM_TYPE_PRIVATE_DATA;
438 break;
442 stream_type = STREAM_TYPE_PRIVATE_DATA;
443 break;
446 stream_type = STREAM_TYPE_METADATA;
447 } else {
448 stream_type = STREAM_TYPE_PRIVATE_DATA;
449 }
450 break;
451 default:
453 "Stream %d, codec %s, is muxed as a private data stream "
454 "and may not be recognized upon reading.\n", st->index,
456 stream_type = STREAM_TYPE_PRIVATE_DATA;
457 break;
458 }
459
460 return stream_type;
461}
462
464{
465 int stream_type;
466 MpegTSWriteStream *ts_st = st->priv_data;
467
468 switch (st->codecpar->codec_id) {
470 stream_type = STREAM_TYPE_VIDEO_MPEG2;
471 break;
472 case AV_CODEC_ID_H264:
473 stream_type = STREAM_TYPE_VIDEO_H264;
474 break;
475 case AV_CODEC_ID_VC1:
476 stream_type = STREAM_TYPE_VIDEO_VC1;
477 break;
478 case AV_CODEC_ID_HEVC:
479 stream_type = STREAM_TYPE_VIDEO_HEVC;
480 break;
483 break;
484 case AV_CODEC_ID_AC3:
485 stream_type = STREAM_TYPE_BLURAY_AUDIO_AC3;
486 break;
487 case AV_CODEC_ID_DTS:
488 stream_type = (st->codecpar->ch_layout.nb_channels > 6) ?
491 break;
494 break;
495 case AV_CODEC_ID_EAC3:
496 stream_type = STREAM_TYPE_BLURAY_AUDIO_EAC3;
497 break;
500 break;
503 break;
504 default:
506 "Stream %d, codec %s, is muxed as a private data stream "
507 "and may not be recognized upon reading.\n", st->index,
509 stream_type = STREAM_TYPE_PRIVATE_DATA;
510 break;
511 }
512
513 return stream_type;
514}
515
517{
518 MpegTSWrite *ts = s->priv_data;
519 uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
520 int val, stream_type, i, err = 0;
521
522 q = data;
523 put16(&q, 0xe000 | service->pcr_pid);
524
525 program_info_length_ptr = q;
526 q += 2; /* patched after */
527
528 /* put program info here */
529 if (ts->m2ts_mode) {
530 put_registration_descriptor(&q, MKTAG('H', 'D', 'M', 'V'));
531 *q++ = 0x88; // descriptor_tag - hdmv_copy_control_descriptor
532 *q++ = 0x04; // descriptor_length
533 put16(&q, 0x0fff); // CA_System_ID
534 *q++ = 0xfc; // private_data_byte
535 *q++ = 0xfc; // private_data_byte
536 }
537
538 val = 0xf000 | (q - program_info_length_ptr - 2);
539 program_info_length_ptr[0] = val >> 8;
540 program_info_length_ptr[1] = val;
541
542 for (i = 0; i < s->nb_streams; i++) {
543 AVStream *st = s->streams[i];
544 MpegTSWriteStream *ts_st = st->priv_data;
545 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
546 const char default_language[] = "und";
547 const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
549
550 if (s->nb_programs) {
551 int k, found = 0;
552 AVProgram *program = service->program;
553
554 for (k = 0; k < program->nb_stream_indexes; k++)
555 if (program->stream_index[k] == i) {
556 found = 1;
557 break;
558 }
559
560 if (!found)
561 continue;
562 }
563
564 if (q - data > SECTION_LENGTH - 32) {
565 err = 1;
566 break;
567 }
568
569 stream_type = ts->m2ts_mode ? get_m2ts_stream_type(s, st) : get_dvb_stream_type(s, st);
570
571 *q++ = stream_type;
572 put16(&q, 0xe000 | ts_st->pid);
573 desc_length_ptr = q;
574 q += 2; /* patched after */
575
576 /* write optional descriptors here */
577 switch (st->codecpar->codec_type) {
580 put_registration_descriptor(&q, MKTAG('A', 'C', '-', '3'));
582 put_registration_descriptor(&q, MKTAG('E', 'A', 'C', '3'));
583 if (ts->flags & MPEGTS_FLAG_SYSTEM_B) {
584 if (codec_id == AV_CODEC_ID_AC3) {
585 DVBAC3Descriptor *dvb_ac3_desc = ts_st->dvb_ac3_desc;
586
587 *q++= AC3_DESCRIPTOR; // AC3 descriptor see A038 DVB SI
588 if (dvb_ac3_desc) {
589 int len = 1 +
590 !!(dvb_ac3_desc->component_type_flag) +
591 !!(dvb_ac3_desc->bsid_flag) +
592 !!(dvb_ac3_desc->mainid_flag) +
593 !!(dvb_ac3_desc->asvc_flag);
594
595 *q++ = len;
596 *q++ = dvb_ac3_desc->component_type_flag << 7 | dvb_ac3_desc->bsid_flag << 6 |
597 dvb_ac3_desc->mainid_flag << 5 | dvb_ac3_desc->asvc_flag << 4;
598
599 if (dvb_ac3_desc->component_type_flag) *q++ = dvb_ac3_desc->component_type;
600 if (dvb_ac3_desc->bsid_flag) *q++ = dvb_ac3_desc->bsid;
601 if (dvb_ac3_desc->mainid_flag) *q++ = dvb_ac3_desc->mainid;
602 if (dvb_ac3_desc->asvc_flag) *q++ = dvb_ac3_desc->asvc;
603 } else {
604 *q++=1; // 1 byte, all flags sets to 0
605 *q++=0; // omit all fields...
606 }
607 } else if (codec_id == AV_CODEC_ID_EAC3) {
608 *q++= ENHANCED_AC3_DESCRIPTOR; // EAC3 descriptor see A038 DVB SI
609 *q++=1; // 1 byte, all flags sets to 0
610 *q++=0; // omit all fields...
611 }
612 }
614 put_registration_descriptor(&q, MKTAG('B', 'S', 'S', 'D'));
615 if (codec_id == AV_CODEC_ID_OPUS) {
616 int ch = st->codecpar->ch_layout.nb_channels;
617
618 /* 6 bytes registration descriptor, 4 bytes Opus audio descriptor */
619 if (q - data > SECTION_LENGTH - 6 - 4) {
620 err = 1;
621 break;
622 }
623
624 put_registration_descriptor(&q, MKTAG('O', 'p', 'u', 's'));
625
627 *q++ = 2;
628 *q++ = 0x80;
629
630 if (st->codecpar->extradata && st->codecpar->extradata_size >= 19) {
631 if (st->codecpar->extradata[18] == 0 && ch <= 2) {
632 /* RTP mapping family */
633 *q++ = ch;
634 } else if (st->codecpar->extradata[18] == 1 && ch <= 8 &&
635 st->codecpar->extradata_size >= 21 + ch) {
636 static const uint8_t coupled_stream_counts[9] = {
637 1, 0, 1, 1, 2, 2, 2, 3, 3
638 };
639 static const uint8_t channel_map_a[8][8] = {
640 {0},
641 {0, 1},
642 {0, 2, 1},
643 {0, 1, 2, 3},
644 {0, 4, 1, 2, 3},
645 {0, 4, 1, 2, 3, 5},
646 {0, 4, 1, 2, 3, 5, 6},
647 {0, 6, 1, 2, 3, 4, 5, 7},
648 };
649 static const uint8_t channel_map_b[8][8] = {
650 {0},
651 {0, 1},
652 {0, 1, 2},
653 {0, 1, 2, 3},
654 {0, 1, 2, 3, 4},
655 {0, 1, 2, 3, 4, 5},
656 {0, 1, 2, 3, 4, 5, 6},
657 {0, 1, 2, 3, 4, 5, 6, 7},
658 };
659 /* Vorbis mapping family */
660
661 if (st->codecpar->extradata[19] == ch - coupled_stream_counts[ch] &&
662 st->codecpar->extradata[20] == coupled_stream_counts[ch] &&
663 memcmp(&st->codecpar->extradata[21], channel_map_a[ch - 1], ch) == 0) {
664 *q++ = ch;
665 } else if (ch >= 2 && st->codecpar->extradata[19] == ch &&
666 st->codecpar->extradata[20] == 0 &&
667 memcmp(&st->codecpar->extradata[21], channel_map_b[ch - 1], ch) == 0) {
668 *q++ = ch | 0x80;
669 } else {
670 /* Unsupported, could write an extended descriptor here */
671 av_log(s, AV_LOG_ERROR, "Unsupported Opus Vorbis-style channel mapping");
672 *q++ = 0xff;
673 }
674 } else {
675 /* Unsupported */
676 av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping for family %d", st->codecpar->extradata[18]);
677 *q++ = 0xff;
678 }
679 } else if (ch <= 2) {
680 /* Assume RTP mapping family */
681 *q++ = ch;
682 } else {
683 /* Unsupported */
684 av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping");
685 *q++ = 0xff;
686 }
687 }
688
689 if (language != default_language ||
693 const char *p, *next;
694 uint8_t *len_ptr;
695
697 len_ptr = q++;
698 *len_ptr = 0;
699
700 for (p = next = language; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
701 if (q - data > SECTION_LENGTH - 4) {
702 err = 1;
703 break;
704 }
705 next = strchr(p, ',');
706 if (strlen(p) != 3 && (!next || next != p + 3))
707 continue; /* not a 3-letter code */
708
709 *q++ = *p++;
710 *q++ = *p++;
711 *q++ = *p++;
712
714 *q++ = 0x01;
716 *q++ = 0x02;
718 *q++ = 0x03;
719 else
720 *q++ = 0; /* undefined type */
721
722 *len_ptr += 4;
723 }
724
725 if (*len_ptr == 0)
726 q -= 2; /* no language codes were written */
727 }
728 break;
731 uint8_t *len_ptr;
732 int extradata_copied = 0;
733
734 *q++ = SUBTITLING_DESCRIPTOR; /* subtitling_descriptor */
735 len_ptr = q++;
736
737 while (strlen(language) >= 3) {
738 if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
739 err = 1;
740 break;
741 }
742 *q++ = *language++;
743 *q++ = *language++;
744 *q++ = *language++;
745 /* Skip comma */
746 if (*language != '\0')
747 language++;
748
749 if (st->codecpar->extradata_size - extradata_copied >= 5) {
750 *q++ = st->codecpar->extradata[extradata_copied + 4]; /* subtitling_type */
751 memcpy(q, st->codecpar->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
752 extradata_copied += 5;
753 q += 4;
754 } else {
755 /* subtitling_type:
756 * 0x10 - normal with no monitor aspect ratio criticality
757 * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
758 *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
759 if ((st->codecpar->extradata_size == 4) && (extradata_copied == 0)) {
760 /* support of old 4-byte extradata format */
761 memcpy(q, st->codecpar->extradata, 4); /* composition_page_id and ancillary_page_id */
762 extradata_copied += 4;
763 q += 4;
764 } else {
765 put16(&q, 1); /* composition_page_id */
766 put16(&q, 1); /* ancillary_page_id */
767 }
768 }
769 }
770
771 *len_ptr = q - len_ptr - 1;
772 } else if (codec_id == AV_CODEC_ID_DVB_TELETEXT) {
773 uint8_t *len_ptr = NULL;
774 int extradata_copied = 0;
775
776 /* The descriptor tag. teletext_descriptor */
777 *q++ = TELETEXT_DESCRIPTOR;
778 len_ptr = q++;
779
780 while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
781 *q++ = *language++;
782 *q++ = *language++;
783 *q++ = *language++;
784 /* Skip comma */
785 if (*language != '\0')
786 language++;
787
788 if (st->codecpar->extradata_size - 1 > extradata_copied) {
789 memcpy(q, st->codecpar->extradata + extradata_copied, 2);
790 extradata_copied += 2;
791 q += 2;
792 } else {
793 /* The Teletext descriptor:
794 * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
795 * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
796 * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
797 *q++ = 0x08;
798 *q++ = 0x00;
799 }
800 }
801
802 *len_ptr = q - len_ptr - 1;
803 } else if (codec_id == AV_CODEC_ID_ARIB_CAPTION) {
804 if (put_arib_caption_descriptor(s, &q, st->codecpar) < 0)
805 break;
806 }
807 break;
809 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
810 put_registration_descriptor(&q, MKTAG('d', 'r', 'a', 'c'));
811 } else if (stream_type == STREAM_TYPE_VIDEO_VC1) {
812 put_registration_descriptor(&q, MKTAG('V', 'C', '-', '1'));
813 } else if (stream_type == STREAM_TYPE_VIDEO_HEVC && s->strict_std_compliance <= FF_COMPLIANCE_NORMAL) {
814 put_registration_descriptor(&q, MKTAG('H', 'E', 'V', 'C'));
815 } else if (stream_type == STREAM_TYPE_VIDEO_CAVS || stream_type == STREAM_TYPE_VIDEO_AVS2 ||
816 stream_type == STREAM_TYPE_VIDEO_AVS3) {
817 put_registration_descriptor(&q, MKTAG('A', 'V', 'S', 'V'));
818 }
819 break;
822 put_registration_descriptor(&q, MKTAG('K', 'L', 'V', 'A'));
823 } else if (codec_id == AV_CODEC_ID_SMPTE_2038) {
824 put_registration_descriptor(&q, MKTAG('V', 'A', 'N', 'C'));
825 } else if (codec_id == AV_CODEC_ID_TIMED_ID3) {
826 const char *tag = "ID3 ";
827 *q++ = METADATA_DESCRIPTOR;
828 *q++ = 13;
829 put16(&q, 0xffff); /* metadata application format */
830 putbuf(&q, tag, strlen(tag));
831 *q++ = 0xff; /* metadata format */
832 putbuf(&q, tag, strlen(tag));
833 *q++ = 0; /* metadata service ID */
834 *q++ = 0xF; /* metadata_locator_record_flag|MPEG_carriage_flags|reserved */
835 }
836 break;
837 }
838
839 val = 0xf000 | (q - desc_length_ptr - 2);
840 desc_length_ptr[0] = val >> 8;
841 desc_length_ptr[1] = val;
842 }
843
844 if (err)
846 "The PMT section cannot fit stream %d and all following streams.\n"
847 "Try reducing the number of languages in the audio streams "
848 "or the total number of streams.\n", i);
849
850 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
851 data, q - data);
852 return 0;
853}
854
856{
857 MpegTSWrite *ts = s->priv_data;
858 MpegTSService *service;
859 uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
860 int i, running_status, free_ca_mode, val;
861
862 q = data;
863 put16(&q, ts->original_network_id);
864 *q++ = 0xff;
865 for (i = 0; i < ts->nb_services; i++) {
866 service = ts->services[i];
867 put16(&q, service->sid);
868 *q++ = 0xfc | 0x00; /* currently no EIT info */
869 desc_list_len_ptr = q;
870 q += 2;
871 running_status = 4; /* running */
872 free_ca_mode = 0;
873
874 /* write only one descriptor for the service name and provider */
875 *q++ = SERVICE_DESCRIPTOR;
876 desc_len_ptr = q;
877 q++;
878 *q++ = ts->service_type;
879 putbuf(&q, service->provider_name, service->provider_name[0] + 1);
880 putbuf(&q, service->name, service->name[0] + 1);
881 desc_len_ptr[0] = q - desc_len_ptr - 1;
882
883 /* fill descriptor length */
884 val = (running_status << 13) | (free_ca_mode << 12) |
885 (q - desc_list_len_ptr - 2);
886 desc_list_len_ptr[0] = val >> 8;
887 desc_list_len_ptr[1] = val;
888 }
890 data, q - data);
891}
892
894{
895 MpegTSWrite *ts = s->priv_data;
896 uint8_t data[SECTION_LENGTH], *q, *desc_len_ptr, *loop_len_ptr;
897
898 q = data;
899
900 //network_descriptors_length
901 put16(&q, 0xf000 | (ts->provider_name[0] + 2));
902
903 //network_name_descriptor
905 putbuf(&q, ts->provider_name, ts->provider_name[0] + 1);
906
907 //transport_stream_loop_length
908 loop_len_ptr = q;
909 q += 2;
910
911 put16(&q, ts->transport_stream_id);
912 put16(&q, ts->original_network_id);
913
914 //transport_descriptors_length
915 desc_len_ptr = q;
916 q += 2;
917
918 //service_list_descriptor
920 *q++ = 3 * ts->nb_services;
921 for (int i = 0; i < ts->nb_services; i++) {
922 put16(&q, ts->services[i]->sid);
923 *q++ = ts->service_type;
924 }
925
926 //calculate lengths
927 put16(&desc_len_ptr, 0xf000 | q - (desc_len_ptr + 2));
928 put16(&loop_len_ptr, 0xf000 | q - (loop_len_ptr + 2));
929
931 data, q - data);
932}
933
934/* This stores a string in buf with the correct encoding and also sets the
935 * first byte as the length. !str is accepted for an empty string.
936 * If the string is already encoded, invalid UTF-8 or has no multibyte sequence
937 * then we keep it as is, otherwise we signal UTF-8 encoding. */
938static int encode_str8(uint8_t *buf, const char *str)
939{
940 size_t str_len;
941 if (!str)
942 str = "";
943 str_len = strlen(str);
944 if (str[0] && (unsigned)str[0] >= 0x20) { /* Make sure the string is not already encoded. */
945 const uint8_t *q = str;
946 int has_multibyte = 0;
947 while (*q) {
948 uint32_t code;
949 GET_UTF8(code, *q++, goto invalid;) /* Is it valid UTF-8? */
950 has_multibyte |= (code > 127); /* Does it have multibyte UTF-8 chars in it? */
951 }
952 if (has_multibyte) { /* If we have multibyte chars and valid UTF-8, then encode as such! */
953 if (str_len > 254)
954 return AVERROR(EINVAL);
955 buf[0] = str_len + 1;
956 buf[1] = 0x15;
957 memcpy(&buf[2], str, str_len);
958 return 0;
959 }
960 }
961invalid:
962 /* Otherwise let's just encode the string as is! */
963 if (str_len > 255)
964 return AVERROR(EINVAL);
965 buf[0] = str_len;
966 memcpy(&buf[1], str, str_len);
967 return 0;
968}
969
970static int64_t get_pcr(const MpegTSWrite *ts)
971{
972 return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
973 ts->first_pcr;
974}
975
976static void write_packet(AVFormatContext *s, const uint8_t *packet)
977{
978 MpegTSWrite *ts = s->priv_data;
979 if (ts->m2ts_mode) {
980 int64_t pcr = get_pcr(s->priv_data);
981 uint32_t tp_extra_header = pcr % 0x3fffffff;
982 tp_extra_header = AV_RB32(&tp_extra_header);
983 avio_write(s->pb, (unsigned char *) &tp_extra_header,
984 sizeof(tp_extra_header));
985 }
986 avio_write(s->pb, packet, TS_PACKET_SIZE);
988}
989
990static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
991{
992 AVFormatContext *ctx = s->opaque;
993 write_packet(ctx, packet);
994}
995
997 const AVDictionary *metadata,
998 AVProgram *program)
999{
1000 MpegTSWrite *ts = s->priv_data;
1001 MpegTSService *service;
1002 AVDictionaryEntry *title, *provider;
1003 char default_service_name[32];
1004 const char *service_name;
1005 const char *provider_name;
1006
1007 title = av_dict_get(metadata, "service_name", NULL, 0);
1008 if (!title)
1009 title = av_dict_get(metadata, "title", NULL, 0);
1010 snprintf(default_service_name, sizeof(default_service_name), "%s%02d", DEFAULT_SERVICE_NAME, ts->nb_services + 1);
1011 service_name = title ? title->value : default_service_name;
1012 provider = av_dict_get(metadata, "service_provider", NULL, 0);
1013 provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
1014
1015 service = av_mallocz(sizeof(MpegTSService));
1016 if (!service)
1017 return NULL;
1018 service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
1019 service->sid = sid;
1020 service->pcr_pid = 0x1fff;
1021 if (encode_str8(service->provider_name, provider_name) < 0 ||
1022 encode_str8(service->name, service_name) < 0) {
1023 av_log(s, AV_LOG_ERROR, "Too long service or provider name\n");
1024 goto fail;
1025 }
1026 ts->sdt.remaining -= 10 + service->provider_name[0] + service->name[0];
1027 if (ts->sdt.remaining < 0)
1028 goto fail;
1029
1030 if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
1031 goto fail;
1032
1034 service->pmt.opaque = s;
1035 service->pmt.cc = 15;
1037 service->program = program;
1038
1039 return service;
1040fail:
1041 av_free(service);
1042 return NULL;
1043}
1044
1046{
1047 MpegTSWrite *ts = s->priv_data;
1048 MpegTSWriteStream *ts_st = pcr_st->priv_data;
1049
1050 if (ts->mux_rate > 1 || ts->pcr_period_ms >= 0) {
1051 int pcr_period_ms = ts->pcr_period_ms == -1 ? PCR_RETRANS_TIME : ts->pcr_period_ms;
1052 ts_st->pcr_period = av_rescale(pcr_period_ms, PCR_TIME_BASE, 1000);
1053 } else {
1054 /* By default, for VBR we select the highest multiple of frame duration which is less than 100 ms. */
1055 int64_t frame_period = 0;
1056 if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1058 if (!frame_size) {
1059 av_log(s, AV_LOG_WARNING, "frame size not set\n");
1060 frame_size = 512;
1061 }
1063 } else if (pcr_st->avg_frame_rate.num) {
1064 frame_period = av_rescale_rnd(pcr_st->avg_frame_rate.den, PCR_TIME_BASE, pcr_st->avg_frame_rate.num, AV_ROUND_UP);
1065 }
1066 if (frame_period > 0 && frame_period <= PCR_TIME_BASE / 10)
1067 ts_st->pcr_period = frame_period * (PCR_TIME_BASE / 10 / frame_period);
1068 else
1069 ts_st->pcr_period = 1;
1070 }
1071
1072 // output a PCR as soon as possible
1073 ts_st->last_pcr = ts->first_pcr - ts_st->pcr_period;
1074}
1075
1077{
1078 MpegTSWrite *ts = s->priv_data;
1079
1080 for (int i = 0; i < ts->nb_services; i++) {
1081 MpegTSService *service = ts->services[i];
1082 AVStream *pcr_st = NULL;
1083 AVProgram *program = service->program;
1084 int nb_streams = program ? program->nb_stream_indexes : s->nb_streams;
1085
1086 for (int j = 0; j < nb_streams; j++) {
1087 AVStream *st = s->streams[program ? program->stream_index[j] : j];
1088 if (!pcr_st ||
1090 {
1091 pcr_st = st;
1092 }
1093 }
1094
1095 if (ts->pcr_pid >= FIRST_OTHER_PID) {
1096 int pcr_period_ms = ts->pcr_period_ms == -1 ? PCR_RETRANS_TIME : ts->pcr_period_ms;
1097 service->pcr_pid = ts->pcr_pid;
1098 ts->pcr_stream_pcr_period = av_rescale(pcr_period_ms, PCR_TIME_BASE, 1000);
1100 av_log(s, AV_LOG_VERBOSE, "service %i using separate PCR pid=%d, pcr_period=%"PRId64"ms\n",
1101 service->sid, ts->pcr_pid, (int64_t)pcr_period_ms);
1102 } else if (pcr_st) {
1103 MpegTSWriteStream *ts_st = pcr_st->priv_data;
1104 service->pcr_pid = ts_st->pid;
1106 av_log(s, AV_LOG_VERBOSE, "service %i using PCR in pid=%i, pcr_period=%"PRId64"ms\n",
1107 service->sid, service->pcr_pid, av_rescale(ts_st->pcr_period, 1000, PCR_TIME_BASE));
1108 }
1109 }
1110}
1111
1113{
1114 MpegTSWrite *ts = s->priv_data;
1115 AVDictionaryEntry *provider;
1116 const char *provider_name;
1117 int i, j;
1118 int ret;
1119
1120 if (ts->m2ts_mode == -1) {
1121 if (av_match_ext(s->url, "m2ts")) {
1122 ts->m2ts_mode = 1;
1123 } else {
1124 ts->m2ts_mode = 0;
1125 }
1126 }
1127
1132
1133 if (ts->m2ts_mode) {
1135 if (s->nb_programs > 1) {
1136 av_log(s, AV_LOG_ERROR, "Only one program is allowed in m2ts mode!\n");
1137 return AVERROR(EINVAL);
1138 }
1139 }
1140
1141 if (s->max_delay < 0) /* Not set by the caller */
1142 s->max_delay = 0;
1143
1144 if (ts->pcr_pid != -1 && ts->pcr_pid < FIRST_OTHER_PID) {
1145 av_log(s, AV_LOG_ERROR, "Invalid PCR PID %d, must be in range [%d, %d]\n",
1147 return AVERROR(EINVAL);
1148 }
1149
1150 // round up to a whole number of TS packets
1151 ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
1152
1153 ts->sdt.remaining = SECTION_LENGTH - 3;
1154
1155 if (!s->nb_programs) {
1156 /* allocate a single DVB service */
1157 if (!mpegts_add_service(s, ts->service_id, s->metadata, NULL))
1158 return AVERROR(ENOMEM);
1159 } else {
1160 for (i = 0; i < s->nb_programs; i++) {
1161 AVProgram *program = s->programs[i];
1162 if (!mpegts_add_service(s, program->id, program->metadata, program))
1163 return AVERROR(ENOMEM);
1164 }
1165 }
1166
1167 ts->pat.pid = PAT_PID;
1168 /* Initialize at 15 so that it wraps and is equal to 0 for the
1169 * first packet we write. */
1170 ts->pat.cc = 15;
1173 ts->pat.opaque = s;
1174
1175 ts->sdt.pid = SDT_PID;
1176 ts->sdt.cc = 15;
1179 ts->sdt.opaque = s;
1180
1181 ts->nit.pid = NIT_PID;
1182 ts->nit.cc = 15;
1185 ts->nit.opaque = s;
1186
1187 ts->pkt = ffformatcontext(s)->pkt;
1188
1189 /* assign pids to each stream */
1190 for (i = 0; i < s->nb_streams; i++) {
1191 AVStream *st = s->streams[i];
1192 MpegTSWriteStream *ts_st;
1193
1194 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
1195 if (!ts_st) {
1196 return AVERROR(ENOMEM);
1197 }
1198 st->priv_data = ts_st;
1199
1200 avpriv_set_pts_info(st, 33, 1, 90000);
1201
1202 ts_st->payload = av_mallocz(ts->pes_payload_size);
1203 if (!ts_st->payload) {
1204 return AVERROR(ENOMEM);
1205 }
1206
1207 /* MPEG pid values < 16 are reserved. Applications which set st->id in
1208 * this range are assigned a calculated pid. */
1209 if (st->id < 16) {
1210 if (ts->m2ts_mode) {
1211 switch (st->codecpar->codec_type) {
1212 case AVMEDIA_TYPE_VIDEO:
1213 ts_st->pid = ts->m2ts_video_pid++;
1214 break;
1215 case AVMEDIA_TYPE_AUDIO:
1216 ts_st->pid = ts->m2ts_audio_pid++;
1217 break;
1219 switch (st->codecpar->codec_id) {
1221 ts_st->pid = ts->m2ts_pgssub_pid++;
1222 break;
1224 ts_st->pid = ts->m2ts_textsub_pid++;
1225 break;
1226 }
1227 break;
1228 }
1229 if (ts->m2ts_video_pid > M2TS_VIDEO_PID + 1 ||
1233 ts_st->pid < 16) {
1234 av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index);
1235 return AVERROR(EINVAL);
1236 }
1237 } else {
1238 ts_st->pid = ts->start_pid + i;
1239 }
1240 } else {
1241 ts_st->pid = st->id;
1242 }
1243 if (ts_st->pid >= 0x1FFF) {
1245 "Invalid stream id %d, must be less than 8191\n", st->id);
1246 return AVERROR(EINVAL);
1247 }
1248 for (j = 0; j < ts->nb_services; j++) {
1249 if (ts->services[j]->pmt.pid > LAST_OTHER_PID) {
1251 "Invalid PMT PID %d, must be less than %d\n", ts->services[j]->pmt.pid, LAST_OTHER_PID + 1);
1252 return AVERROR(EINVAL);
1253 }
1254 if (ts_st->pid == ts->services[j]->pmt.pid) {
1255 av_log(s, AV_LOG_ERROR, "PID %d cannot be both elementary and PMT PID\n", ts_st->pid);
1256 return AVERROR(EINVAL);
1257 }
1258 }
1259 for (j = 0; j < i; j++) {
1260 MpegTSWriteStream *ts_st_prev = s->streams[j]->priv_data;
1261 if (ts_st_prev->pid == ts_st->pid) {
1262 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
1263 return AVERROR(EINVAL);
1264 }
1265 }
1266 ts_st->payload_pts = AV_NOPTS_VALUE;
1267 ts_st->payload_dts = AV_NOPTS_VALUE;
1268 ts_st->cc = 15;
1270 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1271 st->codecpar->extradata_size > 0) {
1272 AVStream *ast;
1273 ts_st->amux = avformat_alloc_context();
1274 if (!ts_st->amux) {
1275 return AVERROR(ENOMEM);
1276 }
1277 ts_st->amux->oformat =
1278 av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
1279 NULL, NULL);
1280 if (!ts_st->amux->oformat) {
1281 return AVERROR(EINVAL);
1282 }
1283 if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
1284 return AVERROR(ENOMEM);
1285 }
1286 ret = avcodec_parameters_copy(ast->codecpar, st->codecpar);
1287 if (ret != 0)
1288 return ret;
1289 ast->time_base = st->time_base;
1290 ret = avformat_write_header(ts_st->amux, NULL);
1291 if (ret < 0)
1292 return ret;
1293 }
1294 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
1296 }
1297 }
1298
1299 if (ts->copyts < 1)
1300 ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
1301
1303
1310
1311 /* assign provider name */
1312 provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
1313 provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
1314 if (encode_str8(ts->provider_name, provider_name) < 0) {
1315 av_log(s, AV_LOG_ERROR, "Too long provider name\n");
1316 return AVERROR(EINVAL);
1317 }
1318
1319 if (ts->mux_rate == 1)
1320 av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
1321 else
1322 av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
1324 "sdt every %"PRId64" ms, pat/pmt every %"PRId64" ms",
1326 av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
1327 if (ts->flags & MPEGTS_FLAG_NIT)
1328 av_log(s, AV_LOG_VERBOSE, ", nit every %"PRId64" ms", av_rescale(ts->nit_period, 1000, PCR_TIME_BASE));
1329 av_log(s, AV_LOG_VERBOSE, "\n");
1330
1331 return 0;
1332}
1333
1334/* send SDT, NIT, PAT and PMT tables regularly */
1335static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int force_nit, int64_t pcr)
1336{
1337 MpegTSWrite *ts = s->priv_data;
1338 int i;
1339
1340 if ((pcr != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
1341 (pcr != AV_NOPTS_VALUE && pcr - ts->last_sdt_ts >= ts->sdt_period) ||
1342 force_sdt
1343 ) {
1344 if (pcr != AV_NOPTS_VALUE)
1345 ts->last_sdt_ts = FFMAX(pcr, ts->last_sdt_ts);
1347 }
1348 if ((pcr != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
1349 (pcr != AV_NOPTS_VALUE && pcr - ts->last_pat_ts >= ts->pat_period) ||
1350 force_pat) {
1351 if (pcr != AV_NOPTS_VALUE)
1352 ts->last_pat_ts = FFMAX(pcr, ts->last_pat_ts);
1354 for (i = 0; i < ts->nb_services; i++)
1356 }
1357 if ((pcr != AV_NOPTS_VALUE && ts->last_nit_ts == AV_NOPTS_VALUE) ||
1358 (pcr != AV_NOPTS_VALUE && pcr - ts->last_nit_ts >= ts->nit_period) ||
1359 force_nit
1360 ) {
1361 if (pcr != AV_NOPTS_VALUE)
1362 ts->last_nit_ts = FFMAX(pcr, ts->last_nit_ts);
1363 if (ts->flags & MPEGTS_FLAG_NIT)
1365 }
1366}
1367
1368static int write_pcr_bits(uint8_t *buf, int64_t pcr)
1369{
1371
1372 *buf++ = pcr_high >> 25;
1373 *buf++ = pcr_high >> 17;
1374 *buf++ = pcr_high >> 9;
1375 *buf++ = pcr_high >> 1;
1376 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
1377 *buf++ = pcr_low;
1378
1379 return 6;
1380}
1381
1382/* Write a single null transport stream packet */
1384{
1385 uint8_t *q;
1386 uint8_t buf[TS_PACKET_SIZE];
1387
1388 q = buf;
1389 *q++ = SYNC_BYTE;
1390 *q++ = 0x00 | (NULL_PID >> 8);
1391 *q++ = NULL_PID & 0xff;
1392 *q++ = 0x10;
1393 memset(q, STUFFING_BYTE, TS_PACKET_SIZE - (q - buf)); /* data_bytes may be assigned any value */
1394 write_packet(s, buf);
1395}
1396
1397/* Write a single transport stream packet with a PCR and no payload */
1398static void mpegts_insert_pcr_only_pid(AVFormatContext *s, int pid, int cc,
1399 int discontinuity)
1400{
1401 MpegTSWrite *ts = s->priv_data;
1402 uint8_t *q;
1403 uint8_t buf[TS_PACKET_SIZE];
1404
1405 q = buf;
1406 *q++ = SYNC_BYTE;
1407 *q++ = (pid >> 8) & 0x1f;
1408 *q++ = pid & 0xff;
1409 *q++ = 0x20 | (cc & 0xf); /* Adaptation only */
1410 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
1411 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
1412 *q++ = 0x10; /* Adaptation flags: PCR present */
1413 if (discontinuity)
1414 q[-1] |= 0x80;
1415
1416 /* PCR coded into 6 bytes */
1417 q += write_pcr_bits(q, get_pcr(ts));
1418
1419 /* stuffing bytes */
1420 memset(q, STUFFING_BYTE, TS_PACKET_SIZE - (q - buf));
1421 write_packet(s, buf);
1422}
1423
1425{
1426 MpegTSWriteStream *ts_st = st->priv_data;
1427 mpegts_insert_pcr_only_pid(s, ts_st->pid, ts_st->cc, ts_st->discontinuity);
1428 ts_st->discontinuity = 0;
1429}
1430
1431static void write_pts(uint8_t *q, int fourbits, int64_t pts)
1432{
1433 int val;
1434
1435 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
1436 *q++ = val;
1437 val = (((pts >> 15) & 0x7fff) << 1) | 1;
1438 *q++ = val >> 8;
1439 *q++ = val;
1440 val = (((pts) & 0x7fff) << 1) | 1;
1441 *q++ = val >> 8;
1442 *q++ = val;
1443}
1444
1445/* Set an adaptation field flag in an MPEG-TS packet*/
1446static void set_af_flag(uint8_t *pkt, int flag)
1447{
1448 // expect at least one flag to set
1450
1451 if ((pkt[3] & 0x20) == 0) {
1452 // no AF yet, set adaptation field flag
1453 pkt[3] |= 0x20;
1454 // 1 byte length, no flags
1455 pkt[4] = 1;
1456 pkt[5] = 0;
1457 }
1458 pkt[5] |= flag;
1459}
1460
1461/* Extend the adaptation field by size bytes */
1462static void extend_af(uint8_t *pkt, int size)
1463{
1464 // expect already existing adaptation field
1465 av_assert0(pkt[3] & 0x20);
1466 pkt[4] += size;
1467}
1468
1469/* Get a pointer to MPEG-TS payload (right after TS packet header) */
1470static uint8_t *get_ts_payload_start(uint8_t *pkt)
1471{
1472 if (pkt[3] & 0x20)
1473 return pkt + 5 + pkt[4];
1474 else
1475 return pkt + 4;
1476}
1477
1478static int get_pes_stream_id(AVFormatContext *s, AVStream *st, int stream_id, int *async)
1479{
1480 MpegTSWrite *ts = s->priv_data;
1481 *async = 0;
1485 else
1487 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1492 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1494 ts->m2ts_mode) {
1496 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1499 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
1501 stream_id == STREAM_ID_PRIVATE_STREAM_1) /* asynchronous KLV */
1502 *async = 1;
1503 return stream_id != -1 ? stream_id : STREAM_ID_METADATA_STREAM;
1504 } else {
1506 }
1507}
1508
1509/* Add a PES header to the front of the payload, and segment into an integer
1510 * number of TS packets. The final TS packet is padded using an oversized
1511 * adaptation header to exactly fill the last TS packet.
1512 * NOTE: 'payload' contains a complete PES payload. */
1514 const uint8_t *payload, int payload_size,
1515 int64_t pts, int64_t dts, int key, int stream_id)
1516{
1517 MpegTSWriteStream *ts_st = st->priv_data;
1518 MpegTSWrite *ts = s->priv_data;
1519 uint8_t buf[TS_PACKET_SIZE];
1520 uint8_t *q;
1521 int val, is_start, len, header_len, write_pcr, flags;
1522 int afc_len, stuffing_len;
1523 int is_dvb_subtitle = (st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE);
1524 int is_dvb_teletext = (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT);
1525 int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
1526 int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
1527 int force_sdt = 0;
1528 int force_nit = 0;
1529
1530 av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
1532 force_pat = 1;
1533 }
1534
1536 force_pat = 1;
1537 force_sdt = 1;
1538 force_nit = 1;
1540 }
1541
1542 is_start = 1;
1543 while (payload_size > 0) {
1544 int64_t pcr = AV_NOPTS_VALUE;
1545 if (ts->mux_rate > 1)
1546 pcr = get_pcr(ts);
1547 else if (dts != AV_NOPTS_VALUE)
1548 pcr = (dts - delay) * SYSTEM_CLOCK_FREQUENCY_DIVISOR;
1549
1550 retransmit_si_info(s, force_pat, force_sdt, force_nit, pcr);
1551 force_pat = 0;
1552 force_sdt = 0;
1553 force_nit = 0;
1554
1555 write_pcr = 0;
1556 if (ts->mux_rate > 1) {
1557 /* Send PCR packets for all PCR streams if needed */
1558 pcr = get_pcr(ts);
1559 if (ts->pcr_pid >= FIRST_OTHER_PID) {
1560 /* Dedicated PCR PID: send adaptation-only packets on that PID */
1561 if (pcr - ts->pcr_stream_last_pcr >= ts->pcr_stream_pcr_period) {
1564 pcr = get_pcr(ts);
1565 }
1567 } else if (pcr >= ts->next_pcr) {
1568 int64_t next_pcr = INT64_MAX;
1569 for (int i = 0; i < s->nb_streams; i++) {
1570 /* Make the current stream the last, because for that we
1571 * can insert the pcr into the payload later */
1572 int st2_index = i < st->index ? i : (i + 1 == s->nb_streams ? st->index : i + 1);
1573 AVStream *st2 = s->streams[st2_index];
1574 MpegTSWriteStream *ts_st2 = st2->priv_data;
1575 if (ts_st2->pcr_period) {
1576 if (pcr - ts_st2->last_pcr >= ts_st2->pcr_period) {
1577 ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period);
1578 if (st2 != st) {
1580 pcr = get_pcr(ts);
1581 } else {
1582 write_pcr = 1;
1583 }
1584 }
1585 next_pcr = FFMIN(next_pcr, ts_st2->last_pcr + ts_st2->pcr_period);
1586 }
1587 }
1588 ts->next_pcr = next_pcr;
1589 }
1590 if (dts != AV_NOPTS_VALUE && (dts - pcr / SYSTEM_CLOCK_FREQUENCY_DIVISOR) > delay) {
1591 /* pcr insert gets priority over null packet insert */
1592 if (ts->pcr_pid >= FIRST_OTHER_PID) {
1593 int64_t cur_pcr = get_pcr(ts);
1594 if (cur_pcr - ts->pcr_stream_last_pcr >= ts->pcr_stream_pcr_period) {
1597 } else {
1599 }
1600 } else if (write_pcr) {
1602 } else {
1604 }
1605 /* recalculate write_pcr and possibly retransmit si_info */
1606 continue;
1607 }
1608 } else if (ts->pcr_pid >= FIRST_OTHER_PID && pcr != AV_NOPTS_VALUE) {
1609 /* VBR mode with dedicated PCR PID */
1610 if (pcr - ts->pcr_stream_last_pcr >= ts->pcr_stream_pcr_period && is_start) {
1613 }
1614 } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) {
1615 if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) {
1616 ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period);
1617 write_pcr = 1;
1618 }
1619 }
1620
1621 /* prepare packet header */
1622 q = buf;
1623 *q++ = SYNC_BYTE;
1624 val = ts_st->pid >> 8;
1625 if (ts->m2ts_mode && st->codecpar->codec_id == AV_CODEC_ID_AC3)
1626 val |= 0x20;
1627 if (is_start)
1628 val |= 0x40;
1629 *q++ = val;
1630 *q++ = ts_st->pid;
1631 ts_st->cc = (ts_st->cc + 1) & 0xf;
1632 *q++ = 0x10 | ts_st->cc; // payload indicator + CC
1633 if (ts_st->discontinuity) {
1634 set_af_flag(buf, 0x80);
1635 q = get_ts_payload_start(buf);
1636 ts_st->discontinuity = 0;
1637 }
1638 if (!(ts->flags & MPEGTS_FLAG_OMIT_RAI) &&
1639 key && is_start && pts != AV_NOPTS_VALUE &&
1640 !is_dvb_teletext /* adaptation+payload forbidden for teletext (ETSI EN 300 472 V1.3.1 4.1) */) {
1641 // set Random Access for key frames
1642 if (ts_st->pcr_period && ts->pcr_pid < FIRST_OTHER_PID)
1643 write_pcr = 1;
1644 set_af_flag(buf, 0x40);
1645 q = get_ts_payload_start(buf);
1646 }
1647 if (write_pcr) {
1648 set_af_flag(buf, 0x10);
1649 q = get_ts_payload_start(buf);
1650 // add 11, pcr references the last byte of program clock reference base
1651 if (dts != AV_NOPTS_VALUE && dts < pcr / SYSTEM_CLOCK_FREQUENCY_DIVISOR)
1652 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
1653 extend_af(buf, write_pcr_bits(q, pcr));
1654 q = get_ts_payload_start(buf);
1655 }
1656 if (is_start) {
1657 int pes_extension = 0;
1658 int pes_header_stuffing_bytes = 0;
1659 int async;
1660 /* write PES header */
1661 *q++ = 0x00;
1662 *q++ = 0x00;
1663 *q++ = 0x01;
1664 *q++ = stream_id = get_pes_stream_id(s, st, stream_id, &async);
1665 if (async)
1666 pts = dts = AV_NOPTS_VALUE;
1667
1668 header_len = 0;
1669
1670 if (stream_id != STREAM_ID_PROGRAM_STREAM_MAP &&
1671 stream_id != STREAM_ID_PADDING_STREAM &&
1672 stream_id != STREAM_ID_PRIVATE_STREAM_2 &&
1673 stream_id != STREAM_ID_ECM_STREAM &&
1674 stream_id != STREAM_ID_EMM_STREAM &&
1676 stream_id != STREAM_ID_DSMCC_STREAM &&
1677 stream_id != STREAM_ID_TYPE_E_STREAM) {
1678
1679 flags = 0;
1680 if (pts != AV_NOPTS_VALUE) {
1681 header_len += 5;
1682 flags |= 0x80;
1683 }
1684 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1685 header_len += 5;
1686 flags |= 0x40;
1687 }
1690 /* set PES_extension_flag */
1691 pes_extension = 1;
1692 flags |= 0x01;
1693
1694 /* One byte for PES2 extension flag +
1695 * one byte for extension length +
1696 * one byte for extension id */
1697 header_len += 3;
1698 }
1699 /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
1700 * otherwise it will not play sound on blu-ray
1701 */
1702 if (ts->m2ts_mode &&
1705 /* set PES_extension_flag */
1706 pes_extension = 1;
1707 flags |= 0x01;
1708 header_len += 3;
1709 }
1710 if (is_dvb_teletext) {
1711 pes_header_stuffing_bytes = 0x24 - header_len;
1712 header_len = 0x24;
1713 }
1714 len = payload_size + header_len + 3;
1715 /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
1716 if (is_dvb_subtitle) {
1717 len += 3;
1718 payload_size++;
1719 }
1720 if (len > 0xffff)
1721 len = 0;
1723 len = 0;
1724 }
1725 *q++ = len >> 8;
1726 *q++ = len;
1727 val = 0x80;
1728 /* data alignment indicator is required for subtitle and data streams */
1730 val |= 0x04;
1731 *q++ = val;
1732 *q++ = flags;
1733 *q++ = header_len;
1734 if (pts != AV_NOPTS_VALUE) {
1735 write_pts(q, flags >> 6, pts);
1736 q += 5;
1737 }
1738 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1739 write_pts(q, 1, dts);
1740 q += 5;
1741 }
1742 if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
1743 flags = 0x01; /* set PES_extension_flag_2 */
1744 *q++ = flags;
1745 *q++ = 0x80 | 0x01; /* marker bit + extension length */
1746 /* Set the stream ID extension flag bit to 0 and
1747 * write the extended stream ID. */
1748 *q++ = 0x00 | 0x60;
1749 }
1750 /* For Blu-ray AC3 Audio Setting extended flags */
1751 if (ts->m2ts_mode &&
1752 pes_extension &&
1754 flags = 0x01; /* set PES_extension_flag_2 */
1755 *q++ = flags;
1756 *q++ = 0x80 | 0x01; /* marker bit + extension length */
1757 *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1758 }
1759
1760
1761 if (is_dvb_subtitle) {
1762 /* First two fields of DVB subtitles PES data:
1763 * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
1764 * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
1765 *q++ = 0x20;
1766 *q++ = 0x00;
1767 }
1768 if (is_dvb_teletext) {
1769 memset(q, STUFFING_BYTE, pes_header_stuffing_bytes);
1770 q += pes_header_stuffing_bytes;
1771 }
1772 } else {
1773 len = payload_size;
1774 *q++ = len >> 8;
1775 *q++ = len;
1776 }
1777 is_start = 0;
1778 }
1779 /* header size */
1780 header_len = q - buf;
1781 /* data len */
1782 len = TS_PACKET_SIZE - header_len;
1783 if (len > payload_size)
1784 len = payload_size;
1785 stuffing_len = TS_PACKET_SIZE - header_len - len;
1786 if (stuffing_len > 0) {
1787 /* add stuffing with AFC */
1788 if (buf[3] & 0x20) {
1789 /* stuffing already present: increase its size */
1790 afc_len = buf[4] + 1;
1791 memmove(buf + 4 + afc_len + stuffing_len,
1792 buf + 4 + afc_len,
1793 header_len - (4 + afc_len));
1794 buf[4] += stuffing_len;
1795 memset(buf + 4 + afc_len, STUFFING_BYTE, stuffing_len);
1796 } else {
1797 /* add stuffing */
1798 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1799 buf[3] |= 0x20;
1800 buf[4] = stuffing_len - 1;
1801 if (stuffing_len >= 2) {
1802 buf[5] = 0x00;
1803 memset(buf + 6, STUFFING_BYTE, stuffing_len - 2);
1804 }
1805 }
1806 }
1807
1808 if (is_dvb_subtitle && payload_size == len) {
1809 memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
1810 buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
1811 } else {
1812 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1813 }
1814
1815 payload += len;
1816 payload_size -= len;
1817 write_packet(s, buf);
1818 }
1819 ts_st->prev_payload_key = key;
1820}
1821
1822static int check_h26x_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt, const char *codec)
1823{
1824 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1825 if (!st->nb_frames) {
1826 av_log(s, AV_LOG_ERROR, "%s bitstream malformed, "
1827 "no startcode found, use the video bitstream filter '%s_mp4toannexb' to fix it "
1828 "('-bsf:v %s_mp4toannexb' option with ffmpeg)\n", codec, codec, codec);
1829 return AVERROR_INVALIDDATA;
1830 }
1831 av_log(s, AV_LOG_WARNING, "%s bitstream error, startcode missing, size %d", codec, pkt->size);
1832 if (pkt->size)
1833 av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
1834 av_log(s, AV_LOG_WARNING, "\n");
1835 }
1836 return 0;
1837}
1838
1840{
1841 return check_h26x_startcode(s, st, pkt, "h264");
1842}
1843
1844/* Based on GStreamer's gst-plugins-base/ext/ogg/gstoggstream.c
1845 * Released under the LGPL v2.1+, written by
1846 * Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
1847 */
1849{
1850 static const int durations[32] = {
1851 480, 960, 1920, 2880, /* Silk NB */
1852 480, 960, 1920, 2880, /* Silk MB */
1853 480, 960, 1920, 2880, /* Silk WB */
1854 480, 960, /* Hybrid SWB */
1855 480, 960, /* Hybrid FB */
1856 120, 240, 480, 960, /* CELT NB */
1857 120, 240, 480, 960, /* CELT NB */
1858 120, 240, 480, 960, /* CELT NB */
1859 120, 240, 480, 960, /* CELT NB */
1860 };
1861 int toc, frame_duration, nframes, duration;
1862
1863 if (pkt->size < 1)
1864 return 0;
1865
1866 toc = pkt->data[0];
1867
1868 frame_duration = durations[toc >> 3];
1869 switch (toc & 3) {
1870 case 0:
1871 nframes = 1;
1872 break;
1873 case 1:
1874 nframes = 2;
1875 break;
1876 case 2:
1877 nframes = 2;
1878 break;
1879 case 3:
1880 if (pkt->size < 2)
1881 return 0;
1882 nframes = pkt->data[1] & 63;
1883 break;
1884 }
1885
1886 duration = nframes * frame_duration;
1887 if (duration > 5760) {
1889 "Opus packet duration > 120 ms, invalid");
1890 return 0;
1891 }
1892
1893 return duration;
1894}
1895
1896static uint8_t *h26x_prefix_aud(const uint8_t *aud, const int aud_size,
1897 const uint8_t *extra_data, const int extra_size, AVPacket *pkt, int *size)
1898{
1899 const int sz = 4; //start code size
1900 uint8_t *data;
1901
1902 if (pkt->size > INT_MAX - sz - aud_size - extra_size)
1903 return NULL;
1904 data = av_malloc(pkt->size + sz + aud_size + extra_size);
1905 if (!data)
1906 return NULL;
1907 AV_WB32(data, 0x00000001);
1908 memcpy(data + sz, aud, aud_size);
1909 memcpy(data + sz + aud_size, extra_data, extra_size);
1910 memcpy(data + sz + aud_size + extra_size, pkt->data, pkt->size);
1911 *size = pkt->size + sz + aud_size + extra_size;
1912 return data;
1913}
1914
1915#define H264_NAL_TYPE(state) (state & 0x1f)
1916#define HEVC_NAL_TYPE(state) ((state & 0x7e) >> 1)
1917#define VVC_NAL_TYPE(state) ((state >> 11) & 0x1f)
1919{
1920 AVStream *st = s->streams[pkt->stream_index];
1921 int size = pkt->size;
1922 const uint8_t *buf = pkt->data;
1923 uint8_t *data = NULL;
1924 MpegTSWrite *ts = s->priv_data;
1925 MpegTSWriteStream *ts_st = st->priv_data;
1926 const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1927 const int64_t max_audio_delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) / 2;
1928 int64_t dts = pkt->dts, pts = pkt->pts;
1929 int opus_samples = 0;
1930 int stream_id = -1;
1931
1932 uint8_t *stream_id_p = av_packet_get_side_data(pkt,
1934 NULL);
1935 if (stream_id_p)
1936 stream_id = stream_id_p[0];
1937
1938 if (!ts->first_dts_checked && dts != AV_NOPTS_VALUE) {
1940 ts->first_dts_checked = 1;
1941 }
1942
1943 if (ts->copyts < 1) {
1944 if (pts != AV_NOPTS_VALUE)
1945 pts += delay;
1946 if (dts != AV_NOPTS_VALUE)
1947 dts += delay;
1948 }
1949
1950 if (!ts_st->first_timestamp_checked && (pts == AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)) {
1951 av_log(s, AV_LOG_ERROR, "first pts and dts value must be set\n");
1952 return AVERROR_INVALIDDATA;
1953 }
1954 ts_st->first_timestamp_checked = 1;
1955
1956 if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1957 const uint8_t *p = buf, *buf_end = p + size;
1958 const uint8_t *found_aud = NULL, *found_aud_end = NULL;
1959 int nal_type;
1960 uint32_t state = -1;
1961 int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
1962 int ret = ff_check_h264_startcode(s, st, pkt);
1963 if (ret < 0)
1964 return ret;
1965
1966 if (extradd && AV_RB24(st->codecpar->extradata) > 1)
1967 extradd = 0;
1968
1969 /* Ensure that all pictures are prefixed with an AUD, and that
1970 * IDR pictures are also prefixed with SPS and PPS. SPS and PPS
1971 * are assumed to be available in 'extradata' if not found in-band. */
1972 do {
1973 p = avpriv_find_start_code(p, buf_end, &state);
1974 nal_type = H264_NAL_TYPE(state);
1975 av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", nal_type);
1976 if (nal_type == H264_NAL_SPS)
1977 extradd = 0;
1978 if (nal_type == H264_NAL_AUD) {
1979 found_aud = p - 4; // start of the 0x000001 start code.
1980 found_aud_end = p + 1; // first byte past the AUD.
1981 if (found_aud < buf)
1982 found_aud = buf;
1983 if (buf_end < found_aud_end)
1984 found_aud_end = buf_end;
1985 }
1986 } while (p < buf_end
1987 && nal_type != H264_NAL_IDR_SLICE
1988 && nal_type != H264_NAL_SLICE
1989 && (extradd > 0 || !found_aud));
1990 if (nal_type != H264_NAL_IDR_SLICE)
1991 extradd = 0;
1992
1993 if (!found_aud) {
1994 /* Prefix 'buf' with the missing AUD, and extradata if needed. */
1995 const uint8_t aud[] = {
1997 0xf0, // any slice type (0xe) + rbsp stop one bit
1998 };
2000 st->codecpar->extradata, extradd, pkt, &size);
2001 if (!data)
2002 return AVERROR(ENOMEM);
2003 } else if (extradd != 0) {
2004 /* Move the AUD up to the beginning of the frame, where the H.264
2005 * spec requires it to appear. Emit the extradata after it. */
2006 PutByteContext pb;
2007 int new_pkt_size;
2008
2009 if (pkt->size > INT_MAX - 1 - extradd)
2010 return AVERROR(ENOMEM);
2011 new_pkt_size = pkt->size + 1 + extradd;
2012 data = av_malloc(new_pkt_size);
2013 if (!data)
2014 return AVERROR(ENOMEM);
2015 bytestream2_init_writer(&pb, data, new_pkt_size);
2016 bytestream2_put_byte(&pb, 0x00);
2017 bytestream2_put_buffer(&pb, found_aud, found_aud_end - found_aud);
2018 bytestream2_put_buffer(&pb, st->codecpar->extradata, extradd);
2019 bytestream2_put_buffer(&pb, pkt->data, found_aud - pkt->data);
2020 bytestream2_put_buffer(&pb, found_aud_end, buf_end - found_aud_end);
2021 av_assert0(new_pkt_size == bytestream2_tell_p(&pb));
2022 buf = data;
2023 size = new_pkt_size;
2024 }
2025 } else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
2026 if (pkt->size < 2) {
2027 av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
2028 return AVERROR_INVALIDDATA;
2029 }
2030 if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
2031 int ret;
2032 AVPacket *pkt2 = ts->pkt;
2033
2034 if (!ts_st->amux) {
2035 av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
2036 "and extradata missing\n");
2037 } else {
2038 av_packet_unref(pkt2);
2039 pkt2->data = pkt->data;
2040 pkt2->size = pkt->size;
2041 av_assert0(pkt->dts != AV_NOPTS_VALUE);
2042 pkt2->dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
2043
2044 ret = avio_open_dyn_buf(&ts_st->amux->pb);
2045 if (ret < 0)
2046 return ret;
2047
2048 ret = av_write_frame(ts_st->amux, pkt2);
2049 if (ret < 0) {
2050 ffio_free_dyn_buf(&ts_st->amux->pb);
2051 return ret;
2052 }
2053 size = avio_close_dyn_buf(ts_st->amux->pb, &data);
2054 ts_st->amux->pb = NULL;
2055 buf = data;
2056 }
2057 }
2058 } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
2059 const uint8_t *p = buf, *buf_end = p + size;
2060 uint32_t state = -1;
2061 int nal_type;
2062 int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
2063 int ret = check_h26x_startcode(s, st, pkt, "hevc");
2064 if (ret < 0)
2065 return ret;
2066
2067 if (extradd && AV_RB24(st->codecpar->extradata) > 1)
2068 extradd = 0;
2069
2070 do {
2071 p = avpriv_find_start_code(p, buf_end, &state);
2072 nal_type = HEVC_NAL_TYPE(state);
2073 av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", nal_type);
2074 if (nal_type == HEVC_NAL_VPS)
2075 extradd = 0;
2076 } while (p < buf_end && nal_type != HEVC_NAL_AUD && nal_type >= HEVC_NAL_VPS);
2077
2078 if (nal_type < HEVC_NAL_BLA_W_LP || nal_type >= HEVC_NAL_RSV_VCL24)
2079 extradd = 0;
2080 if (nal_type != HEVC_NAL_AUD) { // AUD NAL
2081 const uint8_t aud[] = {
2082 (HEVC_NAL_AUD << 1),
2083 0x01,
2084 0x50, // any slice type (0x4) + rbsp stop one bit
2085 };
2087 st->codecpar->extradata, extradd, pkt, &size);
2088 if (!data)
2089 return AVERROR(ENOMEM);
2090 }
2091 } else if (st->codecpar->codec_id == AV_CODEC_ID_VVC) {
2092 const uint8_t *p = buf, *buf_end = p + size;
2093 uint32_t state = -1;
2094 uint32_t nal_type = -1;
2095 int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
2096 int ret = check_h26x_startcode(s, st, pkt, "vvc");
2097 if (ret < 0)
2098 return ret;
2099
2100 if (extradd && AV_RB24(st->codecpar->extradata) > 1)
2101 extradd = 0;
2102
2103 do {
2104 p = avpriv_find_start_code(p, buf_end, &state);
2105 // state contains byte behind start code, p points 2 bytes behind start code
2106 nal_type = VVC_NAL_TYPE(state);
2107 av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", nal_type );
2108 if (nal_type == VVC_VPS_NUT)
2109 extradd = 0;
2110 } while (p < buf_end && nal_type != VVC_AUD_NUT && nal_type >= VVC_OPI_NUT);
2111
2112 if (nal_type >= VVC_OPI_NUT)
2113 extradd = 0;
2114 if (nal_type != VVC_AUD_NUT) { // AUD NAL
2115 const uint8_t aud[] = {
2116 0, // forbidden_zero_bit, nuh_reserved_zero_bit, nuh_layer_id
2117 (VVC_AUD_NUT << 3) | 1, // nal_unit_type, nuh_temporal_id_plus1(1)
2118 (pkt->flags & AV_PKT_FLAG_KEY) << 7 | 0x28, // aud_irap_or_gdr_flag, aud_pic_type(2), rbsp_stop_one_bit
2119 };
2120 buf = data = h26x_prefix_aud(aud, FF_ARRAY_ELEMS(aud), st->codecpar->extradata, extradd, pkt, &size);
2121 if (!data)
2122 return AVERROR(ENOMEM);
2123 }
2124 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
2125 if (pkt->size < 2) {
2126 av_log(s, AV_LOG_ERROR, "Opus packet too short\n");
2127 return AVERROR_INVALIDDATA;
2128 }
2129
2130 /* Add Opus control header */
2131 if ((AV_RB16(pkt->data) >> 5) != 0x3ff) {
2132 uint8_t *side_data;
2133 size_t side_data_size;
2134 int i, n;
2135 int ctrl_header_size;
2136 int trim_start = 0, trim_end = 0;
2137
2138 opus_samples = opus_get_packet_samples(s, pkt);
2139
2140 side_data = av_packet_get_side_data(pkt,
2142 &side_data_size);
2143
2144 if (side_data && side_data_size >= 10) {
2145 trim_end = AV_RL32(side_data + 4) * 48000 / st->codecpar->sample_rate;
2146 }
2147
2148 if (pkt->size > INT_MAX - 7 - pkt->size / 255)
2149 return AVERROR(ENOMEM);
2150 ctrl_header_size = pkt->size + 2 + pkt->size / 255 + 1;
2151 if (ts_st->opus_pending_trim_start)
2152 ctrl_header_size += 2;
2153 if (trim_end)
2154 ctrl_header_size += 2;
2155
2156 data = av_malloc(ctrl_header_size);
2157 if (!data)
2158 return AVERROR(ENOMEM);
2159
2160 data[0] = 0x7f;
2161 data[1] = 0xe0;
2162 if (ts_st->opus_pending_trim_start)
2163 data[1] |= 0x10;
2164 if (trim_end)
2165 data[1] |= 0x08;
2166
2167 n = pkt->size;
2168 i = 2;
2169 do {
2170 data[i] = FFMIN(n, 255);
2171 n -= 255;
2172 i++;
2173 } while (n >= 0);
2174
2175 av_assert0(2 + pkt->size / 255 + 1 == i);
2176
2177 if (ts_st->opus_pending_trim_start) {
2178 trim_start = FFMIN(ts_st->opus_pending_trim_start, opus_samples);
2179 AV_WB16(data + i, trim_start);
2180 i += 2;
2181 ts_st->opus_pending_trim_start -= trim_start;
2182 }
2183 if (trim_end) {
2184 trim_end = FFMIN(trim_end, opus_samples - trim_start);
2185 AV_WB16(data + i, trim_end);
2186 i += 2;
2187 }
2188
2189 memcpy(data + i, pkt->data, pkt->size);
2190 buf = data;
2191 size = ctrl_header_size;
2192 } else {
2193 /* TODO: Can we get TS formatted data here? If so we will
2194 * need to count the samples of that too! */
2195 av_log(s, AV_LOG_WARNING, "Got MPEG-TS formatted Opus data, unhandled");
2196 }
2197 } else if (st->codecpar->codec_id == AV_CODEC_ID_AC3 && !ts_st->dvb_ac3_desc) {
2198 AC3HeaderInfo *hdr = NULL;
2199
2200 if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) >= 0) {
2201 uint8_t number_of_channels_flag;
2202 uint8_t service_type_flag;
2203 uint8_t full_service_flag = 1;
2204 DVBAC3Descriptor *dvb_ac3_desc;
2205
2206 dvb_ac3_desc = av_mallocz(sizeof(*dvb_ac3_desc));
2207 if (!dvb_ac3_desc) {
2208 av_free(hdr);
2209 return AVERROR(ENOMEM);
2210 }
2211
2212 service_type_flag = hdr->bitstream_mode;
2213 switch (hdr->channel_mode) {
2215 number_of_channels_flag = 1;
2216 break;
2217 case AC3_CHMODE_MONO:
2218 number_of_channels_flag = 0;
2219 break;
2220 case AC3_CHMODE_STEREO:
2222 number_of_channels_flag = 3;
2223 else
2224 number_of_channels_flag = 2;
2225 break;
2226 case AC3_CHMODE_3F:
2227 case AC3_CHMODE_2F1R:
2228 case AC3_CHMODE_3F1R:
2229 case AC3_CHMODE_2F2R:
2230 case AC3_CHMODE_3F2R:
2231 number_of_channels_flag = 4;
2232 break;
2233 default: /* reserved */
2234 number_of_channels_flag = 7;
2235 break;
2236 }
2237
2238 if (service_type_flag == 1 || service_type_flag == 4 ||
2239 (service_type_flag == 7 && !number_of_channels_flag))
2240 full_service_flag = 0;
2241
2242 dvb_ac3_desc->component_type_flag = 1;
2243 dvb_ac3_desc->component_type = (full_service_flag << 6) |
2244 ((service_type_flag & 0x7) << 3) |
2245 (number_of_channels_flag & 0x7);
2246 dvb_ac3_desc->bsid_flag = 1;
2247 dvb_ac3_desc->bsid = hdr->bitstream_id;
2248 dvb_ac3_desc->mainid_flag = 0;
2249 dvb_ac3_desc->asvc_flag = 0;
2250
2251 ts_st->dvb_ac3_desc = dvb_ac3_desc;
2252 }
2253 av_free(hdr);
2254 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_BLURAY && ts->m2ts_mode) {
2255 mpegts_write_pes(s, st, buf, size, pts, dts,
2256 pkt->flags & AV_PKT_FLAG_KEY, stream_id);
2257 return 0;
2258 }
2259
2260 if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
2261 (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
2262 dts - ts_st->payload_dts >= max_audio_delay) ||
2263 ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) {
2264 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
2265 ts_st->payload_pts, ts_st->payload_dts,
2266 ts_st->payload_flags & AV_PKT_FLAG_KEY, stream_id);
2267 ts_st->payload_size = 0;
2268 ts_st->opus_queued_samples = 0;
2269 }
2270
2272 av_assert0(!ts_st->payload_size);
2273 // for video and subtitle, write a single pes packet
2274 mpegts_write_pes(s, st, buf, size, pts, dts,
2275 pkt->flags & AV_PKT_FLAG_KEY, stream_id);
2276 ts_st->opus_queued_samples = 0;
2277 av_free(data);
2278 return 0;
2279 }
2280
2281 if (!ts_st->payload_size) {
2282 ts_st->payload_pts = pts;
2283 ts_st->payload_dts = dts;
2284 ts_st->payload_flags = pkt->flags;
2285 }
2286
2287 memcpy(ts_st->payload + ts_st->payload_size, buf, size);
2288 ts_st->payload_size += size;
2289 ts_st->opus_queued_samples += opus_samples;
2290
2291 av_free(data);
2292
2293 return 0;
2294}
2295
2297{
2298 MpegTSWrite *ts = s->priv_data;
2299 int i;
2300
2301 /* flush current packets */
2302 for (i = 0; i < s->nb_streams; i++) {
2303 AVStream *st = s->streams[i];
2304 MpegTSWriteStream *ts_st = st->priv_data;
2305 if (ts_st->payload_size > 0) {
2306 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
2307 ts_st->payload_pts, ts_st->payload_dts,
2308 ts_st->payload_flags & AV_PKT_FLAG_KEY, -1);
2309 ts_st->payload_size = 0;
2310 ts_st->opus_queued_samples = 0;
2311 }
2312 }
2313
2314 if (ts->m2ts_mode) {
2315 int packets = (avio_tell(s->pb) / (TS_PACKET_SIZE + 4)) % 32;
2316 while (packets++ < 32)
2318 }
2319}
2320
2322{
2323 if (!pkt) {
2325 return 1;
2326 } else {
2328 }
2329}
2330
2332{
2333 if (s->pb)
2335
2336 return 0;
2337}
2338
2340{
2341 MpegTSWrite *ts = s->priv_data;
2342 MpegTSService *service;
2343 int i;
2344
2345 for (i = 0; i < s->nb_streams; i++) {
2346 AVStream *st = s->streams[i];
2347 MpegTSWriteStream *ts_st = st->priv_data;
2348 if (ts_st) {
2349 av_freep(&ts_st->dvb_ac3_desc);
2350 av_freep(&ts_st->payload);
2351 if (ts_st->amux) {
2353 ts_st->amux = NULL;
2354 }
2355 }
2356 }
2357
2358 for (i = 0; i < ts->nb_services; i++) {
2359 service = ts->services[i];
2360 av_freep(&service);
2361 }
2362 av_freep(&ts->services);
2363}
2364
2366 const AVPacket *pkt)
2367{
2368 const struct Entry {
2369 enum AVCodecID id;
2370 const char *bsf_name;
2371 uint8_t mask;
2372 uint8_t value;
2373 } list[] = {
2374 { AV_CODEC_ID_H264, "h264_mp4toannexb", 0xff, 0x01 /* configurationVersion in AVCDecoderConfigurationRecord */},
2375 { AV_CODEC_ID_HEVC, "hevc_mp4toannexb", 0xff, 0x01 /* configurationVersion in HEVCDecoderConfigurationRecord */},
2376 { AV_CODEC_ID_VVC, "vvc_mp4toannexb", 0xf8, 0xf8 /* reserved '11111'b in VVCDecoderConfigurationRecord */},
2377 };
2378
2379 for (int i = 0; i < FF_ARRAY_ELEMS(list); i++) {
2380 const struct Entry *e = list + i;
2381 if (e->id == st->codecpar->codec_id &&
2382 pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
2383 (AV_RB24(pkt->data) != 0x000001 ||
2384 (st->codecpar->extradata_size > 0 &&
2385 ((st->codecpar->extradata[0] & e->mask) == e->value))))
2386 return ff_stream_add_bitstream_filter(st, e->bsf_name, NULL);
2387 }
2388 return 1;
2389}
2390
2391#define OFFSET(x) offsetof(MpegTSWrite, x)
2392#define ENC AV_OPT_FLAG_ENCODING_PARAM
2393static const AVOption options[] = {
2394 { "mpegts_transport_stream_id", "Set transport_stream_id field.",
2395 OFFSET(transport_stream_id), AV_OPT_TYPE_INT, { .i64 = 0x0001 }, 0x0001, 0xffff, ENC },
2396 { "mpegts_original_network_id", "Set original_network_id field.",
2397 OFFSET(original_network_id), AV_OPT_TYPE_INT, { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, ENC },
2398 { "mpegts_service_id", "Set service_id field.",
2399 OFFSET(service_id), AV_OPT_TYPE_INT, { .i64 = 0x0001 }, 0x0001, 0xffff, ENC },
2400 { "mpegts_service_type", "Set service_type field.",
2401 OFFSET(service_type), AV_OPT_TYPE_INT, { .i64 = 0x01 }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2402 { "digital_tv", "Digital Television.",
2403 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2404 { "digital_radio", "Digital Radio.",
2405 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2406 { "teletext", "Teletext.",
2407 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2408 { "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
2409 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2410 { "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
2411 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2412 { "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
2413 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2414 { "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
2415 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2416 { "hevc_digital_hdtv", "HEVC Digital Television Service.",
2417 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" },
2418 { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
2419 OFFSET(pmt_start_pid), AV_OPT_TYPE_INT, { .i64 = 0x1000 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
2420 { "mpegts_start_pid", "Set the first pid.",
2421 OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = 0x0100 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
2422 { "mpegts_m2ts_mode", "Enable m2ts mode.", OFFSET(m2ts_mode), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC },
2423 { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
2424 { "pes_payload_size", "Minimum PES packet payload in bytes",
2425 OFFSET(pes_payload_size), AV_OPT_TYPE_INT, { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, ENC },
2426 { "mpegts_flags", "MPEG-TS muxing flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, ENC, .unit = "mpegts_flags" },
2427 { "resend_headers", "Reemit PAT/PMT before writing the next packet",
2428 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX, ENC, .unit = "mpegts_flags" },
2429 { "latm", "Use LATM packetization for AAC",
2430 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX, ENC, .unit = "mpegts_flags" },
2431 { "pat_pmt_at_frames", "Reemit PAT and PMT at each video frame",
2432 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_PAT_PMT_AT_FRAMES}, 0, INT_MAX, ENC, .unit = "mpegts_flags" },
2433 { "system_b", "Conform to System B (DVB) instead of System A (ATSC)",
2434 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_SYSTEM_B }, 0, INT_MAX, ENC, .unit = "mpegts_flags" },
2435 { "initial_discontinuity", "Mark initial packets as discontinuous",
2436 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_DISCONT }, 0, INT_MAX, ENC, .unit = "mpegts_flags" },
2437 { "nit", "Enable NIT transmission",
2438 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_NIT}, 0, INT_MAX, ENC, .unit = "mpegts_flags" },
2439 { "omit_rai", "Disable writing of random access indicator",
2440 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_OMIT_RAI }, 0, INT_MAX, ENC, .unit = "mpegts_flags" },
2441 { "mpegts_copyts", "don't offset dts/pts", OFFSET(copyts), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC },
2442 { "tables_version", "set PAT, PMT, SDT and NIT version", OFFSET(tables_version), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 31, ENC },
2443 { "omit_video_pes_length", "Omit the PES packet length for video packets",
2444 OFFSET(omit_video_pes_length), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC },
2445 { "pcr_period", "PCR retransmission time in milliseconds",
2446 OFFSET(pcr_period_ms), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ENC },
2447 { "mpegts_pcr_pid", "Set a separate PCR PID (-1 means PCR on video PID)",
2448 OFFSET(pcr_pid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, LAST_OTHER_PID, ENC },
2449 { "pat_period", "PAT/PMT retransmission time limit in seconds",
2450 OFFSET(pat_period_us), AV_OPT_TYPE_DURATION, { .i64 = PAT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
2451 { "sdt_period", "SDT retransmission time limit in seconds",
2452 OFFSET(sdt_period_us), AV_OPT_TYPE_DURATION, { .i64 = SDT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
2453 { "nit_period", "NIT retransmission time limit in seconds",
2454 OFFSET(nit_period_us), AV_OPT_TYPE_DURATION, { .i64 = NIT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
2455 { NULL },
2456};
2457
2459 .class_name = "MPEGTS muxer",
2460 .item_name = av_default_item_name,
2461 .option = options,
2462 .version = LIBAVUTIL_VERSION_INT,
2463};
2464
2466 .p.name = "mpegts",
2467 .p.long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
2468 .p.mime_type = "video/MP2T",
2469 .p.extensions = "ts,m2t,m2ts,mts",
2470 .priv_data_size = sizeof(MpegTSWrite),
2471 .p.audio_codec = AV_CODEC_ID_MP2,
2472 .p.video_codec = AV_CODEC_ID_MPEG2VIDEO,
2473 .init = mpegts_init,
2474 .write_packet = mpegts_write_packet,
2475 .write_trailer = mpegts_write_end,
2476 .deinit = mpegts_deinit,
2477 .check_bitstream = mpegts_check_bitstream,
2479 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
2480 .p.priv_class = &mpegts_muxer_class,
2481};
int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf, size_t size)
Definition ac3_parser.c:491
@ AC3_CHMODE_MONO
Definition ac3defs.h:69
@ AC3_CHMODE_STEREO
Definition ac3defs.h:70
@ AC3_CHMODE_2F1R
Definition ac3defs.h:72
@ AC3_CHMODE_DUALMONO
Definition ac3defs.h:68
@ AC3_CHMODE_3F
Definition ac3defs.h:71
@ AC3_CHMODE_3F1R
Definition ac3defs.h:73
@ AC3_CHMODE_2F2R
Definition ac3defs.h:74
@ AC3_CHMODE_3F2R
Definition ac3defs.h:75
@ AC3_DSURMOD_ON
Definition ac3defs.h:82
static double val(void *priv, double ch)
Definition aeval.c:77
const FFOutputFormat ff_mpegts_muxer
Definition mpegtsenc.c:2465
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:856
Main libavformat public API header.
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition avformat.h:504
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition avformat.h:678
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition avformat.h:503
#define AV_DISPOSITION_CLEAN_EFFECTS
The audio stream contains music and sound effects without voice.
Definition avformat.h:686
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition avformat.h:682
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1369
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1324
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1397
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
byte swapping routines
static av_always_inline int bytestream2_tell_p(const PutByteContext *p)
Definition bytestream.h:197
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition bytestream.h:286
#define ENC
Definition caca.c:198
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flag(name)
Definition cbs_h264.c:60
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC aud(CodedBitstreamContext *ctx, RWContext *rw, H264RawAUD *current)
#define s(width, name)
Definition cbs_vp9.c:198
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition common.h:477
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
Misc types and constants that do not belong anywhere else.
#define AV_PROFILE_ARIB_PROFILE_A
Definition defs.h:205
#define AV_PROFILE_ARIB_PROFILE_C
Definition defs.h:206
#define FF_COMPLIANCE_NORMAL
Definition defs.h:60
#define AV_PROFILE_KLVA_SYNC
Definition defs.h:208
static AVPacket * pkt
Public dictionary API.
enum AVCodecID id
Definition dts2pts.c:607
double value
Definition eval.c:102
static struct @346255127015250356166251341105367306144006377143 state
const char * key
static int64_t duration
Definition ffplay.c:411
static unsigned int nb_streams
Definition ffprobe.c:390
static const uint8_t frame_size[4]
Definition g723_1.h:222
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_DIRAC
Definition codec_id.h:166
@ AV_CODEC_ID_AVS3
Definition codec_id.h:245
@ AV_CODEC_ID_TIMED_ID3
Definition codec_id.h:610
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition codec_id.h:576
@ AV_CODEC_ID_EAC3
Definition codec_id.h:497
@ AV_CODEC_ID_DVB_SUBTITLE
Definition codec_id.h:571
@ AV_CODEC_ID_S302M
Definition codec_id.h:358
@ AV_CODEC_ID_SMPTE_2038
Definition codec_id.h:612
@ AV_CODEC_ID_PCM_BLURAY
Definition codec_id.h:356
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_SMPTE_KLV
Definition codec_id.h:608
@ AV_CODEC_ID_HDMV_TEXT_SUBTITLE
Definition codec_id.h:593
@ AV_CODEC_ID_VVC
Definition codec_id.h:247
@ AV_CODEC_ID_VC1
Definition codec_id.h:120
@ AV_CODEC_ID_ARIB_CAPTION
Definition codec_id.h:595
@ AV_CODEC_ID_CAVS
Definition codec_id.h:137
@ AV_CODEC_ID_MP2
Definition codec_id.h:457
@ AV_CODEC_ID_DTS
Definition codec_id.h:461
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_AAC
Definition codec_id.h:459
@ AV_CODEC_ID_TRUEHD
Definition codec_id.h:501
@ AV_CODEC_ID_AC3
Definition codec_id.h:460
@ AV_CODEC_ID_DVB_TELETEXT
Definition codec_id.h:577
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_AVS2
Definition codec_id.h:243
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:458
@ AV_CODEC_ID_MPEG1VIDEO
Definition codec_id.h:51
@ AV_CODEC_ID_AAC_LATM
Definition codec_id.h:506
@ AV_CODEC_ID_OPUS
Definition codec_id.h:517
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_MPEGTS_STREAM_ID
MPEGTS stream ID as uint8_t, this is required to pass the stream ID information from the demuxer to t...
Definition packet.h:212
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:164
av_warn_unused_result 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:505
const 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:79
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition mux.c:1214
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition format.c:41
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
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:421
@ AV_CRC_32_IEEE
Definition crc.h:52
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition mathematics.c:58
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AV_ROUND_UP
Round toward +infinity.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition mem.c:419
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
H.264 common definitions.
@ H264_NAL_SPS
Definition h264.h:41
@ H264_NAL_SLICE
Definition h264.h:35
@ H264_NAL_AUD
Definition h264.h:43
@ H264_NAL_IDR_SLICE
Definition h264.h:39
#define b
Definition input.c:43
#define AV_WB32(p, v)
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_WB16(p, v)
#define AV_RB24(x)
#define AV_RB16(p)
@ HEVC_NAL_AUD
Definition hevc.h:64
@ HEVC_NAL_RSV_VCL24
Definition hevc.h:53
@ HEVC_NAL_VPS
Definition hevc.h:61
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
Definition utils.c:830
@ VVC_OPI_NUT
Definition vvc.h:41
@ VVC_VPS_NUT
Definition vvc.h:43
@ VVC_AUD_NUT
Definition vvc.h:49
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition mux.c:1332
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
version
Definition libkvazaar.c:313
void av_log_once(void *avcl, int initial_level, int subsequent_level, int *state, const char *fmt,...)
Definition log.c:450
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
uint32_t tag
Definition movenc.c:2128
#define STREAM_TYPE_VIDEO_MPEG4
Definition mpeg.h:56
#define STREAM_TYPE_VIDEO_CAVS
Definition mpeg.h:60
#define STREAM_TYPE_AUDIO_MPEG2
Definition mpeg.h:52
#define STREAM_TYPE_VIDEO_HEVC
Definition mpeg.h:58
#define STREAM_TYPE_VIDEO_VVC
Definition mpeg.h:59
#define STREAM_TYPE_PRIVATE_DATA
Definition mpeg.h:54
#define STREAM_TYPE_AUDIO_AAC
Definition mpeg.h:55
#define STREAM_TYPE_VIDEO_H264
Definition mpeg.h:57
#define STREAM_TYPE_AUDIO_MPEG1
Definition mpeg.h:51
#define STREAM_TYPE_VIDEO_MPEG2
Definition mpeg.h:50
#define STREAM_ID_PRIVATE_STREAM_2
Definition mpegts.h:188
#define STREAM_TYPE_VIDEO_AVS3
Definition mpegts.h:153
#define M2TS_TEXTSUB_PID
Definition mpegts.h:79
#define STREAM_TYPE_VIDEO_DIRAC
Definition mpegts.h:155
#define STREAM_ID_PADDING_STREAM
Definition mpegts.h:187
#define DVB_EXTENSION_DESCRIPTOR
Definition mpegts.h:227
#define STREAM_TYPE_BLURAY_SUBTITLE_TEXT
Definition mpegts.h:168
#define M2TS_PGSSUB_START_PID
Definition mpegts.h:78
#define FIRST_OTHER_PID
Definition mpegts.h:65
#define STREAM_TYPE_VIDEO_VC1
Definition mpegts.h:154
#define PAT_PID
Definition mpegts.h:41
#define STREAM_TYPE_ATSC_AUDIO_AC3
Definition mpegts.h:172
#define STREAM_TYPE_BLURAY_AUDIO_EAC3
Definition mpegts.h:162
#define STREAM_ID_METADATA_STREAM
Definition mpegts.h:195
#define M2TS_VIDEO_PID
Definition mpegts.h:75
#define STREAM_TYPE_BLURAY_SUBTITLE_PGS
Definition mpegts.h:167
#define NETWORK_NAME_DESCRIPTOR
Definition mpegts.h:218
#define PAT_TID
Definition mpegts.h:84
#define SUBTITLING_DESCRIPTOR
Definition mpegts.h:223
#define M2TS_AUDIO_START_PID
Definition mpegts.h:77
#define STREAM_ID_EMM_STREAM
Definition mpegts.h:192
#define STREAM_TYPE_BLURAY_AUDIO_TRUEHD
Definition mpegts.h:161
#define PMT_TID
Definition mpegts.h:86
#define STREAM_TYPE_BLURAY_AUDIO_DTS
Definition mpegts.h:160
#define SDT_TID
Definition mpegts.h:92
#define STREAM_TYPE_AUDIO_AAC_LATM
Definition mpegts.h:132
#define SDT_PID
Definition mpegts.h:47
#define ISO_639_LANGUAGE_DESCRIPTOR
Definition mpegts.h:202
#define STREAM_IDENTIFIER_DESCRIPTOR
Definition mpegts.h:221
#define TELETEXT_DESCRIPTOR
Definition mpegts.h:222
#define NIT_TID
Definition mpegts.h:90
#define STREAM_TYPE_VIDEO_AVS2
Definition mpegts.h:152
#define STREAM_TYPE_ATSC_AUDIO_EAC3
Definition mpegts.h:173
#define REGISTRATION_DESCRIPTOR
Definition mpegts.h:201
#define SERVICE_DESCRIPTOR
Definition mpegts.h:220
#define STREAM_TYPE_BLURAY_AUDIO_PCM_BLURAY
Definition mpegts.h:158
#define STREAM_ID_PROGRAM_STREAM_DIRECTORY
Definition mpegts.h:197
#define STREAM_TYPE_BLURAY_AUDIO_AC3
Definition mpegts.h:159
#define STREAM_ID_DSMCC_STREAM
Definition mpegts.h:193
#define NIT_PID
Definition mpegts.h:46
#define STREAM_ID_EXTENDED_STREAM_ID
Definition mpegts.h:196
#define ENHANCED_AC3_DESCRIPTOR
Definition mpegts.h:225
#define LAST_OTHER_PID
Definition mpegts.h:66
#define DATA_COMPONENT_DESCRIPTOR
Definition mpegts.h:238
#define STREAM_ID_PROGRAM_STREAM_MAP
Definition mpegts.h:185
#define STREAM_ID_ECM_STREAM
Definition mpegts.h:191
#define NULL_PID
Definition mpegts.h:70
#define METADATA_DESCRIPTOR
Definition mpegts.h:206
#define AC3_DESCRIPTOR
Definition mpegts.h:224
#define STREAM_ID_PRIVATE_STREAM_1
Definition mpegts.h:186
#define STREAM_ID_TYPE_E_STREAM
Definition mpegts.h:194
#define STREAM_ID_VIDEO_STREAM_0
Definition mpegts.h:190
#define STREAM_TYPE_BLURAY_AUDIO_DTS_HD
Definition mpegts.h:163
#define SYSTEM_CLOCK_FREQUENCY_DIVISOR
Definition mpegts.h:38
#define SYNC_BYTE
Definition mpegts.h:36
#define M2TS_PMT_PID
Definition mpegts.h:73
#define STREAM_TYPE_METADATA
Definition mpegts.h:140
#define SERVICE_LIST_DESCRIPTOR
Definition mpegts.h:219
#define TS_PACKET_SIZE
Definition mpegts.h:29
#define STUFFING_BYTE
Definition mpegts.h:37
#define STREAM_ID_AUDIO_STREAM_0
Definition mpegts.h:189
static const AVClass mpegts_muxer_class
Definition mpegtsenc.c:2458
#define MPEGTS_FLAG_OMIT_RAI
Definition mpegtsenc.c:121
#define SDT_RETRANS_TIME
Definition mpegtsenc.c:247
static int opus_get_packet_samples(AVFormatContext *s, AVPacket *pkt)
Definition mpegtsenc.c:1848
static void mpegts_write_nit(AVFormatContext *s)
Definition mpegtsenc.c:893
static void enable_pcr_generation_for_stream(AVFormatContext *s, AVStream *pcr_st)
Definition mpegtsenc.c:1045
static int get_pes_stream_id(AVFormatContext *s, AVStream *st, int stream_id, int *async)
Definition mpegtsenc.c:1478
#define HEVC_NAL_TYPE(state)
Definition mpegtsenc.c:1916
static void mpegts_deinit(AVFormatContext *s)
Definition mpegtsenc.c:2339
static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
Definition mpegtsenc.c:1918
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:213
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
Definition mpegtsenc.c:1431
#define H264_NAL_TYPE(state)
Definition mpegtsenc.c:1915
static void extend_af(uint8_t *pkt, int size)
Definition mpegtsenc.c:1462
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
Definition mpegtsenc.c:1424
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
Definition mpegtsenc.c:990
static void write_packet(AVFormatContext *s, const uint8_t *packet)
Definition mpegtsenc.c:976
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition mpegtsenc.c:2321
static void select_pcr_streams(AVFormatContext *s)
Definition mpegtsenc.c:1076
#define PCR_TIME_BASE
Definition mpegtsenc.c:45
static void mpegts_write_flush(AVFormatContext *s)
Definition mpegtsenc.c:2296
static int put_arib_caption_descriptor(AVFormatContext *s, uint8_t **q_ptr, AVCodecParameters *codecpar)
Definition mpegtsenc.c:303
#define MPEGTS_FLAG_REEMIT_PAT_PMT
Definition mpegtsenc.c:115
static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
Definition mpegtsenc.c:347
#define PCR_RETRANS_TIME
Definition mpegtsenc.c:249
static void mpegts_write_pes(AVFormatContext *s, AVStream *st, const uint8_t *payload, int payload_size, int64_t pts, int64_t dts, int key, int stream_id)
Definition mpegtsenc.c:1513
#define SECTION_LENGTH
Definition mpegtsenc.c:147
int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
Check presence of H264 startcode.
Definition mpegtsenc.c:1839
static void putbuf(uint8_t **q_ptr, const uint8_t *buf, size_t len)
Definition mpegtsenc.c:297
#define DEFAULT_PES_PAYLOAD_SIZE
Definition mpegtsenc.c:143
static void put16(uint8_t **q_ptr, int val)
Definition mpegtsenc.c:204
static int64_t get_pcr(const MpegTSWrite *ts)
Definition mpegtsenc.c:970
static int get_dvb_stream_type(AVFormatContext *s, AVStream *st)
Definition mpegtsenc.c:359
static void mpegts_write_sdt(AVFormatContext *s)
Definition mpegtsenc.c:855
#define MPEGTS_FLAG_AAC_LATM
Definition mpegtsenc.c:116
static int mpegts_write_end(AVFormatContext *s)
Definition mpegtsenc.c:2331
#define DVB_PRIVATE_NETWORK_START
Definition mpegtsenc.c:49
#define NIT_RETRANS_TIME
Definition mpegtsenc.c:250
static int check_h26x_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt, const char *codec)
Definition mpegtsenc.c:1822
#define PAT_RETRANS_TIME
Definition mpegtsenc.c:248
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
Definition mpegtsenc.c:150
static void mpegts_insert_pcr_only_pid(AVFormatContext *s, int pid, int cc, int discontinuity)
Definition mpegtsenc.c:1398
static void set_af_flag(uint8_t *pkt, int flag)
Definition mpegtsenc.c:1446
#define MPEGTS_FLAG_DISCONT
Definition mpegtsenc.c:119
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES
Definition mpegtsenc.c:117
#define MPEGTS_FLAG_NIT
Definition mpegtsenc.c:120
#define DEFAULT_PROVIDER_NAME
Definition mpegtsenc.c:243
static int mpegts_check_bitstream(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition mpegtsenc.c:2365
static void mpegts_write_pat(AVFormatContext *s)
Definition mpegtsenc.c:276
static int mpegts_init(AVFormatContext *s)
Definition mpegtsenc.c:1112
static MpegTSService * mpegts_add_service(AVFormatContext *s, int sid, const AVDictionary *metadata, AVProgram *program)
Definition mpegtsenc.c:996
static uint8_t * h26x_prefix_aud(const uint8_t *aud, const int aud_size, const uint8_t *extra_data, const int extra_size, AVPacket *pkt, int *size)
Definition mpegtsenc.c:1896
#define OFFSET(x)
Definition mpegtsenc.c:2391
static int encode_str8(uint8_t *buf, const char *str)
Definition mpegtsenc.c:938
static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
Definition mpegtsenc.c:516
#define MPEGTS_FLAG_SYSTEM_B
Definition mpegtsenc.c:118
#define DEFAULT_SERVICE_NAME
Definition mpegtsenc.c:244
static int get_m2ts_stream_type(AVFormatContext *s, AVStream *st)
Definition mpegtsenc.c:463
#define VVC_NAL_TYPE(state)
Definition mpegtsenc.c:1917
static void mpegts_insert_null_packet(AVFormatContext *s)
Definition mpegtsenc.c:1383
static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int force_nit, int64_t pcr)
Definition mpegtsenc.c:1335
@ MPEGTS_SERVICE_TYPE_DIGITAL_RADIO
Definition mpegtsenc.c:75
@ MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV
Definition mpegtsenc.c:81
@ MPEGTS_SERVICE_TYPE_TELETEXT
Definition mpegtsenc.c:76
@ MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO
Definition mpegtsenc.c:77
@ MPEGTS_SERVICE_TYPE_DIGITAL_TV
Definition mpegtsenc.c:74
@ MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV
Definition mpegtsenc.c:80
@ MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV
Definition mpegtsenc.c:79
@ MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV
Definition mpegtsenc.c:78
static uint8_t * get_ts_payload_start(uint8_t *pkt)
Definition mpegtsenc.c:1470
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
Definition mpegtsenc.c:1368
#define FF_OFMT_FLAG_ALLOW_FLUSH
This flag indicates that the muxer stores data internally and supports flushing it.
Definition mux.h:38
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
#define av_bswap32
Definition bswap.h:47
#define FF_ARRAY_ELEMS(a)
const uint8_t * code
Definition spdifenc.c:433
Accelerated start code search function for start codes common to MPEG-1/2/4 video,...
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
Coded AC-3 header values up to the lfeon element, plus derived values.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition codec_par.h:135
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
int initial_padding
Number of padding audio samples at the start.
Definition codec_par.h:239
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1400
AVIOContext * pb
I/O context.
Definition avformat.h:1442
const struct AVOutputFormat * oformat
The output container format.
Definition avformat.h:1419
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1468
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:609
uint8_t * data
Definition packet.h:610
New fields can be added to the end with minor version bumps.
Definition avformat.h:1324
unsigned int nb_stream_indexes
Definition avformat.h:1329
unsigned int * stream_index
Definition avformat.h:1328
AVDictionary * metadata
Definition avformat.h:1330
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:829
AVDictionary * metadata
Definition avformat.h:848
void * priv_data
Definition avformat.h:793
int id
Format-specific stream ID.
Definition avformat.h:780
int index
stream index in AVFormatContext
Definition avformat.h:774
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:857
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
uint8_t asvc
Definition mpegts.h:284
uint8_t bsid_flag
Definition mpegts.h:277
uint8_t mainid_flag
Definition mpegts.h:278
uint8_t bsid
Definition mpegts.h:282
uint8_t component_type_flag
Definition mpegts.h:276
uint8_t mainid
Definition mpegts.h:283
uint8_t component_type
Definition mpegts.h:281
uint8_t asvc_flag
Definition mpegts.h:279
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition internal.h:111
void(* write_packet)(struct MpegTSSection *s, const uint8_t *packet)
Definition mpegtsenc.c:58
int discontinuity
Definition mpegtsenc.c:57
void * opaque
Definition mpegtsenc.c:59
AVProgram * program
Definition mpegtsenc.c:69
uint8_t provider_name[256]
Definition mpegtsenc.c:67
uint8_t name[256]
Definition mpegtsenc.c:66
MpegTSSection pmt
Definition mpegtsenc.c:64
int64_t payload_pts
Definition mpegtsenc.c:259
int opus_pending_trim_start
Definition mpegtsenc.c:271
uint8_t * payload
Definition mpegtsenc.c:262
int first_timestamp_checked
first pts/dts check needed
Definition mpegtsenc.c:257
AVFormatContext * amux
Definition mpegtsenc.c:263
int64_t payload_dts
Definition mpegtsenc.c:260
DVBAC3Descriptor * dvb_ac3_desc
Definition mpegtsenc.c:273
int64_t next_pcr
Definition mpegtsenc.c:96
MpegTSSection sdt
Definition mpegtsenc.c:86
int omit_video_pes_length
Definition mpegtsenc.c:134
int pcr_pid
user-specified separate PCR PID (-1 = use video PID)
Definition mpegtsenc.c:136
int64_t last_pat_ts
Definition mpegtsenc.c:128
int64_t pcr_stream_last_pcr
last PCR sent on the dedicated PID
Definition mpegtsenc.c:138
int mux_rate
set to 1 when VBR
Definition mpegtsenc.c:97
int pcr_period_ms
Definition mpegtsenc.c:114
int pmt_start_pid
Definition mpegtsenc.c:106
int tables_version
Definition mpegtsenc.c:124
int64_t last_sdt_ts
Definition mpegtsenc.c:129
MpegTSService ** services
Definition mpegtsenc.c:88
uint8_t provider_name[256]
Definition mpegtsenc.c:132
int m2ts_video_pid
Definition mpegtsenc.c:109
MpegTSSection pat
Definition mpegtsenc.c:85
int64_t sdt_period_us
Definition mpegtsenc.c:126
int64_t last_nit_ts
Definition mpegtsenc.c:130
int pes_payload_size
Definition mpegtsenc.c:98
int original_network_id
Definition mpegtsenc.c:102
int nb_services
Definition mpegtsenc.c:93
int64_t nit_period
Definition mpegtsenc.c:92
int m2ts_textsub_pid
Definition mpegtsenc.c:112
int m2ts_audio_pid
Definition mpegtsenc.c:110
AVPacket * pkt
Definition mpegtsenc.c:89
int first_dts_checked
Definition mpegtsenc.c:95
int64_t pat_period_us
Definition mpegtsenc.c:125
const AVClass * av_class
Definition mpegtsenc.c:84
int64_t total_size
Definition mpegtsenc.c:99
int service_type
Definition mpegtsenc.c:104
int64_t first_pcr
Definition mpegtsenc.c:94
int64_t pat_period
Definition mpegtsenc.c:91
MpegTSSection nit
Definition mpegtsenc.c:87
int transport_stream_id
Definition mpegtsenc.c:101
int64_t nit_period_us
Definition mpegtsenc.c:127
int m2ts_pgssub_pid
Definition mpegtsenc.c:111
int64_t pcr_stream_pcr_period
PCR period for the dedicated stream.
Definition mpegtsenc.c:137
int64_t sdt_period
Definition mpegtsenc.c:90
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static int64_t pts
int size
enum AVCodecID codec_id
int len