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.

1501 lines
43KB

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