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.

2404 lines
77KB

  1. /*
  2. * MPEG-2 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 synchronization if
  38. * synchronization 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 synchronization.", 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. { 0x21, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_JPEG2000 },
  507. { 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
  508. { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS },
  509. { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
  510. { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
  511. { 0 },
  512. };
  513. static const StreamType HDMV_types[] = {
  514. { 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY },
  515. { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  516. { 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  517. { 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD },
  518. { 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
  519. { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
  520. { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
  521. { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
  522. { 0 },
  523. };
  524. /* ATSC ? */
  525. static const StreamType MISC_types[] = {
  526. { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  527. { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  528. { 0 },
  529. };
  530. static const StreamType REGD_types[] = {
  531. { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
  532. { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  533. { MKTAG('B', 'S', 'S', 'D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
  534. { MKTAG('D', 'T', 'S', '1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  535. { MKTAG('D', 'T', 'S', '2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  536. { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  537. { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
  538. { MKTAG('V', 'C', '-', '1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
  539. { MKTAG('O', 'p', 'u', 's'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_OPUS },
  540. { 0 },
  541. };
  542. /* descriptor present */
  543. static const StreamType DESC_types[] = {
  544. { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
  545. { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
  546. { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  547. { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
  548. { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
  549. { 0 },
  550. };
  551. static void mpegts_find_stream_type(AVStream *st,
  552. uint32_t stream_type,
  553. const StreamType *types)
  554. {
  555. for (; types->stream_type; types++)
  556. if (stream_type == types->stream_type) {
  557. st->codecpar->codec_type = types->codec_type;
  558. st->codecpar->codec_id = types->codec_id;
  559. return;
  560. }
  561. }
  562. static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
  563. uint32_t stream_type, uint32_t prog_reg_desc)
  564. {
  565. avpriv_set_pts_info(st, 33, 1, 90000);
  566. st->priv_data = pes;
  567. st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
  568. st->codecpar->codec_id = AV_CODEC_ID_NONE;
  569. st->need_parsing = AVSTREAM_PARSE_FULL;
  570. pes->st = st;
  571. pes->stream_type = stream_type;
  572. av_log(pes->stream, AV_LOG_DEBUG,
  573. "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
  574. st->index, pes->stream_type, pes->pid, (char *)&prog_reg_desc);
  575. st->codecpar->codec_tag = pes->stream_type;
  576. mpegts_find_stream_type(st, pes->stream_type, ISO_types);
  577. if (prog_reg_desc == AV_RL32("HDMV") &&
  578. st->codecpar->codec_id == AV_CODEC_ID_NONE) {
  579. mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
  580. if (pes->stream_type == 0x83) {
  581. // HDMV TrueHD streams also contain an AC3 coded version of the
  582. // audio track - add a second stream for this
  583. AVStream *sub_st;
  584. // priv_data cannot be shared between streams
  585. PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
  586. if (!sub_pes)
  587. return AVERROR(ENOMEM);
  588. memcpy(sub_pes, pes, sizeof(*sub_pes));
  589. sub_st = avformat_new_stream(pes->stream, NULL);
  590. if (!sub_st) {
  591. av_free(sub_pes);
  592. return AVERROR(ENOMEM);
  593. }
  594. sub_st->id = pes->pid;
  595. avpriv_set_pts_info(sub_st, 33, 1, 90000);
  596. sub_st->priv_data = sub_pes;
  597. sub_st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  598. sub_st->codecpar->codec_id = AV_CODEC_ID_AC3;
  599. sub_st->need_parsing = AVSTREAM_PARSE_FULL;
  600. sub_pes->sub_st = pes->sub_st = sub_st;
  601. }
  602. }
  603. if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
  604. mpegts_find_stream_type(st, pes->stream_type, MISC_types);
  605. return 0;
  606. }
  607. static void new_pes_packet(PESContext *pes, AVPacket *pkt)
  608. {
  609. av_init_packet(pkt);
  610. pkt->buf = pes->buffer;
  611. pkt->data = pes->buffer->data;
  612. pkt->size = pes->data_index;
  613. if (pes->total_size != MAX_PES_PAYLOAD &&
  614. pes->pes_header_size + pes->data_index != pes->total_size +
  615. PES_START_SIZE) {
  616. av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
  617. pes->flags |= AV_PKT_FLAG_CORRUPT;
  618. }
  619. memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  620. // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
  621. if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
  622. pkt->stream_index = pes->sub_st->index;
  623. else
  624. pkt->stream_index = pes->st->index;
  625. pkt->pts = pes->pts;
  626. pkt->dts = pes->dts;
  627. /* store position of first TS packet of this PES packet */
  628. pkt->pos = pes->ts_packet_pos;
  629. pkt->flags = pes->flags;
  630. /* reset pts values */
  631. pes->pts = AV_NOPTS_VALUE;
  632. pes->dts = AV_NOPTS_VALUE;
  633. pes->buffer = NULL;
  634. pes->data_index = 0;
  635. pes->flags = 0;
  636. }
  637. static int read_sl_header(PESContext *pes, SLConfigDescr *sl,
  638. const uint8_t *buf, int buf_size)
  639. {
  640. GetBitContext gb;
  641. int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
  642. int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
  643. int dts_flag = -1, cts_flag = -1;
  644. int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
  645. init_get_bits(&gb, buf, buf_size * 8);
  646. if (sl->use_au_start)
  647. au_start_flag = get_bits1(&gb);
  648. if (sl->use_au_end)
  649. au_end_flag = get_bits1(&gb);
  650. if (!sl->use_au_start && !sl->use_au_end)
  651. au_start_flag = au_end_flag = 1;
  652. if (sl->ocr_len > 0)
  653. ocr_flag = get_bits1(&gb);
  654. if (sl->use_idle)
  655. idle_flag = get_bits1(&gb);
  656. if (sl->use_padding)
  657. padding_flag = get_bits1(&gb);
  658. if (padding_flag)
  659. padding_bits = get_bits(&gb, 3);
  660. if (!idle_flag && (!padding_flag || padding_bits != 0)) {
  661. if (sl->packet_seq_num_len)
  662. skip_bits_long(&gb, sl->packet_seq_num_len);
  663. if (sl->degr_prior_len)
  664. if (get_bits1(&gb))
  665. skip_bits(&gb, sl->degr_prior_len);
  666. if (ocr_flag)
  667. skip_bits_long(&gb, sl->ocr_len);
  668. if (au_start_flag) {
  669. if (sl->use_rand_acc_pt)
  670. get_bits1(&gb);
  671. if (sl->au_seq_num_len > 0)
  672. skip_bits_long(&gb, sl->au_seq_num_len);
  673. if (sl->use_timestamps) {
  674. dts_flag = get_bits1(&gb);
  675. cts_flag = get_bits1(&gb);
  676. }
  677. }
  678. if (sl->inst_bitrate_len)
  679. inst_bitrate_flag = get_bits1(&gb);
  680. if (dts_flag == 1)
  681. dts = get_bits64(&gb, sl->timestamp_len);
  682. if (cts_flag == 1)
  683. cts = get_bits64(&gb, sl->timestamp_len);
  684. if (sl->au_len > 0)
  685. skip_bits_long(&gb, sl->au_len);
  686. if (inst_bitrate_flag)
  687. skip_bits_long(&gb, sl->inst_bitrate_len);
  688. }
  689. if (dts != AV_NOPTS_VALUE)
  690. pes->dts = dts;
  691. if (cts != AV_NOPTS_VALUE)
  692. pes->pts = cts;
  693. if (sl->timestamp_len && sl->timestamp_res)
  694. avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
  695. return (get_bits_count(&gb) + 7) >> 3;
  696. }
  697. /* return non zero if a packet could be constructed */
  698. static int mpegts_push_data(MpegTSFilter *filter,
  699. const uint8_t *buf, int buf_size, int is_start,
  700. int64_t pos)
  701. {
  702. PESContext *pes = filter->u.pes_filter.opaque;
  703. MpegTSContext *ts = pes->ts;
  704. const uint8_t *p;
  705. int len, code;
  706. if (!ts->pkt)
  707. return 0;
  708. if (is_start) {
  709. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  710. new_pes_packet(pes, ts->pkt);
  711. ts->stop_parse = 1;
  712. }
  713. pes->state = MPEGTS_HEADER;
  714. pes->data_index = 0;
  715. pes->ts_packet_pos = pos;
  716. }
  717. p = buf;
  718. while (buf_size > 0) {
  719. switch (pes->state) {
  720. case MPEGTS_HEADER:
  721. len = PES_START_SIZE - pes->data_index;
  722. if (len > buf_size)
  723. len = buf_size;
  724. memcpy(pes->header + pes->data_index, p, len);
  725. pes->data_index += len;
  726. p += len;
  727. buf_size -= len;
  728. if (pes->data_index == PES_START_SIZE) {
  729. /* we got all the PES or section header. We can now
  730. * decide */
  731. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  732. pes->header[2] == 0x01) {
  733. /* it must be an MPEG-2 PES stream */
  734. code = pes->header[3] | 0x100;
  735. av_log(pes->stream, AV_LOG_TRACE, "pid=%x pes_code=%#x\n", pes->pid,
  736. code);
  737. if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
  738. (!pes->sub_st ||
  739. pes->sub_st->discard == AVDISCARD_ALL)) ||
  740. code == 0x1be) /* padding_stream */
  741. goto skip;
  742. /* stream not present in PMT */
  743. if (!pes->st) {
  744. pes->st = avformat_new_stream(ts->stream, NULL);
  745. if (!pes->st)
  746. return AVERROR(ENOMEM);
  747. pes->st->id = pes->pid;
  748. mpegts_set_stream_info(pes->st, pes, 0, 0);
  749. }
  750. pes->total_size = AV_RB16(pes->header + 4);
  751. /* NOTE: a zero total size means the PES size is
  752. * unbounded */
  753. if (!pes->total_size)
  754. pes->total_size = MAX_PES_PAYLOAD;
  755. /* allocate pes buffer */
  756. pes->buffer = av_buffer_alloc(pes->total_size +
  757. AV_INPUT_BUFFER_PADDING_SIZE);
  758. if (!pes->buffer)
  759. return AVERROR(ENOMEM);
  760. if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
  761. code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
  762. code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
  763. code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
  764. pes->state = MPEGTS_PESHEADER;
  765. if (pes->st->codecpar->codec_id == AV_CODEC_ID_NONE) {
  766. av_log(pes->stream, AV_LOG_TRACE,
  767. "pid=%x stream_type=%x probing\n",
  768. pes->pid,
  769. pes->stream_type);
  770. pes->st->codecpar->codec_id = AV_CODEC_ID_PROBE;
  771. }
  772. } else {
  773. pes->state = MPEGTS_PAYLOAD;
  774. pes->data_index = 0;
  775. }
  776. } else {
  777. /* otherwise, it should be a table */
  778. /* skip packet */
  779. skip:
  780. pes->state = MPEGTS_SKIP;
  781. continue;
  782. }
  783. }
  784. break;
  785. /**********************************************/
  786. /* PES packing parsing */
  787. case MPEGTS_PESHEADER:
  788. len = PES_HEADER_SIZE - pes->data_index;
  789. if (len < 0)
  790. return AVERROR_INVALIDDATA;
  791. if (len > buf_size)
  792. len = buf_size;
  793. memcpy(pes->header + pes->data_index, p, len);
  794. pes->data_index += len;
  795. p += len;
  796. buf_size -= len;
  797. if (pes->data_index == PES_HEADER_SIZE) {
  798. pes->pes_header_size = pes->header[8] + 9;
  799. pes->state = MPEGTS_PESHEADER_FILL;
  800. }
  801. break;
  802. case MPEGTS_PESHEADER_FILL:
  803. len = pes->pes_header_size - pes->data_index;
  804. if (len < 0)
  805. return AVERROR_INVALIDDATA;
  806. if (len > buf_size)
  807. len = buf_size;
  808. memcpy(pes->header + pes->data_index, p, len);
  809. pes->data_index += len;
  810. p += len;
  811. buf_size -= len;
  812. if (pes->data_index == pes->pes_header_size) {
  813. const uint8_t *r;
  814. unsigned int flags, pes_ext, skip;
  815. flags = pes->header[7];
  816. r = pes->header + 9;
  817. pes->pts = AV_NOPTS_VALUE;
  818. pes->dts = AV_NOPTS_VALUE;
  819. if ((flags & 0xc0) == 0x80) {
  820. pes->dts = pes->pts = ff_parse_pes_pts(r);
  821. r += 5;
  822. } else if ((flags & 0xc0) == 0xc0) {
  823. pes->pts = ff_parse_pes_pts(r);
  824. r += 5;
  825. pes->dts = ff_parse_pes_pts(r);
  826. r += 5;
  827. }
  828. pes->extended_stream_id = -1;
  829. if (flags & 0x01) { /* PES extension */
  830. pes_ext = *r++;
  831. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  832. skip = (pes_ext >> 4) & 0xb;
  833. skip += skip & 0x9;
  834. r += skip;
  835. if ((pes_ext & 0x41) == 0x01 &&
  836. (r + 2) <= (pes->header + pes->pes_header_size)) {
  837. /* PES extension 2 */
  838. if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
  839. pes->extended_stream_id = r[1];
  840. }
  841. }
  842. /* we got the full header. We parse it and get the payload */
  843. pes->state = MPEGTS_PAYLOAD;
  844. pes->data_index = 0;
  845. if (pes->stream_type == 0x12 && buf_size > 0) {
  846. int sl_header_bytes = read_sl_header(pes, &pes->sl, p,
  847. buf_size);
  848. pes->pes_header_size += sl_header_bytes;
  849. p += sl_header_bytes;
  850. buf_size -= sl_header_bytes;
  851. }
  852. }
  853. break;
  854. case MPEGTS_PAYLOAD:
  855. if (buf_size > 0 && pes->buffer) {
  856. if (pes->data_index > 0 &&
  857. pes->data_index + buf_size > pes->total_size) {
  858. new_pes_packet(pes, ts->pkt);
  859. pes->total_size = MAX_PES_PAYLOAD;
  860. pes->buffer = av_buffer_alloc(pes->total_size +
  861. AV_INPUT_BUFFER_PADDING_SIZE);
  862. if (!pes->buffer)
  863. return AVERROR(ENOMEM);
  864. ts->stop_parse = 1;
  865. } else if (pes->data_index == 0 &&
  866. buf_size > pes->total_size) {
  867. // pes packet size is < ts size packet and pes data is padded with 0xff
  868. // not sure if this is legal in ts but see issue #2392
  869. buf_size = pes->total_size;
  870. }
  871. memcpy(pes->buffer->data + pes->data_index, p, buf_size);
  872. pes->data_index += buf_size;
  873. }
  874. buf_size = 0;
  875. /* emit complete packets with known packet size
  876. * decreases demuxer delay for infrequent packets like subtitles from
  877. * a couple of seconds to milliseconds for properly muxed files.
  878. * total_size is the number of bytes following pes_packet_length
  879. * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
  880. if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
  881. pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
  882. ts->stop_parse = 1;
  883. new_pes_packet(pes, ts->pkt);
  884. }
  885. break;
  886. case MPEGTS_SKIP:
  887. buf_size = 0;
  888. break;
  889. }
  890. }
  891. return 0;
  892. }
  893. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
  894. {
  895. MpegTSFilter *tss;
  896. PESContext *pes;
  897. /* if no pid found, then add a pid context */
  898. pes = av_mallocz(sizeof(PESContext));
  899. if (!pes)
  900. return 0;
  901. pes->ts = ts;
  902. pes->stream = ts->stream;
  903. pes->pid = pid;
  904. pes->pcr_pid = pcr_pid;
  905. pes->state = MPEGTS_SKIP;
  906. pes->pts = AV_NOPTS_VALUE;
  907. pes->dts = AV_NOPTS_VALUE;
  908. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  909. if (!tss) {
  910. av_free(pes);
  911. return 0;
  912. }
  913. return pes;
  914. }
  915. #define MAX_LEVEL 4
  916. typedef struct MP4DescrParseContext {
  917. AVFormatContext *s;
  918. AVIOContext pb;
  919. Mp4Descr *descr;
  920. Mp4Descr *active_descr;
  921. int descr_count;
  922. int max_descr_count;
  923. int level;
  924. } MP4DescrParseContext;
  925. static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s,
  926. const uint8_t *buf, unsigned size,
  927. Mp4Descr *descr, int max_descr_count)
  928. {
  929. int ret;
  930. if (size > (1 << 30))
  931. return AVERROR_INVALIDDATA;
  932. if ((ret = ffio_init_context(&d->pb, (unsigned char *)buf, size, 0,
  933. NULL, NULL, NULL, NULL)) < 0)
  934. return ret;
  935. d->s = s;
  936. d->level = 0;
  937. d->descr_count = 0;
  938. d->descr = descr;
  939. d->active_descr = NULL;
  940. d->max_descr_count = max_descr_count;
  941. return 0;
  942. }
  943. static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
  944. {
  945. int64_t new_off = avio_tell(pb);
  946. (*len) -= new_off - *off;
  947. *off = new_off;
  948. }
  949. static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
  950. int target_tag);
  951. static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
  952. {
  953. while (len > 0) {
  954. int ret = parse_mp4_descr(d, off, len, 0);
  955. if (ret < 0)
  956. return ret;
  957. update_offsets(&d->pb, &off, &len);
  958. }
  959. return 0;
  960. }
  961. static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
  962. {
  963. avio_rb16(&d->pb); // ID
  964. avio_r8(&d->pb);
  965. avio_r8(&d->pb);
  966. avio_r8(&d->pb);
  967. avio_r8(&d->pb);
  968. avio_r8(&d->pb);
  969. update_offsets(&d->pb, &off, &len);
  970. return parse_mp4_descr_arr(d, off, len);
  971. }
  972. static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
  973. {
  974. int id_flags;
  975. if (len < 2)
  976. return 0;
  977. id_flags = avio_rb16(&d->pb);
  978. if (!(id_flags & 0x0020)) { // URL_Flag
  979. update_offsets(&d->pb, &off, &len);
  980. return parse_mp4_descr_arr(d, off, len); // ES_Descriptor[]
  981. } else {
  982. return 0;
  983. }
  984. }
  985. static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  986. {
  987. int es_id = 0;
  988. int ret = 0;
  989. if (d->descr_count >= d->max_descr_count)
  990. return AVERROR_INVALIDDATA;
  991. ff_mp4_parse_es_descr(&d->pb, &es_id);
  992. d->active_descr = d->descr + (d->descr_count++);
  993. d->active_descr->es_id = es_id;
  994. update_offsets(&d->pb, &off, &len);
  995. if ((ret = parse_mp4_descr(d, off, len, MP4DecConfigDescrTag)) < 0)
  996. return ret;
  997. update_offsets(&d->pb, &off, &len);
  998. if (len > 0)
  999. ret = parse_mp4_descr(d, off, len, MP4SLDescrTag);
  1000. d->active_descr = NULL;
  1001. return ret;
  1002. }
  1003. static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off,
  1004. int len)
  1005. {
  1006. Mp4Descr *descr = d->active_descr;
  1007. if (!descr)
  1008. return AVERROR_INVALIDDATA;
  1009. d->active_descr->dec_config_descr = av_malloc(len);
  1010. if (!descr->dec_config_descr)
  1011. return AVERROR(ENOMEM);
  1012. descr->dec_config_descr_len = len;
  1013. avio_read(&d->pb, descr->dec_config_descr, len);
  1014. return 0;
  1015. }
  1016. static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1017. {
  1018. Mp4Descr *descr = d->active_descr;
  1019. int predefined;
  1020. if (!descr)
  1021. return AVERROR_INVALIDDATA;
  1022. predefined = avio_r8(&d->pb);
  1023. if (!predefined) {
  1024. int lengths;
  1025. int flags = avio_r8(&d->pb);
  1026. descr->sl.use_au_start = !!(flags & 0x80);
  1027. descr->sl.use_au_end = !!(flags & 0x40);
  1028. descr->sl.use_rand_acc_pt = !!(flags & 0x20);
  1029. descr->sl.use_padding = !!(flags & 0x08);
  1030. descr->sl.use_timestamps = !!(flags & 0x04);
  1031. descr->sl.use_idle = !!(flags & 0x02);
  1032. descr->sl.timestamp_res = avio_rb32(&d->pb);
  1033. avio_rb32(&d->pb);
  1034. descr->sl.timestamp_len = avio_r8(&d->pb);
  1035. descr->sl.ocr_len = avio_r8(&d->pb);
  1036. descr->sl.au_len = avio_r8(&d->pb);
  1037. descr->sl.inst_bitrate_len = avio_r8(&d->pb);
  1038. lengths = avio_rb16(&d->pb);
  1039. descr->sl.degr_prior_len = lengths >> 12;
  1040. descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
  1041. descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
  1042. if (descr->sl.timestamp_len >= 64 ||
  1043. descr->sl.ocr_len >= 64 ||
  1044. descr->sl.au_len >= 32) {
  1045. return AVERROR_INVALIDDATA;
  1046. }
  1047. } else {
  1048. avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
  1049. }
  1050. return 0;
  1051. }
  1052. static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
  1053. int target_tag)
  1054. {
  1055. int tag;
  1056. int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
  1057. int ret = 0;
  1058. update_offsets(&d->pb, &off, &len);
  1059. if (len < 0 || len1 > len || len1 <= 0) {
  1060. av_log(d->s, AV_LOG_ERROR,
  1061. "Tag %x length violation new length %d bytes remaining %d\n",
  1062. tag, len1, len);
  1063. return AVERROR_INVALIDDATA;
  1064. }
  1065. if (d->level++ >= MAX_LEVEL) {
  1066. av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
  1067. ret = AVERROR_INVALIDDATA;
  1068. goto done;
  1069. }
  1070. if (target_tag && tag != target_tag) {
  1071. av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag,
  1072. target_tag);
  1073. ret = AVERROR_INVALIDDATA;
  1074. goto done;
  1075. }
  1076. switch (tag) {
  1077. case MP4IODescrTag:
  1078. ret = parse_MP4IODescrTag(d, off, len1);
  1079. break;
  1080. case MP4ODescrTag:
  1081. ret = parse_MP4ODescrTag(d, off, len1);
  1082. break;
  1083. case MP4ESDescrTag:
  1084. ret = parse_MP4ESDescrTag(d, off, len1);
  1085. break;
  1086. case MP4DecConfigDescrTag:
  1087. ret = parse_MP4DecConfigDescrTag(d, off, len1);
  1088. break;
  1089. case MP4SLDescrTag:
  1090. ret = parse_MP4SLDescrTag(d, off, len1);
  1091. break;
  1092. }
  1093. done:
  1094. d->level--;
  1095. avio_seek(&d->pb, off + len1, SEEK_SET);
  1096. return ret;
  1097. }
  1098. static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
  1099. Mp4Descr *descr, int *descr_count, int max_descr_count)
  1100. {
  1101. MP4DescrParseContext d;
  1102. int ret;
  1103. ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
  1104. if (ret < 0)
  1105. return ret;
  1106. ret = parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
  1107. *descr_count = d.descr_count;
  1108. return ret;
  1109. }
  1110. static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
  1111. Mp4Descr *descr, int *descr_count, int max_descr_count)
  1112. {
  1113. MP4DescrParseContext d;
  1114. int ret;
  1115. ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
  1116. if (ret < 0)
  1117. return ret;
  1118. ret = parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
  1119. *descr_count = d.descr_count;
  1120. return ret;
  1121. }
  1122. static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section,
  1123. int section_len)
  1124. {
  1125. MpegTSContext *ts = filter->u.section_filter.opaque;
  1126. MpegTSSectionFilter *tssf = &filter->u.section_filter;
  1127. SectionHeader h;
  1128. const uint8_t *p, *p_end;
  1129. AVIOContext pb;
  1130. int mp4_descr_count = 0;
  1131. Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
  1132. int i, pid;
  1133. AVFormatContext *s = ts->stream;
  1134. p_end = section + section_len - 4;
  1135. p = section;
  1136. if (parse_section_header(&h, &p, p_end) < 0)
  1137. return;
  1138. if (h.tid != M4OD_TID)
  1139. return;
  1140. if (h.version == tssf->last_ver)
  1141. return;
  1142. tssf->last_ver = h.version;
  1143. mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
  1144. MAX_MP4_DESCR_COUNT);
  1145. for (pid = 0; pid < NB_PID_MAX; pid++) {
  1146. if (!ts->pids[pid])
  1147. continue;
  1148. for (i = 0; i < mp4_descr_count; i++) {
  1149. PESContext *pes;
  1150. AVStream *st;
  1151. if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
  1152. continue;
  1153. if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
  1154. av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
  1155. continue;
  1156. }
  1157. pes = ts->pids[pid]->u.pes_filter.opaque;
  1158. st = pes->st;
  1159. if (!st)
  1160. continue;
  1161. pes->sl = mp4_descr[i].sl;
  1162. ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
  1163. mp4_descr[i].dec_config_descr_len, 0,
  1164. NULL, NULL, NULL, NULL);
  1165. ff_mp4_read_dec_config_descr(s, st, &pb);
  1166. if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
  1167. st->codecpar->extradata_size > 0)
  1168. st->need_parsing = 0;
  1169. if (st->codecpar->codec_id == AV_CODEC_ID_H264 &&
  1170. st->codecpar->extradata_size > 0)
  1171. st->need_parsing = 0;
  1172. st->codecpar->codec_type = avcodec_get_type(st->codecpar->codec_id);
  1173. }
  1174. }
  1175. for (i = 0; i < mp4_descr_count; i++)
  1176. av_free(mp4_descr[i].dec_config_descr);
  1177. }
  1178. static const uint8_t opus_coupled_stream_cnt[9] = {
  1179. 1, 0, 1, 1, 2, 2, 2, 3, 3
  1180. };
  1181. static const uint8_t opus_stream_cnt[9] = {
  1182. 1, 1, 1, 2, 2, 3, 4, 4, 5,
  1183. };
  1184. static const uint8_t opus_channel_map[8][8] = {
  1185. { 0 },
  1186. { 0,1 },
  1187. { 0,2,1 },
  1188. { 0,1,2,3 },
  1189. { 0,4,1,2,3 },
  1190. { 0,4,1,2,3,5 },
  1191. { 0,4,1,2,3,5,6 },
  1192. { 0,6,1,2,3,4,5,7 },
  1193. };
  1194. int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
  1195. const uint8_t **pp, const uint8_t *desc_list_end,
  1196. Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
  1197. MpegTSContext *ts)
  1198. {
  1199. const uint8_t *desc_end;
  1200. int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code;
  1201. char language[252];
  1202. int i;
  1203. desc_tag = get8(pp, desc_list_end);
  1204. if (desc_tag < 0)
  1205. return AVERROR_INVALIDDATA;
  1206. desc_len = get8(pp, desc_list_end);
  1207. if (desc_len < 0)
  1208. return AVERROR_INVALIDDATA;
  1209. desc_end = *pp + desc_len;
  1210. if (desc_end > desc_list_end)
  1211. return AVERROR_INVALIDDATA;
  1212. av_log(fc, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
  1213. if (st->codecpar->codec_id == AV_CODEC_ID_NONE &&
  1214. stream_type == STREAM_TYPE_PRIVATE_DATA)
  1215. mpegts_find_stream_type(st, desc_tag, DESC_types);
  1216. switch (desc_tag) {
  1217. case 0x1E: /* SL descriptor */
  1218. desc_es_id = get16(pp, desc_end);
  1219. if (desc_es_id < 0)
  1220. break;
  1221. if (ts && ts->pids[pid])
  1222. ts->pids[pid]->es_id = desc_es_id;
  1223. for (i = 0; i < mp4_descr_count; i++)
  1224. if (mp4_descr[i].dec_config_descr_len &&
  1225. mp4_descr[i].es_id == desc_es_id) {
  1226. AVIOContext pb;
  1227. ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
  1228. mp4_descr[i].dec_config_descr_len, 0,
  1229. NULL, NULL, NULL, NULL);
  1230. ff_mp4_read_dec_config_descr(fc, st, &pb);
  1231. if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
  1232. st->codecpar->extradata_size > 0)
  1233. st->need_parsing = 0;
  1234. if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
  1235. mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
  1236. }
  1237. break;
  1238. case 0x1F: /* FMC descriptor */
  1239. if (get16(pp, desc_end) < 0)
  1240. break;
  1241. if (mp4_descr_count > 0 &&
  1242. st->codecpar->codec_id == AV_CODEC_ID_AAC_LATM &&
  1243. mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
  1244. AVIOContext pb;
  1245. ffio_init_context(&pb, mp4_descr->dec_config_descr,
  1246. mp4_descr->dec_config_descr_len, 0,
  1247. NULL, NULL, NULL, NULL);
  1248. ff_mp4_read_dec_config_descr(fc, st, &pb);
  1249. if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
  1250. st->codecpar->extradata_size > 0)
  1251. st->need_parsing = 0;
  1252. }
  1253. break;
  1254. case 0x56: /* DVB teletext descriptor */
  1255. language[0] = get8(pp, desc_end);
  1256. language[1] = get8(pp, desc_end);
  1257. language[2] = get8(pp, desc_end);
  1258. language[3] = 0;
  1259. av_dict_set(&st->metadata, "language", language, 0);
  1260. break;
  1261. case 0x59: /* subtitling descriptor */
  1262. language[0] = get8(pp, desc_end);
  1263. language[1] = get8(pp, desc_end);
  1264. language[2] = get8(pp, desc_end);
  1265. language[3] = 0;
  1266. /* hearing impaired subtitles detection */
  1267. switch (get8(pp, desc_end)) {
  1268. case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
  1269. case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
  1270. case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
  1271. case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
  1272. case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
  1273. case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
  1274. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  1275. break;
  1276. }
  1277. if (st->codecpar->extradata) {
  1278. if (st->codecpar->extradata_size == 4 &&
  1279. memcmp(st->codecpar->extradata, *pp, 4))
  1280. avpriv_request_sample(fc, "DVB sub with multiple IDs");
  1281. } else {
  1282. st->codecpar->extradata = av_malloc(4 + AV_INPUT_BUFFER_PADDING_SIZE);
  1283. if (st->codecpar->extradata) {
  1284. st->codecpar->extradata_size = 4;
  1285. memcpy(st->codecpar->extradata, *pp, 4);
  1286. }
  1287. }
  1288. *pp += 4;
  1289. av_dict_set(&st->metadata, "language", language, 0);
  1290. break;
  1291. case 0x0a: /* ISO 639 language descriptor */
  1292. for (i = 0; i + 4 <= desc_len; i += 4) {
  1293. language[i + 0] = get8(pp, desc_end);
  1294. language[i + 1] = get8(pp, desc_end);
  1295. language[i + 2] = get8(pp, desc_end);
  1296. language[i + 3] = ',';
  1297. switch (get8(pp, desc_end)) {
  1298. case 0x01:
  1299. st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS;
  1300. break;
  1301. case 0x02:
  1302. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  1303. break;
  1304. case 0x03:
  1305. st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
  1306. break;
  1307. }
  1308. }
  1309. if (i && language[0]) {
  1310. language[i - 1] = 0;
  1311. av_dict_set(&st->metadata, "language", language, 0);
  1312. }
  1313. break;
  1314. case 0x05: /* registration descriptor */
  1315. st->codecpar->codec_tag = bytestream_get_le32(pp);
  1316. av_log(fc, AV_LOG_TRACE, "reg_desc=%.4s\n", (char *)&st->codecpar->codec_tag);
  1317. if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
  1318. mpegts_find_stream_type(st, st->codecpar->codec_tag, REGD_types);
  1319. break;
  1320. case 0x7f: /* DVB extension descriptor */
  1321. ext_desc_tag = get8(pp, desc_end);
  1322. if (ext_desc_tag < 0)
  1323. return AVERROR_INVALIDDATA;
  1324. if (st->codecpar->codec_id == AV_CODEC_ID_OPUS &&
  1325. ext_desc_tag == 0x80) { /* User defined (provisional Opus) */
  1326. if (!st->codecpar->extradata) {
  1327. st->codecpar->extradata = av_mallocz(sizeof(opus_default_extradata) +
  1328. AV_INPUT_BUFFER_PADDING_SIZE);
  1329. if (!st->codecpar->extradata)
  1330. return AVERROR(ENOMEM);
  1331. st->codecpar->extradata_size = sizeof(opus_default_extradata);
  1332. memcpy(st->codecpar->extradata, opus_default_extradata, sizeof(opus_default_extradata));
  1333. channel_config_code = get8(pp, desc_end);
  1334. if (channel_config_code < 0)
  1335. return AVERROR_INVALIDDATA;
  1336. if (channel_config_code <= 0x8) {
  1337. st->codecpar->extradata[9] = channels = channel_config_code ? channel_config_code : 2;
  1338. st->codecpar->extradata[18] = channel_config_code ? (channels > 2) : 255;
  1339. st->codecpar->extradata[19] = opus_stream_cnt[channel_config_code];
  1340. st->codecpar->extradata[20] = opus_coupled_stream_cnt[channel_config_code];
  1341. memcpy(&st->codecpar->extradata[21], opus_channel_map[channels - 1], channels);
  1342. } else {
  1343. avpriv_request_sample(fc, "Opus in MPEG-TS - channel_config_code > 0x8");
  1344. }
  1345. st->need_parsing = AVSTREAM_PARSE_FULL;
  1346. }
  1347. }
  1348. break;
  1349. default:
  1350. break;
  1351. }
  1352. *pp = desc_end;
  1353. return 0;
  1354. }
  1355. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1356. {
  1357. MpegTSContext *ts = filter->u.section_filter.opaque;
  1358. MpegTSSectionFilter *tssf = &filter->u.section_filter;
  1359. SectionHeader h1, *h = &h1;
  1360. PESContext *pes;
  1361. AVStream *st;
  1362. const uint8_t *p, *p_end, *desc_list_end;
  1363. int program_info_length, pcr_pid, pid, stream_type;
  1364. int desc_list_len;
  1365. uint32_t prog_reg_desc = 0; /* registration descriptor */
  1366. int mp4_descr_count = 0;
  1367. Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
  1368. int i;
  1369. av_log(ts->stream, AV_LOG_TRACE, "PMT: len %i\n", section_len);
  1370. hex_dump_debug(ts->stream, section, section_len);
  1371. p_end = section + section_len - 4;
  1372. p = section;
  1373. if (parse_section_header(h, &p, p_end) < 0)
  1374. return;
  1375. if (h->version == tssf->last_ver)
  1376. return;
  1377. tssf->last_ver = h->version;
  1378. av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x sec_num=%d/%d\n",
  1379. h->id, h->sec_num, h->last_sec_num);
  1380. if (h->tid != PMT_TID)
  1381. return;
  1382. clear_program(ts, h->id);
  1383. pcr_pid = get16(&p, p_end);
  1384. if (pcr_pid < 0)
  1385. return;
  1386. pcr_pid &= 0x1fff;
  1387. add_pid_to_pmt(ts, h->id, pcr_pid);
  1388. av_log(ts->stream, AV_LOG_TRACE, "pcr_pid=0x%x\n", pcr_pid);
  1389. program_info_length = get16(&p, p_end);
  1390. if (program_info_length < 0)
  1391. return;
  1392. program_info_length &= 0xfff;
  1393. while (program_info_length >= 2) {
  1394. uint8_t tag, len;
  1395. tag = get8(&p, p_end);
  1396. len = get8(&p, p_end);
  1397. av_log(ts->stream, AV_LOG_TRACE, "program tag: 0x%02x len=%d\n", tag, len);
  1398. if (len > program_info_length - 2)
  1399. // something else is broken, exit the program_descriptors_loop
  1400. break;
  1401. program_info_length -= len + 2;
  1402. if (tag == 0x1d) { // IOD descriptor
  1403. get8(&p, p_end); // scope
  1404. get8(&p, p_end); // label
  1405. len -= 2;
  1406. mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
  1407. &mp4_descr_count, MAX_MP4_DESCR_COUNT);
  1408. } else if (tag == 0x05 && len >= 4) { // registration descriptor
  1409. prog_reg_desc = bytestream_get_le32(&p);
  1410. len -= 4;
  1411. }
  1412. p += len;
  1413. }
  1414. p += program_info_length;
  1415. if (p >= p_end)
  1416. goto out;
  1417. // stop parsing after pmt, we found header
  1418. if (!ts->stream->nb_streams)
  1419. ts->stop_parse = 1;
  1420. for (;;) {
  1421. st = 0;
  1422. pes = NULL;
  1423. stream_type = get8(&p, p_end);
  1424. if (stream_type < 0)
  1425. break;
  1426. pid = get16(&p, p_end);
  1427. if (pid < 0)
  1428. break;
  1429. pid &= 0x1fff;
  1430. /* now create stream */
  1431. if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
  1432. pes = ts->pids[pid]->u.pes_filter.opaque;
  1433. if (!pes->st) {
  1434. pes->st = avformat_new_stream(pes->stream, NULL);
  1435. pes->st->id = pes->pid;
  1436. }
  1437. st = pes->st;
  1438. } else if (stream_type != 0x13) {
  1439. if (ts->pids[pid])
  1440. mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably
  1441. pes = add_pes_stream(ts, pid, pcr_pid);
  1442. if (pes) {
  1443. st = avformat_new_stream(pes->stream, NULL);
  1444. st->id = pes->pid;
  1445. }
  1446. } else {
  1447. int idx = ff_find_stream_index(ts->stream, pid);
  1448. if (idx >= 0) {
  1449. st = ts->stream->streams[idx];
  1450. } else {
  1451. st = avformat_new_stream(ts->stream, NULL);
  1452. st->id = pid;
  1453. st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
  1454. }
  1455. }
  1456. if (!st)
  1457. goto out;
  1458. if (pes && !pes->stream_type)
  1459. mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
  1460. add_pid_to_pmt(ts, h->id, pid);
  1461. ff_program_add_stream_index(ts->stream, h->id, st->index);
  1462. desc_list_len = get16(&p, p_end);
  1463. if (desc_list_len < 0)
  1464. break;
  1465. desc_list_len &= 0xfff;
  1466. desc_list_end = p + desc_list_len;
  1467. if (desc_list_end > p_end)
  1468. break;
  1469. for (;;) {
  1470. if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p,
  1471. desc_list_end, mp4_descr,
  1472. mp4_descr_count, pid, ts) < 0)
  1473. break;
  1474. if (pes && prog_reg_desc == AV_RL32("HDMV") &&
  1475. stream_type == 0x83 && pes->sub_st) {
  1476. ff_program_add_stream_index(ts->stream, h->id,
  1477. pes->sub_st->index);
  1478. pes->sub_st->codecpar->codec_tag = st->codecpar->codec_tag;
  1479. }
  1480. }
  1481. p = desc_list_end;
  1482. }
  1483. out:
  1484. for (i = 0; i < mp4_descr_count; i++)
  1485. av_free(mp4_descr[i].dec_config_descr);
  1486. }
  1487. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1488. {
  1489. MpegTSContext *ts = filter->u.section_filter.opaque;
  1490. MpegTSSectionFilter *tssf = &filter->u.section_filter;
  1491. SectionHeader h1, *h = &h1;
  1492. const uint8_t *p, *p_end;
  1493. int sid, pmt_pid;
  1494. av_log(ts->stream, AV_LOG_TRACE, "PAT:\n");
  1495. hex_dump_debug(ts->stream, section, section_len);
  1496. p_end = section + section_len - 4;
  1497. p = section;
  1498. if (parse_section_header(h, &p, p_end) < 0)
  1499. return;
  1500. if (h->tid != PAT_TID)
  1501. return;
  1502. if (h->version == tssf->last_ver)
  1503. return;
  1504. tssf->last_ver = h->version;
  1505. clear_programs(ts);
  1506. for (;;) {
  1507. sid = get16(&p, p_end);
  1508. if (sid < 0)
  1509. break;
  1510. pmt_pid = get16(&p, p_end);
  1511. if (pmt_pid < 0)
  1512. break;
  1513. pmt_pid &= 0x1fff;
  1514. av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  1515. if (sid == 0x0000) {
  1516. /* NIT info */
  1517. } else {
  1518. av_new_program(ts->stream, sid);
  1519. if (ts->pids[pmt_pid])
  1520. mpegts_close_filter(ts, ts->pids[pmt_pid]);
  1521. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  1522. add_pat_entry(ts, sid);
  1523. add_pid_to_pmt(ts, sid, 0); // add pat pid to program
  1524. add_pid_to_pmt(ts, sid, pmt_pid);
  1525. }
  1526. }
  1527. }
  1528. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1529. {
  1530. MpegTSContext *ts = filter->u.section_filter.opaque;
  1531. MpegTSSectionFilter *tssf = &filter->u.section_filter;
  1532. SectionHeader h1, *h = &h1;
  1533. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  1534. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  1535. char *name, *provider_name;
  1536. av_log(ts->stream, AV_LOG_TRACE, "SDT:\n");
  1537. hex_dump_debug(ts->stream, section, section_len);
  1538. p_end = section + section_len - 4;
  1539. p = section;
  1540. if (parse_section_header(h, &p, p_end) < 0)
  1541. return;
  1542. if (h->tid != SDT_TID)
  1543. return;
  1544. if (h->version == tssf->last_ver)
  1545. return;
  1546. tssf->last_ver = h->version;
  1547. onid = get16(&p, p_end);
  1548. if (onid < 0)
  1549. return;
  1550. val = get8(&p, p_end);
  1551. if (val < 0)
  1552. return;
  1553. for (;;) {
  1554. sid = get16(&p, p_end);
  1555. if (sid < 0)
  1556. break;
  1557. val = get8(&p, p_end);
  1558. if (val < 0)
  1559. break;
  1560. desc_list_len = get16(&p, p_end);
  1561. if (desc_list_len < 0)
  1562. break;
  1563. desc_list_len &= 0xfff;
  1564. desc_list_end = p + desc_list_len;
  1565. if (desc_list_end > p_end)
  1566. break;
  1567. for (;;) {
  1568. desc_tag = get8(&p, desc_list_end);
  1569. if (desc_tag < 0)
  1570. break;
  1571. desc_len = get8(&p, desc_list_end);
  1572. desc_end = p + desc_len;
  1573. if (desc_end > desc_list_end)
  1574. break;
  1575. av_log(ts->stream, AV_LOG_TRACE, "tag: 0x%02x len=%d\n",
  1576. desc_tag, desc_len);
  1577. switch (desc_tag) {
  1578. case 0x48:
  1579. service_type = get8(&p, p_end);
  1580. if (service_type < 0)
  1581. break;
  1582. provider_name = getstr8(&p, p_end);
  1583. if (!provider_name)
  1584. break;
  1585. name = getstr8(&p, p_end);
  1586. if (name) {
  1587. AVProgram *program = av_new_program(ts->stream, sid);
  1588. if (program) {
  1589. av_dict_set(&program->metadata, "service_name", name, 0);
  1590. av_dict_set(&program->metadata, "service_provider",
  1591. provider_name, 0);
  1592. }
  1593. }
  1594. av_free(name);
  1595. av_free(provider_name);
  1596. break;
  1597. default:
  1598. break;
  1599. }
  1600. p = desc_end;
  1601. }
  1602. p = desc_list_end;
  1603. }
  1604. }
  1605. /* handle one TS packet */
  1606. static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  1607. {
  1608. MpegTSFilter *tss;
  1609. int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
  1610. has_adaptation, has_payload;
  1611. const uint8_t *p, *p_end;
  1612. int64_t pos;
  1613. pid = AV_RB16(packet + 1) & 0x1fff;
  1614. if (pid && discard_pid(ts, pid))
  1615. return 0;
  1616. is_start = packet[1] & 0x40;
  1617. tss = ts->pids[pid];
  1618. if (ts->auto_guess && !tss && is_start) {
  1619. add_pes_stream(ts, pid, -1);
  1620. tss = ts->pids[pid];
  1621. }
  1622. if (!tss)
  1623. return 0;
  1624. afc = (packet[3] >> 4) & 3;
  1625. if (afc == 0) /* reserved value */
  1626. return 0;
  1627. has_adaptation = afc & 2;
  1628. has_payload = afc & 1;
  1629. is_discontinuity = has_adaptation &&
  1630. packet[4] != 0 && /* with length > 0 */
  1631. (packet[5] & 0x80); /* and discontinuity indicated */
  1632. /* continuity check (currently not used) */
  1633. cc = (packet[3] & 0xf);
  1634. expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
  1635. cc_ok = pid == 0x1FFF || // null packet PID
  1636. is_discontinuity ||
  1637. tss->last_cc < 0 ||
  1638. expected_cc == cc;
  1639. tss->last_cc = cc;
  1640. if (!cc_ok) {
  1641. av_log(ts->stream, AV_LOG_WARNING,
  1642. "Continuity check failed for pid %d expected %d got %d\n",
  1643. pid, expected_cc, cc);
  1644. if (tss->type == MPEGTS_PES) {
  1645. PESContext *pc = tss->u.pes_filter.opaque;
  1646. pc->flags |= AV_PKT_FLAG_CORRUPT;
  1647. }
  1648. }
  1649. if (!has_payload)
  1650. return 0;
  1651. p = packet + 4;
  1652. if (has_adaptation) {
  1653. /* skip adaptation field */
  1654. p += p[0] + 1;
  1655. }
  1656. /* if past the end of packet, ignore */
  1657. p_end = packet + TS_PACKET_SIZE;
  1658. if (p >= p_end)
  1659. return 0;
  1660. pos = avio_tell(ts->stream->pb);
  1661. MOD_UNLIKELY(ts->pos47, pos, ts->raw_packet_size, ts->pos);
  1662. if (tss->type == MPEGTS_SECTION) {
  1663. if (is_start) {
  1664. /* pointer field present */
  1665. len = *p++;
  1666. if (p + len > p_end)
  1667. return 0;
  1668. if (len && cc_ok) {
  1669. /* write remaining section bytes */
  1670. write_section_data(ts, tss,
  1671. p, len, 0);
  1672. /* check whether filter has been closed */
  1673. if (!ts->pids[pid])
  1674. return 0;
  1675. }
  1676. p += len;
  1677. if (p < p_end) {
  1678. write_section_data(ts, tss,
  1679. p, p_end - p, 1);
  1680. }
  1681. } else {
  1682. if (cc_ok) {
  1683. write_section_data(ts, tss,
  1684. p, p_end - p, 0);
  1685. }
  1686. }
  1687. } else {
  1688. int ret;
  1689. // Note: The position here points actually behind the current packet.
  1690. if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
  1691. pos - ts->raw_packet_size)) < 0)
  1692. return ret;
  1693. }
  1694. return 0;
  1695. }
  1696. /* XXX: try to find a better synchro over several packets (use
  1697. * get_packet_size() ?) */
  1698. static int mpegts_resync(AVFormatContext *s)
  1699. {
  1700. MpegTSContext *ts = s->priv_data;
  1701. AVIOContext *pb = s->pb;
  1702. int c, i;
  1703. for (i = 0; i < ts->resync_size; i++) {
  1704. c = avio_r8(pb);
  1705. if (pb->eof_reached)
  1706. return AVERROR_EOF;
  1707. if (c == 0x47) {
  1708. avio_seek(pb, -1, SEEK_CUR);
  1709. return 0;
  1710. }
  1711. }
  1712. av_log(s, AV_LOG_ERROR,
  1713. "max resync size reached, could not find sync byte\n");
  1714. /* no sync found */
  1715. return AVERROR_INVALIDDATA;
  1716. }
  1717. /* return AVERROR_something if error or EOF. Return 0 if OK. */
  1718. static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
  1719. const uint8_t **data)
  1720. {
  1721. AVIOContext *pb = s->pb;
  1722. int len;
  1723. for (;;) {
  1724. len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data);
  1725. if (len != TS_PACKET_SIZE)
  1726. return len < 0 ? len : AVERROR_EOF;
  1727. /* check packet sync byte */
  1728. if ((*data)[0] != 0x47) {
  1729. /* find a new packet start */
  1730. avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1731. if (mpegts_resync(s) < 0)
  1732. return AVERROR(EAGAIN);
  1733. else
  1734. continue;
  1735. } else {
  1736. break;
  1737. }
  1738. }
  1739. return 0;
  1740. }
  1741. static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
  1742. {
  1743. AVIOContext *pb = s->pb;
  1744. int skip = raw_packet_size - TS_PACKET_SIZE;
  1745. if (skip > 0)
  1746. avio_skip(pb, skip);
  1747. }
  1748. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1749. {
  1750. AVFormatContext *s = ts->stream;
  1751. uint8_t packet[TS_PACKET_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  1752. const uint8_t *data;
  1753. int packet_num, ret = 0;
  1754. if (avio_tell(s->pb) != ts->last_pos) {
  1755. int i;
  1756. av_log(ts->stream, AV_LOG_TRACE, "Skipping after seek\n");
  1757. /* seek detected, flush pes buffer */
  1758. for (i = 0; i < NB_PID_MAX; i++) {
  1759. if (ts->pids[i]) {
  1760. if (ts->pids[i]->type == MPEGTS_PES) {
  1761. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1762. av_buffer_unref(&pes->buffer);
  1763. pes->data_index = 0;
  1764. pes->state = MPEGTS_SKIP; /* skip until pes header */
  1765. }
  1766. ts->pids[i]->last_cc = -1;
  1767. }
  1768. }
  1769. }
  1770. ts->stop_parse = 0;
  1771. packet_num = 0;
  1772. memset(packet + TS_PACKET_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  1773. for (;;) {
  1774. if (ts->stop_parse > 0)
  1775. break;
  1776. packet_num++;
  1777. if (nb_packets != 0 && packet_num >= nb_packets)
  1778. break;
  1779. ret = read_packet(s, packet, ts->raw_packet_size, &data);
  1780. if (ret != 0)
  1781. break;
  1782. ret = handle_packet(ts, data);
  1783. finished_reading_packet(s, ts->raw_packet_size);
  1784. if (ret != 0)
  1785. break;
  1786. }
  1787. ts->last_pos = avio_tell(s->pb);
  1788. return ret;
  1789. }
  1790. static int mpegts_probe(AVProbeData *p)
  1791. {
  1792. const int size = p->buf_size;
  1793. int score, fec_score, dvhs_score;
  1794. int check_count = size / TS_FEC_PACKET_SIZE;
  1795. #define CHECK_COUNT 10
  1796. if (check_count < CHECK_COUNT)
  1797. return AVERROR_INVALIDDATA;
  1798. score = analyze(p->buf, TS_PACKET_SIZE * check_count,
  1799. TS_PACKET_SIZE, NULL, 1) * CHECK_COUNT / check_count;
  1800. dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE * check_count,
  1801. TS_DVHS_PACKET_SIZE, NULL, 1) * CHECK_COUNT / check_count;
  1802. fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE * check_count,
  1803. TS_FEC_PACKET_SIZE, NULL, 1) * CHECK_COUNT / check_count;
  1804. av_log(NULL, AV_LOG_TRACE, "score: %d, dvhs_score: %d, fec_score: %d \n",
  1805. score, dvhs_score, fec_score);
  1806. /* we need a clear definition for the returned score otherwise
  1807. * things will become messy sooner or later */
  1808. if (score > fec_score && score > dvhs_score && score > 6)
  1809. return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1810. else if (dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6)
  1811. return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1812. else if (fec_score > 6)
  1813. return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1814. else
  1815. return AVERROR_INVALIDDATA;
  1816. }
  1817. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1818. * (-1) if not available */
  1819. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
  1820. {
  1821. int afc, len, flags;
  1822. const uint8_t *p;
  1823. unsigned int v;
  1824. afc = (packet[3] >> 4) & 3;
  1825. if (afc <= 1)
  1826. return AVERROR_INVALIDDATA;
  1827. p = packet + 4;
  1828. len = p[0];
  1829. p++;
  1830. if (len == 0)
  1831. return AVERROR_INVALIDDATA;
  1832. flags = *p++;
  1833. len--;
  1834. if (!(flags & 0x10))
  1835. return AVERROR_INVALIDDATA;
  1836. if (len < 6)
  1837. return AVERROR_INVALIDDATA;
  1838. v = AV_RB32(p);
  1839. *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
  1840. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1841. return 0;
  1842. }
  1843. static int mpegts_read_header(AVFormatContext *s)
  1844. {
  1845. MpegTSContext *ts = s->priv_data;
  1846. AVIOContext *pb = s->pb;
  1847. uint8_t buf[5 * 1024];
  1848. int len;
  1849. int64_t pos;
  1850. /* read the first 1024 bytes to get packet size */
  1851. pos = avio_tell(pb);
  1852. len = avio_read(pb, buf, sizeof(buf));
  1853. if (len < 0)
  1854. return len;
  1855. if (len != sizeof(buf))
  1856. return AVERROR_BUG;
  1857. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1858. if (ts->raw_packet_size <= 0)
  1859. return AVERROR_INVALIDDATA;
  1860. ts->stream = s;
  1861. ts->auto_guess = 0;
  1862. if (s->iformat == &ff_mpegts_demuxer) {
  1863. /* normal demux */
  1864. /* first do a scan to get all the services */
  1865. if (avio_seek(pb, pos, SEEK_SET) < 0 && pb->seekable)
  1866. av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
  1867. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1868. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1869. handle_packets(ts, s->probesize / ts->raw_packet_size);
  1870. /* if could not find service, enable auto_guess */
  1871. ts->auto_guess = 1;
  1872. av_log(ts->stream, AV_LOG_TRACE, "tuning done\n");
  1873. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1874. } else {
  1875. AVStream *st;
  1876. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1877. int64_t pcrs[2], pcr_h;
  1878. int packet_count[2];
  1879. uint8_t packet[TS_PACKET_SIZE];
  1880. const uint8_t *data;
  1881. /* only read packets */
  1882. st = avformat_new_stream(s, NULL);
  1883. if (!st)
  1884. return AVERROR(ENOMEM);
  1885. avpriv_set_pts_info(st, 60, 1, 27000000);
  1886. st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
  1887. st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
  1888. /* we iterate until we find two PCRs to estimate the bitrate */
  1889. pcr_pid = -1;
  1890. nb_pcrs = 0;
  1891. nb_packets = 0;
  1892. for (;;) {
  1893. ret = read_packet(s, packet, ts->raw_packet_size, &data);
  1894. if (ret < 0)
  1895. return ret;
  1896. pid = AV_RB16(data + 1) & 0x1fff;
  1897. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1898. parse_pcr(&pcr_h, &pcr_l, data) == 0) {
  1899. finished_reading_packet(s, ts->raw_packet_size);
  1900. pcr_pid = pid;
  1901. packet_count[nb_pcrs] = nb_packets;
  1902. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1903. nb_pcrs++;
  1904. if (nb_pcrs >= 2)
  1905. break;
  1906. } else {
  1907. finished_reading_packet(s, ts->raw_packet_size);
  1908. }
  1909. nb_packets++;
  1910. }
  1911. /* NOTE1: the bitrate is computed without the FEC */
  1912. /* NOTE2: it is only the bitrate of the start of the stream */
  1913. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1914. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1915. s->bit_rate = TS_PACKET_SIZE * 8 * 27e6 / ts->pcr_incr;
  1916. st->codecpar->bit_rate = s->bit_rate;
  1917. st->start_time = ts->cur_pcr;
  1918. av_log(ts->stream, AV_LOG_TRACE, "start=%0.3f pcr=%0.3f incr=%d\n",
  1919. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1920. }
  1921. avio_seek(pb, pos, SEEK_SET);
  1922. return 0;
  1923. }
  1924. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1925. static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
  1926. {
  1927. MpegTSContext *ts = s->priv_data;
  1928. int ret, i;
  1929. int64_t pcr_h, next_pcr_h, pos;
  1930. int pcr_l, next_pcr_l;
  1931. uint8_t pcr_buf[12];
  1932. const uint8_t *data;
  1933. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1934. return AVERROR(ENOMEM);
  1935. ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
  1936. pkt->pos = avio_tell(s->pb);
  1937. if (ret < 0) {
  1938. av_packet_unref(pkt);
  1939. return ret;
  1940. }
  1941. if (data != pkt->data)
  1942. memcpy(pkt->data, data, ts->raw_packet_size);
  1943. finished_reading_packet(s, ts->raw_packet_size);
  1944. if (ts->mpeg2ts_compute_pcr) {
  1945. /* compute exact PCR for each packet */
  1946. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1947. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1948. pos = avio_tell(s->pb);
  1949. for (i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1950. avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1951. avio_read(s->pb, pcr_buf, 12);
  1952. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1953. /* XXX: not precise enough */
  1954. ts->pcr_incr =
  1955. ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1956. (i + 1);
  1957. break;
  1958. }
  1959. }
  1960. avio_seek(s->pb, pos, SEEK_SET);
  1961. /* no next PCR found: we use previous increment */
  1962. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1963. }
  1964. pkt->pts = ts->cur_pcr;
  1965. pkt->duration = ts->pcr_incr;
  1966. ts->cur_pcr += ts->pcr_incr;
  1967. }
  1968. pkt->stream_index = 0;
  1969. return 0;
  1970. }
  1971. static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
  1972. {
  1973. MpegTSContext *ts = s->priv_data;
  1974. int ret, i;
  1975. pkt->size = -1;
  1976. ts->pkt = pkt;
  1977. ret = handle_packets(ts, 0);
  1978. if (ret < 0) {
  1979. /* flush pes data left */
  1980. for (i = 0; i < NB_PID_MAX; i++)
  1981. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1982. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1983. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  1984. new_pes_packet(pes, pkt);
  1985. pes->state = MPEGTS_SKIP;
  1986. ret = 0;
  1987. break;
  1988. }
  1989. }
  1990. }
  1991. if (!ret && pkt->size < 0)
  1992. ret = AVERROR(EINTR);
  1993. return ret;
  1994. }
  1995. static void mpegts_free(MpegTSContext *ts)
  1996. {
  1997. int i;
  1998. clear_programs(ts);
  1999. for (i = 0; i < NB_PID_MAX; i++)
  2000. if (ts->pids[i])
  2001. mpegts_close_filter(ts, ts->pids[i]);
  2002. }
  2003. static int mpegts_read_close(AVFormatContext *s)
  2004. {
  2005. MpegTSContext *ts = s->priv_data;
  2006. mpegts_free(ts);
  2007. return 0;
  2008. }
  2009. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  2010. int64_t *ppos, int64_t pos_limit)
  2011. {
  2012. MpegTSContext *ts = s->priv_data;
  2013. int64_t pos, timestamp;
  2014. uint8_t buf[TS_PACKET_SIZE];
  2015. int pcr_l, pcr_pid =
  2016. ((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid;
  2017. const int find_next = 1;
  2018. pos =
  2019. ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) *
  2020. ts->raw_packet_size + ts->pos47;
  2021. if (find_next) {
  2022. for (;;) {
  2023. avio_seek(s->pb, pos, SEEK_SET);
  2024. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  2025. return AV_NOPTS_VALUE;
  2026. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  2027. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  2028. break;
  2029. }
  2030. pos += ts->raw_packet_size;
  2031. }
  2032. } else {
  2033. for (;;) {
  2034. pos -= ts->raw_packet_size;
  2035. if (pos < 0)
  2036. return AV_NOPTS_VALUE;
  2037. avio_seek(s->pb, pos, SEEK_SET);
  2038. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  2039. return AV_NOPTS_VALUE;
  2040. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  2041. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  2042. break;
  2043. }
  2044. }
  2045. }
  2046. *ppos = pos;
  2047. return timestamp;
  2048. }
  2049. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
  2050. {
  2051. MpegTSContext *ts = s->priv_data;
  2052. uint8_t buf[TS_PACKET_SIZE];
  2053. int64_t pos;
  2054. int ret;
  2055. ret = ff_seek_frame_binary(s, stream_index, target_ts, flags);
  2056. if (ret < 0)
  2057. return ret;
  2058. pos = avio_tell(s->pb);
  2059. for (;;) {
  2060. avio_seek(s->pb, pos, SEEK_SET);
  2061. ret = avio_read(s->pb, buf, TS_PACKET_SIZE);
  2062. if (ret < 0)
  2063. return ret;
  2064. if (ret != TS_PACKET_SIZE)
  2065. return AVERROR_EOF;
  2066. // pid = AV_RB16(buf + 1) & 0x1fff;
  2067. if (buf[1] & 0x40)
  2068. break;
  2069. pos += ts->raw_packet_size;
  2070. }
  2071. avio_seek(s->pb, pos, SEEK_SET);
  2072. return 0;
  2073. }
  2074. /**************************************************************/
  2075. /* parsing functions - called from other demuxers such as RTP */
  2076. MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
  2077. {
  2078. MpegTSContext *ts;
  2079. ts = av_mallocz(sizeof(MpegTSContext));
  2080. if (!ts)
  2081. return NULL;
  2082. /* no stream case, currently used by RTP */
  2083. ts->raw_packet_size = TS_PACKET_SIZE;
  2084. ts->stream = s;
  2085. ts->auto_guess = 1;
  2086. return ts;
  2087. }
  2088. /* return the consumed length if a packet was output, or -1 if no
  2089. * packet is output */
  2090. int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  2091. const uint8_t *buf, int len)
  2092. {
  2093. int len1;
  2094. len1 = len;
  2095. ts->pkt = pkt;
  2096. ts->stop_parse = 0;
  2097. for (;;) {
  2098. if (ts->stop_parse > 0)
  2099. break;
  2100. if (len < TS_PACKET_SIZE)
  2101. return AVERROR_INVALIDDATA;
  2102. if (buf[0] != 0x47) {
  2103. buf++;
  2104. len--;
  2105. } else {
  2106. handle_packet(ts, buf);
  2107. buf += TS_PACKET_SIZE;
  2108. len -= TS_PACKET_SIZE;
  2109. }
  2110. }
  2111. return len1 - len;
  2112. }
  2113. void ff_mpegts_parse_close(MpegTSContext *ts)
  2114. {
  2115. mpegts_free(ts);
  2116. av_free(ts);
  2117. }
  2118. AVInputFormat ff_mpegts_demuxer = {
  2119. .name = "mpegts",
  2120. .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
  2121. .priv_data_size = sizeof(MpegTSContext),
  2122. .read_probe = mpegts_probe,
  2123. .read_header = mpegts_read_header,
  2124. .read_packet = mpegts_read_packet,
  2125. .read_close = mpegts_read_close,
  2126. .read_seek = read_seek,
  2127. .read_timestamp = mpegts_get_pcr,
  2128. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  2129. .priv_class = &mpegts_class,
  2130. };
  2131. AVInputFormat ff_mpegtsraw_demuxer = {
  2132. .name = "mpegtsraw",
  2133. .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
  2134. .priv_data_size = sizeof(MpegTSContext),
  2135. .read_header = mpegts_read_header,
  2136. .read_packet = mpegts_raw_read_packet,
  2137. .read_close = mpegts_read_close,
  2138. .read_seek = read_seek,
  2139. .read_timestamp = mpegts_get_pcr,
  2140. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  2141. .priv_class = &mpegtsraw_class,
  2142. };