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