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.

1523 lines
43KB

  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. #include "libavutil/crc.h"
  22. #include "avformat.h"
  23. #include "mpegts.h"
  24. //#define DEBUG_SI
  25. //#define DEBUG_SEEK
  26. /* 1.0 second at 24Mbit/s */
  27. #define MAX_SCAN_PACKETS 32000
  28. /* maximum size in which we look for synchronisation if
  29. synchronisation is lost */
  30. #define MAX_RESYNC_SIZE 4096
  31. #define REGISTRATION_DESCRIPTOR 5
  32. typedef struct PESContext PESContext;
  33. static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type);
  34. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
  35. extern void av_set_program_name(AVProgram *program, char *provider_name, char *name);
  36. extern void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx);
  37. enum MpegTSFilterType {
  38. MPEGTS_PES,
  39. MPEGTS_SECTION,
  40. };
  41. typedef struct MpegTSFilter MpegTSFilter;
  42. typedef void PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start);
  43. typedef struct MpegTSPESFilter {
  44. PESCallback *pes_cb;
  45. void *opaque;
  46. } MpegTSPESFilter;
  47. typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
  48. typedef void SetServiceCallback(void *opaque, int ret);
  49. typedef struct MpegTSSectionFilter {
  50. int section_index;
  51. int section_h_size;
  52. uint8_t *section_buf;
  53. unsigned int check_crc:1;
  54. unsigned int end_of_section_reached:1;
  55. SectionCallback *section_cb;
  56. void *opaque;
  57. } MpegTSSectionFilter;
  58. struct MpegTSFilter {
  59. int pid;
  60. int last_cc; /* last cc code (-1 if first packet) */
  61. enum MpegTSFilterType type;
  62. union {
  63. MpegTSPESFilter pes_filter;
  64. MpegTSSectionFilter section_filter;
  65. } u;
  66. };
  67. #define MAX_PIDS_PER_PROGRAM 64
  68. typedef struct {
  69. unsigned int id; //program id/service id
  70. unsigned int nb_pids;
  71. unsigned int pids[MAX_PIDS_PER_PROGRAM];
  72. } Program_t;
  73. struct MpegTSContext {
  74. /* user data */
  75. AVFormatContext *stream;
  76. /** raw packet size, including FEC if present */
  77. int raw_packet_size;
  78. int pos47;
  79. /** if true, all pids are analyzed to find streams */
  80. int auto_guess;
  81. /** compute exact PCR for each transport stream packet */
  82. int mpeg2ts_compute_pcr;
  83. int64_t cur_pcr; /**< used to estimate the exact PCR */
  84. int pcr_incr; /**< used to estimate the exact PCR */
  85. /* data needed to handle file based ts */
  86. /** stop parsing loop */
  87. int stop_parse;
  88. /** packet containing Audio/Video data */
  89. AVPacket *pkt;
  90. /******************************************/
  91. /* private mpegts data */
  92. /* scan context */
  93. /** structure to keep track of Program->pids mapping */
  94. unsigned int nb_prg;
  95. Program_t *prg;
  96. /** filters for various streams specified by PMT + for the PAT and PMT */
  97. MpegTSFilter *pids[NB_PID_MAX];
  98. };
  99. /* TS stream handling */
  100. enum MpegTSState {
  101. MPEGTS_HEADER = 0,
  102. MPEGTS_PESHEADER_FILL,
  103. MPEGTS_PAYLOAD,
  104. MPEGTS_SKIP,
  105. };
  106. /* enough for PES header + length */
  107. #define PES_START_SIZE 9
  108. #define MAX_PES_HEADER_SIZE (9 + 255)
  109. struct PESContext {
  110. int pid;
  111. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  112. int stream_type;
  113. MpegTSContext *ts;
  114. AVFormatContext *stream;
  115. AVStream *st;
  116. enum MpegTSState state;
  117. /* used to get the format */
  118. int data_index;
  119. int total_size;
  120. int pes_header_size;
  121. int64_t pts, dts;
  122. uint8_t header[MAX_PES_HEADER_SIZE];
  123. };
  124. extern AVInputFormat mpegts_demuxer;
  125. static void clear_program(MpegTSContext *ts, unsigned int programid)
  126. {
  127. int i;
  128. for(i=0; i<ts->nb_prg; i++)
  129. if(ts->prg[i].id == programid)
  130. ts->prg[i].nb_pids = 0;
  131. }
  132. static void clear_programs(MpegTSContext *ts)
  133. {
  134. av_freep(&ts->prg);
  135. ts->nb_prg=0;
  136. }
  137. static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
  138. {
  139. Program_t *p;
  140. void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(Program_t));
  141. if(!tmp)
  142. return;
  143. ts->prg = tmp;
  144. p = &ts->prg[ts->nb_prg];
  145. p->id = programid;
  146. p->nb_pids = 0;
  147. ts->nb_prg++;
  148. }
  149. static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
  150. {
  151. int i;
  152. Program_t *p = NULL;
  153. for(i=0; i<ts->nb_prg; i++) {
  154. if(ts->prg[i].id == programid) {
  155. p = &ts->prg[i];
  156. break;
  157. }
  158. }
  159. if(!p)
  160. return;
  161. if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
  162. return;
  163. p->pids[p->nb_pids++] = pid;
  164. }
  165. /**
  166. * \brief discard_pid() decides if the pid is to be discarded according
  167. * to caller's programs selection
  168. * \param ts : - TS context
  169. * \param pid : - pid
  170. * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
  171. * 0 otherwise
  172. */
  173. static int discard_pid(MpegTSContext *ts, unsigned int pid)
  174. {
  175. int i, j, k;
  176. int used = 0, discarded = 0;
  177. Program_t *p;
  178. for(i=0; i<ts->nb_prg; i++) {
  179. p = &ts->prg[i];
  180. for(j=0; j<p->nb_pids; j++) {
  181. if(p->pids[j] != pid)
  182. continue;
  183. //is program with id p->id set to be discarded?
  184. for(k=0; k<ts->stream->nb_programs; k++) {
  185. if(ts->stream->programs[k]->id == p->id) {
  186. if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
  187. discarded++;
  188. else
  189. used++;
  190. }
  191. }
  192. }
  193. }
  194. return !used && discarded;
  195. }
  196. /**
  197. * Assembles PES packets out of TS packets, and then calls the "section_cb"
  198. * function when they are complete.
  199. */
  200. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  201. const uint8_t *buf, int buf_size, int is_start)
  202. {
  203. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  204. int len;
  205. if (is_start) {
  206. memcpy(tss->section_buf, buf, buf_size);
  207. tss->section_index = buf_size;
  208. tss->section_h_size = -1;
  209. tss->end_of_section_reached = 0;
  210. } else {
  211. if (tss->end_of_section_reached)
  212. return;
  213. len = 4096 - tss->section_index;
  214. if (buf_size < len)
  215. len = buf_size;
  216. memcpy(tss->section_buf + tss->section_index, buf, len);
  217. tss->section_index += len;
  218. }
  219. /* compute section length if possible */
  220. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  221. len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
  222. if (len > 4096)
  223. return;
  224. tss->section_h_size = len;
  225. }
  226. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  227. tss->end_of_section_reached = 1;
  228. if (!tss->check_crc ||
  229. av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
  230. tss->section_buf, tss->section_h_size) == 0)
  231. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  232. }
  233. }
  234. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  235. SectionCallback *section_cb, void *opaque,
  236. int check_crc)
  237. {
  238. MpegTSFilter *filter;
  239. MpegTSSectionFilter *sec;
  240. #ifdef DEBUG_SI
  241. av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
  242. #endif
  243. if (pid >= NB_PID_MAX || ts->pids[pid])
  244. return NULL;
  245. filter = av_mallocz(sizeof(MpegTSFilter));
  246. if (!filter)
  247. return NULL;
  248. ts->pids[pid] = filter;
  249. filter->type = MPEGTS_SECTION;
  250. filter->pid = pid;
  251. filter->last_cc = -1;
  252. sec = &filter->u.section_filter;
  253. sec->section_cb = section_cb;
  254. sec->opaque = opaque;
  255. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  256. sec->check_crc = check_crc;
  257. if (!sec->section_buf) {
  258. av_free(filter);
  259. return NULL;
  260. }
  261. return filter;
  262. }
  263. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  264. PESCallback *pes_cb,
  265. void *opaque)
  266. {
  267. MpegTSFilter *filter;
  268. MpegTSPESFilter *pes;
  269. if (pid >= NB_PID_MAX || ts->pids[pid])
  270. return NULL;
  271. filter = av_mallocz(sizeof(MpegTSFilter));
  272. if (!filter)
  273. return NULL;
  274. ts->pids[pid] = filter;
  275. filter->type = MPEGTS_PES;
  276. filter->pid = pid;
  277. filter->last_cc = -1;
  278. pes = &filter->u.pes_filter;
  279. pes->pes_cb = pes_cb;
  280. pes->opaque = opaque;
  281. return filter;
  282. }
  283. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  284. {
  285. int pid;
  286. pid = filter->pid;
  287. if (filter->type == MPEGTS_SECTION)
  288. av_freep(&filter->u.section_filter.section_buf);
  289. av_free(filter);
  290. ts->pids[pid] = NULL;
  291. }
  292. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  293. int stat[packet_size];
  294. int i;
  295. int x=0;
  296. int best_score=0;
  297. memset(stat, 0, packet_size*sizeof(int));
  298. for(x=i=0; i<size; i++){
  299. if(buf[i] == 0x47){
  300. stat[x]++;
  301. if(stat[x] > best_score){
  302. best_score= stat[x];
  303. if(index) *index= x;
  304. }
  305. }
  306. x++;
  307. if(x == packet_size) x= 0;
  308. }
  309. return best_score;
  310. }
  311. /* autodetect fec presence. Must have at least 1024 bytes */
  312. static int get_packet_size(const uint8_t *buf, int size)
  313. {
  314. int score, fec_score, dvhs_score;
  315. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  316. return -1;
  317. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  318. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  319. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  320. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  321. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  322. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  323. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  324. else return -1;
  325. }
  326. typedef struct SectionHeader {
  327. uint8_t tid;
  328. uint16_t id;
  329. uint8_t version;
  330. uint8_t sec_num;
  331. uint8_t last_sec_num;
  332. } SectionHeader;
  333. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  334. {
  335. const uint8_t *p;
  336. int c;
  337. p = *pp;
  338. if (p >= p_end)
  339. return -1;
  340. c = *p++;
  341. *pp = p;
  342. return c;
  343. }
  344. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  345. {
  346. const uint8_t *p;
  347. int c;
  348. p = *pp;
  349. if ((p + 1) >= p_end)
  350. return -1;
  351. c = AV_RB16(p);
  352. p += 2;
  353. *pp = p;
  354. return c;
  355. }
  356. /* read and allocate a DVB string preceeded by its length */
  357. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  358. {
  359. int len;
  360. const uint8_t *p;
  361. char *str;
  362. p = *pp;
  363. len = get8(&p, p_end);
  364. if (len < 0)
  365. return NULL;
  366. if ((p + len) > p_end)
  367. return NULL;
  368. str = av_malloc(len + 1);
  369. if (!str)
  370. return NULL;
  371. memcpy(str, p, len);
  372. str[len] = '\0';
  373. p += len;
  374. *pp = p;
  375. return str;
  376. }
  377. static int parse_section_header(SectionHeader *h,
  378. const uint8_t **pp, const uint8_t *p_end)
  379. {
  380. int val;
  381. val = get8(pp, p_end);
  382. if (val < 0)
  383. return -1;
  384. h->tid = val;
  385. *pp += 2;
  386. val = get16(pp, p_end);
  387. if (val < 0)
  388. return -1;
  389. h->id = val;
  390. val = get8(pp, p_end);
  391. if (val < 0)
  392. return -1;
  393. h->version = (val >> 1) & 0x1f;
  394. val = get8(pp, p_end);
  395. if (val < 0)
  396. return -1;
  397. h->sec_num = val;
  398. val = get8(pp, p_end);
  399. if (val < 0)
  400. return -1;
  401. h->last_sec_num = val;
  402. return 0;
  403. }
  404. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  405. {
  406. MpegTSContext *ts = filter->u.section_filter.opaque;
  407. SectionHeader h1, *h = &h1;
  408. PESContext *pes;
  409. AVStream *st;
  410. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  411. int program_info_length, pcr_pid, pid, stream_type;
  412. int desc_list_len, desc_len, desc_tag;
  413. int comp_page = 0, anc_page = 0; /* initialize to kill warnings */
  414. char language[4] = {0}; /* initialize to kill warnings */
  415. int has_hdmv_descr = 0;
  416. #ifdef DEBUG_SI
  417. av_log(ts->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
  418. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  419. #endif
  420. p_end = section + section_len - 4;
  421. p = section;
  422. if (parse_section_header(h, &p, p_end) < 0)
  423. return;
  424. #ifdef DEBUG_SI
  425. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n",
  426. h->id, h->sec_num, h->last_sec_num);
  427. #endif
  428. if (h->tid != PMT_TID)
  429. return;
  430. clear_program(ts, h->id);
  431. pcr_pid = get16(&p, p_end) & 0x1fff;
  432. if (pcr_pid < 0)
  433. return;
  434. add_pid_to_pmt(ts, h->id, pcr_pid);
  435. #ifdef DEBUG_SI
  436. av_log(ts->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
  437. #endif
  438. program_info_length = get16(&p, p_end) & 0xfff;
  439. if (program_info_length < 0)
  440. return;
  441. while(program_info_length >= 2) {
  442. uint8_t tag, len;
  443. tag = get8(&p, p_end);
  444. len = get8(&p, p_end);
  445. if(len > program_info_length - 2)
  446. //something else is broken, exit the program_descriptors_loop
  447. break;
  448. program_info_length -= len + 2;
  449. if(tag == REGISTRATION_DESCRIPTOR && len >= 4) {
  450. uint8_t bytes[4];
  451. bytes[0] = get8(&p, p_end);
  452. bytes[1] = get8(&p, p_end);
  453. bytes[2] = get8(&p, p_end);
  454. bytes[3] = get8(&p, p_end);
  455. len -= 4;
  456. if(bytes[0] == 'H' && bytes[1] == 'D' &&
  457. bytes[2] == 'M' && bytes[3] == 'V')
  458. has_hdmv_descr = 1;
  459. }
  460. p += len;
  461. }
  462. p += program_info_length;
  463. if (p >= p_end)
  464. return;
  465. for(;;) {
  466. language[0] = 0;
  467. st = 0;
  468. stream_type = get8(&p, p_end);
  469. if (stream_type < 0)
  470. break;
  471. pid = get16(&p, p_end) & 0x1fff;
  472. if (pid < 0)
  473. break;
  474. desc_list_len = get16(&p, p_end) & 0xfff;
  475. if (desc_list_len < 0)
  476. break;
  477. desc_list_end = p + desc_list_len;
  478. if (desc_list_end > p_end)
  479. break;
  480. for(;;) {
  481. desc_tag = get8(&p, desc_list_end);
  482. if (desc_tag < 0)
  483. break;
  484. if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
  485. if((desc_tag == 0x6A) || (desc_tag == 0x7A)) {
  486. /*assume DVB AC-3 Audio*/
  487. stream_type = STREAM_TYPE_AUDIO_AC3;
  488. } else if(desc_tag == 0x7B) {
  489. /* DVB DTS audio */
  490. stream_type = STREAM_TYPE_AUDIO_DTS;
  491. }
  492. }
  493. desc_len = get8(&p, desc_list_end);
  494. desc_end = p + desc_len;
  495. if (desc_end > desc_list_end)
  496. break;
  497. #ifdef DEBUG_SI
  498. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  499. desc_tag, desc_len);
  500. #endif
  501. switch(desc_tag) {
  502. case DVB_SUBT_DESCID:
  503. if (stream_type == STREAM_TYPE_PRIVATE_DATA)
  504. stream_type = STREAM_TYPE_SUBTITLE_DVB;
  505. language[0] = get8(&p, desc_end);
  506. language[1] = get8(&p, desc_end);
  507. language[2] = get8(&p, desc_end);
  508. language[3] = 0;
  509. get8(&p, desc_end);
  510. comp_page = get16(&p, desc_end);
  511. anc_page = get16(&p, desc_end);
  512. break;
  513. case 0x0a: /* ISO 639 language descriptor */
  514. language[0] = get8(&p, desc_end);
  515. language[1] = get8(&p, desc_end);
  516. language[2] = get8(&p, desc_end);
  517. language[3] = 0;
  518. break;
  519. default:
  520. break;
  521. }
  522. p = desc_end;
  523. }
  524. p = desc_list_end;
  525. #ifdef DEBUG_SI
  526. av_log(ts->stream, AV_LOG_DEBUG, "stream_type=%d pid=0x%x\n",
  527. stream_type, pid);
  528. #endif
  529. /* now create ffmpeg stream */
  530. switch(stream_type) {
  531. case STREAM_TYPE_AUDIO_MPEG1:
  532. case STREAM_TYPE_AUDIO_MPEG2:
  533. case STREAM_TYPE_VIDEO_MPEG1:
  534. case STREAM_TYPE_VIDEO_MPEG2:
  535. case STREAM_TYPE_VIDEO_MPEG4:
  536. case STREAM_TYPE_VIDEO_H264:
  537. case STREAM_TYPE_VIDEO_VC1:
  538. case STREAM_TYPE_AUDIO_AAC:
  539. case STREAM_TYPE_AUDIO_AC3:
  540. case STREAM_TYPE_AUDIO_DTS:
  541. case STREAM_TYPE_AUDIO_HDMV_DTS:
  542. case STREAM_TYPE_SUBTITLE_DVB:
  543. if(stream_type == STREAM_TYPE_AUDIO_HDMV_DTS && !has_hdmv_descr)
  544. break;
  545. if(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES){
  546. pes= ts->pids[pid]->u.pes_filter.opaque;
  547. st= pes->st;
  548. }else{
  549. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  550. pes = add_pes_stream(ts, pid, pcr_pid, stream_type);
  551. if (pes)
  552. st = new_pes_av_stream(pes, 0);
  553. }
  554. add_pid_to_pmt(ts, h->id, pid);
  555. if(st)
  556. av_program_add_stream_index(ts->stream, h->id, st->index);
  557. break;
  558. default:
  559. /* we ignore the other streams */
  560. break;
  561. }
  562. if (st) {
  563. if (language[0] != 0) {
  564. memcpy(st->language, language, 4);
  565. }
  566. if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
  567. st->codec->sub_id = (anc_page << 16) | comp_page;
  568. }
  569. }
  570. }
  571. /* all parameters are there */
  572. ts->stop_parse++;
  573. mpegts_close_filter(ts, filter);
  574. }
  575. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  576. {
  577. MpegTSContext *ts = filter->u.section_filter.opaque;
  578. SectionHeader h1, *h = &h1;
  579. const uint8_t *p, *p_end;
  580. int sid, pmt_pid;
  581. #ifdef DEBUG_SI
  582. av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
  583. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  584. #endif
  585. p_end = section + section_len - 4;
  586. p = section;
  587. if (parse_section_header(h, &p, p_end) < 0)
  588. return;
  589. if (h->tid != PAT_TID)
  590. return;
  591. clear_programs(ts);
  592. for(;;) {
  593. sid = get16(&p, p_end);
  594. if (sid < 0)
  595. break;
  596. pmt_pid = get16(&p, p_end) & 0x1fff;
  597. if (pmt_pid < 0)
  598. break;
  599. #ifdef DEBUG_SI
  600. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  601. #endif
  602. if (sid == 0x0000) {
  603. /* NIT info */
  604. } else {
  605. av_new_program(ts->stream, sid);
  606. ts->stop_parse--;
  607. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  608. add_pat_entry(ts, sid);
  609. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  610. add_pid_to_pmt(ts, sid, pmt_pid);
  611. }
  612. }
  613. /* not found */
  614. ts->stop_parse++;
  615. mpegts_close_filter(ts, filter);
  616. }
  617. static void mpegts_set_service(MpegTSContext *ts)
  618. {
  619. mpegts_open_section_filter(ts, PAT_PID,
  620. pat_cb, ts, 1);
  621. }
  622. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  623. {
  624. MpegTSContext *ts = filter->u.section_filter.opaque;
  625. SectionHeader h1, *h = &h1;
  626. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  627. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  628. char *name, *provider_name;
  629. #ifdef DEBUG_SI
  630. av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
  631. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  632. #endif
  633. p_end = section + section_len - 4;
  634. p = section;
  635. if (parse_section_header(h, &p, p_end) < 0)
  636. return;
  637. if (h->tid != SDT_TID)
  638. return;
  639. onid = get16(&p, p_end);
  640. if (onid < 0)
  641. return;
  642. val = get8(&p, p_end);
  643. if (val < 0)
  644. return;
  645. for(;;) {
  646. sid = get16(&p, p_end);
  647. if (sid < 0)
  648. break;
  649. val = get8(&p, p_end);
  650. if (val < 0)
  651. break;
  652. desc_list_len = get16(&p, p_end) & 0xfff;
  653. if (desc_list_len < 0)
  654. break;
  655. desc_list_end = p + desc_list_len;
  656. if (desc_list_end > p_end)
  657. break;
  658. for(;;) {
  659. desc_tag = get8(&p, desc_list_end);
  660. if (desc_tag < 0)
  661. break;
  662. desc_len = get8(&p, desc_list_end);
  663. desc_end = p + desc_len;
  664. if (desc_end > desc_list_end)
  665. break;
  666. #ifdef DEBUG_SI
  667. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  668. desc_tag, desc_len);
  669. #endif
  670. switch(desc_tag) {
  671. case 0x48:
  672. service_type = get8(&p, p_end);
  673. if (service_type < 0)
  674. break;
  675. provider_name = getstr8(&p, p_end);
  676. if (!provider_name)
  677. break;
  678. name = getstr8(&p, p_end);
  679. if (name) {
  680. AVProgram *program = av_new_program(ts->stream, sid);
  681. if(program)
  682. av_set_program_name(program, provider_name, name);
  683. }
  684. av_free(name);
  685. av_free(provider_name);
  686. break;
  687. default:
  688. break;
  689. }
  690. p = desc_end;
  691. }
  692. p = desc_list_end;
  693. }
  694. }
  695. /* scan services in a transport stream by looking at the SDT */
  696. static void mpegts_scan_sdt(MpegTSContext *ts)
  697. {
  698. mpegts_open_section_filter(ts, SDT_PID,
  699. sdt_cb, ts, 1);
  700. }
  701. static int64_t get_pts(const uint8_t *p)
  702. {
  703. int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  704. pts |= (AV_RB16(p + 1) >> 1) << 15;
  705. pts |= AV_RB16(p + 3) >> 1;
  706. return pts;
  707. }
  708. /* return non zero if a packet could be constructed */
  709. static void mpegts_push_data(MpegTSFilter *filter,
  710. const uint8_t *buf, int buf_size, int is_start)
  711. {
  712. PESContext *pes = filter->u.pes_filter.opaque;
  713. MpegTSContext *ts = pes->ts;
  714. const uint8_t *p;
  715. int len, code;
  716. if(!ts->pkt)
  717. return;
  718. if (is_start) {
  719. pes->state = MPEGTS_HEADER;
  720. pes->data_index = 0;
  721. }
  722. p = buf;
  723. while (buf_size > 0) {
  724. switch(pes->state) {
  725. case MPEGTS_HEADER:
  726. len = PES_START_SIZE - pes->data_index;
  727. if (len > buf_size)
  728. len = buf_size;
  729. memcpy(pes->header + pes->data_index, p, len);
  730. pes->data_index += len;
  731. p += len;
  732. buf_size -= len;
  733. if (pes->data_index == PES_START_SIZE) {
  734. /* we got all the PES or section header. We can now
  735. decide */
  736. #if 0
  737. av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
  738. #endif
  739. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  740. pes->header[2] == 0x01) {
  741. /* it must be an mpeg2 PES stream */
  742. code = pes->header[3] | 0x100;
  743. if (!((code >= 0x1c0 && code <= 0x1df) ||
  744. (code >= 0x1e0 && code <= 0x1ef) ||
  745. (code == 0x1bd) || (code == 0x1fd)))
  746. goto skip;
  747. if (!pes->st) {
  748. /* allocate stream */
  749. new_pes_av_stream(pes, code);
  750. }
  751. pes->state = MPEGTS_PESHEADER_FILL;
  752. pes->total_size = AV_RB16(pes->header + 4);
  753. /* NOTE: a zero total size means the PES size is
  754. unbounded */
  755. if (pes->total_size)
  756. pes->total_size += 6;
  757. pes->pes_header_size = pes->header[8] + 9;
  758. } else {
  759. /* otherwise, it should be a table */
  760. /* skip packet */
  761. skip:
  762. pes->state = MPEGTS_SKIP;
  763. continue;
  764. }
  765. }
  766. break;
  767. /**********************************************/
  768. /* PES packing parsing */
  769. case MPEGTS_PESHEADER_FILL:
  770. len = pes->pes_header_size - pes->data_index;
  771. if (len > buf_size)
  772. len = buf_size;
  773. memcpy(pes->header + pes->data_index, p, len);
  774. pes->data_index += len;
  775. p += len;
  776. buf_size -= len;
  777. if (pes->data_index == pes->pes_header_size) {
  778. const uint8_t *r;
  779. unsigned int flags;
  780. flags = pes->header[7];
  781. r = pes->header + 9;
  782. pes->pts = AV_NOPTS_VALUE;
  783. pes->dts = AV_NOPTS_VALUE;
  784. if ((flags & 0xc0) == 0x80) {
  785. pes->pts = get_pts(r);
  786. r += 5;
  787. } else if ((flags & 0xc0) == 0xc0) {
  788. pes->pts = get_pts(r);
  789. r += 5;
  790. pes->dts = get_pts(r);
  791. r += 5;
  792. }
  793. /* we got the full header. We parse it and get the payload */
  794. pes->state = MPEGTS_PAYLOAD;
  795. }
  796. break;
  797. case MPEGTS_PAYLOAD:
  798. if (pes->total_size) {
  799. len = pes->total_size - pes->data_index;
  800. if (len > buf_size)
  801. len = buf_size;
  802. } else {
  803. len = buf_size;
  804. }
  805. if (len > 0) {
  806. AVPacket *pkt = ts->pkt;
  807. if (pes->st && av_new_packet(pkt, len) == 0) {
  808. memcpy(pkt->data, p, len);
  809. pkt->stream_index = pes->st->index;
  810. pkt->pts = pes->pts;
  811. pkt->dts = pes->dts;
  812. /* reset pts values */
  813. pes->pts = AV_NOPTS_VALUE;
  814. pes->dts = AV_NOPTS_VALUE;
  815. ts->stop_parse = 1;
  816. return;
  817. }
  818. }
  819. buf_size = 0;
  820. break;
  821. case MPEGTS_SKIP:
  822. buf_size = 0;
  823. break;
  824. }
  825. }
  826. }
  827. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
  828. {
  829. AVStream *st;
  830. int codec_type, codec_id;
  831. switch(pes->stream_type){
  832. case STREAM_TYPE_AUDIO_MPEG1:
  833. case STREAM_TYPE_AUDIO_MPEG2:
  834. codec_type = CODEC_TYPE_AUDIO;
  835. codec_id = CODEC_ID_MP3;
  836. break;
  837. case STREAM_TYPE_VIDEO_MPEG1:
  838. case STREAM_TYPE_VIDEO_MPEG2:
  839. codec_type = CODEC_TYPE_VIDEO;
  840. codec_id = CODEC_ID_MPEG2VIDEO;
  841. break;
  842. case STREAM_TYPE_VIDEO_MPEG4:
  843. codec_type = CODEC_TYPE_VIDEO;
  844. codec_id = CODEC_ID_MPEG4;
  845. break;
  846. case STREAM_TYPE_VIDEO_H264:
  847. codec_type = CODEC_TYPE_VIDEO;
  848. codec_id = CODEC_ID_H264;
  849. break;
  850. case STREAM_TYPE_VIDEO_VC1:
  851. codec_type = CODEC_TYPE_VIDEO;
  852. codec_id = CODEC_ID_VC1;
  853. break;
  854. case STREAM_TYPE_AUDIO_AAC:
  855. codec_type = CODEC_TYPE_AUDIO;
  856. codec_id = CODEC_ID_AAC;
  857. break;
  858. case STREAM_TYPE_AUDIO_AC3:
  859. codec_type = CODEC_TYPE_AUDIO;
  860. codec_id = CODEC_ID_AC3;
  861. break;
  862. case STREAM_TYPE_AUDIO_DTS:
  863. case STREAM_TYPE_AUDIO_HDMV_DTS:
  864. codec_type = CODEC_TYPE_AUDIO;
  865. codec_id = CODEC_ID_DTS;
  866. break;
  867. case STREAM_TYPE_SUBTITLE_DVB:
  868. codec_type = CODEC_TYPE_SUBTITLE;
  869. codec_id = CODEC_ID_DVB_SUBTITLE;
  870. break;
  871. default:
  872. if (code >= 0x1c0 && code <= 0x1df) {
  873. codec_type = CODEC_TYPE_AUDIO;
  874. codec_id = CODEC_ID_MP2;
  875. } else if (code == 0x1bd) {
  876. codec_type = CODEC_TYPE_AUDIO;
  877. codec_id = CODEC_ID_AC3;
  878. } else {
  879. codec_type = CODEC_TYPE_VIDEO;
  880. codec_id = CODEC_ID_MPEG1VIDEO;
  881. }
  882. break;
  883. }
  884. st = av_new_stream(pes->stream, pes->pid);
  885. if (st) {
  886. av_set_pts_info(st, 33, 1, 90000);
  887. st->priv_data = pes;
  888. st->codec->codec_type = codec_type;
  889. st->codec->codec_id = codec_id;
  890. st->need_parsing = AVSTREAM_PARSE_FULL;
  891. pes->st = st;
  892. }
  893. return st;
  894. }
  895. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
  896. {
  897. MpegTSFilter *tss;
  898. PESContext *pes;
  899. /* if no pid found, then add a pid context */
  900. pes = av_mallocz(sizeof(PESContext));
  901. if (!pes)
  902. return 0;
  903. pes->ts = ts;
  904. pes->stream = ts->stream;
  905. pes->pid = pid;
  906. pes->pcr_pid = pcr_pid;
  907. pes->stream_type = stream_type;
  908. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  909. if (!tss) {
  910. av_free(pes);
  911. return 0;
  912. }
  913. return pes;
  914. }
  915. /* handle one TS packet */
  916. static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
  917. {
  918. AVFormatContext *s = ts->stream;
  919. MpegTSFilter *tss;
  920. int len, pid, cc, cc_ok, afc, is_start;
  921. const uint8_t *p, *p_end;
  922. pid = AV_RB16(packet + 1) & 0x1fff;
  923. if(pid && discard_pid(ts, pid))
  924. return;
  925. is_start = packet[1] & 0x40;
  926. tss = ts->pids[pid];
  927. if (ts->auto_guess && tss == NULL && is_start) {
  928. add_pes_stream(ts, pid, -1, 0);
  929. tss = ts->pids[pid];
  930. }
  931. if (!tss)
  932. return;
  933. /* continuity check (currently not used) */
  934. cc = (packet[3] & 0xf);
  935. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  936. tss->last_cc = cc;
  937. /* skip adaptation field */
  938. afc = (packet[3] >> 4) & 3;
  939. p = packet + 4;
  940. if (afc == 0) /* reserved value */
  941. return;
  942. if (afc == 2) /* adaptation field only */
  943. return;
  944. if (afc == 3) {
  945. /* skip adapation field */
  946. p += p[0] + 1;
  947. }
  948. /* if past the end of packet, ignore */
  949. p_end = packet + TS_PACKET_SIZE;
  950. if (p >= p_end)
  951. return;
  952. ts->pos47= url_ftell(ts->stream->pb) % ts->raw_packet_size;
  953. if (tss->type == MPEGTS_SECTION) {
  954. if (is_start) {
  955. /* pointer field present */
  956. len = *p++;
  957. if (p + len > p_end)
  958. return;
  959. if (len && cc_ok) {
  960. /* write remaining section bytes */
  961. write_section_data(s, tss,
  962. p, len, 0);
  963. /* check whether filter has been closed */
  964. if (!ts->pids[pid])
  965. return;
  966. }
  967. p += len;
  968. if (p < p_end) {
  969. write_section_data(s, tss,
  970. p, p_end - p, 1);
  971. }
  972. } else {
  973. if (cc_ok) {
  974. write_section_data(s, tss,
  975. p, p_end - p, 0);
  976. }
  977. }
  978. } else {
  979. tss->u.pes_filter.pes_cb(tss,
  980. p, p_end - p, is_start);
  981. }
  982. }
  983. /* XXX: try to find a better synchro over several packets (use
  984. get_packet_size() ?) */
  985. static int mpegts_resync(ByteIOContext *pb)
  986. {
  987. int c, i;
  988. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  989. c = url_fgetc(pb);
  990. if (c < 0)
  991. return -1;
  992. if (c == 0x47) {
  993. url_fseek(pb, -1, SEEK_CUR);
  994. return 0;
  995. }
  996. }
  997. /* no sync found */
  998. return -1;
  999. }
  1000. /* return -1 if error or EOF. Return 0 if OK. */
  1001. static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
  1002. {
  1003. int skip, len;
  1004. for(;;) {
  1005. len = get_buffer(pb, buf, TS_PACKET_SIZE);
  1006. if (len != TS_PACKET_SIZE)
  1007. return AVERROR(EIO);
  1008. /* check paquet sync byte */
  1009. if (buf[0] != 0x47) {
  1010. /* find a new packet start */
  1011. url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1012. if (mpegts_resync(pb) < 0)
  1013. return AVERROR_INVALIDDATA;
  1014. else
  1015. continue;
  1016. } else {
  1017. skip = raw_packet_size - TS_PACKET_SIZE;
  1018. if (skip > 0)
  1019. url_fskip(pb, skip);
  1020. break;
  1021. }
  1022. }
  1023. return 0;
  1024. }
  1025. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1026. {
  1027. AVFormatContext *s = ts->stream;
  1028. ByteIOContext *pb = s->pb;
  1029. uint8_t packet[TS_PACKET_SIZE];
  1030. int packet_num, ret;
  1031. ts->stop_parse = 0;
  1032. packet_num = 0;
  1033. for(;;) {
  1034. if (ts->stop_parse>0)
  1035. break;
  1036. packet_num++;
  1037. if (nb_packets != 0 && packet_num >= nb_packets)
  1038. break;
  1039. ret = read_packet(pb, packet, ts->raw_packet_size);
  1040. if (ret != 0)
  1041. return ret;
  1042. handle_packet(ts, packet);
  1043. }
  1044. return 0;
  1045. }
  1046. static int mpegts_probe(AVProbeData *p)
  1047. {
  1048. #if 1
  1049. const int size= p->buf_size;
  1050. int score, fec_score, dvhs_score;
  1051. #define CHECK_COUNT 10
  1052. if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
  1053. return -1;
  1054. score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
  1055. dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE *CHECK_COUNT, TS_DVHS_PACKET_SIZE, NULL);
  1056. fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
  1057. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  1058. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  1059. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1060. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1061. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1062. else return -1;
  1063. #else
  1064. /* only use the extension for safer guess */
  1065. if (match_ext(p->filename, "ts"))
  1066. return AVPROBE_SCORE_MAX;
  1067. else
  1068. return 0;
  1069. #endif
  1070. }
  1071. /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
  1072. (-1) if not available */
  1073. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1074. const uint8_t *packet)
  1075. {
  1076. int afc, len, flags;
  1077. const uint8_t *p;
  1078. unsigned int v;
  1079. afc = (packet[3] >> 4) & 3;
  1080. if (afc <= 1)
  1081. return -1;
  1082. p = packet + 4;
  1083. len = p[0];
  1084. p++;
  1085. if (len == 0)
  1086. return -1;
  1087. flags = *p++;
  1088. len--;
  1089. if (!(flags & 0x10))
  1090. return -1;
  1091. if (len < 6)
  1092. return -1;
  1093. v = AV_RB32(p);
  1094. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1095. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1096. return 0;
  1097. }
  1098. static int mpegts_read_header(AVFormatContext *s,
  1099. AVFormatParameters *ap)
  1100. {
  1101. MpegTSContext *ts = s->priv_data;
  1102. ByteIOContext *pb = s->pb;
  1103. uint8_t buf[1024];
  1104. int len;
  1105. int64_t pos;
  1106. if (ap) {
  1107. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1108. if(ap->mpeg2ts_raw){
  1109. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1110. return -1;
  1111. }
  1112. }
  1113. /* read the first 1024 bytes to get packet size */
  1114. pos = url_ftell(pb);
  1115. len = get_buffer(pb, buf, sizeof(buf));
  1116. if (len != sizeof(buf))
  1117. goto fail;
  1118. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1119. if (ts->raw_packet_size <= 0)
  1120. goto fail;
  1121. ts->stream = s;
  1122. ts->auto_guess = 0;
  1123. if (s->iformat == &mpegts_demuxer) {
  1124. /* normal demux */
  1125. /* first do a scaning to get all the services */
  1126. url_fseek(pb, pos, SEEK_SET);
  1127. mpegts_scan_sdt(ts);
  1128. mpegts_set_service(ts);
  1129. handle_packets(ts, s->probesize);
  1130. /* if could not find service, enable auto_guess */
  1131. ts->auto_guess = 1;
  1132. #ifdef DEBUG_SI
  1133. av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
  1134. #endif
  1135. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1136. } else {
  1137. AVStream *st;
  1138. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1139. int64_t pcrs[2], pcr_h;
  1140. int packet_count[2];
  1141. uint8_t packet[TS_PACKET_SIZE];
  1142. /* only read packets */
  1143. st = av_new_stream(s, 0);
  1144. if (!st)
  1145. goto fail;
  1146. av_set_pts_info(st, 60, 1, 27000000);
  1147. st->codec->codec_type = CODEC_TYPE_DATA;
  1148. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1149. /* we iterate until we find two PCRs to estimate the bitrate */
  1150. pcr_pid = -1;
  1151. nb_pcrs = 0;
  1152. nb_packets = 0;
  1153. for(;;) {
  1154. ret = read_packet(s->pb, packet, ts->raw_packet_size);
  1155. if (ret < 0)
  1156. return -1;
  1157. pid = AV_RB16(packet + 1) & 0x1fff;
  1158. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1159. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1160. pcr_pid = pid;
  1161. packet_count[nb_pcrs] = nb_packets;
  1162. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1163. nb_pcrs++;
  1164. if (nb_pcrs >= 2)
  1165. break;
  1166. }
  1167. nb_packets++;
  1168. }
  1169. /* NOTE1: the bitrate is computed without the FEC */
  1170. /* NOTE2: it is only the bitrate of the start of the stream */
  1171. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1172. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1173. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1174. st->codec->bit_rate = s->bit_rate;
  1175. st->start_time = ts->cur_pcr;
  1176. #if 0
  1177. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1178. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1179. #endif
  1180. }
  1181. url_fseek(pb, pos, SEEK_SET);
  1182. return 0;
  1183. fail:
  1184. return -1;
  1185. }
  1186. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1187. static int mpegts_raw_read_packet(AVFormatContext *s,
  1188. AVPacket *pkt)
  1189. {
  1190. MpegTSContext *ts = s->priv_data;
  1191. int ret, i;
  1192. int64_t pcr_h, next_pcr_h, pos;
  1193. int pcr_l, next_pcr_l;
  1194. uint8_t pcr_buf[12];
  1195. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1196. return AVERROR(ENOMEM);
  1197. pkt->pos= url_ftell(s->pb);
  1198. ret = read_packet(s->pb, pkt->data, ts->raw_packet_size);
  1199. if (ret < 0) {
  1200. av_free_packet(pkt);
  1201. return ret;
  1202. }
  1203. if (ts->mpeg2ts_compute_pcr) {
  1204. /* compute exact PCR for each packet */
  1205. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1206. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1207. pos = url_ftell(s->pb);
  1208. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1209. url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1210. get_buffer(s->pb, pcr_buf, 12);
  1211. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1212. /* XXX: not precise enough */
  1213. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1214. (i + 1);
  1215. break;
  1216. }
  1217. }
  1218. url_fseek(s->pb, pos, SEEK_SET);
  1219. /* no next PCR found: we use previous increment */
  1220. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1221. }
  1222. pkt->pts = ts->cur_pcr;
  1223. pkt->duration = ts->pcr_incr;
  1224. ts->cur_pcr += ts->pcr_incr;
  1225. }
  1226. pkt->stream_index = 0;
  1227. return 0;
  1228. }
  1229. static int mpegts_read_packet(AVFormatContext *s,
  1230. AVPacket *pkt)
  1231. {
  1232. MpegTSContext *ts = s->priv_data;
  1233. ts->pkt = pkt;
  1234. return handle_packets(ts, 0);
  1235. }
  1236. static int mpegts_read_close(AVFormatContext *s)
  1237. {
  1238. MpegTSContext *ts = s->priv_data;
  1239. int i;
  1240. clear_programs(ts);
  1241. for(i=0;i<NB_PID_MAX;i++)
  1242. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1243. return 0;
  1244. }
  1245. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1246. int64_t *ppos, int64_t pos_limit)
  1247. {
  1248. MpegTSContext *ts = s->priv_data;
  1249. int64_t pos, timestamp;
  1250. uint8_t buf[TS_PACKET_SIZE];
  1251. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1252. const int find_next= 1;
  1253. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1254. if (find_next) {
  1255. for(;;) {
  1256. url_fseek(s->pb, pos, SEEK_SET);
  1257. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1258. return AV_NOPTS_VALUE;
  1259. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1260. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1261. break;
  1262. }
  1263. pos += ts->raw_packet_size;
  1264. }
  1265. } else {
  1266. for(;;) {
  1267. pos -= ts->raw_packet_size;
  1268. if (pos < 0)
  1269. return AV_NOPTS_VALUE;
  1270. url_fseek(s->pb, pos, SEEK_SET);
  1271. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1272. return AV_NOPTS_VALUE;
  1273. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1274. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1275. break;
  1276. }
  1277. }
  1278. }
  1279. *ppos = pos;
  1280. return timestamp;
  1281. }
  1282. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1283. MpegTSContext *ts = s->priv_data;
  1284. uint8_t buf[TS_PACKET_SIZE];
  1285. int64_t pos;
  1286. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1287. return -1;
  1288. pos= url_ftell(s->pb);
  1289. for(;;) {
  1290. url_fseek(s->pb, pos, SEEK_SET);
  1291. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1292. return -1;
  1293. // pid = AV_RB16(buf + 1) & 0x1fff;
  1294. if(buf[1] & 0x40) break;
  1295. pos += ts->raw_packet_size;
  1296. }
  1297. url_fseek(s->pb, pos, SEEK_SET);
  1298. return 0;
  1299. }
  1300. /**************************************************************/
  1301. /* parsing functions - called from other demuxers such as RTP */
  1302. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1303. {
  1304. MpegTSContext *ts;
  1305. ts = av_mallocz(sizeof(MpegTSContext));
  1306. if (!ts)
  1307. return NULL;
  1308. /* no stream case, currently used by RTP */
  1309. ts->raw_packet_size = TS_PACKET_SIZE;
  1310. ts->stream = s;
  1311. ts->auto_guess = 1;
  1312. return ts;
  1313. }
  1314. /* return the consumed length if a packet was output, or -1 if no
  1315. packet is output */
  1316. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1317. const uint8_t *buf, int len)
  1318. {
  1319. int len1;
  1320. len1 = len;
  1321. ts->pkt = pkt;
  1322. ts->stop_parse = 0;
  1323. for(;;) {
  1324. if (ts->stop_parse>0)
  1325. break;
  1326. if (len < TS_PACKET_SIZE)
  1327. return -1;
  1328. if (buf[0] != 0x47) {
  1329. buf++;
  1330. len--;
  1331. } else {
  1332. handle_packet(ts, buf);
  1333. buf += TS_PACKET_SIZE;
  1334. len -= TS_PACKET_SIZE;
  1335. }
  1336. }
  1337. return len1 - len;
  1338. }
  1339. void mpegts_parse_close(MpegTSContext *ts)
  1340. {
  1341. int i;
  1342. for(i=0;i<NB_PID_MAX;i++)
  1343. av_free(ts->pids[i]);
  1344. av_free(ts);
  1345. }
  1346. AVInputFormat mpegts_demuxer = {
  1347. "mpegts",
  1348. NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
  1349. sizeof(MpegTSContext),
  1350. mpegts_probe,
  1351. mpegts_read_header,
  1352. mpegts_read_packet,
  1353. mpegts_read_close,
  1354. read_seek,
  1355. mpegts_get_pcr,
  1356. .flags = AVFMT_SHOW_IDS,
  1357. };
  1358. AVInputFormat mpegtsraw_demuxer = {
  1359. "mpegtsraw",
  1360. NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
  1361. sizeof(MpegTSContext),
  1362. NULL,
  1363. mpegts_read_header,
  1364. mpegts_raw_read_packet,
  1365. mpegts_read_close,
  1366. read_seek,
  1367. mpegts_get_pcr,
  1368. .flags = AVFMT_SHOW_IDS,
  1369. };