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