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.

1548 lines
44KB

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