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.

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