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.

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