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.

1542 lines
44KB

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