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.

3401 lines
114KB

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