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.

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