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.

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