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.

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