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.

1429 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. if (is_start) {
  99. memcpy(tss->section_buf, buf, buf_size);
  100. tss->section_index = buf_size;
  101. tss->section_h_size = -1;
  102. tss->end_of_section_reached = 0;
  103. } else {
  104. if (tss->end_of_section_reached)
  105. return;
  106. len = 4096 - tss->section_index;
  107. if (buf_size < len)
  108. len = buf_size;
  109. memcpy(tss->section_buf + tss->section_index, buf, len);
  110. tss->section_index += len;
  111. }
  112. /* compute section length if possible */
  113. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  114. len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
  115. if (len > 4096)
  116. return;
  117. tss->section_h_size = len;
  118. }
  119. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  120. tss->end_of_section_reached = 1;
  121. if (!tss->check_crc ||
  122. mpegts_crc32(tss->section_buf, tss->section_h_size) == 0)
  123. tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
  124. }
  125. }
  126. MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  127. SectionCallback *section_cb, void *opaque,
  128. int check_crc)
  129. {
  130. MpegTSFilter *filter;
  131. MpegTSSectionFilter *sec;
  132. #ifdef DEBUG_SI
  133. printf("Filter: pid=0x%x\n", pid);
  134. #endif
  135. if (pid >= NB_PID_MAX || ts->pids[pid])
  136. return NULL;
  137. filter = av_mallocz(sizeof(MpegTSFilter));
  138. if (!filter)
  139. return NULL;
  140. ts->pids[pid] = filter;
  141. filter->type = MPEGTS_SECTION;
  142. filter->pid = pid;
  143. filter->last_cc = -1;
  144. sec = &filter->u.section_filter;
  145. sec->section_cb = section_cb;
  146. sec->opaque = opaque;
  147. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  148. sec->check_crc = check_crc;
  149. if (!sec->section_buf) {
  150. av_free(filter);
  151. return NULL;
  152. }
  153. return filter;
  154. }
  155. MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  156. PESCallback *pes_cb,
  157. void *opaque)
  158. {
  159. MpegTSFilter *filter;
  160. MpegTSPESFilter *pes;
  161. if (pid >= NB_PID_MAX || ts->pids[pid])
  162. return NULL;
  163. filter = av_mallocz(sizeof(MpegTSFilter));
  164. if (!filter)
  165. return NULL;
  166. ts->pids[pid] = filter;
  167. filter->type = MPEGTS_PES;
  168. filter->pid = pid;
  169. filter->last_cc = -1;
  170. pes = &filter->u.pes_filter;
  171. pes->pes_cb = pes_cb;
  172. pes->opaque = opaque;
  173. return filter;
  174. }
  175. void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  176. {
  177. int pid;
  178. pid = filter->pid;
  179. if (filter->type == MPEGTS_SECTION)
  180. av_freep(&filter->u.section_filter.section_buf);
  181. else if (filter->type == MPEGTS_PES)
  182. av_freep(&filter->u.pes_filter.opaque);
  183. av_free(filter);
  184. ts->pids[pid] = NULL;
  185. }
  186. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  187. int stat[packet_size];
  188. int i;
  189. int x=0;
  190. int best_score=0;
  191. memset(stat, 0, packet_size*sizeof(int));
  192. for(x=i=0; i<size; i++){
  193. if(buf[i] == 0x47){
  194. stat[x]++;
  195. if(stat[x] > best_score){
  196. best_score= stat[x];
  197. if(index) *index= x;
  198. }
  199. }
  200. x++;
  201. if(x == packet_size) x= 0;
  202. }
  203. return best_score;
  204. }
  205. /* autodetect fec presence. Must have at least 1024 bytes */
  206. static int get_packet_size(const uint8_t *buf, int size)
  207. {
  208. int score, fec_score;
  209. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  210. return -1;
  211. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  212. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  213. // av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", score, fec_score);
  214. if (score > fec_score) return TS_PACKET_SIZE;
  215. else if(score < fec_score) return TS_FEC_PACKET_SIZE;
  216. else return -1;
  217. }
  218. typedef struct SectionHeader {
  219. uint8_t tid;
  220. uint16_t id;
  221. uint8_t version;
  222. uint8_t sec_num;
  223. uint8_t last_sec_num;
  224. } SectionHeader;
  225. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  226. {
  227. const uint8_t *p;
  228. int c;
  229. p = *pp;
  230. if (p >= p_end)
  231. return -1;
  232. c = *p++;
  233. *pp = p;
  234. return c;
  235. }
  236. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  237. {
  238. const uint8_t *p;
  239. int c;
  240. p = *pp;
  241. if ((p + 1) >= p_end)
  242. return -1;
  243. c = (p[0] << 8) | p[1];
  244. p += 2;
  245. *pp = p;
  246. return c;
  247. }
  248. /* read and allocate a DVB string preceeded by its length */
  249. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  250. {
  251. int len;
  252. const uint8_t *p;
  253. char *str;
  254. p = *pp;
  255. len = get8(&p, p_end);
  256. if (len < 0)
  257. return NULL;
  258. if ((p + len) > p_end)
  259. return NULL;
  260. str = av_malloc(len + 1);
  261. if (!str)
  262. return NULL;
  263. memcpy(str, p, len);
  264. str[len] = '\0';
  265. p += len;
  266. *pp = p;
  267. return str;
  268. }
  269. static int parse_section_header(SectionHeader *h,
  270. const uint8_t **pp, const uint8_t *p_end)
  271. {
  272. int val;
  273. val = get8(pp, p_end);
  274. if (val < 0)
  275. return -1;
  276. h->tid = val;
  277. *pp += 2;
  278. val = get16(pp, p_end);
  279. if (val < 0)
  280. return -1;
  281. h->id = val;
  282. val = get8(pp, p_end);
  283. if (val < 0)
  284. return -1;
  285. h->version = (val >> 1) & 0x1f;
  286. val = get8(pp, p_end);
  287. if (val < 0)
  288. return -1;
  289. h->sec_num = val;
  290. val = get8(pp, p_end);
  291. if (val < 0)
  292. return -1;
  293. h->last_sec_num = val;
  294. return 0;
  295. }
  296. static MpegTSService *new_service(MpegTSContext *ts, int sid,
  297. char *provider_name, char *name)
  298. {
  299. MpegTSService *service;
  300. #ifdef DEBUG_SI
  301. printf("new_service: sid=0x%04x provider='%s' name='%s'\n",
  302. sid, provider_name, name);
  303. #endif
  304. service = av_mallocz(sizeof(MpegTSService));
  305. if (!service)
  306. return NULL;
  307. service->sid = sid;
  308. service->provider_name = provider_name;
  309. service->name = name;
  310. dynarray_add(&ts->services, &ts->nb_services, service);
  311. return service;
  312. }
  313. static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
  314. {
  315. MpegTSContext *ts = opaque;
  316. SectionHeader h1, *h = &h1;
  317. const uint8_t *p, *p_end;
  318. int program_info_length, pcr_pid, pid, stream_type, desc_length;
  319. #ifdef DEBUG_SI
  320. printf("PMT:\n");
  321. av_hex_dump(stdout, (uint8_t *)section, section_len);
  322. #endif
  323. p_end = section + section_len - 4;
  324. p = section;
  325. if (parse_section_header(h, &p, p_end) < 0)
  326. return;
  327. #ifdef DEBUG_SI
  328. printf("sid=0x%x sec_num=%d/%d\n", h->id, h->sec_num, h->last_sec_num);
  329. #endif
  330. if (h->tid != PMT_TID || (ts->req_sid >= 0 && h->id != ts->req_sid) )
  331. return;
  332. pcr_pid = get16(&p, p_end) & 0x1fff;
  333. if (pcr_pid < 0)
  334. return;
  335. ts->pcr_pid = pcr_pid;
  336. #ifdef DEBUG_SI
  337. printf("pcr_pid=0x%x\n", pcr_pid);
  338. #endif
  339. program_info_length = get16(&p, p_end) & 0xfff;
  340. if (program_info_length < 0)
  341. return;
  342. p += program_info_length;
  343. if (p >= p_end)
  344. return;
  345. for(;;) {
  346. stream_type = get8(&p, p_end);
  347. if (stream_type < 0)
  348. break;
  349. pid = get16(&p, p_end) & 0x1fff;
  350. if (pid < 0)
  351. break;
  352. desc_length = get16(&p, p_end) & 0xfff;
  353. if (desc_length < 0)
  354. break;
  355. p += desc_length;
  356. if (p > p_end)
  357. return;
  358. #ifdef DEBUG_SI
  359. printf("stream_type=%d pid=0x%x\n", stream_type, pid);
  360. #endif
  361. /* now create ffmpeg stream */
  362. switch(stream_type) {
  363. case STREAM_TYPE_AUDIO_MPEG1:
  364. case STREAM_TYPE_AUDIO_MPEG2:
  365. case STREAM_TYPE_VIDEO_MPEG1:
  366. case STREAM_TYPE_VIDEO_MPEG2:
  367. case STREAM_TYPE_VIDEO_MPEG4:
  368. case STREAM_TYPE_VIDEO_H264:
  369. case STREAM_TYPE_AUDIO_AAC:
  370. case STREAM_TYPE_AUDIO_AC3:
  371. case STREAM_TYPE_AUDIO_DTS:
  372. add_pes_stream(ts, pid, stream_type);
  373. break;
  374. default:
  375. /* we ignore the other streams */
  376. break;
  377. }
  378. }
  379. /* all parameters are there */
  380. ts->set_service_cb(ts->set_service_opaque, 0);
  381. mpegts_close_filter(ts, ts->pmt_filter);
  382. ts->pmt_filter = NULL;
  383. }
  384. static void pat_cb(void *opaque, const uint8_t *section, int section_len)
  385. {
  386. MpegTSContext *ts = opaque;
  387. SectionHeader h1, *h = &h1;
  388. const uint8_t *p, *p_end;
  389. int sid, pmt_pid;
  390. #ifdef DEBUG_SI
  391. printf("PAT:\n");
  392. av_hex_dump(stdout, (uint8_t *)section, section_len);
  393. #endif
  394. p_end = section + section_len - 4;
  395. p = section;
  396. if (parse_section_header(h, &p, p_end) < 0)
  397. return;
  398. if (h->tid != PAT_TID)
  399. return;
  400. for(;;) {
  401. sid = get16(&p, p_end);
  402. if (sid < 0)
  403. break;
  404. pmt_pid = get16(&p, p_end) & 0x1fff;
  405. if (pmt_pid < 0)
  406. break;
  407. #ifdef DEBUG_SI
  408. printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
  409. #endif
  410. if (sid == 0x0000) {
  411. /* NIT info */
  412. } else {
  413. if (ts->req_sid == sid) {
  414. ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,
  415. pmt_cb, ts, 1);
  416. goto found;
  417. }
  418. }
  419. }
  420. /* not found */
  421. ts->set_service_cb(ts->set_service_opaque, -1);
  422. found:
  423. mpegts_close_filter(ts, ts->pat_filter);
  424. ts->pat_filter = NULL;
  425. }
  426. /* add all services found in the PAT */
  427. static void pat_scan_cb(void *opaque, const uint8_t *section, int section_len)
  428. {
  429. MpegTSContext *ts = opaque;
  430. SectionHeader h1, *h = &h1;
  431. const uint8_t *p, *p_end;
  432. int sid, pmt_pid;
  433. char *provider_name, *name;
  434. char buf[256];
  435. #ifdef DEBUG_SI
  436. printf("PAT:\n");
  437. av_hex_dump(stdout, (uint8_t *)section, section_len);
  438. #endif
  439. p_end = section + section_len - 4;
  440. p = section;
  441. if (parse_section_header(h, &p, p_end) < 0)
  442. return;
  443. if (h->tid != PAT_TID)
  444. return;
  445. for(;;) {
  446. sid = get16(&p, p_end);
  447. if (sid < 0)
  448. break;
  449. pmt_pid = get16(&p, p_end) & 0x1fff;
  450. if (pmt_pid < 0)
  451. break;
  452. #ifdef DEBUG_SI
  453. printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
  454. #endif
  455. if (sid == 0x0000) {
  456. /* NIT info */
  457. } else {
  458. /* add the service with a dummy name */
  459. snprintf(buf, sizeof(buf), "Service %x\n", sid);
  460. name = av_strdup(buf);
  461. provider_name = av_strdup("");
  462. if (name && provider_name) {
  463. new_service(ts, sid, provider_name, name);
  464. } else {
  465. av_freep(&name);
  466. av_freep(&provider_name);
  467. }
  468. }
  469. }
  470. ts->stop_parse = 1;
  471. /* remove filter */
  472. mpegts_close_filter(ts, ts->pat_filter);
  473. ts->pat_filter = NULL;
  474. }
  475. void mpegts_set_service(MpegTSContext *ts, int sid,
  476. SetServiceCallback *set_service_cb, void *opaque)
  477. {
  478. ts->set_service_cb = set_service_cb;
  479. ts->set_service_opaque = opaque;
  480. ts->req_sid = sid;
  481. ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
  482. pat_cb, ts, 1);
  483. }
  484. static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
  485. {
  486. MpegTSContext *ts = opaque;
  487. SectionHeader h1, *h = &h1;
  488. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  489. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  490. char *name, *provider_name;
  491. #ifdef DEBUG_SI
  492. printf("SDT:\n");
  493. av_hex_dump(stdout, (uint8_t *)section, section_len);
  494. #endif
  495. p_end = section + section_len - 4;
  496. p = section;
  497. if (parse_section_header(h, &p, p_end) < 0)
  498. return;
  499. if (h->tid != SDT_TID)
  500. return;
  501. onid = get16(&p, p_end);
  502. if (onid < 0)
  503. return;
  504. val = get8(&p, p_end);
  505. if (val < 0)
  506. return;
  507. for(;;) {
  508. sid = get16(&p, p_end);
  509. if (sid < 0)
  510. break;
  511. val = get8(&p, p_end);
  512. if (val < 0)
  513. break;
  514. desc_list_len = get16(&p, p_end) & 0xfff;
  515. if (desc_list_len < 0)
  516. break;
  517. desc_list_end = p + desc_list_len;
  518. if (desc_list_end > p_end)
  519. break;
  520. for(;;) {
  521. desc_tag = get8(&p, desc_list_end);
  522. if (desc_tag < 0)
  523. break;
  524. desc_len = get8(&p, desc_list_end);
  525. desc_end = p + desc_len;
  526. if (desc_end > desc_list_end)
  527. break;
  528. #ifdef DEBUG_SI
  529. printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);
  530. #endif
  531. switch(desc_tag) {
  532. case 0x48:
  533. service_type = get8(&p, p_end);
  534. if (service_type < 0)
  535. break;
  536. provider_name = getstr8(&p, p_end);
  537. if (!provider_name)
  538. break;
  539. name = getstr8(&p, p_end);
  540. if (!name)
  541. break;
  542. new_service(ts, sid, provider_name, name);
  543. break;
  544. default:
  545. break;
  546. }
  547. p = desc_end;
  548. }
  549. p = desc_list_end;
  550. }
  551. ts->stop_parse = 1;
  552. /* remove filter */
  553. mpegts_close_filter(ts, ts->sdt_filter);
  554. ts->sdt_filter = NULL;
  555. }
  556. /* scan services in a transport stream by looking at the SDT */
  557. void mpegts_scan_sdt(MpegTSContext *ts)
  558. {
  559. ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
  560. sdt_cb, ts, 1);
  561. }
  562. /* scan services in a transport stream by looking at the PAT (better
  563. than nothing !) */
  564. void mpegts_scan_pat(MpegTSContext *ts)
  565. {
  566. ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
  567. pat_scan_cb, ts, 1);
  568. }
  569. /* TS stream handling */
  570. enum MpegTSState {
  571. MPEGTS_HEADER = 0,
  572. MPEGTS_PESHEADER_FILL,
  573. MPEGTS_PAYLOAD,
  574. MPEGTS_SKIP,
  575. };
  576. /* enough for PES header + length */
  577. #define PES_START_SIZE 9
  578. #define MAX_PES_HEADER_SIZE (9 + 255)
  579. typedef struct PESContext {
  580. int pid;
  581. int stream_type;
  582. MpegTSContext *ts;
  583. AVFormatContext *stream;
  584. AVStream *st;
  585. enum MpegTSState state;
  586. /* used to get the format */
  587. int data_index;
  588. int total_size;
  589. int pes_header_size;
  590. int64_t pts, dts;
  591. uint8_t header[MAX_PES_HEADER_SIZE];
  592. } PESContext;
  593. static int64_t get_pts(const uint8_t *p)
  594. {
  595. int64_t pts;
  596. int val;
  597. pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  598. val = (p[1] << 8) | p[2];
  599. pts |= (int64_t)(val >> 1) << 15;
  600. val = (p[3] << 8) | p[4];
  601. pts |= (int64_t)(val >> 1);
  602. return pts;
  603. }
  604. /* return non zero if a packet could be constructed */
  605. static void mpegts_push_data(void *opaque,
  606. const uint8_t *buf, int buf_size, int is_start)
  607. {
  608. PESContext *pes = opaque;
  609. MpegTSContext *ts = pes->ts;
  610. AVStream *st;
  611. const uint8_t *p;
  612. int len, code, codec_type, codec_id;
  613. if (is_start) {
  614. pes->state = MPEGTS_HEADER;
  615. pes->data_index = 0;
  616. }
  617. p = buf;
  618. while (buf_size > 0) {
  619. switch(pes->state) {
  620. case MPEGTS_HEADER:
  621. len = PES_START_SIZE - pes->data_index;
  622. if (len > buf_size)
  623. len = buf_size;
  624. memcpy(pes->header + pes->data_index, p, len);
  625. pes->data_index += len;
  626. p += len;
  627. buf_size -= len;
  628. if (pes->data_index == PES_START_SIZE) {
  629. /* we got all the PES or section header. We can now
  630. decide */
  631. #if 0
  632. av_hex_dump(pes->header, pes->data_index);
  633. #endif
  634. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  635. pes->header[2] == 0x01) {
  636. /* it must be an mpeg2 PES stream */
  637. code = pes->header[3] | 0x100;
  638. if (!((code >= 0x1c0 && code <= 0x1df) ||
  639. (code >= 0x1e0 && code <= 0x1ef) ||
  640. (code == 0x1bd)))
  641. goto skip;
  642. if (!pes->st) {
  643. /* allocate stream */
  644. switch(pes->stream_type){
  645. case STREAM_TYPE_AUDIO_MPEG1:
  646. case STREAM_TYPE_AUDIO_MPEG2:
  647. codec_type = CODEC_TYPE_AUDIO;
  648. codec_id = CODEC_ID_MP3;
  649. break;
  650. case STREAM_TYPE_VIDEO_MPEG1:
  651. case STREAM_TYPE_VIDEO_MPEG2:
  652. codec_type = CODEC_TYPE_VIDEO;
  653. codec_id = CODEC_ID_MPEG2VIDEO;
  654. break;
  655. case STREAM_TYPE_VIDEO_MPEG4:
  656. codec_type = CODEC_TYPE_VIDEO;
  657. codec_id = CODEC_ID_MPEG4;
  658. break;
  659. case STREAM_TYPE_VIDEO_H264:
  660. codec_type = CODEC_TYPE_VIDEO;
  661. codec_id = CODEC_ID_H264;
  662. break;
  663. case STREAM_TYPE_AUDIO_AAC:
  664. codec_type = CODEC_TYPE_AUDIO;
  665. codec_id = CODEC_ID_AAC;
  666. break;
  667. case STREAM_TYPE_AUDIO_AC3:
  668. codec_type = CODEC_TYPE_AUDIO;
  669. codec_id = CODEC_ID_AC3;
  670. break;
  671. case STREAM_TYPE_AUDIO_DTS:
  672. codec_type = CODEC_TYPE_AUDIO;
  673. codec_id = CODEC_ID_DTS;
  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 remaining section bytes */
  836. write_section_data(s, tss,
  837. p, len, 0);
  838. /* check whether filter has been closed */
  839. if (!ts->pids[pid])
  840. return;
  841. }
  842. p += len;
  843. if (p < p_end) {
  844. write_section_data(s, tss,
  845. p, p_end - p, 1);
  846. }
  847. } else {
  848. if (cc_ok) {
  849. write_section_data(s, tss,
  850. p, p_end - p, 0);
  851. }
  852. }
  853. } else {
  854. tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
  855. p, p_end - p, is_start);
  856. }
  857. }
  858. /* XXX: try to find a better synchro over several packets (use
  859. get_packet_size() ?) */
  860. static int mpegts_resync(ByteIOContext *pb)
  861. {
  862. int c, i;
  863. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  864. c = url_fgetc(pb);
  865. if (c < 0)
  866. return -1;
  867. if (c == 0x47) {
  868. url_fseek(pb, -1, SEEK_CUR);
  869. return 0;
  870. }
  871. }
  872. /* no sync found */
  873. return -1;
  874. }
  875. /* return -1 if error or EOF. Return 0 if OK. */
  876. static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
  877. {
  878. int skip, len;
  879. for(;;) {
  880. len = get_buffer(pb, buf, TS_PACKET_SIZE);
  881. if (len != TS_PACKET_SIZE)
  882. return AVERROR_IO;
  883. /* check paquet sync byte */
  884. if (buf[0] != 0x47) {
  885. /* find a new packet start */
  886. url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  887. if (mpegts_resync(pb) < 0)
  888. return AVERROR_INVALIDDATA;
  889. else
  890. continue;
  891. } else {
  892. skip = raw_packet_size - TS_PACKET_SIZE;
  893. if (skip > 0)
  894. url_fskip(pb, skip);
  895. break;
  896. }
  897. }
  898. return 0;
  899. }
  900. static int handle_packets(MpegTSContext *ts, int nb_packets)
  901. {
  902. AVFormatContext *s = ts->stream;
  903. ByteIOContext *pb = &s->pb;
  904. uint8_t packet[TS_PACKET_SIZE];
  905. int packet_num, ret;
  906. ts->stop_parse = 0;
  907. packet_num = 0;
  908. for(;;) {
  909. if (ts->stop_parse)
  910. break;
  911. packet_num++;
  912. if (nb_packets != 0 && packet_num >= nb_packets)
  913. break;
  914. ret = read_packet(pb, packet, ts->raw_packet_size);
  915. if (ret != 0)
  916. return ret;
  917. handle_packet(ts, packet);
  918. }
  919. return 0;
  920. }
  921. static int mpegts_probe(AVProbeData *p)
  922. {
  923. #if 1
  924. const int size= p->buf_size;
  925. int score, fec_score;
  926. #define CHECK_COUNT 10
  927. if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
  928. return -1;
  929. score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
  930. fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
  931. // av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", score, fec_score);
  932. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  933. if (score > fec_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  934. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  935. else return -1;
  936. #else
  937. /* only use the extension for safer guess */
  938. if (match_ext(p->filename, "ts"))
  939. return AVPROBE_SCORE_MAX;
  940. else
  941. return 0;
  942. #endif
  943. }
  944. void set_service_cb(void *opaque, int ret)
  945. {
  946. MpegTSContext *ts = opaque;
  947. ts->set_service_ret = ret;
  948. ts->stop_parse = 1;
  949. }
  950. /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
  951. (-1) if not available */
  952. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  953. const uint8_t *packet)
  954. {
  955. int afc, len, flags;
  956. const uint8_t *p;
  957. unsigned int v;
  958. afc = (packet[3] >> 4) & 3;
  959. if (afc <= 1)
  960. return -1;
  961. p = packet + 4;
  962. len = p[0];
  963. p++;
  964. if (len == 0)
  965. return -1;
  966. flags = *p++;
  967. len--;
  968. if (!(flags & 0x10))
  969. return -1;
  970. if (len < 6)
  971. return -1;
  972. v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  973. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  974. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  975. return 0;
  976. }
  977. static int mpegts_read_header(AVFormatContext *s,
  978. AVFormatParameters *ap)
  979. {
  980. MpegTSContext *ts = s->priv_data;
  981. ByteIOContext *pb = &s->pb;
  982. uint8_t buf[1024];
  983. int len, sid, i;
  984. int64_t pos;
  985. MpegTSService *service;
  986. if (ap) {
  987. ts->mpeg2ts_raw = ap->mpeg2ts_raw;
  988. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  989. }
  990. /* read the first 1024 bytes to get packet size */
  991. pos = url_ftell(pb);
  992. len = get_buffer(pb, buf, sizeof(buf));
  993. if (len != sizeof(buf))
  994. goto fail;
  995. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  996. if (ts->raw_packet_size <= 0)
  997. goto fail;
  998. ts->stream = s;
  999. ts->auto_guess = 0;
  1000. if (!ts->mpeg2ts_raw) {
  1001. /* normal demux */
  1002. if (!ts->auto_guess) {
  1003. ts->set_service_ret = -1;
  1004. /* first do a scaning to get all the services */
  1005. url_fseek(pb, pos, SEEK_SET);
  1006. mpegts_scan_sdt(ts);
  1007. handle_packets(ts, MAX_SCAN_PACKETS);
  1008. if (ts->nb_services <= 0) {
  1009. /* no SDT found, we try to look at the PAT */
  1010. /* First remove the SDT filters from each PID */
  1011. int i;
  1012. for (i=0; i < NB_PID_MAX; i++) {
  1013. if (ts->pids[i])
  1014. mpegts_close_filter(ts, ts->pids[i]);
  1015. }
  1016. url_fseek(pb, pos, SEEK_SET);
  1017. mpegts_scan_pat(ts);
  1018. handle_packets(ts, MAX_SCAN_PACKETS);
  1019. }
  1020. if (ts->nb_services <= 0) {
  1021. /* raw transport stream */
  1022. ts->auto_guess = 1;
  1023. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1024. goto do_pcr;
  1025. }
  1026. /* tune to first service found */
  1027. for(i=0; i<ts->nb_services && ts->set_service_ret; i++){
  1028. service = ts->services[i];
  1029. sid = service->sid;
  1030. #ifdef DEBUG_SI
  1031. printf("tuning to '%s'\n", service->name);
  1032. #endif
  1033. /* now find the info for the first service if we found any,
  1034. otherwise try to filter all PATs */
  1035. url_fseek(pb, pos, SEEK_SET);
  1036. mpegts_set_service(ts, sid, set_service_cb, ts);
  1037. handle_packets(ts, MAX_SCAN_PACKETS);
  1038. }
  1039. /* if could not find service, exit */
  1040. if (ts->set_service_ret != 0)
  1041. return -1;
  1042. #ifdef DEBUG_SI
  1043. printf("tuning done\n");
  1044. #endif
  1045. }
  1046. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1047. } else {
  1048. AVStream *st;
  1049. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1050. int64_t pcrs[2], pcr_h;
  1051. int packet_count[2];
  1052. uint8_t packet[TS_PACKET_SIZE];
  1053. /* only read packets */
  1054. do_pcr:
  1055. st = av_new_stream(s, 0);
  1056. if (!st)
  1057. goto fail;
  1058. av_set_pts_info(st, 60, 1, 27000000);
  1059. st->codec.codec_type = CODEC_TYPE_DATA;
  1060. st->codec.codec_id = CODEC_ID_MPEG2TS;
  1061. /* we iterate until we find two PCRs to estimate the bitrate */
  1062. pcr_pid = -1;
  1063. nb_pcrs = 0;
  1064. nb_packets = 0;
  1065. for(;;) {
  1066. ret = read_packet(&s->pb, packet, ts->raw_packet_size);
  1067. if (ret < 0)
  1068. return -1;
  1069. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  1070. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1071. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1072. pcr_pid = pid;
  1073. packet_count[nb_pcrs] = nb_packets;
  1074. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1075. nb_pcrs++;
  1076. if (nb_pcrs >= 2)
  1077. break;
  1078. }
  1079. nb_packets++;
  1080. }
  1081. ts->pcr_pid = pcr_pid;
  1082. /* NOTE1: the bitrate is computed without the FEC */
  1083. /* NOTE2: it is only the bitrate of the start of the stream */
  1084. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1085. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1086. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1087. st->codec.bit_rate = s->bit_rate;
  1088. st->start_time = ts->cur_pcr * 1000000.0 / 27.0e6;
  1089. #if 0
  1090. printf("start=%0.3f pcr=%0.3f incr=%d\n",
  1091. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1092. #endif
  1093. }
  1094. url_fseek(pb, pos, SEEK_SET);
  1095. return 0;
  1096. fail:
  1097. return -1;
  1098. }
  1099. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1100. static int mpegts_raw_read_packet(AVFormatContext *s,
  1101. AVPacket *pkt)
  1102. {
  1103. MpegTSContext *ts = s->priv_data;
  1104. int ret, i;
  1105. int64_t pcr_h, next_pcr_h, pos;
  1106. int pcr_l, next_pcr_l;
  1107. uint8_t pcr_buf[12];
  1108. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1109. return -ENOMEM;
  1110. ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
  1111. if (ret < 0) {
  1112. av_free_packet(pkt);
  1113. return ret;
  1114. }
  1115. if (ts->mpeg2ts_compute_pcr) {
  1116. /* compute exact PCR for each packet */
  1117. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1118. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1119. pos = url_ftell(&s->pb);
  1120. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1121. url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1122. get_buffer(&s->pb, pcr_buf, 12);
  1123. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1124. /* XXX: not precise enough */
  1125. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1126. (i + 1);
  1127. break;
  1128. }
  1129. }
  1130. url_fseek(&s->pb, pos, SEEK_SET);
  1131. /* no next PCR found: we use previous increment */
  1132. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1133. }
  1134. pkt->pts = ts->cur_pcr;
  1135. pkt->duration = ts->pcr_incr;
  1136. ts->cur_pcr += ts->pcr_incr;
  1137. }
  1138. pkt->stream_index = 0;
  1139. return 0;
  1140. }
  1141. static int mpegts_read_packet(AVFormatContext *s,
  1142. AVPacket *pkt)
  1143. {
  1144. MpegTSContext *ts = s->priv_data;
  1145. if (!ts->mpeg2ts_raw) {
  1146. ts->pkt = pkt;
  1147. return handle_packets(ts, 0);
  1148. } else {
  1149. return mpegts_raw_read_packet(s, pkt);
  1150. }
  1151. }
  1152. static int mpegts_read_close(AVFormatContext *s)
  1153. {
  1154. MpegTSContext *ts = s->priv_data;
  1155. int i;
  1156. for(i=0;i<NB_PID_MAX;i++)
  1157. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1158. return 0;
  1159. }
  1160. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1161. int64_t *ppos, int64_t pos_limit)
  1162. {
  1163. MpegTSContext *ts = s->priv_data;
  1164. int64_t pos, timestamp;
  1165. uint8_t buf[TS_PACKET_SIZE];
  1166. int pcr_l, pid;
  1167. const int find_next= 1;
  1168. pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
  1169. if (find_next) {
  1170. for(;;) {
  1171. url_fseek(&s->pb, pos, SEEK_SET);
  1172. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1173. return AV_NOPTS_VALUE;
  1174. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1175. if (pid == ts->pcr_pid &&
  1176. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1177. break;
  1178. }
  1179. pos += ts->raw_packet_size;
  1180. }
  1181. } else {
  1182. for(;;) {
  1183. pos -= ts->raw_packet_size;
  1184. if (pos < 0)
  1185. return AV_NOPTS_VALUE;
  1186. url_fseek(&s->pb, pos, SEEK_SET);
  1187. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1188. return AV_NOPTS_VALUE;
  1189. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1190. if (pid == ts->pcr_pid &&
  1191. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. *ppos = pos;
  1197. return timestamp;
  1198. }
  1199. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1200. MpegTSContext *ts = s->priv_data;
  1201. uint8_t buf[TS_PACKET_SIZE];
  1202. int64_t pos;
  1203. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1204. return -1;
  1205. pos= url_ftell(&s->pb);
  1206. for(;;) {
  1207. url_fseek(&s->pb, pos, SEEK_SET);
  1208. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1209. return -1;
  1210. // pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1211. if(buf[1] & 0x40) break;
  1212. pos += ts->raw_packet_size;
  1213. }
  1214. url_fseek(&s->pb, pos, SEEK_SET);
  1215. return 0;
  1216. }
  1217. /**************************************************************/
  1218. /* parsing functions - called from other demuxers such as RTP */
  1219. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1220. {
  1221. MpegTSContext *ts;
  1222. ts = av_mallocz(sizeof(MpegTSContext));
  1223. if (!ts)
  1224. return NULL;
  1225. /* no stream case, currently used by RTP */
  1226. ts->raw_packet_size = TS_PACKET_SIZE;
  1227. ts->stream = s;
  1228. ts->auto_guess = 1;
  1229. return ts;
  1230. }
  1231. /* return the consumed length if a packet was output, or -1 if no
  1232. packet is output */
  1233. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1234. const uint8_t *buf, int len)
  1235. {
  1236. int len1;
  1237. len1 = len;
  1238. ts->pkt = pkt;
  1239. ts->stop_parse = 0;
  1240. for(;;) {
  1241. if (ts->stop_parse)
  1242. break;
  1243. if (len < TS_PACKET_SIZE)
  1244. return -1;
  1245. if (buf[0] != 0x47) {
  1246. buf++;
  1247. len--;
  1248. } else {
  1249. handle_packet(ts, buf);
  1250. buf += TS_PACKET_SIZE;
  1251. len -= TS_PACKET_SIZE;
  1252. }
  1253. }
  1254. return len1 - len;
  1255. }
  1256. void mpegts_parse_close(MpegTSContext *ts)
  1257. {
  1258. int i;
  1259. for(i=0;i<NB_PID_MAX;i++)
  1260. av_free(ts->pids[i]);
  1261. av_free(ts);
  1262. }
  1263. AVInputFormat mpegts_demux = {
  1264. "mpegts",
  1265. "MPEG2 transport stream format",
  1266. sizeof(MpegTSContext),
  1267. mpegts_probe,
  1268. mpegts_read_header,
  1269. mpegts_read_packet,
  1270. mpegts_read_close,
  1271. read_seek,
  1272. mpegts_get_pcr,
  1273. .flags = AVFMT_SHOW_IDS,
  1274. };
  1275. int mpegts_init(void)
  1276. {
  1277. av_register_input_format(&mpegts_demux);
  1278. #ifdef CONFIG_ENCODERS
  1279. av_register_output_format(&mpegts_mux);
  1280. #endif
  1281. return 0;
  1282. }