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.

1923 lines
60KB

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