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.

1937 lines
60KB

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