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.

1435 lines
40KB

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