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.

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