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.

2384 lines
76KB

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