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.

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