FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mpegtsenc.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 648 1351 48.0%
Functions: 31 39 79.5%
Branches: 313 864 36.2%

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/opt.h"
29
30 #include "libavcodec/ac3_parser_internal.h"
31 #include "libavcodec/avcodec.h"
32 #include "libavcodec/startcode.h"
33
34 #include "avformat.h"
35 #include "avio_internal.h"
36 #include "internal.h"
37 #include "mpegts.h"
38 #include "mux.h"
39
40 #define PCR_TIME_BASE 27000000
41
42 /* write DVB SI sections */
43
44 #define DVB_PRIVATE_NETWORK_START 0xff01
45
46 /*********************************************/
47 /* mpegts section writer */
48
49 typedef struct MpegTSSection {
50 int pid;
51 int cc;
52 int discontinuity;
53 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
54 void *opaque;
55 } MpegTSSection;
56
57 typedef struct MpegTSService {
58 MpegTSSection pmt; /* MPEG-2 PMT table context */
59 int sid; /* service ID */
60 uint8_t name[256];
61 uint8_t provider_name[256];
62 int pcr_pid;
63 AVProgram *program;
64 } MpegTSService;
65
66 // service_type values as defined in ETSI 300 468
67 enum {
68 MPEGTS_SERVICE_TYPE_DIGITAL_TV = 0x01,
69 MPEGTS_SERVICE_TYPE_DIGITAL_RADIO = 0x02,
70 MPEGTS_SERVICE_TYPE_TELETEXT = 0x03,
71 MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO = 0x0A,
72 MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV = 0x11,
73 MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV = 0x16,
74 MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV = 0x19,
75 MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV = 0x1F,
76 };
77 typedef struct MpegTSWrite {
78 const AVClass *av_class;
79 MpegTSSection pat; /* MPEG-2 PAT table */
80 MpegTSSection sdt; /* MPEG-2 SDT table context */
81 MpegTSSection nit; /* MPEG-2 NIT table context */
82 MpegTSService **services;
83 AVPacket *pkt;
84 int64_t sdt_period; /* SDT period in PCR time base */
85 int64_t pat_period; /* PAT/PMT period in PCR time base */
86 int64_t nit_period; /* NIT period in PCR time base */
87 int nb_services;
88 int64_t first_pcr;
89 int first_dts_checked;
90 int64_t next_pcr;
91 int mux_rate; ///< set to 1 when VBR
92 int pes_payload_size;
93 int64_t total_size;
94
95 int transport_stream_id;
96 int original_network_id;
97 int service_id;
98 int service_type;
99
100 int pmt_start_pid;
101 int start_pid;
102 int m2ts_mode;
103 int m2ts_video_pid;
104 int m2ts_audio_pid;
105 int m2ts_pgssub_pid;
106 int m2ts_textsub_pid;
107
108 int pcr_period_ms;
109 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
110 #define MPEGTS_FLAG_AAC_LATM 0x02
111 #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
112 #define MPEGTS_FLAG_SYSTEM_B 0x08
113 #define MPEGTS_FLAG_DISCONT 0x10
114 #define MPEGTS_FLAG_NIT 0x20
115 #define MPEGTS_FLAG_OMIT_RAI 0x40
116 int flags;
117 int copyts;
118 int tables_version;
119 int64_t pat_period_us;
120 int64_t sdt_period_us;
121 int64_t nit_period_us;
122 int64_t last_pat_ts;
123 int64_t last_sdt_ts;
124 int64_t last_nit_ts;
125
126 uint8_t provider_name[256];
127
128 int omit_video_pes_length;
129 } MpegTSWrite;
130
131 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
132 #define DEFAULT_PES_HEADER_FREQ 16
133 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
134
135 /* The section length is 12 bits. The first 2 are set to 0, the remaining
136 * 10 bits should not exceed 1021. */
137 #define SECTION_LENGTH 1020
138
139 /* NOTE: 4 bytes must be left at the end for the crc32 */
140 1244 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
141 {
142 unsigned int crc;
143 unsigned char packet[TS_PACKET_SIZE];
144 const unsigned char *buf_ptr;
145 unsigned char *q;
146 int first, b, len1, left;
147
148 2488 crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE),
149 1244 -1, buf, len - 4));
150
151 1244 buf[len - 4] = (crc >> 24) & 0xff;
152 1244 buf[len - 3] = (crc >> 16) & 0xff;
153 1244 buf[len - 2] = (crc >> 8) & 0xff;
154 1244 buf[len - 1] = crc & 0xff;
155
156 /* send each packet */
157 1244 buf_ptr = buf;
158
2/2
✓ Branch 0 taken 1244 times.
✓ Branch 1 taken 1244 times.
2488 while (len > 0) {
159 1244 first = buf == buf_ptr;
160 1244 q = packet;
161 1244 *q++ = 0x47;
162 1244 b = s->pid >> 8;
163
1/2
✓ Branch 0 taken 1244 times.
✗ Branch 1 not taken.
1244 if (first)
164 1244 b |= 0x40;
165 1244 *q++ = b;
166 1244 *q++ = s->pid;
167 1244 s->cc = s->cc + 1 & 0xf;
168 1244 *q++ = 0x10 | s->cc;
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1244 times.
1244 if (s->discontinuity) {
170 q[-1] |= 0x20;
171 *q++ = 1;
172 *q++ = 0x80;
173 s->discontinuity = 0;
174 }
175
1/2
✓ Branch 0 taken 1244 times.
✗ Branch 1 not taken.
1244 if (first)
176 1244 *q++ = 0; /* 0 offset */
177 1244 len1 = TS_PACKET_SIZE - (q - packet);
178
1/2
✓ Branch 0 taken 1244 times.
✗ Branch 1 not taken.
1244 if (len1 > len)
179 1244 len1 = len;
180 1244 memcpy(q, buf_ptr, len1);
181 1244 q += len1;
182 /* add known padding data */
183 1244 left = TS_PACKET_SIZE - (q - packet);
184
1/2
✓ Branch 0 taken 1244 times.
✗ Branch 1 not taken.
1244 if (left > 0)
185 1244 memset(q, 0xff, left);
186
187 1244 s->write_packet(s, packet);
188
189 1244 buf_ptr += len1;
190 1244 len -= len1;
191 }
192 1244 }
193
194 4985 static inline void put16(uint8_t **q_ptr, int val)
195 {
196 uint8_t *q;
197 4985 q = *q_ptr;
198 4985 *q++ = val >> 8;
199 4985 *q++ = val;
200 4985 *q_ptr = q;
201 4985 }
202
203 1244 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
204 int version, int sec_num, int last_sec_num,
205 uint8_t *buf, int len)
206 {
207 uint8_t section[1024], *q;
208 unsigned int tot_len;
209 /* reserved_future_use field must be set to 1 for SDT and NIT */
210
3/4
✓ Branch 0 taken 1084 times.
✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1084 times.
1244 unsigned int flags = (tid == SDT_TID || tid == NIT_TID) ? 0xf000 : 0xb000;
211
212 1244 tot_len = 3 + 5 + len + 4;
213 /* check if not too big */
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1244 times.
1244 if (tot_len > 1024)
215 return AVERROR_INVALIDDATA;
216
217 1244 q = section;
218 1244 *q++ = tid;
219 1244 put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
220 1244 put16(&q, id);
221 1244 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
222 1244 *q++ = sec_num;
223 1244 *q++ = last_sec_num;
224 1244 memcpy(q, buf, len);
225
226 1244 mpegts_write_section(s, section, tot_len);
227 1244 return 0;
228 }
229
230 /*********************************************/
231 /* mpegts writer */
232
233 #define DEFAULT_PROVIDER_NAME "FFmpeg"
234 #define DEFAULT_SERVICE_NAME "Service"
235
236 /* we retransmit the SI info at this rate */
237 #define SDT_RETRANS_TIME 500
238 #define PAT_RETRANS_TIME 100
239 #define PCR_RETRANS_TIME 20
240 #define NIT_RETRANS_TIME 500
241
242 typedef struct MpegTSWriteStream {
243 int pid; /* stream associated pid */
244 int cc;
245 int discontinuity;
246 int payload_size;
247 int first_timestamp_checked; ///< first pts/dts check needed
248 int prev_payload_key;
249 int64_t payload_pts;
250 int64_t payload_dts;
251 int payload_flags;
252 uint8_t *payload;
253 AVFormatContext *amux;
254 int data_st_warning;
255
256 int64_t pcr_period; /* PCR period in PCR time base */
257 int64_t last_pcr;
258
259 /* For Opus */
260 int opus_queued_samples;
261 int opus_pending_trim_start;
262
263 DVBAC3Descriptor *dvb_ac3_desc;
264 } MpegTSWriteStream;
265
266 542 static void mpegts_write_pat(AVFormatContext *s)
267 {
268 542 MpegTSWrite *ts = s->priv_data;
269 MpegTSService *service;
270 uint8_t data[SECTION_LENGTH], *q;
271 int i;
272
273 542 q = data;
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 if (ts->flags & MPEGTS_FLAG_NIT) {
275 put16(&q, 0x0000);
276 put16(&q, NIT_PID);
277 }
278
2/2
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 542 times.
1084 for (i = 0; i < ts->nb_services; i++) {
279 542 service = ts->services[i];
280 542 put16(&q, service->sid);
281 542 put16(&q, 0xe000 | service->pmt.pid);
282 }
283 542 mpegts_write_section1(&ts->pat, PAT_TID, ts->transport_stream_id, ts->tables_version, 0, 0,
284 542 data, q - data);
285 542 }
286
287 320 static void putbuf(uint8_t **q_ptr, const uint8_t *buf, size_t len)
288 {
289 320 memcpy(*q_ptr, buf, len);
290 320 *q_ptr += len;
291 320 }
292
293 static int put_arib_caption_descriptor(AVFormatContext *s, uint8_t **q_ptr,
294 AVCodecParameters *codecpar)
295 {
296 uint8_t stream_identifier;
297 uint16_t data_component_id;
298 uint8_t *q = *q_ptr;
299
300 switch (codecpar->profile) {
301 case FF_PROFILE_ARIB_PROFILE_A:
302 stream_identifier = 0x30;
303 data_component_id = 0x0008;
304 break;
305 case FF_PROFILE_ARIB_PROFILE_C:
306 stream_identifier = 0x87;
307 data_component_id = 0x0012;
308 break;
309 default:
310 av_log(s, AV_LOG_ERROR,
311 "Unset/unknown ARIB caption profile %d utilized!\n",
312 codecpar->profile);
313 return AVERROR_INVALIDDATA;
314 }
315
316 // stream_identifier_descriptor
317 *q++ = 0x52; // descriptor_tag
318 *q++ = 1; // descriptor_length
319 *q++ = stream_identifier; // component_tag: stream_identifier
320
321 // data_component_descriptor, defined in ARIB STD-B10, part 2, 6.2.20
322 *q++ = 0xFD; // descriptor_tag: ARIB data coding type descriptor
323 *q++ = 3; // descriptor_length
324 put16(&q, data_component_id); // data_component_id
325 // additional_arib_caption_info: defined in ARIB STD-B24, fascicle 1, Part 3, 9.6.1
326 // Here we utilize a pre-defined set of values defined in ARIB TR-B14,
327 // Fascicle 2, 4.2.8.5 for PMT usage, with the reserved bits in the middle
328 // set to 1 (as that is what every broadcaster seems to be doing in
329 // production).
330 *q++ = 0x3D; // DMF('0011'), Reserved('11'), Timing('01')
331
332 *q_ptr = q;
333
334 return 0;
335 }
336
337 52 static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
338 {
339 52 uint8_t *q = *q_ptr;
340 52 *q++ = REGISTRATION_DESCRIPTOR;
341 52 *q++ = 4;
342 52 *q++ = tag;
343 52 *q++ = tag >> 8;
344 52 *q++ = tag >> 16;
345 52 *q++ = tag >> 24;
346 52 *q_ptr = q;
347 52 }
348
349 551 static int get_dvb_stream_type(AVFormatContext *s, AVStream *st)
350 {
351 551 MpegTSWrite *ts = s->priv_data;
352 551 MpegTSWriteStream *ts_st = st->priv_data;
353 int stream_type;
354
355
4/21
✓ Branch 0 taken 9 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 taken 448 times.
✗ Branch 10 not taken.
✗ 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 taken 52 times.
551 switch (st->codecpar->codec_id) {
356 9 case AV_CODEC_ID_MPEG1VIDEO:
357 case AV_CODEC_ID_MPEG2VIDEO:
358 9 stream_type = STREAM_TYPE_VIDEO_MPEG2;
359 9 break;
360 case AV_CODEC_ID_MPEG4:
361 stream_type = STREAM_TYPE_VIDEO_MPEG4;
362 break;
363 42 case AV_CODEC_ID_H264:
364 42 stream_type = STREAM_TYPE_VIDEO_H264;
365 42 break;
366 case AV_CODEC_ID_HEVC:
367 stream_type = STREAM_TYPE_VIDEO_HEVC;
368 break;
369 case AV_CODEC_ID_CAVS:
370 stream_type = STREAM_TYPE_VIDEO_CAVS;
371 break;
372 case AV_CODEC_ID_AVS2:
373 stream_type = STREAM_TYPE_VIDEO_AVS2;
374 break;
375 case AV_CODEC_ID_AVS3:
376 stream_type = STREAM_TYPE_VIDEO_AVS3;
377 break;
378 case AV_CODEC_ID_DIRAC:
379 stream_type = STREAM_TYPE_VIDEO_DIRAC;
380 break;
381 case AV_CODEC_ID_VC1:
382 stream_type = STREAM_TYPE_VIDEO_VC1;
383 break;
384 448 case AV_CODEC_ID_MP2:
385 case AV_CODEC_ID_MP3:
386
1/2
✓ Branch 0 taken 448 times.
✗ Branch 1 not taken.
448 if ( st->codecpar->sample_rate > 0
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448 times.
448 && st->codecpar->sample_rate < 32000) {
388 stream_type = STREAM_TYPE_AUDIO_MPEG2;
389 } else {
390 448 stream_type = STREAM_TYPE_AUDIO_MPEG1;
391 }
392 448 break;
393 case AV_CODEC_ID_AAC:
394 stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
395 ? STREAM_TYPE_AUDIO_AAC_LATM
396 : STREAM_TYPE_AUDIO_AAC;
397 break;
398 case AV_CODEC_ID_AAC_LATM:
399 stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
400 break;
401 case AV_CODEC_ID_AC3:
402 stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
403 ? STREAM_TYPE_PRIVATE_DATA
404 : STREAM_TYPE_AUDIO_AC3;
405 break;
406 case AV_CODEC_ID_EAC3:
407 stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
408 ? STREAM_TYPE_PRIVATE_DATA
409 : STREAM_TYPE_AUDIO_EAC3;
410 break;
411 case AV_CODEC_ID_DTS:
412 stream_type = STREAM_TYPE_AUDIO_DTS;
413 break;
414 case AV_CODEC_ID_TRUEHD:
415 stream_type = STREAM_TYPE_AUDIO_TRUEHD;
416 break;
417 case AV_CODEC_ID_OPUS:
418 stream_type = STREAM_TYPE_PRIVATE_DATA;
419 break;
420 case AV_CODEC_ID_TIMED_ID3:
421 stream_type = STREAM_TYPE_METADATA;
422 break;
423 case AV_CODEC_ID_DVB_SUBTITLE:
424 case AV_CODEC_ID_DVB_TELETEXT:
425 case AV_CODEC_ID_ARIB_CAPTION:
426 stream_type = STREAM_TYPE_PRIVATE_DATA;
427 break;
428 case AV_CODEC_ID_SMPTE_KLV:
429 if (st->codecpar->profile == FF_PROFILE_KLVA_SYNC) {
430 stream_type = STREAM_TYPE_METADATA;
431 } else {
432 stream_type = STREAM_TYPE_PRIVATE_DATA;
433 }
434 break;
435 52 default:
436 52 av_log_once(s, AV_LOG_WARNING, AV_LOG_DEBUG, &ts_st->data_st_warning,
437 "Stream %d, codec %s, is muxed as a private data stream "
438 "and may not be recognized upon reading.\n", st->index,
439 52 avcodec_get_name(st->codecpar->codec_id));
440 52 stream_type = STREAM_TYPE_PRIVATE_DATA;
441 52 break;
442 }
443
444 551 return stream_type;
445 }
446
447 static int get_m2ts_stream_type(AVFormatContext *s, AVStream *st)
448 {
449 int stream_type;
450 MpegTSWriteStream *ts_st = st->priv_data;
451
452 switch (st->codecpar->codec_id) {
453 case AV_CODEC_ID_MPEG2VIDEO:
454 stream_type = STREAM_TYPE_VIDEO_MPEG2;
455 break;
456 case AV_CODEC_ID_H264:
457 stream_type = STREAM_TYPE_VIDEO_H264;
458 break;
459 case AV_CODEC_ID_VC1:
460 stream_type = STREAM_TYPE_VIDEO_VC1;
461 break;
462 case AV_CODEC_ID_HEVC:
463 stream_type = STREAM_TYPE_VIDEO_HEVC;
464 break;
465 case AV_CODEC_ID_PCM_BLURAY:
466 stream_type = 0x80;
467 break;
468 case AV_CODEC_ID_AC3:
469 stream_type = 0x81;
470 break;
471 case AV_CODEC_ID_DTS:
472 stream_type = (st->codecpar->ch_layout.nb_channels > 6) ? 0x85 : 0x82;
473 break;
474 case AV_CODEC_ID_TRUEHD:
475 stream_type = 0x83;
476 break;
477 case AV_CODEC_ID_EAC3:
478 stream_type = 0x84;
479 break;
480 case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
481 stream_type = 0x90;
482 break;
483 case AV_CODEC_ID_HDMV_TEXT_SUBTITLE:
484 stream_type = 0x92;
485 break;
486 default:
487 av_log_once(s, AV_LOG_WARNING, AV_LOG_DEBUG, &ts_st->data_st_warning,
488 "Stream %d, codec %s, is muxed as a private data stream "
489 "and may not be recognized upon reading.\n", st->index,
490 avcodec_get_name(st->codecpar->codec_id));
491 stream_type = STREAM_TYPE_PRIVATE_DATA;
492 break;
493 }
494
495 return stream_type;
496 }
497
498 542 static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
499 {
500 542 MpegTSWrite *ts = s->priv_data;
501 uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
502 542 int val, stream_type, i, err = 0;
503
504 542 q = data;
505 542 put16(&q, 0xe000 | service->pcr_pid);
506
507 542 program_info_length_ptr = q;
508 542 q += 2; /* patched after */
509
510 /* put program info here */
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 if (ts->m2ts_mode) {
512 put_registration_descriptor(&q, MKTAG('H', 'D', 'M', 'V'));
513 *q++ = 0x88; // descriptor_tag - hdmv_copy_control_descriptor
514 *q++ = 0x04; // descriptor_length
515 put16(&q, 0x0fff); // CA_System_ID
516 *q++ = 0xfc; // private_data_byte
517 *q++ = 0xfc; // private_data_byte
518 }
519
520 542 val = 0xf000 | (q - program_info_length_ptr - 2);
521 542 program_info_length_ptr[0] = val >> 8;
522 542 program_info_length_ptr[1] = val;
523
524
2/2
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 542 times.
1093 for (i = 0; i < s->nb_streams; i++) {
525 551 AVStream *st = s->streams[i];
526 551 MpegTSWriteStream *ts_st = st->priv_data;
527 551 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
528 551 const char default_language[] = "und";
529
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 509 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
551 const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
530 551 enum AVCodecID codec_id = st->codecpar->codec_id;
531
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 551 times.
551 if (s->nb_programs) {
533 int k, found = 0;
534 AVProgram *program = service->program;
535
536 for (k = 0; k < program->nb_stream_indexes; k++)
537 if (program->stream_index[k] == i) {
538 found = 1;
539 break;
540 }
541
542 if (!found)
543 continue;
544 }
545
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 551 times.
551 if (q - data > SECTION_LENGTH - 32) {
547 err = 1;
548 break;
549 }
550
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 551 times.
551 stream_type = ts->m2ts_mode ? get_m2ts_stream_type(s, st) : get_dvb_stream_type(s, st);
552
553 551 *q++ = stream_type;
554 551 put16(&q, 0xe000 | ts_st->pid);
555 551 desc_length_ptr = q;
556 551 q += 2; /* patched after */
557
558 /* write optional descriptors here */
559
2/5
✓ Branch 0 taken 500 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
551 switch (st->codecpar->codec_type) {
560 500 case AVMEDIA_TYPE_AUDIO:
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 500 times.
500 if (codec_id == AV_CODEC_ID_AC3)
562 put_registration_descriptor(&q, MKTAG('A', 'C', '-', '3'));
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 500 times.
500 if (codec_id == AV_CODEC_ID_EAC3)
564 put_registration_descriptor(&q, MKTAG('E', 'A', 'C', '3'));
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 500 times.
500 if (ts->flags & MPEGTS_FLAG_SYSTEM_B) {
566 if (codec_id == AV_CODEC_ID_AC3) {
567 DVBAC3Descriptor *dvb_ac3_desc = ts_st->dvb_ac3_desc;
568
569 *q++=0x6a; // AC3 descriptor see A038 DVB SI
570 if (dvb_ac3_desc) {
571 int len = 1 +
572 !!(dvb_ac3_desc->component_type_flag) +
573 !!(dvb_ac3_desc->bsid_flag) +
574 !!(dvb_ac3_desc->mainid_flag) +
575 !!(dvb_ac3_desc->asvc_flag);
576
577 *q++ = len;
578 *q++ = dvb_ac3_desc->component_type_flag << 7 | dvb_ac3_desc->bsid_flag << 6 |
579 dvb_ac3_desc->mainid_flag << 5 | dvb_ac3_desc->asvc_flag << 4;
580
581 if (dvb_ac3_desc->component_type_flag) *q++ = dvb_ac3_desc->component_type;
582 if (dvb_ac3_desc->bsid_flag) *q++ = dvb_ac3_desc->bsid;
583 if (dvb_ac3_desc->mainid_flag) *q++ = dvb_ac3_desc->mainid;
584 if (dvb_ac3_desc->asvc_flag) *q++ = dvb_ac3_desc->asvc;
585 } else {
586 *q++=1; // 1 byte, all flags sets to 0
587 *q++=0; // omit all fields...
588 }
589 } else if (codec_id == AV_CODEC_ID_EAC3) {
590 *q++=0x7a; // EAC3 descriptor see A038 DVB SI
591 *q++=1; // 1 byte, all flags sets to 0
592 *q++=0; // omit all fields...
593 }
594 }
595
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 448 times.
500 if (codec_id == AV_CODEC_ID_S302M)
596 52 put_registration_descriptor(&q, MKTAG('B', 'S', 'S', 'D'));
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 500 times.
500 if (codec_id == AV_CODEC_ID_OPUS) {
598 int ch = st->codecpar->ch_layout.nb_channels;
599
600 /* 6 bytes registration descriptor, 4 bytes Opus audio descriptor */
601 if (q - data > SECTION_LENGTH - 6 - 4) {
602 err = 1;
603 break;
604 }
605
606 put_registration_descriptor(&q, MKTAG('O', 'p', 'u', 's'));
607
608 *q++ = 0x7f; /* DVB extension descriptor */
609 *q++ = 2;
610 *q++ = 0x80;
611
612 if (st->codecpar->extradata && st->codecpar->extradata_size >= 19) {
613 if (st->codecpar->extradata[18] == 0 && ch <= 2) {
614 /* RTP mapping family */
615 *q++ = ch;
616 } else if (st->codecpar->extradata[18] == 1 && ch <= 8 &&
617 st->codecpar->extradata_size >= 21 + ch) {
618 static const uint8_t coupled_stream_counts[9] = {
619 1, 0, 1, 1, 2, 2, 2, 3, 3
620 };
621 static const uint8_t channel_map_a[8][8] = {
622 {0},
623 {0, 1},
624 {0, 2, 1},
625 {0, 1, 2, 3},
626 {0, 4, 1, 2, 3},
627 {0, 4, 1, 2, 3, 5},
628 {0, 4, 1, 2, 3, 5, 6},
629 {0, 6, 1, 2, 3, 4, 5, 7},
630 };
631 static const uint8_t channel_map_b[8][8] = {
632 {0},
633 {0, 1},
634 {0, 1, 2},
635 {0, 1, 2, 3},
636 {0, 1, 2, 3, 4},
637 {0, 1, 2, 3, 4, 5},
638 {0, 1, 2, 3, 4, 5, 6},
639 {0, 1, 2, 3, 4, 5, 6, 7},
640 };
641 /* Vorbis mapping family */
642
643 if (st->codecpar->extradata[19] == ch - coupled_stream_counts[ch] &&
644 st->codecpar->extradata[20] == coupled_stream_counts[ch] &&
645 memcmp(&st->codecpar->extradata[21], channel_map_a[ch - 1], ch) == 0) {
646 *q++ = ch;
647 } else if (ch >= 2 && st->codecpar->extradata[19] == ch &&
648 st->codecpar->extradata[20] == 0 &&
649 memcmp(&st->codecpar->extradata[21], channel_map_b[ch - 1], ch) == 0) {
650 *q++ = ch | 0x80;
651 } else {
652 /* Unsupported, could write an extended descriptor here */
653 av_log(s, AV_LOG_ERROR, "Unsupported Opus Vorbis-style channel mapping");
654 *q++ = 0xff;
655 }
656 } else {
657 /* Unsupported */
658 av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping for family %d", st->codecpar->extradata[18]);
659 *q++ = 0xff;
660 }
661 } else if (ch <= 2) {
662 /* Assume RTP mapping family */
663 *q++ = ch;
664 } else {
665 /* Unsupported */
666 av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping");
667 *q++ = 0xff;
668 }
669 }
670
671
1/2
✓ Branch 0 taken 500 times.
✗ Branch 1 not taken.
500 if (language != default_language ||
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 500 times.
500 st->disposition & (AV_DISPOSITION_CLEAN_EFFECTS |
673 AV_DISPOSITION_HEARING_IMPAIRED |
674 AV_DISPOSITION_VISUAL_IMPAIRED)) {
675 const char *p, *next;
676 uint8_t *len_ptr;
677
678 *q++ = ISO_639_LANGUAGE_DESCRIPTOR;
679 len_ptr = q++;
680 *len_ptr = 0;
681
682 for (p = next = language; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
683 if (q - data > SECTION_LENGTH - 4) {
684 err = 1;
685 break;
686 }
687 next = strchr(p, ',');
688 if (strlen(p) != 3 && (!next || next != p + 3))
689 continue; /* not a 3-letter code */
690
691 *q++ = *p++;
692 *q++ = *p++;
693 *q++ = *p++;
694
695 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
696 *q++ = 0x01;
697 else if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
698 *q++ = 0x02;
699 else if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
700 *q++ = 0x03;
701 else
702 *q++ = 0; /* undefined type */
703
704 *len_ptr += 4;
705 }
706
707 if (*len_ptr == 0)
708 q -= 2; /* no language codes were written */
709 }
710 500 break;
711 case AVMEDIA_TYPE_SUBTITLE:
712 if (codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
713 uint8_t *len_ptr;
714 int extradata_copied = 0;
715
716 *q++ = 0x59; /* subtitling_descriptor */
717 len_ptr = q++;
718
719 while (strlen(language) >= 3) {
720 if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
721 err = 1;
722 break;
723 }
724 *q++ = *language++;
725 *q++ = *language++;
726 *q++ = *language++;
727 /* Skip comma */
728 if (*language != '\0')
729 language++;
730
731 if (st->codecpar->extradata_size - extradata_copied >= 5) {
732 *q++ = st->codecpar->extradata[extradata_copied + 4]; /* subtitling_type */
733 memcpy(q, st->codecpar->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
734 extradata_copied += 5;
735 q += 4;
736 } else {
737 /* subtitling_type:
738 * 0x10 - normal with no monitor aspect ratio criticality
739 * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
740 *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
741 if ((st->codecpar->extradata_size == 4) && (extradata_copied == 0)) {
742 /* support of old 4-byte extradata format */
743 memcpy(q, st->codecpar->extradata, 4); /* composition_page_id and ancillary_page_id */
744 extradata_copied += 4;
745 q += 4;
746 } else {
747 put16(&q, 1); /* composition_page_id */
748 put16(&q, 1); /* ancillary_page_id */
749 }
750 }
751 }
752
753 *len_ptr = q - len_ptr - 1;
754 } else if (codec_id == AV_CODEC_ID_DVB_TELETEXT) {
755 uint8_t *len_ptr = NULL;
756 int extradata_copied = 0;
757
758 /* The descriptor tag. teletext_descriptor */
759 *q++ = 0x56;
760 len_ptr = q++;
761
762 while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
763 *q++ = *language++;
764 *q++ = *language++;
765 *q++ = *language++;
766 /* Skip comma */
767 if (*language != '\0')
768 language++;
769
770 if (st->codecpar->extradata_size - 1 > extradata_copied) {
771 memcpy(q, st->codecpar->extradata + extradata_copied, 2);
772 extradata_copied += 2;
773 q += 2;
774 } else {
775 /* The Teletext descriptor:
776 * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
777 * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
778 * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
779 *q++ = 0x08;
780 *q++ = 0x00;
781 }
782 }
783
784 *len_ptr = q - len_ptr - 1;
785 } else if (codec_id == AV_CODEC_ID_ARIB_CAPTION) {
786 if (put_arib_caption_descriptor(s, &q, st->codecpar) < 0)
787 break;
788 }
789 break;
790 51 case AVMEDIA_TYPE_VIDEO:
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
792 put_registration_descriptor(&q, MKTAG('d', 'r', 'a', 'c'));
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 } else if (stream_type == STREAM_TYPE_VIDEO_VC1) {
794 put_registration_descriptor(&q, MKTAG('V', 'C', '-', '1'));
795
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
51 } else if (stream_type == STREAM_TYPE_VIDEO_HEVC && s->strict_std_compliance <= FF_COMPLIANCE_NORMAL) {
796 put_registration_descriptor(&q, MKTAG('H', 'E', 'V', 'C'));
797
3/6
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 51 times.
51 } else if (stream_type == STREAM_TYPE_VIDEO_CAVS || stream_type == STREAM_TYPE_VIDEO_AVS2 ||
798 stream_type == STREAM_TYPE_VIDEO_AVS3) {
799 put_registration_descriptor(&q, MKTAG('A', 'V', 'S', 'V'));
800 }
801 51 break;
802 case AVMEDIA_TYPE_DATA:
803 if (codec_id == AV_CODEC_ID_SMPTE_KLV) {
804 put_registration_descriptor(&q, MKTAG('K', 'L', 'V', 'A'));
805 } else if (codec_id == AV_CODEC_ID_TIMED_ID3) {
806 const char *tag = "ID3 ";
807 *q++ = METADATA_DESCRIPTOR;
808 *q++ = 13;
809 put16(&q, 0xffff); /* metadata application format */
810 putbuf(&q, tag, strlen(tag));
811 *q++ = 0xff; /* metadata format */
812 putbuf(&q, tag, strlen(tag));
813 *q++ = 0; /* metadata service ID */
814 *q++ = 0xF; /* metadata_locator_record_flag|MPEG_carriage_flags|reserved */
815 }
816 break;
817 }
818
819 551 val = 0xf000 | (q - desc_length_ptr - 2);
820 551 desc_length_ptr[0] = val >> 8;
821 551 desc_length_ptr[1] = val;
822 }
823
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 if (err)
825 av_log(s, AV_LOG_ERROR,
826 "The PMT section cannot fit stream %d and all following streams.\n"
827 "Try reducing the number of languages in the audio streams "
828 "or the total number of streams.\n", i);
829
830 542 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
831 542 data, q - data);
832 542 return 0;
833 }
834
835 160 static void mpegts_write_sdt(AVFormatContext *s)
836 {
837 160 MpegTSWrite *ts = s->priv_data;
838 MpegTSService *service;
839 uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
840 int i, running_status, free_ca_mode, val;
841
842 160 q = data;
843 160 put16(&q, ts->original_network_id);
844 160 *q++ = 0xff;
845
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 160 times.
320 for (i = 0; i < ts->nb_services; i++) {
846 160 service = ts->services[i];
847 160 put16(&q, service->sid);
848 160 *q++ = 0xfc | 0x00; /* currently no EIT info */
849 160 desc_list_len_ptr = q;
850 160 q += 2;
851 160 running_status = 4; /* running */
852 160 free_ca_mode = 0;
853
854 /* write only one descriptor for the service name and provider */
855 160 *q++ = 0x48;
856 160 desc_len_ptr = q;
857 160 q++;
858 160 *q++ = ts->service_type;
859 160 putbuf(&q, service->provider_name, service->provider_name[0] + 1);
860 160 putbuf(&q, service->name, service->name[0] + 1);
861 160 desc_len_ptr[0] = q - desc_len_ptr - 1;
862
863 /* fill descriptor length */
864 160 val = (running_status << 13) | (free_ca_mode << 12) |
865 160 (q - desc_list_len_ptr - 2);
866 160 desc_list_len_ptr[0] = val >> 8;
867 160 desc_list_len_ptr[1] = val;
868 }
869 160 mpegts_write_section1(&ts->sdt, SDT_TID, ts->transport_stream_id, ts->tables_version, 0, 0,
870 160 data, q - data);
871 160 }
872
873 static void mpegts_write_nit(AVFormatContext *s)
874 {
875 MpegTSWrite *ts = s->priv_data;
876 uint8_t data[SECTION_LENGTH], *q, *desc_len_ptr, *loop_len_ptr;
877
878 q = data;
879
880 //network_descriptors_length
881 put16(&q, 0xf000 | (ts->provider_name[0] + 2));
882
883 //network_name_descriptor
884 *q++ = 0x40;
885 putbuf(&q, ts->provider_name, ts->provider_name[0] + 1);
886
887 //transport_stream_loop_length
888 loop_len_ptr = q;
889 q += 2;
890
891 put16(&q, ts->transport_stream_id);
892 put16(&q, ts->original_network_id);
893
894 //transport_descriptors_length
895 desc_len_ptr = q;
896 q += 2;
897
898 //service_list_descriptor
899 *q++ = 0x41;
900 *q++ = 3 * ts->nb_services;
901 for (int i = 0; i < ts->nb_services; i++) {
902 put16(&q, ts->services[i]->sid);
903 *q++ = ts->service_type;
904 }
905
906 //calculate lengths
907 put16(&desc_len_ptr, 0xf000 | q - (desc_len_ptr + 2));
908 put16(&loop_len_ptr, 0xf000 | q - (loop_len_ptr + 2));
909
910 mpegts_write_section1(&ts->nit, NIT_TID, ts->original_network_id, ts->tables_version, 0, 0,
911 data, q - data);
912 }
913
914 /* This stores a string in buf with the correct encoding and also sets the
915 * first byte as the length. !str is accepted for an empty string.
916 * If the string is already encoded, invalid UTF-8 or has no multibyte sequence
917 * then we keep it as is, otherwise we signal UTF-8 encoding. */
918 60 static int encode_str8(uint8_t *buf, const char *str)
919 {
920 size_t str_len;
921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!str)
922 str = "";
923 60 str_len = strlen(str);
924
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
60 if (str[0] && (unsigned)str[0] >= 0x20) { /* Make sure the string is not already encoded. */
925 60 const uint8_t *q = str;
926 60 int has_multibyte = 0;
927
2/2
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 60 times.
479 while (*q) {
928 uint32_t code;
929
3/8
✓ Branch 0 taken 419 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 419 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 419 times.
419 GET_UTF8(code, *q++, goto invalid;) /* Is it valid UTF-8? */
930 419 has_multibyte |= (code > 127); /* Does it have multibyte UTF-8 chars in it? */
931 }
932
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (has_multibyte) { /* If we have multibyte chars and valid UTF-8, then encode as such! */
933 if (str_len > 254)
934 return AVERROR(EINVAL);
935 buf[0] = str_len + 1;
936 buf[1] = 0x15;
937 memcpy(&buf[2], str, str_len);
938 return 0;
939 }
940 }
941 60 invalid:
942 /* Otherwise let's just encode the string as is! */
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (str_len > 255)
944 return AVERROR(EINVAL);
945 60 buf[0] = str_len;
946 60 memcpy(&buf[1], str, str_len);
947 60 return 0;
948 }
949
950 static int64_t get_pcr(const MpegTSWrite *ts)
951 {
952 return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
953 ts->first_pcr;
954 }
955
956 65814 static void write_packet(AVFormatContext *s, const uint8_t *packet)
957 {
958 65814 MpegTSWrite *ts = s->priv_data;
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65814 times.
65814 if (ts->m2ts_mode) {
960 int64_t pcr = get_pcr(s->priv_data);
961 uint32_t tp_extra_header = pcr % 0x3fffffff;
962 tp_extra_header = AV_RB32(&tp_extra_header);
963 avio_write(s->pb, (unsigned char *) &tp_extra_header,
964 sizeof(tp_extra_header));
965 }
966 65814 avio_write(s->pb, packet, TS_PACKET_SIZE);
967 65814 ts->total_size += TS_PACKET_SIZE;
968 65814 }
969
970 1244 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
971 {
972 1244 AVFormatContext *ctx = s->opaque;
973 1244 write_packet(ctx, packet);
974 1244 }
975
976 20 static MpegTSService *mpegts_add_service(AVFormatContext *s, int sid,
977 const AVDictionary *metadata,
978 AVProgram *program)
979 {
980 20 MpegTSWrite *ts = s->priv_data;
981 MpegTSService *service;
982 AVDictionaryEntry *title, *provider;
983 char default_service_name[32];
984 const char *service_name;
985 const char *provider_name;
986
987 20 title = av_dict_get(metadata, "service_name", NULL, 0);
988
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (!title)
989 20 title = av_dict_get(metadata, "title", NULL, 0);
990 20 snprintf(default_service_name, sizeof(default_service_name), "%s%02d", DEFAULT_SERVICE_NAME, ts->nb_services + 1);
991
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19 times.
20 service_name = title ? title->value : default_service_name;
992 20 provider = av_dict_get(metadata, "service_provider", NULL, 0);
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
994
995 20 service = av_mallocz(sizeof(MpegTSService));
996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!service)
997 return NULL;
998 20 service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
999 20 service->sid = sid;
1000 20 service->pcr_pid = 0x1fff;
1001
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
40 if (encode_str8(service->provider_name, provider_name) < 0 ||
1002 20 encode_str8(service->name, service_name) < 0) {
1003 av_log(s, AV_LOG_ERROR, "Too long service or provider name\n");
1004 goto fail;
1005 }
1006
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
1007 goto fail;
1008
1009 20 service->pmt.write_packet = section_write_packet;
1010 20 service->pmt.opaque = s;
1011 20 service->pmt.cc = 15;
1012 20 service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
1013 20 service->program = program;
1014
1015 20 return service;
1016 fail:
1017 av_free(service);
1018 return NULL;
1019 }
1020
1021 20 static void enable_pcr_generation_for_stream(AVFormatContext *s, AVStream *pcr_st)
1022 {
1023 20 MpegTSWrite *ts = s->priv_data;
1024 20 MpegTSWriteStream *ts_st = pcr_st->priv_data;
1025
1026
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 if (ts->mux_rate > 1 || ts->pcr_period_ms >= 0) {
1027 int pcr_period_ms = ts->pcr_period_ms == -1 ? PCR_RETRANS_TIME : ts->pcr_period_ms;
1028 ts_st->pcr_period = av_rescale(pcr_period_ms, PCR_TIME_BASE, 1000);
1029 } else {
1030 /* By default, for VBR we select the highest multiple of frame duration which is less than 100 ms. */
1031 20 int64_t frame_period = 0;
1032
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6 times.
20 if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1033 14 int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0);
1034
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (!frame_size) {
1035 1 av_log(s, AV_LOG_WARNING, "frame size not set\n");
1036 1 frame_size = 512;
1037 }
1038 14 frame_period = av_rescale_rnd(frame_size, PCR_TIME_BASE, pcr_st->codecpar->sample_rate, AV_ROUND_UP);
1039
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 } else if (pcr_st->avg_frame_rate.num) {
1040 6 frame_period = av_rescale_rnd(pcr_st->avg_frame_rate.den, PCR_TIME_BASE, pcr_st->avg_frame_rate.num, AV_ROUND_UP);
1041 }
1042
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 if (frame_period > 0 && frame_period <= PCR_TIME_BASE / 10)
1043 20 ts_st->pcr_period = frame_period * (PCR_TIME_BASE / 10 / frame_period);
1044 else
1045 ts_st->pcr_period = 1;
1046 }
1047
1048 // output a PCR as soon as possible
1049 20 ts_st->last_pcr = ts->first_pcr - ts_st->pcr_period;
1050 20 }
1051
1052 20 static void select_pcr_streams(AVFormatContext *s)
1053 {
1054 20 MpegTSWrite *ts = s->priv_data;
1055
1056
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 for (int i = 0; i < ts->nb_services; i++) {
1057 20 MpegTSService *service = ts->services[i];
1058 20 AVStream *pcr_st = NULL;
1059 20 AVProgram *program = service->program;
1060
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 int nb_streams = program ? program->nb_stream_indexes : s->nb_streams;
1061
1062
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 20 times.
41 for (int j = 0; j < nb_streams; j++) {
1063
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 AVStream *st = s->streams[program ? program->stream_index[j] : j];
1064
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (!pcr_st ||
1065
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)
1066 {
1067 20 pcr_st = st;
1068 }
1069 }
1070
1071
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (pcr_st) {
1072 20 MpegTSWriteStream *ts_st = pcr_st->priv_data;
1073 20 service->pcr_pid = ts_st->pid;
1074 20 enable_pcr_generation_for_stream(s, pcr_st);
1075 20 av_log(s, AV_LOG_VERBOSE, "service %i using PCR in pid=%i, pcr_period=%"PRId64"ms\n",
1076 service->sid, service->pcr_pid, av_rescale(ts_st->pcr_period, 1000, PCR_TIME_BASE));
1077 }
1078 }
1079 20 }
1080
1081 20 static int mpegts_init(AVFormatContext *s)
1082 {
1083 20 MpegTSWrite *ts = s->priv_data;
1084 AVDictionaryEntry *provider;
1085 const char *provider_name;
1086 int i, j;
1087 int ret;
1088
1089
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (ts->m2ts_mode == -1) {
1090
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (av_match_ext(s->url, "m2ts")) {
1091 ts->m2ts_mode = 1;
1092 } else {
1093 20 ts->m2ts_mode = 0;
1094 }
1095 }
1096
1097 20 ts->m2ts_video_pid = M2TS_VIDEO_PID;
1098 20 ts->m2ts_audio_pid = M2TS_AUDIO_START_PID;
1099 20 ts->m2ts_pgssub_pid = M2TS_PGSSUB_START_PID;
1100 20 ts->m2ts_textsub_pid = M2TS_TEXTSUB_PID;
1101
1102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ts->m2ts_mode) {
1103 ts->pmt_start_pid = M2TS_PMT_PID;
1104 if (s->nb_programs > 1) {
1105 av_log(s, AV_LOG_ERROR, "Only one program is allowed in m2ts mode!\n");
1106 return AVERROR(EINVAL);
1107 }
1108 }
1109
1110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (s->max_delay < 0) /* Not set by the caller */
1111 s->max_delay = 0;
1112
1113 // round up to a whole number of TS packets
1114 20 ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
1115
1116
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (!s->nb_programs) {
1117 /* allocate a single DVB service */
1118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (!mpegts_add_service(s, ts->service_id, s->metadata, NULL))
1119 return AVERROR(ENOMEM);
1120 } else {
1121 for (i = 0; i < s->nb_programs; i++) {
1122 AVProgram *program = s->programs[i];
1123 if (!mpegts_add_service(s, program->id, program->metadata, program))
1124 return AVERROR(ENOMEM);
1125 }
1126 }
1127
1128 20 ts->pat.pid = PAT_PID;
1129 /* Initialize at 15 so that it wraps and is equal to 0 for the
1130 * first packet we write. */
1131 20 ts->pat.cc = 15;
1132 20 ts->pat.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
1133 20 ts->pat.write_packet = section_write_packet;
1134 20 ts->pat.opaque = s;
1135
1136 20 ts->sdt.pid = SDT_PID;
1137 20 ts->sdt.cc = 15;
1138 20 ts->sdt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
1139 20 ts->sdt.write_packet = section_write_packet;
1140 20 ts->sdt.opaque = s;
1141
1142 20 ts->nit.pid = NIT_PID;
1143 20 ts->nit.cc = 15;
1144 20 ts->nit.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
1145 20 ts->nit.write_packet = section_write_packet;
1146 20 ts->nit.opaque = s;
1147
1148 20 ts->pkt = ffformatcontext(s)->pkt;
1149
1150 /* assign pids to each stream */
1151
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 20 times.
41 for (i = 0; i < s->nb_streams; i++) {
1152 21 AVStream *st = s->streams[i];
1153 MpegTSWriteStream *ts_st;
1154
1155 21 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
1156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!ts_st) {
1157 return AVERROR(ENOMEM);
1158 }
1159 21 st->priv_data = ts_st;
1160
1161 21 avpriv_set_pts_info(st, 33, 1, 90000);
1162
1163 21 ts_st->payload = av_mallocz(ts->pes_payload_size);
1164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!ts_st->payload) {
1165 return AVERROR(ENOMEM);
1166 }
1167
1168 /* MPEG pid values < 16 are reserved. Applications which set st->id in
1169 * this range are assigned a calculated pid. */
1170
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (st->id < 16) {
1171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ts->m2ts_mode) {
1172 switch (st->codecpar->codec_type) {
1173 case AVMEDIA_TYPE_VIDEO:
1174 ts_st->pid = ts->m2ts_video_pid++;
1175 break;
1176 case AVMEDIA_TYPE_AUDIO:
1177 ts_st->pid = ts->m2ts_audio_pid++;
1178 break;
1179 case AVMEDIA_TYPE_SUBTITLE:
1180 switch (st->codecpar->codec_id) {
1181 case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
1182 ts_st->pid = ts->m2ts_pgssub_pid++;
1183 break;
1184 case AV_CODEC_ID_HDMV_TEXT_SUBTITLE:
1185 ts_st->pid = ts->m2ts_textsub_pid++;
1186 break;
1187 }
1188 break;
1189 }
1190 if (ts->m2ts_video_pid > M2TS_VIDEO_PID + 1 ||
1191 ts->m2ts_audio_pid > M2TS_AUDIO_START_PID + 32 ||
1192 ts->m2ts_pgssub_pid > M2TS_PGSSUB_START_PID + 32 ||
1193 ts->m2ts_textsub_pid > M2TS_TEXTSUB_PID + 1 ||
1194 ts_st->pid < 16) {
1195 av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index);
1196 return AVERROR(EINVAL);
1197 }
1198 } else {
1199 21 ts_st->pid = ts->start_pid + i;
1200 }
1201 } else {
1202 ts_st->pid = st->id;
1203 }
1204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ts_st->pid >= 0x1FFF) {
1205 av_log(s, AV_LOG_ERROR,
1206 "Invalid stream id %d, must be less than 8191\n", st->id);
1207 return AVERROR(EINVAL);
1208 }
1209
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
42 for (j = 0; j < ts->nb_services; j++) {
1210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ts->services[j]->pmt.pid > LAST_OTHER_PID) {
1211 av_log(s, AV_LOG_ERROR,
1212 "Invalid PMT PID %d, must be less than %d\n", ts->services[j]->pmt.pid, LAST_OTHER_PID + 1);
1213 return AVERROR(EINVAL);
1214 }
1215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ts_st->pid == ts->services[j]->pmt.pid) {
1216 av_log(s, AV_LOG_ERROR, "PID %d cannot be both elementary and PMT PID\n", ts_st->pid);
1217 return AVERROR(EINVAL);
1218 }
1219 }
1220
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
22 for (j = 0; j < i; j++) {
1221 1 MpegTSWriteStream *ts_st_prev = s->streams[j]->priv_data;
1222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ts_st_prev->pid == ts_st->pid) {
1223 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
1224 return AVERROR(EINVAL);
1225 }
1226 }
1227 21 ts_st->payload_pts = AV_NOPTS_VALUE;
1228 21 ts_st->payload_dts = AV_NOPTS_VALUE;
1229 21 ts_st->cc = 15;
1230 21 ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
1231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1232 st->codecpar->extradata_size > 0) {
1233 AVStream *ast;
1234 ts_st->amux = avformat_alloc_context();
1235 if (!ts_st->amux) {
1236 return AVERROR(ENOMEM);
1237 }
1238 ts_st->amux->oformat =
1239 av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
1240 NULL, NULL);
1241 if (!ts_st->amux->oformat) {
1242 return AVERROR(EINVAL);
1243 }
1244 if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
1245 return AVERROR(ENOMEM);
1246 }
1247 ret = avcodec_parameters_copy(ast->codecpar, st->codecpar);
1248 if (ret != 0)
1249 return ret;
1250 ast->time_base = st->time_base;
1251 ret = avformat_write_header(ts_st->amux, NULL);
1252 if (ret < 0)
1253 return ret;
1254 }
1255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
1256 ts_st->opus_pending_trim_start = st->codecpar->initial_padding * 48000 / st->codecpar->sample_rate;
1257 }
1258 }
1259
1260
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (ts->copyts < 1)
1261 20 ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
1262
1263 20 select_pcr_streams(s);
1264
1265 20 ts->last_pat_ts = AV_NOPTS_VALUE;
1266 20 ts->last_sdt_ts = AV_NOPTS_VALUE;
1267 20 ts->last_nit_ts = AV_NOPTS_VALUE;
1268 20 ts->pat_period = av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE);
1269 20 ts->sdt_period = av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE);
1270 20 ts->nit_period = av_rescale(ts->nit_period_us, PCR_TIME_BASE, AV_TIME_BASE);
1271
1272 /* assign provider name */
1273 20 provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
1274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
1275
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (encode_str8(ts->provider_name, provider_name) < 0) {
1276 av_log(s, AV_LOG_ERROR, "Too long provider name\n");
1277 return AVERROR(EINVAL);
1278 }
1279
1280
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (ts->mux_rate == 1)
1281 20 av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
1282 else
1283 av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
1284 20 av_log(s, AV_LOG_VERBOSE,
1285 "sdt every %"PRId64" ms, pat/pmt every %"PRId64" ms",
1286 av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE),
1287 av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
1288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ts->flags & MPEGTS_FLAG_NIT)
1289 av_log(s, AV_LOG_VERBOSE, ", nit every %"PRId64" ms", av_rescale(ts->nit_period, 1000, PCR_TIME_BASE));
1290 20 av_log(s, AV_LOG_VERBOSE, "\n");
1291
1292 20 return 0;
1293 }
1294
1295 /* send SDT, NIT, PAT and PMT tables regularly */
1296 64570 static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int force_nit, int64_t pcr)
1297 {
1298 64570 MpegTSWrite *ts = s->priv_data;
1299 int i;
1300
1301
4/6
✓ Branch 0 taken 64570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64550 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 64550 times.
✗ Branch 5 not taken.
64570 if ((pcr != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
1302
4/4
✓ Branch 0 taken 64456 times.
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 64410 times.
64550 (pcr != AV_NOPTS_VALUE && pcr - ts->last_sdt_ts >= ts->sdt_period) ||
1303 force_sdt
1304 ) {
1305
1/2
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
160 if (pcr != AV_NOPTS_VALUE)
1306 160 ts->last_sdt_ts = FFMAX(pcr, ts->last_sdt_ts);
1307 160 mpegts_write_sdt(s);
1308 }
1309
4/6
✓ Branch 0 taken 64570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64550 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 64550 times.
✗ Branch 5 not taken.
64570 if ((pcr != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
1310
4/4
✓ Branch 0 taken 64074 times.
✓ Branch 1 taken 476 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 64028 times.
64550 (pcr != AV_NOPTS_VALUE && pcr - ts->last_pat_ts >= ts->pat_period) ||
1311 force_pat) {
1312
1/2
✓ Branch 0 taken 542 times.
✗ Branch 1 not taken.
542 if (pcr != AV_NOPTS_VALUE)
1313 542 ts->last_pat_ts = FFMAX(pcr, ts->last_pat_ts);
1314 542 mpegts_write_pat(s);
1315
2/2
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 542 times.
1084 for (i = 0; i < ts->nb_services; i++)
1316 542 mpegts_write_pmt(s, ts->services[i]);
1317 }
1318
4/6
✓ Branch 0 taken 64570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64550 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 64550 times.
✗ Branch 5 not taken.
64570 if ((pcr != AV_NOPTS_VALUE && ts->last_nit_ts == AV_NOPTS_VALUE) ||
1319
4/4
✓ Branch 0 taken 64199 times.
✓ Branch 1 taken 351 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 64153 times.
64550 (pcr != AV_NOPTS_VALUE && pcr - ts->last_nit_ts >= ts->nit_period) ||
1320 force_nit
1321 ) {
1322
1/2
✓ Branch 0 taken 417 times.
✗ Branch 1 not taken.
417 if (pcr != AV_NOPTS_VALUE)
1323 417 ts->last_nit_ts = FFMAX(pcr, ts->last_nit_ts);
1324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 417 times.
417 if (ts->flags & MPEGTS_FLAG_NIT)
1325 mpegts_write_nit(s);
1326 }
1327 64570 }
1328
1329 4117 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
1330 {
1331 4117 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
1332
1333 4117 *buf++ = pcr_high >> 25;
1334 4117 *buf++ = pcr_high >> 17;
1335 4117 *buf++ = pcr_high >> 9;
1336 4117 *buf++ = pcr_high >> 1;
1337 4117 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
1338 4117 *buf++ = pcr_low;
1339
1340 4117 return 6;
1341 }
1342
1343 /* Write a single null transport stream packet */
1344 static void mpegts_insert_null_packet(AVFormatContext *s)
1345 {
1346 uint8_t *q;
1347 uint8_t buf[TS_PACKET_SIZE];
1348
1349 q = buf;
1350 *q++ = 0x47;
1351 *q++ = 0x00 | 0x1f;
1352 *q++ = 0xff;
1353 *q++ = 0x10;
1354 memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
1355 write_packet(s, buf);
1356 }
1357
1358 /* Write a single transport stream packet with a PCR and no payload */
1359 static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
1360 {
1361 MpegTSWrite *ts = s->priv_data;
1362 MpegTSWriteStream *ts_st = st->priv_data;
1363 uint8_t *q;
1364 uint8_t buf[TS_PACKET_SIZE];
1365
1366 q = buf;
1367 *q++ = 0x47;
1368 *q++ = ts_st->pid >> 8;
1369 *q++ = ts_st->pid;
1370 *q++ = 0x20 | ts_st->cc; /* Adaptation only */
1371 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
1372 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
1373 *q++ = 0x10; /* Adaptation flags: PCR present */
1374 if (ts_st->discontinuity) {
1375 q[-1] |= 0x80;
1376 ts_st->discontinuity = 0;
1377 }
1378
1379 /* PCR coded into 6 bytes */
1380 q += write_pcr_bits(q, get_pcr(ts));
1381
1382 /* stuffing bytes */
1383 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
1384 write_packet(s, buf);
1385 }
1386
1387 4311 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
1388 {
1389 int val;
1390
1391 4311 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
1392 4311 *q++ = val;
1393 4311 val = (((pts >> 15) & 0x7fff) << 1) | 1;
1394 4311 *q++ = val >> 8;
1395 4311 *q++ = val;
1396 4311 val = (((pts) & 0x7fff) << 1) | 1;
1397 4311 *q++ = val >> 8;
1398 4311 *q++ = val;
1399 4311 }
1400
1401 /* Set an adaptation field flag in an MPEG-TS packet*/
1402 8165 static void set_af_flag(uint8_t *pkt, int flag)
1403 {
1404 // expect at least one flag to set
1405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8165 times.
8165 av_assert0(flag);
1406
1407
2/2
✓ Branch 0 taken 4120 times.
✓ Branch 1 taken 4045 times.
8165 if ((pkt[3] & 0x20) == 0) {
1408 // no AF yet, set adaptation field flag
1409 4120 pkt[3] |= 0x20;
1410 // 1 byte length, no flags
1411 4120 pkt[4] = 1;
1412 4120 pkt[5] = 0;
1413 }
1414 8165 pkt[5] |= flag;
1415 8165 }
1416
1417 /* Extend the adaptation field by size bytes */
1418 4117 static void extend_af(uint8_t *pkt, int size)
1419 {
1420 // expect already existing adaptation field
1421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4117 times.
4117 av_assert0(pkt[3] & 0x20);
1422 4117 pkt[4] += size;
1423 4117 }
1424
1425 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
1426 12282 static uint8_t *get_ts_payload_start(uint8_t *pkt)
1427 {
1428
1/2
✓ Branch 0 taken 12282 times.
✗ Branch 1 not taken.
12282 if (pkt[3] & 0x20)
1429 12282 return pkt + 5 + pkt[4];
1430 else
1431 return pkt + 4;
1432 }
1433
1434 4190 static int get_pes_stream_id(AVFormatContext *s, AVStream *st, int stream_id, int *async)
1435 {
1436 4190 MpegTSWrite *ts = s->priv_data;
1437 4190 *async = 0;
1438
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 4039 times.
4190 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
151 if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)
1440 return STREAM_ID_EXTENDED_STREAM_ID;
1441 else
1442 151 return STREAM_ID_VIDEO_STREAM_0;
1443
1/2
✓ Branch 0 taken 4039 times.
✗ Branch 1 not taken.
4039 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1444
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 3780 times.
4039 (st->codecpar->codec_id == AV_CODEC_ID_MP2 ||
1445
1/2
✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
259 st->codecpar->codec_id == AV_CODEC_ID_MP3 ||
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 st->codecpar->codec_id == AV_CODEC_ID_AAC)) {
1447 3780 return STREAM_ID_AUDIO_STREAM_0;
1448
1/2
✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
259 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
1450 ts->m2ts_mode) {
1451 return STREAM_ID_EXTENDED_STREAM_ID;
1452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1453 st->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {
1454 return STREAM_ID_PRIVATE_STREAM_1;
1455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
1456 if (stream_id == STREAM_ID_PRIVATE_STREAM_1) /* asynchronous KLV */
1457 *async = 1;
1458 return stream_id != -1 ? stream_id : STREAM_ID_METADATA_STREAM;
1459 } else {
1460 259 return STREAM_ID_PRIVATE_STREAM_1;
1461 }
1462 }
1463
1464 /* Add a PES header to the front of the payload, and segment into an integer
1465 * number of TS packets. The final TS packet is padded using an oversized
1466 * adaptation header to exactly fill the last TS packet.
1467 * NOTE: 'payload' contains a complete PES payload. */
1468 4190 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
1469 const uint8_t *payload, int payload_size,
1470 int64_t pts, int64_t dts, int key, int stream_id)
1471 {
1472 4190 MpegTSWriteStream *ts_st = st->priv_data;
1473 4190 MpegTSWrite *ts = s->priv_data;
1474 uint8_t buf[TS_PACKET_SIZE];
1475 uint8_t *q;
1476 int val, is_start, len, header_len, write_pcr, flags;
1477 int afc_len, stuffing_len;
1478 4190 int is_dvb_subtitle = (st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE);
1479 4190 int is_dvb_teletext = (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT);
1480 4190 int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
1481
5/6
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 4039 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 142 times.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
4190 int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
1482 4190 int force_sdt = 0;
1483 4190 int force_nit = 0;
1484
1485
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4190 av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
1486
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4190 if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1487 force_pat = 1;
1488 }
1489
1490
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 4129 times.
4190 if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1491 61 force_pat = 1;
1492 61 force_sdt = 1;
1493 61 force_nit = 1;
1494 61 ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
1495 }
1496
1497 4190 is_start = 1;
1498
2/2
✓ Branch 0 taken 64570 times.
✓ Branch 1 taken 4190 times.
68760 while (payload_size > 0) {
1499 64570 int64_t pcr = AV_NOPTS_VALUE;
1500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64570 times.
64570 if (ts->mux_rate > 1)
1501 pcr = get_pcr(ts);
1502
1/2
✓ Branch 0 taken 64570 times.
✗ Branch 1 not taken.
64570 else if (dts != AV_NOPTS_VALUE)
1503 64570 pcr = (dts - delay) * 300;
1504
1505 64570 retransmit_si_info(s, force_pat, force_sdt, force_nit, pcr);
1506 64570 force_pat = 0;
1507 64570 force_sdt = 0;
1508 64570 force_nit = 0;
1509
1510 64570 write_pcr = 0;
1511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64570 times.
64570 if (ts->mux_rate > 1) {
1512 /* Send PCR packets for all PCR streams if needed */
1513 pcr = get_pcr(ts);
1514 if (pcr >= ts->next_pcr) {
1515 int64_t next_pcr = INT64_MAX;
1516 for (int i = 0; i < s->nb_streams; i++) {
1517 /* Make the current stream the last, because for that we
1518 * can insert the pcr into the payload later */
1519 int st2_index = i < st->index ? i : (i + 1 == s->nb_streams ? st->index : i + 1);
1520 AVStream *st2 = s->streams[st2_index];
1521 MpegTSWriteStream *ts_st2 = st2->priv_data;
1522 if (ts_st2->pcr_period) {
1523 if (pcr - ts_st2->last_pcr >= ts_st2->pcr_period) {
1524 ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period);
1525 if (st2 != st) {
1526 mpegts_insert_pcr_only(s, st2);
1527 pcr = get_pcr(ts);
1528 } else {
1529 write_pcr = 1;
1530 }
1531 }
1532 next_pcr = FFMIN(next_pcr, ts_st2->last_pcr + ts_st2->pcr_period);
1533 }
1534 }
1535 ts->next_pcr = next_pcr;
1536 }
1537 if (dts != AV_NOPTS_VALUE && (dts - pcr / 300) > delay) {
1538 /* pcr insert gets priority over null packet insert */
1539 if (write_pcr)
1540 mpegts_insert_pcr_only(s, st);
1541 else
1542 mpegts_insert_null_packet(s);
1543 /* recalculate write_pcr and possibly retransmit si_info */
1544 continue;
1545 }
1546
3/4
✓ Branch 0 taken 64525 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 64525 times.
✗ Branch 3 not taken.
64570 } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) {
1547
4/4
✓ Branch 0 taken 3210 times.
✓ Branch 1 taken 61315 times.
✓ Branch 2 taken 2648 times.
✓ Branch 3 taken 562 times.
64525 if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) {
1548 2648 ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period);
1549 2648 write_pcr = 1;
1550 }
1551 }
1552
1553 /* prepare packet header */
1554 64570 q = buf;
1555 64570 *q++ = 0x47;
1556 64570 val = ts_st->pid >> 8;
1557
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 64570 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
64570 if (ts->m2ts_mode && st->codecpar->codec_id == AV_CODEC_ID_AC3)
1558 val |= 0x20;
1559
2/2
✓ Branch 0 taken 4190 times.
✓ Branch 1 taken 60380 times.
64570 if (is_start)
1560 4190 val |= 0x40;
1561 64570 *q++ = val;
1562 64570 *q++ = ts_st->pid;
1563 64570 ts_st->cc = ts_st->cc + 1 & 0xf;
1564 64570 *q++ = 0x10 | ts_st->cc; // payload indicator + CC
1565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64570 times.
64570 if (ts_st->discontinuity) {
1566 set_af_flag(buf, 0x80);
1567 q = get_ts_payload_start(buf);
1568 ts_st->discontinuity = 0;
1569 }
1570
3/4
✓ Branch 0 taken 64570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61812 times.
✓ Branch 3 taken 2758 times.
64570 if (!(ts->flags & MPEGTS_FLAG_OMIT_RAI) &&
1571
4/6
✓ Branch 0 taken 4048 times.
✓ Branch 1 taken 57764 times.
✓ Branch 2 taken 4048 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4048 times.
✗ Branch 5 not taken.
61812 key && is_start && pts != AV_NOPTS_VALUE &&
1572 !is_dvb_teletext /* adaptation+payload forbidden for teletext (ETSI EN 300 472 V1.3.1 4.1) */) {
1573 // set Random Access for key frames
1574
2/2
✓ Branch 0 taken 4045 times.
✓ Branch 1 taken 3 times.
4048 if (ts_st->pcr_period)
1575 4045 write_pcr = 1;
1576 4048 set_af_flag(buf, 0x40);
1577 4048 q = get_ts_payload_start(buf);
1578 }
1579
2/2
✓ Branch 0 taken 4117 times.
✓ Branch 1 taken 60453 times.
64570 if (write_pcr) {
1580 4117 set_af_flag(buf, 0x10);
1581 4117 q = get_ts_payload_start(buf);
1582 // add 11, pcr references the last byte of program clock reference base
1583
2/4
✓ Branch 0 taken 4117 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4117 times.
4117 if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
1584 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
1585 4117 extend_af(buf, write_pcr_bits(q, pcr));
1586 4117 q = get_ts_payload_start(buf);
1587 }
1588
2/2
✓ Branch 0 taken 4190 times.
✓ Branch 1 taken 60380 times.
64570 if (is_start) {
1589 4190 int pes_extension = 0;
1590 4190 int pes_header_stuffing_bytes = 0;
1591 int async;
1592 /* write PES header */
1593 4190 *q++ = 0x00;
1594 4190 *q++ = 0x00;
1595 4190 *q++ = 0x01;
1596 4190 *q++ = stream_id = get_pes_stream_id(s, st, stream_id, &async);
1597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
4190 if (async)
1598 pts = dts = AV_NOPTS_VALUE;
1599
1600 4190 header_len = 0;
1601
1602
2/4
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4190 times.
✗ Branch 3 not taken.
4190 if (stream_id != STREAM_ID_PROGRAM_STREAM_MAP &&
1603
1/2
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
4190 stream_id != STREAM_ID_PADDING_STREAM &&
1604
1/2
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
4190 stream_id != STREAM_ID_PRIVATE_STREAM_2 &&
1605
1/2
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
4190 stream_id != STREAM_ID_ECM_STREAM &&
1606
1/2
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
4190 stream_id != STREAM_ID_EMM_STREAM &&
1607
1/2
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
4190 stream_id != STREAM_ID_PROGRAM_STREAM_DIRECTORY &&
1608
1/2
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
4190 stream_id != STREAM_ID_DSMCC_STREAM &&
1609 stream_id != STREAM_ID_TYPE_E_STREAM) {
1610
1611 4190 flags = 0;
1612
1/2
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
4190 if (pts != AV_NOPTS_VALUE) {
1613 4190 header_len += 5;
1614 4190 flags |= 0x80;
1615 }
1616
4/6
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4190 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 121 times.
✓ Branch 5 taken 4069 times.
4190 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1617 121 header_len += 5;
1618 121 flags |= 0x40;
1619 }
1620
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 4039 times.
4190 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
151 st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
1622 /* set PES_extension_flag */
1623 pes_extension = 1;
1624 flags |= 0x01;
1625
1626 /* One byte for PES2 extension flag +
1627 * one byte for extension length +
1628 * one byte for extension id */
1629 header_len += 3;
1630 }
1631 /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
1632 * otherwise it will not play sound on blu-ray
1633 */
1634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
4190 if (ts->m2ts_mode &&
1635 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1636 st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1637 /* set PES_extension_flag */
1638 pes_extension = 1;
1639 flags |= 0x01;
1640 header_len += 3;
1641 }
1642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
4190 if (is_dvb_teletext) {
1643 pes_header_stuffing_bytes = 0x24 - header_len;
1644 header_len = 0x24;
1645 }
1646 4190 len = payload_size + header_len + 3;
1647 /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
1648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
4190 if (is_dvb_subtitle) {
1649 len += 3;
1650 payload_size++;
1651 }
1652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
4190 if (len > 0xffff)
1653 len = 0;
1654
3/4
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 151 times.
✓ Branch 3 taken 4039 times.
4190 if (ts->omit_video_pes_length && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1655 151 len = 0;
1656 }
1657 4190 *q++ = len >> 8;
1658 4190 *q++ = len;
1659 4190 val = 0x80;
1660 /* data alignment indicator is required for subtitle and data streams */
1661
2/4
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4190 times.
4190 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
1662 val |= 0x04;
1663 4190 *q++ = val;
1664 4190 *q++ = flags;
1665 4190 *q++ = header_len;
1666
1/2
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
4190 if (pts != AV_NOPTS_VALUE) {
1667 4190 write_pts(q, flags >> 6, pts);
1668 4190 q += 5;
1669 }
1670
4/6
✓ Branch 0 taken 4190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4190 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 121 times.
✓ Branch 5 taken 4069 times.
4190 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1671 121 write_pts(q, 1, dts);
1672 121 q += 5;
1673 }
1674
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4190 if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
1675 flags = 0x01; /* set PES_extension_flag_2 */
1676 *q++ = flags;
1677 *q++ = 0x80 | 0x01; /* marker bit + extension length */
1678 /* Set the stream ID extension flag bit to 0 and
1679 * write the extended stream ID. */
1680 *q++ = 0x00 | 0x60;
1681 }
1682 /* For Blu-ray AC3 Audio Setting extended flags */
1683
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4190 if (ts->m2ts_mode &&
1684 pes_extension &&
1685 st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1686 flags = 0x01; /* set PES_extension_flag_2 */
1687 *q++ = flags;
1688 *q++ = 0x80 | 0x01; /* marker bit + extension length */
1689 *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1690 }
1691
1692
1693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
4190 if (is_dvb_subtitle) {
1694 /* First two fields of DVB subtitles PES data:
1695 * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
1696 * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
1697 *q++ = 0x20;
1698 *q++ = 0x00;
1699 }
1700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4190 times.
4190 if (is_dvb_teletext) {
1701 memset(q, 0xff, pes_header_stuffing_bytes);
1702 q += pes_header_stuffing_bytes;
1703 }
1704 } else {
1705 len = payload_size;
1706 *q++ = len >> 8;
1707 *q++ = len;
1708 }
1709 4190 is_start = 0;
1710 }
1711 /* header size */
1712 64570 header_len = q - buf;
1713 /* data len */
1714 64570 len = TS_PACKET_SIZE - header_len;
1715
2/2
✓ Branch 0 taken 4189 times.
✓ Branch 1 taken 60381 times.
64570 if (len > payload_size)
1716 4189 len = payload_size;
1717 64570 stuffing_len = TS_PACKET_SIZE - header_len - len;
1718
2/2
✓ Branch 0 taken 4189 times.
✓ Branch 1 taken 60381 times.
64570 if (stuffing_len > 0) {
1719 /* add stuffing with AFC */
1720
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4188 times.
4189 if (buf[3] & 0x20) {
1721 /* stuffing already present: increase its size */
1722 1 afc_len = buf[4] + 1;
1723 1 memmove(buf + 4 + afc_len + stuffing_len,
1724 1 buf + 4 + afc_len,
1725 1 header_len - (4 + afc_len));
1726 1 buf[4] += stuffing_len;
1727 1 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1728 } else {
1729 /* add stuffing */
1730 4188 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1731 4188 buf[3] |= 0x20;
1732 4188 buf[4] = stuffing_len - 1;
1733
2/2
✓ Branch 0 taken 4186 times.
✓ Branch 1 taken 2 times.
4188 if (stuffing_len >= 2) {
1734 4186 buf[5] = 0x00;
1735 4186 memset(buf + 6, 0xff, stuffing_len - 2);
1736 }
1737 }
1738 }
1739
1740
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 64570 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
64570 if (is_dvb_subtitle && payload_size == len) {
1741 memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
1742 buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
1743 } else {
1744 64570 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1745 }
1746
1747 64570 payload += len;
1748 64570 payload_size -= len;
1749 64570 write_packet(s, buf);
1750 }
1751 4190 ts_st->prev_payload_key = key;
1752 4190 }
1753
1754 205 int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
1755 {
1756
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) {
1757 if (!st->nb_frames) {
1758 av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1759 "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1760 "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1761 return AVERROR_INVALIDDATA;
1762 }
1763 av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing, size %d", pkt->size);
1764 if (pkt->size)
1765 av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
1766 av_log(s, AV_LOG_WARNING, "\n");
1767 }
1768 205 return 0;
1769 }
1770
1771 static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
1772 {
1773 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1774 if (!st->nb_frames) {
1775 av_log(s, AV_LOG_ERROR, "HEVC bitstream malformed, no startcode found\n");
1776 return AVERROR_PATCHWELCOME;
1777 }
1778 av_log(s, AV_LOG_WARNING, "HEVC bitstream error, startcode missing, size %d", pkt->size);
1779 if (pkt->size)
1780 av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
1781 av_log(s, AV_LOG_WARNING, "\n");
1782 }
1783 return 0;
1784 }
1785
1786 /* Based on GStreamer's gst-plugins-base/ext/ogg/gstoggstream.c
1787 * Released under the LGPL v2.1+, written by
1788 * Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
1789 */
1790 static int opus_get_packet_samples(AVFormatContext *s, AVPacket *pkt)
1791 {
1792 static const int durations[32] = {
1793 480, 960, 1920, 2880, /* Silk NB */
1794 480, 960, 1920, 2880, /* Silk MB */
1795 480, 960, 1920, 2880, /* Silk WB */
1796 480, 960, /* Hybrid SWB */
1797 480, 960, /* Hybrid FB */
1798 120, 240, 480, 960, /* CELT NB */
1799 120, 240, 480, 960, /* CELT NB */
1800 120, 240, 480, 960, /* CELT NB */
1801 120, 240, 480, 960, /* CELT NB */
1802 };
1803 int toc, frame_duration, nframes, duration;
1804
1805 if (pkt->size < 1)
1806 return 0;
1807
1808 toc = pkt->data[0];
1809
1810 frame_duration = durations[toc >> 3];
1811 switch (toc & 3) {
1812 case 0:
1813 nframes = 1;
1814 break;
1815 case 1:
1816 nframes = 2;
1817 break;
1818 case 2:
1819 nframes = 2;
1820 break;
1821 case 3:
1822 if (pkt->size < 2)
1823 return 0;
1824 nframes = pkt->data[1] & 63;
1825 break;
1826 }
1827
1828 duration = nframes * frame_duration;
1829 if (duration > 5760) {
1830 av_log(s, AV_LOG_WARNING,
1831 "Opus packet duration > 120 ms, invalid");
1832 return 0;
1833 }
1834
1835 return duration;
1836 }
1837
1838 7958 static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1839 {
1840 7958 AVStream *st = s->streams[pkt->stream_index];
1841 7958 int size = pkt->size;
1842 7958 const uint8_t *buf = pkt->data;
1843 7958 uint8_t *data = NULL;
1844 7958 MpegTSWrite *ts = s->priv_data;
1845 7958 MpegTSWriteStream *ts_st = st->priv_data;
1846 7958 const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1847 7958 const int64_t max_audio_delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) / 2;
1848 7958 int64_t dts = pkt->dts, pts = pkt->pts;
1849 7958 int opus_samples = 0;
1850 size_t side_data_size;
1851 7958 uint8_t *side_data = NULL;
1852 7958 int stream_id = -1;
1853
1854 7958 side_data = av_packet_get_side_data(pkt,
1855 AV_PKT_DATA_MPEGTS_STREAM_ID,
1856 &side_data_size);
1857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7958 times.
7958 if (side_data)
1858 stream_id = side_data[0];
1859
1860
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 7938 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
7958 if (!ts->first_dts_checked && dts != AV_NOPTS_VALUE) {
1861 20 ts->first_pcr += dts * 300;
1862 20 ts->first_dts_checked = 1;
1863 }
1864
1865
1/2
✓ Branch 0 taken 7958 times.
✗ Branch 1 not taken.
7958 if (ts->copyts < 1) {
1866
1/2
✓ Branch 0 taken 7958 times.
✗ Branch 1 not taken.
7958 if (pts != AV_NOPTS_VALUE)
1867 7958 pts += delay;
1868
1/2
✓ Branch 0 taken 7958 times.
✗ Branch 1 not taken.
7958 if (dts != AV_NOPTS_VALUE)
1869 7958 dts += delay;
1870 }
1871
1872
4/6
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7937 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 21 times.
7958 if (!ts_st->first_timestamp_checked && (pts == AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)) {
1873 av_log(s, AV_LOG_ERROR, "first pts and dts value must be set\n");
1874 return AVERROR_INVALIDDATA;
1875 }
1876 7958 ts_st->first_timestamp_checked = 1;
1877
1878
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 7832 times.
7958 if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
1879 126 const uint8_t *p = buf, *buf_end = p + size;
1880 126 uint32_t state = -1;
1881
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;
1882 126 int ret = ff_check_h264_startcode(s, st, pkt);
1883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (ret < 0)
1884 return ret;
1885
1886
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)
1887 6 extradd = 0;
1888
1889 do {
1890 127 p = avpriv_find_start_code(p, buf_end, &state);
1891 127 av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", state & 0x1f);
1892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if ((state & 0x1f) == 7)
1893 extradd = 0;
1894
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 126 times.
127 } while (p < buf_end && (state & 0x1f) != 9 &&
1895
3/6
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
128 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1896
1897
1/2
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
126 if ((state & 0x1f) != 5)
1898 126 extradd = 0;
1899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if ((state & 0x1f) != 9) { // AUD NAL
1900 data = av_malloc(pkt->size + 6 + extradd);
1901 if (!data)
1902 return AVERROR(ENOMEM);
1903 memcpy(data + 6, st->codecpar->extradata, extradd);
1904 memcpy(data + 6 + extradd, pkt->data, pkt->size);
1905 AV_WB32(data, 0x00000001);
1906 data[4] = 0x09;
1907 data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1908 buf = data;
1909 size = pkt->size + 6 + extradd;
1910 }
1911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7832 times.
7832 } else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
1912 if (pkt->size < 2) {
1913 av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1914 return AVERROR_INVALIDDATA;
1915 }
1916 if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1917 int ret;
1918 AVPacket *pkt2 = ts->pkt;
1919
1920 if (!ts_st->amux) {
1921 av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1922 "and extradata missing\n");
1923 } else {
1924 av_packet_unref(pkt2);
1925 pkt2->data = pkt->data;
1926 pkt2->size = pkt->size;
1927 av_assert0(pkt->dts != AV_NOPTS_VALUE);
1928 pkt2->dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
1929
1930 ret = avio_open_dyn_buf(&ts_st->amux->pb);
1931 if (ret < 0)
1932 return ret;
1933
1934 ret = av_write_frame(ts_st->amux, pkt2);
1935 if (ret < 0) {
1936 ffio_free_dyn_buf(&ts_st->amux->pb);
1937 return ret;
1938 }
1939 size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1940 ts_st->amux->pb = NULL;
1941 buf = data;
1942 }
1943 }
1944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7832 times.
7832 } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
1945 const uint8_t *p = buf, *buf_end = p + size;
1946 uint32_t state = -1;
1947 int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
1948 int ret = check_hevc_startcode(s, st, pkt);
1949 if (ret < 0)
1950 return ret;
1951
1952 if (extradd && AV_RB24(st->codecpar->extradata) > 1)
1953 extradd = 0;
1954
1955 do {
1956 p = avpriv_find_start_code(p, buf_end, &state);
1957 av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", (state & 0x7e)>>1);
1958 if ((state & 0x7e) == 2*32)
1959 extradd = 0;
1960 } while (p < buf_end && (state & 0x7e) != 2*35 &&
1961 (state & 0x7e) >= 2*32);
1962
1963 if ((state & 0x7e) < 2*16 || (state & 0x7e) >= 2*24)
1964 extradd = 0;
1965 if ((state & 0x7e) != 2*35) { // AUD NAL
1966 data = av_malloc(pkt->size + 7 + extradd);
1967 if (!data)
1968 return AVERROR(ENOMEM);
1969 memcpy(data + 7, st->codecpar->extradata, extradd);
1970 memcpy(data + 7 + extradd, pkt->data, pkt->size);
1971 AV_WB32(data, 0x00000001);
1972 data[4] = 2*35;
1973 data[5] = 1;
1974 data[6] = 0x50; // any slice type (0x4) + rbsp stop one bit
1975 buf = data;
1976 size = pkt->size + 7 + extradd;
1977 }
1978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7832 times.
7832 } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
1979 if (pkt->size < 2) {
1980 av_log(s, AV_LOG_ERROR, "Opus packet too short\n");
1981 return AVERROR_INVALIDDATA;
1982 }
1983
1984 /* Add Opus control header */
1985 if ((AV_RB16(pkt->data) >> 5) != 0x3ff) {
1986 uint8_t *side_data;
1987 size_t side_data_size;
1988 int i, n;
1989 int ctrl_header_size;
1990 int trim_start = 0, trim_end = 0;
1991
1992 opus_samples = opus_get_packet_samples(s, pkt);
1993
1994 side_data = av_packet_get_side_data(pkt,
1995 AV_PKT_DATA_SKIP_SAMPLES,
1996 &side_data_size);
1997
1998 if (side_data && side_data_size >= 10) {
1999 trim_end = AV_RL32(side_data + 4) * 48000 / st->codecpar->sample_rate;
2000 }
2001
2002 ctrl_header_size = pkt->size + 2 + pkt->size / 255 + 1;
2003 if (ts_st->opus_pending_trim_start)
2004 ctrl_header_size += 2;
2005 if (trim_end)
2006 ctrl_header_size += 2;
2007
2008 data = av_malloc(ctrl_header_size);
2009 if (!data)
2010 return AVERROR(ENOMEM);
2011
2012 data[0] = 0x7f;
2013 data[1] = 0xe0;
2014 if (ts_st->opus_pending_trim_start)
2015 data[1] |= 0x10;
2016 if (trim_end)
2017 data[1] |= 0x08;
2018
2019 n = pkt->size;
2020 i = 2;
2021 do {
2022 data[i] = FFMIN(n, 255);
2023 n -= 255;
2024 i++;
2025 } while (n >= 0);
2026
2027 av_assert0(2 + pkt->size / 255 + 1 == i);
2028
2029 if (ts_st->opus_pending_trim_start) {
2030 trim_start = FFMIN(ts_st->opus_pending_trim_start, opus_samples);
2031 AV_WB16(data + i, trim_start);
2032 i += 2;
2033 ts_st-></