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.

1470 lines
41KB

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