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.

1553 lines
45KB

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