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.

2398 lines
77KB

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