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.

1886 lines
58KB

  1. /*
  2. * MPEG2 transport stream (aka DVB) demuxer
  3. * Copyright (c) 2002-2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. //#define DEBUG
  22. //#define DEBUG_SEEK
  23. //#define USE_SYNCPOINT_SEARCH
  24. #include "libavutil/crc.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavcodec/bytestream.h"
  27. #include "avformat.h"
  28. #include "mpegts.h"
  29. #include "internal.h"
  30. #include "avio_internal.h"
  31. #include "seek.h"
  32. #include "mpeg.h"
  33. #include "isom.h"
  34. /* maximum size in which we look for synchronisation if
  35. synchronisation is lost */
  36. #define MAX_RESYNC_SIZE 65536
  37. #define MAX_PES_PAYLOAD 200*1024
  38. enum MpegTSFilterType {
  39. MPEGTS_PES,
  40. MPEGTS_SECTION,
  41. };
  42. typedef struct MpegTSFilter MpegTSFilter;
  43. typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
  44. typedef struct MpegTSPESFilter {
  45. PESCallback *pes_cb;
  46. void *opaque;
  47. } MpegTSPESFilter;
  48. typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
  49. typedef void SetServiceCallback(void *opaque, int ret);
  50. typedef struct MpegTSSectionFilter {
  51. int section_index;
  52. int section_h_size;
  53. uint8_t *section_buf;
  54. unsigned int check_crc:1;
  55. unsigned int end_of_section_reached:1;
  56. SectionCallback *section_cb;
  57. void *opaque;
  58. } MpegTSSectionFilter;
  59. struct MpegTSFilter {
  60. int pid;
  61. int last_cc; /* last cc code (-1 if first packet) */
  62. enum MpegTSFilterType type;
  63. union {
  64. MpegTSPESFilter pes_filter;
  65. MpegTSSectionFilter section_filter;
  66. } u;
  67. };
  68. #define MAX_PIDS_PER_PROGRAM 64
  69. struct Program {
  70. unsigned int id; //program id/service id
  71. unsigned int nb_pids;
  72. unsigned int pids[MAX_PIDS_PER_PROGRAM];
  73. };
  74. struct MpegTSContext {
  75. /* user data */
  76. AVFormatContext *stream;
  77. /** raw packet size, including FEC if present */
  78. int raw_packet_size;
  79. int pos47;
  80. /** if true, all pids are analyzed to find streams */
  81. int auto_guess;
  82. /** compute exact PCR for each transport stream packet */
  83. int mpeg2ts_compute_pcr;
  84. int64_t cur_pcr; /**< used to estimate the exact PCR */
  85. int pcr_incr; /**< used to estimate the exact PCR */
  86. /* data needed to handle file based ts */
  87. /** stop parsing loop */
  88. int stop_parse;
  89. /** packet containing Audio/Video data */
  90. AVPacket *pkt;
  91. /** to detect seek */
  92. int64_t last_pos;
  93. /******************************************/
  94. /* private mpegts data */
  95. /* scan context */
  96. /** structure to keep track of Program->pids mapping */
  97. unsigned int nb_prg;
  98. struct Program *prg;
  99. /** filters for various streams specified by PMT + for the PAT and PMT */
  100. MpegTSFilter *pids[NB_PID_MAX];
  101. };
  102. /* TS stream handling */
  103. enum MpegTSState {
  104. MPEGTS_HEADER = 0,
  105. MPEGTS_PESHEADER,
  106. MPEGTS_PESHEADER_FILL,
  107. MPEGTS_PAYLOAD,
  108. MPEGTS_SKIP,
  109. };
  110. /* enough for PES header + length */
  111. #define PES_START_SIZE 6
  112. #define PES_HEADER_SIZE 9
  113. #define MAX_PES_HEADER_SIZE (9 + 255)
  114. typedef struct PESContext {
  115. int pid;
  116. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  117. int stream_type;
  118. MpegTSContext *ts;
  119. AVFormatContext *stream;
  120. AVStream *st;
  121. AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
  122. enum MpegTSState state;
  123. /* used to get the format */
  124. int data_index;
  125. int total_size;
  126. int pes_header_size;
  127. int extended_stream_id;
  128. int64_t pts, dts;
  129. int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
  130. uint8_t header[MAX_PES_HEADER_SIZE];
  131. uint8_t *buffer;
  132. } PESContext;
  133. extern AVInputFormat ff_mpegts_demuxer;
  134. static void clear_program(MpegTSContext *ts, unsigned int programid)
  135. {
  136. int i;
  137. for(i=0; i<ts->nb_prg; i++)
  138. if(ts->prg[i].id == programid)
  139. ts->prg[i].nb_pids = 0;
  140. }
  141. static void clear_programs(MpegTSContext *ts)
  142. {
  143. av_freep(&ts->prg);
  144. ts->nb_prg=0;
  145. }
  146. static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
  147. {
  148. struct Program *p;
  149. void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
  150. if(!tmp)
  151. return;
  152. ts->prg = tmp;
  153. p = &ts->prg[ts->nb_prg];
  154. p->id = programid;
  155. p->nb_pids = 0;
  156. ts->nb_prg++;
  157. }
  158. static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
  159. {
  160. int i;
  161. struct Program *p = NULL;
  162. for(i=0; i<ts->nb_prg; i++) {
  163. if(ts->prg[i].id == programid) {
  164. p = &ts->prg[i];
  165. break;
  166. }
  167. }
  168. if(!p)
  169. return;
  170. if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
  171. return;
  172. p->pids[p->nb_pids++] = pid;
  173. }
  174. /**
  175. * \brief discard_pid() decides if the pid is to be discarded according
  176. * to caller's programs selection
  177. * \param ts : - TS context
  178. * \param pid : - pid
  179. * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
  180. * 0 otherwise
  181. */
  182. static int discard_pid(MpegTSContext *ts, unsigned int pid)
  183. {
  184. int i, j, k;
  185. int used = 0, discarded = 0;
  186. struct Program *p;
  187. for(i=0; i<ts->nb_prg; i++) {
  188. p = &ts->prg[i];
  189. for(j=0; j<p->nb_pids; j++) {
  190. if(p->pids[j] != pid)
  191. continue;
  192. //is program with id p->id set to be discarded?
  193. for(k=0; k<ts->stream->nb_programs; k++) {
  194. if(ts->stream->programs[k]->id == p->id) {
  195. if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
  196. discarded++;
  197. else
  198. used++;
  199. }
  200. }
  201. }
  202. }
  203. return !used && discarded;
  204. }
  205. /**
  206. * Assemble PES packets out of TS packets, and then call the "section_cb"
  207. * function when they are complete.
  208. */
  209. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  210. const uint8_t *buf, int buf_size, int is_start)
  211. {
  212. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  213. int len;
  214. if (is_start) {
  215. memcpy(tss->section_buf, buf, buf_size);
  216. tss->section_index = buf_size;
  217. tss->section_h_size = -1;
  218. tss->end_of_section_reached = 0;
  219. } else {
  220. if (tss->end_of_section_reached)
  221. return;
  222. len = 4096 - tss->section_index;
  223. if (buf_size < len)
  224. len = buf_size;
  225. memcpy(tss->section_buf + tss->section_index, buf, len);
  226. tss->section_index += len;
  227. }
  228. /* compute section length if possible */
  229. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  230. len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
  231. if (len > 4096)
  232. return;
  233. tss->section_h_size = len;
  234. }
  235. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  236. tss->end_of_section_reached = 1;
  237. if (!tss->check_crc ||
  238. av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
  239. tss->section_buf, tss->section_h_size) == 0)
  240. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  241. }
  242. }
  243. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  244. SectionCallback *section_cb, void *opaque,
  245. int check_crc)
  246. {
  247. MpegTSFilter *filter;
  248. MpegTSSectionFilter *sec;
  249. av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
  250. if (pid >= NB_PID_MAX || ts->pids[pid])
  251. return NULL;
  252. filter = av_mallocz(sizeof(MpegTSFilter));
  253. if (!filter)
  254. return NULL;
  255. ts->pids[pid] = filter;
  256. filter->type = MPEGTS_SECTION;
  257. filter->pid = pid;
  258. filter->last_cc = -1;
  259. sec = &filter->u.section_filter;
  260. sec->section_cb = section_cb;
  261. sec->opaque = opaque;
  262. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  263. sec->check_crc = check_crc;
  264. if (!sec->section_buf) {
  265. av_free(filter);
  266. return NULL;
  267. }
  268. return filter;
  269. }
  270. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  271. PESCallback *pes_cb,
  272. void *opaque)
  273. {
  274. MpegTSFilter *filter;
  275. MpegTSPESFilter *pes;
  276. if (pid >= NB_PID_MAX || ts->pids[pid])
  277. return NULL;
  278. filter = av_mallocz(sizeof(MpegTSFilter));
  279. if (!filter)
  280. return NULL;
  281. ts->pids[pid] = filter;
  282. filter->type = MPEGTS_PES;
  283. filter->pid = pid;
  284. filter->last_cc = -1;
  285. pes = &filter->u.pes_filter;
  286. pes->pes_cb = pes_cb;
  287. pes->opaque = opaque;
  288. return filter;
  289. }
  290. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  291. {
  292. int pid;
  293. pid = filter->pid;
  294. if (filter->type == MPEGTS_SECTION)
  295. av_freep(&filter->u.section_filter.section_buf);
  296. else if (filter->type == MPEGTS_PES) {
  297. PESContext *pes = filter->u.pes_filter.opaque;
  298. av_freep(&pes->buffer);
  299. /* referenced private data will be freed later in
  300. * av_close_input_stream */
  301. if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
  302. av_freep(&filter->u.pes_filter.opaque);
  303. }
  304. }
  305. av_free(filter);
  306. ts->pids[pid] = NULL;
  307. }
  308. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  309. int stat[TS_MAX_PACKET_SIZE];
  310. int i;
  311. int x=0;
  312. int best_score=0;
  313. memset(stat, 0, packet_size*sizeof(int));
  314. for(x=i=0; i<size-3; i++){
  315. if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
  316. stat[x]++;
  317. if(stat[x] > best_score){
  318. best_score= stat[x];
  319. if(index) *index= x;
  320. }
  321. }
  322. x++;
  323. if(x == packet_size) x= 0;
  324. }
  325. return best_score;
  326. }
  327. /* autodetect fec presence. Must have at least 1024 bytes */
  328. static int get_packet_size(const uint8_t *buf, int size)
  329. {
  330. int score, fec_score, dvhs_score;
  331. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  332. return -1;
  333. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  334. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  335. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  336. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  337. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  338. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  339. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  340. else return -1;
  341. }
  342. typedef struct SectionHeader {
  343. uint8_t tid;
  344. uint16_t id;
  345. uint8_t version;
  346. uint8_t sec_num;
  347. uint8_t last_sec_num;
  348. } SectionHeader;
  349. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  350. {
  351. const uint8_t *p;
  352. int c;
  353. p = *pp;
  354. if (p >= p_end)
  355. return -1;
  356. c = *p++;
  357. *pp = p;
  358. return c;
  359. }
  360. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  361. {
  362. const uint8_t *p;
  363. int c;
  364. p = *pp;
  365. if ((p + 1) >= p_end)
  366. return -1;
  367. c = AV_RB16(p);
  368. p += 2;
  369. *pp = p;
  370. return c;
  371. }
  372. /* read and allocate a DVB string preceeded by its length */
  373. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  374. {
  375. int len;
  376. const uint8_t *p;
  377. char *str;
  378. p = *pp;
  379. len = get8(&p, p_end);
  380. if (len < 0)
  381. return NULL;
  382. if ((p + len) > p_end)
  383. return NULL;
  384. str = av_malloc(len + 1);
  385. if (!str)
  386. return NULL;
  387. memcpy(str, p, len);
  388. str[len] = '\0';
  389. p += len;
  390. *pp = p;
  391. return str;
  392. }
  393. static int parse_section_header(SectionHeader *h,
  394. const uint8_t **pp, const uint8_t *p_end)
  395. {
  396. int val;
  397. val = get8(pp, p_end);
  398. if (val < 0)
  399. return -1;
  400. h->tid = val;
  401. *pp += 2;
  402. val = get16(pp, p_end);
  403. if (val < 0)
  404. return -1;
  405. h->id = val;
  406. val = get8(pp, p_end);
  407. if (val < 0)
  408. return -1;
  409. h->version = (val >> 1) & 0x1f;
  410. val = get8(pp, p_end);
  411. if (val < 0)
  412. return -1;
  413. h->sec_num = val;
  414. val = get8(pp, p_end);
  415. if (val < 0)
  416. return -1;
  417. h->last_sec_num = val;
  418. return 0;
  419. }
  420. typedef struct {
  421. uint32_t stream_type;
  422. enum AVMediaType codec_type;
  423. enum CodecID codec_id;
  424. } StreamType;
  425. static const StreamType ISO_types[] = {
  426. { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
  427. { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
  428. { 0x03, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
  429. { 0x04, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
  430. { 0x0f, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC },
  431. { 0x10, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG4 },
  432. { 0x11, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC_LATM }, /* LATM syntax */
  433. { 0x1b, AVMEDIA_TYPE_VIDEO, CODEC_ID_H264 },
  434. { 0xd1, AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
  435. { 0xea, AVMEDIA_TYPE_VIDEO, CODEC_ID_VC1 },
  436. { 0 },
  437. };
  438. static const StreamType HDMV_types[] = {
  439. { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
  440. { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  441. { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  442. { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
  443. { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
  444. { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
  445. { 0 },
  446. };
  447. /* ATSC ? */
  448. static const StreamType MISC_types[] = {
  449. { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  450. { 0x8a, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  451. { 0 },
  452. };
  453. static const StreamType REGD_types[] = {
  454. { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
  455. { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  456. { 0 },
  457. };
  458. /* descriptor present */
  459. static const StreamType DESC_types[] = {
  460. { 0x6a, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */
  461. { 0x7a, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
  462. { 0x7b, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  463. { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
  464. { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
  465. { 0 },
  466. };
  467. static void mpegts_find_stream_type(AVStream *st,
  468. uint32_t stream_type, const StreamType *types)
  469. {
  470. for (; types->stream_type; types++) {
  471. if (stream_type == types->stream_type) {
  472. st->codec->codec_type = types->codec_type;
  473. st->codec->codec_id = types->codec_id;
  474. return;
  475. }
  476. }
  477. }
  478. static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
  479. uint32_t stream_type, uint32_t prog_reg_desc)
  480. {
  481. av_set_pts_info(st, 33, 1, 90000);
  482. st->priv_data = pes;
  483. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  484. st->codec->codec_id = CODEC_ID_NONE;
  485. st->need_parsing = AVSTREAM_PARSE_FULL;
  486. pes->st = st;
  487. pes->stream_type = stream_type;
  488. av_log(pes->stream, AV_LOG_DEBUG,
  489. "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
  490. st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
  491. st->codec->codec_tag = pes->stream_type;
  492. mpegts_find_stream_type(st, pes->stream_type, ISO_types);
  493. if (prog_reg_desc == AV_RL32("HDMV") &&
  494. st->codec->codec_id == CODEC_ID_NONE) {
  495. mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
  496. if (pes->stream_type == 0x83) {
  497. // HDMV TrueHD streams also contain an AC3 coded version of the
  498. // audio track - add a second stream for this
  499. AVStream *sub_st;
  500. // priv_data cannot be shared between streams
  501. PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
  502. if (!sub_pes)
  503. return AVERROR(ENOMEM);
  504. memcpy(sub_pes, pes, sizeof(*sub_pes));
  505. sub_st = av_new_stream(pes->stream, pes->pid);
  506. if (!sub_st) {
  507. av_free(sub_pes);
  508. return AVERROR(ENOMEM);
  509. }
  510. av_set_pts_info(sub_st, 33, 1, 90000);
  511. sub_st->priv_data = sub_pes;
  512. sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  513. sub_st->codec->codec_id = CODEC_ID_AC3;
  514. sub_st->need_parsing = AVSTREAM_PARSE_FULL;
  515. sub_pes->sub_st = pes->sub_st = sub_st;
  516. }
  517. }
  518. if (st->codec->codec_id == CODEC_ID_NONE)
  519. mpegts_find_stream_type(st, pes->stream_type, MISC_types);
  520. return 0;
  521. }
  522. static void new_pes_packet(PESContext *pes, AVPacket *pkt)
  523. {
  524. av_init_packet(pkt);
  525. pkt->destruct = av_destruct_packet;
  526. pkt->data = pes->buffer;
  527. pkt->size = pes->data_index;
  528. memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  529. // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
  530. if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
  531. pkt->stream_index = pes->sub_st->index;
  532. else
  533. pkt->stream_index = pes->st->index;
  534. pkt->pts = pes->pts;
  535. pkt->dts = pes->dts;
  536. /* store position of first TS packet of this PES packet */
  537. pkt->pos = pes->ts_packet_pos;
  538. /* reset pts values */
  539. pes->pts = AV_NOPTS_VALUE;
  540. pes->dts = AV_NOPTS_VALUE;
  541. pes->buffer = NULL;
  542. pes->data_index = 0;
  543. }
  544. /* return non zero if a packet could be constructed */
  545. static int mpegts_push_data(MpegTSFilter *filter,
  546. const uint8_t *buf, int buf_size, int is_start,
  547. int64_t pos)
  548. {
  549. PESContext *pes = filter->u.pes_filter.opaque;
  550. MpegTSContext *ts = pes->ts;
  551. const uint8_t *p;
  552. int len, code;
  553. if(!ts->pkt)
  554. return 0;
  555. if (is_start) {
  556. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  557. new_pes_packet(pes, ts->pkt);
  558. ts->stop_parse = 1;
  559. }
  560. pes->state = MPEGTS_HEADER;
  561. pes->data_index = 0;
  562. pes->ts_packet_pos = pos;
  563. }
  564. p = buf;
  565. while (buf_size > 0) {
  566. switch(pes->state) {
  567. case MPEGTS_HEADER:
  568. len = PES_START_SIZE - pes->data_index;
  569. if (len > buf_size)
  570. len = buf_size;
  571. memcpy(pes->header + pes->data_index, p, len);
  572. pes->data_index += len;
  573. p += len;
  574. buf_size -= len;
  575. if (pes->data_index == PES_START_SIZE) {
  576. /* we got all the PES or section header. We can now
  577. decide */
  578. #if 0
  579. av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
  580. #endif
  581. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  582. pes->header[2] == 0x01) {
  583. /* it must be an mpeg2 PES stream */
  584. code = pes->header[3] | 0x100;
  585. av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
  586. if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
  587. code == 0x1be) /* padding_stream */
  588. goto skip;
  589. /* stream not present in PMT */
  590. if (!pes->st) {
  591. pes->st = av_new_stream(ts->stream, pes->pid);
  592. if (!pes->st)
  593. return AVERROR(ENOMEM);
  594. mpegts_set_stream_info(pes->st, pes, 0, 0);
  595. }
  596. pes->total_size = AV_RB16(pes->header + 4);
  597. /* NOTE: a zero total size means the PES size is
  598. unbounded */
  599. if (!pes->total_size)
  600. pes->total_size = MAX_PES_PAYLOAD;
  601. /* allocate pes buffer */
  602. pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
  603. if (!pes->buffer)
  604. return AVERROR(ENOMEM);
  605. if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
  606. code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
  607. code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
  608. code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
  609. pes->state = MPEGTS_PESHEADER;
  610. if (pes->st->codec->codec_id == CODEC_ID_NONE) {
  611. av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
  612. pes->pid, pes->stream_type);
  613. pes->st->codec->codec_id = CODEC_ID_PROBE;
  614. }
  615. } else {
  616. pes->state = MPEGTS_PAYLOAD;
  617. pes->data_index = 0;
  618. }
  619. } else {
  620. /* otherwise, it should be a table */
  621. /* skip packet */
  622. skip:
  623. pes->state = MPEGTS_SKIP;
  624. continue;
  625. }
  626. }
  627. break;
  628. /**********************************************/
  629. /* PES packing parsing */
  630. case MPEGTS_PESHEADER:
  631. len = PES_HEADER_SIZE - pes->data_index;
  632. if (len < 0)
  633. return -1;
  634. if (len > buf_size)
  635. len = buf_size;
  636. memcpy(pes->header + pes->data_index, p, len);
  637. pes->data_index += len;
  638. p += len;
  639. buf_size -= len;
  640. if (pes->data_index == PES_HEADER_SIZE) {
  641. pes->pes_header_size = pes->header[8] + 9;
  642. pes->state = MPEGTS_PESHEADER_FILL;
  643. }
  644. break;
  645. case MPEGTS_PESHEADER_FILL:
  646. len = pes->pes_header_size - pes->data_index;
  647. if (len < 0)
  648. return -1;
  649. if (len > buf_size)
  650. len = buf_size;
  651. memcpy(pes->header + pes->data_index, p, len);
  652. pes->data_index += len;
  653. p += len;
  654. buf_size -= len;
  655. if (pes->data_index == pes->pes_header_size) {
  656. const uint8_t *r;
  657. unsigned int flags, pes_ext, skip;
  658. flags = pes->header[7];
  659. r = pes->header + 9;
  660. pes->pts = AV_NOPTS_VALUE;
  661. pes->dts = AV_NOPTS_VALUE;
  662. if ((flags & 0xc0) == 0x80) {
  663. pes->dts = pes->pts = ff_parse_pes_pts(r);
  664. r += 5;
  665. } else if ((flags & 0xc0) == 0xc0) {
  666. pes->pts = ff_parse_pes_pts(r);
  667. r += 5;
  668. pes->dts = ff_parse_pes_pts(r);
  669. r += 5;
  670. }
  671. pes->extended_stream_id = -1;
  672. if (flags & 0x01) { /* PES extension */
  673. pes_ext = *r++;
  674. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  675. skip = (pes_ext >> 4) & 0xb;
  676. skip += skip & 0x9;
  677. r += skip;
  678. if ((pes_ext & 0x41) == 0x01 &&
  679. (r + 2) <= (pes->header + pes->pes_header_size)) {
  680. /* PES extension 2 */
  681. if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
  682. pes->extended_stream_id = r[1];
  683. }
  684. }
  685. /* we got the full header. We parse it and get the payload */
  686. pes->state = MPEGTS_PAYLOAD;
  687. pes->data_index = 0;
  688. }
  689. break;
  690. case MPEGTS_PAYLOAD:
  691. if (buf_size > 0 && pes->buffer) {
  692. if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
  693. new_pes_packet(pes, ts->pkt);
  694. pes->total_size = MAX_PES_PAYLOAD;
  695. pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
  696. if (!pes->buffer)
  697. return AVERROR(ENOMEM);
  698. ts->stop_parse = 1;
  699. } else if (pes->data_index == 0 && buf_size > pes->total_size) {
  700. // pes packet size is < ts size packet and pes data is padded with 0xff
  701. // not sure if this is legal in ts but see issue #2392
  702. buf_size = pes->total_size;
  703. }
  704. memcpy(pes->buffer+pes->data_index, p, buf_size);
  705. pes->data_index += buf_size;
  706. }
  707. buf_size = 0;
  708. /* emit complete packets with known packet size
  709. * decreases demuxer delay for infrequent packets like subtitles from
  710. * a couple of seconds to milliseconds for properly muxed files.
  711. * total_size is the number of bytes following pes_packet_length
  712. * in the pes header, i.e. not counting the first 6 bytes */
  713. if (pes->total_size < MAX_PES_PAYLOAD &&
  714. pes->pes_header_size + pes->data_index == pes->total_size + 6) {
  715. ts->stop_parse = 1;
  716. new_pes_packet(pes, ts->pkt);
  717. }
  718. break;
  719. case MPEGTS_SKIP:
  720. buf_size = 0;
  721. break;
  722. }
  723. }
  724. return 0;
  725. }
  726. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
  727. {
  728. MpegTSFilter *tss;
  729. PESContext *pes;
  730. /* if no pid found, then add a pid context */
  731. pes = av_mallocz(sizeof(PESContext));
  732. if (!pes)
  733. return 0;
  734. pes->ts = ts;
  735. pes->stream = ts->stream;
  736. pes->pid = pid;
  737. pes->pcr_pid = pcr_pid;
  738. pes->state = MPEGTS_SKIP;
  739. pes->pts = AV_NOPTS_VALUE;
  740. pes->dts = AV_NOPTS_VALUE;
  741. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  742. if (!tss) {
  743. av_free(pes);
  744. return 0;
  745. }
  746. return pes;
  747. }
  748. static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
  749. int *es_id, uint8_t **dec_config_descr,
  750. int *dec_config_descr_size)
  751. {
  752. AVIOContext pb;
  753. int tag;
  754. unsigned len;
  755. ffio_init_context(&pb, buf, size, 0, NULL, NULL, NULL, NULL);
  756. len = ff_mp4_read_descr(s, &pb, &tag);
  757. if (tag == MP4IODescrTag) {
  758. avio_rb16(&pb); // ID
  759. avio_r8(&pb);
  760. avio_r8(&pb);
  761. avio_r8(&pb);
  762. avio_r8(&pb);
  763. avio_r8(&pb);
  764. len = ff_mp4_read_descr(s, &pb, &tag);
  765. if (tag == MP4ESDescrTag) {
  766. *es_id = avio_rb16(&pb); /* ES_ID */
  767. av_dlog(s, "ES_ID %#x\n", *es_id);
  768. avio_r8(&pb); /* priority */
  769. len = ff_mp4_read_descr(s, &pb, &tag);
  770. if (tag == MP4DecConfigDescrTag) {
  771. *dec_config_descr = av_malloc(len);
  772. if (!*dec_config_descr)
  773. return AVERROR(ENOMEM);
  774. *dec_config_descr_size = len;
  775. avio_read(&pb, *dec_config_descr, len);
  776. }
  777. }
  778. }
  779. return 0;
  780. }
  781. int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
  782. const uint8_t **pp, const uint8_t *desc_list_end,
  783. int mp4_dec_config_descr_len, int mp4_es_id, int pid,
  784. uint8_t *mp4_dec_config_descr)
  785. {
  786. const uint8_t *desc_end;
  787. int desc_len, desc_tag;
  788. char language[252];
  789. int i;
  790. desc_tag = get8(pp, desc_list_end);
  791. if (desc_tag < 0)
  792. return -1;
  793. desc_len = get8(pp, desc_list_end);
  794. if (desc_len < 0)
  795. return -1;
  796. desc_end = *pp + desc_len;
  797. if (desc_end > desc_list_end)
  798. return -1;
  799. av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
  800. if (st->codec->codec_id == CODEC_ID_NONE &&
  801. stream_type == STREAM_TYPE_PRIVATE_DATA)
  802. mpegts_find_stream_type(st, desc_tag, DESC_types);
  803. switch(desc_tag) {
  804. case 0x1F: /* FMC descriptor */
  805. get16(pp, desc_end);
  806. if (st->codec->codec_id == CODEC_ID_AAC_LATM &&
  807. mp4_dec_config_descr_len && mp4_es_id == pid) {
  808. AVIOContext pb;
  809. ffio_init_context(&pb, mp4_dec_config_descr,
  810. mp4_dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  811. ff_mp4_read_dec_config_descr(fc, st, &pb);
  812. if (st->codec->codec_id == CODEC_ID_AAC &&
  813. st->codec->extradata_size > 0)
  814. st->need_parsing = 0;
  815. }
  816. break;
  817. case 0x56: /* DVB teletext descriptor */
  818. language[0] = get8(pp, desc_end);
  819. language[1] = get8(pp, desc_end);
  820. language[2] = get8(pp, desc_end);
  821. language[3] = 0;
  822. av_metadata_set2(&st->metadata, "language", language, 0);
  823. break;
  824. case 0x59: /* subtitling descriptor */
  825. language[0] = get8(pp, desc_end);
  826. language[1] = get8(pp, desc_end);
  827. language[2] = get8(pp, desc_end);
  828. language[3] = 0;
  829. /* hearing impaired subtitles detection */
  830. switch(get8(pp, desc_end)) {
  831. case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
  832. case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
  833. case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
  834. case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
  835. case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
  836. case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
  837. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  838. break;
  839. }
  840. if (st->codec->extradata) {
  841. if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
  842. av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
  843. } else {
  844. st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
  845. if (st->codec->extradata) {
  846. st->codec->extradata_size = 4;
  847. memcpy(st->codec->extradata, *pp, 4);
  848. }
  849. }
  850. *pp += 4;
  851. av_metadata_set2(&st->metadata, "language", language, 0);
  852. break;
  853. case 0x0a: /* ISO 639 language descriptor */
  854. for (i = 0; i + 4 <= desc_len; i += 4) {
  855. language[i + 0] = get8(pp, desc_end);
  856. language[i + 1] = get8(pp, desc_end);
  857. language[i + 2] = get8(pp, desc_end);
  858. language[i + 3] = ',';
  859. switch (get8(pp, desc_end)) {
  860. case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
  861. case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
  862. case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
  863. }
  864. }
  865. if (i) {
  866. language[i - 1] = 0;
  867. av_metadata_set2(&st->metadata, "language", language, 0);
  868. }
  869. break;
  870. case 0x05: /* registration descriptor */
  871. st->codec->codec_tag = bytestream_get_le32(pp);
  872. av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
  873. if (st->codec->codec_id == CODEC_ID_NONE &&
  874. stream_type == STREAM_TYPE_PRIVATE_DATA)
  875. mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
  876. break;
  877. default:
  878. break;
  879. }
  880. *pp = desc_end;
  881. return 0;
  882. }
  883. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  884. {
  885. MpegTSContext *ts = filter->u.section_filter.opaque;
  886. SectionHeader h1, *h = &h1;
  887. PESContext *pes;
  888. AVStream *st;
  889. const uint8_t *p, *p_end, *desc_list_end;
  890. int program_info_length, pcr_pid, pid, stream_type;
  891. int desc_list_len;
  892. uint32_t prog_reg_desc = 0; /* registration descriptor */
  893. uint8_t *mp4_dec_config_descr = NULL;
  894. int mp4_dec_config_descr_len = 0;
  895. int mp4_es_id = 0;
  896. #ifdef DEBUG
  897. av_dlog(ts->stream, "PMT: len %i\n", section_len);
  898. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  899. #endif
  900. p_end = section + section_len - 4;
  901. p = section;
  902. if (parse_section_header(h, &p, p_end) < 0)
  903. return;
  904. av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
  905. h->id, h->sec_num, h->last_sec_num);
  906. if (h->tid != PMT_TID)
  907. return;
  908. clear_program(ts, h->id);
  909. pcr_pid = get16(&p, p_end) & 0x1fff;
  910. if (pcr_pid < 0)
  911. return;
  912. add_pid_to_pmt(ts, h->id, pcr_pid);
  913. av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
  914. program_info_length = get16(&p, p_end) & 0xfff;
  915. if (program_info_length < 0)
  916. return;
  917. while(program_info_length >= 2) {
  918. uint8_t tag, len;
  919. tag = get8(&p, p_end);
  920. len = get8(&p, p_end);
  921. av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
  922. if(len > program_info_length - 2)
  923. //something else is broken, exit the program_descriptors_loop
  924. break;
  925. program_info_length -= len + 2;
  926. if (tag == 0x1d) { // IOD descriptor
  927. get8(&p, p_end); // scope
  928. get8(&p, p_end); // label
  929. len -= 2;
  930. mp4_read_iods(ts->stream, p, len, &mp4_es_id,
  931. &mp4_dec_config_descr, &mp4_dec_config_descr_len);
  932. } else if (tag == 0x05 && len >= 4) { // registration descriptor
  933. prog_reg_desc = bytestream_get_le32(&p);
  934. len -= 4;
  935. }
  936. p += len;
  937. }
  938. p += program_info_length;
  939. if (p >= p_end)
  940. goto out;
  941. // stop parsing after pmt, we found header
  942. if (!ts->stream->nb_streams)
  943. ts->stop_parse = 1;
  944. for(;;) {
  945. st = 0;
  946. stream_type = get8(&p, p_end);
  947. if (stream_type < 0)
  948. break;
  949. pid = get16(&p, p_end) & 0x1fff;
  950. if (pid < 0)
  951. break;
  952. /* now create ffmpeg stream */
  953. if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
  954. pes = ts->pids[pid]->u.pes_filter.opaque;
  955. if (!pes->st)
  956. pes->st = av_new_stream(pes->stream, pes->pid);
  957. st = pes->st;
  958. } else {
  959. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  960. pes = add_pes_stream(ts, pid, pcr_pid);
  961. if (pes)
  962. st = av_new_stream(pes->stream, pes->pid);
  963. }
  964. if (!st)
  965. goto out;
  966. if (!pes->stream_type)
  967. mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
  968. add_pid_to_pmt(ts, h->id, pid);
  969. ff_program_add_stream_index(ts->stream, h->id, st->index);
  970. desc_list_len = get16(&p, p_end) & 0xfff;
  971. if (desc_list_len < 0)
  972. break;
  973. desc_list_end = p + desc_list_len;
  974. if (desc_list_end > p_end)
  975. break;
  976. for(;;) {
  977. if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
  978. mp4_dec_config_descr_len, mp4_es_id, pid, mp4_dec_config_descr) < 0)
  979. break;
  980. if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
  981. ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
  982. pes->sub_st->codec->codec_tag = st->codec->codec_tag;
  983. }
  984. }
  985. p = desc_list_end;
  986. }
  987. out:
  988. av_free(mp4_dec_config_descr);
  989. }
  990. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  991. {
  992. MpegTSContext *ts = filter->u.section_filter.opaque;
  993. SectionHeader h1, *h = &h1;
  994. const uint8_t *p, *p_end;
  995. int sid, pmt_pid;
  996. #ifdef DEBUG
  997. av_dlog(ts->stream, "PAT:\n");
  998. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  999. #endif
  1000. p_end = section + section_len - 4;
  1001. p = section;
  1002. if (parse_section_header(h, &p, p_end) < 0)
  1003. return;
  1004. if (h->tid != PAT_TID)
  1005. return;
  1006. clear_programs(ts);
  1007. for(;;) {
  1008. sid = get16(&p, p_end);
  1009. if (sid < 0)
  1010. break;
  1011. pmt_pid = get16(&p, p_end) & 0x1fff;
  1012. if (pmt_pid < 0)
  1013. break;
  1014. av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  1015. if (sid == 0x0000) {
  1016. /* NIT info */
  1017. } else {
  1018. av_new_program(ts->stream, sid);
  1019. if (ts->pids[pmt_pid])
  1020. mpegts_close_filter(ts, ts->pids[pmt_pid]);
  1021. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  1022. add_pat_entry(ts, sid);
  1023. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  1024. add_pid_to_pmt(ts, sid, pmt_pid);
  1025. }
  1026. }
  1027. }
  1028. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1029. {
  1030. MpegTSContext *ts = filter->u.section_filter.opaque;
  1031. SectionHeader h1, *h = &h1;
  1032. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  1033. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  1034. char *name, *provider_name;
  1035. #ifdef DEBUG
  1036. av_dlog(ts->stream, "SDT:\n");
  1037. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  1038. #endif
  1039. p_end = section + section_len - 4;
  1040. p = section;
  1041. if (parse_section_header(h, &p, p_end) < 0)
  1042. return;
  1043. if (h->tid != SDT_TID)
  1044. return;
  1045. onid = get16(&p, p_end);
  1046. if (onid < 0)
  1047. return;
  1048. val = get8(&p, p_end);
  1049. if (val < 0)
  1050. return;
  1051. for(;;) {
  1052. sid = get16(&p, p_end);
  1053. if (sid < 0)
  1054. break;
  1055. val = get8(&p, p_end);
  1056. if (val < 0)
  1057. break;
  1058. desc_list_len = get16(&p, p_end) & 0xfff;
  1059. if (desc_list_len < 0)
  1060. break;
  1061. desc_list_end = p + desc_list_len;
  1062. if (desc_list_end > p_end)
  1063. break;
  1064. for(;;) {
  1065. desc_tag = get8(&p, desc_list_end);
  1066. if (desc_tag < 0)
  1067. break;
  1068. desc_len = get8(&p, desc_list_end);
  1069. desc_end = p + desc_len;
  1070. if (desc_end > desc_list_end)
  1071. break;
  1072. av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
  1073. desc_tag, desc_len);
  1074. switch(desc_tag) {
  1075. case 0x48:
  1076. service_type = get8(&p, p_end);
  1077. if (service_type < 0)
  1078. break;
  1079. provider_name = getstr8(&p, p_end);
  1080. if (!provider_name)
  1081. break;
  1082. name = getstr8(&p, p_end);
  1083. if (name) {
  1084. AVProgram *program = av_new_program(ts->stream, sid);
  1085. if(program) {
  1086. av_metadata_set2(&program->metadata, "service_name", name, 0);
  1087. av_metadata_set2(&program->metadata, "service_provider", provider_name, 0);
  1088. }
  1089. }
  1090. av_free(name);
  1091. av_free(provider_name);
  1092. break;
  1093. default:
  1094. break;
  1095. }
  1096. p = desc_end;
  1097. }
  1098. p = desc_list_end;
  1099. }
  1100. }
  1101. /* handle one TS packet */
  1102. static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  1103. {
  1104. AVFormatContext *s = ts->stream;
  1105. MpegTSFilter *tss;
  1106. int len, pid, cc, cc_ok, afc, is_start;
  1107. const uint8_t *p, *p_end;
  1108. int64_t pos;
  1109. pid = AV_RB16(packet + 1) & 0x1fff;
  1110. if(pid && discard_pid(ts, pid))
  1111. return 0;
  1112. is_start = packet[1] & 0x40;
  1113. tss = ts->pids[pid];
  1114. if (ts->auto_guess && tss == NULL && is_start) {
  1115. add_pes_stream(ts, pid, -1);
  1116. tss = ts->pids[pid];
  1117. }
  1118. if (!tss)
  1119. return 0;
  1120. /* continuity check (currently not used) */
  1121. cc = (packet[3] & 0xf);
  1122. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  1123. tss->last_cc = cc;
  1124. /* skip adaptation field */
  1125. afc = (packet[3] >> 4) & 3;
  1126. p = packet + 4;
  1127. if (afc == 0) /* reserved value */
  1128. return 0;
  1129. if (afc == 2) /* adaptation field only */
  1130. return 0;
  1131. if (afc == 3) {
  1132. /* skip adapation field */
  1133. p += p[0] + 1;
  1134. }
  1135. /* if past the end of packet, ignore */
  1136. p_end = packet + TS_PACKET_SIZE;
  1137. if (p >= p_end)
  1138. return 0;
  1139. pos = avio_tell(ts->stream->pb);
  1140. ts->pos47= pos % ts->raw_packet_size;
  1141. if (tss->type == MPEGTS_SECTION) {
  1142. if (is_start) {
  1143. /* pointer field present */
  1144. len = *p++;
  1145. if (p + len > p_end)
  1146. return 0;
  1147. if (len && cc_ok) {
  1148. /* write remaining section bytes */
  1149. write_section_data(s, tss,
  1150. p, len, 0);
  1151. /* check whether filter has been closed */
  1152. if (!ts->pids[pid])
  1153. return 0;
  1154. }
  1155. p += len;
  1156. if (p < p_end) {
  1157. write_section_data(s, tss,
  1158. p, p_end - p, 1);
  1159. }
  1160. } else {
  1161. if (cc_ok) {
  1162. write_section_data(s, tss,
  1163. p, p_end - p, 0);
  1164. }
  1165. }
  1166. } else {
  1167. int ret;
  1168. // Note: The position here points actually behind the current packet.
  1169. if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
  1170. pos - ts->raw_packet_size)) < 0)
  1171. return ret;
  1172. }
  1173. return 0;
  1174. }
  1175. /* XXX: try to find a better synchro over several packets (use
  1176. get_packet_size() ?) */
  1177. static int mpegts_resync(AVFormatContext *s)
  1178. {
  1179. AVIOContext *pb = s->pb;
  1180. int c, i;
  1181. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1182. c = avio_r8(pb);
  1183. if (pb->eof_reached)
  1184. return -1;
  1185. if (c == 0x47) {
  1186. avio_seek(pb, -1, SEEK_CUR);
  1187. return 0;
  1188. }
  1189. }
  1190. av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
  1191. /* no sync found */
  1192. return -1;
  1193. }
  1194. /* return -1 if error or EOF. Return 0 if OK. */
  1195. static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
  1196. {
  1197. AVIOContext *pb = s->pb;
  1198. int skip, len;
  1199. for(;;) {
  1200. len = avio_read(pb, buf, TS_PACKET_SIZE);
  1201. if (len != TS_PACKET_SIZE)
  1202. return len < 0 ? len : AVERROR_EOF;
  1203. /* check paquet sync byte */
  1204. if (buf[0] != 0x47) {
  1205. /* find a new packet start */
  1206. avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1207. if (mpegts_resync(s) < 0)
  1208. return AVERROR(EAGAIN);
  1209. else
  1210. continue;
  1211. } else {
  1212. skip = raw_packet_size - TS_PACKET_SIZE;
  1213. if (skip > 0)
  1214. avio_skip(pb, skip);
  1215. break;
  1216. }
  1217. }
  1218. return 0;
  1219. }
  1220. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1221. {
  1222. AVFormatContext *s = ts->stream;
  1223. uint8_t packet[TS_PACKET_SIZE];
  1224. int packet_num, ret;
  1225. ts->stop_parse = 0;
  1226. packet_num = 0;
  1227. for(;;) {
  1228. if (ts->stop_parse>0)
  1229. break;
  1230. packet_num++;
  1231. if (nb_packets != 0 && packet_num >= nb_packets)
  1232. break;
  1233. ret = read_packet(s, packet, ts->raw_packet_size);
  1234. if (ret != 0)
  1235. return ret;
  1236. ret = handle_packet(ts, packet);
  1237. if (ret != 0)
  1238. return ret;
  1239. }
  1240. return 0;
  1241. }
  1242. static int mpegts_probe(AVProbeData *p)
  1243. {
  1244. #if 1
  1245. const int size= p->buf_size;
  1246. int score, fec_score, dvhs_score;
  1247. int check_count= size / TS_FEC_PACKET_SIZE;
  1248. #define CHECK_COUNT 10
  1249. if (check_count < CHECK_COUNT)
  1250. return -1;
  1251. score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1252. dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
  1253. fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1254. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  1255. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  1256. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1257. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1258. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1259. else return -1;
  1260. #else
  1261. /* only use the extension for safer guess */
  1262. if (av_match_ext(p->filename, "ts"))
  1263. return AVPROBE_SCORE_MAX;
  1264. else
  1265. return 0;
  1266. #endif
  1267. }
  1268. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1269. (-1) if not available */
  1270. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1271. const uint8_t *packet)
  1272. {
  1273. int afc, len, flags;
  1274. const uint8_t *p;
  1275. unsigned int v;
  1276. afc = (packet[3] >> 4) & 3;
  1277. if (afc <= 1)
  1278. return -1;
  1279. p = packet + 4;
  1280. len = p[0];
  1281. p++;
  1282. if (len == 0)
  1283. return -1;
  1284. flags = *p++;
  1285. len--;
  1286. if (!(flags & 0x10))
  1287. return -1;
  1288. if (len < 6)
  1289. return -1;
  1290. v = AV_RB32(p);
  1291. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1292. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1293. return 0;
  1294. }
  1295. static int mpegts_read_header(AVFormatContext *s,
  1296. AVFormatParameters *ap)
  1297. {
  1298. MpegTSContext *ts = s->priv_data;
  1299. AVIOContext *pb = s->pb;
  1300. uint8_t buf[5*1024];
  1301. int len;
  1302. int64_t pos;
  1303. if (ap) {
  1304. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1305. if(ap->mpeg2ts_raw){
  1306. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1307. return -1;
  1308. }
  1309. }
  1310. /* read the first 1024 bytes to get packet size */
  1311. pos = avio_tell(pb);
  1312. len = avio_read(pb, buf, sizeof(buf));
  1313. if (len != sizeof(buf))
  1314. goto fail;
  1315. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1316. if (ts->raw_packet_size <= 0)
  1317. goto fail;
  1318. ts->stream = s;
  1319. ts->auto_guess = 0;
  1320. if (s->iformat == &ff_mpegts_demuxer) {
  1321. /* normal demux */
  1322. /* first do a scaning to get all the services */
  1323. if (avio_seek(pb, pos, SEEK_SET) < 0)
  1324. av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
  1325. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1326. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1327. handle_packets(ts, s->probesize / ts->raw_packet_size);
  1328. /* if could not find service, enable auto_guess */
  1329. ts->auto_guess = 1;
  1330. av_dlog(ts->stream, "tuning done\n");
  1331. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1332. } else {
  1333. AVStream *st;
  1334. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1335. int64_t pcrs[2], pcr_h;
  1336. int packet_count[2];
  1337. uint8_t packet[TS_PACKET_SIZE];
  1338. /* only read packets */
  1339. st = av_new_stream(s, 0);
  1340. if (!st)
  1341. goto fail;
  1342. av_set_pts_info(st, 60, 1, 27000000);
  1343. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1344. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1345. /* we iterate until we find two PCRs to estimate the bitrate */
  1346. pcr_pid = -1;
  1347. nb_pcrs = 0;
  1348. nb_packets = 0;
  1349. for(;;) {
  1350. ret = read_packet(s, packet, ts->raw_packet_size);
  1351. if (ret < 0)
  1352. return -1;
  1353. pid = AV_RB16(packet + 1) & 0x1fff;
  1354. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1355. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1356. pcr_pid = pid;
  1357. packet_count[nb_pcrs] = nb_packets;
  1358. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1359. nb_pcrs++;
  1360. if (nb_pcrs >= 2)
  1361. break;
  1362. }
  1363. nb_packets++;
  1364. }
  1365. /* NOTE1: the bitrate is computed without the FEC */
  1366. /* NOTE2: it is only the bitrate of the start of the stream */
  1367. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1368. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1369. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1370. st->codec->bit_rate = s->bit_rate;
  1371. st->start_time = ts->cur_pcr;
  1372. #if 0
  1373. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1374. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1375. #endif
  1376. }
  1377. avio_seek(pb, pos, SEEK_SET);
  1378. return 0;
  1379. fail:
  1380. return -1;
  1381. }
  1382. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1383. static int mpegts_raw_read_packet(AVFormatContext *s,
  1384. AVPacket *pkt)
  1385. {
  1386. MpegTSContext *ts = s->priv_data;
  1387. int ret, i;
  1388. int64_t pcr_h, next_pcr_h, pos;
  1389. int pcr_l, next_pcr_l;
  1390. uint8_t pcr_buf[12];
  1391. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1392. return AVERROR(ENOMEM);
  1393. pkt->pos= avio_tell(s->pb);
  1394. ret = read_packet(s, pkt->data, ts->raw_packet_size);
  1395. if (ret < 0) {
  1396. av_free_packet(pkt);
  1397. return ret;
  1398. }
  1399. if (ts->mpeg2ts_compute_pcr) {
  1400. /* compute exact PCR for each packet */
  1401. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1402. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1403. pos = avio_tell(s->pb);
  1404. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1405. avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1406. avio_read(s->pb, pcr_buf, 12);
  1407. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1408. /* XXX: not precise enough */
  1409. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1410. (i + 1);
  1411. break;
  1412. }
  1413. }
  1414. avio_seek(s->pb, pos, SEEK_SET);
  1415. /* no next PCR found: we use previous increment */
  1416. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1417. }
  1418. pkt->pts = ts->cur_pcr;
  1419. pkt->duration = ts->pcr_incr;
  1420. ts->cur_pcr += ts->pcr_incr;
  1421. }
  1422. pkt->stream_index = 0;
  1423. return 0;
  1424. }
  1425. static int mpegts_read_packet(AVFormatContext *s,
  1426. AVPacket *pkt)
  1427. {
  1428. MpegTSContext *ts = s->priv_data;
  1429. int ret, i;
  1430. if (avio_tell(s->pb) != ts->last_pos) {
  1431. /* seek detected, flush pes buffer */
  1432. for (i = 0; i < NB_PID_MAX; i++) {
  1433. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1434. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1435. av_freep(&pes->buffer);
  1436. pes->data_index = 0;
  1437. pes->state = MPEGTS_SKIP; /* skip until pes header */
  1438. }
  1439. }
  1440. }
  1441. ts->pkt = pkt;
  1442. ret = handle_packets(ts, 0);
  1443. if (ret < 0) {
  1444. /* flush pes data left */
  1445. for (i = 0; i < NB_PID_MAX; i++) {
  1446. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1447. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1448. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  1449. new_pes_packet(pes, pkt);
  1450. pes->state = MPEGTS_SKIP;
  1451. ret = 0;
  1452. break;
  1453. }
  1454. }
  1455. }
  1456. }
  1457. ts->last_pos = avio_tell(s->pb);
  1458. return ret;
  1459. }
  1460. static int mpegts_read_close(AVFormatContext *s)
  1461. {
  1462. MpegTSContext *ts = s->priv_data;
  1463. int i;
  1464. clear_programs(ts);
  1465. for(i=0;i<NB_PID_MAX;i++)
  1466. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1467. return 0;
  1468. }
  1469. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1470. int64_t *ppos, int64_t pos_limit)
  1471. {
  1472. MpegTSContext *ts = s->priv_data;
  1473. int64_t pos, timestamp;
  1474. uint8_t buf[TS_PACKET_SIZE];
  1475. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1476. const int find_next= 1;
  1477. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1478. if (find_next) {
  1479. for(;;) {
  1480. avio_seek(s->pb, pos, SEEK_SET);
  1481. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1482. return AV_NOPTS_VALUE;
  1483. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1484. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1485. break;
  1486. }
  1487. pos += ts->raw_packet_size;
  1488. }
  1489. } else {
  1490. for(;;) {
  1491. pos -= ts->raw_packet_size;
  1492. if (pos < 0)
  1493. return AV_NOPTS_VALUE;
  1494. avio_seek(s->pb, pos, SEEK_SET);
  1495. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1496. return AV_NOPTS_VALUE;
  1497. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1498. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1499. break;
  1500. }
  1501. }
  1502. }
  1503. *ppos = pos;
  1504. return timestamp;
  1505. }
  1506. #ifdef USE_SYNCPOINT_SEARCH
  1507. static int read_seek2(AVFormatContext *s,
  1508. int stream_index,
  1509. int64_t min_ts,
  1510. int64_t target_ts,
  1511. int64_t max_ts,
  1512. int flags)
  1513. {
  1514. int64_t pos;
  1515. int64_t ts_ret, ts_adj;
  1516. int stream_index_gen_search;
  1517. AVStream *st;
  1518. AVParserState *backup;
  1519. backup = ff_store_parser_state(s);
  1520. // detect direction of seeking for search purposes
  1521. flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
  1522. AVSEEK_FLAG_BACKWARD : 0;
  1523. if (flags & AVSEEK_FLAG_BYTE) {
  1524. // use position directly, we will search starting from it
  1525. pos = target_ts;
  1526. } else {
  1527. // search for some position with good timestamp match
  1528. if (stream_index < 0) {
  1529. stream_index_gen_search = av_find_default_stream_index(s);
  1530. if (stream_index_gen_search < 0) {
  1531. ff_restore_parser_state(s, backup);
  1532. return -1;
  1533. }
  1534. st = s->streams[stream_index_gen_search];
  1535. // timestamp for default must be expressed in AV_TIME_BASE units
  1536. ts_adj = av_rescale(target_ts,
  1537. st->time_base.den,
  1538. AV_TIME_BASE * (int64_t)st->time_base.num);
  1539. } else {
  1540. ts_adj = target_ts;
  1541. stream_index_gen_search = stream_index;
  1542. }
  1543. pos = av_gen_search(s, stream_index_gen_search, ts_adj,
  1544. 0, INT64_MAX, -1,
  1545. AV_NOPTS_VALUE,
  1546. AV_NOPTS_VALUE,
  1547. flags, &ts_ret, mpegts_get_pcr);
  1548. if (pos < 0) {
  1549. ff_restore_parser_state(s, backup);
  1550. return -1;
  1551. }
  1552. }
  1553. // search for actual matching keyframe/starting position for all streams
  1554. if (ff_gen_syncpoint_search(s, stream_index, pos,
  1555. min_ts, target_ts, max_ts,
  1556. flags) < 0) {
  1557. ff_restore_parser_state(s, backup);
  1558. return -1;
  1559. }
  1560. ff_free_parser_state(s, backup);
  1561. return 0;
  1562. }
  1563. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
  1564. {
  1565. int ret;
  1566. if (flags & AVSEEK_FLAG_BACKWARD) {
  1567. flags &= ~AVSEEK_FLAG_BACKWARD;
  1568. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
  1569. if (ret < 0)
  1570. // for compatibility reasons, seek to the best-fitting timestamp
  1571. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
  1572. } else {
  1573. ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
  1574. if (ret < 0)
  1575. // for compatibility reasons, seek to the best-fitting timestamp
  1576. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
  1577. }
  1578. return ret;
  1579. }
  1580. #else
  1581. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1582. MpegTSContext *ts = s->priv_data;
  1583. uint8_t buf[TS_PACKET_SIZE];
  1584. int64_t pos;
  1585. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1586. return -1;
  1587. pos= avio_tell(s->pb);
  1588. for(;;) {
  1589. avio_seek(s->pb, pos, SEEK_SET);
  1590. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1591. return -1;
  1592. // pid = AV_RB16(buf + 1) & 0x1fff;
  1593. if(buf[1] & 0x40) break;
  1594. pos += ts->raw_packet_size;
  1595. }
  1596. avio_seek(s->pb, pos, SEEK_SET);
  1597. return 0;
  1598. }
  1599. #endif
  1600. /**************************************************************/
  1601. /* parsing functions - called from other demuxers such as RTP */
  1602. MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
  1603. {
  1604. MpegTSContext *ts;
  1605. ts = av_mallocz(sizeof(MpegTSContext));
  1606. if (!ts)
  1607. return NULL;
  1608. /* no stream case, currently used by RTP */
  1609. ts->raw_packet_size = TS_PACKET_SIZE;
  1610. ts->stream = s;
  1611. ts->auto_guess = 1;
  1612. return ts;
  1613. }
  1614. /* return the consumed length if a packet was output, or -1 if no
  1615. packet is output */
  1616. int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1617. const uint8_t *buf, int len)
  1618. {
  1619. int len1;
  1620. len1 = len;
  1621. ts->pkt = pkt;
  1622. ts->stop_parse = 0;
  1623. for(;;) {
  1624. if (ts->stop_parse>0)
  1625. break;
  1626. if (len < TS_PACKET_SIZE)
  1627. return -1;
  1628. if (buf[0] != 0x47) {
  1629. buf++;
  1630. len--;
  1631. } else {
  1632. handle_packet(ts, buf);
  1633. buf += TS_PACKET_SIZE;
  1634. len -= TS_PACKET_SIZE;
  1635. }
  1636. }
  1637. return len1 - len;
  1638. }
  1639. void ff_mpegts_parse_close(MpegTSContext *ts)
  1640. {
  1641. int i;
  1642. for(i=0;i<NB_PID_MAX;i++)
  1643. av_free(ts->pids[i]);
  1644. av_free(ts);
  1645. }
  1646. AVInputFormat ff_mpegts_demuxer = {
  1647. "mpegts",
  1648. NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
  1649. sizeof(MpegTSContext),
  1650. mpegts_probe,
  1651. mpegts_read_header,
  1652. mpegts_read_packet,
  1653. mpegts_read_close,
  1654. read_seek,
  1655. mpegts_get_pcr,
  1656. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1657. #ifdef USE_SYNCPOINT_SEARCH
  1658. .read_seek2 = read_seek2,
  1659. #endif
  1660. };
  1661. AVInputFormat ff_mpegtsraw_demuxer = {
  1662. "mpegtsraw",
  1663. NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
  1664. sizeof(MpegTSContext),
  1665. NULL,
  1666. mpegts_read_header,
  1667. mpegts_raw_read_packet,
  1668. mpegts_read_close,
  1669. read_seek,
  1670. mpegts_get_pcr,
  1671. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1672. #ifdef USE_SYNCPOINT_SEARCH
  1673. .read_seek2 = read_seek2,
  1674. #endif
  1675. };