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.

1907 lines
59KB

  1. /*
  2. * MPEG2 transport stream (aka DVB) demuxer
  3. * Copyright (c) 2002-2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. //#define DEBUG
  22. //#define DEBUG_SEEK
  23. //#define USE_SYNCPOINT_SEARCH
  24. #include "libavutil/crc.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/opt.h"
  28. #include "libavcodec/bytestream.h"
  29. #include "avformat.h"
  30. #include "mpegts.h"
  31. #include "internal.h"
  32. #include "avio_internal.h"
  33. #include "seek.h"
  34. #include "mpeg.h"
  35. #include "isom.h"
  36. /* maximum size in which we look for synchronisation if
  37. synchronisation is lost */
  38. #define MAX_RESYNC_SIZE 65536
  39. #define MAX_PES_PAYLOAD 200*1024
  40. enum MpegTSFilterType {
  41. MPEGTS_PES,
  42. MPEGTS_SECTION,
  43. };
  44. typedef struct MpegTSFilter MpegTSFilter;
  45. typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
  46. typedef struct MpegTSPESFilter {
  47. PESCallback *pes_cb;
  48. void *opaque;
  49. } MpegTSPESFilter;
  50. typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
  51. typedef void SetServiceCallback(void *opaque, int ret);
  52. typedef struct MpegTSSectionFilter {
  53. int section_index;
  54. int section_h_size;
  55. uint8_t *section_buf;
  56. unsigned int check_crc:1;
  57. unsigned int end_of_section_reached:1;
  58. SectionCallback *section_cb;
  59. void *opaque;
  60. } MpegTSSectionFilter;
  61. struct MpegTSFilter {
  62. int pid;
  63. int last_cc; /* last cc code (-1 if first packet) */
  64. enum MpegTSFilterType type;
  65. union {
  66. MpegTSPESFilter pes_filter;
  67. MpegTSSectionFilter section_filter;
  68. } u;
  69. };
  70. #define MAX_PIDS_PER_PROGRAM 64
  71. struct Program {
  72. unsigned int id; //program id/service id
  73. unsigned int nb_pids;
  74. unsigned int pids[MAX_PIDS_PER_PROGRAM];
  75. };
  76. struct MpegTSContext {
  77. const AVClass *class;
  78. /* user data */
  79. AVFormatContext *stream;
  80. /** raw packet size, including FEC if present */
  81. int raw_packet_size;
  82. int pos47;
  83. /** if true, all pids are analyzed to find streams */
  84. int auto_guess;
  85. /** compute exact PCR for each transport stream packet */
  86. int mpeg2ts_compute_pcr;
  87. int64_t cur_pcr; /**< used to estimate the exact PCR */
  88. int pcr_incr; /**< used to estimate the exact PCR */
  89. /* data needed to handle file based ts */
  90. /** stop parsing loop */
  91. int stop_parse;
  92. /** packet containing Audio/Video data */
  93. AVPacket *pkt;
  94. /** to detect seek */
  95. int64_t last_pos;
  96. /******************************************/
  97. /* private mpegts data */
  98. /* scan context */
  99. /** structure to keep track of Program->pids mapping */
  100. unsigned int nb_prg;
  101. struct Program *prg;
  102. /** filters for various streams specified by PMT + for the PAT and PMT */
  103. MpegTSFilter *pids[NB_PID_MAX];
  104. };
  105. static const AVOption options[] = {
  106. {"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), FF_OPT_TYPE_INT,
  107. {.dbl = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  108. { NULL },
  109. };
  110. static const AVClass mpegtsraw_class = {
  111. .class_name = "mpegtsraw demuxer",
  112. .item_name = av_default_item_name,
  113. .option = options,
  114. .version = LIBAVUTIL_VERSION_INT,
  115. };
  116. /* TS stream handling */
  117. enum MpegTSState {
  118. MPEGTS_HEADER = 0,
  119. MPEGTS_PESHEADER,
  120. MPEGTS_PESHEADER_FILL,
  121. MPEGTS_PAYLOAD,
  122. MPEGTS_SKIP,
  123. };
  124. /* enough for PES header + length */
  125. #define PES_START_SIZE 6
  126. #define PES_HEADER_SIZE 9
  127. #define MAX_PES_HEADER_SIZE (9 + 255)
  128. typedef struct PESContext {
  129. int pid;
  130. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  131. int stream_type;
  132. MpegTSContext *ts;
  133. AVFormatContext *stream;
  134. AVStream *st;
  135. AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
  136. enum MpegTSState state;
  137. /* used to get the format */
  138. int data_index;
  139. int total_size;
  140. int pes_header_size;
  141. int extended_stream_id;
  142. int64_t pts, dts;
  143. int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
  144. uint8_t header[MAX_PES_HEADER_SIZE];
  145. uint8_t *buffer;
  146. } PESContext;
  147. extern AVInputFormat ff_mpegts_demuxer;
  148. static void clear_program(MpegTSContext *ts, unsigned int programid)
  149. {
  150. int i;
  151. for(i=0; i<ts->nb_prg; i++)
  152. if(ts->prg[i].id == programid)
  153. ts->prg[i].nb_pids = 0;
  154. }
  155. static void clear_programs(MpegTSContext *ts)
  156. {
  157. av_freep(&ts->prg);
  158. ts->nb_prg=0;
  159. }
  160. static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
  161. {
  162. struct Program *p;
  163. void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
  164. if(!tmp)
  165. return;
  166. ts->prg = tmp;
  167. p = &ts->prg[ts->nb_prg];
  168. p->id = programid;
  169. p->nb_pids = 0;
  170. ts->nb_prg++;
  171. }
  172. static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
  173. {
  174. int i;
  175. struct Program *p = NULL;
  176. for(i=0; i<ts->nb_prg; i++) {
  177. if(ts->prg[i].id == programid) {
  178. p = &ts->prg[i];
  179. break;
  180. }
  181. }
  182. if(!p)
  183. return;
  184. if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
  185. return;
  186. p->pids[p->nb_pids++] = pid;
  187. }
  188. /**
  189. * \brief discard_pid() decides if the pid is to be discarded according
  190. * to caller's programs selection
  191. * \param ts : - TS context
  192. * \param pid : - pid
  193. * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
  194. * 0 otherwise
  195. */
  196. static int discard_pid(MpegTSContext *ts, unsigned int pid)
  197. {
  198. int i, j, k;
  199. int used = 0, discarded = 0;
  200. struct Program *p;
  201. for(i=0; i<ts->nb_prg; i++) {
  202. p = &ts->prg[i];
  203. for(j=0; j<p->nb_pids; j++) {
  204. if(p->pids[j] != pid)
  205. continue;
  206. //is program with id p->id set to be discarded?
  207. for(k=0; k<ts->stream->nb_programs; k++) {
  208. if(ts->stream->programs[k]->id == p->id) {
  209. if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
  210. discarded++;
  211. else
  212. used++;
  213. }
  214. }
  215. }
  216. }
  217. return !used && discarded;
  218. }
  219. /**
  220. * Assemble PES packets out of TS packets, and then call the "section_cb"
  221. * function when they are complete.
  222. */
  223. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  224. const uint8_t *buf, int buf_size, int is_start)
  225. {
  226. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  227. int len;
  228. if (is_start) {
  229. memcpy(tss->section_buf, buf, buf_size);
  230. tss->section_index = buf_size;
  231. tss->section_h_size = -1;
  232. tss->end_of_section_reached = 0;
  233. } else {
  234. if (tss->end_of_section_reached)
  235. return;
  236. len = 4096 - tss->section_index;
  237. if (buf_size < len)
  238. len = buf_size;
  239. memcpy(tss->section_buf + tss->section_index, buf, len);
  240. tss->section_index += len;
  241. }
  242. /* compute section length if possible */
  243. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  244. len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
  245. if (len > 4096)
  246. return;
  247. tss->section_h_size = len;
  248. }
  249. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  250. tss->end_of_section_reached = 1;
  251. if (!tss->check_crc ||
  252. av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
  253. tss->section_buf, tss->section_h_size) == 0)
  254. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  255. }
  256. }
  257. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  258. SectionCallback *section_cb, void *opaque,
  259. int check_crc)
  260. {
  261. MpegTSFilter *filter;
  262. MpegTSSectionFilter *sec;
  263. av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
  264. if (pid >= NB_PID_MAX || ts->pids[pid])
  265. return NULL;
  266. filter = av_mallocz(sizeof(MpegTSFilter));
  267. if (!filter)
  268. return NULL;
  269. ts->pids[pid] = filter;
  270. filter->type = MPEGTS_SECTION;
  271. filter->pid = pid;
  272. filter->last_cc = -1;
  273. sec = &filter->u.section_filter;
  274. sec->section_cb = section_cb;
  275. sec->opaque = opaque;
  276. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  277. sec->check_crc = check_crc;
  278. if (!sec->section_buf) {
  279. av_free(filter);
  280. return NULL;
  281. }
  282. return filter;
  283. }
  284. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  285. PESCallback *pes_cb,
  286. void *opaque)
  287. {
  288. MpegTSFilter *filter;
  289. MpegTSPESFilter *pes;
  290. if (pid >= NB_PID_MAX || ts->pids[pid])
  291. return NULL;
  292. filter = av_mallocz(sizeof(MpegTSFilter));
  293. if (!filter)
  294. return NULL;
  295. ts->pids[pid] = filter;
  296. filter->type = MPEGTS_PES;
  297. filter->pid = pid;
  298. filter->last_cc = -1;
  299. pes = &filter->u.pes_filter;
  300. pes->pes_cb = pes_cb;
  301. pes->opaque = opaque;
  302. return filter;
  303. }
  304. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  305. {
  306. int pid;
  307. pid = filter->pid;
  308. if (filter->type == MPEGTS_SECTION)
  309. av_freep(&filter->u.section_filter.section_buf);
  310. else if (filter->type == MPEGTS_PES) {
  311. PESContext *pes = filter->u.pes_filter.opaque;
  312. av_freep(&pes->buffer);
  313. /* referenced private data will be freed later in
  314. * av_close_input_stream */
  315. if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
  316. av_freep(&filter->u.pes_filter.opaque);
  317. }
  318. }
  319. av_free(filter);
  320. ts->pids[pid] = NULL;
  321. }
  322. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  323. int stat[TS_MAX_PACKET_SIZE];
  324. int i;
  325. int x=0;
  326. int best_score=0;
  327. memset(stat, 0, packet_size*sizeof(int));
  328. for(x=i=0; i<size-3; i++){
  329. if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
  330. stat[x]++;
  331. if(stat[x] > best_score){
  332. best_score= stat[x];
  333. if(index) *index= x;
  334. }
  335. }
  336. x++;
  337. if(x == packet_size) x= 0;
  338. }
  339. return best_score;
  340. }
  341. /* autodetect fec presence. Must have at least 1024 bytes */
  342. static int get_packet_size(const uint8_t *buf, int size)
  343. {
  344. int score, fec_score, dvhs_score;
  345. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  346. return -1;
  347. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  348. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  349. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  350. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  351. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  352. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  353. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  354. else return -1;
  355. }
  356. typedef struct SectionHeader {
  357. uint8_t tid;
  358. uint16_t id;
  359. uint8_t version;
  360. uint8_t sec_num;
  361. uint8_t last_sec_num;
  362. } SectionHeader;
  363. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  364. {
  365. const uint8_t *p;
  366. int c;
  367. p = *pp;
  368. if (p >= p_end)
  369. return -1;
  370. c = *p++;
  371. *pp = p;
  372. return c;
  373. }
  374. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  375. {
  376. const uint8_t *p;
  377. int c;
  378. p = *pp;
  379. if ((p + 1) >= p_end)
  380. return -1;
  381. c = AV_RB16(p);
  382. p += 2;
  383. *pp = p;
  384. return c;
  385. }
  386. /* read and allocate a DVB string preceeded by its length */
  387. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  388. {
  389. int len;
  390. const uint8_t *p;
  391. char *str;
  392. p = *pp;
  393. len = get8(&p, p_end);
  394. if (len < 0)
  395. return NULL;
  396. if ((p + len) > p_end)
  397. return NULL;
  398. str = av_malloc(len + 1);
  399. if (!str)
  400. return NULL;
  401. memcpy(str, p, len);
  402. str[len] = '\0';
  403. p += len;
  404. *pp = p;
  405. return str;
  406. }
  407. static int parse_section_header(SectionHeader *h,
  408. const uint8_t **pp, const uint8_t *p_end)
  409. {
  410. int val;
  411. val = get8(pp, p_end);
  412. if (val < 0)
  413. return -1;
  414. h->tid = val;
  415. *pp += 2;
  416. val = get16(pp, p_end);
  417. if (val < 0)
  418. return -1;
  419. h->id = val;
  420. val = get8(pp, p_end);
  421. if (val < 0)
  422. return -1;
  423. h->version = (val >> 1) & 0x1f;
  424. val = get8(pp, p_end);
  425. if (val < 0)
  426. return -1;
  427. h->sec_num = val;
  428. val = get8(pp, p_end);
  429. if (val < 0)
  430. return -1;
  431. h->last_sec_num = val;
  432. return 0;
  433. }
  434. typedef struct {
  435. uint32_t stream_type;
  436. enum AVMediaType codec_type;
  437. enum CodecID codec_id;
  438. } StreamType;
  439. static const StreamType ISO_types[] = {
  440. { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
  441. { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
  442. { 0x03, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
  443. { 0x04, AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3 },
  444. { 0x0f, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC },
  445. { 0x10, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG4 },
  446. { 0x11, AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC_LATM }, /* LATM syntax */
  447. { 0x1b, AVMEDIA_TYPE_VIDEO, CODEC_ID_H264 },
  448. { 0xd1, AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
  449. { 0xea, AVMEDIA_TYPE_VIDEO, CODEC_ID_VC1 },
  450. { 0 },
  451. };
  452. static const StreamType HDMV_types[] = {
  453. { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
  454. { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  455. { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  456. { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
  457. { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
  458. { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
  459. { 0 },
  460. };
  461. /* ATSC ? */
  462. static const StreamType MISC_types[] = {
  463. { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  464. { 0x8a, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  465. { 0 },
  466. };
  467. static const StreamType REGD_types[] = {
  468. { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
  469. { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
  470. { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, CODEC_ID_S302M },
  471. { 0 },
  472. };
  473. /* descriptor present */
  474. static const StreamType DESC_types[] = {
  475. { 0x6a, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */
  476. { 0x7a, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
  477. { 0x7b, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
  478. { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
  479. { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
  480. { 0 },
  481. };
  482. static void mpegts_find_stream_type(AVStream *st,
  483. uint32_t stream_type, const StreamType *types)
  484. {
  485. for (; types->stream_type; types++) {
  486. if (stream_type == types->stream_type) {
  487. st->codec->codec_type = types->codec_type;
  488. st->codec->codec_id = types->codec_id;
  489. st->request_probe = 0;
  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 && !pes->st->request_probe) {
  624. av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
  625. pes->pid, pes->stream_type);
  626. pes->st->request_probe= 1;
  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_metadata_set2(&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_metadata_set2(&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_metadata_set2(&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. #ifdef DEBUG
  910. av_dlog(ts->stream, "PMT: len %i\n", section_len);
  911. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  912. #endif
  913. p_end = section + section_len - 4;
  914. p = section;
  915. if (parse_section_header(h, &p, p_end) < 0)
  916. return;
  917. av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
  918. h->id, h->sec_num, h->last_sec_num);
  919. if (h->tid != PMT_TID)
  920. return;
  921. clear_program(ts, h->id);
  922. pcr_pid = get16(&p, p_end) & 0x1fff;
  923. if (pcr_pid < 0)
  924. return;
  925. add_pid_to_pmt(ts, h->id, pcr_pid);
  926. av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
  927. program_info_length = get16(&p, p_end) & 0xfff;
  928. if (program_info_length < 0)
  929. return;
  930. while(program_info_length >= 2) {
  931. uint8_t tag, len;
  932. tag = get8(&p, p_end);
  933. len = get8(&p, p_end);
  934. av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
  935. if(len > program_info_length - 2)
  936. //something else is broken, exit the program_descriptors_loop
  937. break;
  938. program_info_length -= len + 2;
  939. if (tag == 0x1d) { // IOD descriptor
  940. get8(&p, p_end); // scope
  941. get8(&p, p_end); // label
  942. len -= 2;
  943. mp4_read_iods(ts->stream, p, len, &mp4_es_id,
  944. &mp4_dec_config_descr, &mp4_dec_config_descr_len);
  945. } else if (tag == 0x05 && len >= 4) { // registration descriptor
  946. prog_reg_desc = bytestream_get_le32(&p);
  947. len -= 4;
  948. }
  949. p += len;
  950. }
  951. p += program_info_length;
  952. if (p >= p_end)
  953. goto out;
  954. // stop parsing after pmt, we found header
  955. if (!ts->stream->nb_streams)
  956. ts->stop_parse = 1;
  957. for(;;) {
  958. st = 0;
  959. stream_type = get8(&p, p_end);
  960. if (stream_type < 0)
  961. break;
  962. pid = get16(&p, p_end) & 0x1fff;
  963. if (pid < 0)
  964. break;
  965. /* now create ffmpeg stream */
  966. if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
  967. pes = ts->pids[pid]->u.pes_filter.opaque;
  968. if (!pes->st)
  969. pes->st = av_new_stream(pes->stream, pes->pid);
  970. st = pes->st;
  971. } else {
  972. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  973. pes = add_pes_stream(ts, pid, pcr_pid);
  974. if (pes)
  975. st = av_new_stream(pes->stream, pes->pid);
  976. }
  977. if (!st)
  978. goto out;
  979. if (!pes->stream_type)
  980. mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
  981. add_pid_to_pmt(ts, h->id, pid);
  982. ff_program_add_stream_index(ts->stream, h->id, st->index);
  983. desc_list_len = get16(&p, p_end) & 0xfff;
  984. if (desc_list_len < 0)
  985. break;
  986. desc_list_end = p + desc_list_len;
  987. if (desc_list_end > p_end)
  988. break;
  989. for(;;) {
  990. if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
  991. mp4_dec_config_descr_len, mp4_es_id, pid, mp4_dec_config_descr) < 0)
  992. break;
  993. if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
  994. ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
  995. pes->sub_st->codec->codec_tag = st->codec->codec_tag;
  996. }
  997. }
  998. p = desc_list_end;
  999. }
  1000. out:
  1001. av_free(mp4_dec_config_descr);
  1002. }
  1003. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1004. {
  1005. MpegTSContext *ts = filter->u.section_filter.opaque;
  1006. SectionHeader h1, *h = &h1;
  1007. const uint8_t *p, *p_end;
  1008. int sid, pmt_pid;
  1009. #ifdef DEBUG
  1010. av_dlog(ts->stream, "PAT:\n");
  1011. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  1012. #endif
  1013. p_end = section + section_len - 4;
  1014. p = section;
  1015. if (parse_section_header(h, &p, p_end) < 0)
  1016. return;
  1017. if (h->tid != PAT_TID)
  1018. return;
  1019. clear_programs(ts);
  1020. for(;;) {
  1021. sid = get16(&p, p_end);
  1022. if (sid < 0)
  1023. break;
  1024. pmt_pid = get16(&p, p_end) & 0x1fff;
  1025. if (pmt_pid < 0)
  1026. break;
  1027. av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  1028. if (sid == 0x0000) {
  1029. /* NIT info */
  1030. } else {
  1031. av_new_program(ts->stream, sid);
  1032. if (ts->pids[pmt_pid])
  1033. mpegts_close_filter(ts, ts->pids[pmt_pid]);
  1034. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  1035. add_pat_entry(ts, sid);
  1036. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  1037. add_pid_to_pmt(ts, sid, pmt_pid);
  1038. }
  1039. }
  1040. }
  1041. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1042. {
  1043. MpegTSContext *ts = filter->u.section_filter.opaque;
  1044. SectionHeader h1, *h = &h1;
  1045. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  1046. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  1047. char *name, *provider_name;
  1048. #ifdef DEBUG
  1049. av_dlog(ts->stream, "SDT:\n");
  1050. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  1051. #endif
  1052. p_end = section + section_len - 4;
  1053. p = section;
  1054. if (parse_section_header(h, &p, p_end) < 0)
  1055. return;
  1056. if (h->tid != SDT_TID)
  1057. return;
  1058. onid = get16(&p, p_end);
  1059. if (onid < 0)
  1060. return;
  1061. val = get8(&p, p_end);
  1062. if (val < 0)
  1063. return;
  1064. for(;;) {
  1065. sid = get16(&p, p_end);
  1066. if (sid < 0)
  1067. break;
  1068. val = get8(&p, p_end);
  1069. if (val < 0)
  1070. break;
  1071. desc_list_len = get16(&p, p_end) & 0xfff;
  1072. if (desc_list_len < 0)
  1073. break;
  1074. desc_list_end = p + desc_list_len;
  1075. if (desc_list_end > p_end)
  1076. break;
  1077. for(;;) {
  1078. desc_tag = get8(&p, desc_list_end);
  1079. if (desc_tag < 0)
  1080. break;
  1081. desc_len = get8(&p, desc_list_end);
  1082. desc_end = p + desc_len;
  1083. if (desc_end > desc_list_end)
  1084. break;
  1085. av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
  1086. desc_tag, desc_len);
  1087. switch(desc_tag) {
  1088. case 0x48:
  1089. service_type = get8(&p, p_end);
  1090. if (service_type < 0)
  1091. break;
  1092. provider_name = getstr8(&p, p_end);
  1093. if (!provider_name)
  1094. break;
  1095. name = getstr8(&p, p_end);
  1096. if (name) {
  1097. AVProgram *program = av_new_program(ts->stream, sid);
  1098. if(program) {
  1099. av_metadata_set2(&program->metadata, "service_name", name, 0);
  1100. av_metadata_set2(&program->metadata, "service_provider", provider_name, 0);
  1101. }
  1102. }
  1103. av_free(name);
  1104. av_free(provider_name);
  1105. break;
  1106. default:
  1107. break;
  1108. }
  1109. p = desc_end;
  1110. }
  1111. p = desc_list_end;
  1112. }
  1113. }
  1114. /* handle one TS packet */
  1115. static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  1116. {
  1117. AVFormatContext *s = ts->stream;
  1118. MpegTSFilter *tss;
  1119. int len, pid, cc, cc_ok, afc, is_start;
  1120. const uint8_t *p, *p_end;
  1121. int64_t pos;
  1122. pid = AV_RB16(packet + 1) & 0x1fff;
  1123. if(pid && discard_pid(ts, pid))
  1124. return 0;
  1125. is_start = packet[1] & 0x40;
  1126. tss = ts->pids[pid];
  1127. if (ts->auto_guess && tss == NULL && is_start) {
  1128. add_pes_stream(ts, pid, -1);
  1129. tss = ts->pids[pid];
  1130. }
  1131. if (!tss)
  1132. return 0;
  1133. /* continuity check (currently not used) */
  1134. cc = (packet[3] & 0xf);
  1135. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  1136. tss->last_cc = cc;
  1137. /* skip adaptation field */
  1138. afc = (packet[3] >> 4) & 3;
  1139. p = packet + 4;
  1140. if (afc == 0) /* reserved value */
  1141. return 0;
  1142. if (afc == 2) /* adaptation field only */
  1143. return 0;
  1144. if (afc == 3) {
  1145. /* skip adapation field */
  1146. p += p[0] + 1;
  1147. }
  1148. /* if past the end of packet, ignore */
  1149. p_end = packet + TS_PACKET_SIZE;
  1150. if (p >= p_end)
  1151. return 0;
  1152. pos = avio_tell(ts->stream->pb);
  1153. ts->pos47= pos % ts->raw_packet_size;
  1154. if (tss->type == MPEGTS_SECTION) {
  1155. if (is_start) {
  1156. /* pointer field present */
  1157. len = *p++;
  1158. if (p + len > p_end)
  1159. return 0;
  1160. if (len && cc_ok) {
  1161. /* write remaining section bytes */
  1162. write_section_data(s, tss,
  1163. p, len, 0);
  1164. /* check whether filter has been closed */
  1165. if (!ts->pids[pid])
  1166. return 0;
  1167. }
  1168. p += len;
  1169. if (p < p_end) {
  1170. write_section_data(s, tss,
  1171. p, p_end - p, 1);
  1172. }
  1173. } else {
  1174. if (cc_ok) {
  1175. write_section_data(s, tss,
  1176. p, p_end - p, 0);
  1177. }
  1178. }
  1179. } else {
  1180. int ret;
  1181. // Note: The position here points actually behind the current packet.
  1182. if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
  1183. pos - ts->raw_packet_size)) < 0)
  1184. return ret;
  1185. }
  1186. return 0;
  1187. }
  1188. /* XXX: try to find a better synchro over several packets (use
  1189. get_packet_size() ?) */
  1190. static int mpegts_resync(AVFormatContext *s)
  1191. {
  1192. AVIOContext *pb = s->pb;
  1193. int c, i;
  1194. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1195. c = avio_r8(pb);
  1196. if (url_feof(pb))
  1197. return -1;
  1198. if (c == 0x47) {
  1199. avio_seek(pb, -1, SEEK_CUR);
  1200. return 0;
  1201. }
  1202. }
  1203. av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
  1204. /* no sync found */
  1205. return -1;
  1206. }
  1207. /* return -1 if error or EOF. Return 0 if OK. */
  1208. static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
  1209. {
  1210. AVIOContext *pb = s->pb;
  1211. int skip, len;
  1212. for(;;) {
  1213. len = avio_read(pb, buf, TS_PACKET_SIZE);
  1214. if (len != TS_PACKET_SIZE)
  1215. return len < 0 ? len : AVERROR_EOF;
  1216. /* check paquet sync byte */
  1217. if (buf[0] != 0x47) {
  1218. /* find a new packet start */
  1219. avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1220. if (mpegts_resync(s) < 0)
  1221. return AVERROR(EAGAIN);
  1222. else
  1223. continue;
  1224. } else {
  1225. skip = raw_packet_size - TS_PACKET_SIZE;
  1226. if (skip > 0)
  1227. avio_skip(pb, skip);
  1228. break;
  1229. }
  1230. }
  1231. return 0;
  1232. }
  1233. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1234. {
  1235. AVFormatContext *s = ts->stream;
  1236. uint8_t packet[TS_PACKET_SIZE];
  1237. int packet_num, ret;
  1238. ts->stop_parse = 0;
  1239. packet_num = 0;
  1240. for(;;) {
  1241. if (ts->stop_parse>0)
  1242. break;
  1243. packet_num++;
  1244. if (nb_packets != 0 && packet_num >= nb_packets)
  1245. break;
  1246. ret = read_packet(s, packet, ts->raw_packet_size);
  1247. if (ret != 0)
  1248. return ret;
  1249. ret = handle_packet(ts, packet);
  1250. if (ret != 0)
  1251. return ret;
  1252. }
  1253. return 0;
  1254. }
  1255. static int mpegts_probe(AVProbeData *p)
  1256. {
  1257. #if 1
  1258. const int size= p->buf_size;
  1259. int score, fec_score, dvhs_score;
  1260. int check_count= size / TS_FEC_PACKET_SIZE;
  1261. #define CHECK_COUNT 10
  1262. if (check_count < CHECK_COUNT)
  1263. return -1;
  1264. score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1265. dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
  1266. fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1267. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  1268. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  1269. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1270. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1271. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1272. else return -1;
  1273. #else
  1274. /* only use the extension for safer guess */
  1275. if (av_match_ext(p->filename, "ts"))
  1276. return AVPROBE_SCORE_MAX;
  1277. else
  1278. return 0;
  1279. #endif
  1280. }
  1281. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1282. (-1) if not available */
  1283. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1284. const uint8_t *packet)
  1285. {
  1286. int afc, len, flags;
  1287. const uint8_t *p;
  1288. unsigned int v;
  1289. afc = (packet[3] >> 4) & 3;
  1290. if (afc <= 1)
  1291. return -1;
  1292. p = packet + 4;
  1293. len = p[0];
  1294. p++;
  1295. if (len == 0)
  1296. return -1;
  1297. flags = *p++;
  1298. len--;
  1299. if (!(flags & 0x10))
  1300. return -1;
  1301. if (len < 6)
  1302. return -1;
  1303. v = AV_RB32(p);
  1304. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1305. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1306. return 0;
  1307. }
  1308. static int mpegts_read_header(AVFormatContext *s,
  1309. AVFormatParameters *ap)
  1310. {
  1311. MpegTSContext *ts = s->priv_data;
  1312. AVIOContext *pb = s->pb;
  1313. uint8_t buf[8*1024];
  1314. int len;
  1315. int64_t pos;
  1316. #if FF_API_FORMAT_PARAMETERS
  1317. if (ap) {
  1318. if (ap->mpeg2ts_compute_pcr)
  1319. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1320. if(ap->mpeg2ts_raw){
  1321. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1322. return -1;
  1323. }
  1324. }
  1325. #endif
  1326. /* read the first 1024 bytes to get packet size */
  1327. pos = avio_tell(pb);
  1328. len = avio_read(pb, buf, sizeof(buf));
  1329. if (len != sizeof(buf))
  1330. goto fail;
  1331. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1332. if (ts->raw_packet_size <= 0) {
  1333. av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
  1334. ts->raw_packet_size = TS_PACKET_SIZE;
  1335. }
  1336. ts->stream = s;
  1337. ts->auto_guess = 0;
  1338. if (s->iformat == &ff_mpegts_demuxer) {
  1339. /* normal demux */
  1340. /* first do a scaning to get all the services */
  1341. if (avio_seek(pb, pos, SEEK_SET) < 0)
  1342. av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
  1343. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1344. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1345. handle_packets(ts, s->probesize / ts->raw_packet_size);
  1346. /* if could not find service, enable auto_guess */
  1347. ts->auto_guess = 1;
  1348. av_dlog(ts->stream, "tuning done\n");
  1349. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1350. } else {
  1351. AVStream *st;
  1352. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1353. int64_t pcrs[2], pcr_h;
  1354. int packet_count[2];
  1355. uint8_t packet[TS_PACKET_SIZE];
  1356. /* only read packets */
  1357. st = av_new_stream(s, 0);
  1358. if (!st)
  1359. goto fail;
  1360. av_set_pts_info(st, 60, 1, 27000000);
  1361. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1362. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1363. /* we iterate until we find two PCRs to estimate the bitrate */
  1364. pcr_pid = -1;
  1365. nb_pcrs = 0;
  1366. nb_packets = 0;
  1367. for(;;) {
  1368. ret = read_packet(s, packet, ts->raw_packet_size);
  1369. if (ret < 0)
  1370. return -1;
  1371. pid = AV_RB16(packet + 1) & 0x1fff;
  1372. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1373. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1374. pcr_pid = pid;
  1375. packet_count[nb_pcrs] = nb_packets;
  1376. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1377. nb_pcrs++;
  1378. if (nb_pcrs >= 2)
  1379. break;
  1380. }
  1381. nb_packets++;
  1382. }
  1383. /* NOTE1: the bitrate is computed without the FEC */
  1384. /* NOTE2: it is only the bitrate of the start of the stream */
  1385. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1386. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1387. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1388. st->codec->bit_rate = s->bit_rate;
  1389. st->start_time = ts->cur_pcr;
  1390. #if 0
  1391. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1392. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1393. #endif
  1394. }
  1395. avio_seek(pb, pos, SEEK_SET);
  1396. return 0;
  1397. fail:
  1398. return -1;
  1399. }
  1400. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1401. static int mpegts_raw_read_packet(AVFormatContext *s,
  1402. AVPacket *pkt)
  1403. {
  1404. MpegTSContext *ts = s->priv_data;
  1405. int ret, i;
  1406. int64_t pcr_h, next_pcr_h, pos;
  1407. int pcr_l, next_pcr_l;
  1408. uint8_t pcr_buf[12];
  1409. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1410. return AVERROR(ENOMEM);
  1411. pkt->pos= avio_tell(s->pb);
  1412. ret = read_packet(s, pkt->data, ts->raw_packet_size);
  1413. if (ret < 0) {
  1414. av_free_packet(pkt);
  1415. return ret;
  1416. }
  1417. if (ts->mpeg2ts_compute_pcr) {
  1418. /* compute exact PCR for each packet */
  1419. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1420. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1421. pos = avio_tell(s->pb);
  1422. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1423. avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1424. avio_read(s->pb, pcr_buf, 12);
  1425. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1426. /* XXX: not precise enough */
  1427. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1428. (i + 1);
  1429. break;
  1430. }
  1431. }
  1432. avio_seek(s->pb, pos, SEEK_SET);
  1433. /* no next PCR found: we use previous increment */
  1434. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1435. }
  1436. pkt->pts = ts->cur_pcr;
  1437. pkt->duration = ts->pcr_incr;
  1438. ts->cur_pcr += ts->pcr_incr;
  1439. }
  1440. pkt->stream_index = 0;
  1441. return 0;
  1442. }
  1443. static int mpegts_read_packet(AVFormatContext *s,
  1444. AVPacket *pkt)
  1445. {
  1446. MpegTSContext *ts = s->priv_data;
  1447. int ret, i;
  1448. if (avio_tell(s->pb) != ts->last_pos) {
  1449. /* seek detected, flush pes buffer */
  1450. for (i = 0; i < NB_PID_MAX; i++) {
  1451. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1452. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1453. av_freep(&pes->buffer);
  1454. pes->data_index = 0;
  1455. pes->state = MPEGTS_SKIP; /* skip until pes header */
  1456. }
  1457. }
  1458. }
  1459. ts->pkt = pkt;
  1460. ret = handle_packets(ts, 0);
  1461. if (ret < 0) {
  1462. /* flush pes data left */
  1463. for (i = 0; i < NB_PID_MAX; i++) {
  1464. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  1465. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1466. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  1467. new_pes_packet(pes, pkt);
  1468. pes->state = MPEGTS_SKIP;
  1469. ret = 0;
  1470. break;
  1471. }
  1472. }
  1473. }
  1474. }
  1475. ts->last_pos = avio_tell(s->pb);
  1476. return ret;
  1477. }
  1478. static int mpegts_read_close(AVFormatContext *s)
  1479. {
  1480. MpegTSContext *ts = s->priv_data;
  1481. int i;
  1482. clear_programs(ts);
  1483. for(i=0;i<NB_PID_MAX;i++)
  1484. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1485. return 0;
  1486. }
  1487. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1488. int64_t *ppos, int64_t pos_limit)
  1489. {
  1490. MpegTSContext *ts = s->priv_data;
  1491. int64_t pos, timestamp;
  1492. uint8_t buf[TS_PACKET_SIZE];
  1493. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1494. const int find_next= 1;
  1495. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1496. if (find_next) {
  1497. for(;;) {
  1498. avio_seek(s->pb, pos, SEEK_SET);
  1499. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1500. return AV_NOPTS_VALUE;
  1501. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1502. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1503. break;
  1504. }
  1505. pos += ts->raw_packet_size;
  1506. }
  1507. } else {
  1508. for(;;) {
  1509. pos -= ts->raw_packet_size;
  1510. if (pos < 0)
  1511. return AV_NOPTS_VALUE;
  1512. avio_seek(s->pb, pos, SEEK_SET);
  1513. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1514. return AV_NOPTS_VALUE;
  1515. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1516. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1517. break;
  1518. }
  1519. }
  1520. }
  1521. *ppos = pos;
  1522. return timestamp;
  1523. }
  1524. #ifdef USE_SYNCPOINT_SEARCH
  1525. static int read_seek2(AVFormatContext *s,
  1526. int stream_index,
  1527. int64_t min_ts,
  1528. int64_t target_ts,
  1529. int64_t max_ts,
  1530. int flags)
  1531. {
  1532. int64_t pos;
  1533. int64_t ts_ret, ts_adj;
  1534. int stream_index_gen_search;
  1535. AVStream *st;
  1536. AVParserState *backup;
  1537. backup = ff_store_parser_state(s);
  1538. // detect direction of seeking for search purposes
  1539. flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
  1540. AVSEEK_FLAG_BACKWARD : 0;
  1541. if (flags & AVSEEK_FLAG_BYTE) {
  1542. // use position directly, we will search starting from it
  1543. pos = target_ts;
  1544. } else {
  1545. // search for some position with good timestamp match
  1546. if (stream_index < 0) {
  1547. stream_index_gen_search = av_find_default_stream_index(s);
  1548. if (stream_index_gen_search < 0) {
  1549. ff_restore_parser_state(s, backup);
  1550. return -1;
  1551. }
  1552. st = s->streams[stream_index_gen_search];
  1553. // timestamp for default must be expressed in AV_TIME_BASE units
  1554. ts_adj = av_rescale(target_ts,
  1555. st->time_base.den,
  1556. AV_TIME_BASE * (int64_t)st->time_base.num);
  1557. } else {
  1558. ts_adj = target_ts;
  1559. stream_index_gen_search = stream_index;
  1560. }
  1561. pos = av_gen_search(s, stream_index_gen_search, ts_adj,
  1562. 0, INT64_MAX, -1,
  1563. AV_NOPTS_VALUE,
  1564. AV_NOPTS_VALUE,
  1565. flags, &ts_ret, mpegts_get_pcr);
  1566. if (pos < 0) {
  1567. ff_restore_parser_state(s, backup);
  1568. return -1;
  1569. }
  1570. }
  1571. // search for actual matching keyframe/starting position for all streams
  1572. if (ff_gen_syncpoint_search(s, stream_index, pos,
  1573. min_ts, target_ts, max_ts,
  1574. flags) < 0) {
  1575. ff_restore_parser_state(s, backup);
  1576. return -1;
  1577. }
  1578. ff_free_parser_state(s, backup);
  1579. return 0;
  1580. }
  1581. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
  1582. {
  1583. int ret;
  1584. if (flags & AVSEEK_FLAG_BACKWARD) {
  1585. flags &= ~AVSEEK_FLAG_BACKWARD;
  1586. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
  1587. if (ret < 0)
  1588. // for compatibility reasons, seek to the best-fitting timestamp
  1589. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
  1590. } else {
  1591. ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
  1592. if (ret < 0)
  1593. // for compatibility reasons, seek to the best-fitting timestamp
  1594. ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
  1595. }
  1596. return ret;
  1597. }
  1598. #else
  1599. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1600. MpegTSContext *ts = s->priv_data;
  1601. uint8_t buf[TS_PACKET_SIZE];
  1602. int64_t pos;
  1603. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1604. return -1;
  1605. pos= avio_tell(s->pb);
  1606. for(;;) {
  1607. avio_seek(s->pb, pos, SEEK_SET);
  1608. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1609. return -1;
  1610. // pid = AV_RB16(buf + 1) & 0x1fff;
  1611. if(buf[1] & 0x40) break;
  1612. pos += ts->raw_packet_size;
  1613. }
  1614. avio_seek(s->pb, pos, SEEK_SET);
  1615. return 0;
  1616. }
  1617. #endif
  1618. /**************************************************************/
  1619. /* parsing functions - called from other demuxers such as RTP */
  1620. MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
  1621. {
  1622. MpegTSContext *ts;
  1623. ts = av_mallocz(sizeof(MpegTSContext));
  1624. if (!ts)
  1625. return NULL;
  1626. /* no stream case, currently used by RTP */
  1627. ts->raw_packet_size = TS_PACKET_SIZE;
  1628. ts->stream = s;
  1629. ts->auto_guess = 1;
  1630. return ts;
  1631. }
  1632. /* return the consumed length if a packet was output, or -1 if no
  1633. packet is output */
  1634. int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1635. const uint8_t *buf, int len)
  1636. {
  1637. int len1;
  1638. len1 = len;
  1639. ts->pkt = pkt;
  1640. ts->stop_parse = 0;
  1641. for(;;) {
  1642. if (ts->stop_parse>0)
  1643. break;
  1644. if (len < TS_PACKET_SIZE)
  1645. return -1;
  1646. if (buf[0] != 0x47) {
  1647. buf++;
  1648. len--;
  1649. } else {
  1650. handle_packet(ts, buf);
  1651. buf += TS_PACKET_SIZE;
  1652. len -= TS_PACKET_SIZE;
  1653. }
  1654. }
  1655. return len1 - len;
  1656. }
  1657. void ff_mpegts_parse_close(MpegTSContext *ts)
  1658. {
  1659. int i;
  1660. for(i=0;i<NB_PID_MAX;i++)
  1661. av_free(ts->pids[i]);
  1662. av_free(ts);
  1663. }
  1664. AVInputFormat ff_mpegts_demuxer = {
  1665. "mpegts",
  1666. NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
  1667. sizeof(MpegTSContext),
  1668. mpegts_probe,
  1669. mpegts_read_header,
  1670. mpegts_read_packet,
  1671. mpegts_read_close,
  1672. read_seek,
  1673. mpegts_get_pcr,
  1674. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1675. #ifdef USE_SYNCPOINT_SEARCH
  1676. .read_seek2 = read_seek2,
  1677. #endif
  1678. };
  1679. AVInputFormat ff_mpegtsraw_demuxer = {
  1680. "mpegtsraw",
  1681. NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
  1682. sizeof(MpegTSContext),
  1683. NULL,
  1684. mpegts_read_header,
  1685. mpegts_raw_read_packet,
  1686. mpegts_read_close,
  1687. read_seek,
  1688. mpegts_get_pcr,
  1689. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1690. #ifdef USE_SYNCPOINT_SEARCH
  1691. .read_seek2 = read_seek2,
  1692. #endif
  1693. .priv_class = &mpegtsraw_class,
  1694. };