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.

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