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.

1537 lines
44KB

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