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.

1554 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. #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. /* set service context (XXX: allocated it ?) */
  96. SetServiceCallback *set_service_cb;
  97. void *set_service_opaque;
  98. /** filter for the PAT */
  99. MpegTSFilter *pat_filter;
  100. /** filter for the PMT for the MPEG program number specified by req_sid */
  101. MpegTSFilter *pmt_filter;
  102. /** MPEG program number of stream we want to decode */
  103. int req_sid;
  104. /** filters for various streams specified by PMT + for the PAT and PMT */
  105. MpegTSFilter *pids[NB_PID_MAX];
  106. };
  107. /**
  108. * Assembles PES packets out of TS packets, and then calls the "section_cb"
  109. * function when they are complete.
  110. */
  111. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  112. const uint8_t *buf, int buf_size, int is_start)
  113. {
  114. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  115. int len;
  116. if (is_start) {
  117. memcpy(tss->section_buf, buf, buf_size);
  118. tss->section_index = buf_size;
  119. tss->section_h_size = -1;
  120. tss->end_of_section_reached = 0;
  121. } else {
  122. if (tss->end_of_section_reached)
  123. return;
  124. len = 4096 - tss->section_index;
  125. if (buf_size < len)
  126. len = buf_size;
  127. memcpy(tss->section_buf + tss->section_index, buf, len);
  128. tss->section_index += len;
  129. }
  130. /* compute section length if possible */
  131. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  132. len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
  133. if (len > 4096)
  134. return;
  135. tss->section_h_size = len;
  136. }
  137. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  138. tss->end_of_section_reached = 1;
  139. if (!tss->check_crc ||
  140. av_crc(av_crc04C11DB7, -1, tss->section_buf, tss->section_h_size) == 0)
  141. tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
  142. }
  143. }
  144. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  145. SectionCallback *section_cb, void *opaque,
  146. int check_crc)
  147. {
  148. MpegTSFilter *filter;
  149. MpegTSSectionFilter *sec;
  150. #ifdef DEBUG_SI
  151. av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
  152. #endif
  153. if (pid >= NB_PID_MAX || ts->pids[pid])
  154. return NULL;
  155. filter = av_mallocz(sizeof(MpegTSFilter));
  156. if (!filter)
  157. return NULL;
  158. ts->pids[pid] = filter;
  159. filter->type = MPEGTS_SECTION;
  160. filter->pid = pid;
  161. filter->last_cc = -1;
  162. sec = &filter->u.section_filter;
  163. sec->section_cb = section_cb;
  164. sec->opaque = opaque;
  165. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  166. sec->check_crc = check_crc;
  167. if (!sec->section_buf) {
  168. av_free(filter);
  169. return NULL;
  170. }
  171. return filter;
  172. }
  173. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  174. PESCallback *pes_cb,
  175. void *opaque)
  176. {
  177. MpegTSFilter *filter;
  178. MpegTSPESFilter *pes;
  179. if (pid >= NB_PID_MAX || ts->pids[pid])
  180. return NULL;
  181. filter = av_mallocz(sizeof(MpegTSFilter));
  182. if (!filter)
  183. return NULL;
  184. ts->pids[pid] = filter;
  185. filter->type = MPEGTS_PES;
  186. filter->pid = pid;
  187. filter->last_cc = -1;
  188. pes = &filter->u.pes_filter;
  189. pes->pes_cb = pes_cb;
  190. pes->opaque = opaque;
  191. return filter;
  192. }
  193. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  194. {
  195. int pid;
  196. pid = filter->pid;
  197. if (filter->type == MPEGTS_SECTION)
  198. av_freep(&filter->u.section_filter.section_buf);
  199. else if (filter->type == MPEGTS_PES)
  200. av_freep(&filter->u.pes_filter.opaque);
  201. av_free(filter);
  202. ts->pids[pid] = NULL;
  203. }
  204. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  205. int stat[packet_size];
  206. int i;
  207. int x=0;
  208. int best_score=0;
  209. memset(stat, 0, packet_size*sizeof(int));
  210. for(x=i=0; i<size; i++){
  211. if(buf[i] == 0x47){
  212. stat[x]++;
  213. if(stat[x] > best_score){
  214. best_score= stat[x];
  215. if(index) *index= x;
  216. }
  217. }
  218. x++;
  219. if(x == packet_size) x= 0;
  220. }
  221. return best_score;
  222. }
  223. /* autodetect fec presence. Must have at least 1024 bytes */
  224. static int get_packet_size(const uint8_t *buf, int size)
  225. {
  226. int score, fec_score, dvhs_score;
  227. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  228. return -1;
  229. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  230. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  231. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  232. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  233. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  234. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  235. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  236. else return -1;
  237. }
  238. typedef struct SectionHeader {
  239. uint8_t tid;
  240. uint16_t id;
  241. uint8_t version;
  242. uint8_t sec_num;
  243. uint8_t last_sec_num;
  244. } SectionHeader;
  245. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  246. {
  247. const uint8_t *p;
  248. int c;
  249. p = *pp;
  250. if (p >= p_end)
  251. return -1;
  252. c = *p++;
  253. *pp = p;
  254. return c;
  255. }
  256. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  257. {
  258. const uint8_t *p;
  259. int c;
  260. p = *pp;
  261. if ((p + 1) >= p_end)
  262. return -1;
  263. c = (p[0] << 8) | p[1];
  264. p += 2;
  265. *pp = p;
  266. return c;
  267. }
  268. /* read and allocate a DVB string preceeded by its length */
  269. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  270. {
  271. int len;
  272. const uint8_t *p;
  273. char *str;
  274. p = *pp;
  275. len = get8(&p, p_end);
  276. if (len < 0)
  277. return NULL;
  278. if ((p + len) > p_end)
  279. return NULL;
  280. str = av_malloc(len + 1);
  281. if (!str)
  282. return NULL;
  283. memcpy(str, p, len);
  284. str[len] = '\0';
  285. p += len;
  286. *pp = p;
  287. return str;
  288. }
  289. static int parse_section_header(SectionHeader *h,
  290. const uint8_t **pp, const uint8_t *p_end)
  291. {
  292. int val;
  293. val = get8(pp, p_end);
  294. if (val < 0)
  295. return -1;
  296. h->tid = val;
  297. *pp += 2;
  298. val = get16(pp, p_end);
  299. if (val < 0)
  300. return -1;
  301. h->id = val;
  302. val = get8(pp, p_end);
  303. if (val < 0)
  304. return -1;
  305. h->version = (val >> 1) & 0x1f;
  306. val = get8(pp, p_end);
  307. if (val < 0)
  308. return -1;
  309. h->sec_num = val;
  310. val = get8(pp, p_end);
  311. if (val < 0)
  312. return -1;
  313. h->last_sec_num = val;
  314. return 0;
  315. }
  316. static MpegTSService *new_service(MpegTSContext *ts, int sid,
  317. char *provider_name, char *name)
  318. {
  319. MpegTSService *service;
  320. #ifdef DEBUG_SI
  321. av_log(ts->stream, AV_LOG_DEBUG, "new_service: "
  322. "sid=0x%04x provider='%s' name='%s'\n",
  323. sid, provider_name, name);
  324. #endif
  325. service = av_mallocz(sizeof(MpegTSService));
  326. if (!service)
  327. return NULL;
  328. service->sid = sid;
  329. service->provider_name = provider_name;
  330. service->name = name;
  331. dynarray_add(&ts->services, &ts->nb_services, service);
  332. return service;
  333. }
  334. static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
  335. {
  336. MpegTSContext *ts = opaque;
  337. SectionHeader h1, *h = &h1;
  338. PESContext *pes;
  339. AVStream *st;
  340. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  341. int program_info_length, pcr_pid, pid, stream_type;
  342. int desc_list_len, desc_len, desc_tag;
  343. int comp_page = 0, anc_page = 0; /* initialize to kill warnings */
  344. char language[4] = {0}; /* initialize to kill warnings */
  345. #ifdef DEBUG_SI
  346. av_log(ts->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
  347. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  348. #endif
  349. p_end = section + section_len - 4;
  350. p = section;
  351. if (parse_section_header(h, &p, p_end) < 0)
  352. return;
  353. #ifdef DEBUG_SI
  354. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n",
  355. h->id, h->sec_num, h->last_sec_num);
  356. #endif
  357. if (h->tid != PMT_TID || (ts->req_sid >= 0 && h->id != ts->req_sid) )
  358. return;
  359. pcr_pid = get16(&p, p_end) & 0x1fff;
  360. if (pcr_pid < 0)
  361. return;
  362. ts->pcr_pid = pcr_pid;
  363. #ifdef DEBUG_SI
  364. av_log(ts->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
  365. #endif
  366. program_info_length = get16(&p, p_end) & 0xfff;
  367. if (program_info_length < 0)
  368. return;
  369. p += program_info_length;
  370. if (p >= p_end)
  371. return;
  372. for(;;) {
  373. language[0] = 0;
  374. st = 0;
  375. stream_type = get8(&p, p_end);
  376. if (stream_type < 0)
  377. break;
  378. pid = get16(&p, p_end) & 0x1fff;
  379. if (pid < 0)
  380. break;
  381. desc_list_len = get16(&p, p_end) & 0xfff;
  382. if (desc_list_len < 0)
  383. break;
  384. desc_list_end = p + desc_list_len;
  385. if (desc_list_end > p_end)
  386. break;
  387. for(;;) {
  388. desc_tag = get8(&p, desc_list_end);
  389. if (desc_tag < 0)
  390. break;
  391. if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
  392. if((desc_tag == 0x6A) || (desc_tag == 0x7A)) {
  393. /*assume DVB AC-3 Audio*/
  394. stream_type = STREAM_TYPE_AUDIO_AC3;
  395. } else if(desc_tag == 0x7B) {
  396. /* DVB DTS audio */
  397. stream_type = STREAM_TYPE_AUDIO_DTS;
  398. }
  399. }
  400. desc_len = get8(&p, desc_list_end);
  401. desc_end = p + desc_len;
  402. if (desc_end > desc_list_end)
  403. break;
  404. #ifdef DEBUG_SI
  405. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  406. desc_tag, desc_len);
  407. #endif
  408. switch(desc_tag) {
  409. case DVB_SUBT_DESCID:
  410. if (stream_type == STREAM_TYPE_PRIVATE_DATA)
  411. stream_type = STREAM_TYPE_SUBTITLE_DVB;
  412. language[0] = get8(&p, desc_end);
  413. language[1] = get8(&p, desc_end);
  414. language[2] = get8(&p, desc_end);
  415. language[3] = 0;
  416. get8(&p, desc_end);
  417. comp_page = get16(&p, desc_end);
  418. anc_page = get16(&p, desc_end);
  419. break;
  420. case 0x0a: /* ISO 639 language descriptor */
  421. language[0] = get8(&p, desc_end);
  422. language[1] = get8(&p, desc_end);
  423. language[2] = get8(&p, desc_end);
  424. language[3] = 0;
  425. break;
  426. default:
  427. break;
  428. }
  429. p = desc_end;
  430. }
  431. p = desc_list_end;
  432. #ifdef DEBUG_SI
  433. av_log(ts->stream, AV_LOG_DEBUG, "stream_type=%d pid=0x%x\n",
  434. stream_type, pid);
  435. #endif
  436. /* now create ffmpeg stream */
  437. switch(stream_type) {
  438. case STREAM_TYPE_AUDIO_MPEG1:
  439. case STREAM_TYPE_AUDIO_MPEG2:
  440. case STREAM_TYPE_VIDEO_MPEG1:
  441. case STREAM_TYPE_VIDEO_MPEG2:
  442. case STREAM_TYPE_VIDEO_MPEG4:
  443. case STREAM_TYPE_VIDEO_H264:
  444. case STREAM_TYPE_VIDEO_VC1:
  445. case STREAM_TYPE_AUDIO_AAC:
  446. case STREAM_TYPE_AUDIO_AC3:
  447. case STREAM_TYPE_AUDIO_DTS:
  448. case STREAM_TYPE_SUBTITLE_DVB:
  449. pes = add_pes_stream(ts, pid, stream_type);
  450. if (pes)
  451. st = new_pes_av_stream(pes, 0);
  452. break;
  453. default:
  454. /* we ignore the other streams */
  455. break;
  456. }
  457. if (st) {
  458. if (language[0] != 0) {
  459. st->language[0] = language[0];
  460. st->language[1] = language[1];
  461. st->language[2] = language[2];
  462. st->language[3] = language[3];
  463. }
  464. if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
  465. st->codec->sub_id = (anc_page << 16) | comp_page;
  466. }
  467. }
  468. }
  469. /* all parameters are there */
  470. ts->set_service_cb(ts->set_service_opaque, 0);
  471. mpegts_close_filter(ts, ts->pmt_filter);
  472. ts->pmt_filter = NULL;
  473. }
  474. static void pat_cb(void *opaque, const uint8_t *section, int section_len)
  475. {
  476. MpegTSContext *ts = opaque;
  477. SectionHeader h1, *h = &h1;
  478. const uint8_t *p, *p_end;
  479. int sid, pmt_pid;
  480. #ifdef DEBUG_SI
  481. av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
  482. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  483. #endif
  484. p_end = section + section_len - 4;
  485. p = section;
  486. if (parse_section_header(h, &p, p_end) < 0)
  487. return;
  488. if (h->tid != PAT_TID)
  489. return;
  490. for(;;) {
  491. sid = get16(&p, p_end);
  492. if (sid < 0)
  493. break;
  494. pmt_pid = get16(&p, p_end) & 0x1fff;
  495. if (pmt_pid < 0)
  496. break;
  497. #ifdef DEBUG_SI
  498. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  499. #endif
  500. if (sid == 0x0000) {
  501. /* NIT info */
  502. } else {
  503. if (ts->req_sid == sid) {
  504. ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,
  505. pmt_cb, ts, 1);
  506. goto found;
  507. }
  508. }
  509. }
  510. /* not found */
  511. ts->set_service_cb(ts->set_service_opaque, -1);
  512. found:
  513. mpegts_close_filter(ts, ts->pat_filter);
  514. ts->pat_filter = NULL;
  515. }
  516. /* add all services found in the PAT */
  517. static void pat_scan_cb(void *opaque, const uint8_t *section, int section_len)
  518. {
  519. MpegTSContext *ts = opaque;
  520. SectionHeader h1, *h = &h1;
  521. const uint8_t *p, *p_end;
  522. int sid, pmt_pid;
  523. char *provider_name, *name;
  524. char buf[256];
  525. #ifdef DEBUG_SI
  526. av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
  527. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  528. #endif
  529. p_end = section + section_len - 4;
  530. p = section;
  531. if (parse_section_header(h, &p, p_end) < 0)
  532. return;
  533. if (h->tid != PAT_TID)
  534. return;
  535. for(;;) {
  536. sid = get16(&p, p_end);
  537. if (sid < 0)
  538. break;
  539. pmt_pid = get16(&p, p_end) & 0x1fff;
  540. if (pmt_pid < 0)
  541. break;
  542. #ifdef DEBUG_SI
  543. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  544. #endif
  545. if (sid == 0x0000) {
  546. /* NIT info */
  547. } else {
  548. /* add the service with a dummy name */
  549. snprintf(buf, sizeof(buf), "Service %x\n", sid);
  550. name = av_strdup(buf);
  551. provider_name = av_strdup("");
  552. if (name && provider_name) {
  553. new_service(ts, sid, provider_name, name);
  554. } else {
  555. av_freep(&name);
  556. av_freep(&provider_name);
  557. }
  558. }
  559. }
  560. ts->stop_parse = 1;
  561. /* remove filter */
  562. mpegts_close_filter(ts, ts->pat_filter);
  563. ts->pat_filter = NULL;
  564. }
  565. static void mpegts_set_service(MpegTSContext *ts, int sid,
  566. SetServiceCallback *set_service_cb, void *opaque)
  567. {
  568. ts->set_service_cb = set_service_cb;
  569. ts->set_service_opaque = opaque;
  570. ts->req_sid = sid;
  571. ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
  572. pat_cb, ts, 1);
  573. }
  574. static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
  575. {
  576. MpegTSContext *ts = opaque;
  577. SectionHeader h1, *h = &h1;
  578. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  579. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  580. char *name, *provider_name;
  581. #ifdef DEBUG_SI
  582. av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
  583. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  584. #endif
  585. p_end = section + section_len - 4;
  586. p = section;
  587. if (parse_section_header(h, &p, p_end) < 0)
  588. return;
  589. if (h->tid != SDT_TID)
  590. return;
  591. onid = get16(&p, p_end);
  592. if (onid < 0)
  593. return;
  594. val = get8(&p, p_end);
  595. if (val < 0)
  596. return;
  597. for(;;) {
  598. sid = get16(&p, p_end);
  599. if (sid < 0)
  600. break;
  601. val = get8(&p, p_end);
  602. if (val < 0)
  603. break;
  604. desc_list_len = get16(&p, p_end) & 0xfff;
  605. if (desc_list_len < 0)
  606. break;
  607. desc_list_end = p + desc_list_len;
  608. if (desc_list_end > p_end)
  609. break;
  610. for(;;) {
  611. desc_tag = get8(&p, desc_list_end);
  612. if (desc_tag < 0)
  613. break;
  614. desc_len = get8(&p, desc_list_end);
  615. desc_end = p + desc_len;
  616. if (desc_end > desc_list_end)
  617. break;
  618. #ifdef DEBUG_SI
  619. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  620. desc_tag, desc_len);
  621. #endif
  622. switch(desc_tag) {
  623. case 0x48:
  624. service_type = get8(&p, p_end);
  625. if (service_type < 0)
  626. break;
  627. provider_name = getstr8(&p, p_end);
  628. if (!provider_name)
  629. break;
  630. name = getstr8(&p, p_end);
  631. if (!name)
  632. break;
  633. new_service(ts, sid, provider_name, name);
  634. break;
  635. default:
  636. break;
  637. }
  638. p = desc_end;
  639. }
  640. p = desc_list_end;
  641. }
  642. ts->stop_parse = 1;
  643. /* remove filter */
  644. mpegts_close_filter(ts, ts->sdt_filter);
  645. ts->sdt_filter = NULL;
  646. }
  647. /* scan services in a transport stream by looking at the SDT */
  648. static void mpegts_scan_sdt(MpegTSContext *ts)
  649. {
  650. ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
  651. sdt_cb, ts, 1);
  652. }
  653. /* scan services in a transport stream by looking at the PAT (better
  654. than nothing !) */
  655. static void mpegts_scan_pat(MpegTSContext *ts)
  656. {
  657. ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
  658. pat_scan_cb, ts, 1);
  659. }
  660. /* TS stream handling */
  661. enum MpegTSState {
  662. MPEGTS_HEADER = 0,
  663. MPEGTS_PESHEADER_FILL,
  664. MPEGTS_PAYLOAD,
  665. MPEGTS_SKIP,
  666. };
  667. /* enough for PES header + length */
  668. #define PES_START_SIZE 9
  669. #define MAX_PES_HEADER_SIZE (9 + 255)
  670. struct PESContext {
  671. int pid;
  672. int stream_type;
  673. MpegTSContext *ts;
  674. AVFormatContext *stream;
  675. AVStream *st;
  676. enum MpegTSState state;
  677. /* used to get the format */
  678. int data_index;
  679. int total_size;
  680. int pes_header_size;
  681. int64_t pts, dts;
  682. uint8_t header[MAX_PES_HEADER_SIZE];
  683. };
  684. static int64_t get_pts(const uint8_t *p)
  685. {
  686. int64_t pts;
  687. int val;
  688. pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  689. val = (p[1] << 8) | p[2];
  690. pts |= (int64_t)(val >> 1) << 15;
  691. val = (p[3] << 8) | p[4];
  692. pts |= (int64_t)(val >> 1);
  693. return pts;
  694. }
  695. /* return non zero if a packet could be constructed */
  696. static void mpegts_push_data(void *opaque,
  697. const uint8_t *buf, int buf_size, int is_start)
  698. {
  699. PESContext *pes = opaque;
  700. MpegTSContext *ts = pes->ts;
  701. const uint8_t *p;
  702. int len, code;
  703. if(!ts->pkt)
  704. return;
  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_log(pes->stream, AV_LOG_DEBUG, 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 = AVSTREAM_PARSE_FULL;
  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->stop_parse = 1;
  1057. }
  1058. /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
  1059. (-1) if not available */
  1060. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1061. const uint8_t *packet)
  1062. {
  1063. int afc, len, flags;
  1064. const uint8_t *p;
  1065. unsigned int v;
  1066. afc = (packet[3] >> 4) & 3;
  1067. if (afc <= 1)
  1068. return -1;
  1069. p = packet + 4;
  1070. len = p[0];
  1071. p++;
  1072. if (len == 0)
  1073. return -1;
  1074. flags = *p++;
  1075. len--;
  1076. if (!(flags & 0x10))
  1077. return -1;
  1078. if (len < 6)
  1079. return -1;
  1080. v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  1081. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1082. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1083. return 0;
  1084. }
  1085. static int mpegts_read_header(AVFormatContext *s,
  1086. AVFormatParameters *ap)
  1087. {
  1088. MpegTSContext *ts = s->priv_data;
  1089. ByteIOContext *pb = &s->pb;
  1090. uint8_t buf[1024];
  1091. int len, sid, i;
  1092. int64_t pos;
  1093. MpegTSService *service;
  1094. if (ap) {
  1095. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1096. if(ap->mpeg2ts_raw){
  1097. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1098. return -1;
  1099. }
  1100. }
  1101. /* read the first 1024 bytes to get packet size */
  1102. pos = url_ftell(pb);
  1103. len = get_buffer(pb, buf, sizeof(buf));
  1104. if (len != sizeof(buf))
  1105. goto fail;
  1106. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1107. if (ts->raw_packet_size <= 0)
  1108. goto fail;
  1109. ts->stream = s;
  1110. ts->auto_guess = 0;
  1111. if (s->iformat == &mpegts_demuxer) {
  1112. /* normal demux */
  1113. if (!ts->auto_guess) {
  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; 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, enable auto_guess */
  1150. ts->auto_guess = 1;
  1151. #ifdef DEBUG_SI
  1152. av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
  1153. #endif
  1154. }
  1155. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1156. } else {
  1157. AVStream *st;
  1158. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1159. int64_t pcrs[2], pcr_h;
  1160. int packet_count[2];
  1161. uint8_t packet[TS_PACKET_SIZE];
  1162. /* only read packets */
  1163. do_pcr:
  1164. st = av_new_stream(s, 0);
  1165. if (!st)
  1166. goto fail;
  1167. av_set_pts_info(st, 60, 1, 27000000);
  1168. st->codec->codec_type = CODEC_TYPE_DATA;
  1169. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1170. /* we iterate until we find two PCRs to estimate the bitrate */
  1171. pcr_pid = -1;
  1172. nb_pcrs = 0;
  1173. nb_packets = 0;
  1174. for(;;) {
  1175. ret = read_packet(&s->pb, packet, ts->raw_packet_size);
  1176. if (ret < 0)
  1177. return -1;
  1178. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  1179. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1180. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1181. pcr_pid = pid;
  1182. packet_count[nb_pcrs] = nb_packets;
  1183. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1184. nb_pcrs++;
  1185. if (nb_pcrs >= 2)
  1186. break;
  1187. }
  1188. nb_packets++;
  1189. }
  1190. ts->pcr_pid = pcr_pid;
  1191. /* NOTE1: the bitrate is computed without the FEC */
  1192. /* NOTE2: it is only the bitrate of the start of the stream */
  1193. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1194. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1195. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1196. st->codec->bit_rate = s->bit_rate;
  1197. st->start_time = ts->cur_pcr;
  1198. #if 0
  1199. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1200. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1201. #endif
  1202. }
  1203. url_fseek(pb, pos, SEEK_SET);
  1204. return 0;
  1205. fail:
  1206. return -1;
  1207. }
  1208. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1209. static int mpegts_raw_read_packet(AVFormatContext *s,
  1210. AVPacket *pkt)
  1211. {
  1212. MpegTSContext *ts = s->priv_data;
  1213. int ret, i;
  1214. int64_t pcr_h, next_pcr_h, pos;
  1215. int pcr_l, next_pcr_l;
  1216. uint8_t pcr_buf[12];
  1217. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1218. return AVERROR(ENOMEM);
  1219. pkt->pos= url_ftell(&s->pb);
  1220. ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
  1221. if (ret < 0) {
  1222. av_free_packet(pkt);
  1223. return ret;
  1224. }
  1225. if (ts->mpeg2ts_compute_pcr) {
  1226. /* compute exact PCR for each packet */
  1227. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1228. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1229. pos = url_ftell(&s->pb);
  1230. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1231. url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1232. get_buffer(&s->pb, pcr_buf, 12);
  1233. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1234. /* XXX: not precise enough */
  1235. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1236. (i + 1);
  1237. break;
  1238. }
  1239. }
  1240. url_fseek(&s->pb, pos, SEEK_SET);
  1241. /* no next PCR found: we use previous increment */
  1242. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1243. }
  1244. pkt->pts = ts->cur_pcr;
  1245. pkt->duration = ts->pcr_incr;
  1246. ts->cur_pcr += ts->pcr_incr;
  1247. }
  1248. pkt->stream_index = 0;
  1249. return 0;
  1250. }
  1251. static int mpegts_read_packet(AVFormatContext *s,
  1252. AVPacket *pkt)
  1253. {
  1254. MpegTSContext *ts = s->priv_data;
  1255. ts->pkt = pkt;
  1256. return handle_packets(ts, 0);
  1257. }
  1258. static int mpegts_read_close(AVFormatContext *s)
  1259. {
  1260. MpegTSContext *ts = s->priv_data;
  1261. int i;
  1262. for(i=0;i<NB_PID_MAX;i++)
  1263. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1264. for(i = 0; i < ts->nb_services; i++){
  1265. av_free(ts->services[i]->provider_name);
  1266. av_free(ts->services[i]->name);
  1267. av_free(ts->services[i]);
  1268. }
  1269. av_freep(&ts->services);
  1270. return 0;
  1271. }
  1272. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1273. int64_t *ppos, int64_t pos_limit)
  1274. {
  1275. MpegTSContext *ts = s->priv_data;
  1276. int64_t pos, timestamp;
  1277. uint8_t buf[TS_PACKET_SIZE];
  1278. int pcr_l, pid;
  1279. const int find_next= 1;
  1280. pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
  1281. if (find_next) {
  1282. for(;;) {
  1283. url_fseek(&s->pb, pos, SEEK_SET);
  1284. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1285. return AV_NOPTS_VALUE;
  1286. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1287. if (pid == ts->pcr_pid &&
  1288. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1289. break;
  1290. }
  1291. pos += ts->raw_packet_size;
  1292. }
  1293. } else {
  1294. for(;;) {
  1295. pos -= ts->raw_packet_size;
  1296. if (pos < 0)
  1297. return AV_NOPTS_VALUE;
  1298. url_fseek(&s->pb, pos, SEEK_SET);
  1299. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1300. return AV_NOPTS_VALUE;
  1301. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1302. if (pid == ts->pcr_pid &&
  1303. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1304. break;
  1305. }
  1306. }
  1307. }
  1308. *ppos = pos;
  1309. return timestamp;
  1310. }
  1311. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1312. MpegTSContext *ts = s->priv_data;
  1313. uint8_t buf[TS_PACKET_SIZE];
  1314. int64_t pos;
  1315. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1316. return -1;
  1317. pos= url_ftell(&s->pb);
  1318. for(;;) {
  1319. url_fseek(&s->pb, pos, SEEK_SET);
  1320. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1321. return -1;
  1322. // pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1323. if(buf[1] & 0x40) break;
  1324. pos += ts->raw_packet_size;
  1325. }
  1326. url_fseek(&s->pb, pos, SEEK_SET);
  1327. return 0;
  1328. }
  1329. /**************************************************************/
  1330. /* parsing functions - called from other demuxers such as RTP */
  1331. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1332. {
  1333. MpegTSContext *ts;
  1334. ts = av_mallocz(sizeof(MpegTSContext));
  1335. if (!ts)
  1336. return NULL;
  1337. /* no stream case, currently used by RTP */
  1338. ts->raw_packet_size = TS_PACKET_SIZE;
  1339. ts->stream = s;
  1340. ts->auto_guess = 1;
  1341. return ts;
  1342. }
  1343. /* return the consumed length if a packet was output, or -1 if no
  1344. packet is output */
  1345. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1346. const uint8_t *buf, int len)
  1347. {
  1348. int len1;
  1349. len1 = len;
  1350. ts->pkt = pkt;
  1351. ts->stop_parse = 0;
  1352. for(;;) {
  1353. if (ts->stop_parse)
  1354. break;
  1355. if (len < TS_PACKET_SIZE)
  1356. return -1;
  1357. if (buf[0] != 0x47) {
  1358. buf++;
  1359. len--;
  1360. } else {
  1361. handle_packet(ts, buf);
  1362. buf += TS_PACKET_SIZE;
  1363. len -= TS_PACKET_SIZE;
  1364. }
  1365. }
  1366. return len1 - len;
  1367. }
  1368. void mpegts_parse_close(MpegTSContext *ts)
  1369. {
  1370. int i;
  1371. for(i=0;i<NB_PID_MAX;i++)
  1372. av_free(ts->pids[i]);
  1373. av_free(ts);
  1374. }
  1375. AVInputFormat mpegts_demuxer = {
  1376. "mpegts",
  1377. "MPEG2 transport stream format",
  1378. sizeof(MpegTSContext),
  1379. mpegts_probe,
  1380. mpegts_read_header,
  1381. mpegts_read_packet,
  1382. mpegts_read_close,
  1383. read_seek,
  1384. mpegts_get_pcr,
  1385. .flags = AVFMT_SHOW_IDS,
  1386. };
  1387. AVInputFormat mpegtsraw_demuxer = {
  1388. "mpegtsraw",
  1389. "MPEG2 raw transport stream format",
  1390. sizeof(MpegTSContext),
  1391. mpegts_probe,
  1392. mpegts_read_header,
  1393. mpegts_raw_read_packet,
  1394. mpegts_read_close,
  1395. read_seek,
  1396. mpegts_get_pcr,
  1397. .flags = AVFMT_SHOW_IDS,
  1398. };