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.

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