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.

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