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.

1898 lines
58KB

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