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