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.

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