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.

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