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.

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