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.

1887 lines
58KB

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