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.

1528 lines
43KB

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