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.

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