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.

1524 lines
43KB

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