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.

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