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.

1551 lines
45KB

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