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.

1553 lines
45KB

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