forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpegtsenc.c
2314 lines (2079 loc) · 85.1 KB
/
mpegtsenc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MPEG-2 transport stream (aka DVB) muxer
* Copyright (c) 2003 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/avassert.h"
#include "libavutil/bswap.h"
#include "libavutil/crc.h"
#include "libavutil/dict.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavcodec/ac3_parser_internal.h"
#include "libavcodec/internal.h"
#include "avformat.h"
#include "avio_internal.h"
#include "internal.h"
#include "mpegts.h"
#define PCR_TIME_BASE 27000000
/* write DVB SI sections */
#define DVB_PRIVATE_NETWORK_START 0xff01
/*********************************************/
/* mpegts section writer */
typedef struct MpegTSSection {
int pid;
int cc;
int discontinuity;
void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
void *opaque;
} MpegTSSection;
typedef struct MpegTSService {
MpegTSSection pmt; /* MPEG-2 PMT table context */
int sid; /* service ID */
uint8_t name[256];
uint8_t provider_name[256];
int pcr_pid;
AVProgram *program;
} MpegTSService;
// service_type values as defined in ETSI 300 468
enum {
MPEGTS_SERVICE_TYPE_DIGITAL_TV = 0x01,
MPEGTS_SERVICE_TYPE_DIGITAL_RADIO = 0x02,
MPEGTS_SERVICE_TYPE_TELETEXT = 0x03,
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO = 0x0A,
MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV = 0x11,
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV = 0x16,
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV = 0x19,
MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV = 0x1F,
};
typedef struct MpegTSWrite {
const AVClass *av_class;
MpegTSSection pat; /* MPEG-2 PAT table */
MpegTSSection sdt; /* MPEG-2 SDT table context */
MpegTSSection nit; /* MPEG-2 NIT table context */
MpegTSService **services;
AVPacket *pkt;
int64_t sdt_period; /* SDT period in PCR time base */
int64_t pat_period; /* PAT/PMT period in PCR time base */
int64_t nit_period; /* NIT period in PCR time base */
int nb_services;
int64_t first_pcr;
int first_dts_checked;
int64_t next_pcr;
int mux_rate; ///< set to 1 when VBR
int pes_payload_size;
int64_t total_size;
int transport_stream_id;
int original_network_id;
int service_id;
int service_type;
int pmt_start_pid;
int start_pid;
int m2ts_mode;
int m2ts_video_pid;
int m2ts_audio_pid;
int m2ts_pgssub_pid;
int m2ts_textsub_pid;
int pcr_period_ms;
#define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
#define MPEGTS_FLAG_AAC_LATM 0x02
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
#define MPEGTS_FLAG_SYSTEM_B 0x08
#define MPEGTS_FLAG_DISCONT 0x10
#define MPEGTS_FLAG_NIT 0x20
int flags;
int copyts;
int tables_version;
int64_t pat_period_us;
int64_t sdt_period_us;
int64_t nit_period_us;
int64_t last_pat_ts;
int64_t last_sdt_ts;
int64_t last_nit_ts;
uint8_t provider_name[256];
int omit_video_pes_length;
} MpegTSWrite;
/* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
#define DEFAULT_PES_HEADER_FREQ 16
#define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
/* The section length is 12 bits. The first 2 are set to 0, the remaining
* 10 bits should not exceed 1021. */
#define SECTION_LENGTH 1020
/* NOTE: 4 bytes must be left at the end for the crc32 */
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
{
unsigned int crc;
unsigned char packet[TS_PACKET_SIZE];
const unsigned char *buf_ptr;
unsigned char *q;
int first, b, len1, left;
crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE),
-1, buf, len - 4));
buf[len - 4] = (crc >> 24) & 0xff;
buf[len - 3] = (crc >> 16) & 0xff;
buf[len - 2] = (crc >> 8) & 0xff;
buf[len - 1] = crc & 0xff;
/* send each packet */
buf_ptr = buf;
while (len > 0) {
first = buf == buf_ptr;
q = packet;
*q++ = 0x47;
b = s->pid >> 8;
if (first)
b |= 0x40;
*q++ = b;
*q++ = s->pid;
s->cc = s->cc + 1 & 0xf;
*q++ = 0x10 | s->cc;
if (s->discontinuity) {
q[-1] |= 0x20;
*q++ = 1;
*q++ = 0x80;
s->discontinuity = 0;
}
if (first)
*q++ = 0; /* 0 offset */
len1 = TS_PACKET_SIZE - (q - packet);
if (len1 > len)
len1 = len;
memcpy(q, buf_ptr, len1);
q += len1;
/* add known padding data */
left = TS_PACKET_SIZE - (q - packet);
if (left > 0)
memset(q, 0xff, left);
s->write_packet(s, packet);
buf_ptr += len1;
len -= len1;
}
}
static inline void put16(uint8_t **q_ptr, int val)
{
uint8_t *q;
q = *q_ptr;
*q++ = val >> 8;
*q++ = val;
*q_ptr = q;
}
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)
{
uint8_t section[1024], *q;
unsigned int tot_len;
/* reserved_future_use field must be set to 1 for SDT and NIT */
unsigned int flags = (tid == SDT_TID || tid == NIT_TID) ? 0xf000 : 0xb000;
tot_len = 3 + 5 + len + 4;
/* check if not too big */
if (tot_len > 1024)
return AVERROR_INVALIDDATA;
q = section;
*q++ = tid;
put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
put16(&q, id);
*q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
*q++ = sec_num;
*q++ = last_sec_num;
memcpy(q, buf, len);
mpegts_write_section(s, section, tot_len);
return 0;
}
/*********************************************/
/* mpegts writer */
#define DEFAULT_PROVIDER_NAME "FFmpeg"
#define DEFAULT_SERVICE_NAME "Service"
/* we retransmit the SI info at this rate */
#define SDT_RETRANS_TIME 500
#define PAT_RETRANS_TIME 100
#define PCR_RETRANS_TIME 20
#define NIT_RETRANS_TIME 500
typedef struct MpegTSWriteStream {
int pid; /* stream associated pid */
int cc;
int discontinuity;
int payload_size;
int first_timestamp_checked; ///< first pts/dts check needed
int prev_payload_key;
int64_t payload_pts;
int64_t payload_dts;
int payload_flags;
uint8_t *payload;
AVFormatContext *amux;
int data_st_warning;
int64_t pcr_period; /* PCR period in PCR time base */
int64_t last_pcr;
/* For Opus */
int opus_queued_samples;
int opus_pending_trim_start;
DVBAC3Descriptor *dvb_ac3_desc;
} MpegTSWriteStream;
static void mpegts_write_pat(AVFormatContext *s)
{
MpegTSWrite *ts = s->priv_data;
MpegTSService *service;
uint8_t data[SECTION_LENGTH], *q;
int i;
q = data;
if (ts->flags & MPEGTS_FLAG_NIT) {
put16(&q, 0x0000);
put16(&q, NIT_PID);
}
for (i = 0; i < ts->nb_services; i++) {
service = ts->services[i];
put16(&q, service->sid);
put16(&q, 0xe000 | service->pmt.pid);
}
mpegts_write_section1(&ts->pat, PAT_TID, ts->transport_stream_id, ts->tables_version, 0, 0,
data, q - data);
}
static void putbuf(uint8_t **q_ptr, const uint8_t *buf, size_t len)
{
memcpy(*q_ptr, buf, len);
*q_ptr += len;
}
static int put_arib_caption_descriptor(AVFormatContext *s, uint8_t **q_ptr,
AVCodecParameters *codecpar)
{
uint8_t stream_identifier;
uint16_t data_component_id;
uint8_t *q = *q_ptr;
switch (codecpar->profile) {
case FF_PROFILE_ARIB_PROFILE_A:
stream_identifier = 0x30;
data_component_id = 0x0008;
break;
case FF_PROFILE_ARIB_PROFILE_C:
stream_identifier = 0x87;
data_component_id = 0x0012;
break;
default:
av_log(s, AV_LOG_ERROR,
"Unset/unknown ARIB caption profile %d utilized!\n",
codecpar->profile);
return AVERROR_INVALIDDATA;
}
// stream_identifier_descriptor
*q++ = 0x52; // descriptor_tag
*q++ = 1; // descriptor_length
*q++ = stream_identifier; // component_tag: stream_identifier
// data_component_descriptor, defined in ARIB STD-B10, part 2, 6.2.20
*q++ = 0xFD; // descriptor_tag: ARIB data coding type descriptor
*q++ = 3; // descriptor_length
put16(&q, data_component_id); // data_component_id
// additional_arib_caption_info: defined in ARIB STD-B24, fascicle 1, Part 3, 9.6.1
// Here we utilize a pre-defined set of values defined in ARIB TR-B14,
// Fascicle 2, 4.2.8.5 for PMT usage, with the reserved bits in the middle
// set to 1 (as that is what every broadcaster seems to be doing in
// production).
*q++ = 0x3D; // DMF('0011'), Reserved('11'), Timing('01')
*q_ptr = q;
return 0;
}
static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
{
uint8_t *q = *q_ptr;
*q++ = REGISTRATION_DESCRIPTOR;
*q++ = 4;
*q++ = tag;
*q++ = tag >> 8;
*q++ = tag >> 16;
*q++ = tag >> 24;
*q_ptr = q;
}
static int get_dvb_stream_type(AVFormatContext *s, AVStream *st)
{
MpegTSWrite *ts = s->priv_data;
MpegTSWriteStream *ts_st = st->priv_data;
int stream_type;
switch (st->codecpar->codec_id) {
case AV_CODEC_ID_MPEG1VIDEO:
case AV_CODEC_ID_MPEG2VIDEO:
stream_type = STREAM_TYPE_VIDEO_MPEG2;
break;
case AV_CODEC_ID_MPEG4:
stream_type = STREAM_TYPE_VIDEO_MPEG4;
break;
case AV_CODEC_ID_H264:
stream_type = STREAM_TYPE_VIDEO_H264;
break;
case AV_CODEC_ID_HEVC:
stream_type = STREAM_TYPE_VIDEO_HEVC;
break;
case AV_CODEC_ID_CAVS:
stream_type = STREAM_TYPE_VIDEO_CAVS;
break;
case AV_CODEC_ID_AVS2:
stream_type = STREAM_TYPE_VIDEO_AVS2;
break;
case AV_CODEC_ID_AVS3:
stream_type = STREAM_TYPE_VIDEO_AVS3;
break;
case AV_CODEC_ID_DIRAC:
stream_type = STREAM_TYPE_VIDEO_DIRAC;
break;
case AV_CODEC_ID_VC1:
stream_type = STREAM_TYPE_VIDEO_VC1;
break;
case AV_CODEC_ID_MP2:
case AV_CODEC_ID_MP3:
if ( st->codecpar->sample_rate > 0
&& st->codecpar->sample_rate < 32000) {
stream_type = STREAM_TYPE_AUDIO_MPEG2;
} else {
stream_type = STREAM_TYPE_AUDIO_MPEG1;
}
break;
case AV_CODEC_ID_AAC:
stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
? STREAM_TYPE_AUDIO_AAC_LATM
: STREAM_TYPE_AUDIO_AAC;
break;
case AV_CODEC_ID_AAC_LATM:
stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
break;
case AV_CODEC_ID_AC3:
stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
? STREAM_TYPE_PRIVATE_DATA
: STREAM_TYPE_AUDIO_AC3;
break;
case AV_CODEC_ID_EAC3:
stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
? STREAM_TYPE_PRIVATE_DATA
: STREAM_TYPE_AUDIO_EAC3;
break;
case AV_CODEC_ID_DTS:
stream_type = STREAM_TYPE_AUDIO_DTS;
break;
case AV_CODEC_ID_TRUEHD:
stream_type = STREAM_TYPE_AUDIO_TRUEHD;
break;
case AV_CODEC_ID_OPUS:
stream_type = STREAM_TYPE_PRIVATE_DATA;
break;
case AV_CODEC_ID_TIMED_ID3:
stream_type = STREAM_TYPE_METADATA;
break;
case AV_CODEC_ID_DVB_SUBTITLE:
case AV_CODEC_ID_DVB_TELETEXT:
case AV_CODEC_ID_ARIB_CAPTION:
stream_type = STREAM_TYPE_PRIVATE_DATA;
break;
case AV_CODEC_ID_SMPTE_KLV:
if (st->codecpar->profile == FF_PROFILE_KLVA_SYNC) {
stream_type = STREAM_TYPE_METADATA;
} else {
stream_type = STREAM_TYPE_PRIVATE_DATA;
}
break;
default:
av_log_once(s, AV_LOG_WARNING, AV_LOG_DEBUG, &ts_st->data_st_warning,
"Stream %d, codec %s, is muxed as a private data stream "
"and may not be recognized upon reading.\n", st->index,
avcodec_get_name(st->codecpar->codec_id));
stream_type = STREAM_TYPE_PRIVATE_DATA;
break;
}
return stream_type;
}
static int get_m2ts_stream_type(AVFormatContext *s, AVStream *st)
{
int stream_type;
MpegTSWriteStream *ts_st = st->priv_data;
switch (st->codecpar->codec_id) {
case AV_CODEC_ID_MPEG2VIDEO:
stream_type = STREAM_TYPE_VIDEO_MPEG2;
break;
case AV_CODEC_ID_H264:
stream_type = STREAM_TYPE_VIDEO_H264;
break;
case AV_CODEC_ID_VC1:
stream_type = STREAM_TYPE_VIDEO_VC1;
break;
case AV_CODEC_ID_HEVC:
stream_type = STREAM_TYPE_VIDEO_HEVC;
break;
case AV_CODEC_ID_PCM_BLURAY:
stream_type = 0x80;
break;
case AV_CODEC_ID_AC3:
stream_type = 0x81;
break;
case AV_CODEC_ID_DTS:
stream_type = (st->codecpar->channels > 6) ? 0x85 : 0x82;
break;
case AV_CODEC_ID_TRUEHD:
stream_type = 0x83;
break;
case AV_CODEC_ID_EAC3:
stream_type = 0x84;
break;
case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
stream_type = 0x90;
break;
case AV_CODEC_ID_HDMV_TEXT_SUBTITLE:
stream_type = 0x92;
break;
default:
av_log_once(s, AV_LOG_WARNING, AV_LOG_DEBUG, &ts_st->data_st_warning,
"Stream %d, codec %s, is muxed as a private data stream "
"and may not be recognized upon reading.\n", st->index,
avcodec_get_name(st->codecpar->codec_id));
stream_type = STREAM_TYPE_PRIVATE_DATA;
break;
}
return stream_type;
}
static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
{
MpegTSWrite *ts = s->priv_data;
uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
int val, stream_type, i, err = 0;
q = data;
put16(&q, 0xe000 | service->pcr_pid);
program_info_length_ptr = q;
q += 2; /* patched after */
/* put program info here */
if (ts->m2ts_mode) {
put_registration_descriptor(&q, MKTAG('H', 'D', 'M', 'V'));
*q++ = 0x88; // descriptor_tag - hdmv_copy_control_descriptor
*q++ = 0x04; // descriptor_length
put16(&q, 0x0fff); // CA_System_ID
*q++ = 0xfc; // private_data_byte
*q++ = 0xfc; // private_data_byte
}
val = 0xf000 | (q - program_info_length_ptr - 2);
program_info_length_ptr[0] = val >> 8;
program_info_length_ptr[1] = val;
for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i];
MpegTSWriteStream *ts_st = st->priv_data;
AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
const char default_language[] = "und";
const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
enum AVCodecID codec_id = st->codecpar->codec_id;
if (s->nb_programs) {
int k, found = 0;
AVProgram *program = service->program;
for (k = 0; k < program->nb_stream_indexes; k++)
if (program->stream_index[k] == i) {
found = 1;
break;
}
if (!found)
continue;
}
if (q - data > SECTION_LENGTH - 32) {
err = 1;
break;
}
stream_type = ts->m2ts_mode ? get_m2ts_stream_type(s, st) : get_dvb_stream_type(s, st);
*q++ = stream_type;
put16(&q, 0xe000 | ts_st->pid);
desc_length_ptr = q;
q += 2; /* patched after */
/* write optional descriptors here */
switch (st->codecpar->codec_type) {
case AVMEDIA_TYPE_AUDIO:
if (codec_id == AV_CODEC_ID_AC3)
put_registration_descriptor(&q, MKTAG('A', 'C', '-', '3'));
if (codec_id == AV_CODEC_ID_EAC3)
put_registration_descriptor(&q, MKTAG('E', 'A', 'C', '3'));
if (ts->flags & MPEGTS_FLAG_SYSTEM_B) {
if (codec_id == AV_CODEC_ID_AC3) {
DVBAC3Descriptor *dvb_ac3_desc = ts_st->dvb_ac3_desc;
*q++=0x6a; // AC3 descriptor see A038 DVB SI
if (dvb_ac3_desc) {
int len = 1 +
!!(dvb_ac3_desc->component_type_flag) +
!!(dvb_ac3_desc->bsid_flag) +
!!(dvb_ac3_desc->mainid_flag) +
!!(dvb_ac3_desc->asvc_flag);
*q++ = len;
*q++ = dvb_ac3_desc->component_type_flag << 7 | dvb_ac3_desc->bsid_flag << 6 |
dvb_ac3_desc->mainid_flag << 5 | dvb_ac3_desc->asvc_flag << 4;
if (dvb_ac3_desc->component_type_flag) *q++ = dvb_ac3_desc->component_type;
if (dvb_ac3_desc->bsid_flag) *q++ = dvb_ac3_desc->bsid;
if (dvb_ac3_desc->mainid_flag) *q++ = dvb_ac3_desc->mainid;
if (dvb_ac3_desc->asvc_flag) *q++ = dvb_ac3_desc->asvc;
} else {
*q++=1; // 1 byte, all flags sets to 0
*q++=0; // omit all fields...
}
} else if (codec_id == AV_CODEC_ID_EAC3) {
*q++=0x7a; // EAC3 descriptor see A038 DVB SI
*q++=1; // 1 byte, all flags sets to 0
*q++=0; // omit all fields...
}
}
if (codec_id == AV_CODEC_ID_S302M)
put_registration_descriptor(&q, MKTAG('B', 'S', 'S', 'D'));
if (codec_id == AV_CODEC_ID_OPUS) {
/* 6 bytes registration descriptor, 4 bytes Opus audio descriptor */
if (q - data > SECTION_LENGTH - 6 - 4) {
err = 1;
break;
}
put_registration_descriptor(&q, MKTAG('O', 'p', 'u', 's'));
*q++ = 0x7f; /* DVB extension descriptor */
*q++ = 2;
*q++ = 0x80;
if (st->codecpar->extradata && st->codecpar->extradata_size >= 19) {
if (st->codecpar->extradata[18] == 0 && st->codecpar->channels <= 2) {
/* RTP mapping family */
*q++ = st->codecpar->channels;
} else if (st->codecpar->extradata[18] == 1 && st->codecpar->channels <= 8 &&
st->codecpar->extradata_size >= 21 + st->codecpar->channels) {
static const uint8_t coupled_stream_counts[9] = {
1, 0, 1, 1, 2, 2, 2, 3, 3
};
static const uint8_t channel_map_a[8][8] = {
{0},
{0, 1},
{0, 2, 1},
{0, 1, 2, 3},
{0, 4, 1, 2, 3},
{0, 4, 1, 2, 3, 5},
{0, 4, 1, 2, 3, 5, 6},
{0, 6, 1, 2, 3, 4, 5, 7},
};
static const uint8_t channel_map_b[8][8] = {
{0},
{0, 1},
{0, 1, 2},
{0, 1, 2, 3},
{0, 1, 2, 3, 4},
{0, 1, 2, 3, 4, 5},
{0, 1, 2, 3, 4, 5, 6},
{0, 1, 2, 3, 4, 5, 6, 7},
};
/* Vorbis mapping family */
if (st->codecpar->extradata[19] == st->codecpar->channels - coupled_stream_counts[st->codecpar->channels] &&
st->codecpar->extradata[20] == coupled_stream_counts[st->codecpar->channels] &&
memcmp(&st->codecpar->extradata[21], channel_map_a[st->codecpar->channels-1], st->codecpar->channels) == 0) {
*q++ = st->codecpar->channels;
} else if (st->codecpar->channels >= 2 && st->codecpar->extradata[19] == st->codecpar->channels &&
st->codecpar->extradata[20] == 0 &&
memcmp(&st->codecpar->extradata[21], channel_map_b[st->codecpar->channels-1], st->codecpar->channels) == 0) {
*q++ = st->codecpar->channels | 0x80;
} else {
/* Unsupported, could write an extended descriptor here */
av_log(s, AV_LOG_ERROR, "Unsupported Opus Vorbis-style channel mapping");
*q++ = 0xff;
}
} else {
/* Unsupported */
av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping for family %d", st->codecpar->extradata[18]);
*q++ = 0xff;
}
} else if (st->codecpar->channels <= 2) {
/* Assume RTP mapping family */
*q++ = st->codecpar->channels;
} else {
/* Unsupported */
av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping");
*q++ = 0xff;
}
}
if (language != default_language ||
st->disposition & (AV_DISPOSITION_CLEAN_EFFECTS |
AV_DISPOSITION_HEARING_IMPAIRED |
AV_DISPOSITION_VISUAL_IMPAIRED)) {
const char *p, *next;
uint8_t *len_ptr;
*q++ = ISO_639_LANGUAGE_DESCRIPTOR;
len_ptr = q++;
*len_ptr = 0;
for (p = next = language; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
if (q - data > SECTION_LENGTH - 4) {
err = 1;
break;
}
next = strchr(p, ',');
if (strlen(p) != 3 && (!next || next != p + 3))
continue; /* not a 3-letter code */
*q++ = *p++;
*q++ = *p++;
*q++ = *p++;
if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
*q++ = 0x01;
else if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
*q++ = 0x02;
else if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
*q++ = 0x03;
else
*q++ = 0; /* undefined type */
*len_ptr += 4;
}
if (*len_ptr == 0)
q -= 2; /* no language codes were written */
}
break;
case AVMEDIA_TYPE_SUBTITLE:
if (codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
uint8_t *len_ptr;
int extradata_copied = 0;
*q++ = 0x59; /* subtitling_descriptor */
len_ptr = q++;
while (strlen(language) >= 3) {
if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
err = 1;
break;
}
*q++ = *language++;
*q++ = *language++;
*q++ = *language++;
/* Skip comma */
if (*language != '\0')
language++;
if (st->codecpar->extradata_size - extradata_copied >= 5) {
*q++ = st->codecpar->extradata[extradata_copied + 4]; /* subtitling_type */
memcpy(q, st->codecpar->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
extradata_copied += 5;
q += 4;
} else {
/* subtitling_type:
* 0x10 - normal with no monitor aspect ratio criticality
* 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
*q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
if ((st->codecpar->extradata_size == 4) && (extradata_copied == 0)) {
/* support of old 4-byte extradata format */
memcpy(q, st->codecpar->extradata, 4); /* composition_page_id and ancillary_page_id */
extradata_copied += 4;
q += 4;
} else {
put16(&q, 1); /* composition_page_id */
put16(&q, 1); /* ancillary_page_id */
}
}
}
*len_ptr = q - len_ptr - 1;
} else if (codec_id == AV_CODEC_ID_DVB_TELETEXT) {
uint8_t *len_ptr = NULL;
int extradata_copied = 0;
/* The descriptor tag. teletext_descriptor */
*q++ = 0x56;
len_ptr = q++;
while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
*q++ = *language++;
*q++ = *language++;
*q++ = *language++;
/* Skip comma */
if (*language != '\0')
language++;
if (st->codecpar->extradata_size - 1 > extradata_copied) {
memcpy(q, st->codecpar->extradata + extradata_copied, 2);
extradata_copied += 2;
q += 2;
} else {
/* The Teletext descriptor:
* teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
* teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
* teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
*q++ = 0x08;
*q++ = 0x00;
}
}
*len_ptr = q - len_ptr - 1;
} else if (codec_id == AV_CODEC_ID_ARIB_CAPTION) {
if (put_arib_caption_descriptor(s, &q, st->codecpar) < 0)
break;
}
break;
case AVMEDIA_TYPE_VIDEO:
if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
put_registration_descriptor(&q, MKTAG('d', 'r', 'a', 'c'));
} else if (stream_type == STREAM_TYPE_VIDEO_VC1) {
put_registration_descriptor(&q, MKTAG('V', 'C', '-', '1'));
} else if (stream_type == STREAM_TYPE_VIDEO_HEVC && s->strict_std_compliance <= FF_COMPLIANCE_NORMAL) {
put_registration_descriptor(&q, MKTAG('H', 'E', 'V', 'C'));
} else if (stream_type == STREAM_TYPE_VIDEO_CAVS || stream_type == STREAM_TYPE_VIDEO_AVS2 ||
stream_type == STREAM_TYPE_VIDEO_AVS3) {
put_registration_descriptor(&q, MKTAG('A', 'V', 'S', 'V'));
}
break;
case AVMEDIA_TYPE_DATA:
if (codec_id == AV_CODEC_ID_SMPTE_KLV) {
put_registration_descriptor(&q, MKTAG('K', 'L', 'V', 'A'));
} else if (codec_id == AV_CODEC_ID_TIMED_ID3) {
const char *tag = "ID3 ";
*q++ = METADATA_DESCRIPTOR;
*q++ = 13;
put16(&q, 0xffff); /* metadata application format */
putbuf(&q, tag, strlen(tag));
*q++ = 0xff; /* metadata format */
putbuf(&q, tag, strlen(tag));
*q++ = 0; /* metadata service ID */
*q++ = 0xF; /* metadata_locator_record_flag|MPEG_carriage_flags|reserved */
}
break;
}
val = 0xf000 | (q - desc_length_ptr - 2);
desc_length_ptr[0] = val >> 8;
desc_length_ptr[1] = val;
}
if (err)
av_log(s, AV_LOG_ERROR,
"The PMT section cannot fit stream %d and all following streams.\n"
"Try reducing the number of languages in the audio streams "
"or the total number of streams.\n", i);
mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
data, q - data);
return 0;
}
static void mpegts_write_sdt(AVFormatContext *s)
{
MpegTSWrite *ts = s->priv_data;
MpegTSService *service;
uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
int i, running_status, free_ca_mode, val;
q = data;
put16(&q, ts->original_network_id);
*q++ = 0xff;
for (i = 0; i < ts->nb_services; i++) {
service = ts->services[i];
put16(&q, service->sid);
*q++ = 0xfc | 0x00; /* currently no EIT info */
desc_list_len_ptr = q;
q += 2;
running_status = 4; /* running */
free_ca_mode = 0;
/* write only one descriptor for the service name and provider */
*q++ = 0x48;
desc_len_ptr = q;
q++;
*q++ = ts->service_type;
putbuf(&q, service->provider_name, service->provider_name[0] + 1);
putbuf(&q, service->name, service->name[0] + 1);
desc_len_ptr[0] = q - desc_len_ptr - 1;
/* fill descriptor length */
val = (running_status << 13) | (free_ca_mode << 12) |
(q - desc_list_len_ptr - 2);
desc_list_len_ptr[0] = val >> 8;
desc_list_len_ptr[1] = val;
}
mpegts_write_section1(&ts->sdt, SDT_TID, ts->transport_stream_id, ts->tables_version, 0, 0,
data, q - data);
}
static void mpegts_write_nit(AVFormatContext *s)
{
MpegTSWrite *ts = s->priv_data;
uint8_t data[SECTION_LENGTH], *q, *desc_len_ptr, *loop_len_ptr;
q = data;
//network_descriptors_length
put16(&q, 0xf000 | (ts->provider_name[0] + 2));
//network_name_descriptor
*q++ = 0x40;
putbuf(&q, ts->provider_name, ts->provider_name[0] + 1);
//transport_stream_loop_length
loop_len_ptr = q;
q += 2;
put16(&q, ts->transport_stream_id);
put16(&q, ts->original_network_id);
//transport_descriptors_length
desc_len_ptr = q;
q += 2;
//service_list_descriptor
*q++ = 0x41;
*q++ = 3 * ts->nb_services;
for (int i = 0; i < ts->nb_services; i++) {
put16(&q, ts->services[i]->sid);
*q++ = ts->service_type;
}
//calculate lengths
put16(&desc_len_ptr, 0xf000 | q - (desc_len_ptr + 2));
put16(&loop_len_ptr, 0xf000 | q - (loop_len_ptr + 2));
mpegts_write_section1(&ts->nit, NIT_TID, ts->original_network_id, ts->tables_version, 0, 0,
data, q - data);
}
/* This stores a string in buf with the correct encoding and also sets the
* first byte as the length. !str is accepted for an empty string.
* If the string is already encoded, invalid UTF-8 or has no multibyte sequence
* then we keep it as is, otherwise we signal UTF-8 encoding. */
static int encode_str8(uint8_t *buf, const char *str)
{
size_t str_len;
if (!str)
str = "";
str_len = strlen(str);
if (str[0] && (unsigned)str[0] >= 0x20) { /* Make sure the string is not already encoded. */
const uint8_t *q = str;
int has_multibyte = 0;
while (*q) {
uint32_t code;
GET_UTF8(code, *q++, goto invalid;) /* Is it valid UTF-8? */
has_multibyte |= (code > 127); /* Does it have multibyte UTF-8 chars in it? */
}
if (has_multibyte) { /* If we have multibyte chars and valid UTF-8, then encode as such! */
if (str_len > 254)
return AVERROR(EINVAL);
buf[0] = str_len + 1;
buf[1] = 0x15;
memcpy(&buf[2], str, str_len);
return 0;
}
}
invalid:
/* Otherwise let's just encode the string as is! */
if (str_len > 255)
return AVERROR(EINVAL);
buf[0] = str_len;
memcpy(&buf[1], str, str_len);
return 0;
}
static int64_t get_pcr(const MpegTSWrite *ts)
{
return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
ts->first_pcr;
}
static void write_packet(AVFormatContext *s, const uint8_t *packet)
{
MpegTSWrite *ts = s->priv_data;
if (ts->m2ts_mode) {
int64_t pcr = get_pcr(s->priv_data);
uint32_t tp_extra_header = pcr % 0x3fffffff;
tp_extra_header = AV_RB32(&tp_extra_header);
avio_write(s->pb, (unsigned char *) &tp_extra_header,
sizeof(tp_extra_header));
}
avio_write(s->pb, packet, TS_PACKET_SIZE);
ts->total_size += TS_PACKET_SIZE;
}
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
{
AVFormatContext *ctx = s->opaque;
write_packet(ctx, packet);
}
static MpegTSService *mpegts_add_service(AVFormatContext *s, int sid,
const AVDictionary *metadata,
AVProgram *program)
{
MpegTSWrite *ts = s->priv_data;
MpegTSService *service;
AVDictionaryEntry *title, *provider;
char default_service_name[32];
const char *service_name;
const char *provider_name;
title = av_dict_get(metadata, "service_name", NULL, 0);
if (!title)
title = av_dict_get(metadata, "title", NULL, 0);
snprintf(default_service_name, sizeof(default_service_name), "%s%02d", DEFAULT_SERVICE_NAME, ts->nb_services + 1);
service_name = title ? title->value : default_service_name;
provider = av_dict_get(metadata, "service_provider", NULL, 0);
provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
service = av_mallocz(sizeof(MpegTSService));
if (!service)
return NULL;
service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
service->sid = sid;
service->pcr_pid = 0x1fff;
if (encode_str8(service->provider_name, provider_name) < 0 ||
encode_str8(service->name, service_name) < 0) {
av_log(s, AV_LOG_ERROR, "Too long service or provider name\n");
goto fail;
}