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.

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