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.

1426 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, 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. av_set_pts_info(st, 60, 1, 90000);
  691. st->priv_data = pes;
  692. st->codec.codec_type = codec_type;
  693. st->codec.codec_id = codec_id;
  694. st->need_parsing = 1;
  695. pes->st = st;
  696. }
  697. }
  698. pes->state = MPEGTS_PESHEADER_FILL;
  699. pes->total_size = (pes->header[4] << 8) | pes->header[5];
  700. /* NOTE: a zero total size means the PES size is
  701. unbounded */
  702. if (pes->total_size)
  703. pes->total_size += 6;
  704. pes->pes_header_size = pes->header[8] + 9;
  705. } else {
  706. /* otherwise, it should be a table */
  707. /* skip packet */
  708. skip:
  709. pes->state = MPEGTS_SKIP;
  710. continue;
  711. }
  712. }
  713. break;
  714. /**********************************************/
  715. /* PES packing parsing */
  716. case MPEGTS_PESHEADER_FILL:
  717. len = pes->pes_header_size - pes->data_index;
  718. if (len > buf_size)
  719. len = buf_size;
  720. memcpy(pes->header + pes->data_index, p, len);
  721. pes->data_index += len;
  722. p += len;
  723. buf_size -= len;
  724. if (pes->data_index == pes->pes_header_size) {
  725. const uint8_t *r;
  726. unsigned int flags;
  727. flags = pes->header[7];
  728. r = pes->header + 9;
  729. pes->pts = AV_NOPTS_VALUE;
  730. pes->dts = AV_NOPTS_VALUE;
  731. if ((flags & 0xc0) == 0x80) {
  732. pes->pts = get_pts(r);
  733. r += 5;
  734. } else if ((flags & 0xc0) == 0xc0) {
  735. pes->pts = get_pts(r);
  736. r += 5;
  737. pes->dts = get_pts(r);
  738. r += 5;
  739. }
  740. /* we got the full header. We parse it and get the payload */
  741. pes->state = MPEGTS_PAYLOAD;
  742. }
  743. break;
  744. case MPEGTS_PAYLOAD:
  745. if (pes->total_size) {
  746. len = pes->total_size - pes->data_index;
  747. if (len > buf_size)
  748. len = buf_size;
  749. } else {
  750. len = buf_size;
  751. }
  752. if (len > 0) {
  753. AVPacket *pkt = ts->pkt;
  754. if (pes->st && av_new_packet(pkt, len) == 0) {
  755. memcpy(pkt->data, p, len);
  756. pkt->stream_index = pes->st->index;
  757. pkt->pts = pes->pts;
  758. pkt->dts = pes->dts;
  759. /* reset pts values */
  760. pes->pts = AV_NOPTS_VALUE;
  761. pes->dts = AV_NOPTS_VALUE;
  762. ts->stop_parse = 1;
  763. return;
  764. }
  765. }
  766. buf_size = 0;
  767. break;
  768. case MPEGTS_SKIP:
  769. buf_size = 0;
  770. break;
  771. }
  772. }
  773. }
  774. static int add_pes_stream(MpegTSContext *ts, int pid, int stream_type)
  775. {
  776. MpegTSFilter *tss;
  777. PESContext *pes;
  778. /* if no pid found, then add a pid context */
  779. pes = av_mallocz(sizeof(PESContext));
  780. if (!pes)
  781. return -1;
  782. pes->ts = ts;
  783. pes->stream = ts->stream;
  784. pes->pid = pid;
  785. pes->stream_type = stream_type;
  786. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  787. if (!tss) {
  788. av_free(pes);
  789. return -1;
  790. }
  791. return 0;
  792. }
  793. /* handle one TS packet */
  794. static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
  795. {
  796. AVFormatContext *s = ts->stream;
  797. MpegTSFilter *tss;
  798. int len, pid, cc, cc_ok, afc, is_start;
  799. const uint8_t *p, *p_end;
  800. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  801. is_start = packet[1] & 0x40;
  802. tss = ts->pids[pid];
  803. if (ts->auto_guess && tss == NULL && is_start) {
  804. add_pes_stream(ts, pid, 0);
  805. tss = ts->pids[pid];
  806. }
  807. if (!tss)
  808. return;
  809. /* continuity check (currently not used) */
  810. cc = (packet[3] & 0xf);
  811. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  812. tss->last_cc = cc;
  813. /* skip adaptation field */
  814. afc = (packet[3] >> 4) & 3;
  815. p = packet + 4;
  816. if (afc == 0) /* reserved value */
  817. return;
  818. if (afc == 2) /* adaptation field only */
  819. return;
  820. if (afc == 3) {
  821. /* skip adapation field */
  822. p += p[0] + 1;
  823. }
  824. /* if past the end of packet, ignore */
  825. p_end = packet + TS_PACKET_SIZE;
  826. if (p >= p_end)
  827. return;
  828. if (tss->type == MPEGTS_SECTION) {
  829. if (is_start) {
  830. /* pointer field present */
  831. len = *p++;
  832. if (p + len > p_end)
  833. return;
  834. if (len && cc_ok) {
  835. /* write remaning section bytes */
  836. write_section_data(s, tss,
  837. p, len, 0);
  838. }
  839. p += len;
  840. if (p < p_end) {
  841. write_section_data(s, tss,
  842. p, p_end - p, 1);
  843. }
  844. } else {
  845. if (cc_ok) {
  846. write_section_data(s, tss,
  847. p, p_end - p, 0);
  848. }
  849. }
  850. } else {
  851. tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
  852. p, p_end - p, is_start);
  853. }
  854. }
  855. /* XXX: try to find a better synchro over several packets (use
  856. get_packet_size() ?) */
  857. static int mpegts_resync(ByteIOContext *pb)
  858. {
  859. int c, i;
  860. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  861. c = url_fgetc(pb);
  862. if (c < 0)
  863. return -1;
  864. if (c == 0x47) {
  865. url_fseek(pb, -1, SEEK_CUR);
  866. return 0;
  867. }
  868. }
  869. /* no sync found */
  870. return -1;
  871. }
  872. /* return -1 if error or EOF. Return 0 if OK. */
  873. static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
  874. {
  875. int skip, len;
  876. for(;;) {
  877. len = get_buffer(pb, buf, TS_PACKET_SIZE);
  878. if (len != TS_PACKET_SIZE)
  879. return AVERROR_IO;
  880. /* check paquet sync byte */
  881. if (buf[0] != 0x47) {
  882. /* find a new packet start */
  883. url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  884. if (mpegts_resync(pb) < 0)
  885. return AVERROR_INVALIDDATA;
  886. else
  887. continue;
  888. } else {
  889. skip = raw_packet_size - TS_PACKET_SIZE;
  890. if (skip > 0)
  891. url_fskip(pb, skip);
  892. break;
  893. }
  894. }
  895. return 0;
  896. }
  897. static int handle_packets(MpegTSContext *ts, int nb_packets)
  898. {
  899. AVFormatContext *s = ts->stream;
  900. ByteIOContext *pb = &s->pb;
  901. uint8_t packet[TS_PACKET_SIZE];
  902. int packet_num, ret;
  903. ts->stop_parse = 0;
  904. packet_num = 0;
  905. for(;;) {
  906. if (ts->stop_parse)
  907. break;
  908. packet_num++;
  909. if (nb_packets != 0 && packet_num >= nb_packets)
  910. break;
  911. ret = read_packet(pb, packet, ts->raw_packet_size);
  912. if (ret != 0)
  913. return ret;
  914. handle_packet(ts, packet);
  915. }
  916. return 0;
  917. }
  918. static int mpegts_probe(AVProbeData *p)
  919. {
  920. #if 1
  921. const int size= p->buf_size;
  922. int score, fec_score;
  923. #define CHECK_COUNT 10
  924. if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
  925. return -1;
  926. score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
  927. fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
  928. // av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", score, fec_score);
  929. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  930. if (score > fec_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  931. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  932. else return -1;
  933. #else
  934. /* only use the extension for safer guess */
  935. if (match_ext(p->filename, "ts"))
  936. return AVPROBE_SCORE_MAX;
  937. else
  938. return 0;
  939. #endif
  940. }
  941. void set_service_cb(void *opaque, int ret)
  942. {
  943. MpegTSContext *ts = opaque;
  944. ts->set_service_ret = ret;
  945. ts->stop_parse = 1;
  946. }
  947. /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
  948. (-1) if not available */
  949. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  950. const uint8_t *packet)
  951. {
  952. int afc, len, flags;
  953. const uint8_t *p;
  954. unsigned int v;
  955. afc = (packet[3] >> 4) & 3;
  956. if (afc <= 1)
  957. return -1;
  958. p = packet + 4;
  959. len = p[0];
  960. p++;
  961. if (len == 0)
  962. return -1;
  963. flags = *p++;
  964. len--;
  965. if (!(flags & 0x10))
  966. return -1;
  967. if (len < 6)
  968. return -1;
  969. v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  970. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  971. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  972. return 0;
  973. }
  974. static int mpegts_read_header(AVFormatContext *s,
  975. AVFormatParameters *ap)
  976. {
  977. MpegTSContext *ts = s->priv_data;
  978. ByteIOContext *pb = &s->pb;
  979. uint8_t buf[1024];
  980. int len, sid, i;
  981. int64_t pos;
  982. MpegTSService *service;
  983. if (ap) {
  984. ts->mpeg2ts_raw = ap->mpeg2ts_raw;
  985. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  986. }
  987. /* read the first 1024 bytes to get packet size */
  988. pos = url_ftell(pb);
  989. len = get_buffer(pb, buf, sizeof(buf));
  990. if (len != sizeof(buf))
  991. goto fail;
  992. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  993. if (ts->raw_packet_size <= 0)
  994. goto fail;
  995. ts->stream = s;
  996. ts->auto_guess = 0;
  997. if (!ts->mpeg2ts_raw) {
  998. /* normal demux */
  999. if (!ts->auto_guess) {
  1000. ts->set_service_ret = -1;
  1001. /* first do a scaning to get all the services */
  1002. url_fseek(pb, pos, SEEK_SET);
  1003. mpegts_scan_sdt(ts);
  1004. handle_packets(ts, MAX_SCAN_PACKETS);
  1005. if (ts->nb_services <= 0) {
  1006. /* no SDT found, we try to look at the PAT */
  1007. /* First remove the SDT filters from each PID */
  1008. int i;
  1009. for (i=0; i < NB_PID_MAX; i++) {
  1010. if (ts->pids[i])
  1011. mpegts_close_filter(ts, ts->pids[i]);
  1012. }
  1013. url_fseek(pb, pos, SEEK_SET);
  1014. mpegts_scan_pat(ts);
  1015. handle_packets(ts, MAX_SCAN_PACKETS);
  1016. }
  1017. if (ts->nb_services <= 0) {
  1018. /* raw transport stream */
  1019. ts->auto_guess = 1;
  1020. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1021. goto do_pcr;
  1022. }
  1023. /* tune to first service found */
  1024. for(i=0; i<ts->nb_services && ts->set_service_ret; i++){
  1025. service = ts->services[i];
  1026. sid = service->sid;
  1027. #ifdef DEBUG_SI
  1028. printf("tuning to '%s'\n", service->name);
  1029. #endif
  1030. /* now find the info for the first service if we found any,
  1031. otherwise try to filter all PATs */
  1032. url_fseek(pb, pos, SEEK_SET);
  1033. mpegts_set_service(ts, sid, set_service_cb, ts);
  1034. handle_packets(ts, MAX_SCAN_PACKETS);
  1035. }
  1036. /* if could not find service, exit */
  1037. if (ts->set_service_ret != 0)
  1038. return -1;
  1039. #ifdef DEBUG_SI
  1040. printf("tuning done\n");
  1041. #endif
  1042. }
  1043. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1044. } else {
  1045. AVStream *st;
  1046. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1047. int64_t pcrs[2], pcr_h;
  1048. int packet_count[2];
  1049. uint8_t packet[TS_PACKET_SIZE];
  1050. /* only read packets */
  1051. do_pcr:
  1052. st = av_new_stream(s, 0);
  1053. if (!st)
  1054. goto fail;
  1055. av_set_pts_info(st, 60, 1, 27000000);
  1056. st->codec.codec_type = CODEC_TYPE_DATA;
  1057. st->codec.codec_id = CODEC_ID_MPEG2TS;
  1058. /* we iterate until we find two PCRs to estimate the bitrate */
  1059. pcr_pid = -1;
  1060. nb_pcrs = 0;
  1061. nb_packets = 0;
  1062. for(;;) {
  1063. ret = read_packet(&s->pb, packet, ts->raw_packet_size);
  1064. if (ret < 0)
  1065. return -1;
  1066. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  1067. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1068. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1069. pcr_pid = pid;
  1070. packet_count[nb_pcrs] = nb_packets;
  1071. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1072. nb_pcrs++;
  1073. if (nb_pcrs >= 2)
  1074. break;
  1075. }
  1076. nb_packets++;
  1077. }
  1078. ts->pcr_pid = pcr_pid;
  1079. /* NOTE1: the bitrate is computed without the FEC */
  1080. /* NOTE2: it is only the bitrate of the start of the stream */
  1081. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1082. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1083. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1084. st->codec.bit_rate = s->bit_rate;
  1085. st->start_time = ts->cur_pcr * 1000000.0 / 27.0e6;
  1086. #if 0
  1087. printf("start=%0.3f pcr=%0.3f incr=%d\n",
  1088. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1089. #endif
  1090. }
  1091. url_fseek(pb, pos, SEEK_SET);
  1092. return 0;
  1093. fail:
  1094. return -1;
  1095. }
  1096. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1097. static int mpegts_raw_read_packet(AVFormatContext *s,
  1098. AVPacket *pkt)
  1099. {
  1100. MpegTSContext *ts = s->priv_data;
  1101. int ret, i;
  1102. int64_t pcr_h, next_pcr_h, pos;
  1103. int pcr_l, next_pcr_l;
  1104. uint8_t pcr_buf[12];
  1105. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1106. return -ENOMEM;
  1107. ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
  1108. if (ret < 0) {
  1109. av_free_packet(pkt);
  1110. return ret;
  1111. }
  1112. if (ts->mpeg2ts_compute_pcr) {
  1113. /* compute exact PCR for each packet */
  1114. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1115. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1116. pos = url_ftell(&s->pb);
  1117. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1118. url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1119. get_buffer(&s->pb, pcr_buf, 12);
  1120. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1121. /* XXX: not precise enough */
  1122. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1123. (i + 1);
  1124. break;
  1125. }
  1126. }
  1127. url_fseek(&s->pb, pos, SEEK_SET);
  1128. /* no next PCR found: we use previous increment */
  1129. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1130. }
  1131. pkt->pts = ts->cur_pcr;
  1132. pkt->duration = ts->pcr_incr;
  1133. ts->cur_pcr += ts->pcr_incr;
  1134. }
  1135. pkt->stream_index = 0;
  1136. return 0;
  1137. }
  1138. static int mpegts_read_packet(AVFormatContext *s,
  1139. AVPacket *pkt)
  1140. {
  1141. MpegTSContext *ts = s->priv_data;
  1142. if (!ts->mpeg2ts_raw) {
  1143. ts->pkt = pkt;
  1144. return handle_packets(ts, 0);
  1145. } else {
  1146. return mpegts_raw_read_packet(s, pkt);
  1147. }
  1148. }
  1149. static int mpegts_read_close(AVFormatContext *s)
  1150. {
  1151. MpegTSContext *ts = s->priv_data;
  1152. int i;
  1153. for(i=0;i<NB_PID_MAX;i++)
  1154. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1155. return 0;
  1156. }
  1157. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1158. int64_t *ppos, int64_t pos_limit)
  1159. {
  1160. MpegTSContext *ts = s->priv_data;
  1161. int64_t pos, timestamp;
  1162. uint8_t buf[TS_PACKET_SIZE];
  1163. int pcr_l, pid;
  1164. const int find_next= 1;
  1165. pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
  1166. if (find_next) {
  1167. for(;;) {
  1168. url_fseek(&s->pb, pos, SEEK_SET);
  1169. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1170. return AV_NOPTS_VALUE;
  1171. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1172. if (pid == ts->pcr_pid &&
  1173. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1174. break;
  1175. }
  1176. pos += ts->raw_packet_size;
  1177. }
  1178. } else {
  1179. for(;;) {
  1180. pos -= ts->raw_packet_size;
  1181. if (pos < 0)
  1182. return AV_NOPTS_VALUE;
  1183. url_fseek(&s->pb, pos, SEEK_SET);
  1184. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1185. return AV_NOPTS_VALUE;
  1186. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1187. if (pid == ts->pcr_pid &&
  1188. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. *ppos = pos;
  1194. return timestamp;
  1195. }
  1196. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts){
  1197. MpegTSContext *ts = s->priv_data;
  1198. uint8_t buf[TS_PACKET_SIZE];
  1199. int64_t pos;
  1200. if(av_seek_frame_binary(s, stream_index, target_ts) < 0)
  1201. return -1;
  1202. pos= url_ftell(&s->pb);
  1203. for(;;) {
  1204. url_fseek(&s->pb, pos, SEEK_SET);
  1205. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1206. return -1;
  1207. // pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1208. if(buf[1] & 0x40) break;
  1209. pos += ts->raw_packet_size;
  1210. }
  1211. url_fseek(&s->pb, pos, SEEK_SET);
  1212. return 0;
  1213. }
  1214. /**************************************************************/
  1215. /* parsing functions - called from other demuxers such as RTP */
  1216. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1217. {
  1218. MpegTSContext *ts;
  1219. ts = av_mallocz(sizeof(MpegTSContext));
  1220. if (!ts)
  1221. return NULL;
  1222. /* no stream case, currently used by RTP */
  1223. ts->raw_packet_size = TS_PACKET_SIZE;
  1224. ts->stream = s;
  1225. ts->auto_guess = 1;
  1226. return ts;
  1227. }
  1228. /* return the consumed length if a packet was output, or -1 if no
  1229. packet is output */
  1230. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1231. const uint8_t *buf, int len)
  1232. {
  1233. int len1;
  1234. len1 = len;
  1235. ts->pkt = pkt;
  1236. ts->stop_parse = 0;
  1237. for(;;) {
  1238. if (ts->stop_parse)
  1239. break;
  1240. if (len < TS_PACKET_SIZE)
  1241. return -1;
  1242. if (buf[0] != 0x47) {
  1243. buf++;
  1244. len--;
  1245. } else {
  1246. handle_packet(ts, buf);
  1247. buf += TS_PACKET_SIZE;
  1248. len -= TS_PACKET_SIZE;
  1249. }
  1250. }
  1251. return len1 - len;
  1252. }
  1253. void mpegts_parse_close(MpegTSContext *ts)
  1254. {
  1255. int i;
  1256. for(i=0;i<NB_PID_MAX;i++)
  1257. av_free(ts->pids[i]);
  1258. av_free(ts);
  1259. }
  1260. AVInputFormat mpegts_demux = {
  1261. "mpegts",
  1262. "MPEG2 transport stream format",
  1263. sizeof(MpegTSContext),
  1264. mpegts_probe,
  1265. mpegts_read_header,
  1266. mpegts_read_packet,
  1267. mpegts_read_close,
  1268. read_seek,
  1269. mpegts_get_pcr,
  1270. .flags = AVFMT_SHOW_IDS,
  1271. };
  1272. int mpegts_init(void)
  1273. {
  1274. av_register_input_format(&mpegts_demux);
  1275. #ifdef CONFIG_ENCODERS
  1276. av_register_output_format(&mpegts_mux);
  1277. #endif
  1278. return 0;
  1279. }