FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mpegtsenc.c
Date: 2026-09-10 12:58:17
Exec Total Coverage
Lines: 715 1430 50.0%
Functions: 35 41 85.4%
Branches: 355 918 38.7%

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