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.

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