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.

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