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.

1882 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. { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, CODEC_ID_S302M },
  457. { 0 },
  458. };
  459. /* descriptor present */
  460. static const StreamType DESC_types[] = {
  461. { 0x6a, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */
  462. { 0x7a, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
  463. { 0x7b, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  464. { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
  465. { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
  466. { 0 },
  467. };
  468. static void mpegts_find_stream_type(AVStream *st,
  469. uint32_t stream_type, const StreamType *types)
  470. {
  471. for (; types->stream_type; types++) {
  472. if (stream_type == types->stream_type) {
  473. st->codec->codec_type = types->codec_type;
  474. st->codec->codec_id = types->codec_id;
  475. return;
  476. }
  477. }
  478. }
  479. static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
  480. uint32_t stream_type, uint32_t prog_reg_desc)
  481. {
  482. av_set_pts_info(st, 33, 1, 90000);
  483. st->priv_data = pes;
  484. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  485. st->codec->codec_id = CODEC_ID_NONE;
  486. st->need_parsing = AVSTREAM_PARSE_FULL;
  487. pes->st = st;
  488. pes->stream_type = stream_type;
  489. av_log(pes->stream, AV_LOG_DEBUG,
  490. "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
  491. st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
  492. st->codec->codec_tag = pes->stream_type;
  493. mpegts_find_stream_type(st, pes->stream_type, ISO_types);
  494. if (prog_reg_desc == AV_RL32("HDMV") &&
  495. st->codec->codec_id == CODEC_ID_NONE) {
  496. mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
  497. if (pes->stream_type == 0x83) {
  498. // HDMV TrueHD streams also contain an AC3 coded version of the
  499. // audio track - add a second stream for this
  500. AVStream *sub_st;
  501. // priv_data cannot be shared between streams
  502. PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
  503. if (!sub_pes)
  504. return AVERROR(ENOMEM);
  505. memcpy(sub_pes, pes, sizeof(*sub_pes));
  506. sub_st = av_new_stream(pes->stream, pes->pid);
  507. if (!sub_st) {
  508. av_free(sub_pes);
  509. return AVERROR(ENOMEM);
  510. }
  511. av_set_pts_info(sub_st, 33, 1, 90000);
  512. sub_st->priv_data = sub_pes;
  513. sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  514. sub_st->codec->codec_id = CODEC_ID_AC3;
  515. sub_st->need_parsing = AVSTREAM_PARSE_FULL;
  516. sub_pes->sub_st = pes->sub_st = sub_st;
  517. }
  518. }
  519. if (st->codec->codec_id == CODEC_ID_NONE)
  520. mpegts_find_stream_type(st, pes->stream_type, MISC_types);
  521. return 0;
  522. }
  523. static void new_pes_packet(PESContext *pes, AVPacket *pkt)
  524. {
  525. av_init_packet(pkt);
  526. pkt->destruct = av_destruct_packet;
  527. pkt->data = pes->buffer;
  528. pkt->size = pes->data_index;
  529. memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  530. // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
  531. if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
  532. pkt->stream_index = pes->sub_st->index;
  533. else
  534. pkt->stream_index = pes->st->index;
  535. pkt->pts = pes->pts;
  536. pkt->dts = pes->dts;
  537. /* store position of first TS packet of this PES packet */
  538. pkt->pos = pes->ts_packet_pos;
  539. /* reset pts values */
  540. pes->pts = AV_NOPTS_VALUE;
  541. pes->dts = AV_NOPTS_VALUE;
  542. pes->buffer = NULL;
  543. pes->data_index = 0;
  544. }
  545. /* return non zero if a packet could be constructed */
  546. static int mpegts_push_data(MpegTSFilter *filter,
  547. const uint8_t *buf, int buf_size, int is_start,
  548. int64_t pos)
  549. {
  550. PESContext *pes = filter->u.pes_filter.opaque;
  551. MpegTSContext *ts = pes->ts;
  552. const uint8_t *p;
  553. int len, code;
  554. if(!ts->pkt)
  555. return 0;
  556. if (is_start) {
  557. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  558. new_pes_packet(pes, ts->pkt);
  559. ts->stop_parse = 1;
  560. }
  561. pes->state = MPEGTS_HEADER;
  562. pes->data_index = 0;
  563. pes->ts_packet_pos = pos;
  564. }
  565. p = buf;
  566. while (buf_size > 0) {
  567. switch(pes->state) {
  568. case MPEGTS_HEADER:
  569. len = PES_START_SIZE - pes->data_index;
  570. if (len > buf_size)
  571. len = buf_size;
  572. memcpy(pes->header + pes->data_index, p, len);
  573. pes->data_index += len;
  574. p += len;
  575. buf_size -= len;
  576. if (pes->data_index == PES_START_SIZE) {
  577. /* we got all the PES or section header. We can now
  578. decide */
  579. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  580. pes->header[2] == 0x01) {
  581. /* it must be an mpeg2 PES stream */
  582. code = pes->header[3] | 0x100;
  583. av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
  584. if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
  585. code == 0x1be) /* padding_stream */
  586. goto skip;
  587. /* stream not present in PMT */
  588. if (!pes->st) {
  589. pes->st = av_new_stream(ts->stream, pes->pid);
  590. if (!pes->st)
  591. return AVERROR(ENOMEM);
  592. mpegts_set_stream_info(pes->st, pes, 0, 0);
  593. }
  594. pes->total_size = AV_RB16(pes->header + 4);
  595. /* NOTE: a zero total size means the PES size is
  596. unbounded */
  597. if (!pes->total_size)
  598. pes->total_size = MAX_PES_PAYLOAD;
  599. /* allocate pes buffer */
  600. pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
  601. if (!pes->buffer)
  602. return AVERROR(ENOMEM);
  603. if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
  604. code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
  605. code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
  606. code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
  607. pes->state = MPEGTS_PESHEADER;
  608. if (pes->st->codec->codec_id == CODEC_ID_NONE) {
  609. av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
  610. pes->pid, pes->stream_type);
  611. pes->st->codec->codec_id = CODEC_ID_PROBE;
  612. }
  613. } else {
  614. pes->state = MPEGTS_PAYLOAD;
  615. pes->data_index = 0;
  616. }
  617. } else {
  618. /* otherwise, it should be a table */
  619. /* skip packet */
  620. skip:
  621. pes->state = MPEGTS_SKIP;
  622. continue;
  623. }
  624. }
  625. break;
  626. /**********************************************/
  627. /* PES packing parsing */
  628. case MPEGTS_PESHEADER:
  629. len = PES_HEADER_SIZE - pes->data_index;
  630. if (len < 0)
  631. return -1;
  632. if (len > buf_size)
  633. len = buf_size;
  634. memcpy(pes->header + pes->data_index, p, len);
  635. pes->data_index += len;
  636. p += len;
  637. buf_size -= len;
  638. if (pes->data_index == PES_HEADER_SIZE) {
  639. pes->pes_header_size = pes->header[8] + 9;
  640. pes->state = MPEGTS_PESHEADER_FILL;
  641. }
  642. break;
  643. case MPEGTS_PESHEADER_FILL:
  644. len = pes->pes_header_size - pes->data_index;
  645. if (len < 0)
  646. return -1;
  647. if (len > buf_size)
  648. len = buf_size;
  649. memcpy(pes->header + pes->data_index, p, len);
  650. pes->data_index += len;
  651. p += len;
  652. buf_size -= len;
  653. if (pes->data_index == pes->pes_header_size) {
  654. const uint8_t *r;
  655. unsigned int flags, pes_ext, skip;
  656. flags = pes->header[7];
  657. r = pes->header + 9;
  658. pes->pts = AV_NOPTS_VALUE;
  659. pes->dts = AV_NOPTS_VALUE;
  660. if ((flags & 0xc0) == 0x80) {
  661. pes->dts = pes->pts = ff_parse_pes_pts(r);
  662. r += 5;
  663. } else if ((flags & 0xc0) == 0xc0) {
  664. pes->pts = ff_parse_pes_pts(r);
  665. r += 5;
  666. pes->dts = ff_parse_pes_pts(r);
  667. r += 5;
  668. }
  669. pes->extended_stream_id = -1;
  670. if (flags & 0x01) { /* PES extension */
  671. pes_ext = *r++;
  672. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  673. skip = (pes_ext >> 4) & 0xb;
  674. skip += skip & 0x9;
  675. r += skip;
  676. if ((pes_ext & 0x41) == 0x01 &&
  677. (r + 2) <= (pes->header + pes->pes_header_size)) {
  678. /* PES extension 2 */
  679. if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
  680. pes->extended_stream_id = r[1];
  681. }
  682. }
  683. /* we got the full header. We parse it and get the payload */
  684. pes->state = MPEGTS_PAYLOAD;
  685. pes->data_index = 0;
  686. }
  687. break;
  688. case MPEGTS_PAYLOAD:
  689. if (buf_size > 0 && pes->buffer) {
  690. if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
  691. new_pes_packet(pes, ts->pkt);
  692. pes->total_size = MAX_PES_PAYLOAD;
  693. pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
  694. if (!pes->buffer)
  695. return AVERROR(ENOMEM);
  696. ts->stop_parse = 1;
  697. } else if (pes->data_index == 0 && buf_size > pes->total_size) {
  698. // pes packet size is < ts size packet and pes data is padded with 0xff
  699. // not sure if this is legal in ts but see issue #2392
  700. buf_size = pes->total_size;
  701. }
  702. memcpy(pes->buffer+pes->data_index, p, buf_size);
  703. pes->data_index += buf_size;
  704. }
  705. buf_size = 0;
  706. /* emit complete packets with known packet size
  707. * decreases demuxer delay for infrequent packets like subtitles from
  708. * a couple of seconds to milliseconds for properly muxed files.
  709. * total_size is the number of bytes following pes_packet_length
  710. * in the pes header, i.e. not counting the first 6 bytes */
  711. if (pes->total_size < MAX_PES_PAYLOAD &&
  712. pes->pes_header_size + pes->data_index == pes->total_size + 6) {
  713. ts->stop_parse = 1;
  714. new_pes_packet(pes, ts->pkt);
  715. }
  716. break;
  717. case MPEGTS_SKIP:
  718. buf_size = 0;
  719. break;
  720. }
  721. }
  722. return 0;
  723. }
  724. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
  725. {
  726. MpegTSFilter *tss;
  727. PESContext *pes;
  728. /* if no pid found, then add a pid context */
  729. pes = av_mallocz(sizeof(PESContext));
  730. if (!pes)
  731. return 0;
  732. pes->ts = ts;
  733. pes->stream = ts->stream;
  734. pes->pid = pid;
  735. pes->pcr_pid = pcr_pid;
  736. pes->state = MPEGTS_SKIP;
  737. pes->pts = AV_NOPTS_VALUE;
  738. pes->dts = AV_NOPTS_VALUE;
  739. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  740. if (!tss) {
  741. av_free(pes);
  742. return 0;
  743. }
  744. return pes;
  745. }
  746. static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
  747. int *es_id, uint8_t **dec_config_descr,
  748. int *dec_config_descr_size)
  749. {
  750. AVIOContext pb;
  751. int tag;
  752. unsigned len;
  753. ffio_init_context(&pb, buf, size, 0, NULL, NULL, NULL, NULL);
  754. len = ff_mp4_read_descr(s, &pb, &tag);
  755. if (tag == MP4IODescrTag) {
  756. avio_rb16(&pb); // ID
  757. avio_r8(&pb);
  758. avio_r8(&pb);
  759. avio_r8(&pb);
  760. avio_r8(&pb);
  761. avio_r8(&pb);
  762. len = ff_mp4_read_descr(s, &pb, &tag);
  763. if (tag == MP4ESDescrTag) {
  764. *es_id = avio_rb16(&pb); /* ES_ID */
  765. av_dlog(s, "ES_ID %#x\n", *es_id);
  766. avio_r8(&pb); /* priority */
  767. len = ff_mp4_read_descr(s, &pb, &tag);
  768. if (tag == MP4DecConfigDescrTag) {
  769. *dec_config_descr = av_malloc(len);
  770. if (!*dec_config_descr)
  771. return AVERROR(ENOMEM);
  772. *dec_config_descr_size = len;
  773. avio_read(&pb, *dec_config_descr, len);
  774. }
  775. }
  776. }
  777. return 0;
  778. }
  779. int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
  780. const uint8_t **pp, const uint8_t *desc_list_end,
  781. int mp4_dec_config_descr_len, int mp4_es_id, int pid,
  782. uint8_t *mp4_dec_config_descr)
  783. {
  784. const uint8_t *desc_end;
  785. int desc_len, desc_tag;
  786. char language[252];
  787. int i;
  788. desc_tag = get8(pp, desc_list_end);
  789. if (desc_tag < 0)
  790. return -1;
  791. desc_len = get8(pp, desc_list_end);
  792. if (desc_len < 0)
  793. return -1;
  794. desc_end = *pp + desc_len;
  795. if (desc_end > desc_list_end)
  796. return -1;
  797. av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
  798. if (st->codec->codec_id == CODEC_ID_NONE &&
  799. stream_type == STREAM_TYPE_PRIVATE_DATA)
  800. mpegts_find_stream_type(st, desc_tag, DESC_types);
  801. switch(desc_tag) {
  802. case 0x1F: /* FMC descriptor */
  803. get16(pp, desc_end);
  804. if (st->codec->codec_id == CODEC_ID_AAC_LATM &&
  805. mp4_dec_config_descr_len && mp4_es_id == pid) {
  806. AVIOContext pb;
  807. ffio_init_context(&pb, mp4_dec_config_descr,
  808. mp4_dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  809. ff_mp4_read_dec_config_descr(fc, st, &pb);
  810. if (st->codec->codec_id == CODEC_ID_AAC &&
  811. st->codec->extradata_size > 0)
  812. st->need_parsing = 0;
  813. }
  814. break;
  815. case 0x56: /* DVB teletext descriptor */
  816. language[0] = get8(pp, desc_end);
  817. language[1] = get8(pp, desc_end);
  818. language[2] = get8(pp, desc_end);
  819. language[3] = 0;
  820. av_metadata_set2(&st->metadata, "language", language, 0);
  821. break;
  822. case 0x59: /* subtitling descriptor */
  823. language[0] = get8(pp, desc_end);
  824. language[1] = get8(pp, desc_end);
  825. language[2] = get8(pp, desc_end);
  826. language[3] = 0;
  827. /* hearing impaired subtitles detection */
  828. switch(get8(pp, desc_end)) {
  829. case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
  830. case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
  831. case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
  832. case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
  833. case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
  834. case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
  835. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  836. break;
  837. }
  838. if (st->codec->extradata) {
  839. if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
  840. av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
  841. } else {
  842. st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
  843. if (st->codec->extradata) {
  844. st->codec->extradata_size = 4;
  845. memcpy(st->codec->extradata, *pp, 4);
  846. }
  847. }
  848. *pp += 4;
  849. av_metadata_set2(&st->metadata, "language", language, 0);
  850. break;
  851. case 0x0a: /* ISO 639 language descriptor */
  852. for (i = 0; i + 4 <= desc_len; i += 4) {
  853. language[i + 0] = get8(pp, desc_end);
  854. language[i + 1] = get8(pp, desc_end);
  855. language[i + 2] = get8(pp, desc_end);
  856. language[i + 3] = ',';
  857. switch (get8(pp, desc_end)) {
  858. case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
  859. case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
  860. case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
  861. }
  862. }
  863. if (i) {
  864. language[i - 1] = 0;
  865. av_metadata_set2(&st->metadata, "language", language, 0);
  866. }
  867. break;
  868. case 0x05: /* registration descriptor */
  869. st->codec->codec_tag = bytestream_get_le32(pp);
  870. av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
  871. if (st->codec->codec_id == CODEC_ID_NONE &&
  872. stream_type == STREAM_TYPE_PRIVATE_DATA)
  873. mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
  874. break;
  875. default:
  876. break;
  877. }
  878. *pp = desc_end;
  879. return 0;
  880. }
  881. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  882. {
  883. MpegTSContext *ts = filter->u.section_filter.opaque;
  884. SectionHeader h1, *h = &h1;
  885. PESContext *pes;
  886. AVStream *st;
  887. const uint8_t *p, *p_end, *desc_list_end;
  888. int program_info_length, pcr_pid, pid, stream_type;
  889. int desc_list_len;
  890. uint32_t prog_reg_desc = 0; /* registration descriptor */
  891. uint8_t *mp4_dec_config_descr = NULL;
  892. int mp4_dec_config_descr_len = 0;
  893. int mp4_es_id = 0;
  894. #ifdef DEBUG
  895. av_dlog(ts->stream, "PMT: len %i\n", section_len);
  896. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  897. #endif
  898. p_end = section + section_len - 4;
  899. p = section;
  900. if (parse_section_header(h, &p, p_end) < 0)
  901. return;
  902. av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
  903. h->id, h->sec_num, h->last_sec_num);
  904. if (h->tid != PMT_TID)
  905. return;
  906. clear_program(ts, h->id);
  907. pcr_pid = get16(&p, p_end) & 0x1fff;
  908. if (pcr_pid < 0)
  909. return;
  910. add_pid_to_pmt(ts, h->id, pcr_pid);
  911. av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
  912. program_info_length = get16(&p, p_end) & 0xfff;
  913. if (program_info_length < 0)
  914. return;
  915. while(program_info_length >= 2) {
  916. uint8_t tag, len;
  917. tag = get8(&p, p_end);
  918. len = get8(&p, p_end);
  919. av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
  920. if(len > program_info_length - 2)
  921. //something else is broken, exit the program_descriptors_loop
  922. break;
  923. program_info_length -= len + 2;
  924. if (tag == 0x1d) { // IOD descriptor
  925. get8(&p, p_end); // scope
  926. get8(&p, p_end); // label
  927. len -= 2;
  928. mp4_read_iods(ts->stream, p, len, &mp4_es_id,
  929. &mp4_dec_config_descr, &mp4_dec_config_descr_len);
  930. } else if (tag == 0x05 && len >= 4) { // registration descriptor
  931. prog_reg_desc = bytestream_get_le32(&p);
  932. len -= 4;
  933. }
  934. p += len;
  935. }
  936. p += program_info_length;
  937. if (p >= p_end)
  938. goto out;
  939. // stop parsing after pmt, we found header
  940. if (!ts->stream->nb_streams)
  941. ts->stop_parse = 1;
  942. for(;;) {
  943. st = 0;
  944. stream_type = get8(&p, p_end);
  945. if (stream_type < 0)
  946. break;
  947. pid = get16(&p, p_end) & 0x1fff;
  948. if (pid < 0)
  949. break;
  950. /* now create ffmpeg stream */
  951. if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
  952. pes = ts->pids[pid]->u.pes_filter.opaque;
  953. if (!pes->st)
  954. pes->st = av_new_stream(pes->stream, pes->pid);
  955. st = pes->st;
  956. } else {
  957. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  958. pes = add_pes_stream(ts, pid, pcr_pid);
  959. if (pes)
  960. st = av_new_stream(pes->stream, pes->pid);
  961. }
  962. if (!st)
  963. goto out;
  964. if (!pes->stream_type)
  965. mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
  966. add_pid_to_pmt(ts, h->id, pid);
  967. ff_program_add_stream_index(ts->stream, h->id, st->index);
  968. desc_list_len = get16(&p, p_end) & 0xfff;
  969. if (desc_list_len < 0)
  970. break;
  971. desc_list_end = p + desc_list_len;
  972. if (desc_list_end > p_end)
  973. break;
  974. for(;;) {
  975. if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
  976. mp4_dec_config_descr_len, mp4_es_id, pid, mp4_dec_config_descr) < 0)
  977. break;
  978. if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
  979. ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
  980. pes->sub_st->codec->codec_tag = st->codec->codec_tag;
  981. }
  982. }
  983. p = desc_list_end;
  984. }
  985. out:
  986. av_free(mp4_dec_config_descr);
  987. }
  988. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  989. {
  990. MpegTSContext *ts = filter->u.section_filter.opaque;
  991. SectionHeader h1, *h = &h1;
  992. const uint8_t *p, *p_end;
  993. int sid, pmt_pid;
  994. #ifdef DEBUG
  995. av_dlog(ts->stream, "PAT:\n");
  996. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  997. #endif
  998. p_end = section + section_len - 4;
  999. p = section;
  1000. if (parse_section_header(h, &p, p_end) < 0)
  1001. return;
  1002. if (h->tid != PAT_TID)
  1003. return;
  1004. clear_programs(ts);
  1005. for(;;) {
  1006. sid = get16(&p, p_end);
  1007. if (sid < 0)
  1008. break;
  1009. pmt_pid = get16(&p, p_end) & 0x1fff;
  1010. if (pmt_pid < 0)
  1011. break;
  1012. av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  1013. if (sid == 0x0000) {
  1014. /* NIT info */
  1015. } else {
  1016. av_new_program(ts->stream, sid);
  1017. if (ts->pids[pmt_pid])
  1018. mpegts_close_filter(ts, ts->pids[pmt_pid]);
  1019. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  1020. add_pat_entry(ts, sid);
  1021. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  1022. add_pid_to_pmt(ts, sid, pmt_pid);
  1023. }
  1024. }
  1025. }
  1026. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1027. {
  1028. MpegTSContext *ts = filter->u.section_filter.opaque;
  1029. SectionHeader h1, *h = &h1;
  1030. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  1031. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  1032. char *name, *provider_name;
  1033. #ifdef DEBUG
  1034. av_dlog(ts->stream, "SDT:\n");
  1035. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  1036. #endif
  1037. p_end = section + section_len - 4;
  1038. p = section;
  1039. if (parse_section_header(h, &p, p_end) < 0)
  1040. return;
  1041. if (h->tid != SDT_TID)
  1042. return;
  1043. onid = get16(&p, p_end);
  1044. if (onid < 0)
  1045. return;
  1046. val = get8(&p, p_end);
  1047. if (val < 0)
  1048. return;
  1049. for(;;) {
  1050. sid = get16(&p, p_end);
  1051. if (sid < 0)
  1052. break;
  1053. val = get8(&p, p_end);
  1054. if (val < 0)
  1055. break;
  1056. desc_list_len = get16(&p, p_end) & 0xfff;
  1057. if (desc_list_len < 0)
  1058. break;
  1059. desc_list_end = p + desc_list_len;
  1060. if (desc_list_end > p_end)
  1061. break;
  1062. for(;;) {
  1063. desc_tag = get8(&p, desc_list_end);
  1064. if (desc_tag < 0)
  1065. break;
  1066. desc_len = get8(&p, desc_list_end);
  1067. desc_end = p + desc_len;
  1068. if (desc_end > desc_list_end)
  1069. break;
  1070. av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
  1071. desc_tag, desc_len);
  1072. switch(desc_tag) {
  1073. case 0x48:
  1074. service_type = get8(&p, p_end);
  1075. if (service_type < 0)
  1076. break;
  1077. provider_name = getstr8(&p, p_end);
  1078. if (!provider_name)
  1079. break;
  1080. name = getstr8(&p, p_end);
  1081. if (name) {
  1082. AVProgram *program = av_new_program(ts->stream, sid);
  1083. if(program) {
  1084. av_metadata_set2(&program->metadata, "service_name", name, 0);
  1085. av_metadata_set2(&program->metadata, "service_provider", provider_name, 0);
  1086. }
  1087. }
  1088. av_free(name);
  1089. av_free(provider_name);
  1090. break;
  1091. default:
  1092. break;
  1093. }
  1094. p = desc_end;
  1095. }
  1096. p = desc_list_end;
  1097. }
  1098. }
  1099. /* handle one TS packet */
  1100. static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  1101. {
  1102. AVFormatContext *s = ts->stream;
  1103. MpegTSFilter *tss;
  1104. int len, pid, cc, cc_ok, afc, is_start;
  1105. const uint8_t *p, *p_end;
  1106. int64_t pos;
  1107. pid = AV_RB16(packet + 1) & 0x1fff;
  1108. if(pid && discard_pid(ts, pid))
  1109. return 0;
  1110. is_start = packet[1] & 0x40;
  1111. tss = ts->pids[pid];
  1112. if (ts->auto_guess && tss == NULL && is_start) {
  1113. add_pes_stream(ts, pid, -1);
  1114. tss = ts->pids[pid];
  1115. }
  1116. if (!tss)
  1117. return 0;
  1118. /* continuity check (currently not used) */
  1119. cc = (packet[3] & 0xf);
  1120. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  1121. tss->last_cc = cc;
  1122. /* skip adaptation field */
  1123. afc = (packet[3] >> 4) & 3;
  1124. p = packet + 4;
  1125. if (afc == 0) /* reserved value */
  1126. return 0;
  1127. if (afc == 2) /* adaptation field only */
  1128. return 0;
  1129. if (afc == 3) {
  1130. /* skip adapation field */
  1131. p += p[0] + 1;
  1132. }
  1133. /* if past the end of packet, ignore */
  1134. p_end = packet + TS_PACKET_SIZE;
  1135. if (p >= p_end)
  1136. return 0;
  1137. pos = avio_tell(ts->stream->pb);
  1138. ts->pos47= pos % ts->raw_packet_size;
  1139. if (tss->type == MPEGTS_SECTION) {
  1140. if (is_start) {
  1141. /* pointer field present */
  1142. len = *p++;
  1143. if (p + len > p_end)
  1144. return 0;
  1145. if (len && cc_ok) {
  1146. /* write remaining section bytes */
  1147. write_section_data(s, tss,
  1148. p, len, 0);
  1149. /* check whether filter has been closed */
  1150. if (!ts->pids[pid])
  1151. return 0;
  1152. }
  1153. p += len;
  1154. if (p < p_end) {
  1155. write_section_data(s, tss,
  1156. p, p_end - p, 1);
  1157. }
  1158. } else {
  1159. if (cc_ok) {
  1160. write_section_data(s, tss,
  1161. p, p_end - p, 0);
  1162. }
  1163. }
  1164. } else {
  1165. int ret;
  1166. // Note: The position here points actually behind the current packet.
  1167. if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
  1168. pos - ts->raw_packet_size)) < 0)
  1169. return ret;
  1170. }
  1171. return 0;
  1172. }
  1173. /* XXX: try to find a better synchro over several packets (use
  1174. get_packet_size() ?) */
  1175. static int mpegts_resync(AVFormatContext *s)
  1176. {
  1177. AVIOContext *pb = s->pb;
  1178. int c, i;
  1179. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1180. c = avio_r8(pb);
  1181. if (pb->eof_reached)
  1182. return -1;
  1183. if (c == 0x47) {
  1184. avio_seek(pb, -1, SEEK_CUR);
  1185. return 0;
  1186. }
  1187. }
  1188. av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
  1189. /* no sync found */
  1190. return -1;
  1191. }
  1192. /* return -1 if error or EOF. Return 0 if OK. */
  1193. static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
  1194. {
  1195. AVIOContext *pb = s->pb;
  1196. int skip, len;
  1197. for(;;) {
  1198. len = avio_read(pb, buf, TS_PACKET_SIZE);
  1199. if (len != TS_PACKET_SIZE)
  1200. return len < 0 ? len : AVERROR_EOF;
  1201. /* check paquet sync byte */
  1202. if (buf[0] != 0x47) {
  1203. /* find a new packet start */
  1204. avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1205. if (mpegts_resync(s) < 0)
  1206. return AVERROR(EAGAIN);
  1207. else
  1208. continue;
  1209. } else {
  1210. skip = raw_packet_size - TS_PACKET_SIZE;
  1211. if (skip > 0)
  1212. avio_skip(pb, skip);
  1213. break;
  1214. }
  1215. }
  1216. return 0;
  1217. }
  1218. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1219. {
  1220. AVFormatContext *s = ts->stream;
  1221. uint8_t packet[TS_PACKET_SIZE];
  1222. int packet_num, ret;
  1223. ts->stop_parse = 0;
  1224. packet_num = 0;
  1225. for(;;) {
  1226. if (ts->stop_parse>0)
  1227. break;
  1228. packet_num++;
  1229. if (nb_packets != 0 && packet_num >= nb_packets)
  1230. break;
  1231. ret = read_packet(s, packet, ts->raw_packet_size);
  1232. if (ret != 0)
  1233. return ret;
  1234. ret = handle_packet(ts, packet);
  1235. if (ret != 0)
  1236. return ret;
  1237. }
  1238. return 0;
  1239. }
  1240. static int mpegts_probe(AVProbeData *p)
  1241. {
  1242. #if 1
  1243. const int size= p->buf_size;
  1244. int score, fec_score, dvhs_score;
  1245. int check_count= size / TS_FEC_PACKET_SIZE;
  1246. #define CHECK_COUNT 10
  1247. if (check_count < CHECK_COUNT)
  1248. return -1;
  1249. score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1250. dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
  1251. fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1252. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  1253. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  1254. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1255. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1256. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1257. else return -1;
  1258. #else
  1259. /* only use the extension for safer guess */
  1260. if (av_match_ext(p->filename, "ts"))
  1261. return AVPROBE_SCORE_MAX;
  1262. else
  1263. return 0;
  1264. #endif
  1265. }
  1266. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1267. (-1) if not available */
  1268. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1269. const uint8_t *packet)
  1270. {
  1271. int afc, len, flags;
  1272. const uint8_t *p;
  1273. unsigned int v;
  1274. afc = (packet[3] >> 4) & 3;
  1275. if (afc <= 1)
  1276. return -1;
  1277. p = packet + 4;
  1278. len = p[0];
  1279. p++;
  1280. if (len == 0)
  1281. return -1;
  1282. flags = *p++;
  1283. len--;
  1284. if (!(flags & 0x10))
  1285. return -1;
  1286. if (len < 6)
  1287. return -1;
  1288. v = AV_RB32(p);
  1289. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1290. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1291. return 0;
  1292. }
  1293. static int mpegts_read_header(AVFormatContext *s,
  1294. AVFormatParameters *ap)
  1295. {
  1296. MpegTSContext *ts = s->priv_data;
  1297. AVIOContext *pb = s->pb;
  1298. uint8_t buf[5*1024];
  1299. int len;
  1300. int64_t pos;
  1301. if (ap) {
  1302. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1303. if(ap->mpeg2ts_raw){
  1304. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1305. return -1;
  1306. }
  1307. }
  1308. /* read the first 1024 bytes to get packet size */
  1309. pos = avio_tell(pb);
  1310. len = avio_read(pb, buf, sizeof(buf));
  1311. if (len != sizeof(buf))
  1312. goto fail;
  1313. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1314. if (ts->raw_packet_size <= 0)
  1315. goto fail;
  1316. ts->stream = s;
  1317. ts->auto_guess = 0;
  1318. if (s->iformat == &ff_mpegts_demuxer) {
  1319. /* normal demux */
  1320. /* first do a scaning to get all the services */
  1321. if (avio_seek(pb, pos, SEEK_SET) < 0)
  1322. av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
  1323. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1324. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1325. handle_packets(ts, s->probesize / ts->raw_packet_size);
  1326. /* if could not find service, enable auto_guess */
  1327. ts->auto_guess = 1;
  1328. av_dlog(ts->stream, "tuning done\n");
  1329. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1330. } else {
  1331. AVStream *st;
  1332. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1333. int64_t pcrs[2], pcr_h;
  1334. int packet_count[2];
  1335. uint8_t packet[TS_PACKET_SIZE];
  1336. /* only read packets */
  1337. st = av_new_stream(s, 0);
  1338. if (!st)
  1339. goto fail;
  1340. av_set_pts_info(st, 60, 1, 27000000);
  1341. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1342. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1343. /* we iterate until we find two PCRs to estimate the bitrate */
  1344. pcr_pid = -1;
  1345. nb_pcrs = 0;
  1346. nb_packets = 0;
  1347. for(;;) {
  1348. ret = read_packet(s, packet, ts->raw_packet_size);
  1349. if (ret < 0)
  1350. return -1;
  1351. pid = AV_RB16(packet + 1) & 0x1fff;
  1352. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1353. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1354. pcr_pid = pid;
  1355. packet_count[nb_pcrs] = nb_packets;
  1356. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1357. nb_pcrs++;
  1358. if (nb_pcrs >= 2)
  1359. break;
  1360. }
  1361. nb_packets++;
  1362. }
  1363. /* NOTE1: the bitrate is computed without the FEC */
  1364. /* NOTE2: it is only the bitrate of the start of the stream */
  1365. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1366. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1367. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1368. st->codec->bit_rate = s->bit_rate;
  1369. st->start_time = ts->cur_pcr;
  1370. av_dlog(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1371. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1372. }
  1373. avio_seek(pb, pos, SEEK_SET);
  1374. return 0;
  1375. fail:
  1376. return -1;
  1377. }
  1378. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1379. static int mpegts_raw_read_packet(AVFormatContext *s,
  1380. AVPacket *pkt)
  1381. {
  1382. MpegTSContext *ts = s->priv_data;
  1383. int ret, i;
  1384. int64_t pcr_h, next_pcr_h, pos;
  1385. int pcr_l, next_pcr_l;
  1386. uint8_t pcr_buf[12];
  1387. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1388. return AVERROR(ENOMEM);
  1389. pkt->pos= avio_tell(s->pb);
  1390. ret = read_packet(s, pkt->data, ts->raw_packet_size);
  1391. if (ret < 0) {
  1392. av_free_packet(pkt);
  1393. return ret;
  1394. }
  1395. if (ts->mpeg2ts_compute_pcr) {
  1396. /* compute exact PCR for each packet */
  1397. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1398. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1399. pos = avio_tell(s->pb);
  1400. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1401. avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1402. avio_read(s->pb, pcr_buf, 12);
  1403. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1404. /* XXX: not precise enough */
  1405. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1406. (i + 1);
  1407. break;
  1408. }
  1409. }
  1410. avio_seek(s->pb, pos, SEEK_SET);
  1411. /* no next PCR found: we use previous increment */
  1412. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1413. }
  1414. pkt->pts = ts->cur_pcr;
  1415. pkt->duration = ts->pcr_incr;
  1416. ts->cur_pcr += ts->pcr_incr;
  1417. }
  1418. pkt->stream_index = 0;
  1419. return 0;
  1420. }
  1421. static int mpegts_read_packet(AVFormatContext *s,
  1422. AVPacket *pkt)
  1423. {
  1424. MpegTSContext *ts = s->priv_data;
  1425. int ret, i;
  1426. if (avio_tell(s->pb) != ts->last_pos) {
  1427. /* seek detected, flush pes buffer */
  1428. for (i = 0; i < NB_PID_MAX; i++) {
  1429. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1430. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1431. av_freep(&pes->buffer);
  1432. pes->data_index = 0;
  1433. pes->state = MPEGTS_SKIP; /* skip until pes header */
  1434. }
  1435. }
  1436. }
  1437. ts->pkt = pkt;
  1438. ret = handle_packets(ts, 0);
  1439. if (ret < 0) {
  1440. /* flush pes data left */
  1441. for (i = 0; i < NB_PID_MAX; i++) {
  1442. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1443. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1444. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  1445. new_pes_packet(pes, pkt);
  1446. pes->state = MPEGTS_SKIP;
  1447. ret = 0;
  1448. break;
  1449. }
  1450. }
  1451. }
  1452. }
  1453. ts->last_pos = avio_tell(s->pb);
  1454. return ret;
  1455. }
  1456. static int mpegts_read_close(AVFormatContext *s)
  1457. {
  1458. MpegTSContext *ts = s->priv_data;
  1459. int i;
  1460. clear_programs(ts);
  1461. for(i=0;i<NB_PID_MAX;i++)
  1462. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1463. return 0;
  1464. }
  1465. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1466. int64_t *ppos, int64_t pos_limit)
  1467. {
  1468. MpegTSContext *ts = s->priv_data;
  1469. int64_t pos, timestamp;
  1470. uint8_t buf[TS_PACKET_SIZE];
  1471. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1472. const int find_next= 1;
  1473. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1474. if (find_next) {
  1475. for(;;) {
  1476. avio_seek(s->pb, pos, SEEK_SET);
  1477. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1478. return AV_NOPTS_VALUE;
  1479. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1480. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1481. break;
  1482. }
  1483. pos += ts->raw_packet_size;
  1484. }
  1485. } else {
  1486. for(;;) {
  1487. pos -= ts->raw_packet_size;
  1488. if (pos < 0)
  1489. return AV_NOPTS_VALUE;
  1490. avio_seek(s->pb, pos, SEEK_SET);
  1491. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1492. return AV_NOPTS_VALUE;
  1493. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1494. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1495. break;
  1496. }
  1497. }
  1498. }
  1499. *ppos = pos;
  1500. return timestamp;
  1501. }
  1502. #ifdef USE_SYNCPOINT_SEARCH
  1503. static int read_seek2(AVFormatContext *s,
  1504. int stream_index,
  1505. int64_t min_ts,
  1506. int64_t target_ts,
  1507. int64_t max_ts,
  1508. int flags)
  1509. {
  1510. int64_t pos;
  1511. int64_t ts_ret, ts_adj;
  1512. int stream_index_gen_search;
  1513. AVStream *st;
  1514. AVParserState *backup;
  1515. backup = ff_store_parser_state(s);
  1516. // detect direction of seeking for search purposes
  1517. flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
  1518. AVSEEK_FLAG_BACKWARD : 0;
  1519. if (flags & AVSEEK_FLAG_BYTE) {
  1520. // use position directly, we will search starting from it
  1521. pos = target_ts;
  1522. } else {
  1523. // search for some position with good timestamp match
  1524. if (stream_index < 0) {
  1525. stream_index_gen_search = av_find_default_stream_index(s);
  1526. if (stream_index_gen_search < 0) {
  1527. ff_restore_parser_state(s, backup);
  1528. return -1;
  1529. }
  1530. st = s->streams[stream_index_gen_search];
  1531. // timestamp for default must be expressed in AV_TIME_BASE units
  1532. ts_adj = av_rescale(target_ts,
  1533. st->time_base.den,
  1534. AV_TIME_BASE * (int64_t)st->time_base.num);
  1535. } else {
  1536. ts_adj = target_ts;
  1537. stream_index_gen_search = stream_index;
  1538. }
  1539. pos = av_gen_search(s, stream_index_gen_search, ts_adj,
  1540. 0, INT64_MAX, -1,
  1541. AV_NOPTS_VALUE,
  1542. AV_NOPTS_VALUE,
  1543. flags, &ts_ret, mpegts_get_pcr);
  1544. if (pos < 0) {
  1545. ff_restore_parser_state(s, backup);
  1546. return -1;
  1547. }
  1548. }
  1549. // search for actual matching keyframe/starting position for all streams
  1550. if (ff_gen_syncpoint_search(s, stream_index, pos,
  1551. min_ts, target_ts, max_ts,
  1552. flags) < 0) {
  1553. ff_restore_parser_state(s, backup);
  1554. return -1;
  1555. }
  1556. ff_free_parser_state(s, backup);
  1557. return 0;
  1558. }
  1559. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
  1560. {
  1561. int ret;
  1562. if (flags & AVSEEK_FLAG_BACKWARD) {
  1563. flags &= ~AVSEEK_FLAG_BACKWARD;
  1564. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
  1565. if (ret < 0)
  1566. // for compatibility reasons, seek to the best-fitting timestamp
  1567. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
  1568. } else {
  1569. ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
  1570. if (ret < 0)
  1571. // for compatibility reasons, seek to the best-fitting timestamp
  1572. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
  1573. }
  1574. return ret;
  1575. }
  1576. #else
  1577. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1578. MpegTSContext *ts = s->priv_data;
  1579. uint8_t buf[TS_PACKET_SIZE];
  1580. int64_t pos;
  1581. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1582. return -1;
  1583. pos= avio_tell(s->pb);
  1584. for(;;) {
  1585. avio_seek(s->pb, pos, SEEK_SET);
  1586. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1587. return -1;
  1588. // pid = AV_RB16(buf + 1) & 0x1fff;
  1589. if(buf[1] & 0x40) break;
  1590. pos += ts->raw_packet_size;
  1591. }
  1592. avio_seek(s->pb, pos, SEEK_SET);
  1593. return 0;
  1594. }
  1595. #endif
  1596. /**************************************************************/
  1597. /* parsing functions - called from other demuxers such as RTP */
  1598. MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
  1599. {
  1600. MpegTSContext *ts;
  1601. ts = av_mallocz(sizeof(MpegTSContext));
  1602. if (!ts)
  1603. return NULL;
  1604. /* no stream case, currently used by RTP */
  1605. ts->raw_packet_size = TS_PACKET_SIZE;
  1606. ts->stream = s;
  1607. ts->auto_guess = 1;
  1608. return ts;
  1609. }
  1610. /* return the consumed length if a packet was output, or -1 if no
  1611. packet is output */
  1612. int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1613. const uint8_t *buf, int len)
  1614. {
  1615. int len1;
  1616. len1 = len;
  1617. ts->pkt = pkt;
  1618. ts->stop_parse = 0;
  1619. for(;;) {
  1620. if (ts->stop_parse>0)
  1621. break;
  1622. if (len < TS_PACKET_SIZE)
  1623. return -1;
  1624. if (buf[0] != 0x47) {
  1625. buf++;
  1626. len--;
  1627. } else {
  1628. handle_packet(ts, buf);
  1629. buf += TS_PACKET_SIZE;
  1630. len -= TS_PACKET_SIZE;
  1631. }
  1632. }
  1633. return len1 - len;
  1634. }
  1635. void ff_mpegts_parse_close(MpegTSContext *ts)
  1636. {
  1637. int i;
  1638. for(i=0;i<NB_PID_MAX;i++)
  1639. av_free(ts->pids[i]);
  1640. av_free(ts);
  1641. }
  1642. AVInputFormat ff_mpegts_demuxer = {
  1643. "mpegts",
  1644. NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
  1645. sizeof(MpegTSContext),
  1646. mpegts_probe,
  1647. mpegts_read_header,
  1648. mpegts_read_packet,
  1649. mpegts_read_close,
  1650. read_seek,
  1651. mpegts_get_pcr,
  1652. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1653. #ifdef USE_SYNCPOINT_SEARCH
  1654. .read_seek2 = read_seek2,
  1655. #endif
  1656. };
  1657. AVInputFormat ff_mpegtsraw_demuxer = {
  1658. "mpegtsraw",
  1659. NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
  1660. sizeof(MpegTSContext),
  1661. NULL,
  1662. mpegts_read_header,
  1663. mpegts_raw_read_packet,
  1664. mpegts_read_close,
  1665. read_seek,
  1666. mpegts_get_pcr,
  1667. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1668. #ifdef USE_SYNCPOINT_SEARCH
  1669. .read_seek2 = read_seek2,
  1670. #endif
  1671. };