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.

1549 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 "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 mpegts_set_service(MpegTSContext *ts)
  623. {
  624. mpegts_open_section_filter(ts, PAT_PID,
  625. pat_cb, ts, 1);
  626. }
  627. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  628. {
  629. MpegTSContext *ts = filter->u.section_filter.opaque;
  630. SectionHeader h1, *h = &h1;
  631. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  632. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  633. char *name, *provider_name;
  634. #ifdef DEBUG
  635. dprintf(ts->stream, "SDT:\n");
  636. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  637. #endif
  638. p_end = section + section_len - 4;
  639. p = section;
  640. if (parse_section_header(h, &p, p_end) < 0)
  641. return;
  642. if (h->tid != SDT_TID)
  643. return;
  644. onid = get16(&p, p_end);
  645. if (onid < 0)
  646. return;
  647. val = get8(&p, p_end);
  648. if (val < 0)
  649. return;
  650. for(;;) {
  651. sid = get16(&p, p_end);
  652. if (sid < 0)
  653. break;
  654. val = get8(&p, p_end);
  655. if (val < 0)
  656. break;
  657. desc_list_len = get16(&p, p_end) & 0xfff;
  658. if (desc_list_len < 0)
  659. break;
  660. desc_list_end = p + desc_list_len;
  661. if (desc_list_end > p_end)
  662. break;
  663. for(;;) {
  664. desc_tag = get8(&p, desc_list_end);
  665. if (desc_tag < 0)
  666. break;
  667. desc_len = get8(&p, desc_list_end);
  668. desc_end = p + desc_len;
  669. if (desc_end > desc_list_end)
  670. break;
  671. dprintf(ts->stream, "tag: 0x%02x len=%d\n",
  672. desc_tag, desc_len);
  673. switch(desc_tag) {
  674. case 0x48:
  675. service_type = get8(&p, p_end);
  676. if (service_type < 0)
  677. break;
  678. provider_name = getstr8(&p, p_end);
  679. if (!provider_name)
  680. break;
  681. name = getstr8(&p, p_end);
  682. if (name) {
  683. AVProgram *program = av_new_program(ts->stream, sid);
  684. if(program) {
  685. av_metadata_set(&program->metadata, "name", name);
  686. av_metadata_set(&program->metadata, "provider_name", provider_name);
  687. }
  688. }
  689. av_free(name);
  690. av_free(provider_name);
  691. break;
  692. default:
  693. break;
  694. }
  695. p = desc_end;
  696. }
  697. p = desc_list_end;
  698. }
  699. }
  700. /* scan services in a transport stream by looking at the SDT */
  701. static void mpegts_scan_sdt(MpegTSContext *ts)
  702. {
  703. mpegts_open_section_filter(ts, SDT_PID,
  704. sdt_cb, ts, 1);
  705. }
  706. static int64_t get_pts(const uint8_t *p)
  707. {
  708. int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  709. pts |= (AV_RB16(p + 1) >> 1) << 15;
  710. pts |= AV_RB16(p + 3) >> 1;
  711. return pts;
  712. }
  713. /* return non zero if a packet could be constructed */
  714. static void mpegts_push_data(MpegTSFilter *filter,
  715. const uint8_t *buf, int buf_size, int is_start,
  716. int64_t pos)
  717. {
  718. PESContext *pes = filter->u.pes_filter.opaque;
  719. MpegTSContext *ts = pes->ts;
  720. const uint8_t *p;
  721. int len, code;
  722. if(!ts->pkt)
  723. return;
  724. if (is_start) {
  725. pes->state = MPEGTS_HEADER;
  726. pes->data_index = 0;
  727. pes->ts_packet_pos = pos;
  728. }
  729. p = buf;
  730. while (buf_size > 0) {
  731. switch(pes->state) {
  732. case MPEGTS_HEADER:
  733. len = PES_START_SIZE - pes->data_index;
  734. if (len > buf_size)
  735. len = buf_size;
  736. memcpy(pes->header + pes->data_index, p, len);
  737. pes->data_index += len;
  738. p += len;
  739. buf_size -= len;
  740. if (pes->data_index == PES_START_SIZE) {
  741. /* we got all the PES or section header. We can now
  742. decide */
  743. #if 0
  744. av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
  745. #endif
  746. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  747. pes->header[2] == 0x01) {
  748. /* it must be an mpeg2 PES stream */
  749. code = pes->header[3] | 0x100;
  750. if (!pes->st ||
  751. !((code >= 0x1c0 && code <= 0x1df) ||
  752. (code >= 0x1e0 && code <= 0x1ef) ||
  753. (code == 0x1bd) || (code == 0x1fd)))
  754. goto skip;
  755. pes->state = MPEGTS_PESHEADER_FILL;
  756. pes->total_size = AV_RB16(pes->header + 4);
  757. /* NOTE: a zero total size means the PES size is
  758. unbounded */
  759. if (pes->total_size)
  760. pes->total_size += 6;
  761. pes->pes_header_size = pes->header[8] + 9;
  762. } else {
  763. /* otherwise, it should be a table */
  764. /* skip packet */
  765. skip:
  766. pes->state = MPEGTS_SKIP;
  767. continue;
  768. }
  769. }
  770. break;
  771. /**********************************************/
  772. /* PES packing parsing */
  773. case MPEGTS_PESHEADER_FILL:
  774. len = pes->pes_header_size - pes->data_index;
  775. if (len > buf_size)
  776. len = buf_size;
  777. memcpy(pes->header + pes->data_index, p, len);
  778. pes->data_index += len;
  779. p += len;
  780. buf_size -= len;
  781. if (pes->data_index == pes->pes_header_size) {
  782. const uint8_t *r;
  783. unsigned int flags;
  784. flags = pes->header[7];
  785. r = pes->header + 9;
  786. pes->pts = AV_NOPTS_VALUE;
  787. pes->dts = AV_NOPTS_VALUE;
  788. if ((flags & 0xc0) == 0x80) {
  789. pes->dts = pes->pts = get_pts(r);
  790. r += 5;
  791. } else if ((flags & 0xc0) == 0xc0) {
  792. pes->pts = get_pts(r);
  793. r += 5;
  794. pes->dts = get_pts(r);
  795. r += 5;
  796. }
  797. /* we got the full header. We parse it and get the payload */
  798. pes->state = MPEGTS_PAYLOAD;
  799. }
  800. break;
  801. case MPEGTS_PAYLOAD:
  802. if (pes->total_size) {
  803. len = pes->total_size - pes->data_index;
  804. if (len > buf_size)
  805. len = buf_size;
  806. } else {
  807. len = buf_size;
  808. }
  809. if (len > 0) {
  810. AVPacket *pkt = ts->pkt;
  811. if (pes->st && av_new_packet(pkt, len) == 0) {
  812. memcpy(pkt->data, p, len);
  813. pkt->stream_index = pes->st->index;
  814. pkt->pts = pes->pts;
  815. pkt->dts = pes->dts;
  816. /* store position of first TS packet of this PES packet */
  817. pkt->pos = pes->ts_packet_pos;
  818. /* reset pts values */
  819. pes->pts = AV_NOPTS_VALUE;
  820. pes->dts = AV_NOPTS_VALUE;
  821. ts->stop_parse = 1;
  822. return;
  823. }
  824. }
  825. buf_size = 0;
  826. break;
  827. case MPEGTS_SKIP:
  828. buf_size = 0;
  829. break;
  830. }
  831. }
  832. }
  833. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
  834. {
  835. AVStream *st;
  836. enum CodecID codec_id;
  837. enum CodecType codec_type;
  838. switch(pes->stream_type){
  839. case STREAM_TYPE_AUDIO_MPEG1:
  840. case STREAM_TYPE_AUDIO_MPEG2:
  841. codec_type = CODEC_TYPE_AUDIO;
  842. codec_id = CODEC_ID_MP3;
  843. break;
  844. case STREAM_TYPE_VIDEO_MPEG1:
  845. case STREAM_TYPE_VIDEO_MPEG2:
  846. codec_type = CODEC_TYPE_VIDEO;
  847. codec_id = CODEC_ID_MPEG2VIDEO;
  848. break;
  849. case STREAM_TYPE_VIDEO_MPEG4:
  850. codec_type = CODEC_TYPE_VIDEO;
  851. codec_id = CODEC_ID_MPEG4;
  852. break;
  853. case STREAM_TYPE_VIDEO_H264:
  854. codec_type = CODEC_TYPE_VIDEO;
  855. codec_id = CODEC_ID_H264;
  856. break;
  857. case STREAM_TYPE_VIDEO_VC1:
  858. codec_type = CODEC_TYPE_VIDEO;
  859. codec_id = CODEC_ID_VC1;
  860. break;
  861. case STREAM_TYPE_VIDEO_DIRAC:
  862. codec_type = CODEC_TYPE_VIDEO;
  863. codec_id = CODEC_ID_DIRAC;
  864. break;
  865. case STREAM_TYPE_AUDIO_AAC:
  866. codec_type = CODEC_TYPE_AUDIO;
  867. codec_id = CODEC_ID_AAC;
  868. break;
  869. case STREAM_TYPE_AUDIO_AC3:
  870. codec_type = CODEC_TYPE_AUDIO;
  871. codec_id = CODEC_ID_AC3;
  872. break;
  873. case STREAM_TYPE_AUDIO_DTS:
  874. case STREAM_TYPE_AUDIO_HDMV_DTS:
  875. codec_type = CODEC_TYPE_AUDIO;
  876. codec_id = CODEC_ID_DTS;
  877. break;
  878. case STREAM_TYPE_SUBTITLE_DVB:
  879. codec_type = CODEC_TYPE_SUBTITLE;
  880. codec_id = CODEC_ID_DVB_SUBTITLE;
  881. break;
  882. default:
  883. if (code >= 0x1c0 && code <= 0x1df) {
  884. codec_type = CODEC_TYPE_AUDIO;
  885. codec_id = CODEC_ID_MP2;
  886. } else if (code == 0x1bd) {
  887. codec_type = CODEC_TYPE_AUDIO;
  888. codec_id = CODEC_ID_AC3;
  889. } else {
  890. codec_type = CODEC_TYPE_VIDEO;
  891. codec_id = CODEC_ID_PROBE;
  892. }
  893. break;
  894. }
  895. st = av_new_stream(pes->stream, pes->pid);
  896. if (st) {
  897. av_set_pts_info(st, 33, 1, 90000);
  898. st->priv_data = pes;
  899. st->codec->codec_type = codec_type;
  900. st->codec->codec_id = codec_id;
  901. st->need_parsing = AVSTREAM_PARSE_FULL;
  902. pes->st = st;
  903. }
  904. return st;
  905. }
  906. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
  907. {
  908. MpegTSFilter *tss;
  909. PESContext *pes;
  910. /* if no pid found, then add a pid context */
  911. pes = av_mallocz(sizeof(PESContext));
  912. if (!pes)
  913. return 0;
  914. pes->ts = ts;
  915. pes->stream = ts->stream;
  916. pes->pid = pid;
  917. pes->pcr_pid = pcr_pid;
  918. pes->stream_type = stream_type;
  919. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  920. if (!tss) {
  921. av_free(pes);
  922. return 0;
  923. }
  924. return pes;
  925. }
  926. /* handle one TS packet */
  927. static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
  928. {
  929. AVFormatContext *s = ts->stream;
  930. MpegTSFilter *tss;
  931. int len, pid, cc, cc_ok, afc, is_start;
  932. const uint8_t *p, *p_end;
  933. int64_t pos;
  934. pid = AV_RB16(packet + 1) & 0x1fff;
  935. if(pid && discard_pid(ts, pid))
  936. return;
  937. is_start = packet[1] & 0x40;
  938. tss = ts->pids[pid];
  939. if (ts->auto_guess && tss == NULL && is_start) {
  940. add_pes_stream(ts, pid, -1, 0);
  941. tss = ts->pids[pid];
  942. }
  943. if (!tss)
  944. return;
  945. /* continuity check (currently not used) */
  946. cc = (packet[3] & 0xf);
  947. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  948. tss->last_cc = cc;
  949. /* skip adaptation field */
  950. afc = (packet[3] >> 4) & 3;
  951. p = packet + 4;
  952. if (afc == 0) /* reserved value */
  953. return;
  954. if (afc == 2) /* adaptation field only */
  955. return;
  956. if (afc == 3) {
  957. /* skip adapation field */
  958. p += p[0] + 1;
  959. }
  960. /* if past the end of packet, ignore */
  961. p_end = packet + TS_PACKET_SIZE;
  962. if (p >= p_end)
  963. return;
  964. pos = url_ftell(ts->stream->pb);
  965. ts->pos47= pos % ts->raw_packet_size;
  966. if (tss->type == MPEGTS_SECTION) {
  967. if (is_start) {
  968. /* pointer field present */
  969. len = *p++;
  970. if (p + len > p_end)
  971. return;
  972. if (len && cc_ok) {
  973. /* write remaining section bytes */
  974. write_section_data(s, tss,
  975. p, len, 0);
  976. /* check whether filter has been closed */
  977. if (!ts->pids[pid])
  978. return;
  979. }
  980. p += len;
  981. if (p < p_end) {
  982. write_section_data(s, tss,
  983. p, p_end - p, 1);
  984. }
  985. } else {
  986. if (cc_ok) {
  987. write_section_data(s, tss,
  988. p, p_end - p, 0);
  989. }
  990. }
  991. } else {
  992. // Note: The position here points actually behind the current packet.
  993. tss->u.pes_filter.pes_cb(tss,
  994. p, p_end - p, is_start, pos - ts->raw_packet_size);
  995. }
  996. }
  997. /* XXX: try to find a better synchro over several packets (use
  998. get_packet_size() ?) */
  999. static int mpegts_resync(ByteIOContext *pb)
  1000. {
  1001. int c, i;
  1002. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1003. c = url_fgetc(pb);
  1004. if (c < 0)
  1005. return -1;
  1006. if (c == 0x47) {
  1007. url_fseek(pb, -1, SEEK_CUR);
  1008. return 0;
  1009. }
  1010. }
  1011. /* no sync found */
  1012. return -1;
  1013. }
  1014. /* return -1 if error or EOF. Return 0 if OK. */
  1015. static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
  1016. {
  1017. int skip, len;
  1018. for(;;) {
  1019. len = get_buffer(pb, buf, TS_PACKET_SIZE);
  1020. if (len != TS_PACKET_SIZE)
  1021. return AVERROR(EIO);
  1022. /* check paquet sync byte */
  1023. if (buf[0] != 0x47) {
  1024. /* find a new packet start */
  1025. url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  1026. if (mpegts_resync(pb) < 0)
  1027. return AVERROR_INVALIDDATA;
  1028. else
  1029. continue;
  1030. } else {
  1031. skip = raw_packet_size - TS_PACKET_SIZE;
  1032. if (skip > 0)
  1033. url_fskip(pb, skip);
  1034. break;
  1035. }
  1036. }
  1037. return 0;
  1038. }
  1039. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1040. {
  1041. AVFormatContext *s = ts->stream;
  1042. ByteIOContext *pb = s->pb;
  1043. uint8_t packet[TS_PACKET_SIZE];
  1044. int packet_num, ret;
  1045. ts->stop_parse = 0;
  1046. packet_num = 0;
  1047. for(;;) {
  1048. if (ts->stop_parse>0)
  1049. break;
  1050. packet_num++;
  1051. if (nb_packets != 0 && packet_num >= nb_packets)
  1052. break;
  1053. ret = read_packet(pb, packet, ts->raw_packet_size);
  1054. if (ret != 0)
  1055. return ret;
  1056. handle_packet(ts, packet);
  1057. }
  1058. return 0;
  1059. }
  1060. static int mpegts_probe(AVProbeData *p)
  1061. {
  1062. #if 1
  1063. const int size= p->buf_size;
  1064. int score, fec_score, dvhs_score;
  1065. int check_count= size / TS_FEC_PACKET_SIZE;
  1066. #define CHECK_COUNT 10
  1067. if (check_count < CHECK_COUNT)
  1068. return -1;
  1069. score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1070. dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
  1071. fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
  1072. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  1073. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  1074. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  1075. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  1076. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  1077. else return -1;
  1078. #else
  1079. /* only use the extension for safer guess */
  1080. if (match_ext(p->filename, "ts"))
  1081. return AVPROBE_SCORE_MAX;
  1082. else
  1083. return 0;
  1084. #endif
  1085. }
  1086. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1087. (-1) if not available */
  1088. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1089. const uint8_t *packet)
  1090. {
  1091. int afc, len, flags;
  1092. const uint8_t *p;
  1093. unsigned int v;
  1094. afc = (packet[3] >> 4) & 3;
  1095. if (afc <= 1)
  1096. return -1;
  1097. p = packet + 4;
  1098. len = p[0];
  1099. p++;
  1100. if (len == 0)
  1101. return -1;
  1102. flags = *p++;
  1103. len--;
  1104. if (!(flags & 0x10))
  1105. return -1;
  1106. if (len < 6)
  1107. return -1;
  1108. v = AV_RB32(p);
  1109. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1110. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1111. return 0;
  1112. }
  1113. static int mpegts_read_header(AVFormatContext *s,
  1114. AVFormatParameters *ap)
  1115. {
  1116. MpegTSContext *ts = s->priv_data;
  1117. ByteIOContext *pb = s->pb;
  1118. uint8_t buf[5*1024];
  1119. int len;
  1120. int64_t pos;
  1121. if (ap) {
  1122. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1123. if(ap->mpeg2ts_raw){
  1124. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1125. return -1;
  1126. }
  1127. }
  1128. /* read the first 1024 bytes to get packet size */
  1129. pos = url_ftell(pb);
  1130. len = get_buffer(pb, buf, sizeof(buf));
  1131. if (len != sizeof(buf))
  1132. goto fail;
  1133. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1134. if (ts->raw_packet_size <= 0)
  1135. goto fail;
  1136. ts->stream = s;
  1137. ts->auto_guess = 0;
  1138. if (s->iformat == &mpegts_demuxer) {
  1139. /* normal demux */
  1140. /* first do a scaning to get all the services */
  1141. url_fseek(pb, pos, SEEK_SET);
  1142. mpegts_scan_sdt(ts);
  1143. mpegts_set_service(ts);
  1144. handle_packets(ts, s->probesize);
  1145. /* if could not find service, enable auto_guess */
  1146. ts->auto_guess = 1;
  1147. dprintf(ts->stream, "tuning done\n");
  1148. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1149. } else {
  1150. AVStream *st;
  1151. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1152. int64_t pcrs[2], pcr_h;
  1153. int packet_count[2];
  1154. uint8_t packet[TS_PACKET_SIZE];
  1155. /* only read packets */
  1156. st = av_new_stream(s, 0);
  1157. if (!st)
  1158. goto fail;
  1159. av_set_pts_info(st, 60, 1, 27000000);
  1160. st->codec->codec_type = CODEC_TYPE_DATA;
  1161. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1162. /* we iterate until we find two PCRs to estimate the bitrate */
  1163. pcr_pid = -1;
  1164. nb_pcrs = 0;
  1165. nb_packets = 0;
  1166. for(;;) {
  1167. ret = read_packet(s->pb, packet, ts->raw_packet_size);
  1168. if (ret < 0)
  1169. return -1;
  1170. pid = AV_RB16(packet + 1) & 0x1fff;
  1171. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1172. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1173. pcr_pid = pid;
  1174. packet_count[nb_pcrs] = nb_packets;
  1175. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1176. nb_pcrs++;
  1177. if (nb_pcrs >= 2)
  1178. break;
  1179. }
  1180. nb_packets++;
  1181. }
  1182. /* NOTE1: the bitrate is computed without the FEC */
  1183. /* NOTE2: it is only the bitrate of the start of the stream */
  1184. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1185. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1186. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1187. st->codec->bit_rate = s->bit_rate;
  1188. st->start_time = ts->cur_pcr;
  1189. #if 0
  1190. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1191. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1192. #endif
  1193. }
  1194. url_fseek(pb, pos, SEEK_SET);
  1195. return 0;
  1196. fail:
  1197. return -1;
  1198. }
  1199. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1200. static int mpegts_raw_read_packet(AVFormatContext *s,
  1201. AVPacket *pkt)
  1202. {
  1203. MpegTSContext *ts = s->priv_data;
  1204. int ret, i;
  1205. int64_t pcr_h, next_pcr_h, pos;
  1206. int pcr_l, next_pcr_l;
  1207. uint8_t pcr_buf[12];
  1208. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1209. return AVERROR(ENOMEM);
  1210. pkt->pos= url_ftell(s->pb);
  1211. ret = read_packet(s->pb, pkt->data, ts->raw_packet_size);
  1212. if (ret < 0) {
  1213. av_free_packet(pkt);
  1214. return ret;
  1215. }
  1216. if (ts->mpeg2ts_compute_pcr) {
  1217. /* compute exact PCR for each packet */
  1218. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1219. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1220. pos = url_ftell(s->pb);
  1221. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1222. url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1223. get_buffer(s->pb, pcr_buf, 12);
  1224. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1225. /* XXX: not precise enough */
  1226. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1227. (i + 1);
  1228. break;
  1229. }
  1230. }
  1231. url_fseek(s->pb, pos, SEEK_SET);
  1232. /* no next PCR found: we use previous increment */
  1233. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1234. }
  1235. pkt->pts = ts->cur_pcr;
  1236. pkt->duration = ts->pcr_incr;
  1237. ts->cur_pcr += ts->pcr_incr;
  1238. }
  1239. pkt->stream_index = 0;
  1240. return 0;
  1241. }
  1242. static int mpegts_read_packet(AVFormatContext *s,
  1243. AVPacket *pkt)
  1244. {
  1245. MpegTSContext *ts = s->priv_data;
  1246. ts->pkt = pkt;
  1247. return handle_packets(ts, 0);
  1248. }
  1249. static int mpegts_read_close(AVFormatContext *s)
  1250. {
  1251. MpegTSContext *ts = s->priv_data;
  1252. int i;
  1253. clear_programs(ts);
  1254. for(i=0;i<NB_PID_MAX;i++)
  1255. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1256. return 0;
  1257. }
  1258. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1259. int64_t *ppos, int64_t pos_limit)
  1260. {
  1261. MpegTSContext *ts = s->priv_data;
  1262. int64_t pos, timestamp;
  1263. uint8_t buf[TS_PACKET_SIZE];
  1264. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1265. const int find_next= 1;
  1266. pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
  1267. if (find_next) {
  1268. for(;;) {
  1269. url_fseek(s->pb, pos, SEEK_SET);
  1270. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1271. return AV_NOPTS_VALUE;
  1272. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1273. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1274. break;
  1275. }
  1276. pos += ts->raw_packet_size;
  1277. }
  1278. } else {
  1279. for(;;) {
  1280. pos -= ts->raw_packet_size;
  1281. if (pos < 0)
  1282. return AV_NOPTS_VALUE;
  1283. url_fseek(s->pb, pos, SEEK_SET);
  1284. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1285. return AV_NOPTS_VALUE;
  1286. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  1287. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1288. break;
  1289. }
  1290. }
  1291. }
  1292. *ppos = pos;
  1293. return timestamp;
  1294. }
  1295. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1296. MpegTSContext *ts = s->priv_data;
  1297. uint8_t buf[TS_PACKET_SIZE];
  1298. int64_t pos;
  1299. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1300. return -1;
  1301. pos= url_ftell(s->pb);
  1302. for(;;) {
  1303. url_fseek(s->pb, pos, SEEK_SET);
  1304. if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1305. return -1;
  1306. // pid = AV_RB16(buf + 1) & 0x1fff;
  1307. if(buf[1] & 0x40) break;
  1308. pos += ts->raw_packet_size;
  1309. }
  1310. url_fseek(s->pb, pos, SEEK_SET);
  1311. return 0;
  1312. }
  1313. /**************************************************************/
  1314. /* parsing functions - called from other demuxers such as RTP */
  1315. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1316. {
  1317. MpegTSContext *ts;
  1318. ts = av_mallocz(sizeof(MpegTSContext));
  1319. if (!ts)
  1320. return NULL;
  1321. /* no stream case, currently used by RTP */
  1322. ts->raw_packet_size = TS_PACKET_SIZE;
  1323. ts->stream = s;
  1324. ts->auto_guess = 1;
  1325. return ts;
  1326. }
  1327. /* return the consumed length if a packet was output, or -1 if no
  1328. packet is output */
  1329. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1330. const uint8_t *buf, int len)
  1331. {
  1332. int len1;
  1333. len1 = len;
  1334. ts->pkt = pkt;
  1335. ts->stop_parse = 0;
  1336. for(;;) {
  1337. if (ts->stop_parse>0)
  1338. break;
  1339. if (len < TS_PACKET_SIZE)
  1340. return -1;
  1341. if (buf[0] != 0x47) {
  1342. buf++;
  1343. len--;
  1344. } else {
  1345. handle_packet(ts, buf);
  1346. buf += TS_PACKET_SIZE;
  1347. len -= TS_PACKET_SIZE;
  1348. }
  1349. }
  1350. return len1 - len;
  1351. }
  1352. void mpegts_parse_close(MpegTSContext *ts)
  1353. {
  1354. int i;
  1355. for(i=0;i<NB_PID_MAX;i++)
  1356. av_free(ts->pids[i]);
  1357. av_free(ts);
  1358. }
  1359. AVInputFormat mpegts_demuxer = {
  1360. "mpegts",
  1361. NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
  1362. sizeof(MpegTSContext),
  1363. mpegts_probe,
  1364. mpegts_read_header,
  1365. mpegts_read_packet,
  1366. mpegts_read_close,
  1367. read_seek,
  1368. mpegts_get_pcr,
  1369. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1370. };
  1371. AVInputFormat mpegtsraw_demuxer = {
  1372. "mpegtsraw",
  1373. NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
  1374. sizeof(MpegTSContext),
  1375. NULL,
  1376. mpegts_read_header,
  1377. mpegts_raw_read_packet,
  1378. mpegts_read_close,
  1379. read_seek,
  1380. mpegts_get_pcr,
  1381. .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
  1382. };