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.

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