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