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.

1487 lines
42KB

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