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.

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