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. //#define DEBUG_SI
  25. //#define DEBUG_SEEK
  26. /* 1.0 second at 24Mbit/s */
  27. #define MAX_SCAN_PACKETS 32000
  28. /* maximum size in which we look for synchronisation if
  29. synchronisation is lost */
  30. #define MAX_RESYNC_SIZE 4096
  31. #define REGISTRATION_DESCRIPTOR 5
  32. typedef struct PESContext PESContext;
  33. static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type);
  34. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
  35. extern void av_set_program_name(AVProgram *program, char *provider_name, char *name);
  36. extern void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx);
  37. enum MpegTSFilterType {
  38. MPEGTS_PES,
  39. MPEGTS_SECTION,
  40. };
  41. typedef struct MpegTSFilter MpegTSFilter;
  42. typedef void PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start);
  43. typedef struct MpegTSPESFilter {
  44. PESCallback *pes_cb;
  45. void *opaque;
  46. } MpegTSPESFilter;
  47. typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
  48. typedef void SetServiceCallback(void *opaque, int ret);
  49. typedef struct MpegTSSectionFilter {
  50. int section_index;
  51. int section_h_size;
  52. uint8_t *section_buf;
  53. 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. typedef struct {
  69. unsigned int id; //program id/service id
  70. unsigned int nb_pids;
  71. unsigned int pids[MAX_PIDS_PER_PROGRAM];
  72. } Program_t;
  73. struct MpegTSContext {
  74. /* user data */
  75. AVFormatContext *stream;
  76. /** raw packet size, including FEC if present */
  77. int raw_packet_size;
  78. int pos47;
  79. /** if true, all pids are analyzed to find streams */
  80. int auto_guess;
  81. /** compute exact PCR for each transport stream packet */
  82. int mpeg2ts_compute_pcr;
  83. int64_t cur_pcr; /**< used to estimate the exact PCR */
  84. int pcr_incr; /**< used to estimate the exact PCR */
  85. /* data needed to handle file based ts */
  86. /** stop parsing loop */
  87. int stop_parse;
  88. /** packet containing Audio/Video data */
  89. AVPacket *pkt;
  90. /******************************************/
  91. /* private mpegts data */
  92. /* scan context */
  93. /** structure to keep track of Program->pids mapping */
  94. unsigned int nb_prg;
  95. Program_t *prg;
  96. /** filters for various streams specified by PMT + for the PAT and PMT */
  97. MpegTSFilter *pids[NB_PID_MAX];
  98. };
  99. /* TS stream handling */
  100. enum MpegTSState {
  101. MPEGTS_HEADER = 0,
  102. MPEGTS_PESHEADER_FILL,
  103. MPEGTS_PAYLOAD,
  104. MPEGTS_SKIP,
  105. };
  106. /* enough for PES header + length */
  107. #define PES_START_SIZE 9
  108. #define MAX_PES_HEADER_SIZE (9 + 255)
  109. struct PESContext {
  110. int pid;
  111. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  112. int stream_type;
  113. MpegTSContext *ts;
  114. AVFormatContext *stream;
  115. AVStream *st;
  116. enum MpegTSState state;
  117. /* used to get the format */
  118. int data_index;
  119. int total_size;
  120. int pes_header_size;
  121. int64_t pts, dts;
  122. uint8_t header[MAX_PES_HEADER_SIZE];
  123. };
  124. extern AVInputFormat mpegts_demuxer;
  125. static void clear_program(MpegTSContext *ts, unsigned int programid)
  126. {
  127. int i;
  128. for(i=0; i<ts->nb_prg; i++)
  129. if(ts->prg[i].id == programid)
  130. ts->prg[i].nb_pids = 0;
  131. }
  132. static void clear_programs(MpegTSContext *ts)
  133. {
  134. av_freep(&ts->prg);
  135. ts->nb_prg=0;
  136. }
  137. static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
  138. {
  139. Program_t *p;
  140. void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(Program_t));
  141. if(!tmp)
  142. return;
  143. ts->prg = tmp;
  144. p = &ts->prg[ts->nb_prg];
  145. p->id = programid;
  146. p->nb_pids = 0;
  147. ts->nb_prg++;
  148. }
  149. static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
  150. {
  151. int i;
  152. Program_t *p = NULL;
  153. for(i=0; i<ts->nb_prg; i++) {
  154. if(ts->prg[i].id == programid) {
  155. p = &ts->prg[i];
  156. break;
  157. }
  158. }
  159. if(!p)
  160. return;
  161. if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
  162. return;
  163. p->pids[p->nb_pids++] = pid;
  164. }
  165. /**
  166. * \brief discard_pid() decides if the pid is to be discarded according
  167. * to caller's programs selection
  168. * \param ts : - TS context
  169. * \param pid : - pid
  170. * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
  171. * 0 otherwise
  172. */
  173. static int discard_pid(MpegTSContext *ts, unsigned int pid)
  174. {
  175. int i, j, k;
  176. int used = 0, discarded = 0;
  177. Program_t *p;
  178. for(i=0; i<ts->nb_prg; i++) {
  179. p = &ts->prg[i];
  180. for(j=0; j<p->nb_pids; j++) {
  181. if(p->pids[j] != pid)
  182. continue;
  183. //is program with id p->id set to be discarded?
  184. for(k=0; k<ts->stream->nb_programs; k++) {
  185. if(ts->stream->programs[k]->id == p->id) {
  186. if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
  187. discarded++;
  188. else
  189. used++;
  190. }
  191. }
  192. }
  193. }
  194. return !used && discarded;
  195. }
  196. /**
  197. * Assembles PES packets out of TS packets, and then calls the "section_cb"
  198. * function when they are complete.
  199. */
  200. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  201. const uint8_t *buf, int buf_size, int is_start)
  202. {
  203. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  204. int len;
  205. if (is_start) {
  206. memcpy(tss->section_buf, buf, buf_size);
  207. tss->section_index = buf_size;
  208. tss->section_h_size = -1;
  209. tss->end_of_section_reached = 0;
  210. } else {
  211. if (tss->end_of_section_reached)
  212. return;
  213. len = 4096 - tss->section_index;
  214. if (buf_size < len)
  215. len = buf_size;
  216. memcpy(tss->section_buf + tss->section_index, buf, len);
  217. tss->section_index += len;
  218. }
  219. /* compute section length if possible */
  220. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  221. len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
  222. if (len > 4096)
  223. return;
  224. tss->section_h_size = len;
  225. }
  226. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  227. tss->end_of_section_reached = 1;
  228. if (!tss->check_crc ||
  229. av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
  230. tss->section_buf, tss->section_h_size) == 0)
  231. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  232. }
  233. }
  234. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  235. SectionCallback *section_cb, void *opaque,
  236. int check_crc)
  237. {
  238. MpegTSFilter *filter;
  239. MpegTSSectionFilter *sec;
  240. #ifdef DEBUG_SI
  241. av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
  242. #endif
  243. if (pid >= NB_PID_MAX || ts->pids[pid])
  244. return NULL;
  245. filter = av_mallocz(sizeof(MpegTSFilter));
  246. if (!filter)
  247. return NULL;
  248. ts->pids[pid] = filter;
  249. filter->type = MPEGTS_SECTION;
  250. filter->pid = pid;
  251. filter->last_cc = -1;
  252. sec = &filter->u.section_filter;
  253. sec->section_cb = section_cb;
  254. sec->opaque = opaque;
  255. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  256. sec->check_crc = check_crc;
  257. if (!sec->section_buf) {
  258. av_free(filter);
  259. return NULL;
  260. }
  261. return filter;
  262. }
  263. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  264. PESCallback *pes_cb,
  265. void *opaque)
  266. {
  267. MpegTSFilter *filter;
  268. MpegTSPESFilter *pes;
  269. if (pid >= NB_PID_MAX || ts->pids[pid])
  270. return NULL;
  271. filter = av_mallocz(sizeof(MpegTSFilter));
  272. if (!filter)
  273. return NULL;
  274. ts->pids[pid] = filter;
  275. filter->type = MPEGTS_PES;
  276. filter->pid = pid;
  277. filter->last_cc = -1;
  278. pes = &filter->u.pes_filter;
  279. pes->pes_cb = pes_cb;
  280. pes->opaque = opaque;
  281. return filter;
  282. }
  283. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  284. {
  285. int pid;
  286. pid = filter->pid;
  287. if (filter->type == MPEGTS_SECTION)
  288. av_freep(&filter->u.section_filter.section_buf);
  289. 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->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. int codec_type, codec_id;
  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 90 kHz PCR and the extension for the 27 MHz 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. };