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.

1518 lines
43KB

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