blob: 1f874623826529dbb88df8e16fde06802d015408
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 "libavutil/buffer.h" |
23 | #include "libavutil/crc.h" |
24 | #include "libavutil/internal.h" |
25 | #include "libavutil/intreadwrite.h" |
26 | #include "libavutil/log.h" |
27 | #include "libavutil/dict.h" |
28 | #include "libavutil/mathematics.h" |
29 | #include "libavutil/opt.h" |
30 | #include "libavutil/avassert.h" |
31 | #include "libavcodec/bytestream.h" |
32 | #include "libavcodec/get_bits.h" |
33 | #include "libavcodec/opus.h" |
34 | #include "avformat.h" |
35 | #include "mpegts.h" |
36 | #include "internal.h" |
37 | #include "avio_internal.h" |
38 | #include "mpeg.h" |
39 | #include "isom.h" |
40 | |
41 | /* maximum size in which we look for synchronization if |
42 | * synchronization is lost */ |
43 | #define MAX_RESYNC_SIZE 65536 |
44 | |
45 | #define MAX_PES_PAYLOAD 200 * 1024 |
46 | |
47 | #define MAX_MP4_DESCR_COUNT 16 |
48 | |
49 | #define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \ |
50 | do { \ |
51 | if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \ |
52 | (modulus) = (dividend) % (divisor); \ |
53 | (prev_dividend) = (dividend); \ |
54 | } while (0) |
55 | |
56 | enum MpegTSFilterType { |
57 | MPEGTS_PES, |
58 | MPEGTS_SECTION, |
59 | MPEGTS_PCR, |
60 | }; |
61 | |
62 | typedef struct MpegTSFilter MpegTSFilter; |
63 | |
64 | typedef int PESCallback (MpegTSFilter *f, const uint8_t *buf, int len, |
65 | int is_start, int64_t pos); |
66 | |
67 | typedef struct MpegTSPESFilter { |
68 | PESCallback *pes_cb; |
69 | void *opaque; |
70 | } MpegTSPESFilter; |
71 | |
72 | typedef void SectionCallback (MpegTSFilter *f, const uint8_t *buf, int len); |
73 | |
74 | typedef void SetServiceCallback (void *opaque, int ret); |
75 | |
76 | typedef struct MpegTSSectionFilter { |
77 | int section_index; |
78 | int section_h_size; |
79 | int last_ver; |
80 | unsigned crc; |
81 | unsigned last_crc; |
82 | uint8_t *section_buf; |
83 | unsigned int check_crc : 1; |
84 | unsigned int end_of_section_reached : 1; |
85 | SectionCallback *section_cb; |
86 | void *opaque; |
87 | } MpegTSSectionFilter; |
88 | |
89 | struct MpegTSFilter { |
90 | int pid; |
91 | int es_id; |
92 | int last_cc; /* last cc code (-1 if first packet) */ |
93 | int64_t last_pcr; |
94 | enum MpegTSFilterType type; |
95 | union { |
96 | MpegTSPESFilter pes_filter; |
97 | MpegTSSectionFilter section_filter; |
98 | } u; |
99 | }; |
100 | |
101 | #define MAX_PIDS_PER_PROGRAM 64 |
102 | struct Program { |
103 | unsigned int id; // program id/service id |
104 | unsigned int nb_pids; |
105 | unsigned int pids[MAX_PIDS_PER_PROGRAM]; |
106 | |
107 | /** have we found pmt for this program */ |
108 | int pmt_found; |
109 | }; |
110 | |
111 | struct MpegTSContext { |
112 | const AVClass *class; |
113 | /* user data */ |
114 | AVFormatContext *stream; |
115 | /** raw packet size, including FEC if present */ |
116 | int raw_packet_size; |
117 | |
118 | int size_stat[3]; |
119 | int size_stat_count; |
120 | #define SIZE_STAT_THRESHOLD 10 |
121 | |
122 | int64_t pos47_full; |
123 | |
124 | /** if true, all pids are analyzed to find streams */ |
125 | int auto_guess; |
126 | |
127 | /** compute exact PCR for each transport stream packet */ |
128 | int mpeg2ts_compute_pcr; |
129 | |
130 | /** fix dvb teletext pts */ |
131 | int fix_teletext_pts; |
132 | |
133 | int64_t cur_pcr; /**< used to estimate the exact PCR */ |
134 | int pcr_incr; /**< used to estimate the exact PCR */ |
135 | |
136 | /* data needed to handle file based ts */ |
137 | /** stop parsing loop */ |
138 | int stop_parse; |
139 | /** packet containing Audio/Video data */ |
140 | AVPacket *pkt; |
141 | /** to detect seek */ |
142 | int64_t last_pos; |
143 | |
144 | int skip_changes; |
145 | int skip_clear; |
146 | |
147 | int scan_all_pmts; |
148 | |
149 | int resync_size; |
150 | |
151 | /******************************************/ |
152 | /* private mpegts data */ |
153 | /* scan context */ |
154 | /** structure to keep track of Program->pids mapping */ |
155 | unsigned int nb_prg; |
156 | struct Program *prg; |
157 | |
158 | int8_t crc_validity[NB_PID_MAX]; |
159 | /** filters for various streams specified by PMT + for the PAT and PMT */ |
160 | MpegTSFilter *pids[NB_PID_MAX]; |
161 | int current_pid; |
162 | }; |
163 | |
164 | #define MPEGTS_OPTIONS \ |
165 | { "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 } |
166 | |
167 | static const AVOption options[] = { |
168 | MPEGTS_OPTIONS, |
169 | {"fix_teletext_pts", "try to fix pts values of dvb teletext streams", offsetof(MpegTSContext, fix_teletext_pts), AV_OPT_TYPE_BOOL, |
170 | {.i64 = 1}, 0, 1, AV_OPT_FLAG_DECODING_PARAM }, |
171 | {"ts_packetsize", "output option carrying the raw packet size", offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT, |
172 | {.i64 = 0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY }, |
173 | {"scan_all_pmts", "scan and combine all PMTs", offsetof(MpegTSContext, scan_all_pmts), AV_OPT_TYPE_BOOL, |
174 | { .i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM }, |
175 | {"skip_changes", "skip changing / adding streams / programs", offsetof(MpegTSContext, skip_changes), AV_OPT_TYPE_BOOL, |
176 | {.i64 = 0}, 0, 1, 0 }, |
177 | {"skip_clear", "skip clearing programs", offsetof(MpegTSContext, skip_clear), AV_OPT_TYPE_BOOL, |
178 | {.i64 = 0}, 0, 1, 0 }, |
179 | { NULL }, |
180 | }; |
181 | |
182 | static const AVClass mpegts_class = { |
183 | .class_name = "mpegts demuxer", |
184 | .item_name = av_default_item_name, |
185 | .option = options, |
186 | .version = LIBAVUTIL_VERSION_INT, |
187 | }; |
188 | |
189 | static const AVOption raw_options[] = { |
190 | MPEGTS_OPTIONS, |
191 | { "compute_pcr", "compute exact PCR for each transport stream packet", |
192 | offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_BOOL, |
193 | { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM }, |
194 | { "ts_packetsize", "output option carrying the raw packet size", |
195 | offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT, |
196 | { .i64 = 0 }, 0, 0, |
197 | AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY }, |
198 | { NULL }, |
199 | }; |
200 | |
201 | static const AVClass mpegtsraw_class = { |
202 | .class_name = "mpegtsraw demuxer", |
203 | .item_name = av_default_item_name, |
204 | .option = raw_options, |
205 | .version = LIBAVUTIL_VERSION_INT, |
206 | }; |
207 | |
208 | /* TS stream handling */ |
209 | |
210 | enum MpegTSState { |
211 | MPEGTS_HEADER = 0, |
212 | MPEGTS_PESHEADER, |
213 | MPEGTS_PESHEADER_FILL, |
214 | MPEGTS_PAYLOAD, |
215 | MPEGTS_SKIP, |
216 | }; |
217 | |
218 | /* enough for PES header + length */ |
219 | #define PES_START_SIZE 6 |
220 | #define PES_HEADER_SIZE 9 |
221 | #define MAX_PES_HEADER_SIZE (9 + 255) |
222 | |
223 | typedef struct PESContext { |
224 | int pid; |
225 | int pcr_pid; /**< if -1 then all packets containing PCR are considered */ |
226 | int stream_type; |
227 | MpegTSContext *ts; |
228 | AVFormatContext *stream; |
229 | AVStream *st; |
230 | AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */ |
231 | enum MpegTSState state; |
232 | /* used to get the format */ |
233 | int data_index; |
234 | int flags; /**< copied to the AVPacket flags */ |
235 | int total_size; |
236 | int pes_header_size; |
237 | int extended_stream_id; |
238 | uint8_t stream_id; |
239 | int64_t pts, dts; |
240 | int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */ |
241 | uint8_t header[MAX_PES_HEADER_SIZE]; |
242 | AVBufferRef *buffer; |
243 | SLConfigDescr sl; |
244 | } PESContext; |
245 | |
246 | extern AVInputFormat ff_mpegts_demuxer; |
247 | |
248 | static struct Program * get_program(MpegTSContext *ts, unsigned int programid) |
249 | { |
250 | int i; |
251 | for (i = 0; i < ts->nb_prg; i++) { |
252 | if (ts->prg[i].id == programid) { |
253 | return &ts->prg[i]; |
254 | } |
255 | } |
256 | return NULL; |
257 | } |
258 | |
259 | static void clear_avprogram(MpegTSContext *ts, unsigned int programid) |
260 | { |
261 | AVProgram *prg = NULL; |
262 | int i; |
263 | |
264 | for (i = 0; i < ts->stream->nb_programs; i++) |
265 | if (ts->stream->programs[i]->id == programid) { |
266 | prg = ts->stream->programs[i]; |
267 | break; |
268 | } |
269 | if (!prg) |
270 | return; |
271 | prg->nb_stream_indexes = 0; |
272 | } |
273 | |
274 | static void clear_program(MpegTSContext *ts, unsigned int programid) |
275 | { |
276 | int i; |
277 | |
278 | clear_avprogram(ts, programid); |
279 | for (i = 0; i < ts->nb_prg; i++) |
280 | if (ts->prg[i].id == programid) { |
281 | ts->prg[i].nb_pids = 0; |
282 | ts->prg[i].pmt_found = 0; |
283 | } |
284 | } |
285 | |
286 | static void clear_programs(MpegTSContext *ts) |
287 | { |
288 | av_freep(&ts->prg); |
289 | ts->nb_prg = 0; |
290 | } |
291 | |
292 | static void add_pat_entry(MpegTSContext *ts, unsigned int programid) |
293 | { |
294 | struct Program *p; |
295 | if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) { |
296 | ts->nb_prg = 0; |
297 | return; |
298 | } |
299 | p = &ts->prg[ts->nb_prg]; |
300 | p->id = programid; |
301 | p->nb_pids = 0; |
302 | p->pmt_found = 0; |
303 | ts->nb_prg++; |
304 | } |
305 | |
306 | static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, |
307 | unsigned int pid) |
308 | { |
309 | struct Program *p = get_program(ts, programid); |
310 | int i; |
311 | if (!p) |
312 | return; |
313 | |
314 | if (p->nb_pids >= MAX_PIDS_PER_PROGRAM) |
315 | return; |
316 | |
317 | for (i = 0; i < p->nb_pids; i++) |
318 | if (p->pids[i] == pid) |
319 | return; |
320 | |
321 | p->pids[p->nb_pids++] = pid; |
322 | } |
323 | |
324 | static void set_pmt_found(MpegTSContext *ts, unsigned int programid) |
325 | { |
326 | struct Program *p = get_program(ts, programid); |
327 | if (!p) |
328 | return; |
329 | |
330 | p->pmt_found = 1; |
331 | } |
332 | |
333 | static void set_pcr_pid(AVFormatContext *s, unsigned int programid, unsigned int pid) |
334 | { |
335 | int i; |
336 | for (i = 0; i < s->nb_programs; i++) { |
337 | if (s->programs[i]->id == programid) { |
338 | s->programs[i]->pcr_pid = pid; |
339 | break; |
340 | } |
341 | } |
342 | } |
343 | |
344 | /** |
345 | * @brief discard_pid() decides if the pid is to be discarded according |
346 | * to caller's programs selection |
347 | * @param ts : - TS context |
348 | * @param pid : - pid |
349 | * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL |
350 | * 0 otherwise |
351 | */ |
352 | static int discard_pid(MpegTSContext *ts, unsigned int pid) |
353 | { |
354 | int i, j, k; |
355 | int used = 0, discarded = 0; |
356 | struct Program *p; |
357 | |
358 | /* If none of the programs have .discard=AVDISCARD_ALL then there's |
359 | * no way we have to discard this packet */ |
360 | for (k = 0; k < ts->stream->nb_programs; k++) |
361 | if (ts->stream->programs[k]->discard == AVDISCARD_ALL) |
362 | break; |
363 | if (k == ts->stream->nb_programs) |
364 | return 0; |
365 | |
366 | for (i = 0; i < ts->nb_prg; i++) { |
367 | p = &ts->prg[i]; |
368 | for (j = 0; j < p->nb_pids; j++) { |
369 | if (p->pids[j] != pid) |
370 | continue; |
371 | // is program with id p->id set to be discarded? |
372 | for (k = 0; k < ts->stream->nb_programs; k++) { |
373 | if (ts->stream->programs[k]->id == p->id) { |
374 | if (ts->stream->programs[k]->discard == AVDISCARD_ALL) |
375 | discarded++; |
376 | else |
377 | used++; |
378 | } |
379 | } |
380 | } |
381 | } |
382 | |
383 | return !used && discarded; |
384 | } |
385 | |
386 | /** |
387 | * Assemble PES packets out of TS packets, and then call the "section_cb" |
388 | * function when they are complete. |
389 | */ |
390 | static void write_section_data(MpegTSContext *ts, MpegTSFilter *tss1, |
391 | const uint8_t *buf, int buf_size, int is_start) |
392 | { |
393 | MpegTSSectionFilter *tss = &tss1->u.section_filter; |
394 | int len; |
395 | |
396 | if (is_start) { |
397 | memcpy(tss->section_buf, buf, buf_size); |
398 | tss->section_index = buf_size; |
399 | tss->section_h_size = -1; |
400 | tss->end_of_section_reached = 0; |
401 | } else { |
402 | if (tss->end_of_section_reached) |
403 | return; |
404 | len = 4096 - tss->section_index; |
405 | if (buf_size < len) |
406 | len = buf_size; |
407 | memcpy(tss->section_buf + tss->section_index, buf, len); |
408 | tss->section_index += len; |
409 | } |
410 | |
411 | /* compute section length if possible */ |
412 | if (tss->section_h_size == -1 && tss->section_index >= 3) { |
413 | len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3; |
414 | if (len > 4096) |
415 | return; |
416 | tss->section_h_size = len; |
417 | } |
418 | |
419 | if (tss->section_h_size != -1 && |
420 | tss->section_index >= tss->section_h_size) { |
421 | int crc_valid = 1; |
422 | tss->end_of_section_reached = 1; |
423 | |
424 | if (tss->check_crc) { |
425 | crc_valid = !av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, tss->section_buf, tss->section_h_size); |
426 | if (tss->section_h_size >= 4) |
427 | tss->crc = AV_RB32(tss->section_buf + tss->section_h_size - 4); |
428 | |
429 | if (crc_valid) { |
430 | ts->crc_validity[ tss1->pid ] = 100; |
431 | }else if (ts->crc_validity[ tss1->pid ] > -10) { |
432 | ts->crc_validity[ tss1->pid ]--; |
433 | }else |
434 | crc_valid = 2; |
435 | } |
436 | if (crc_valid) { |
437 | tss->section_cb(tss1, tss->section_buf, tss->section_h_size); |
438 | if (crc_valid != 1) |
439 | tss->last_ver = -1; |
440 | } |
441 | } |
442 | } |
443 | |
444 | static MpegTSFilter *mpegts_open_filter(MpegTSContext *ts, unsigned int pid, |
445 | enum MpegTSFilterType type) |
446 | { |
447 | MpegTSFilter *filter; |
448 | |
449 | av_log(ts->stream, AV_LOG_TRACE, "Filter: pid=0x%x type=%d\n", pid, type); |
450 | |
451 | if (pid >= NB_PID_MAX || ts->pids[pid]) |
452 | return NULL; |
453 | filter = av_mallocz(sizeof(MpegTSFilter)); |
454 | if (!filter) |
455 | return NULL; |
456 | ts->pids[pid] = filter; |
457 | |
458 | filter->type = type; |
459 | filter->pid = pid; |
460 | filter->es_id = -1; |
461 | filter->last_cc = -1; |
462 | filter->last_pcr= -1; |
463 | |
464 | return filter; |
465 | } |
466 | |
467 | static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, |
468 | unsigned int pid, |
469 | SectionCallback *section_cb, |
470 | void *opaque, |
471 | int check_crc) |
472 | { |
473 | MpegTSFilter *filter; |
474 | MpegTSSectionFilter *sec; |
475 | |
476 | if (!(filter = mpegts_open_filter(ts, pid, MPEGTS_SECTION))) |
477 | return NULL; |
478 | sec = &filter->u.section_filter; |
479 | sec->section_cb = section_cb; |
480 | sec->opaque = opaque; |
481 | sec->section_buf = av_malloc(MAX_SECTION_SIZE); |
482 | sec->check_crc = check_crc; |
483 | sec->last_ver = -1; |
484 | |
485 | if (!sec->section_buf) { |
486 | av_free(filter); |
487 | return NULL; |
488 | } |
489 | return filter; |
490 | } |
491 | |
492 | static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid, |
493 | PESCallback *pes_cb, |
494 | void *opaque) |
495 | { |
496 | MpegTSFilter *filter; |
497 | MpegTSPESFilter *pes; |
498 | |
499 | if (!(filter = mpegts_open_filter(ts, pid, MPEGTS_PES))) |
500 | return NULL; |
501 | |
502 | pes = &filter->u.pes_filter; |
503 | pes->pes_cb = pes_cb; |
504 | pes->opaque = opaque; |
505 | return filter; |
506 | } |
507 | |
508 | static MpegTSFilter *mpegts_open_pcr_filter(MpegTSContext *ts, unsigned int pid) |
509 | { |
510 | return mpegts_open_filter(ts, pid, MPEGTS_PCR); |
511 | } |
512 | |
513 | static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter) |
514 | { |
515 | int pid; |
516 | |
517 | pid = filter->pid; |
518 | if (filter->type == MPEGTS_SECTION) |
519 | av_freep(&filter->u.section_filter.section_buf); |
520 | else if (filter->type == MPEGTS_PES) { |
521 | PESContext *pes = filter->u.pes_filter.opaque; |
522 | av_buffer_unref(&pes->buffer); |
523 | /* referenced private data will be freed later in |
524 | * avformat_close_input */ |
525 | if (!((PESContext *)filter->u.pes_filter.opaque)->st) { |
526 | av_freep(&filter->u.pes_filter.opaque); |
527 | } |
528 | } |
529 | |
530 | av_free(filter); |
531 | ts->pids[pid] = NULL; |
532 | } |
533 | |
534 | static int analyze(const uint8_t *buf, int size, int packet_size, |
535 | int probe) |
536 | { |
537 | int stat[TS_MAX_PACKET_SIZE]; |
538 | int stat_all = 0; |
539 | int i; |
540 | int best_score = 0; |
541 | |
542 | memset(stat, 0, packet_size * sizeof(*stat)); |
543 | |
544 | for (i = 0; i < size - 3; i++) { |
545 | if (buf[i] == 0x47) { |
546 | int pid = AV_RB16(buf+1) & 0x1FFF; |
547 | int asc = buf[i + 3] & 0x30; |
548 | if (!probe || pid == 0x1FFF || asc) { |
549 | int x = i % packet_size; |
550 | stat[x]++; |
551 | stat_all++; |
552 | if (stat[x] > best_score) { |
553 | best_score = stat[x]; |
554 | } |
555 | } |
556 | } |
557 | } |
558 | |
559 | return best_score - FFMAX(stat_all - 10*best_score, 0)/10; |
560 | } |
561 | |
562 | /* autodetect fec presence. Must have at least 1024 bytes */ |
563 | static int get_packet_size(const uint8_t *buf, int size) |
564 | { |
565 | int score, fec_score, dvhs_score; |
566 | |
567 | if (size < (TS_FEC_PACKET_SIZE * 5 + 1)) |
568 | return AVERROR_INVALIDDATA; |
569 | |
570 | score = analyze(buf, size, TS_PACKET_SIZE, 0); |
571 | dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, 0); |
572 | fec_score = analyze(buf, size, TS_FEC_PACKET_SIZE, 0); |
573 | av_log(NULL, AV_LOG_TRACE, "score: %d, dvhs_score: %d, fec_score: %d \n", |
574 | score, dvhs_score, fec_score); |
575 | |
576 | if (score > fec_score && score > dvhs_score) |
577 | return TS_PACKET_SIZE; |
578 | else if (dvhs_score > score && dvhs_score > fec_score) |
579 | return TS_DVHS_PACKET_SIZE; |
580 | else if (score < fec_score && dvhs_score < fec_score) |
581 | return TS_FEC_PACKET_SIZE; |
582 | else |
583 | return AVERROR_INVALIDDATA; |
584 | } |
585 | |
586 | typedef struct SectionHeader { |
587 | uint8_t tid; |
588 | uint16_t id; |
589 | uint8_t version; |
590 | uint8_t sec_num; |
591 | uint8_t last_sec_num; |
592 | } SectionHeader; |
593 | |
594 | static int skip_identical(const SectionHeader *h, MpegTSSectionFilter *tssf) |
595 | { |
596 | if (h->version == tssf->last_ver && tssf->last_crc == tssf->crc) |
597 | return 1; |
598 | |
599 | tssf->last_ver = h->version; |
600 | tssf->last_crc = tssf->crc; |
601 | |
602 | return 0; |
603 | } |
604 | |
605 | static inline int get8(const uint8_t **pp, const uint8_t *p_end) |
606 | { |
607 | const uint8_t *p; |
608 | int c; |
609 | |
610 | p = *pp; |
611 | if (p >= p_end) |
612 | return AVERROR_INVALIDDATA; |
613 | c = *p++; |
614 | *pp = p; |
615 | return c; |
616 | } |
617 | |
618 | static inline int get16(const uint8_t **pp, const uint8_t *p_end) |
619 | { |
620 | const uint8_t *p; |
621 | int c; |
622 | |
623 | p = *pp; |
624 | if (1 >= p_end - p) |
625 | return AVERROR_INVALIDDATA; |
626 | c = AV_RB16(p); |
627 | p += 2; |
628 | *pp = p; |
629 | return c; |
630 | } |
631 | |
632 | /* read and allocate a DVB string preceded by its length */ |
633 | static char *getstr8(const uint8_t **pp, const uint8_t *p_end) |
634 | { |
635 | int len; |
636 | const uint8_t *p; |
637 | char *str; |
638 | |
639 | p = *pp; |
640 | len = get8(&p, p_end); |
641 | if (len < 0) |
642 | return NULL; |
643 | if (len > p_end - p) |
644 | return NULL; |
645 | str = av_malloc(len + 1); |
646 | if (!str) |
647 | return NULL; |
648 | memcpy(str, p, len); |
649 | str[len] = '\0'; |
650 | p += len; |
651 | *pp = p; |
652 | return str; |
653 | } |
654 | |
655 | static int parse_section_header(SectionHeader *h, |
656 | const uint8_t **pp, const uint8_t *p_end) |
657 | { |
658 | int val; |
659 | |
660 | val = get8(pp, p_end); |
661 | if (val < 0) |
662 | return val; |
663 | h->tid = val; |
664 | *pp += 2; |
665 | val = get16(pp, p_end); |
666 | if (val < 0) |
667 | return val; |
668 | h->id = val; |
669 | val = get8(pp, p_end); |
670 | if (val < 0) |
671 | return val; |
672 | h->version = (val >> 1) & 0x1f; |
673 | val = get8(pp, p_end); |
674 | if (val < 0) |
675 | return val; |
676 | h->sec_num = val; |
677 | val = get8(pp, p_end); |
678 | if (val < 0) |
679 | return val; |
680 | h->last_sec_num = val; |
681 | return 0; |
682 | } |
683 | |
684 | typedef struct StreamType { |
685 | uint32_t stream_type; |
686 | enum AVMediaType codec_type; |
687 | enum AVCodecID codec_id; |
688 | } StreamType; |
689 | |
690 | static const StreamType ISO_types[] = { |
691 | { 0x01, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO }, |
692 | { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO }, |
693 | { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 }, |
694 | { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 }, |
695 | { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC }, |
696 | { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 }, |
697 | /* Makito encoder sets stream type 0x11 for AAC, |
698 | * so auto-detect LOAS/LATM instead of hardcoding it. */ |
699 | #if !CONFIG_LOAS_DEMUXER |
700 | { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */ |
701 | #endif |
702 | { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 }, |
703 | { 0x1c, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC }, |
704 | { 0x20, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 }, |
705 | { 0x21, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_JPEG2000 }, |
706 | { 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC }, |
707 | { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS }, |
708 | { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC }, |
709 | { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 }, |
710 | { 0 }, |
711 | }; |
712 | |
713 | static const StreamType HDMV_types[] = { |
714 | { 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY }, |
715 | { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, |
716 | { 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, |
717 | { 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD }, |
718 | { 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, |
719 | { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */ |
720 | { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/ |
721 | { 0xa1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC3 Secondary Audio */ |
722 | { 0xa2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS Express Secondary Audio */ |
723 | { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE }, |
724 | { 0x92, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_TEXT_SUBTITLE }, |
725 | { 0 }, |
726 | }; |
727 | |
728 | /* SCTE types */ |
729 | static const StreamType SCTE_types[] = { |
730 | { 0x86, AVMEDIA_TYPE_DATA, AV_CODEC_ID_SCTE_35 }, |
731 | { 0 }, |
732 | }; |
733 | |
734 | /* ATSC ? */ |
735 | static const StreamType MISC_types[] = { |
736 | { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, |
737 | { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, |
738 | { 0 }, |
739 | }; |
740 | |
741 | static const StreamType REGD_types[] = { |
742 | { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC }, |
743 | { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, |
744 | { MKTAG('B', 'S', 'S', 'D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M }, |
745 | { MKTAG('D', 'T', 'S', '1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, |
746 | { MKTAG('D', 'T', 'S', '2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, |
747 | { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, |
748 | { MKTAG('D', 'T', 'S', 'H'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, |
749 | { MKTAG('E', 'A', 'C', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, |
750 | { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC }, |
751 | { MKTAG('K', 'L', 'V', 'A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV }, |
752 | { MKTAG('I', 'D', '3', ' '), AVMEDIA_TYPE_DATA, AV_CODEC_ID_TIMED_ID3 }, |
753 | { MKTAG('V', 'C', '-', '1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 }, |
754 | { MKTAG('O', 'p', 'u', 's'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_OPUS }, |
755 | { MKTAG('D', 'R', 'A', '1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DRA }, |
756 | { MKTAG('d', 'v', 'h', 'e'), AVMEDIA_TYPE_VIDEO , AV_CODEC_ID_HEVC}, /* HEVC Dobly vision ES dvhe*/ |
757 | { MKTAG('D', 'O', 'V', 'I'), AVMEDIA_TYPE_VIDEO , AV_CODEC_ID_HEVC}, /* HEVC Dobly vision ES dovi*/ |
758 | { 0 }, |
759 | }; |
760 | |
761 | static const StreamType METADATA_types[] = { |
762 | { MKTAG('K','L','V','A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV }, |
763 | { MKTAG('I','D','3',' '), AVMEDIA_TYPE_DATA, AV_CODEC_ID_TIMED_ID3 }, |
764 | { 0 }, |
765 | }; |
766 | |
767 | /* descriptor present */ |
768 | static const StreamType DESC_types[] = { |
769 | { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */ |
770 | { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */ |
771 | { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, |
772 | { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT }, |
773 | { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */ |
774 | { 0 }, |
775 | }; |
776 | |
777 | static void mpegts_find_stream_type(AVStream *st, |
778 | uint32_t stream_type, |
779 | const StreamType *types) |
780 | { |
781 | for (; types->stream_type; types++) |
782 | if (stream_type == types->stream_type) { |
783 | if (st->codecpar->codec_type != types->codec_type || |
784 | st->codecpar->codec_id != types->codec_id) { |
785 | st->codecpar->codec_type = types->codec_type; |
786 | st->codecpar->codec_id = types->codec_id; |
787 | st->internal->need_context_update = 1; |
788 | } |
789 | st->request_probe = 0; |
790 | return; |
791 | } |
792 | } |
793 | |
794 | static int mpegts_set_stream_info(AVStream *st, PESContext *pes, |
795 | uint32_t stream_type, uint32_t prog_reg_desc) |
796 | { |
797 | int old_codec_type = st->codecpar->codec_type; |
798 | int old_codec_id = st->codecpar->codec_id; |
799 | int old_codec_tag = st->codecpar->codec_tag; |
800 | |
801 | if (avcodec_is_open(st->internal->avctx)) { |
802 | av_log(pes->stream, AV_LOG_DEBUG, "cannot set stream info, internal codec is open\n"); |
803 | return 0; |
804 | } |
805 | |
806 | avpriv_set_pts_info(st, 33, 1, 90000); |
807 | st->priv_data = pes; |
808 | st->codecpar->codec_type = AVMEDIA_TYPE_DATA; |
809 | st->codecpar->codec_id = AV_CODEC_ID_NONE; |
810 | st->need_parsing = AVSTREAM_PARSE_FULL; |
811 | pes->st = st; |
812 | pes->stream_type = stream_type; |
813 | |
814 | av_log(pes->stream, AV_LOG_DEBUG, |
815 | "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n", |
816 | st->index, pes->stream_type, pes->pid, (char *)&prog_reg_desc); |
817 | |
818 | st->codecpar->codec_tag = pes->stream_type; |
819 | |
820 | mpegts_find_stream_type(st, pes->stream_type, ISO_types); |
821 | if (pes->stream_type == 4) |
822 | st->request_probe = 50; |
823 | if ((prog_reg_desc == AV_RL32("HDMV") || |
824 | prog_reg_desc == AV_RL32("HDPR")) && |
825 | st->codecpar->codec_id == AV_CODEC_ID_NONE) { |
826 | mpegts_find_stream_type(st, pes->stream_type, HDMV_types); |
827 | if (pes->stream_type == 0x83) { |
828 | // HDMV TrueHD streams also contain an AC3 coded version of the |
829 | // audio track - add a second stream for this |
830 | AVStream *sub_st; |
831 | // priv_data cannot be shared between streams |
832 | PESContext *sub_pes = av_malloc(sizeof(*sub_pes)); |
833 | if (!sub_pes) |
834 | return AVERROR(ENOMEM); |
835 | memcpy(sub_pes, pes, sizeof(*sub_pes)); |
836 | |
837 | sub_st = avformat_new_stream(pes->stream, NULL); |
838 | if (!sub_st) { |
839 | av_free(sub_pes); |
840 | return AVERROR(ENOMEM); |
841 | } |
842 | |
843 | sub_st->id = pes->pid; |
844 | avpriv_set_pts_info(sub_st, 33, 1, 90000); |
845 | sub_st->priv_data = sub_pes; |
846 | sub_st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
847 | sub_st->codecpar->codec_id = AV_CODEC_ID_AC3; |
848 | sub_st->need_parsing = AVSTREAM_PARSE_FULL; |
849 | sub_pes->sub_st = pes->sub_st = sub_st; |
850 | } |
851 | if (pes->stream_type == 0x81) { |
852 | // HDMV AC3 streams also contain an TRUEHD coded version of the |
853 | // audio track - add a second stream for this |
854 | AVStream *sub_st; |
855 | // priv_data cannot be shared between streams |
856 | PESContext *sub_pes = av_malloc(sizeof(*sub_pes)); |
857 | if (!sub_pes) |
858 | return AVERROR(ENOMEM); |
859 | memcpy(sub_pes, pes, sizeof(*sub_pes)); |
860 | |
861 | sub_st = avformat_new_stream(pes->stream, NULL); |
862 | if (!sub_st) { |
863 | av_free(sub_pes); |
864 | return AVERROR(ENOMEM); |
865 | } |
866 | |
867 | sub_st->id = pes->pid; |
868 | avpriv_set_pts_info(sub_st, 33, 1, 90000); |
869 | sub_st->priv_data = sub_pes; |
870 | sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
871 | sub_st->codec->codec_id = AV_CODEC_ID_TRUEHD; |
872 | sub_st->need_parsing = AVSTREAM_PARSE_FULL; |
873 | sub_st->discard = AVDISCARD_ALL; |
874 | sub_pes->sub_st = pes->sub_st = sub_st; |
875 | } |
876 | } |
877 | if (st->codec->codec_id == AV_CODEC_ID_NONE && pes->stream_type == 0x82) { |
878 | AVStream *sub_st; |
879 | PESContext *sub_pes = av_malloc(sizeof(*sub_pes)); |
880 | if (!sub_pes) |
881 | return AVERROR(ENOMEM); |
882 | memcpy(sub_pes, pes, sizeof(*sub_pes)); |
883 | sub_st = avformat_new_stream(pes->stream, NULL); |
884 | if (!sub_st) { |
885 | av_free(sub_pes); |
886 | return AVERROR(ENOMEM); |
887 | } |
888 | |
889 | sub_st->id = pes->pid; |
890 | avpriv_set_pts_info(sub_st, 33, 1, 90000); |
891 | sub_st->priv_data = sub_pes; |
892 | sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
893 | sub_st->codec->codec_id = AV_CODEC_ID_DTS; |
894 | sub_st->need_parsing = AVSTREAM_PARSE_FULL; |
895 | sub_pes->sub_st = pes->sub_st = sub_st; |
896 | } |
897 | if (st->codecpar->codec_id == AV_CODEC_ID_NONE) |
898 | mpegts_find_stream_type(st, pes->stream_type, MISC_types); |
899 | if (st->codecpar->codec_id == AV_CODEC_ID_NONE) { |
900 | st->codecpar->codec_id = old_codec_id; |
901 | st->codecpar->codec_type = old_codec_type; |
902 | } |
903 | if ((st->codecpar->codec_id == AV_CODEC_ID_NONE || |
904 | (st->request_probe > 0 && st->request_probe < AVPROBE_SCORE_STREAM_RETRY / 5)) && |
905 | st->probe_packets > 0 && |
906 | stream_type == STREAM_TYPE_PRIVATE_DATA) { |
907 | st->codecpar->codec_type = AVMEDIA_TYPE_DATA; |
908 | st->codecpar->codec_id = AV_CODEC_ID_BIN_DATA; |
909 | st->request_probe = AVPROBE_SCORE_STREAM_RETRY / 5; |
910 | } |
911 | |
912 | /* queue a context update if properties changed */ |
913 | if (old_codec_type != st->codecpar->codec_type || |
914 | old_codec_id != st->codecpar->codec_id || |
915 | old_codec_tag != st->codecpar->codec_tag) |
916 | st->internal->need_context_update = 1; |
917 | |
918 | return 0; |
919 | } |
920 | |
921 | static void reset_pes_packet_state(PESContext *pes) |
922 | { |
923 | pes->pts = AV_NOPTS_VALUE; |
924 | pes->dts = AV_NOPTS_VALUE; |
925 | pes->data_index = 0; |
926 | pes->flags = 0; |
927 | av_buffer_unref(&pes->buffer); |
928 | } |
929 | |
930 | static void new_data_packet(const uint8_t *buffer, int len, AVPacket *pkt) |
931 | { |
932 | av_init_packet(pkt); |
933 | pkt->data = (uint8_t *)buffer; |
934 | pkt->size = len; |
935 | } |
936 | |
937 | static int new_pes_packet(PESContext *pes, AVPacket *pkt) |
938 | { |
939 | char *sd; |
940 | |
941 | av_init_packet(pkt); |
942 | |
943 | pkt->buf = pes->buffer; |
944 | pkt->data = pes->buffer->data; |
945 | pkt->size = pes->data_index; |
946 | |
947 | if (pes->total_size != MAX_PES_PAYLOAD && |
948 | pes->pes_header_size + pes->data_index != pes->total_size + |
949 | PES_START_SIZE) { |
950 | av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n"); |
951 | pes->flags |= AV_PKT_FLAG_CORRUPT; |
952 | } |
953 | memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
954 | |
955 | // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID |
956 | if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76) |
957 | pkt->stream_index = pes->sub_st->index; |
958 | else if (pes->sub_st && pes->stream_type == 0x81 && pes->extended_stream_id == 0x72) |
959 | pkt->stream_index = pes->sub_st->index; |
960 | else |
961 | pkt->stream_index = pes->st->index; |
962 | pkt->pts = pes->pts; |
963 | pkt->dts = pes->dts; |
964 | /* store position of first TS packet of this PES packet */ |
965 | pkt->pos = pes->ts_packet_pos; |
966 | pkt->flags = pes->flags; |
967 | |
968 | pes->buffer = NULL; |
969 | reset_pes_packet_state(pes); |
970 | |
971 | sd = av_packet_new_side_data(pkt, AV_PKT_DATA_MPEGTS_STREAM_ID, 1); |
972 | if (!sd) |
973 | return AVERROR(ENOMEM); |
974 | *sd = pes->stream_id; |
975 | |
976 | return 0; |
977 | } |
978 | |
979 | static uint64_t get_ts64(GetBitContext *gb, int bits) |
980 | { |
981 | if (get_bits_left(gb) < bits) |
982 | return AV_NOPTS_VALUE; |
983 | return get_bits64(gb, bits); |
984 | } |
985 | |
986 | static int read_sl_header(PESContext *pes, SLConfigDescr *sl, |
987 | const uint8_t *buf, int buf_size) |
988 | { |
989 | GetBitContext gb; |
990 | int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0; |
991 | int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0; |
992 | int dts_flag = -1, cts_flag = -1; |
993 | int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE; |
994 | uint8_t buf_padded[128 + AV_INPUT_BUFFER_PADDING_SIZE]; |
995 | int buf_padded_size = FFMIN(buf_size, sizeof(buf_padded) - AV_INPUT_BUFFER_PADDING_SIZE); |
996 | |
997 | memcpy(buf_padded, buf, buf_padded_size); |
998 | |
999 | init_get_bits(&gb, buf_padded, buf_padded_size * 8); |
1000 | |
1001 | if (sl->use_au_start) |
1002 | au_start_flag = get_bits1(&gb); |
1003 | if (sl->use_au_end) |
1004 | au_end_flag = get_bits1(&gb); |
1005 | if (!sl->use_au_start && !sl->use_au_end) |
1006 | au_start_flag = au_end_flag = 1; |
1007 | if (sl->ocr_len > 0) |
1008 | ocr_flag = get_bits1(&gb); |
1009 | if (sl->use_idle) |
1010 | idle_flag = get_bits1(&gb); |
1011 | if (sl->use_padding) |
1012 | padding_flag = get_bits1(&gb); |
1013 | if (padding_flag) |
1014 | padding_bits = get_bits(&gb, 3); |
1015 | |
1016 | if (!idle_flag && (!padding_flag || padding_bits != 0)) { |
1017 | if (sl->packet_seq_num_len) |
1018 | skip_bits_long(&gb, sl->packet_seq_num_len); |
1019 | if (sl->degr_prior_len) |
1020 | if (get_bits1(&gb)) |
1021 | skip_bits(&gb, sl->degr_prior_len); |
1022 | if (ocr_flag) |
1023 | skip_bits_long(&gb, sl->ocr_len); |
1024 | if (au_start_flag) { |
1025 | if (sl->use_rand_acc_pt) |
1026 | get_bits1(&gb); |
1027 | if (sl->au_seq_num_len > 0) |
1028 | skip_bits_long(&gb, sl->au_seq_num_len); |
1029 | if (sl->use_timestamps) { |
1030 | dts_flag = get_bits1(&gb); |
1031 | cts_flag = get_bits1(&gb); |
1032 | } |
1033 | } |
1034 | if (sl->inst_bitrate_len) |
1035 | inst_bitrate_flag = get_bits1(&gb); |
1036 | if (dts_flag == 1) |
1037 | dts = get_ts64(&gb, sl->timestamp_len); |
1038 | if (cts_flag == 1) |
1039 | cts = get_ts64(&gb, sl->timestamp_len); |
1040 | if (sl->au_len > 0) |
1041 | skip_bits_long(&gb, sl->au_len); |
1042 | if (inst_bitrate_flag) |
1043 | skip_bits_long(&gb, sl->inst_bitrate_len); |
1044 | } |
1045 | |
1046 | if (dts != AV_NOPTS_VALUE) |
1047 | pes->dts = dts; |
1048 | if (cts != AV_NOPTS_VALUE) |
1049 | pes->pts = cts; |
1050 | |
1051 | if (sl->timestamp_len && sl->timestamp_res) |
1052 | avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res); |
1053 | |
1054 | return (get_bits_count(&gb) + 7) >> 3; |
1055 | } |
1056 | |
1057 | /* return non zero if a packet could be constructed */ |
1058 | static int mpegts_push_data(MpegTSFilter *filter, |
1059 | const uint8_t *buf, int buf_size, int is_start, |
1060 | int64_t pos) |
1061 | { |
1062 | PESContext *pes = filter->u.pes_filter.opaque; |
1063 | MpegTSContext *ts = pes->ts; |
1064 | const uint8_t *p; |
1065 | int ret, len, code; |
1066 | |
1067 | if (!ts->pkt) |
1068 | return 0; |
1069 | |
1070 | if (is_start) { |
1071 | if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { |
1072 | ret = new_pes_packet(pes, ts->pkt); |
1073 | if (ret < 0) |
1074 | return ret; |
1075 | ts->stop_parse = 1; |
1076 | } else { |
1077 | reset_pes_packet_state(pes); |
1078 | } |
1079 | pes->state = MPEGTS_HEADER; |
1080 | pes->ts_packet_pos = pos; |
1081 | } |
1082 | p = buf; |
1083 | while (buf_size > 0) { |
1084 | switch (pes->state) { |
1085 | case MPEGTS_HEADER: |
1086 | len = PES_START_SIZE - pes->data_index; |
1087 | if (len > buf_size) |
1088 | len = buf_size; |
1089 | memcpy(pes->header + pes->data_index, p, len); |
1090 | pes->data_index += len; |
1091 | p += len; |
1092 | buf_size -= len; |
1093 | if (pes->data_index == PES_START_SIZE) { |
1094 | /* we got all the PES or section header. We can now |
1095 | * decide */ |
1096 | if (pes->header[0] == 0x00 && pes->header[1] == 0x00 && |
1097 | pes->header[2] == 0x01) { |
1098 | /* it must be an MPEG-2 PES stream */ |
1099 | code = pes->header[3] | 0x100; |
1100 | av_log(pes->stream, AV_LOG_TRACE, "pid=%x pes_code=%#x\n", pes->pid, |
1101 | code); |
1102 | pes->stream_id = pes->header[3]; |
1103 | |
1104 | if ((pes->st && pes->st->discard == AVDISCARD_ALL && |
1105 | (!pes->sub_st || |
1106 | pes->sub_st->discard == AVDISCARD_ALL)) || |
1107 | code == 0x1be) /* padding_stream */ |
1108 | goto skip; |
1109 | |
1110 | /* stream not present in PMT */ |
1111 | if (!pes->st) { |
1112 | if (ts->skip_changes) |
1113 | goto skip; |
1114 | |
1115 | pes->st = avformat_new_stream(ts->stream, NULL); |
1116 | if (!pes->st) |
1117 | return AVERROR(ENOMEM); |
1118 | pes->st->id = pes->pid; |
1119 | mpegts_set_stream_info(pes->st, pes, 0, 0); |
1120 | } |
1121 | |
1122 | pes->total_size = AV_RB16(pes->header + 4); |
1123 | /* NOTE: a zero total size means the PES size is |
1124 | * unbounded */ |
1125 | if (!pes->total_size) |
1126 | pes->total_size = MAX_PES_PAYLOAD; |
1127 | |
1128 | /* allocate pes buffer */ |
1129 | pes->buffer = av_buffer_alloc(pes->total_size + |
1130 | AV_INPUT_BUFFER_PADDING_SIZE); |
1131 | if (!pes->buffer) |
1132 | return AVERROR(ENOMEM); |
1133 | |
1134 | if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */ |
1135 | code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */ |
1136 | code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */ |
1137 | code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */ |
1138 | pes->state = MPEGTS_PESHEADER; |
1139 | if (pes->st->codecpar->codec_id == AV_CODEC_ID_NONE && !pes->st->request_probe) { |
1140 | av_log(pes->stream, AV_LOG_TRACE, |
1141 | "pid=%x stream_type=%x probing\n", |
1142 | pes->pid, |
1143 | pes->stream_type); |
1144 | pes->st->request_probe = 1; |
1145 | } |
1146 | } else { |
1147 | pes->pes_header_size = 6; |
1148 | pes->state = MPEGTS_PAYLOAD; |
1149 | pes->data_index = 0; |
1150 | } |
1151 | } else { |
1152 | /* otherwise, it should be a table */ |
1153 | /* skip packet */ |
1154 | skip: |
1155 | pes->state = MPEGTS_SKIP; |
1156 | continue; |
1157 | } |
1158 | } |
1159 | break; |
1160 | /**********************************************/ |
1161 | /* PES packing parsing */ |
1162 | case MPEGTS_PESHEADER: |
1163 | len = PES_HEADER_SIZE - pes->data_index; |
1164 | if (len < 0) |
1165 | return AVERROR_INVALIDDATA; |
1166 | if (len > buf_size) |
1167 | len = buf_size; |
1168 | memcpy(pes->header + pes->data_index, p, len); |
1169 | pes->data_index += len; |
1170 | p += len; |
1171 | buf_size -= len; |
1172 | if (pes->data_index == PES_HEADER_SIZE) { |
1173 | pes->pes_header_size = pes->header[8] + 9; |
1174 | pes->state = MPEGTS_PESHEADER_FILL; |
1175 | } |
1176 | break; |
1177 | case MPEGTS_PESHEADER_FILL: |
1178 | len = pes->pes_header_size - pes->data_index; |
1179 | if (len < 0) |
1180 | return AVERROR_INVALIDDATA; |
1181 | if (len > buf_size) |
1182 | len = buf_size; |
1183 | memcpy(pes->header + pes->data_index, p, len); |
1184 | pes->data_index += len; |
1185 | p += len; |
1186 | buf_size -= len; |
1187 | if (pes->data_index == pes->pes_header_size) { |
1188 | const uint8_t *r; |
1189 | unsigned int flags, pes_ext, skip; |
1190 | |
1191 | flags = pes->header[7]; |
1192 | r = pes->header + 9; |
1193 | pes->pts = AV_NOPTS_VALUE; |
1194 | pes->dts = AV_NOPTS_VALUE; |
1195 | if ((flags & 0xc0) == 0x80) { |
1196 | pes->dts = pes->pts = ff_parse_pes_pts(r); |
1197 | r += 5; |
1198 | } else if ((flags & 0xc0) == 0xc0) { |
1199 | pes->pts = ff_parse_pes_pts(r); |
1200 | r += 5; |
1201 | pes->dts = ff_parse_pes_pts(r); |
1202 | r += 5; |
1203 | } |
1204 | pes->extended_stream_id = -1; |
1205 | if (flags & 0x01) { /* PES extension */ |
1206 | pes_ext = *r++; |
1207 | /* Skip PES private data, program packet sequence counter and P-STD buffer */ |
1208 | skip = (pes_ext >> 4) & 0xb; |
1209 | skip += skip & 0x9; |
1210 | r += skip; |
1211 | if ((pes_ext & 0x41) == 0x01 && |
1212 | (r + 2) <= (pes->header + pes->pes_header_size)) { |
1213 | /* PES extension 2 */ |
1214 | if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0) |
1215 | pes->extended_stream_id = r[1]; |
1216 | } |
1217 | } |
1218 | |
1219 | /* we got the full header. We parse it and get the payload */ |
1220 | pes->state = MPEGTS_PAYLOAD; |
1221 | pes->data_index = 0; |
1222 | if (pes->stream_type == 0x12 && buf_size > 0) { |
1223 | int sl_header_bytes = read_sl_header(pes, &pes->sl, p, |
1224 | buf_size); |
1225 | pes->pes_header_size += sl_header_bytes; |
1226 | p += sl_header_bytes; |
1227 | buf_size -= sl_header_bytes; |
1228 | } |
1229 | if (pes->stream_type == 0x15 && buf_size >= 5) { |
1230 | /* skip metadata access unit header */ |
1231 | pes->pes_header_size += 5; |
1232 | p += 5; |
1233 | buf_size -= 5; |
1234 | } |
1235 | if ( pes->ts->fix_teletext_pts |
1236 | && ( pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT |
1237 | || pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE) |
1238 | ) { |
1239 | AVProgram *p = NULL; |
1240 | while ((p = av_find_program_from_stream(pes->stream, p, pes->st->index))) { |
1241 | if (p->pcr_pid != -1 && p->discard != AVDISCARD_ALL) { |
1242 | MpegTSFilter *f = pes->ts->pids[p->pcr_pid]; |
1243 | if (f) { |
1244 | AVStream *st = NULL; |
1245 | if (f->type == MPEGTS_PES) { |
1246 | PESContext *pcrpes = f->u.pes_filter.opaque; |
1247 | if (pcrpes) |
1248 | st = pcrpes->st; |
1249 | } else if (f->type == MPEGTS_PCR) { |
1250 | int i; |
1251 | for (i = 0; i < p->nb_stream_indexes; i++) { |
1252 | AVStream *pst = pes->stream->streams[p->stream_index[i]]; |
1253 | if (pst->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) |
1254 | st = pst; |
1255 | } |
1256 | } |
1257 | if (f->last_pcr != -1 && st && st->discard != AVDISCARD_ALL) { |
1258 | // teletext packets do not always have correct timestamps, |
1259 | // the standard says they should be handled after 40.6 ms at most, |
1260 | // and the pcr error to this packet should be no more than 100 ms. |
1261 | // TODO: we should interpolate the PCR, not just use the last one |
1262 | int64_t pcr = f->last_pcr / 300; |
1263 | pes->st->pts_wrap_reference = st->pts_wrap_reference; |
1264 | pes->st->pts_wrap_behavior = st->pts_wrap_behavior; |
1265 | if (pes->dts == AV_NOPTS_VALUE || pes->dts < pcr) { |
1266 | pes->pts = pes->dts = pcr; |
1267 | } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT && |
1268 | pes->dts > pcr + 3654 + 9000) { |
1269 | pes->pts = pes->dts = pcr + 3654 + 9000; |
1270 | } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE && |
1271 | pes->dts > pcr + 10*90000) { //10sec |
1272 | pes->pts = pes->dts = pcr + 3654 + 9000; |
1273 | } |
1274 | break; |
1275 | } |
1276 | } |
1277 | } |
1278 | } |
1279 | } |
1280 | } |
1281 | break; |
1282 | case MPEGTS_PAYLOAD: |
1283 | if (pes->buffer) { |
1284 | if (pes->data_index > 0 && |
1285 | pes->data_index + buf_size > pes->total_size) { |
1286 | ret = new_pes_packet(pes, ts->pkt); |
1287 | if (ret < 0) |
1288 | return ret; |
1289 | pes->total_size = MAX_PES_PAYLOAD; |
1290 | pes->buffer = av_buffer_alloc(pes->total_size + |
1291 | AV_INPUT_BUFFER_PADDING_SIZE); |
1292 | if (!pes->buffer) |
1293 | return AVERROR(ENOMEM); |
1294 | ts->stop_parse = 1; |
1295 | } else if (pes->data_index == 0 && |
1296 | buf_size > pes->total_size) { |
1297 | // pes packet size is < ts size packet and pes data is padded with 0xff |
1298 | // not sure if this is legal in ts but see issue #2392 |
1299 | buf_size = pes->total_size; |
1300 | } |
1301 | memcpy(pes->buffer->data + pes->data_index, p, buf_size); |
1302 | pes->data_index += buf_size; |
1303 | /* emit complete packets with known packet size |
1304 | * decreases demuxer delay for infrequent packets like subtitles from |
1305 | * a couple of seconds to milliseconds for properly muxed files. |
1306 | * total_size is the number of bytes following pes_packet_length |
1307 | * in the pes header, i.e. not counting the first PES_START_SIZE bytes */ |
1308 | if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD && |
1309 | pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) { |
1310 | ts->stop_parse = 1; |
1311 | ret = new_pes_packet(pes, ts->pkt); |
1312 | if (ret < 0) |
1313 | return ret; |
1314 | } |
1315 | } |
1316 | buf_size = 0; |
1317 | break; |
1318 | case MPEGTS_SKIP: |
1319 | buf_size = 0; |
1320 | break; |
1321 | } |
1322 | } |
1323 | |
1324 | return 0; |
1325 | } |
1326 | |
1327 | static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid) |
1328 | { |
1329 | MpegTSFilter *tss; |
1330 | PESContext *pes; |
1331 | |
1332 | /* if no pid found, then add a pid context */ |
1333 | pes = av_mallocz(sizeof(PESContext)); |
1334 | if (!pes) |
1335 | return 0; |
1336 | pes->ts = ts; |
1337 | pes->stream = ts->stream; |
1338 | pes->pid = pid; |
1339 | pes->pcr_pid = pcr_pid; |
1340 | pes->state = MPEGTS_SKIP; |
1341 | pes->pts = AV_NOPTS_VALUE; |
1342 | pes->dts = AV_NOPTS_VALUE; |
1343 | tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes); |
1344 | if (!tss) { |
1345 | av_free(pes); |
1346 | return 0; |
1347 | } |
1348 | return pes; |
1349 | } |
1350 | |
1351 | #define MAX_LEVEL 4 |
1352 | typedef struct MP4DescrParseContext { |
1353 | AVFormatContext *s; |
1354 | AVIOContext pb; |
1355 | Mp4Descr *descr; |
1356 | Mp4Descr *active_descr; |
1357 | int descr_count; |
1358 | int max_descr_count; |
1359 | int level; |
1360 | int predefined_SLConfigDescriptor_seen; |
1361 | } MP4DescrParseContext; |
1362 | |
1363 | static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s, |
1364 | const uint8_t *buf, unsigned size, |
1365 | Mp4Descr *descr, int max_descr_count) |
1366 | { |
1367 | int ret; |
1368 | if (size > (1 << 30)) |
1369 | return AVERROR_INVALIDDATA; |
1370 | |
1371 | if ((ret = ffio_init_context(&d->pb, (unsigned char *)buf, size, 0, |
1372 | NULL, NULL, NULL, NULL)) < 0) |
1373 | return ret; |
1374 | |
1375 | d->s = s; |
1376 | d->level = 0; |
1377 | d->descr_count = 0; |
1378 | d->descr = descr; |
1379 | d->active_descr = NULL; |
1380 | d->max_descr_count = max_descr_count; |
1381 | |
1382 | return 0; |
1383 | } |
1384 | |
1385 | static void update_offsets(AVIOContext *pb, int64_t *off, int *len) |
1386 | { |
1387 | int64_t new_off = avio_tell(pb); |
1388 | (*len) -= new_off - *off; |
1389 | *off = new_off; |
1390 | } |
1391 | |
1392 | static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, |
1393 | int target_tag); |
1394 | |
1395 | static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len) |
1396 | { |
1397 | while (len > 0) { |
1398 | int ret = parse_mp4_descr(d, off, len, 0); |
1399 | if (ret < 0) |
1400 | return ret; |
1401 | update_offsets(&d->pb, &off, &len); |
1402 | } |
1403 | return 0; |
1404 | } |
1405 | |
1406 | static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len) |
1407 | { |
1408 | avio_rb16(&d->pb); // ID |
1409 | avio_r8(&d->pb); |
1410 | avio_r8(&d->pb); |
1411 | avio_r8(&d->pb); |
1412 | avio_r8(&d->pb); |
1413 | avio_r8(&d->pb); |
1414 | update_offsets(&d->pb, &off, &len); |
1415 | return parse_mp4_descr_arr(d, off, len); |
1416 | } |
1417 | |
1418 | static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len) |
1419 | { |
1420 | int id_flags; |
1421 | if (len < 2) |
1422 | return 0; |
1423 | id_flags = avio_rb16(&d->pb); |
1424 | if (!(id_flags & 0x0020)) { // URL_Flag |
1425 | update_offsets(&d->pb, &off, &len); |
1426 | return parse_mp4_descr_arr(d, off, len); // ES_Descriptor[] |
1427 | } else { |
1428 | return 0; |
1429 | } |
1430 | } |
1431 | |
1432 | static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len) |
1433 | { |
1434 | int es_id = 0; |
1435 | int ret = 0; |
1436 | |
1437 | if (d->descr_count >= d->max_descr_count) |
1438 | return AVERROR_INVALIDDATA; |
1439 | ff_mp4_parse_es_descr(&d->pb, &es_id); |
1440 | d->active_descr = d->descr + (d->descr_count++); |
1441 | |
1442 | d->active_descr->es_id = es_id; |
1443 | update_offsets(&d->pb, &off, &len); |
1444 | if ((ret = parse_mp4_descr(d, off, len, MP4DecConfigDescrTag)) < 0) |
1445 | return ret; |
1446 | update_offsets(&d->pb, &off, &len); |
1447 | if (len > 0) |
1448 | ret = parse_mp4_descr(d, off, len, MP4SLDescrTag); |
1449 | d->active_descr = NULL; |
1450 | return ret; |
1451 | } |
1452 | |
1453 | static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, |
1454 | int len) |
1455 | { |
1456 | Mp4Descr *descr = d->active_descr; |
1457 | if (!descr) |
1458 | return AVERROR_INVALIDDATA; |
1459 | d->active_descr->dec_config_descr = av_malloc(len); |
1460 | if (!descr->dec_config_descr) |
1461 | return AVERROR(ENOMEM); |
1462 | descr->dec_config_descr_len = len; |
1463 | avio_read(&d->pb, descr->dec_config_descr, len); |
1464 | return 0; |
1465 | } |
1466 | |
1467 | static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len) |
1468 | { |
1469 | Mp4Descr *descr = d->active_descr; |
1470 | int predefined; |
1471 | if (!descr) |
1472 | return AVERROR_INVALIDDATA; |
1473 | |
1474 | #define R8_CHECK_CLIP_MAX(dst, maxv) do { \ |
1475 | descr->sl.dst = avio_r8(&d->pb); \ |
1476 | if (descr->sl.dst > maxv) { \ |
1477 | descr->sl.dst = maxv; \ |
1478 | return AVERROR_INVALIDDATA; \ |
1479 | } \ |
1480 | } while (0) |
1481 | |
1482 | predefined = avio_r8(&d->pb); |
1483 | if (!predefined) { |
1484 | int lengths; |
1485 | int flags = avio_r8(&d->pb); |
1486 | descr->sl.use_au_start = !!(flags & 0x80); |
1487 | descr->sl.use_au_end = !!(flags & 0x40); |
1488 | descr->sl.use_rand_acc_pt = !!(flags & 0x20); |
1489 | descr->sl.use_padding = !!(flags & 0x08); |
1490 | descr->sl.use_timestamps = !!(flags & 0x04); |
1491 | descr->sl.use_idle = !!(flags & 0x02); |
1492 | descr->sl.timestamp_res = avio_rb32(&d->pb); |
1493 | avio_rb32(&d->pb); |
1494 | R8_CHECK_CLIP_MAX(timestamp_len, 63); |
1495 | R8_CHECK_CLIP_MAX(ocr_len, 63); |
1496 | R8_CHECK_CLIP_MAX(au_len, 31); |
1497 | descr->sl.inst_bitrate_len = avio_r8(&d->pb); |
1498 | lengths = avio_rb16(&d->pb); |
1499 | descr->sl.degr_prior_len = lengths >> 12; |
1500 | descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f; |
1501 | descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f; |
1502 | } else if (!d->predefined_SLConfigDescriptor_seen){ |
1503 | avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor"); |
1504 | d->predefined_SLConfigDescriptor_seen = 1; |
1505 | } |
1506 | return 0; |
1507 | } |
1508 | |
1509 | static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, |
1510 | int target_tag) |
1511 | { |
1512 | int tag; |
1513 | int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag); |
1514 | int ret = 0; |
1515 | |
1516 | update_offsets(&d->pb, &off, &len); |
1517 | if (len < 0 || len1 > len || len1 <= 0) { |
1518 | av_log(d->s, AV_LOG_ERROR, |
1519 | "Tag %x length violation new length %d bytes remaining %d\n", |
1520 | tag, len1, len); |
1521 | return AVERROR_INVALIDDATA; |
1522 | } |
1523 | |
1524 | if (d->level++ >= MAX_LEVEL) { |
1525 | av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n"); |
1526 | ret = AVERROR_INVALIDDATA; |
1527 | goto done; |
1528 | } |
1529 | |
1530 | if (target_tag && tag != target_tag) { |
1531 | av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, |
1532 | target_tag); |
1533 | ret = AVERROR_INVALIDDATA; |
1534 | goto done; |
1535 | } |
1536 | |
1537 | switch (tag) { |
1538 | case MP4IODescrTag: |
1539 | ret = parse_MP4IODescrTag(d, off, len1); |
1540 | break; |
1541 | case MP4ODescrTag: |
1542 | ret = parse_MP4ODescrTag(d, off, len1); |
1543 | break; |
1544 | case MP4ESDescrTag: |
1545 | ret = parse_MP4ESDescrTag(d, off, len1); |
1546 | break; |
1547 | case MP4DecConfigDescrTag: |
1548 | ret = parse_MP4DecConfigDescrTag(d, off, len1); |
1549 | break; |
1550 | case MP4SLDescrTag: |
1551 | ret = parse_MP4SLDescrTag(d, off, len1); |
1552 | break; |
1553 | } |
1554 | |
1555 | |
1556 | done: |
1557 | d->level--; |
1558 | avio_seek(&d->pb, off + len1, SEEK_SET); |
1559 | return ret; |
1560 | } |
1561 | |
1562 | static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size, |
1563 | Mp4Descr *descr, int *descr_count, int max_descr_count) |
1564 | { |
1565 | MP4DescrParseContext d; |
1566 | int ret; |
1567 | |
1568 | ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count); |
1569 | if (ret < 0) |
1570 | return ret; |
1571 | |
1572 | ret = parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag); |
1573 | |
1574 | *descr_count = d.descr_count; |
1575 | return ret; |
1576 | } |
1577 | |
1578 | static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size, |
1579 | Mp4Descr *descr, int *descr_count, int max_descr_count) |
1580 | { |
1581 | MP4DescrParseContext d; |
1582 | int ret; |
1583 | |
1584 | ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count); |
1585 | if (ret < 0) |
1586 | return ret; |
1587 | |
1588 | ret = parse_mp4_descr_arr(&d, avio_tell(&d.pb), size); |
1589 | |
1590 | *descr_count = d.descr_count; |
1591 | return ret; |
1592 | } |
1593 | |
1594 | static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, |
1595 | int section_len) |
1596 | { |
1597 | MpegTSContext *ts = filter->u.section_filter.opaque; |
1598 | MpegTSSectionFilter *tssf = &filter->u.section_filter; |
1599 | SectionHeader h; |
1600 | const uint8_t *p, *p_end; |
1601 | AVIOContext pb; |
1602 | int mp4_descr_count = 0; |
1603 | Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } }; |
1604 | int i, pid; |
1605 | AVFormatContext *s = ts->stream; |
1606 | |
1607 | p_end = section + section_len - 4; |
1608 | p = section; |
1609 | if (parse_section_header(&h, &p, p_end) < 0) |
1610 | return; |
1611 | if (h.tid != M4OD_TID) |
1612 | return; |
1613 | if (skip_identical(&h, tssf)) |
1614 | return; |
1615 | |
1616 | mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count, |
1617 | MAX_MP4_DESCR_COUNT); |
1618 | |
1619 | for (pid = 0; pid < NB_PID_MAX; pid++) { |
1620 | if (!ts->pids[pid]) |
1621 | continue; |
1622 | for (i = 0; i < mp4_descr_count; i++) { |
1623 | PESContext *pes; |
1624 | AVStream *st; |
1625 | if (ts->pids[pid]->es_id != mp4_descr[i].es_id) |
1626 | continue; |
1627 | if (ts->pids[pid]->type != MPEGTS_PES) { |
1628 | av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid); |
1629 | continue; |
1630 | } |
1631 | pes = ts->pids[pid]->u.pes_filter.opaque; |
1632 | st = pes->st; |
1633 | if (!st) |
1634 | continue; |
1635 | |
1636 | pes->sl = mp4_descr[i].sl; |
1637 | |
1638 | ffio_init_context(&pb, mp4_descr[i].dec_config_descr, |
1639 | mp4_descr[i].dec_config_descr_len, 0, |
1640 | NULL, NULL, NULL, NULL); |
1641 | ff_mp4_read_dec_config_descr(s, st, &pb); |
1642 | if (st->codecpar->codec_id == AV_CODEC_ID_AAC && |
1643 | st->codecpar->extradata_size > 0) |
1644 | st->need_parsing = 0; |
1645 | if (st->codecpar->codec_id == AV_CODEC_ID_H264 && |
1646 | st->codecpar->extradata_size > 0) |
1647 | st->need_parsing = 0; |
1648 | |
1649 | st->codecpar->codec_type = avcodec_get_type(st->codecpar->codec_id); |
1650 | st->internal->need_context_update = 1; |
1651 | } |
1652 | } |
1653 | for (i = 0; i < mp4_descr_count; i++) |
1654 | av_free(mp4_descr[i].dec_config_descr); |
1655 | } |
1656 | |
1657 | static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section, |
1658 | int section_len) |
1659 | { |
1660 | AVProgram *prg = NULL; |
1661 | MpegTSContext *ts = filter->u.section_filter.opaque; |
1662 | |
1663 | int idx = ff_find_stream_index(ts->stream, filter->pid); |
1664 | if (idx < 0) |
1665 | return; |
1666 | |
1667 | new_data_packet(section, section_len, ts->pkt); |
1668 | ts->pkt->stream_index = idx; |
1669 | prg = av_find_program_from_stream(ts->stream, NULL, idx); |
1670 | if (prg && prg->pcr_pid != -1 && prg->discard != AVDISCARD_ALL) { |
1671 | MpegTSFilter *f = ts->pids[prg->pcr_pid]; |
1672 | if (f && f->last_pcr != -1) |
1673 | ts->pkt->pts = ts->pkt->dts = f->last_pcr/300; |
1674 | } |
1675 | ts->stop_parse = 1; |
1676 | |
1677 | } |
1678 | |
1679 | static const uint8_t opus_coupled_stream_cnt[9] = { |
1680 | 1, 0, 1, 1, 2, 2, 2, 3, 3 |
1681 | }; |
1682 | |
1683 | static const uint8_t opus_stream_cnt[9] = { |
1684 | 1, 1, 1, 2, 2, 3, 4, 4, 5, |
1685 | }; |
1686 | |
1687 | static const uint8_t opus_channel_map[8][8] = { |
1688 | { 0 }, |
1689 | { 0,1 }, |
1690 | { 0,2,1 }, |
1691 | { 0,1,2,3 }, |
1692 | { 0,4,1,2,3 }, |
1693 | { 0,4,1,2,3,5 }, |
1694 | { 0,4,1,2,3,5,6 }, |
1695 | { 0,6,1,2,3,4,5,7 }, |
1696 | }; |
1697 | |
1698 | int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type, |
1699 | const uint8_t **pp, const uint8_t *desc_list_end, |
1700 | Mp4Descr *mp4_descr, int mp4_descr_count, int pid, |
1701 | MpegTSContext *ts) |
1702 | { |
1703 | const uint8_t *desc_end; |
1704 | int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code; |
1705 | char language[252]; |
1706 | int i; |
1707 | |
1708 | desc_tag = get8(pp, desc_list_end); |
1709 | if (desc_tag < 0) |
1710 | return AVERROR_INVALIDDATA; |
1711 | desc_len = get8(pp, desc_list_end); |
1712 | if (desc_len < 0) |
1713 | return AVERROR_INVALIDDATA; |
1714 | desc_end = *pp + desc_len; |
1715 | if (desc_end > desc_list_end) |
1716 | return AVERROR_INVALIDDATA; |
1717 | |
1718 | av_log(fc, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", desc_tag, desc_len); |
1719 | |
1720 | if ((st->codecpar->codec_id == AV_CODEC_ID_NONE || st->request_probe > 0) && |
1721 | stream_type == STREAM_TYPE_PRIVATE_DATA) |
1722 | mpegts_find_stream_type(st, desc_tag, DESC_types); |
1723 | |
1724 | switch (desc_tag) { |
1725 | case 0x1E: /* SL descriptor */ |
1726 | desc_es_id = get16(pp, desc_end); |
1727 | if (desc_es_id < 0) |
1728 | break; |
1729 | if (ts && ts->pids[pid]) |
1730 | ts->pids[pid]->es_id = desc_es_id; |
1731 | for (i = 0; i < mp4_descr_count; i++) |
1732 | if (mp4_descr[i].dec_config_descr_len && |
1733 | mp4_descr[i].es_id == desc_es_id) { |
1734 | AVIOContext pb; |
1735 | ffio_init_context(&pb, mp4_descr[i].dec_config_descr, |
1736 | mp4_descr[i].dec_config_descr_len, 0, |
1737 | NULL, NULL, NULL, NULL); |
1738 | ff_mp4_read_dec_config_descr(fc, st, &pb); |
1739 | if (st->codecpar->codec_id == AV_CODEC_ID_AAC && |
1740 | st->codecpar->extradata_size > 0) { |
1741 | st->need_parsing = 0; |
1742 | st->internal->need_context_update = 1; |
1743 | } |
1744 | if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4SYSTEMS) |
1745 | mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1); |
1746 | } |
1747 | break; |
1748 | case 0x1F: /* FMC descriptor */ |
1749 | if (get16(pp, desc_end) < 0) |
1750 | break; |
1751 | if (mp4_descr_count > 0 && |
1752 | (st->codecpar->codec_id == AV_CODEC_ID_AAC_LATM || |
1753 | (st->request_probe == 0 && st->codecpar->codec_id == AV_CODEC_ID_NONE) || |
1754 | st->request_probe > 0) && |
1755 | mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) { |
1756 | AVIOContext pb; |
1757 | ffio_init_context(&pb, mp4_descr->dec_config_descr, |
1758 | mp4_descr->dec_config_descr_len, 0, |
1759 | NULL, NULL, NULL, NULL); |
1760 | ff_mp4_read_dec_config_descr(fc, st, &pb); |
1761 | if (st->codecpar->codec_id == AV_CODEC_ID_AAC && |
1762 | st->codecpar->extradata_size > 0) { |
1763 | st->request_probe = st->need_parsing = 0; |
1764 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
1765 | st->internal->need_context_update = 1; |
1766 | } |
1767 | } |
1768 | break; |
1769 | case 0x56: /* DVB teletext descriptor */ |
1770 | { |
1771 | uint8_t *extradata = NULL; |
1772 | int language_count = desc_len / 5; |
1773 | |
1774 | if (desc_len > 0 && desc_len % 5 != 0) |
1775 | return AVERROR_INVALIDDATA; |
1776 | |
1777 | if (language_count > 0) { |
1778 | /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */ |
1779 | av_assert0(language_count <= sizeof(language) / 4); |
1780 | |
1781 | if (st->codecpar->extradata == NULL) { |
1782 | if (ff_alloc_extradata(st->codecpar, language_count * 2)) { |
1783 | return AVERROR(ENOMEM); |
1784 | } |
1785 | } |
1786 | |
1787 | if (st->codecpar->extradata_size < language_count * 2) |
1788 | return AVERROR_INVALIDDATA; |
1789 | |
1790 | extradata = st->codecpar->extradata; |
1791 | |
1792 | for (i = 0; i < language_count; i++) { |
1793 | language[i * 4 + 0] = get8(pp, desc_end); |
1794 | language[i * 4 + 1] = get8(pp, desc_end); |
1795 | language[i * 4 + 2] = get8(pp, desc_end); |
1796 | language[i * 4 + 3] = ','; |
1797 | |
1798 | memcpy(extradata, *pp, 2); |
1799 | extradata += 2; |
1800 | |
1801 | *pp += 2; |
1802 | } |
1803 | |
1804 | language[i * 4 - 1] = 0; |
1805 | av_dict_set(&st->metadata, "language", language, 0); |
1806 | st->internal->need_context_update = 1; |
1807 | } |
1808 | } |
1809 | break; |
1810 | case 0x59: /* subtitling descriptor */ |
1811 | { |
1812 | /* 8 bytes per DVB subtitle substream data: |
1813 | * ISO_639_language_code (3 bytes), |
1814 | * subtitling_type (1 byte), |
1815 | * composition_page_id (2 bytes), |
1816 | * ancillary_page_id (2 bytes) */ |
1817 | int language_count = desc_len / 8; |
1818 | |
1819 | if (desc_len > 0 && desc_len % 8 != 0) |
1820 | return AVERROR_INVALIDDATA; |
1821 | |
1822 | if (language_count > 1) { |
1823 | avpriv_request_sample(fc, "DVB subtitles with multiple languages"); |
1824 | } |
1825 | |
1826 | if (language_count > 0) { |
1827 | uint8_t *extradata; |
1828 | |
1829 | /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */ |
1830 | av_assert0(language_count <= sizeof(language) / 4); |
1831 | |
1832 | if (st->codecpar->extradata == NULL) { |
1833 | if (ff_alloc_extradata(st->codecpar, language_count * 5)) { |
1834 | return AVERROR(ENOMEM); |
1835 | } |
1836 | } |
1837 | |
1838 | if (st->codecpar->extradata_size < language_count * 5) |
1839 | return AVERROR_INVALIDDATA; |
1840 | |
1841 | extradata = st->codecpar->extradata; |
1842 | |
1843 | for (i = 0; i < language_count; i++) { |
1844 | language[i * 4 + 0] = get8(pp, desc_end); |
1845 | language[i * 4 + 1] = get8(pp, desc_end); |
1846 | language[i * 4 + 2] = get8(pp, desc_end); |
1847 | language[i * 4 + 3] = ','; |
1848 | |
1849 | /* hearing impaired subtitles detection using subtitling_type */ |
1850 | switch (*pp[0]) { |
1851 | case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */ |
1852 | case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */ |
1853 | case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */ |
1854 | case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */ |
1855 | case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */ |
1856 | case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */ |
1857 | st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; |
1858 | break; |
1859 | } |
1860 | |
1861 | extradata[4] = get8(pp, desc_end); /* subtitling_type */ |
1862 | memcpy(extradata, *pp, 4); /* composition_page_id and ancillary_page_id */ |
1863 | extradata += 5; |
1864 | |
1865 | *pp += 4; |
1866 | } |
1867 | |
1868 | language[i * 4 - 1] = 0; |
1869 | av_dict_set(&st->metadata, "language", language, 0); |
1870 | st->internal->need_context_update = 1; |
1871 | } |
1872 | } |
1873 | break; |
1874 | case 0x0a: /* ISO 639 language descriptor */ |
1875 | for (i = 0; i + 4 <= desc_len; i += 4) { |
1876 | language[i + 0] = get8(pp, desc_end); |
1877 | language[i + 1] = get8(pp, desc_end); |
1878 | language[i + 2] = get8(pp, desc_end); |
1879 | language[i + 3] = ','; |
1880 | switch (get8(pp, desc_end)) { |
1881 | case 0x01: |
1882 | st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; |
1883 | break; |
1884 | case 0x02: |
1885 | st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; |
1886 | break; |
1887 | case 0x03: |
1888 | st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; |
1889 | break; |
1890 | } |
1891 | } |
1892 | if (i && language[0]) { |
1893 | language[i - 1] = 0; |
1894 | av_dict_set(&st->metadata, "language", language, 0); |
1895 | } |
1896 | break; |
1897 | case 0x05: /* registration descriptor */ |
1898 | st->codecpar->codec_tag = bytestream_get_le32(pp); |
1899 | av_log(fc, AV_LOG_TRACE, "reg_desc=%.4s\n", (char *)&st->codecpar->codec_tag); |
1900 | if (st->codecpar->codec_id == AV_CODEC_ID_NONE || st->request_probe > 0) { |
1901 | mpegts_find_stream_type(st, st->codecpar->codec_tag, REGD_types); |
1902 | if (st->codecpar->codec_tag == MKTAG('B', 'S', 'S', 'D')) |
1903 | st->request_probe = 50; |
1904 | } |
1905 | break; |
1906 | case 0x52: /* stream identifier descriptor */ |
1907 | st->stream_identifier = 1 + get8(pp, desc_end); |
1908 | break; |
1909 | case 0x26: /* metadata descriptor */ |
1910 | if (get16(pp, desc_end) == 0xFFFF) |
1911 | *pp += 4; |
1912 | if (get8(pp, desc_end) == 0xFF) { |
1913 | st->codecpar->codec_tag = bytestream_get_le32(pp); |
1914 | if (st->codecpar->codec_id == AV_CODEC_ID_NONE) |
1915 | mpegts_find_stream_type(st, st->codecpar->codec_tag, METADATA_types); |
1916 | } |
1917 | break; |
1918 | case 0x7f: /* DVB extension descriptor */ |
1919 | ext_desc_tag = get8(pp, desc_end); |
1920 | if (ext_desc_tag < 0) |
1921 | return AVERROR_INVALIDDATA; |
1922 | if (st->codecpar->codec_id == AV_CODEC_ID_OPUS && |
1923 | ext_desc_tag == 0x80) { /* User defined (provisional Opus) */ |
1924 | if (!st->codecpar->extradata) { |
1925 | st->codecpar->extradata = av_mallocz(sizeof(opus_default_extradata) + |
1926 | AV_INPUT_BUFFER_PADDING_SIZE); |
1927 | if (!st->codecpar->extradata) |
1928 | return AVERROR(ENOMEM); |
1929 | |
1930 | st->codecpar->extradata_size = sizeof(opus_default_extradata); |
1931 | memcpy(st->codecpar->extradata, opus_default_extradata, sizeof(opus_default_extradata)); |
1932 | |
1933 | channel_config_code = get8(pp, desc_end); |
1934 | if (channel_config_code < 0) |
1935 | return AVERROR_INVALIDDATA; |
1936 | if (channel_config_code <= 0x8) { |
1937 | st->codecpar->extradata[9] = channels = channel_config_code ? channel_config_code : 2; |
1938 | st->codecpar->extradata[18] = channel_config_code ? (channels > 2) : /* Dual Mono */ 255; |
1939 | st->codecpar->extradata[19] = opus_stream_cnt[channel_config_code]; |
1940 | st->codecpar->extradata[20] = opus_coupled_stream_cnt[channel_config_code]; |
1941 | memcpy(&st->codecpar->extradata[21], opus_channel_map[channels - 1], channels); |
1942 | } else { |
1943 | avpriv_request_sample(fc, "Opus in MPEG-TS - channel_config_code > 0x8"); |
1944 | } |
1945 | st->need_parsing = AVSTREAM_PARSE_FULL; |
1946 | st->internal->need_context_update = 1; |
1947 | } |
1948 | } |
1949 | break; |
1950 | default: |
1951 | break; |
1952 | } |
1953 | *pp = desc_end; |
1954 | return 0; |
1955 | } |
1956 | |
1957 | static int is_pes_stream(int stream_type, uint32_t prog_reg_desc) |
1958 | { |
1959 | return !(stream_type == 0x13 || |
1960 | (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) ); |
1961 | } |
1962 | |
1963 | static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len) |
1964 | { |
1965 | MpegTSContext *ts = filter->u.section_filter.opaque; |
1966 | MpegTSSectionFilter *tssf = &filter->u.section_filter; |
1967 | SectionHeader h1, *h = &h1; |
1968 | PESContext *pes; |
1969 | AVStream *st; |
1970 | const uint8_t *p, *p_end, *desc_list_end; |
1971 | int program_info_length, pcr_pid, pid, stream_type; |
1972 | int desc_list_len; |
1973 | uint32_t prog_reg_desc = 0; /* registration descriptor */ |
1974 | |
1975 | int mp4_descr_count = 0; |
1976 | Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } }; |
1977 | int i; |
1978 | |
1979 | av_log(ts->stream, AV_LOG_TRACE, "PMT: len %i\n", section_len); |
1980 | hex_dump_debug(ts->stream, section, section_len); |
1981 | |
1982 | p_end = section + section_len - 4; |
1983 | p = section; |
1984 | if (parse_section_header(h, &p, p_end) < 0) |
1985 | return; |
1986 | if (skip_identical(h, tssf)) |
1987 | return; |
1988 | |
1989 | av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x sec_num=%d/%d version=%d tid=%d\n", |
1990 | h->id, h->sec_num, h->last_sec_num, h->version, h->tid); |
1991 | |
1992 | if (h->tid != PMT_TID) |
1993 | return; |
1994 | if (!ts->scan_all_pmts && ts->skip_changes) |
1995 | return; |
1996 | |
1997 | if (!ts->skip_clear) |
1998 | clear_program(ts, h->id); |
1999 | |
2000 | pcr_pid = get16(&p, p_end); |
2001 | if (pcr_pid < 0) |
2002 | return; |
2003 | pcr_pid &= 0x1fff; |
2004 | add_pid_to_pmt(ts, h->id, pcr_pid); |
2005 | set_pcr_pid(ts->stream, h->id, pcr_pid); |
2006 | |
2007 | av_log(ts->stream, AV_LOG_TRACE, "pcr_pid=0x%x\n", pcr_pid); |
2008 | |
2009 | program_info_length = get16(&p, p_end); |
2010 | if (program_info_length < 0) |
2011 | return; |
2012 | program_info_length &= 0xfff; |
2013 | while (program_info_length >= 2) { |
2014 | uint8_t tag, len; |
2015 | tag = get8(&p, p_end); |
2016 | len = get8(&p, p_end); |
2017 | |
2018 | av_log(ts->stream, AV_LOG_TRACE, "program tag: 0x%02x len=%d\n", tag, len); |
2019 | |
2020 | if (len > program_info_length - 2) |
2021 | // something else is broken, exit the program_descriptors_loop |
2022 | break; |
2023 | program_info_length -= len + 2; |
2024 | if (tag == 0x1d) { // IOD descriptor |
2025 | get8(&p, p_end); // scope |
2026 | get8(&p, p_end); // label |
2027 | len -= 2; |
2028 | mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count, |
2029 | &mp4_descr_count, MAX_MP4_DESCR_COUNT); |
2030 | } else if (tag == 0x05 && len >= 4) { // registration descriptor |
2031 | prog_reg_desc = bytestream_get_le32(&p); |
2032 | len -= 4; |
2033 | } |
2034 | p += len; |
2035 | } |
2036 | p += program_info_length; |
2037 | if (p >= p_end) |
2038 | goto out; |
2039 | |
2040 | // stop parsing after pmt, we found header |
2041 | if (!ts->stream->nb_streams) |
2042 | ts->stop_parse = 2; |
2043 | |
2044 | set_pmt_found(ts, h->id); |
2045 | |
2046 | |
2047 | for (;;) { |
2048 | st = 0; |
2049 | pes = NULL; |
2050 | stream_type = get8(&p, p_end); |
2051 | if (stream_type < 0) |
2052 | break; |
2053 | pid = get16(&p, p_end); |
2054 | if (pid < 0) |
2055 | goto out; |
2056 | pid &= 0x1fff; |
2057 | if (pid == ts->current_pid) |
2058 | goto out; |
2059 | |
2060 | /* now create stream */ |
2061 | if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) { |
2062 | pes = ts->pids[pid]->u.pes_filter.opaque; |
2063 | if (!pes->st) { |
2064 | pes->st = avformat_new_stream(pes->stream, NULL); |
2065 | if (!pes->st) |
2066 | goto out; |
2067 | pes->st->id = pes->pid; |
2068 | } |
2069 | st = pes->st; |
2070 | } else if (is_pes_stream(stream_type, prog_reg_desc)) { |
2071 | if (ts->pids[pid]) |
2072 | mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably |
2073 | pes = add_pes_stream(ts, pid, pcr_pid); |
2074 | if (pes) { |
2075 | st = avformat_new_stream(pes->stream, NULL); |
2076 | if (!st) |
2077 | goto out; |
2078 | st->id = pes->pid; |
2079 | } |
2080 | } else { |
2081 | int idx = ff_find_stream_index(ts->stream, pid); |
2082 | if (idx >= 0) { |
2083 | st = ts->stream->streams[idx]; |
2084 | } else { |
2085 | st = avformat_new_stream(ts->stream, NULL); |
2086 | if (!st) |
2087 | goto out; |
2088 | st->id = pid; |
2089 | st->codecpar->codec_type = AVMEDIA_TYPE_DATA; |
2090 | if (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) { |
2091 | mpegts_find_stream_type(st, stream_type, SCTE_types); |
2092 | mpegts_open_section_filter(ts, pid, scte_data_cb, ts, 1); |
2093 | } |
2094 | } |
2095 | } |
2096 | |
2097 | if (!st) |
2098 | goto out; |
2099 | |
2100 | if (pes && !pes->stream_type) |
2101 | mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc); |
2102 | |
2103 | add_pid_to_pmt(ts, h->id, pid); |
2104 | |
2105 | av_program_add_stream_index(ts->stream, h->id, st->index); |
2106 | |
2107 | desc_list_len = get16(&p, p_end); |
2108 | if (desc_list_len < 0) |
2109 | goto out; |
2110 | desc_list_len &= 0xfff; |
2111 | desc_list_end = p + desc_list_len; |
2112 | if (desc_list_end > p_end) |
2113 | goto out; |
2114 | for (;;) { |
2115 | if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, |
2116 | desc_list_end, mp4_descr, |
2117 | mp4_descr_count, pid, ts) < 0) |
2118 | break; |
2119 | |
2120 | if (pes && prog_reg_desc == AV_RL32("HDMV") && |
2121 | (stream_type == 0x83 || stream_type == 0x81) && pes->sub_st) { |
2122 | av_program_add_stream_index(ts->stream, h->id, |
2123 | pes->sub_st->index); |
2124 | pes->sub_st->codecpar->codec_tag = st->codecpar->codec_tag; |
2125 | } |
2126 | } |
2127 | p = desc_list_end; |
2128 | } |
2129 | |
2130 | if (!ts->pids[pcr_pid]) |
2131 | mpegts_open_pcr_filter(ts, pcr_pid); |
2132 | |
2133 | out: |
2134 | for (i = 0; i < mp4_descr_count; i++) |
2135 | av_free(mp4_descr[i].dec_config_descr); |
2136 | } |
2137 | |
2138 | static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len) |
2139 | { |
2140 | MpegTSContext *ts = filter->u.section_filter.opaque; |
2141 | MpegTSSectionFilter *tssf = &filter->u.section_filter; |
2142 | SectionHeader h1, *h = &h1; |
2143 | const uint8_t *p, *p_end; |
2144 | int sid, pmt_pid; |
2145 | AVProgram *program; |
2146 | |
2147 | av_log(ts->stream, AV_LOG_TRACE, "PAT:\n"); |
2148 | hex_dump_debug(ts->stream, section, section_len); |
2149 | |
2150 | p_end = section + section_len - 4; |
2151 | p = section; |
2152 | if (parse_section_header(h, &p, p_end) < 0) |
2153 | return; |
2154 | if (h->tid != PAT_TID) |
2155 | return; |
2156 | if (ts->skip_changes) |
2157 | return; |
2158 | |
2159 | if (skip_identical(h, tssf)) |
2160 | return; |
2161 | ts->stream->ts_id = h->id; |
2162 | |
2163 | clear_programs(ts); |
2164 | for (;;) { |
2165 | sid = get16(&p, p_end); |
2166 | if (sid < 0) |
2167 | break; |
2168 | pmt_pid = get16(&p, p_end); |
2169 | if (pmt_pid < 0) |
2170 | break; |
2171 | pmt_pid &= 0x1fff; |
2172 | |
2173 | if (pmt_pid == ts->current_pid) |
2174 | break; |
2175 | |
2176 | av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x pid=0x%x\n", sid, pmt_pid); |
2177 | |
2178 | if (sid == 0x0000) { |
2179 | /* NIT info */ |
2180 | } else { |
2181 | MpegTSFilter *fil = ts->pids[pmt_pid]; |
2182 | program = av_new_program(ts->stream, sid); |
2183 | if (program) { |
2184 | program->program_num = sid; |
2185 | program->pmt_pid = pmt_pid; |
2186 | } |
2187 | if (fil) |
2188 | if ( fil->type != MPEGTS_SECTION |
2189 | || fil->pid != pmt_pid |
2190 | || fil->u.section_filter.section_cb != pmt_cb) |
2191 | mpegts_close_filter(ts, ts->pids[pmt_pid]); |
2192 | |
2193 | if (!ts->pids[pmt_pid]) |
2194 | mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1); |
2195 | add_pat_entry(ts, sid); |
2196 | add_pid_to_pmt(ts, sid, 0); // add pat pid to program |
2197 | add_pid_to_pmt(ts, sid, pmt_pid); |
2198 | } |
2199 | } |
2200 | |
2201 | if (sid < 0) { |
2202 | int i,j; |
2203 | for (j=0; j<ts->stream->nb_programs; j++) { |
2204 | for (i = 0; i < ts->nb_prg; i++) |
2205 | if (ts->prg[i].id == ts->stream->programs[j]->id) |
2206 | break; |
2207 | if (i==ts->nb_prg && !ts->skip_clear) |
2208 | clear_avprogram(ts, ts->stream->programs[j]->id); |
2209 | } |
2210 | } |
2211 | } |
2212 | |
2213 | static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len) |
2214 | { |
2215 | MpegTSContext *ts = filter->u.section_filter.opaque; |
2216 | MpegTSSectionFilter *tssf = &filter->u.section_filter; |
2217 | SectionHeader h1, *h = &h1; |
2218 | const uint8_t *p, *p_end, *desc_list_end, *desc_end; |
2219 | int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type; |
2220 | char *name, *provider_name; |
2221 | |
2222 | av_log(ts->stream, AV_LOG_TRACE, "SDT:\n"); |
2223 | hex_dump_debug(ts->stream, section, section_len); |
2224 | |
2225 | p_end = section + section_len - 4; |
2226 | p = section; |
2227 | if (parse_section_header(h, &p, p_end) < 0) |
2228 | return; |
2229 | if (h->tid != SDT_TID) |
2230 | return; |
2231 | if (ts->skip_changes) |
2232 | return; |
2233 | if (skip_identical(h, tssf)) |
2234 | return; |
2235 | |
2236 | onid = get16(&p, p_end); |
2237 | if (onid < 0) |
2238 | return; |
2239 | val = get8(&p, p_end); |
2240 | if (val < 0) |
2241 | return; |
2242 | for (;;) { |
2243 | sid = get16(&p, p_end); |
2244 | if (sid < 0) |
2245 | break; |
2246 | val = get8(&p, p_end); |
2247 | if (val < 0) |
2248 | break; |
2249 | desc_list_len = get16(&p, p_end); |
2250 | if (desc_list_len < 0) |
2251 | break; |
2252 | desc_list_len &= 0xfff; |
2253 | desc_list_end = p + desc_list_len; |
2254 | if (desc_list_end > p_end) |
2255 | break; |
2256 | for (;;) { |
2257 | desc_tag = get8(&p, desc_list_end); |
2258 | if (desc_tag < 0) |
2259 | break; |
2260 | desc_len = get8(&p, desc_list_end); |
2261 | desc_end = p + desc_len; |
2262 | if (desc_len < 0 || desc_end > desc_list_end) |
2263 | break; |
2264 | |
2265 | av_log(ts->stream, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", |
2266 | desc_tag, desc_len); |
2267 | |
2268 | switch (desc_tag) { |
2269 | case 0x48: |
2270 | service_type = get8(&p, p_end); |
2271 | if (service_type < 0) |
2272 | break; |
2273 | provider_name = getstr8(&p, p_end); |
2274 | if (!provider_name) |
2275 | break; |
2276 | name = getstr8(&p, p_end); |
2277 | if (name) { |
2278 | AVProgram *program = av_new_program(ts->stream, sid); |
2279 | if (program) { |
2280 | av_dict_set(&program->metadata, "service_name", name, 0); |
2281 | av_dict_set(&program->metadata, "service_provider", |
2282 | provider_name, 0); |
2283 | } |
2284 | } |
2285 | av_free(name); |
2286 | av_free(provider_name); |
2287 | break; |
2288 | default: |
2289 | break; |
2290 | } |
2291 | p = desc_end; |
2292 | } |
2293 | p = desc_list_end; |
2294 | } |
2295 | } |
2296 | |
2297 | static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, |
2298 | const uint8_t *packet); |
2299 | |
2300 | /* handle one TS packet */ |
2301 | static int handle_packet(MpegTSContext *ts, const uint8_t *packet) |
2302 | { |
2303 | MpegTSFilter *tss; |
2304 | int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity, |
2305 | has_adaptation, has_payload; |
2306 | const uint8_t *p, *p_end; |
2307 | int64_t pos; |
2308 | |
2309 | pid = AV_RB16(packet + 1) & 0x1fff; |
2310 | if (pid && discard_pid(ts, pid)) |
2311 | return 0; |
2312 | is_start = packet[1] & 0x40; |
2313 | tss = ts->pids[pid]; |
2314 | if (ts->auto_guess && !tss && is_start) { |
2315 | add_pes_stream(ts, pid, -1); |
2316 | tss = ts->pids[pid]; |
2317 | } |
2318 | if (!tss) |
2319 | return 0; |
2320 | ts->current_pid = pid; |
2321 | |
2322 | afc = (packet[3] >> 4) & 3; |
2323 | if (afc == 0) /* reserved value */ |
2324 | return 0; |
2325 | has_adaptation = afc & 2; |
2326 | has_payload = afc & 1; |
2327 | is_discontinuity = has_adaptation && |
2328 | packet[4] != 0 && /* with length > 0 */ |
2329 | (packet[5] & 0x80); /* and discontinuity indicated */ |
2330 | |
2331 | /* continuity check (currently not used) */ |
2332 | cc = (packet[3] & 0xf); |
2333 | expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc; |
2334 | cc_ok = pid == 0x1FFF || // null packet PID |
2335 | is_discontinuity || |
2336 | tss->last_cc < 0 || |
2337 | expected_cc == cc; |
2338 | |
2339 | tss->last_cc = cc; |
2340 | if (!cc_ok) { |
2341 | av_log(ts->stream, AV_LOG_DEBUG, |
2342 | "Continuity check failed for pid %d expected %d got %d\n", |
2343 | pid, expected_cc, cc); |
2344 | if (tss->type == MPEGTS_PES) { |
2345 | PESContext *pc = tss->u.pes_filter.opaque; |
2346 | pc->flags |= AV_PKT_FLAG_CORRUPT; |
2347 | } |
2348 | } |
2349 | |
2350 | p = packet + 4; |
2351 | if (has_adaptation) { |
2352 | int64_t pcr_h; |
2353 | int pcr_l; |
2354 | if (parse_pcr(&pcr_h, &pcr_l, packet) == 0) |
2355 | tss->last_pcr = pcr_h * 300 + pcr_l; |
2356 | /* skip adaptation field */ |
2357 | p += p[0] + 1; |
2358 | } |
2359 | /* if past the end of packet, ignore */ |
2360 | p_end = packet + TS_PACKET_SIZE; |
2361 | if (p >= p_end || !has_payload) |
2362 | return 0; |
2363 | |
2364 | pos = avio_tell(ts->stream->pb); |
2365 | if (pos >= 0) { |
2366 | av_assert0(pos >= TS_PACKET_SIZE); |
2367 | ts->pos47_full = pos - TS_PACKET_SIZE; |
2368 | } |
2369 | |
2370 | if (tss->type == MPEGTS_SECTION) { |
2371 | if (is_start) { |
2372 | /* pointer field present */ |
2373 | len = *p++; |
2374 | if (len > p_end - p) |
2375 | return 0; |
2376 | if (len && cc_ok) { |
2377 | /* write remaining section bytes */ |
2378 | write_section_data(ts, tss, |
2379 | p, len, 0); |
2380 | /* check whether filter has been closed */ |
2381 | if (!ts->pids[pid]) |
2382 | return 0; |
2383 | } |
2384 | p += len; |
2385 | if (p < p_end) { |
2386 | write_section_data(ts, tss, |
2387 | p, p_end - p, 1); |
2388 | } |
2389 | } else { |
2390 | if (cc_ok) { |
2391 | write_section_data(ts, tss, |
2392 | p, p_end - p, 0); |
2393 | } |
2394 | } |
2395 | |
2396 | // stop find_stream_info from waiting for more streams |
2397 | // when all programs have received a PMT |
2398 | if (ts->stream->ctx_flags & AVFMTCTX_NOHEADER && ts->scan_all_pmts <= 0) { |
2399 | int i; |
2400 | for (i = 0; i < ts->nb_prg; i++) { |
2401 | if (!ts->prg[i].pmt_found) |
2402 | break; |
2403 | } |
2404 | if (i == ts->nb_prg && ts->nb_prg > 0) { |
2405 | int types = 0; |
2406 | for (i = 0; i < ts->stream->nb_streams; i++) { |
2407 | AVStream *st = ts->stream->streams[i]; |
2408 | if (st->codecpar->codec_type >= 0) |
2409 | types |= 1<<st->codecpar->codec_type; |
2410 | } |
2411 | if ((types & (1<<AVMEDIA_TYPE_AUDIO) && types & (1<<AVMEDIA_TYPE_VIDEO)) || pos > 100000) { |
2412 | av_log(ts->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n"); |
2413 | ts->stream->ctx_flags &= ~AVFMTCTX_NOHEADER; |
2414 | } |
2415 | } |
2416 | } |
2417 | |
2418 | } else { |
2419 | int ret; |
2420 | // Note: The position here points actually behind the current packet. |
2421 | if (tss->type == MPEGTS_PES) { |
2422 | if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start, |
2423 | pos - ts->raw_packet_size)) < 0) |
2424 | return ret; |
2425 | } |
2426 | } |
2427 | |
2428 | return 0; |
2429 | } |
2430 | |
2431 | static void reanalyze(MpegTSContext *ts) { |
2432 | AVIOContext *pb = ts->stream->pb; |
2433 | int64_t pos = avio_tell(pb); |
2434 | if (pos < 0) |
2435 | return; |
2436 | pos -= ts->pos47_full; |
2437 | if (pos == TS_PACKET_SIZE) { |
2438 | ts->size_stat[0] ++; |
2439 | } else if (pos == TS_DVHS_PACKET_SIZE) { |
2440 | ts->size_stat[1] ++; |
2441 | } else if (pos == TS_FEC_PACKET_SIZE) { |
2442 | ts->size_stat[2] ++; |
2443 | } |
2444 | |
2445 | ts->size_stat_count ++; |
2446 | if (ts->size_stat_count > SIZE_STAT_THRESHOLD) { |
2447 | int newsize = 0; |
2448 | if (ts->size_stat[0] > SIZE_STAT_THRESHOLD) { |
2449 | newsize = TS_PACKET_SIZE; |
2450 | } else if (ts->size_stat[1] > SIZE_STAT_THRESHOLD) { |
2451 | newsize = TS_DVHS_PACKET_SIZE; |
2452 | } else if (ts->size_stat[2] > SIZE_STAT_THRESHOLD) { |
2453 | newsize = TS_FEC_PACKET_SIZE; |
2454 | } |
2455 | if (newsize && newsize != ts->raw_packet_size) { |
2456 | av_log(ts->stream, AV_LOG_WARNING, "changing packet size to %d\n", newsize); |
2457 | ts->raw_packet_size = newsize; |
2458 | } |
2459 | ts->size_stat_count = 0; |
2460 | memset(ts->size_stat, 0, sizeof(ts->size_stat)); |
2461 | } |
2462 | } |
2463 | |
2464 | /* XXX: try to find a better synchro over several packets (use |
2465 | * get_packet_size() ?) */ |
2466 | static int mpegts_resync(AVFormatContext *s, int seekback, const uint8_t *current_packet) |
2467 | { |
2468 | MpegTSContext *ts = s->priv_data; |
2469 | AVIOContext *pb = s->pb; |
2470 | int c, i; |
2471 | uint64_t pos = avio_tell(pb); |
2472 | |
2473 | avio_seek(pb, -FFMIN(seekback, pos), SEEK_CUR); |
2474 | |
2475 | //Special case for files like 01c56b0dc1.ts |
2476 | if (current_packet[0] == 0x80 && current_packet[12] == 0x47) { |
2477 | avio_seek(pb, 12, SEEK_CUR); |
2478 | return 0; |
2479 | } |
2480 | |
2481 | for (i = 0; i < ts->resync_size; i++) { |
2482 | c = avio_r8(pb); |
2483 | if (avio_feof(pb)) |
2484 | return AVERROR_EOF; |
2485 | if (c == 0x47) { |
2486 | avio_seek(pb, -1, SEEK_CUR); |
2487 | reanalyze(s->priv_data); |
2488 | return 0; |
2489 | } |
2490 | } |
2491 | av_log(s, AV_LOG_ERROR, |
2492 | "max resync size reached, could not find sync byte\n"); |
2493 | /* no sync found */ |
2494 | return AVERROR_INVALIDDATA; |
2495 | } |
2496 | |
2497 | /* return AVERROR_something if error or EOF. Return 0 if OK. */ |
2498 | static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, |
2499 | const uint8_t **data) |
2500 | { |
2501 | AVIOContext *pb = s->pb; |
2502 | int len; |
2503 | |
2504 | for (;;) { |
2505 | len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data); |
2506 | if (len != TS_PACKET_SIZE) |
2507 | return len < 0 ? len : AVERROR_EOF; |
2508 | /* check packet sync byte */ |
2509 | if ((*data)[0] != 0x47) { |
2510 | /* find a new packet start */ |
2511 | |
2512 | if (mpegts_resync(s, raw_packet_size, *data) < 0) |
2513 | return AVERROR(EAGAIN); |
2514 | else |
2515 | continue; |
2516 | } else { |
2517 | break; |
2518 | } |
2519 | } |
2520 | return 0; |
2521 | } |
2522 | |
2523 | static void finished_reading_packet(AVFormatContext *s, int raw_packet_size) |
2524 | { |
2525 | AVIOContext *pb = s->pb; |
2526 | int skip = raw_packet_size - TS_PACKET_SIZE; |
2527 | if (skip > 0) |
2528 | avio_skip(pb, skip); |
2529 | } |
2530 | |
2531 | static int handle_packets(MpegTSContext *ts, int64_t nb_packets) |
2532 | { |
2533 | AVFormatContext *s = ts->stream; |
2534 | uint8_t packet[TS_PACKET_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; |
2535 | const uint8_t *data; |
2536 | int64_t packet_num; |
2537 | int ret = 0; |
2538 | |
2539 | if (avio_tell(s->pb) != ts->last_pos) { |
2540 | int i; |
2541 | av_log(ts->stream, AV_LOG_TRACE, "Skipping after seek\n"); |
2542 | /* seek detected, flush pes buffer */ |
2543 | for (i = 0; i < NB_PID_MAX; i++) { |
2544 | if (ts->pids[i]) { |
2545 | if (ts->pids[i]->type == MPEGTS_PES) { |
2546 | PESContext *pes = ts->pids[i]->u.pes_filter.opaque; |
2547 | av_buffer_unref(&pes->buffer); |
2548 | pes->data_index = 0; |
2549 | pes->state = MPEGTS_SKIP; /* skip until pes header */ |
2550 | } else if (ts->pids[i]->type == MPEGTS_SECTION) { |
2551 | ts->pids[i]->u.section_filter.last_ver = -1; |
2552 | } |
2553 | ts->pids[i]->last_cc = -1; |
2554 | ts->pids[i]->last_pcr = -1; |
2555 | } |
2556 | } |
2557 | } |
2558 | |
2559 | ts->stop_parse = 0; |
2560 | packet_num = 0; |
2561 | memset(packet + TS_PACKET_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
2562 | for (;;) { |
2563 | packet_num++; |
2564 | if (nb_packets != 0 && packet_num >= nb_packets || |
2565 | ts->stop_parse > 1) { |
2566 | ret = AVERROR(EAGAIN); |
2567 | break; |
2568 | } |
2569 | if (ts->stop_parse > 0) |
2570 | break; |
2571 | |
2572 | ret = read_packet(s, packet, ts->raw_packet_size, &data); |
2573 | if (ret != 0) |
2574 | break; |
2575 | ret = handle_packet(ts, data); |
2576 | finished_reading_packet(s, ts->raw_packet_size); |
2577 | if (ret != 0) |
2578 | break; |
2579 | } |
2580 | ts->last_pos = avio_tell(s->pb); |
2581 | return ret; |
2582 | } |
2583 | |
2584 | static int mpegts_probe(AVProbeData *p) |
2585 | { |
2586 | const int size = p->buf_size; |
2587 | int maxscore = 0; |
2588 | int sumscore = 0; |
2589 | int i; |
2590 | int check_count = size / TS_FEC_PACKET_SIZE; |
2591 | #define CHECK_COUNT 10 |
2592 | #define CHECK_BLOCK 100 |
2593 | |
2594 | if (!check_count) |
2595 | return 0; |
2596 | |
2597 | for (i = 0; i<check_count; i+=CHECK_BLOCK) { |
2598 | int left = FFMIN(check_count - i, CHECK_BLOCK); |
2599 | int score = analyze(p->buf + TS_PACKET_SIZE *i, TS_PACKET_SIZE *left, TS_PACKET_SIZE , 1); |
2600 | int dvhs_score = analyze(p->buf + TS_DVHS_PACKET_SIZE*i, TS_DVHS_PACKET_SIZE*left, TS_DVHS_PACKET_SIZE, 1); |
2601 | int fec_score = analyze(p->buf + TS_FEC_PACKET_SIZE *i, TS_FEC_PACKET_SIZE *left, TS_FEC_PACKET_SIZE , 1); |
2602 | score = FFMAX3(score, dvhs_score, fec_score); |
2603 | sumscore += score; |
2604 | maxscore = FFMAX(maxscore, score); |
2605 | } |
2606 | |
2607 | sumscore = sumscore * CHECK_COUNT / check_count; |
2608 | maxscore = maxscore * CHECK_COUNT / CHECK_BLOCK; |
2609 | |
2610 | ff_dlog(0, "TS score: %d %d\n", sumscore, maxscore); |
2611 | |
2612 | if (check_count > CHECK_COUNT && sumscore > 6) { |
2613 | return AVPROBE_SCORE_MAX + sumscore - CHECK_COUNT; |
2614 | } else if (check_count >= CHECK_COUNT && sumscore > 6) { |
2615 | return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT; |
2616 | } else if (check_count >= CHECK_COUNT && maxscore > 6) { |
2617 | return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT; |
2618 | } else if (sumscore > 6) { |
2619 | return 2; |
2620 | } else { |
2621 | return 0; |
2622 | } |
2623 | } |
2624 | |
2625 | /* return the 90kHz PCR and the extension for the 27MHz PCR. return |
2626 | * (-1) if not available */ |
2627 | static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet) |
2628 | { |
2629 | int afc, len, flags; |
2630 | const uint8_t *p; |
2631 | unsigned int v; |
2632 | |
2633 | afc = (packet[3] >> 4) & 3; |
2634 | if (afc <= 1) |
2635 | return AVERROR_INVALIDDATA; |
2636 | p = packet + 4; |
2637 | len = p[0]; |
2638 | p++; |
2639 | if (len == 0) |
2640 | return AVERROR_INVALIDDATA; |
2641 | flags = *p++; |
2642 | len--; |
2643 | if (!(flags & 0x10)) |
2644 | return AVERROR_INVALIDDATA; |
2645 | if (len < 6) |
2646 | return AVERROR_INVALIDDATA; |
2647 | v = AV_RB32(p); |
2648 | *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7); |
2649 | *ppcr_low = ((p[4] & 1) << 8) | p[5]; |
2650 | return 0; |
2651 | } |
2652 | |
2653 | static void check_ac3_dts(AVFormatContext * s) |
2654 | { |
2655 | |
2656 | MpegTSContext *ts = s->priv_data; |
2657 | int s_index; |
2658 | const uint8_t *data; |
2659 | uint8_t packet[TS_PACKET_SIZE]; |
2660 | for (s_index = 0; s_index < s->nb_streams; s_index++) { |
2661 | int codec_id = s->streams[s_index]->codecpar->codec_id; |
2662 | int c_pid = s->streams[s_index]->id; |
2663 | if (codec_id == AV_CODEC_ID_AC3) { |
2664 | int packet_num, ret; |
2665 | int pes_header_pos, es_header_pos; |
2666 | packet_num = 0; |
2667 | for (;;) { |
2668 | packet_num++; |
2669 | if (packet_num >= 180000) { |
2670 | break; |
2671 | } |
2672 | ret = read_packet(s, packet, ts->raw_packet_size,&data); |
2673 | if (ret != 0) { |
2674 | return; |
2675 | } |
2676 | if ((c_pid != (AV_RB16(data + 1) & 0x1fff)) && (1 != (data[1] & 0x40))) { //pid equal and must have pes/es header |
2677 | continue; |
2678 | } |
2679 | //seek to the pes header |
2680 | for (pes_header_pos = 0; pes_header_pos < ts->raw_packet_size - 3; pes_header_pos++) { |
2681 | if (data[pes_header_pos] == 0x00 && data[pes_header_pos + 1] == 0x00 && data[pes_header_pos + 2] == 0x01) { |
2682 | break; |
2683 | } |
2684 | } |
2685 | if (pes_header_pos == ts->raw_packet_size - 3) { |
2686 | continue; |
2687 | } |
2688 | //we found the pes header and parse |
2689 | es_header_pos = data[pes_header_pos + 8] + 9 + pes_header_pos; //pes header size |
2690 | if (data[es_header_pos] == 0x1F && data[es_header_pos + 1] == 0xFF && data[es_header_pos + 2] == 0xE8 && data[es_header_pos] == 0x00) { |
2691 | s->streams[s_index]->codecpar->codec_id = AV_CODEC_ID_DTS; |
2692 | } |
2693 | if (data[es_header_pos] == 0xFF && data[es_header_pos + 1] == 0x1F && data[es_header_pos + 2] == 0x00 && data[es_header_pos + 3] == 0xE8) { |
2694 | s->streams[s_index]->codecpar->codec_id = AV_CODEC_ID_DTS; |
2695 | } |
2696 | if (data[es_header_pos] == 0x7F && data[es_header_pos + 1] == 0xFE && data[es_header_pos + 2] == 0x80 && data[es_header_pos + 3] == 0x01) { |
2697 | s->streams[s_index]->codecpar->codec_id = AV_CODEC_ID_DTS; |
2698 | } |
2699 | if (data[es_header_pos] == 0xFE && data[es_header_pos + 1] == 0x7F && data[es_header_pos + 2] == 0x01 && data[es_header_pos + 3] == 0x80) { |
2700 | s->streams[s_index]->codecpar->codec_id = AV_CODEC_ID_DTS; |
2701 | } |
2702 | |
2703 | break; |
2704 | } |
2705 | |
2706 | } |
2707 | } |
2708 | } |
2709 | |
2710 | |
2711 | static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos) { |
2712 | |
2713 | /* NOTE: We attempt to seek on non-seekable files as well, as the |
2714 | * probe buffer usually is big enough. Only warn if the seek failed |
2715 | * on files where the seek should work. */ |
2716 | if (avio_seek(pb, pos, SEEK_SET) < 0) |
2717 | av_log(s, (pb->seekable & AVIO_SEEKABLE_NORMAL) ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n"); |
2718 | } |
2719 | |
2720 | static int mpegts_read_header(AVFormatContext *s) |
2721 | { |
2722 | MpegTSContext *ts = s->priv_data; |
2723 | AVIOContext *pb = s->pb; |
2724 | uint8_t buf[8 * 1024] = {0}; |
2725 | int len; |
2726 | int64_t pos, probesize = s->probesize; |
2727 | |
2728 | if (ffio_ensure_seekback(pb, probesize) < 0) |
2729 | av_log(s, AV_LOG_WARNING, "Failed to allocate buffers for seekback\n"); |
2730 | |
2731 | /* read the first 8192 bytes to get packet size */ |
2732 | pos = avio_tell(pb); |
2733 | len = avio_read(pb, buf, sizeof(buf)); |
2734 | ts->raw_packet_size = get_packet_size(buf, len); |
2735 | if (ts->raw_packet_size <= 0) { |
2736 | av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n"); |
2737 | ts->raw_packet_size = TS_PACKET_SIZE; |
2738 | } |
2739 | ts->stream = s; |
2740 | ts->auto_guess = 0; |
2741 | |
2742 | if (s->iformat == &ff_mpegts_demuxer) { |
2743 | /* normal demux */ |
2744 | |
2745 | /* first do a scan to get all the services */ |
2746 | seek_back(s, pb, pos); |
2747 | |
2748 | mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1); |
2749 | |
2750 | mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1); |
2751 | |
2752 | handle_packets(ts, probesize / ts->raw_packet_size); |
2753 | /* if could not find service, enable auto_guess */ |
2754 | |
2755 | ts->auto_guess = 1; |
2756 | |
2757 | av_log(ts->stream, AV_LOG_TRACE, "tuning done\n"); |
2758 | |
2759 | s->ctx_flags |= AVFMTCTX_NOHEADER; |
2760 | } else { |
2761 | AVStream *st; |
2762 | int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l; |
2763 | int64_t pcrs[2], pcr_h; |
2764 | int packet_count[2]; |
2765 | uint8_t packet[TS_PACKET_SIZE]; |
2766 | const uint8_t *data; |
2767 | |
2768 | /* only read packets */ |
2769 | |
2770 | st = avformat_new_stream(s, NULL); |
2771 | if (!st) |
2772 | return AVERROR(ENOMEM); |
2773 | avpriv_set_pts_info(st, 60, 1, 27000000); |
2774 | st->codecpar->codec_type = AVMEDIA_TYPE_DATA; |
2775 | st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS; |
2776 | |
2777 | /* we iterate until we find two PCRs to estimate the bitrate */ |
2778 | pcr_pid = -1; |
2779 | nb_pcrs = 0; |
2780 | nb_packets = 0; |
2781 | for (;;) { |
2782 | ret = read_packet(s, packet, ts->raw_packet_size, &data); |
2783 | if (ret < 0) |
2784 | return ret; |
2785 | pid = AV_RB16(data + 1) & 0x1fff; |
2786 | if ((pcr_pid == -1 || pcr_pid == pid) && |
2787 | parse_pcr(&pcr_h, &pcr_l, data) == 0) { |
2788 | finished_reading_packet(s, ts->raw_packet_size); |
2789 | pcr_pid = pid; |
2790 | packet_count[nb_pcrs] = nb_packets; |
2791 | pcrs[nb_pcrs] = pcr_h * 300 + pcr_l; |
2792 | nb_pcrs++; |
2793 | if (nb_pcrs >= 2) { |
2794 | if (pcrs[1] - pcrs[0] > 0) { |
2795 | /* the difference needs to be positive to make sense for bitrate computation */ |
2796 | break; |
2797 | } else { |
2798 | av_log(ts->stream, AV_LOG_WARNING, "invalid pcr pair %"PRId64" >= %"PRId64"\n", pcrs[0], pcrs[1]); |
2799 | pcrs[0] = pcrs[1]; |
2800 | packet_count[0] = packet_count[1]; |
2801 | nb_pcrs--; |
2802 | } |
2803 | } |
2804 | } else { |
2805 | finished_reading_packet(s, ts->raw_packet_size); |
2806 | } |
2807 | nb_packets++; |
2808 | } |
2809 | |
2810 | /* NOTE1: the bitrate is computed without the FEC */ |
2811 | /* NOTE2: it is only the bitrate of the start of the stream */ |
2812 | ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]); |
2813 | ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0]; |
2814 | s->bit_rate = TS_PACKET_SIZE * 8 * 27000000LL / ts->pcr_incr; |
2815 | st->codecpar->bit_rate = s->bit_rate; |
2816 | st->start_time = ts->cur_pcr; |
2817 | av_log(ts->stream, AV_LOG_TRACE, "start=%0.3f pcr=%0.3f incr=%d\n", |
2818 | st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr); |
2819 | } |
2820 | |
2821 | check_ac3_dts(s); |
2822 | seek_back(s, pb, pos); |
2823 | return 0; |
2824 | } |
2825 | |
2826 | #define MAX_PACKET_READAHEAD ((128 * 1024) / 188) |
2827 | |
2828 | static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt) |
2829 | { |
2830 | MpegTSContext *ts = s->priv_data; |
2831 | int ret, i; |
2832 | int64_t pcr_h, next_pcr_h, pos; |
2833 | int pcr_l, next_pcr_l; |
2834 | uint8_t pcr_buf[12]; |
2835 | const uint8_t *data; |
2836 | |
2837 | if (av_new_packet(pkt, TS_PACKET_SIZE) < 0) |
2838 | return AVERROR(ENOMEM); |
2839 | ret = read_packet(s, pkt->data, ts->raw_packet_size, &data); |
2840 | pkt->pos = avio_tell(s->pb); |
2841 | if (ret < 0) { |
2842 | av_packet_unref(pkt); |
2843 | return ret; |
2844 | } |
2845 | if (data != pkt->data) |
2846 | memcpy(pkt->data, data, ts->raw_packet_size); |
2847 | finished_reading_packet(s, ts->raw_packet_size); |
2848 | if (ts->mpeg2ts_compute_pcr) { |
2849 | /* compute exact PCR for each packet */ |
2850 | if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) { |
2851 | /* we read the next PCR (XXX: optimize it by using a bigger buffer */ |
2852 | pos = avio_tell(s->pb); |
2853 | for (i = 0; i < MAX_PACKET_READAHEAD; i++) { |
2854 | avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET); |
2855 | avio_read(s->pb, pcr_buf, 12); |
2856 | if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) { |
2857 | /* XXX: not precise enough */ |
2858 | ts->pcr_incr = |
2859 | ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) / |
2860 | (i + 1); |
2861 | break; |
2862 | } |
2863 | } |
2864 | avio_seek(s->pb, pos, SEEK_SET); |
2865 | /* no next PCR found: we use previous increment */ |
2866 | ts->cur_pcr = pcr_h * 300 + pcr_l; |
2867 | } |
2868 | pkt->pts = ts->cur_pcr; |
2869 | pkt->duration = ts->pcr_incr; |
2870 | ts->cur_pcr += ts->pcr_incr; |
2871 | } |
2872 | pkt->stream_index = 0; |
2873 | return 0; |
2874 | } |
2875 | |
2876 | static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt) |
2877 | { |
2878 | MpegTSContext *ts = s->priv_data; |
2879 | int ret, i; |
2880 | |
2881 | pkt->size = -1; |
2882 | ts->pkt = pkt; |
2883 | ret = handle_packets(ts, 0); |
2884 | if (ret < 0) { |
2885 | av_packet_unref(ts->pkt); |
2886 | /* flush pes data left */ |
2887 | for (i = 0; i < NB_PID_MAX; i++) |
2888 | if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) { |
2889 | PESContext *pes = ts->pids[i]->u.pes_filter.opaque; |
2890 | if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { |
2891 | ret = new_pes_packet(pes, pkt); |
2892 | if (ret < 0) |
2893 | return ret; |
2894 | pes->state = MPEGTS_SKIP; |
2895 | ret = 0; |
2896 | break; |
2897 | } |
2898 | } |
2899 | } |
2900 | |
2901 | if (!ret && pkt->size < 0) |
2902 | ret = AVERROR_INVALIDDATA; |
2903 | return ret; |
2904 | } |
2905 | |
2906 | static void mpegts_free(MpegTSContext *ts) |
2907 | { |
2908 | int i; |
2909 | |
2910 | clear_programs(ts); |
2911 | |
2912 | for (i = 0; i < NB_PID_MAX; i++) |
2913 | if (ts->pids[i]) |
2914 | mpegts_close_filter(ts, ts->pids[i]); |
2915 | } |
2916 | |
2917 | static int mpegts_read_close(AVFormatContext *s) |
2918 | { |
2919 | MpegTSContext *ts = s->priv_data; |
2920 | mpegts_free(ts); |
2921 | return 0; |
2922 | } |
2923 | |
2924 | static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index, |
2925 | int64_t *ppos, int64_t pos_limit) |
2926 | { |
2927 | MpegTSContext *ts = s->priv_data; |
2928 | int64_t pos, timestamp; |
2929 | uint8_t buf[TS_PACKET_SIZE]; |
2930 | int pcr_l, pcr_pid = |
2931 | ((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid; |
2932 | int pos47 = ts->pos47_full % ts->raw_packet_size; |
2933 | pos = |
2934 | ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * |
2935 | ts->raw_packet_size + pos47; |
2936 | while(pos < pos_limit) { |
2937 | if (avio_seek(s->pb, pos, SEEK_SET) < 0) |
2938 | return AV_NOPTS_VALUE; |
2939 | if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE) |
2940 | return AV_NOPTS_VALUE; |
2941 | if (buf[0] != 0x47) { |
2942 | if (mpegts_resync(s, TS_PACKET_SIZE, buf) < 0) |
2943 | return AV_NOPTS_VALUE; |
2944 | pos = avio_tell(s->pb); |
2945 | continue; |
2946 | } |
2947 | if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) && |
2948 | parse_pcr(×tamp, &pcr_l, buf) == 0) { |
2949 | *ppos = pos; |
2950 | return timestamp; |
2951 | } |
2952 | pos += ts->raw_packet_size; |
2953 | } |
2954 | |
2955 | return AV_NOPTS_VALUE; |
2956 | } |
2957 | |
2958 | static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index, |
2959 | int64_t *ppos, int64_t pos_limit) |
2960 | { |
2961 | MpegTSContext *ts = s->priv_data; |
2962 | int64_t pos; |
2963 | int pos47 = ts->pos47_full % ts->raw_packet_size; |
2964 | pos = ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * ts->raw_packet_size + pos47; |
2965 | ff_read_frame_flush(s); |
2966 | if (avio_seek(s->pb, pos, SEEK_SET) < 0) |
2967 | return AV_NOPTS_VALUE; |
2968 | while(pos < pos_limit) { |
2969 | int ret; |
2970 | AVPacket pkt; |
2971 | av_init_packet(&pkt); |
2972 | ret = av_read_frame(s, &pkt); |
2973 | if (ret < 0) |
2974 | return AV_NOPTS_VALUE; |
2975 | if (pkt.dts != AV_NOPTS_VALUE && pkt.pos >= 0) { |
2976 | ff_reduce_index(s, pkt.stream_index); |
2977 | av_add_index_entry(s->streams[pkt.stream_index], pkt.pos, pkt.dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */); |
2978 | if (pkt.stream_index == stream_index && pkt.pos >= *ppos) { |
2979 | int64_t dts = pkt.dts; |
2980 | *ppos = pkt.pos; |
2981 | av_packet_unref(&pkt); |
2982 | return dts; |
2983 | } |
2984 | } |
2985 | pos = pkt.pos; |
2986 | av_packet_unref(&pkt); |
2987 | } |
2988 | |
2989 | return AV_NOPTS_VALUE; |
2990 | } |
2991 | |
2992 | /**************************************************************/ |
2993 | /* parsing functions - called from other demuxers such as RTP */ |
2994 | |
2995 | MpegTSContext *avpriv_mpegts_parse_open(AVFormatContext *s) |
2996 | { |
2997 | MpegTSContext *ts; |
2998 | |
2999 | ts = av_mallocz(sizeof(MpegTSContext)); |
3000 | if (!ts) |
3001 | return NULL; |
3002 | /* no stream case, currently used by RTP */ |
3003 | ts->raw_packet_size = TS_PACKET_SIZE; |
3004 | ts->stream = s; |
3005 | ts->auto_guess = 1; |
3006 | mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1); |
3007 | mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1); |
3008 | |
3009 | return ts; |
3010 | } |
3011 | |
3012 | /* return the consumed length if a packet was output, or -1 if no |
3013 | * packet is output */ |
3014 | int avpriv_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt, |
3015 | const uint8_t *buf, int len) |
3016 | { |
3017 | int len1; |
3018 | |
3019 | len1 = len; |
3020 | ts->pkt = pkt; |
3021 | for (;;) { |
3022 | ts->stop_parse = 0; |
3023 | if (len < TS_PACKET_SIZE) |
3024 | return AVERROR_INVALIDDATA; |
3025 | if (buf[0] != 0x47) { |
3026 | buf++; |
3027 | len--; |
3028 | } else { |
3029 | handle_packet(ts, buf); |
3030 | buf += TS_PACKET_SIZE; |
3031 | len -= TS_PACKET_SIZE; |
3032 | if (ts->stop_parse == 1) |
3033 | break; |
3034 | } |
3035 | } |
3036 | return len1 - len; |
3037 | } |
3038 | |
3039 | void avpriv_mpegts_parse_close(MpegTSContext *ts) |
3040 | { |
3041 | mpegts_free(ts); |
3042 | av_free(ts); |
3043 | } |
3044 | |
3045 | AVInputFormat ff_mpegts_demuxer = { |
3046 | .name = "mpegts", |
3047 | .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"), |
3048 | .priv_data_size = sizeof(MpegTSContext), |
3049 | .read_probe = mpegts_probe, |
3050 | .read_header = mpegts_read_header, |
3051 | .read_packet = mpegts_read_packet, |
3052 | .read_close = mpegts_read_close, |
3053 | .read_timestamp = mpegts_get_dts, |
3054 | .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT, |
3055 | .priv_class = &mpegts_class, |
3056 | }; |
3057 | |
3058 | AVInputFormat ff_mpegtsraw_demuxer = { |
3059 | .name = "mpegtsraw", |
3060 | .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"), |
3061 | .priv_data_size = sizeof(MpegTSContext), |
3062 | .read_header = mpegts_read_header, |
3063 | .read_packet = mpegts_raw_read_packet, |
3064 | .read_close = mpegts_read_close, |
3065 | .read_timestamp = mpegts_get_dts, |
3066 | .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT, |
3067 | .priv_class = &mpegtsraw_class, |
3068 | }; |
3069 |