You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3364 lines
112KB

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