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
42KB

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