FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mpegts.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 1099 1922 57.2%
Functions: 49 70 70.0%
Branches: 599 1291 46.4%

Line Branch Exec Source
1 /*
2 * MPEG-2 transport stream (aka DVB) demuxer
3 * Copyright (c) 2002-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 "config_components.h"
23
24 #include "libavutil/buffer.h"
25 #include "libavutil/common.h"
26 #include "libavutil/crc.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/log.h"
30 #include "libavutil/dict.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/dovi_meta.h"
35 #include "libavcodec/avcodec.h"
36 #include "libavcodec/bytestream.h"
37 #include "libavcodec/get_bits.h"
38 #include "libavcodec/opus.h"
39 #include "avformat.h"
40 #include "mpegts.h"
41 #include "internal.h"
42 #include "avio_internal.h"
43 #include "demux.h"
44 #include "mpeg.h"
45 #include "isom.h"
46 #if CONFIG_ICONV
47 #include <iconv.h>
48 #endif
49
50 /* maximum size in which we look for synchronization if
51 * synchronization is lost */
52 #define MAX_RESYNC_SIZE 65536
53
54 #define MAX_MP4_DESCR_COUNT 16
55
56 #define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \
57 do { \
58 if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \
59 (modulus) = (dividend) % (divisor); \
60 (prev_dividend) = (dividend); \
61 } while (0)
62
63 #define PROBE_PACKET_MAX_BUF 8192
64 #define PROBE_PACKET_MARGIN 5
65
66 enum MpegTSFilterType {
67 MPEGTS_PES,
68 MPEGTS_SECTION,
69 MPEGTS_PCR,
70 };
71
72 typedef struct MpegTSFilter MpegTSFilter;
73
74 typedef int PESCallback (MpegTSFilter *f, const uint8_t *buf, int len,
75 int is_start, int64_t pos);
76
77 typedef struct MpegTSPESFilter {
78 PESCallback *pes_cb;
79 void *opaque;
80 } MpegTSPESFilter;
81
82 typedef void SectionCallback (MpegTSFilter *f, const uint8_t *buf, int len);
83
84 typedef void SetServiceCallback (void *opaque, int ret);
85
86 typedef struct MpegTSSectionFilter {
87 int section_index;
88 int section_h_size;
89 int last_ver;
90 unsigned crc;
91 unsigned last_crc;
92 uint8_t *section_buf;
93 unsigned int check_crc : 1;
94 unsigned int end_of_section_reached : 1;
95 SectionCallback *section_cb;
96 void *opaque;
97 } MpegTSSectionFilter;
98
99 struct MpegTSFilter {
100 int pid;
101 int es_id;
102 int last_cc; /* last cc code (-1 if first packet) */
103 int64_t last_pcr;
104 int discard;
105 enum MpegTSFilterType type;
106 union {
107 MpegTSPESFilter pes_filter;
108 MpegTSSectionFilter section_filter;
109 } u;
110 };
111
112 struct Stream {
113 int idx;
114 int stream_identifier;
115 };
116
117 #define MAX_STREAMS_PER_PROGRAM 128
118 #define MAX_PIDS_PER_PROGRAM (MAX_STREAMS_PER_PROGRAM + 2)
119 struct Program {
120 unsigned int id; // program id/service id
121 unsigned int nb_pids;
122 unsigned int pids[MAX_PIDS_PER_PROGRAM];
123 unsigned int nb_streams;
124 struct Stream streams[MAX_STREAMS_PER_PROGRAM];
125
126 /** have we found pmt for this program */
127 int pmt_found;
128 };
129
130 struct MpegTSContext {
131 const AVClass *class;
132 /* user data */
133 AVFormatContext *stream;
134 /** raw packet size, including FEC if present */
135 int raw_packet_size;
136
137 int64_t pos47_full;
138
139 /** if true, all pids are analyzed to find streams */
140 int auto_guess;
141
142 /** compute exact PCR for each transport stream packet */
143 int mpeg2ts_compute_pcr;
144
145 /** fix dvb teletext pts */
146 int fix_teletext_pts;
147
148 int64_t cur_pcr; /**< used to estimate the exact PCR */
149 int64_t pcr_incr; /**< used to estimate the exact PCR */
150
151 /* data needed to handle file based ts */
152 /** stop parsing loop */
153 int stop_parse;
154 /** packet containing Audio/Video data */
155 AVPacket *pkt;
156 /** to detect seek */
157 int64_t last_pos;
158
159 int skip_changes;
160 int skip_clear;
161 int skip_unknown_pmt;
162
163 int scan_all_pmts;
164
165 int resync_size;
166 int merge_pmt_versions;
167 int max_packet_size;
168
169 /******************************************/
170 /* private mpegts data */
171 /* scan context */
172 /** structure to keep track of Program->pids mapping */
173 unsigned int nb_prg;
174 struct Program *prg;
175
176 int8_t crc_validity[NB_PID_MAX];
177 /** filters for various streams specified by PMT + for the PAT and PMT */
178 MpegTSFilter *pids[NB_PID_MAX];
179 int current_pid;
180
181 AVStream *epg_stream;
182 AVBufferPool* pools[32];
183 };
184
185 #define MPEGTS_OPTIONS \
186 { "resync_size", "set size limit for looking up a new synchronization", offsetof(MpegTSContext, resync_size), AV_OPT_TYPE_INT, { .i64 = MAX_RESYNC_SIZE}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }
187
188 static const AVOption options[] = {
189 MPEGTS_OPTIONS,
190 {"fix_teletext_pts", "try to fix pts values of dvb teletext streams", offsetof(MpegTSContext, fix_teletext_pts), AV_OPT_TYPE_BOOL,
191 {.i64 = 1}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
192 {"ts_packetsize", "output option carrying the raw packet size", offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT,
193 {.i64 = 0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
194 {"scan_all_pmts", "scan and combine all PMTs", offsetof(MpegTSContext, scan_all_pmts), AV_OPT_TYPE_BOOL,
195 {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM },
196 {"skip_unknown_pmt", "skip PMTs for programs not advertised in the PAT", offsetof(MpegTSContext, skip_unknown_pmt), AV_OPT_TYPE_BOOL,
197 {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
198 {"merge_pmt_versions", "re-use streams when PMT's version/pids change", offsetof(MpegTSContext, merge_pmt_versions), AV_OPT_TYPE_BOOL,
199 {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
200 {"skip_changes", "skip changing / adding streams / programs", offsetof(MpegTSContext, skip_changes), AV_OPT_TYPE_BOOL,
201 {.i64 = 0}, 0, 1, 0 },
202 {"skip_clear", "skip clearing programs", offsetof(MpegTSContext, skip_clear), AV_OPT_TYPE_BOOL,
203 {.i64 = 0}, 0, 1, 0 },
204 {"max_packet_size", "maximum size of emitted packet", offsetof(MpegTSContext, max_packet_size), AV_OPT_TYPE_INT,
205 {.i64 = 204800}, 1, INT_MAX/2, AV_OPT_FLAG_DECODING_PARAM },
206 { NULL },
207 };
208
209 static const AVClass mpegts_class = {
210 .class_name = "mpegts demuxer",
211 .item_name = av_default_item_name,
212 .option = options,
213 .version = LIBAVUTIL_VERSION_INT,
214 };
215
216 static const AVOption raw_options[] = {
217 MPEGTS_OPTIONS,
218 { "compute_pcr", "compute exact PCR for each transport stream packet",
219 offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_BOOL,
220 { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
221 { "ts_packetsize", "output option carrying the raw packet size",
222 offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT,
223 { .i64 = 0 }, 0, 0,
224 AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
225 { NULL },
226 };
227
228 static const AVClass mpegtsraw_class = {
229 .class_name = "mpegtsraw demuxer",
230 .item_name = av_default_item_name,
231 .option = raw_options,
232 .version = LIBAVUTIL_VERSION_INT,
233 };
234
235 /* TS stream handling */
236
237 enum MpegTSState {
238 MPEGTS_HEADER = 0,
239 MPEGTS_PESHEADER,
240 MPEGTS_PESHEADER_FILL,
241 MPEGTS_PAYLOAD,
242 MPEGTS_SKIP,
243 };
244
245 /* enough for PES header + length */
246 #define PES_START_SIZE 6
247 #define PES_HEADER_SIZE 9
248 #define MAX_PES_HEADER_SIZE (9 + 255)
249
250 typedef struct PESContext {
251 int pid;
252 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
253 int stream_type;
254 MpegTSContext *ts;
255 AVFormatContext *stream;
256 AVStream *st;
257 AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
258 enum MpegTSState state;
259 /* used to get the format */
260 int data_index;
261 int flags; /**< copied to the AVPacket flags */
262 int PES_packet_length;
263 int pes_header_size;
264 int extended_stream_id;
265 uint8_t stream_id;
266 int64_t pts, dts;
267 int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
268 uint8_t header[MAX_PES_HEADER_SIZE];
269 AVBufferRef *buffer;
270 SLConfigDescr sl;
271 int merged_st;
272 } PESContext;
273
274 extern const AVInputFormat ff_mpegts_demuxer;
275
276 431 static struct Program * get_program(MpegTSContext *ts, unsigned int programid)
277 {
278 int i;
279
2/2
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 48 times.
431 for (i = 0; i < ts->nb_prg; i++) {
280
1/2
✓ Branch 0 taken 383 times.
✗ Branch 1 not taken.
383 if (ts->prg[i].id == programid) {
281 383 return &ts->prg[i];
282 }
283 }
284 48 return NULL;
285 }
286
287 157 static void clear_avprogram(MpegTSContext *ts, unsigned int programid)
288 {
289 157 AVProgram *prg = NULL;
290 int i;
291
292
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 for (i = 0; i < ts->stream->nb_programs; i++)
293
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 if (ts->stream->programs[i]->id == programid) {
294 157 prg = ts->stream->programs[i];
295 157 break;
296 }
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (!prg)
298 return;
299 157 prg->nb_stream_indexes = 0;
300 }
301
302 287 static void clear_program(struct Program *p)
303 {
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (!p)
305 return;
306 287 p->nb_pids = 0;
307 287 p->nb_streams = 0;
308 287 p->pmt_found = 0;
309 }
310
311 48 static void clear_programs(MpegTSContext *ts)
312 {
313 48 av_freep(&ts->prg);
314 48 ts->nb_prg = 0;
315 48 }
316
317 192 static struct Program * add_program(MpegTSContext *ts, unsigned int programid)
318 {
319 192 struct Program *p = get_program(ts, programid);
320
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 48 times.
192 if (p)
321 144 return p;
322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
48 if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) {
323 ts->nb_prg = 0;
324 return NULL;
325 }
326 48 p = &ts->prg[ts->nb_prg];
327 48 p->id = programid;
328 48 clear_program(p);
329 48 ts->nb_prg++;
330 48 return p;
331 }
332
333 1165 static void add_pid_to_program(struct Program *p, unsigned int pid)
334 {
335 int i;
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1165 times.
1165 if (!p)
337 return;
338
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1165 times.
1165 if (p->nb_pids >= MAX_PIDS_PER_PROGRAM)
340 return;
341
342
2/2
✓ Branch 0 taken 1537 times.
✓ Branch 1 taken 776 times.
2313 for (i = 0; i < p->nb_pids; i++)
343
2/2
✓ Branch 0 taken 389 times.
✓ Branch 1 taken 1148 times.
1537 if (p->pids[i] == pid)
344 389 return;
345
346 776 p->pids[p->nb_pids++] = pid;
347 }
348
349 239 static void update_av_program_info(AVFormatContext *s, unsigned int programid,
350 unsigned int pid, int version)
351 {
352 int i;
353
1/2
✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
239 for (i = 0; i < s->nb_programs; i++) {
354 239 AVProgram *program = s->programs[i];
355
1/2
✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
239 if (program->id == programid) {
356 239 int old_pcr_pid = program->pcr_pid,
357 239 old_version = program->pmt_version;
358 239 program->pcr_pid = pid;
359 239 program->pmt_version = version;
360
361
4/4
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 178 times.
239 if (old_version != -1 && old_version != version) {
362 13 av_log(s, AV_LOG_VERBOSE,
363 "detected PMT change (program=%d, version=%d/%d, pcr_pid=0x%x/0x%x)\n",
364 programid, old_version, version, old_pcr_pid, pid);
365 }
366 239 break;
367 }
368 }
369 239 }
370
371 /**
372 * @brief discard_pid() decides if the pid is to be discarded according
373 * to caller's programs selection
374 * @param ts : - TS context
375 * @param pid : - pid
376 * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
377 * 0 otherwise
378 */
379 27575 static int discard_pid(MpegTSContext *ts, unsigned int pid)
380 {
381 int i, j, k;
382 27575 int used = 0, discarded = 0;
383 struct Program *p;
384
385
2/2
✓ Branch 0 taken 2810 times.
✓ Branch 1 taken 24765 times.
27575 if (pid == PAT_PID)
386 2810 return 0;
387
388 /* If none of the programs have .discard=AVDISCARD_ALL then there's
389 * no way we have to discard this packet */
390
2/2
✓ Branch 0 taken 24739 times.
✓ Branch 1 taken 24765 times.
49504 for (k = 0; k < ts->stream->nb_programs; k++)
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24739 times.
24739 if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
392 break;
393
1/2
✓ Branch 0 taken 24765 times.
✗ Branch 1 not taken.
24765 if (k == ts->stream->nb_programs)
394 24765 return 0;
395
396 for (i = 0; i < ts->nb_prg; i++) {
397 p = &ts->prg[i];
398 for (j = 0; j < p->nb_pids; j++) {
399 if (p->pids[j] != pid)
400 continue;
401 // is program with id p->id set to be discarded?
402 for (k = 0; k < ts->stream->nb_programs; k++) {
403 if (ts->stream->programs[k]->id == p->id) {
404 if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
405 discarded++;
406 else
407 used++;
408 }
409 }
410 }
411 }
412
413 return !used && discarded;
414 }
415
416 /**
417 * Assemble PES packets out of TS packets, and then call the "section_cb"
418 * function when they are complete.
419 */
420 6071 static void write_section_data(MpegTSContext *ts, MpegTSFilter *tss1,
421 const uint8_t *buf, int buf_size, int is_start)
422 {
423 6071 MpegTSSectionFilter *tss = &tss1->u.section_filter;
424 6071 uint8_t *cur_section_buf = NULL;
425 int len, offset;
426
427
2/2
✓ Branch 0 taken 6036 times.
✓ Branch 1 taken 35 times.
6071 if (is_start) {
428 6036 memcpy(tss->section_buf, buf, buf_size);
429 6036 tss->section_index = buf_size;
430 6036 tss->section_h_size = -1;
431 6036 tss->end_of_section_reached = 0;
432 } else {
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (tss->end_of_section_reached)
434 return;
435 35 len = MAX_SECTION_SIZE - tss->section_index;
436
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 if (buf_size < len)
437 35 len = buf_size;
438 35 memcpy(tss->section_buf + tss->section_index, buf, len);
439 35 tss->section_index += len;
440 }
441
442 6071 offset = 0;
443 6071 cur_section_buf = tss->section_buf;
444
3/4
✓ Branch 0 taken 12107 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6279 times.
✓ Branch 3 taken 5828 times.
12107 while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && cur_section_buf[0] != 0xff) {
445 /* compute section length if possible */
446
3/4
✓ Branch 0 taken 6279 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6071 times.
✓ Branch 3 taken 208 times.
6279 if (tss->section_h_size == -1 && tss->section_index - offset >= 3) {
447 6071 len = (AV_RB16(cur_section_buf + 1) & 0xfff) + 3;
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6071 times.
6071 if (len > MAX_SECTION_SIZE)
449 return;
450 6071 tss->section_h_size = len;
451 }
452
453
2/2
✓ Branch 0 taken 6071 times.
✓ Branch 1 taken 208 times.
6279 if (tss->section_h_size != -1 &&
454
2/2
✓ Branch 0 taken 6036 times.
✓ Branch 1 taken 35 times.
6071 tss->section_index >= offset + tss->section_h_size) {
455 6036 int crc_valid = 1;
456 6036 tss->end_of_section_reached = 1;
457
458
1/2
✓ Branch 0 taken 6036 times.
✗ Branch 1 not taken.
6036 if (tss->check_crc) {
459 6036 crc_valid = !av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, cur_section_buf, tss->section_h_size);
460
1/2
✓ Branch 0 taken 6036 times.
✗ Branch 1 not taken.
6036 if (tss->section_h_size >= 4)
461 6036 tss->crc = AV_RB32(cur_section_buf + tss->section_h_size - 4);
462
463
1/2
✓ Branch 0 taken 6036 times.
✗ Branch 1 not taken.
6036 if (crc_valid) {
464 6036 ts->crc_validity[ tss1->pid ] = 100;
465 }else if (ts->crc_validity[ tss1->pid ] > -10) {
466 ts->crc_validity[ tss1->pid ]--;
467 }else
468 crc_valid = 2;
469 }
470
1/2
✓ Branch 0 taken 6036 times.
✗ Branch 1 not taken.
6036 if (crc_valid) {
471 6036 tss->section_cb(tss1, cur_section_buf, tss->section_h_size);
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6036 times.
6036 if (crc_valid != 1)
473 tss->last_ver = -1;
474 }
475
476 6036 cur_section_buf += tss->section_h_size;
477 6036 offset += tss->section_h_size;
478 6036 tss->section_h_size = -1;
479 } else {
480 243 tss->section_h_size = -1;
481 243 tss->end_of_section_reached = 0;
482 243 break;
483 }
484 }
485 }
486
487 304 static MpegTSFilter *mpegts_open_filter(MpegTSContext *ts, unsigned int pid,
488 enum MpegTSFilterType type)
489 {
490 MpegTSFilter *filter;
491
492 304 av_log(ts->stream, AV_LOG_TRACE, "Filter: pid=0x%x type=%d\n", pid, type);
493
494
2/4
✓ Branch 0 taken 304 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 304 times.
304 if (pid >= NB_PID_MAX || ts->pids[pid])
495 return NULL;
496 304 filter = av_mallocz(sizeof(MpegTSFilter));
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 if (!filter)
498 return NULL;
499 304 ts->pids[pid] = filter;
500
501 304 filter->type = type;
502 304 filter->pid = pid;
503 304 filter->es_id = -1;
504 304 filter->last_cc = -1;
505 304 filter->last_pcr= -1;
506
507 304 return filter;
508 }
509
510 197 static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts,
511 unsigned int pid,
512 SectionCallback *section_cb,
513 void *opaque,
514 int check_crc)
515 {
516 MpegTSFilter *filter;
517 MpegTSSectionFilter *sec;
518 197 uint8_t *section_buf = av_mallocz(MAX_SECTION_SIZE);
519
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (!section_buf)
521 return NULL;
522
523
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 197 times.
197 if (!(filter = mpegts_open_filter(ts, pid, MPEGTS_SECTION))) {
524 av_free(section_buf);
525 return NULL;
526 }
527 197 sec = &filter->u.section_filter;
528 197 sec->section_cb = section_cb;
529 197 sec->opaque = opaque;
530 197 sec->section_buf = section_buf;
531 197 sec->check_crc = check_crc;
532 197 sec->last_ver = -1;
533
534 197 return filter;
535 }
536
537 107 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
538 PESCallback *pes_cb,
539 void *opaque)
540 {
541 MpegTSFilter *filter;
542 MpegTSPESFilter *pes;
543
544
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 107 times.
107 if (!(filter = mpegts_open_filter(ts, pid, MPEGTS_PES)))
545 return NULL;
546
547 107 pes = &filter->u.pes_filter;
548 107 pes->pes_cb = pes_cb;
549 107 pes->opaque = opaque;
550 107 return filter;
551 }
552
553 static MpegTSFilter *mpegts_open_pcr_filter(MpegTSContext *ts, unsigned int pid)
554 {
555 return mpegts_open_filter(ts, pid, MPEGTS_PCR);
556 }
557
558 304 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
559 {
560 int pid;
561
562 304 pid = filter->pid;
563
2/2
✓ Branch 0 taken 197 times.
✓ Branch 1 taken 107 times.
304 if (filter->type == MPEGTS_SECTION)
564 197 av_freep(&filter->u.section_filter.section_buf);
565
1/2
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
107 else if (filter->type == MPEGTS_PES) {
566 107 PESContext *pes = filter->u.pes_filter.opaque;
567 107 av_buffer_unref(&pes->buffer);
568 /* referenced private data will be freed later in
569 * avformat_close_input (pes->st->priv_data == pes) */
570
4/4
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 91 times.
107 if (!pes->st || pes->merged_st) {
571 16 av_freep(&filter->u.pes_filter.opaque);
572 }
573 }
574
575 304 av_free(filter);
576 304 ts->pids[pid] = NULL;
577 304 }
578
579 35514 static int analyze(const uint8_t *buf, int size, int packet_size,
580 int probe)
581 {
582 int stat[TS_MAX_PACKET_SIZE];
583 35514 int stat_all = 0;
584 int i;
585 35514 int best_score = 0;
586
587 35514 memset(stat, 0, packet_size * sizeof(*stat));
588
589
2/2
✓ Branch 0 taken 400549514 times.
✓ Branch 1 taken 35514 times.
400585028 for (i = 0; i < size - 3; i++) {
590
2/2
✓ Branch 0 taken 749012 times.
✓ Branch 1 taken 399800502 times.
400549514 if (buf[i] == 0x47) {
591 749012 int pid = AV_RB16(buf+1) & 0x1FFF;
592 749012 int asc = buf[i + 3] & 0x30;
593
6/6
✓ Branch 0 taken 730328 times.
✓ Branch 1 taken 18684 times.
✓ Branch 2 taken 729724 times.
✓ Branch 3 taken 604 times.
✓ Branch 4 taken 584127 times.
✓ Branch 5 taken 145597 times.
749012 if (!probe || pid == 0x1FFF || asc) {
594 603415 int x = i % packet_size;
595 603415 stat[x]++;
596 603415 stat_all++;
597
2/2
✓ Branch 0 taken 56002 times.
✓ Branch 1 taken 547413 times.
603415 if (stat[x] > best_score) {
598 56002 best_score = stat[x];
599 }
600 }
601 }
602 }
603
604 35514 return best_score - FFMAX(stat_all - 10*best_score, 0)/10;
605 }
606
607 /* autodetect fec presence */
608 96 static int get_packet_size(AVFormatContext* s)
609 {
610 int score, fec_score, dvhs_score;
611 int margin;
612 int ret;
613
614 /*init buffer to store stream for probing */
615 96 uint8_t buf[PROBE_PACKET_MAX_BUF] = {0};
616 96 int buf_size = 0;
617 96 int max_iterations = 16;
618
619
2/4
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
96 while (buf_size < PROBE_PACKET_MAX_BUF && max_iterations--) {
620 96 ret = avio_read_partial(s->pb, buf + buf_size, PROBE_PACKET_MAX_BUF - buf_size);
621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (ret < 0)
622 return AVERROR_INVALIDDATA;
623 96 buf_size += ret;
624
625 96 score = analyze(buf, buf_size, TS_PACKET_SIZE, 0);
626 96 dvhs_score = analyze(buf, buf_size, TS_DVHS_PACKET_SIZE, 0);
627 96 fec_score = analyze(buf, buf_size, TS_FEC_PACKET_SIZE, 0);
628 96 av_log(s, AV_LOG_TRACE, "Probe: %d, score: %d, dvhs_score: %d, fec_score: %d \n",
629 buf_size, score, dvhs_score, fec_score);
630
631 96 margin = mid_pred(score, fec_score, dvhs_score);
632
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (buf_size < PROBE_PACKET_MAX_BUF)
634 margin += PROBE_PACKET_MARGIN; /*if buffer not filled */
635
636
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if (score > margin)
637 96 return TS_PACKET_SIZE;
638 else if (dvhs_score > margin)
639 return TS_DVHS_PACKET_SIZE;
640 else if (fec_score > margin)
641 return TS_FEC_PACKET_SIZE;
642 }
643 return AVERROR_INVALIDDATA;
644 }
645
646 typedef struct SectionHeader {
647 uint8_t tid;
648 uint16_t id;
649 uint8_t version;
650 uint8_t current_next;
651 uint8_t sec_num;
652 uint8_t last_sec_num;
653 } SectionHeader;
654
655 5418 static int skip_identical(const SectionHeader *h, MpegTSSectionFilter *tssf)
656 {
657
3/4
✓ Branch 0 taken 4890 times.
✓ Branch 1 taken 528 times.
✓ Branch 2 taken 4890 times.
✗ Branch 3 not taken.
5418 if (h->version == tssf->last_ver && tssf->last_crc == tssf->crc)
658 4890 return 1;
659
660 528 tssf->last_ver = h->version;
661 528 tssf->last_crc = tssf->crc;
662
663 528 return 0;
664 }
665
666 29070 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
667 {
668 const uint8_t *p;
669 int c;
670
671 29070 p = *pp;
672
2/2
✓ Branch 0 taken 1298 times.
✓ Branch 1 taken 27772 times.
29070 if (p >= p_end)
673 1298 return AVERROR_INVALIDDATA;
674 27772 c = *p++;
675 27772 *pp = p;
676 27772 return c;
677 }
678
679 8961 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
680 {
681 const uint8_t *p;
682 int c;
683
684 8961 p = *pp;
685
2/2
✓ Branch 0 taken 289 times.
✓ Branch 1 taken 8672 times.
8961 if (1 >= p_end - p)
686 289 return AVERROR_INVALIDDATA;
687 8672 c = AV_RB16(p);
688 8672 p += 2;
689 8672 *pp = p;
690 8672 return c;
691 }
692
693 /* read and allocate a DVB string preceded by its length */
694 194 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
695 {
696 int len;
697 const uint8_t *p;
698 char *str;
699
700 194 p = *pp;
701 194 len = get8(&p, p_end);
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 if (len < 0)
703 return NULL;
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 if (len > p_end - p)
705 return NULL;
706 #if CONFIG_ICONV
707
1/2
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
194 if (len) {
708 194 const char *encodings[] = {
709 "ISO6937", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7",
710 "ISO-8859-8", "ISO-8859-9", "ISO-8859-10", "ISO-8859-11",
711 "", "ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "", "", "", "",
712 "", "UCS-2BE", "KSC_5601", "GB2312", "UCS-2BE", "UTF-8", "", "",
713 "", "", "", "", "", "", "", ""
714 };
715 iconv_t cd;
716 char *in, *out;
717 194 size_t inlen = len, outlen = inlen * 6 + 1;
718
2/12
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 194 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
194 if (len >= 3 && p[0] == 0x10 && !p[1] && p[2] && p[2] <= 0xf && p[2] != 0xc) {
719 char iso8859[12];
720 snprintf(iso8859, sizeof(iso8859), "ISO-8859-%d", p[2]);
721 inlen -= 3;
722 in = (char *)p + 3;
723 cd = iconv_open("UTF-8", iso8859);
724
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 186 times.
194 } else if (p[0] < 0x20) {
725 8 inlen -= 1;
726 8 in = (char *)p + 1;
727 8 cd = iconv_open("UTF-8", encodings[*p]);
728 } else {
729 186 in = (char *)p;
730 186 cd = iconv_open("UTF-8", encodings[0]);
731 }
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 if (cd == (iconv_t)-1)
733 goto no_iconv;
734 194 str = out = av_malloc(outlen);
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 if (!str) {
736 iconv_close(cd);
737 194 return NULL;
738 }
739
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 194 times.
194 if (iconv(cd, &in, &inlen, &out, &outlen) == -1) {
740 iconv_close(cd);
741 av_freep(&str);
742 goto no_iconv;
743 }
744 194 iconv_close(cd);
745 194 *out = 0;
746 194 *pp = p + len;
747 194 return str;
748 }
749 no_iconv:
750 #endif
751 str = av_malloc(len + 1);
752 if (!str)
753 return NULL;
754 memcpy(str, p, len);
755 str[len] = '\0';
756 p += len;
757 *pp = p;
758 return str;
759 }
760
761 6034 static int parse_section_header(SectionHeader *h,
762 const uint8_t **pp, const uint8_t *p_end)
763 {
764 int val;
765
766 6034 val = get8(pp, p_end);
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6034 times.
6034 if (val < 0)
768 return val;
769 6034 h->tid = val;
770 6034 *pp += 2;
771 6034 val = get16(pp, p_end);
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6034 times.
6034 if (val < 0)
773 return val;
774 6034 h->id = val;
775 6034 val = get8(pp, p_end);
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6034 times.
6034 if (val < 0)
777 return val;
778 6034 h->version = (val >> 1) & 0x1f;
779 6034 h->current_next = val & 0x01;
780 6034 val = get8(pp, p_end);
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6034 times.
6034 if (val < 0)
782 return val;
783 6034 h->sec_num = val;
784 6034 val = get8(pp, p_end);
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6034 times.
6034 if (val < 0)
786 return val;
787 6034 h->last_sec_num = val;
788 6034 return 0;
789 }
790
791 typedef struct StreamType {
792 uint32_t stream_type;
793 enum AVMediaType codec_type;
794 enum AVCodecID codec_id;
795 } StreamType;
796
797 static const StreamType ISO_types[] = {
798 { 0x01, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
799 { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
800 { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
801 { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
802 { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
803 { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 },
804 /* Makito encoder sets stream type 0x11 for AAC,
805 * so auto-detect LOAS/LATM instead of hardcoding it. */
806 #if !CONFIG_LOAS_DEMUXER
807 { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
808 #endif
809 { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
810 { 0x1c, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
811 { 0x20, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
812 { 0x21, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_JPEG2000 },
813 { 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
814 { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS },
815 { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
816 { 0xd2, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_AVS2 },
817 { 0xd4, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_AVS3 },
818 { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
819 { 0 },
820 };
821
822 static const StreamType HDMV_types[] = {
823 { 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY },
824 { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
825 { 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
826 { 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD },
827 { 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
828 { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
829 { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
830 { 0xa1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC3 Secondary Audio */
831 { 0xa2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS Express Secondary Audio */
832 { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
833 { 0x92, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_TEXT_SUBTITLE },
834 { 0 },
835 };
836
837 /* SCTE types */
838 static const StreamType SCTE_types[] = {
839 { 0x86, AVMEDIA_TYPE_DATA, AV_CODEC_ID_SCTE_35 },
840 { 0 },
841 };
842
843 /* ATSC ? */
844 static const StreamType MISC_types[] = {
845 { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
846 { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
847 { 0 },
848 };
849
850 /* HLS Sample Encryption Types */
851 static const StreamType HLS_SAMPLE_ENC_types[] = {
852 { 0xdb, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264},
853 { 0xcf, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
854 { 0xc1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
855 { 0xc2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3},
856 { 0 },
857 };
858
859 static const StreamType REGD_types[] = {
860 { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
861 { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
862 { MKTAG('B', 'S', 'S', 'D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
863 { MKTAG('D', 'T', 'S', '1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
864 { MKTAG('D', 'T', 'S', '2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
865 { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
866 { MKTAG('E', 'A', 'C', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
867 { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
868 { MKTAG('K', 'L', 'V', 'A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV },
869 { MKTAG('I', 'D', '3', ' '), AVMEDIA_TYPE_DATA, AV_CODEC_ID_TIMED_ID3 },
870 { MKTAG('V', 'C', '-', '1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
871 { MKTAG('O', 'p', 'u', 's'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_OPUS },
872 { 0 },
873 };
874
875 static const StreamType METADATA_types[] = {
876 { MKTAG('K','L','V','A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV },
877 { MKTAG('I','D','3',' '), AVMEDIA_TYPE_DATA, AV_CODEC_ID_TIMED_ID3 },
878 { 0 },
879 };
880
881 /* descriptor present */
882 static const StreamType DESC_types[] = {
883 { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
884 { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
885 { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
886 { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
887 { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
888 { 0 },
889 };
890
891 140 static void mpegts_find_stream_type(AVStream *st,
892 uint32_t stream_type,
893 const StreamType *types)
894 {
895 140 FFStream *const sti = ffstream(st);
896
2/2
✓ Branch 0 taken 718 times.
✓ Branch 1 taken 44 times.
762 for (; types->stream_type; types++)
897
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 622 times.
718 if (stream_type == types->stream_type) {
898
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 88 times.
96 if (st->codecpar->codec_type != types->codec_type ||
899
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 st->codecpar->codec_id != types->codec_id) {
900 95 st->codecpar->codec_type = types->codec_type;
901 95 st->codecpar->codec_id = types->codec_id;
902 95 sti->need_context_update = 1;
903 }
904 96 sti->request_probe = 0;
905 96 return;
906 }
907 }
908
909 91 static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
910 uint32_t stream_type, uint32_t prog_reg_desc)
911 {
912 91 FFStream *const sti = ffstream(st);
913 91 int old_codec_type = st->codecpar->codec_type;
914 91 int old_codec_id = st->codecpar->codec_id;
915 91 int old_codec_tag = st->codecpar->codec_tag;
916
917
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 91 times.
91 if (avcodec_is_open(sti->avctx)) {
918 av_log(pes->stream, AV_LOG_DEBUG, "cannot set stream info, internal codec is open\n");
919 return 0;
920 }
921
922 91 avpriv_set_pts_info(st, 33, 1, 90000);
923 91 st->priv_data = pes;
924 91 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
925 91 st->codecpar->codec_id = AV_CODEC_ID_NONE;
926 91 sti->need_parsing = AVSTREAM_PARSE_FULL;
927 91 pes->st = st;
928 91 pes->stream_type = stream_type;
929
930 91 av_log(pes->stream, AV_LOG_DEBUG,
931 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
932 st->index, pes->stream_type, pes->pid, (char *)&prog_reg_desc);
933
934 91 st->codecpar->codec_tag = pes->stream_type;
935
936 91 mpegts_find_stream_type(st, pes->stream_type, ISO_types);
937
4/4
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 84 times.
91 if (pes->stream_type == 4 || pes->stream_type == 0x0f)
938 7 sti->request_probe = 50;
939
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
91 if ((prog_reg_desc == AV_RL32("HDMV") ||
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91 times.
91 prog_reg_desc == AV_RL32("HDPR")) &&
941 st->codecpar->codec_id == AV_CODEC_ID_NONE) {
942 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
943 if (pes->stream_type == 0x83) {
944 // HDMV TrueHD streams also contain an AC3 coded version of the
945 // audio track - add a second stream for this
946 AVStream *sub_st;
947 // priv_data cannot be shared between streams
948 PESContext *sub_pes = av_memdup(pes, sizeof(*sub_pes));
949 if (!sub_pes)
950 return AVERROR(ENOMEM);
951
952 sub_st = avformat_new_stream(pes->stream, NULL);
953 if (!sub_st) {
954 av_free(sub_pes);
955 return AVERROR(ENOMEM);
956 }
957
958 sub_st->id = pes->pid;
959 avpriv_set_pts_info(sub_st, 33, 1, 90000);
960 sub_st->priv_data = sub_pes;
961 sub_st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
962 sub_st->codecpar->codec_id = AV_CODEC_ID_AC3;
963 ffstream(sub_st)->need_parsing = AVSTREAM_PARSE_FULL;
964 sub_pes->sub_st = pes->sub_st = sub_st;
965 }
966 }
967
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 69 times.
91 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
968 22 mpegts_find_stream_type(st, pes->stream_type, MISC_types);
969
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 84 times.
91 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
970 7 mpegts_find_stream_type(st, pes->stream_type, HLS_SAMPLE_ENC_types);
971
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 84 times.
91 if (st->codecpar->codec_id == AV_CODEC_ID_NONE) {
972 7 st->codecpar->codec_id = old_codec_id;
973 7 st->codecpar->codec_type = old_codec_type;
974 }
975
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 7 times.
91 if ((st->codecpar->codec_id == AV_CODEC_ID_NONE ||
976
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 77 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
84 (sti->request_probe > 0 && sti->request_probe < AVPROBE_SCORE_STREAM_RETRY / 5)) &&
977
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
7 sti->probe_packets > 0 &&
978 stream_type == STREAM_TYPE_PRIVATE_DATA) {
979 5 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
980 5 st->codecpar->codec_id = AV_CODEC_ID_BIN_DATA;
981 5 sti->request_probe = AVPROBE_SCORE_STREAM_RETRY / 5;
982 }
983
984 /* queue a context update if properties changed */
985
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 89 times.
91 if (old_codec_type != st->codecpar->codec_type ||
986
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 old_codec_id != st->codecpar->codec_id ||
987
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 old_codec_tag != st->codecpar->codec_tag)
988 91 sti->need_context_update = 1;
989
990 91 return 0;
991 }
992
993 39540 static void reset_pes_packet_state(PESContext *pes)
994 {
995 39540 pes->pts = AV_NOPTS_VALUE;
996 39540 pes->dts = AV_NOPTS_VALUE;
997 39540 pes->data_index = 0;
998 39540 pes->flags = 0;
999 39540 av_buffer_unref(&pes->buffer);
1000 39540 }
1001
1002 12 static void new_data_packet(const uint8_t *buffer, int len, AVPacket *pkt)
1003 {
1004 12 av_packet_unref(pkt);
1005 12 pkt->data = (uint8_t *)buffer;
1006 12 pkt->size = len;
1007 12 }
1008
1009 20846 static int new_pes_packet(PESContext *pes, AVPacket *pkt)
1010 {
1011 uint8_t *sd;
1012
1013 20846 av_packet_unref(pkt);
1014
1015 20846 pkt->buf = pes->buffer;
1016 20846 pkt->data = pes->buffer->data;
1017 20846 pkt->size = pes->data_index;
1018
1019
2/2
✓ Branch 0 taken 17940 times.
✓ Branch 1 taken 2906 times.
20846 if (pes->PES_packet_length &&
1020
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 17872 times.
17940 pes->pes_header_size + pes->data_index != pes->PES_packet_length +
1021 PES_START_SIZE) {
1022 68 av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
1023 68 pes->flags |= AV_PKT_FLAG_CORRUPT;
1024 }
1025 20846 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1026
1027 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
1028
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 20846 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
20846 if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
1029 pkt->stream_index = pes->sub_st->index;
1030 else
1031 20846 pkt->stream_index = pes->st->index;
1032 20846 pkt->pts = pes->pts;
1033 20846 pkt->dts = pes->dts;
1034 /* store position of first TS packet of this PES packet */
1035 20846 pkt->pos = pes->ts_packet_pos;
1036 20846 pkt->flags = pes->flags;
1037
1038 20846 pes->buffer = NULL;
1039 20846 reset_pes_packet_state(pes);
1040
1041 20846 sd = av_packet_new_side_data(pkt, AV_PKT_DATA_MPEGTS_STREAM_ID, 1);
1042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20846 times.
20846 if (!sd)
1043 return AVERROR(ENOMEM);
1044 20846 *sd = pes->stream_id;
1045
1046 20846 return 0;
1047 }
1048
1049 static uint64_t get_ts64(GetBitContext *gb, int bits)
1050 {
1051 if (get_bits_left(gb) < bits)
1052 return AV_NOPTS_VALUE;
1053 return get_bits64(gb, bits);
1054 }
1055
1056 static int read_sl_header(PESContext *pes, SLConfigDescr *sl,
1057 const uint8_t *buf, int buf_size)
1058 {
1059 GetBitContext gb;
1060 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
1061 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
1062 int dts_flag = -1, cts_flag = -1;
1063 int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
1064 uint8_t buf_padded[128 + AV_INPUT_BUFFER_PADDING_SIZE];
1065 int buf_padded_size = FFMIN(buf_size, sizeof(buf_padded) - AV_INPUT_BUFFER_PADDING_SIZE);
1066
1067 memcpy(buf_padded, buf, buf_padded_size);
1068
1069 init_get_bits(&gb, buf_padded, buf_padded_size * 8);
1070
1071 if (sl->use_au_start)
1072 au_start_flag = get_bits1(&gb);
1073 if (sl->use_au_end)
1074 au_end_flag = get_bits1(&gb);
1075 if (!sl->use_au_start && !sl->use_au_end)
1076 au_start_flag = au_end_flag = 1;
1077 if (sl->ocr_len > 0)
1078 ocr_flag = get_bits1(&gb);
1079 if (sl->use_idle)
1080 idle_flag = get_bits1(&gb);
1081 if (sl->use_padding)
1082 padding_flag = get_bits1(&gb);
1083 if (padding_flag)
1084 padding_bits = get_bits(&gb, 3);
1085
1086 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
1087 if (sl->packet_seq_num_len)
1088 skip_bits_long(&gb, sl->packet_seq_num_len);
1089 if (sl->degr_prior_len)
1090 if (get_bits1(&gb))
1091 skip_bits(&gb, sl->degr_prior_len);
1092 if (ocr_flag)
1093 skip_bits_long(&gb, sl->ocr_len);
1094 if (au_start_flag) {
1095 if (sl->use_rand_acc_pt)
1096 get_bits1(&gb);
1097 if (sl->au_seq_num_len > 0)
1098 skip_bits_long(&gb, sl->au_seq_num_len);
1099 if (sl->use_timestamps) {
1100 dts_flag = get_bits1(&gb);
1101 cts_flag = get_bits1(&gb);
1102 }
1103 }
1104 if (sl->inst_bitrate_len)
1105 inst_bitrate_flag = get_bits1(&gb);
1106 if (dts_flag == 1)
1107 dts = get_ts64(&gb, sl->timestamp_len);
1108 if (cts_flag == 1)
1109 cts = get_ts64(&gb, sl->timestamp_len);
1110 if (sl->au_len > 0)
1111 skip_bits_long(&gb, sl->au_len);
1112 if (inst_bitrate_flag)
1113 skip_bits_long(&gb, sl->inst_bitrate_len);
1114 }
1115
1116 if (dts != AV_NOPTS_VALUE)
1117 pes->dts = dts;
1118 if (cts != AV_NOPTS_VALUE)
1119 pes->pts = cts;
1120
1121 if (sl->timestamp_len && sl->timestamp_res)
1122 avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
1123
1124 return (get_bits_count(&gb) + 7) >> 3;
1125 }
1126
1127 20942 static AVBufferRef *buffer_pool_get(MpegTSContext *ts, int size)
1128 {
1129 20942 int index = av_log2(size + AV_INPUT_BUFFER_PADDING_SIZE);
1130
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 20814 times.
20942 if (!ts->pools[index]) {
1131
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 24 times.
128 int pool_size = FFMIN(ts->max_packet_size + AV_INPUT_BUFFER_PADDING_SIZE, 2 << index);
1132 128 ts->pools[index] = av_buffer_pool_init(pool_size, NULL);
1133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (!ts->pools[index])
1134 return NULL;
1135 }
1136 20942 return av_buffer_pool_get(ts->pools[index]);
1137 }
1138
1139 /* return non zero if a packet could be constructed */
1140 319156 static int mpegts_push_data(MpegTSFilter *filter,
1141 const uint8_t *buf, int buf_size, int is_start,
1142 int64_t pos)
1143 {
1144 319156 PESContext *pes = filter->u.pes_filter.opaque;
1145 319156 MpegTSContext *ts = pes->ts;
1146 const uint8_t *p;
1147 int ret, len;
1148
1149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 319156 times.
319156 if (!ts->pkt)
1150 return 0;
1151
1152
2/2
✓ Branch 0 taken 21532 times.
✓ Branch 1 taken 297624 times.
319156 if (is_start) {
1153
3/4
✓ Branch 0 taken 2838 times.
✓ Branch 1 taken 18694 times.
✓ Branch 2 taken 2838 times.
✗ Branch 3 not taken.
21532 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
1154 2838 ret = new_pes_packet(pes, ts->pkt);
1155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2838 times.
2838 if (ret < 0)
1156 return ret;
1157 2838 ts->stop_parse = 1;
1158 } else {
1159 18694 reset_pes_packet_state(pes);
1160 }
1161 21532 pes->state = MPEGTS_HEADER;
1162 21532 pes->ts_packet_pos = pos;
1163 }
1164 319156 p = buf;
1165
2/2
✓ Branch 0 taken 382570 times.
✓ Branch 1 taken 319156 times.
1020882 while (buf_size > 0) {
1166
5/6
✓ Branch 0 taken 21532 times.
✓ Branch 1 taken 20942 times.
✓ Branch 2 taken 20942 times.
✓ Branch 3 taken 303386 times.
✓ Branch 4 taken 15768 times.
✗ Branch 5 not taken.
382570 switch (pes->state) {
1167 21532 case MPEGTS_HEADER:
1168 21532 len = PES_START_SIZE - pes->data_index;
1169
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21530 times.
21532 if (len > buf_size)
1170 2 len = buf_size;
1171 21532 memcpy(pes->header + pes->data_index, p, len);
1172 21532 pes->data_index += len;
1173 21532 p += len;
1174 21532 buf_size -= len;
1175
2/2
✓ Branch 0 taken 21530 times.
✓ Branch 1 taken 2 times.
21532 if (pes->data_index == PES_START_SIZE) {
1176 /* we got all the PES or section header. We can now
1177 * decide */
1178
3/4
✓ Branch 0 taken 21523 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21523 times.
21530 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21523 times.
21523 pes->header[2] == 0x01) {
1180 /* it must be an MPEG-2 PES stream */
1181 21523 pes->stream_id = pes->header[3];
1182 21523 av_log(pes->stream, AV_LOG_TRACE, "pid=%x stream_id=%#x\n", pes->pid, pes->stream_id);
1183
1184
3/4
✓ Branch 0 taken 21523 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 581 times.
✓ Branch 3 taken 20942 times.
21523 if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
1185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 581 times.
581 (!pes->sub_st ||
1186 pes->sub_st->discard == AVDISCARD_ALL)) ||
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
20942 pes->stream_id == STREAM_ID_PADDING_STREAM)
1188 581 goto skip;
1189
1190 /* stream not present in PMT */
1191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
20942 if (!pes->st) {
1192 if (ts->skip_changes)
1193 goto skip;
1194 if (ts->merge_pmt_versions)
1195 goto skip; /* wait for PMT to merge new stream */
1196
1197 pes->st = avformat_new_stream(ts->stream, NULL);
1198 if (!pes->st)
1199 return AVERROR(ENOMEM);
1200 pes->st->id = pes->pid;
1201 mpegts_set_stream_info(pes->st, pes, 0, 0);
1202 }
1203
1204 20942 pes->PES_packet_length = AV_RB16(pes->header + 4);
1205 /* NOTE: zero length means the PES size is unbounded */
1206
1207
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 if (pes->stream_id != STREAM_ID_PROGRAM_STREAM_MAP &&
1208
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 pes->stream_id != STREAM_ID_PRIVATE_STREAM_2 &&
1209
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 pes->stream_id != STREAM_ID_ECM_STREAM &&
1210
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 pes->stream_id != STREAM_ID_EMM_STREAM &&
1211
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 pes->stream_id != STREAM_ID_PROGRAM_STREAM_DIRECTORY &&
1212
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 pes->stream_id != STREAM_ID_DSMCC_STREAM &&
1213
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
41884 pes->stream_id != STREAM_ID_TYPE_E_STREAM) {
1214 20942 FFStream *const pes_sti = ffstream(pes->st);
1215 20942 pes->state = MPEGTS_PESHEADER;
1216
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20940 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
20942 if (pes->st->codecpar->codec_id == AV_CODEC_ID_NONE && !pes_sti->request_probe) {
1217 2 av_log(pes->stream, AV_LOG_TRACE,
1218 "pid=%x stream_type=%x probing\n",
1219 pes->pid,
1220 pes->stream_type);
1221 2 pes_sti->request_probe = 1;
1222 }
1223 } else {
1224 pes->pes_header_size = 6;
1225 pes->state = MPEGTS_PAYLOAD;
1226 pes->data_index = 0;
1227 }
1228 } else {
1229 /* otherwise, it should be a table */
1230 /* skip packet */
1231 7 skip:
1232 588 pes->state = MPEGTS_SKIP;
1233 588 continue;
1234 }
1235 }
1236 20944 break;
1237 /**********************************************/
1238 /* PES packing parsing */
1239 20942 case MPEGTS_PESHEADER:
1240 20942 len = PES_HEADER_SIZE - pes->data_index;
1241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
20942 if (len < 0)
1242 return AVERROR_INVALIDDATA;
1243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
20942 if (len > buf_size)
1244 len = buf_size;
1245 20942 memcpy(pes->header + pes->data_index, p, len);
1246 20942 pes->data_index += len;
1247 20942 p += len;
1248 20942 buf_size -= len;
1249
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 if (pes->data_index == PES_HEADER_SIZE) {
1250 20942 pes->pes_header_size = pes->header[8] + 9;
1251 20942 pes->state = MPEGTS_PESHEADER_FILL;
1252 }
1253 20942 break;
1254 20942 case MPEGTS_PESHEADER_FILL:
1255 20942 len = pes->pes_header_size - pes->data_index;
1256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
20942 if (len < 0)
1257 return AVERROR_INVALIDDATA;
1258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
20942 if (len > buf_size)
1259 len = buf_size;
1260 20942 memcpy(pes->header + pes->data_index, p, len);
1261 20942 pes->data_index += len;
1262 20942 p += len;
1263 20942 buf_size -= len;
1264
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 if (pes->data_index == pes->pes_header_size) {
1265 const uint8_t *r;
1266 unsigned int flags, pes_ext, skip;
1267
1268 20942 flags = pes->header[7];
1269 20942 r = pes->header + 9;
1270 20942 pes->pts = AV_NOPTS_VALUE;
1271 20942 pes->dts = AV_NOPTS_VALUE;
1272
2/2
✓ Branch 0 taken 9493 times.
✓ Branch 1 taken 11449 times.
20942 if ((flags & 0xc0) == 0x80) {
1273 9493 pes->dts = pes->pts = ff_parse_pes_pts(r);
1274 9493 r += 5;
1275
2/2
✓ Branch 0 taken 1840 times.
✓ Branch 1 taken 9609 times.
11449 } else if ((flags & 0xc0) == 0xc0) {
1276 1840 pes->pts = ff_parse_pes_pts(r);
1277 1840 r += 5;
1278 1840 pes->dts = ff_parse_pes_pts(r);
1279 1840 r += 5;
1280 }
1281 20942 pes->extended_stream_id = -1;
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
20942 if (flags & 0x01) { /* PES extension */
1283 pes_ext = *r++;
1284 /* Skip PES private data, program packet sequence counter and P-STD buffer */
1285 skip = (pes_ext >> 4) & 0xb;
1286 skip += skip & 0x9;
1287 r += skip;
1288 if ((pes_ext & 0x41) == 0x01 &&
1289 (r + 2) <= (pes->header + pes->pes_header_size)) {
1290 /* PES extension 2 */
1291 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1292 pes->extended_stream_id = r[1];
1293 }
1294 }
1295
1296 /* we got the full header. We parse it and get the payload */
1297 20942 pes->state = MPEGTS_PAYLOAD;
1298 20942 pes->data_index = 0;
1299
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20942 if (pes->stream_type == 0x12 && buf_size > 0) {
1300 int sl_header_bytes = read_sl_header(pes, &pes->sl, p,
1301 buf_size);
1302 pes->pes_header_size += sl_header_bytes;
1303 p += sl_header_bytes;
1304 buf_size -= sl_header_bytes;
1305 }
1306
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20942 if (pes->stream_type == 0x15 && buf_size >= 5) {
1307 /* skip metadata access unit header */
1308 pes->pes_header_size += 5;
1309 p += 5;
1310 buf_size -= 5;
1311 }
1312
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 if ( pes->ts->fix_teletext_pts
1313
1/2
✓ Branch 0 taken 20942 times.
✗ Branch 1 not taken.
20942 && ( pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT
1314
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 20688 times.
20942 || pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
1315 ) {
1316 254 AVProgram *p = NULL;
1317 254 int pcr_found = 0;
1318
2/2
✓ Branch 1 taken 254 times.
✓ Branch 2 taken 254 times.
508 while ((p = av_find_program_from_stream(pes->stream, p, pes->st->index))) {
1319
2/4
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 254 times.
✗ Branch 3 not taken.
254 if (p->pcr_pid != -1 && p->discard != AVDISCARD_ALL) {
1320 254 MpegTSFilter *f = pes->ts->pids[p->pcr_pid];
1321
1/2
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
254 if (f) {
1322 254 AVStream *st = NULL;
1323
1/2
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
254 if (f->type == MPEGTS_PES) {
1324 254 PESContext *pcrpes = f->u.pes_filter.opaque;
1325
1/2
✓ Branch 0 taken 254 times.
✗ Branch 1 not taken.
254 if (pcrpes)
1326 254 st = pcrpes->st;
1327 } else if (f->type == MPEGTS_PCR) {
1328 int i;
1329 for (i = 0; i < p->nb_stream_indexes; i++) {
1330 AVStream *pst = pes->stream->streams[p->stream_index[i]];
1331 if (pst->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1332 st = pst;
1333 }
1334 }
1335
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 254 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
254 if (f->last_pcr != -1 && !f->discard) {
1336 // teletext packets do not always have correct timestamps,
1337 // the standard says they should be handled after 40.6 ms at most,
1338 // and the pcr error to this packet should be no more than 100 ms.
1339 // TODO: we should interpolate the PCR, not just use the last one
1340 int64_t pcr = f->last_pcr / 300;
1341 pcr_found = 1;
1342 if (st) {
1343 const FFStream *const sti = ffstream(st);
1344 FFStream *const pes_sti = ffstream(pes->st);
1345
1346 pes_sti->pts_wrap_reference = sti->pts_wrap_reference;
1347 pes_sti->pts_wrap_behavior = sti->pts_wrap_behavior;
1348 }
1349 if (pes->dts == AV_NOPTS_VALUE || pes->dts < pcr) {
1350 pes->pts = pes->dts = pcr;
1351 } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT &&
1352 pes->dts > pcr + 3654 + 9000) {
1353 pes->pts = pes->dts = pcr + 3654 + 9000;
1354 } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
1355 pes->dts > pcr + 10*90000) { //10sec
1356 pes->pts = pes->dts = pcr + 3654 + 9000;
1357 }
1358 break;
1359 }
1360 }
1361 }
1362 }
1363
1364
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 254 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
254 if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT &&
1365 !pcr_found) {
1366 av_log(pes->stream, AV_LOG_VERBOSE,
1367 "Forcing DTS/PTS to be unset for a "
1368 "non-trustworthy PES packet for PID %d as "
1369 "PCR hasn't been received yet.\n",
1370 pes->pid);
1371 pes->dts = pes->pts = AV_NOPTS_VALUE;
1372 }
1373 }
1374 }
1375 20942 break;
1376 303386 case MPEGTS_PAYLOAD:
1377 do {
1378 303386 int max_packet_size = ts->max_packet_size;
1379
3/4
✓ Branch 0 taken 147325 times.
✓ Branch 1 taken 156061 times.
✓ Branch 2 taken 147325 times.
✗ Branch 3 not taken.
303386 if (pes->PES_packet_length && pes->PES_packet_length + PES_START_SIZE > pes->pes_header_size)
1380 147325 max_packet_size = pes->PES_packet_length + PES_START_SIZE - pes->pes_header_size;
1381
1382
2/2
✓ Branch 0 taken 282444 times.
✓ Branch 1 taken 20942 times.
303386 if (pes->data_index > 0 &&
1383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 282444 times.
282444 pes->data_index + buf_size > max_packet_size) {
1384 ret = new_pes_packet(pes, ts->pkt);
1385 if (ret < 0)
1386 return ret;
1387 pes->PES_packet_length = 0;
1388 max_packet_size = ts->max_packet_size;
1389 ts->stop_parse = 1;
1390
3/4
✓ Branch 0 taken 20942 times.
✓ Branch 1 taken 282444 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20942 times.
303386 } else if (pes->data_index == 0 &&
1391 buf_size > max_packet_size) {
1392 // pes packet size is < ts size packet and pes data is padded with 0xff
1393 // not sure if this is legal in ts but see issue #2392
1394 buf_size = max_packet_size;
1395 }
1396
1397
2/2
✓ Branch 0 taken 20942 times.
✓ Branch 1 taken 282444 times.
303386 if (!pes->buffer) {
1398 20942 pes->buffer = buffer_pool_get(ts, max_packet_size);
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20942 times.
20942 if (!pes->buffer)
1400 return AVERROR(ENOMEM);
1401 }
1402
1403 303386 memcpy(pes->buffer->data + pes->data_index, p, buf_size);
1404 303386 pes->data_index += buf_size;
1405 /* emit complete packets with known packet size
1406 * decreases demuxer delay for infrequent packets like subtitles from
1407 * a couple of seconds to milliseconds for properly muxed files. */
1408
4/4
✓ Branch 0 taken 300548 times.
✓ Branch 1 taken 2838 times.
✓ Branch 2 taken 147325 times.
✓ Branch 3 taken 153223 times.
303386 if (!ts->stop_parse && pes->PES_packet_length &&
1409
2/2
✓ Branch 0 taken 17872 times.
✓ Branch 1 taken 129453 times.
147325 pes->pes_header_size + pes->data_index == pes->PES_packet_length + PES_START_SIZE) {
1410 17872 ts->stop_parse = 1;
1411 17872 ret = new_pes_packet(pes, ts->pkt);
1412 17872 pes->state = MPEGTS_SKIP;
1413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17872 times.
17872 if (ret < 0)
1414 return ret;
1415 }
1416 } while (0);
1417 303386 buf_size = 0;
1418 303386 break;
1419 15768 case MPEGTS_SKIP:
1420 15768 buf_size = 0;
1421 15768 break;
1422 }
1423 }
1424
1425 319156 return 0;
1426 }
1427
1428 107 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
1429 {
1430 MpegTSFilter *tss;
1431 PESContext *pes;
1432
1433 /* if no pid found, then add a pid context */
1434 107 pes = av_mallocz(sizeof(PESContext));
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 if (!pes)
1436 return 0;
1437 107 pes->ts = ts;
1438 107 pes->stream = ts->stream;
1439 107 pes->pid = pid;
1440 107 pes->pcr_pid = pcr_pid;
1441 107 pes->state = MPEGTS_SKIP;
1442 107 pes->pts = AV_NOPTS_VALUE;
1443 107 pes->dts = AV_NOPTS_VALUE;
1444 107 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
1445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 if (!tss) {
1446 av_free(pes);
1447 return 0;
1448 }
1449 107 return pes;
1450 }
1451
1452 #define MAX_LEVEL 4
1453 typedef struct MP4DescrParseContext {
1454 AVFormatContext *s;
1455 FFIOContext pb;
1456 Mp4Descr *descr;
1457 Mp4Descr *active_descr;
1458 int descr_count;
1459 int max_descr_count;
1460 int level;
1461 int predefined_SLConfigDescriptor_seen;
1462 } MP4DescrParseContext;
1463
1464 static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s,
1465 const uint8_t *buf, unsigned size,
1466 Mp4Descr *descr, int max_descr_count)
1467 {
1468 if (size > (1 << 30))
1469 return AVERROR_INVALIDDATA;
1470
1471 ffio_init_context(&d->pb, (unsigned char *)buf, size,
1472 0, NULL, NULL, NULL, NULL);
1473
1474 d->s = s;
1475 d->level = 0;
1476 d->descr_count = 0;
1477 d->descr = descr;
1478 d->active_descr = NULL;
1479 d->max_descr_count = max_descr_count;
1480
1481 return 0;
1482 }
1483
1484 static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
1485 {
1486 int64_t new_off = avio_tell(pb);
1487 (*len) -= new_off - *off;
1488 *off = new_off;
1489 }
1490
1491 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1492 int target_tag);
1493
1494 static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
1495 {
1496 while (len > 0) {
1497 int ret = parse_mp4_descr(d, off, len, 0);
1498 if (ret < 0)
1499 return ret;
1500 update_offsets(&d->pb.pub, &off, &len);
1501 }
1502 return 0;
1503 }
1504
1505 static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1506 {
1507 AVIOContext *const pb = &d->pb.pub;
1508 avio_rb16(pb); // ID
1509 avio_r8(pb);
1510 avio_r8(pb);
1511 avio_r8(pb);
1512 avio_r8(pb);
1513 avio_r8(pb);
1514 update_offsets(pb, &off, &len);
1515 return parse_mp4_descr_arr(d, off, len);
1516 }
1517
1518 static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1519 {
1520 int id_flags;
1521 if (len < 2)
1522 return 0;
1523 id_flags = avio_rb16(&d->pb.pub);
1524 if (!(id_flags & 0x0020)) { // URL_Flag
1525 update_offsets(&d->pb.pub, &off, &len);
1526 return parse_mp4_descr_arr(d, off, len); // ES_Descriptor[]
1527 } else {
1528 return 0;
1529 }
1530 }
1531
1532 static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1533 {
1534 AVIOContext *const pb = &d->pb.pub;
1535 int es_id = 0;
1536 int ret = 0;
1537
1538 if (d->descr_count >= d->max_descr_count)
1539 return AVERROR_INVALIDDATA;
1540 ff_mp4_parse_es_descr(pb, &es_id);
1541 d->active_descr = d->descr + (d->descr_count++);
1542
1543 d->active_descr->es_id = es_id;
1544 update_offsets(pb, &off, &len);
1545 if ((ret = parse_mp4_descr(d, off, len, MP4DecConfigDescrTag)) < 0)
1546 return ret;
1547 update_offsets(pb, &off, &len);
1548 if (len > 0)
1549 ret = parse_mp4_descr(d, off, len, MP4SLDescrTag);
1550 d->active_descr = NULL;
1551 return ret;
1552 }
1553
1554 static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off,
1555 int len)
1556 {
1557 Mp4Descr *descr = d->active_descr;
1558 if (!descr)
1559 return AVERROR_INVALIDDATA;
1560 d->active_descr->dec_config_descr = av_malloc(len);
1561 if (!descr->dec_config_descr)
1562 return AVERROR(ENOMEM);
1563 descr->dec_config_descr_len = len;
1564 avio_read(&d->pb.pub, descr->dec_config_descr, len);
1565 return 0;
1566 }
1567
1568 static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1569 {
1570 Mp4Descr *descr = d->active_descr;
1571 AVIOContext *const pb = &d->pb.pub;
1572 int predefined;
1573 if (!descr)
1574 return AVERROR_INVALIDDATA;
1575
1576 #define R8_CHECK_CLIP_MAX(dst, maxv) do { \
1577 descr->sl.dst = avio_r8(pb); \
1578 if (descr->sl.dst > maxv) { \
1579 descr->sl.dst = maxv; \
1580 return AVERROR_INVALIDDATA; \
1581 } \
1582 } while (0)
1583
1584 predefined = avio_r8(pb);
1585 if (!predefined) {
1586 int lengths;
1587 int flags = avio_r8(pb);
1588 descr->sl.use_au_start = !!(flags & 0x80);
1589 descr->sl.use_au_end = !!(flags & 0x40);
1590 descr->sl.use_rand_acc_pt = !!(flags & 0x20);
1591 descr->sl.use_padding = !!(flags & 0x08);
1592 descr->sl.use_timestamps = !!(flags & 0x04);
1593 descr->sl.use_idle = !!(flags & 0x02);
1594 descr->sl.timestamp_res = avio_rb32(pb);
1595 avio_rb32(pb);
1596 R8_CHECK_CLIP_MAX(timestamp_len, 63);
1597 R8_CHECK_CLIP_MAX(ocr_len, 63);
1598 R8_CHECK_CLIP_MAX(au_len, 31);
1599 descr->sl.inst_bitrate_len = avio_r8(pb);
1600 lengths = avio_rb16(pb);
1601 descr->sl.degr_prior_len = lengths >> 12;
1602 descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
1603 descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
1604 } else if (!d->predefined_SLConfigDescriptor_seen){
1605 avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
1606 d->predefined_SLConfigDescriptor_seen = 1;
1607 }
1608 return 0;
1609 }
1610
1611 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1612 int target_tag)
1613 {
1614 int tag;
1615 AVIOContext *const pb = &d->pb.pub;
1616 int len1 = ff_mp4_read_descr(d->s, pb, &tag);
1617 int ret = 0;
1618
1619 update_offsets(pb, &off, &len);
1620 if (len < 0 || len1 > len || len1 <= 0) {
1621 av_log(d->s, AV_LOG_ERROR,
1622 "Tag %x length violation new length %d bytes remaining %d\n",
1623 tag, len1, len);
1624 return AVERROR_INVALIDDATA;
1625 }
1626
1627 if (d->level++ >= MAX_LEVEL) {
1628 av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
1629 ret = AVERROR_INVALIDDATA;
1630 goto done;
1631 }
1632
1633 if (target_tag && tag != target_tag) {
1634 av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag,
1635 target_tag);
1636 ret = AVERROR_INVALIDDATA;
1637 goto done;
1638 }
1639
1640 switch (tag) {
1641 case MP4IODescrTag:
1642 ret = parse_MP4IODescrTag(d, off, len1);
1643 break;
1644 case MP4ODescrTag:
1645 ret = parse_MP4ODescrTag(d, off, len1);
1646 break;
1647 case MP4ESDescrTag:
1648 ret = parse_MP4ESDescrTag(d, off, len1);
1649 break;
1650 case MP4DecConfigDescrTag:
1651 ret = parse_MP4DecConfigDescrTag(d, off, len1);
1652 break;
1653 case MP4SLDescrTag:
1654 ret = parse_MP4SLDescrTag(d, off, len1);
1655 break;
1656 }
1657
1658
1659 done:
1660 d->level--;
1661 avio_seek(pb, off + len1, SEEK_SET);
1662 return ret;
1663 }
1664
1665 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
1666 Mp4Descr *descr, int *descr_count, int max_descr_count)
1667 {
1668 MP4DescrParseContext d;
1669 int ret;
1670
1671 ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1672 if (ret < 0)
1673 return ret;
1674
1675 ret = parse_mp4_descr(&d, avio_tell(&d.pb.pub), size, MP4IODescrTag);
1676
1677 *descr_count = d.descr_count;
1678 return ret;
1679 }
1680
1681 static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
1682 Mp4Descr *descr, int *descr_count, int max_descr_count)
1683 {
1684 MP4DescrParseContext d;
1685 int ret;
1686
1687 ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1688 if (ret < 0)
1689 return ret;
1690
1691 ret = parse_mp4_descr_arr(&d, avio_tell(&d.pb.pub), size);
1692
1693 *descr_count = d.descr_count;
1694 return ret;
1695 }
1696
1697 static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section,
1698 int section_len)
1699 {
1700 MpegTSContext *ts = filter->u.section_filter.opaque;
1701 MpegTSSectionFilter *tssf = &filter->u.section_filter;
1702 SectionHeader h;
1703 const uint8_t *p, *p_end;
1704 int mp4_descr_count = 0;
1705 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
1706 int i, pid;
1707 AVFormatContext *s = ts->stream;
1708
1709 p_end = section + section_len - 4;
1710 p = section;
1711 if (parse_section_header(&h, &p, p_end) < 0)
1712 return;
1713 if (h.tid != M4OD_TID)
1714 return;
1715 if (skip_identical(&h, tssf))
1716 return;
1717
1718 mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1719 MAX_MP4_DESCR_COUNT);
1720
1721 for (pid = 0; pid < NB_PID_MAX; pid++) {
1722 if (!ts->pids[pid])
1723 continue;
1724 for (i = 0; i < mp4_descr_count; i++) {
1725 PESContext *pes;
1726 AVStream *st;
1727 FFStream *sti;
1728 FFIOContext pb;
1729 if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
1730 continue;
1731 if (ts->pids[pid]->type != MPEGTS_PES) {
1732 av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
1733 continue;
1734 }
1735 pes = ts->pids[pid]->u.pes_filter.opaque;
1736 st = pes->st;
1737 if (!st)
1738 continue;
1739 sti = ffstream(st);
1740
1741 pes->sl = mp4_descr[i].sl;
1742
1743 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1744 mp4_descr[i].dec_config_descr_len, 0,
1745 NULL, NULL, NULL, NULL);
1746 ff_mp4_read_dec_config_descr(s, st, &pb.pub);
1747 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1748 st->codecpar->extradata_size > 0)
1749 sti->need_parsing = 0;
1750 if (st->codecpar->codec_id == AV_CODEC_ID_H264 &&
1751 st->codecpar->extradata_size > 0)
1752 sti->need_parsing = 0;
1753
1754 st->codecpar->codec_type = avcodec_get_type(st->codecpar->codec_id);
1755 sti->need_context_update = 1;
1756 }
1757 }
1758 for (i = 0; i < mp4_descr_count; i++)
1759 av_free(mp4_descr[i].dec_config_descr);
1760 }
1761
1762 static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section,
1763 int section_len)
1764 {
1765 AVProgram *prg = NULL;
1766 MpegTSContext *ts = filter->u.section_filter.opaque;
1767
1768 int idx = ff_find_stream_index(ts->stream, filter->pid);
1769 if (idx < 0)
1770 return;
1771
1772 /**
1773 * In case we receive an SCTE-35 packet before mpegts context is fully
1774 * initialized.
1775 */
1776 if (!ts->pkt)
1777 return;
1778
1779 new_data_packet(section, section_len, ts->pkt);
1780 ts->pkt->stream_index = idx;
1781 prg = av_find_program_from_stream(ts->stream, NULL, idx);
1782 if (prg && prg->pcr_pid != -1 && prg->discard != AVDISCARD_ALL) {
1783 MpegTSFilter *f = ts->pids[prg->pcr_pid];
1784 if (f && f->last_pcr != -1)
1785 ts->pkt->pts = ts->pkt->dts = f->last_pcr/300;
1786 }
1787 ts->stop_parse = 1;
1788
1789 }
1790
1791 static const uint8_t opus_coupled_stream_cnt[9] = {
1792 1, 0, 1, 1, 2, 2, 2, 3, 3
1793 };
1794
1795 static const uint8_t opus_stream_cnt[9] = {
1796 1, 1, 1, 2, 2, 3, 4, 4, 5,
1797 };
1798
1799 static const uint8_t opus_channel_map[8][8] = {
1800 { 0 },
1801 { 0,1 },
1802 { 0,2,1 },
1803 { 0,1,2,3 },
1804 { 0,4,1,2,3 },
1805 { 0,4,1,2,3,5 },
1806 { 0,4,1,2,3,5,6 },
1807 { 0,6,1,2,3,4,5,7 },
1808 };
1809
1810 797 int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
1811 const uint8_t **pp, const uint8_t *desc_list_end,
1812 Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
1813 MpegTSContext *ts)
1814 {
1815 797 FFStream *const sti = ffstream(st);
1816 const uint8_t *desc_end;
1817 int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code;
1818 char language[252];
1819 int i;
1820
1821 797 desc_tag = get8(pp, desc_list_end);
1822
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 302 times.
797 if (desc_tag < 0)
1823 495 return AVERROR_INVALIDDATA;
1824 302 desc_len = get8(pp, desc_list_end);
1825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (desc_len < 0)
1826 return AVERROR_INVALIDDATA;
1827 302 desc_end = *pp + desc_len;
1828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (desc_end > desc_list_end)
1829 return AVERROR_INVALIDDATA;
1830
1831 302 av_log(fc, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
1832
1833
6/6
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 273 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 17 times.
302 if ((st->codecpar->codec_id == AV_CODEC_ID_NONE || sti->request_probe > 0) &&
1834 stream_type == STREAM_TYPE_PRIVATE_DATA)
1835 12 mpegts_find_stream_type(st, desc_tag, DESC_types);
1836
1837
7/15
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 204 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 31 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 40 times.
302 switch (desc_tag) {
1838 case VIDEO_STREAM_DESCRIPTOR:
1839 if (get8(pp, desc_end) & 0x1) {
1840 st->disposition |= AV_DISPOSITION_STILL_IMAGE;
1841 }
1842 break;
1843 case SL_DESCRIPTOR:
1844 desc_es_id = get16(pp, desc_end);
1845 if (desc_es_id < 0)
1846 break;
1847 if (ts && ts->pids[pid])
1848 ts->pids[pid]->es_id = desc_es_id;
1849 for (i = 0; i < mp4_descr_count; i++)
1850 if (mp4_descr[i].dec_config_descr_len &&
1851 mp4_descr[i].es_id == desc_es_id) {
1852 FFIOContext pb;
1853 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1854 mp4_descr[i].dec_config_descr_len, 0,
1855 NULL, NULL, NULL, NULL);
1856 ff_mp4_read_dec_config_descr(fc, st, &pb.pub);
1857 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1858 st->codecpar->extradata_size > 0) {
1859 sti->need_parsing = 0;
1860 sti->need_context_update = 1;
1861 }
1862 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
1863 mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
1864 }
1865 break;
1866 case FMC_DESCRIPTOR:
1867 if (get16(pp, desc_end) < 0)
1868 break;
1869 if (mp4_descr_count > 0 &&
1870 (st->codecpar->codec_id == AV_CODEC_ID_AAC_LATM ||
1871 (sti->request_probe == 0 && st->codecpar->codec_id == AV_CODEC_ID_NONE) ||
1872 sti->request_probe > 0) &&
1873 mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
1874 FFIOContext pb;
1875 ffio_init_context(&pb, mp4_descr->dec_config_descr,
1876 mp4_descr->dec_config_descr_len, 0,
1877 NULL, NULL, NULL, NULL);
1878 ff_mp4_read_dec_config_descr(fc, st, &pb.pub);
1879 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1880 st->codecpar->extradata_size > 0) {
1881 sti->request_probe = sti->need_parsing = 0;
1882 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
1883 sti->need_context_update = 1;
1884 }
1885 }
1886 break;
1887 case 0x56: /* DVB teletext descriptor */
1888 {
1889 uint8_t *extradata = NULL;
1890 int language_count = desc_len / 5, ret;
1891
1892 if (desc_len > 0 && desc_len % 5 != 0)
1893 return AVERROR_INVALIDDATA;
1894
1895 if (language_count > 0) {
1896 /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
1897 av_assert0(language_count <= sizeof(language) / 4);
1898
1899 if (st->codecpar->extradata == NULL) {
1900 ret = ff_alloc_extradata(st->codecpar, language_count * 2);
1901 if (ret < 0)
1902 return ret;
1903 }
1904
1905 if (st->codecpar->extradata_size < language_count * 2)
1906 return AVERROR_INVALIDDATA;
1907
1908 extradata = st->codecpar->extradata;
1909
1910 for (i = 0; i < language_count; i++) {
1911 language[i * 4 + 0] = get8(pp, desc_end);
1912 language[i * 4 + 1] = get8(pp, desc_end);
1913 language[i * 4 + 2] = get8(pp, desc_end);
1914 language[i * 4 + 3] = ',';
1915
1916 memcpy(extradata, *pp, 2);
1917 extradata += 2;
1918
1919 *pp += 2;
1920 }
1921
1922 language[i * 4 - 1] = 0;
1923 av_dict_set(&st->metadata, "language", language, 0);
1924 sti->need_context_update = 1;
1925 }
1926 }
1927 break;
1928 9 case 0x59: /* subtitling descriptor */
1929 {
1930 /* 8 bytes per DVB subtitle substream data:
1931 * ISO_639_language_code (3 bytes),
1932 * subtitling_type (1 byte),
1933 * composition_page_id (2 bytes),
1934 * ancillary_page_id (2 bytes) */
1935 9 int language_count = desc_len / 8, ret;
1936
1937
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if (desc_len > 0 && desc_len % 8 != 0)
1938 return AVERROR_INVALIDDATA;
1939
1940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (language_count > 1) {
1941 avpriv_request_sample(fc, "DVB subtitles with multiple languages");
1942 }
1943
1944
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (language_count > 0) {
1945 uint8_t *extradata;
1946
1947 /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
1948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 av_assert0(language_count <= sizeof(language) / 4);
1949
1950
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (st->codecpar->extradata == NULL) {
1951 3 ret = ff_alloc_extradata(st->codecpar, language_count * 5);
1952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
1953 return ret;
1954 }
1955
1956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (st->codecpar->extradata_size < language_count * 5)
1957 return AVERROR_INVALIDDATA;
1958
1959 9 extradata = st->codecpar->extradata;
1960
1961
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (i = 0; i < language_count; i++) {
1962 9 language[i * 4 + 0] = get8(pp, desc_end);
1963 9 language[i * 4 + 1] = get8(pp, desc_end);
1964 9 language[i * 4 + 2] = get8(pp, desc_end);
1965 9 language[i * 4 + 3] = ',';
1966
1967 /* hearing impaired subtitles detection using subtitling_type */
1968
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 switch (*pp[0]) {
1969 case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1970 case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1971 case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1972 case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1973 case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1974 case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1975 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
1976 break;
1977 }
1978
1979 9 extradata[4] = get8(pp, desc_end); /* subtitling_type */
1980 9 memcpy(extradata, *pp, 4); /* composition_page_id and ancillary_page_id */
1981 9 extradata += 5;
1982
1983 9 *pp += 4;
1984 }
1985
1986 9 language[i * 4 - 1] = 0;
1987 9 av_dict_set(&st->metadata, "language", language, 0);
1988 9 sti->need_context_update = 1;
1989 }
1990 }
1991 9 break;
1992 204 case ISO_639_LANGUAGE_DESCRIPTOR:
1993
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 204 times.
402 for (i = 0; i + 4 <= desc_len; i += 4) {
1994 198 language[i + 0] = get8(pp, desc_end);
1995 198 language[i + 1] = get8(pp, desc_end);
1996 198 language[i + 2] = get8(pp, desc_end);
1997 198 language[i + 3] = ',';
1998 198 switch (get8(pp, desc_end)) {
1999 case 0x01:
2000 st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS;
2001 break;
2002 case 0x02:
2003 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
2004 break;
2005 44 case 0x03:
2006 44 st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
2007 44 st->disposition |= AV_DISPOSITION_DESCRIPTIONS;
2008 44 break;
2009 }
2010 }
2011
4/4
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 194 times.
✓ Branch 3 taken 4 times.
204 if (i && language[0]) {
2012 194 language[i - 1] = 0;
2013 /* don't overwrite language, as it may already have been set by
2014 * another, more specific descriptor (e.g. supplementary audio) */
2015 194 av_dict_set(&st->metadata, "language", language, AV_DICT_DONT_OVERWRITE);
2016 }
2017 204 break;
2018 8 case REGISTRATION_DESCRIPTOR:
2019 8 st->codecpar->codec_tag = bytestream_get_le32(pp);
2020 8 av_log(fc, AV_LOG_TRACE, "reg_desc=%.4s\n", (char *)&st->codecpar->codec_tag);
2021
3/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 5 times.
8 if (st->codecpar->codec_id == AV_CODEC_ID_NONE || sti->request_probe > 0) {
2022 3 mpegts_find_stream_type(st, st->codecpar->codec_tag, REGD_types);
2023
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (st->codecpar->codec_tag == MKTAG('B', 'S', 'S', 'D'))
2024 2 sti->request_probe = 50;
2025 }
2026 8 break;
2027 31 case 0x52: /* stream identifier descriptor */
2028 31 sti->stream_identifier = 1 + get8(pp, desc_end);
2029 31 break;
2030 case METADATA_DESCRIPTOR:
2031 if (get16(pp, desc_end) == 0xFFFF)
2032 *pp += 4;
2033 if (get8(pp, desc_end) == 0xFF) {
2034 st->codecpar->codec_tag = bytestream_get_le32(pp);
2035 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
2036 mpegts_find_stream_type(st, st->codecpar->codec_tag, METADATA_types);
2037 }
2038 break;
2039 4 case 0x7f: /* DVB extension descriptor */
2040 4 ext_desc_tag = get8(pp, desc_end);
2041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ext_desc_tag < 0)
2042 return AVERROR_INVALIDDATA;
2043
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
4 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS &&
2044 ext_desc_tag == 0x80) { /* User defined (provisional Opus) */
2045
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (!st->codecpar->extradata) {
2046 1 st->codecpar->extradata = av_mallocz(sizeof(opus_default_extradata) +
2047 AV_INPUT_BUFFER_PADDING_SIZE);
2048
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st->codecpar->extradata)
2049 return AVERROR(ENOMEM);
2050
2051 1 st->codecpar->extradata_size = sizeof(opus_default_extradata);
2052 1 memcpy(st->codecpar->extradata, opus_default_extradata, sizeof(opus_default_extradata));
2053
2054 1 channel_config_code = get8(pp, desc_end);
2055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (channel_config_code < 0)
2056 return AVERROR_INVALIDDATA;
2057
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (channel_config_code <= 0x8) {
2058
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 st->codecpar->extradata[9] = channels = channel_config_code ? channel_config_code : 2;
2059 1 AV_WL32(&st->codecpar->extradata[12], 48000);
2060
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 st->codecpar->extradata[18] = channel_config_code ? (channels > 2) : /* Dual Mono */ 255;
2061 1 st->codecpar->extradata[19] = opus_stream_cnt[channel_config_code];
2062 1 st->codecpar->extradata[20] = opus_coupled_stream_cnt[channel_config_code];
2063 1 memcpy(&st->codecpar->extradata[21], opus_channel_map[channels - 1], channels);
2064
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 st->codecpar->extradata_size = st->codecpar->extradata[18] ? 21 + channels : 19;
2065 } else {
2066 avpriv_request_sample(fc, "Opus in MPEG-TS - channel_config_code > 0x8");
2067 }