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.

968 lines
26KB

  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. /* 1.0 second at 24Mbit/s */
  23. #define MAX_SCAN_PACKETS 16000
  24. static int add_pes_stream(AVFormatContext *s, int pid);
  25. enum MpegTSFilterType {
  26. MPEGTS_PES,
  27. MPEGTS_SECTION,
  28. };
  29. /* XXX: suppress 'pkt' parameter */
  30. typedef void PESCallback(void *opaque, const uint8_t *buf, int len, int is_start);
  31. typedef struct MpegTSPESFilter {
  32. PESCallback *pes_cb;
  33. void *opaque;
  34. } MpegTSPESFilter;
  35. typedef void SectionCallback(void *opaque, const uint8_t *buf, int len);
  36. typedef void SetServiceCallback(void *opaque, int ret);
  37. typedef struct MpegTSSectionFilter {
  38. int section_index;
  39. int section_h_size;
  40. uint8_t *section_buf;
  41. int check_crc:1;
  42. int end_of_section_reached:1;
  43. SectionCallback *section_cb;
  44. void *opaque;
  45. } MpegTSSectionFilter;
  46. typedef struct MpegTSFilter {
  47. int pid;
  48. int last_cc; /* last cc code (-1 if first packet) */
  49. enum MpegTSFilterType type;
  50. union {
  51. MpegTSPESFilter pes_filter;
  52. MpegTSSectionFilter section_filter;
  53. } u;
  54. } MpegTSFilter;
  55. typedef struct MpegTSService {
  56. int running:1;
  57. int sid;
  58. char *provider_name;
  59. char *name;
  60. } MpegTSService;
  61. typedef struct MpegTSContext {
  62. /* user data */
  63. AVFormatContext *stream;
  64. int raw_packet_size; /* raw packet size, including FEC if present */
  65. int auto_guess; /* if true, all pids are analized to find streams */
  66. int set_service_ret;
  67. /* data needed to handle file based ts */
  68. int stop_parse; /* stop parsing loop */
  69. AVPacket *pkt; /* packet containing av data */
  70. /******************************************/
  71. /* private mpegts data */
  72. /* scan context */
  73. MpegTSFilter *sdt_filter;
  74. int nb_services;
  75. MpegTSService **services;
  76. /* set service context (XXX: allocated it ?) */
  77. SetServiceCallback *set_service_cb;
  78. void *set_service_opaque;
  79. MpegTSFilter *pat_filter;
  80. MpegTSFilter *pmt_filter;
  81. int req_sid;
  82. MpegTSFilter *pids[NB_PID_MAX];
  83. } MpegTSContext;
  84. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  85. const uint8_t *buf, int buf_size, int is_start)
  86. {
  87. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  88. int len;
  89. unsigned int crc;
  90. if (is_start) {
  91. memcpy(tss->section_buf, buf, buf_size);
  92. tss->section_index = buf_size;
  93. tss->section_h_size = -1;
  94. tss->end_of_section_reached = 0;
  95. } else {
  96. if (tss->end_of_section_reached)
  97. return;
  98. len = 4096 - tss->section_index;
  99. if (buf_size < len)
  100. len = buf_size;
  101. memcpy(tss->section_buf + tss->section_index, buf, len);
  102. tss->section_index += len;
  103. }
  104. /* compute section length if possible */
  105. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  106. len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
  107. if (len > 4096)
  108. return;
  109. tss->section_h_size = len;
  110. }
  111. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  112. if (tss->check_crc) {
  113. crc = mpegts_crc32(tss->section_buf, tss->section_h_size);
  114. if (crc != 0)
  115. goto invalid_crc;
  116. }
  117. tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
  118. invalid_crc:
  119. tss->end_of_section_reached = 1;
  120. }
  121. }
  122. MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  123. SectionCallback *section_cb, void *opaque,
  124. int check_crc)
  125. {
  126. MpegTSFilter *filter;
  127. MpegTSSectionFilter *sec;
  128. if (pid >= NB_PID_MAX || ts->pids[pid])
  129. return NULL;
  130. filter = av_mallocz(sizeof(MpegTSFilter));
  131. if (!filter)
  132. return NULL;
  133. ts->pids[pid] = filter;
  134. filter->type = MPEGTS_SECTION;
  135. filter->pid = pid;
  136. filter->last_cc = -1;
  137. sec = &filter->u.section_filter;
  138. sec->section_cb = section_cb;
  139. sec->opaque = opaque;
  140. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  141. sec->check_crc = check_crc;
  142. if (!sec->section_buf) {
  143. av_free(filter);
  144. return NULL;
  145. }
  146. return filter;
  147. }
  148. MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  149. PESCallback *pes_cb,
  150. void *opaque)
  151. {
  152. MpegTSFilter *filter;
  153. MpegTSPESFilter *pes;
  154. if (pid >= NB_PID_MAX || ts->pids[pid])
  155. return NULL;
  156. filter = av_mallocz(sizeof(MpegTSFilter));
  157. if (!filter)
  158. return NULL;
  159. ts->pids[pid] = filter;
  160. filter->type = MPEGTS_PES;
  161. filter->pid = pid;
  162. filter->last_cc = -1;
  163. pes = &filter->u.pes_filter;
  164. pes->pes_cb = pes_cb;
  165. pes->opaque = opaque;
  166. return filter;
  167. }
  168. void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  169. {
  170. int pid;
  171. pid = filter->pid;
  172. if (filter->type == MPEGTS_SECTION)
  173. av_freep(&filter->u.section_filter.section_buf);
  174. av_free(filter);
  175. ts->pids[pid] = NULL;
  176. }
  177. /* autodetect fec presence. Must have at least 1024 bytes */
  178. static int get_packet_size(const uint8_t *buf, int size)
  179. {
  180. int i;
  181. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  182. return -1;
  183. for(i=0;i<5;i++) {
  184. if (buf[i * TS_PACKET_SIZE] != 0x47)
  185. goto try_fec;
  186. }
  187. return TS_PACKET_SIZE;
  188. try_fec:
  189. for(i=0;i<5;i++) {
  190. if (buf[i * TS_FEC_PACKET_SIZE] != 0x47)
  191. return -1;
  192. }
  193. return TS_FEC_PACKET_SIZE;
  194. }
  195. typedef struct SectionHeader {
  196. uint8_t tid;
  197. uint16_t id;
  198. uint8_t version;
  199. uint8_t sec_num;
  200. uint8_t last_sec_num;
  201. } SectionHeader;
  202. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  203. {
  204. const uint8_t *p;
  205. int c;
  206. p = *pp;
  207. if (p >= p_end)
  208. return -1;
  209. c = *p++;
  210. *pp = p;
  211. return c;
  212. }
  213. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  214. {
  215. const uint8_t *p;
  216. int c;
  217. p = *pp;
  218. if ((p + 1) >= p_end)
  219. return -1;
  220. c = (p[0] << 8) | p[1];
  221. p += 2;
  222. *pp = p;
  223. return c;
  224. }
  225. /* read and allocate a DVB string preceeded by its length */
  226. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  227. {
  228. int len;
  229. const uint8_t *p;
  230. char *str;
  231. p = *pp;
  232. len = get8(&p, p_end);
  233. if (len < 0)
  234. return NULL;
  235. if ((p + len) > p_end)
  236. return NULL;
  237. str = av_malloc(len + 1);
  238. if (!str)
  239. return NULL;
  240. memcpy(str, p, len);
  241. str[len] = '\0';
  242. p += len;
  243. *pp = p;
  244. return str;
  245. }
  246. static int parse_section_header(SectionHeader *h,
  247. const uint8_t **pp, const uint8_t *p_end)
  248. {
  249. int val;
  250. val = get8(pp, p_end);
  251. if (val < 0)
  252. return -1;
  253. h->tid = val;
  254. *pp += 2;
  255. val = get16(pp, p_end);
  256. if (val < 0)
  257. return -1;
  258. h->id = val;
  259. val = get8(pp, p_end);
  260. if (val < 0)
  261. return -1;
  262. h->version = (val >> 1) & 0x1f;
  263. val = get8(pp, p_end);
  264. if (val < 0)
  265. return -1;
  266. h->sec_num = val;
  267. val = get8(pp, p_end);
  268. if (val < 0)
  269. return -1;
  270. h->last_sec_num = val;
  271. return 0;
  272. }
  273. static MpegTSService *new_service(MpegTSContext *ts, int sid,
  274. char *provider_name, char *name)
  275. {
  276. MpegTSService *service;
  277. #ifdef DEBUG_SI
  278. printf("new_service: sid=0x%04x provider='%s' name='%s'\n",
  279. sid, provider_name, name);
  280. #endif
  281. service = av_mallocz(sizeof(MpegTSService));
  282. if (!service)
  283. return NULL;
  284. service->sid = sid;
  285. service->provider_name = provider_name;
  286. service->name = name;
  287. dynarray_add(&ts->services, &ts->nb_services, service);
  288. return service;
  289. }
  290. static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
  291. {
  292. MpegTSContext *ts = opaque;
  293. SectionHeader h1, *h = &h1;
  294. const uint8_t *p, *p_end;
  295. int program_info_length, pcr_pid, pid, stream_type, desc_length;
  296. #ifdef DEBUG_SI
  297. printf("PMT:\n");
  298. av_hex_dump((uint8_t *)section, section_len);
  299. #endif
  300. p_end = section + section_len - 4;
  301. p = section;
  302. if (parse_section_header(h, &p, p_end) < 0)
  303. return;
  304. #ifdef DEBUG_SI
  305. printf("sid=0x%x sec_num=%d/%d\n", h->id, h->sec_num, h->last_sec_num);
  306. #endif
  307. if (h->tid != PMT_TID || (ts->req_sid >= 0 && h->id != ts->req_sid) )
  308. return;
  309. pcr_pid = get16(&p, p_end) & 0x1fff;
  310. if (pcr_pid < 0)
  311. return;
  312. #ifdef DEBUG_SI
  313. printf("pcr_pid=0x%x\n", pcr_pid);
  314. #endif
  315. program_info_length = get16(&p, p_end) & 0xfff;
  316. if (program_info_length < 0)
  317. return;
  318. p += program_info_length;
  319. if (p >= p_end)
  320. return;
  321. for(;;) {
  322. stream_type = get8(&p, p_end);
  323. if (stream_type < 0)
  324. break;
  325. pid = get16(&p, p_end) & 0x1fff;
  326. if (pid < 0)
  327. break;
  328. desc_length = get16(&p, p_end) & 0xfff;
  329. if (desc_length < 0)
  330. break;
  331. p += desc_length;
  332. if (p > p_end)
  333. return;
  334. #ifdef DEBUG_SI
  335. printf("stream_type=%d pid=0x%x\n", stream_type, pid);
  336. #endif
  337. /* now create ffmpeg stream */
  338. switch(stream_type) {
  339. case STREAM_TYPE_AUDIO_MPEG1:
  340. case STREAM_TYPE_AUDIO_MPEG2:
  341. case STREAM_TYPE_VIDEO_MPEG1:
  342. case STREAM_TYPE_VIDEO_MPEG2:
  343. case STREAM_TYPE_AUDIO_AC3:
  344. add_pes_stream(ts->stream, pid);
  345. break;
  346. default:
  347. /* we ignore the other streams */
  348. break;
  349. }
  350. }
  351. /* all parameters are there */
  352. ts->set_service_cb(ts->set_service_opaque, 0);
  353. mpegts_close_filter(ts, ts->pmt_filter);
  354. ts->pmt_filter = NULL;
  355. }
  356. static void pat_cb(void *opaque, const uint8_t *section, int section_len)
  357. {
  358. MpegTSContext *ts = opaque;
  359. SectionHeader h1, *h = &h1;
  360. const uint8_t *p, *p_end;
  361. int sid, pmt_pid;
  362. #ifdef DEBUG_SI
  363. printf("PAT:\n");
  364. av_hex_dump((uint8_t *)section, section_len);
  365. #endif
  366. p_end = section + section_len - 4;
  367. p = section;
  368. if (parse_section_header(h, &p, p_end) < 0)
  369. return;
  370. if (h->tid != PAT_TID)
  371. return;
  372. for(;;) {
  373. sid = get16(&p, p_end);
  374. if (sid < 0)
  375. break;
  376. pmt_pid = get16(&p, p_end) & 0x1fff;
  377. if (pmt_pid < 0)
  378. break;
  379. #ifdef DEBUG_SI
  380. printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
  381. #endif
  382. if (sid == 0x0000) {
  383. /* NIT info */
  384. } else {
  385. if (ts->req_sid == sid || ts->req_sid < 0) {
  386. ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,
  387. pmt_cb, ts, 1);
  388. goto found;
  389. }
  390. }
  391. }
  392. /* not found */
  393. ts->set_service_cb(ts->set_service_opaque, -1);
  394. found:
  395. mpegts_close_filter(ts, ts->pat_filter);
  396. ts->pat_filter = NULL;
  397. }
  398. void mpegts_set_service(MpegTSContext *ts, int sid,
  399. SetServiceCallback *set_service_cb, void *opaque)
  400. {
  401. ts->set_service_cb = set_service_cb;
  402. ts->set_service_opaque = opaque;
  403. ts->req_sid = sid;
  404. ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
  405. pat_cb, ts, 1);
  406. }
  407. static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
  408. {
  409. MpegTSContext *ts = opaque;
  410. SectionHeader h1, *h = &h1;
  411. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  412. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  413. char *name, *provider_name;
  414. #ifdef DEBUG_SI
  415. printf("SDT:\n");
  416. av_hex_dump((uint8_t *)section, section_len);
  417. #endif
  418. p_end = section + section_len - 4;
  419. p = section;
  420. if (parse_section_header(h, &p, p_end) < 0)
  421. return;
  422. if (h->tid != SDT_TID)
  423. return;
  424. onid = get16(&p, p_end);
  425. if (onid < 0)
  426. return;
  427. val = get8(&p, p_end);
  428. if (val < 0)
  429. return;
  430. for(;;) {
  431. sid = get16(&p, p_end);
  432. if (sid < 0)
  433. break;
  434. val = get8(&p, p_end);
  435. if (val < 0)
  436. break;
  437. desc_list_len = get16(&p, p_end) & 0xfff;
  438. if (desc_list_len < 0)
  439. break;
  440. desc_list_end = p + desc_list_len;
  441. if (desc_list_end > p_end)
  442. break;
  443. for(;;) {
  444. desc_tag = get8(&p, desc_list_end);
  445. if (desc_tag < 0)
  446. break;
  447. desc_len = get8(&p, desc_list_end);
  448. desc_end = p + desc_len;
  449. if (desc_end > desc_list_end)
  450. break;
  451. #ifdef DEBUG_SI
  452. printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);
  453. #endif
  454. switch(desc_tag) {
  455. case 0x48:
  456. service_type = get8(&p, p_end);
  457. if (service_type < 0)
  458. break;
  459. provider_name = getstr8(&p, p_end);
  460. if (!provider_name)
  461. break;
  462. name = getstr8(&p, p_end);
  463. if (!name)
  464. break;
  465. new_service(ts, sid, provider_name, name);
  466. break;
  467. default:
  468. break;
  469. }
  470. p = desc_end;
  471. }
  472. p = desc_list_end;
  473. }
  474. ts->stop_parse = 1;
  475. /* remove filter */
  476. mpegts_close_filter(ts, ts->sdt_filter);
  477. ts->sdt_filter = NULL;
  478. }
  479. /* scan services an a transport stream by looking at the sdt */
  480. void mpegts_scan_sdt(MpegTSContext *ts)
  481. {
  482. ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
  483. sdt_cb, ts, 1);
  484. }
  485. /* TS stream handling */
  486. enum MpegTSState {
  487. MPEGTS_HEADER = 0,
  488. MPEGTS_PESHEADER_FILL,
  489. MPEGTS_PAYLOAD,
  490. MPEGTS_SKIP,
  491. };
  492. /* enough for PES header + length */
  493. #define PES_START_SIZE 9
  494. #define MAX_PES_HEADER_SIZE (9 + 255)
  495. typedef struct PESContext {
  496. int pid;
  497. AVFormatContext *stream;
  498. AVStream *st;
  499. enum MpegTSState state;
  500. /* used to get the format */
  501. int data_index;
  502. int total_size;
  503. int pes_header_size;
  504. int64_t pts, dts;
  505. uint8_t header[MAX_PES_HEADER_SIZE];
  506. } PESContext;
  507. static int64_t get_pts(const uint8_t *p)
  508. {
  509. int64_t pts;
  510. int val;
  511. pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  512. val = (p[1] << 8) | p[2];
  513. pts |= (int64_t)(val >> 1) << 15;
  514. val = (p[3] << 8) | p[4];
  515. pts |= (int64_t)(val >> 1);
  516. return pts;
  517. }
  518. /* return non zero if a packet could be constructed */
  519. static void mpegts_push_data(void *opaque,
  520. const uint8_t *buf, int buf_size, int is_start)
  521. {
  522. PESContext *pes = opaque;
  523. MpegTSContext *ts = pes->stream->priv_data;
  524. AVStream *st;
  525. const uint8_t *p;
  526. int len, code, codec_type, codec_id;
  527. if (is_start) {
  528. pes->state = MPEGTS_HEADER;
  529. pes->data_index = 0;
  530. }
  531. p = buf;
  532. while (buf_size > 0) {
  533. switch(pes->state) {
  534. case MPEGTS_HEADER:
  535. len = PES_START_SIZE - pes->data_index;
  536. if (len > buf_size)
  537. len = buf_size;
  538. memcpy(pes->header + pes->data_index, p, len);
  539. pes->data_index += len;
  540. p += len;
  541. buf_size -= len;
  542. if (pes->data_index == PES_START_SIZE) {
  543. /* we got all the PES or section header. We can now
  544. decide */
  545. #if 0
  546. av_hex_dump(pes->header, pes->data_index);
  547. #endif
  548. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  549. pes->header[2] == 0x01) {
  550. /* it must be an mpeg2 PES stream */
  551. code = pes->header[3] | 0x100;
  552. if (!((code >= 0x1c0 && code <= 0x1df) ||
  553. (code >= 0x1e0 && code <= 0x1ef) ||
  554. (code == 0x1bd)))
  555. goto skip;
  556. if (!pes->st) {
  557. /* allocate stream */
  558. if (code >= 0x1c0 && code <= 0x1df) {
  559. codec_type = CODEC_TYPE_AUDIO;
  560. codec_id = CODEC_ID_MP2;
  561. } else if (code == 0x1bd) {
  562. codec_type = CODEC_TYPE_AUDIO;
  563. codec_id = CODEC_ID_AC3;
  564. } else {
  565. codec_type = CODEC_TYPE_VIDEO;
  566. codec_id = CODEC_ID_MPEG1VIDEO;
  567. }
  568. st = av_new_stream(pes->stream, pes->pid);
  569. if (st) {
  570. st->priv_data = pes;
  571. st->codec.codec_type = codec_type;
  572. st->codec.codec_id = codec_id;
  573. pes->st = st;
  574. }
  575. }
  576. pes->state = MPEGTS_PESHEADER_FILL;
  577. pes->total_size = (pes->header[4] << 8) | pes->header[5];
  578. /* NOTE: a zero total size means the PES size is
  579. unbounded */
  580. if (pes->total_size)
  581. pes->total_size += 6;
  582. pes->pes_header_size = pes->header[8] + 9;
  583. } else {
  584. /* otherwise, it should be a table */
  585. /* skip packet */
  586. skip:
  587. pes->state = MPEGTS_SKIP;
  588. continue;
  589. }
  590. }
  591. break;
  592. /**********************************************/
  593. /* PES packing parsing */
  594. case MPEGTS_PESHEADER_FILL:
  595. len = pes->pes_header_size - pes->data_index;
  596. if (len > buf_size)
  597. len = buf_size;
  598. memcpy(pes->header + pes->data_index, p, len);
  599. pes->data_index += len;
  600. p += len;
  601. buf_size -= len;
  602. if (pes->data_index == pes->pes_header_size) {
  603. const uint8_t *r;
  604. unsigned int flags;
  605. flags = pes->header[7];
  606. r = pes->header + 9;
  607. pes->pts = AV_NOPTS_VALUE;
  608. pes->dts = AV_NOPTS_VALUE;
  609. if ((flags & 0xc0) == 0x80) {
  610. pes->pts = get_pts(r);
  611. r += 5;
  612. } else if ((flags & 0xc0) == 0xc0) {
  613. pes->pts = get_pts(r);
  614. r += 5;
  615. pes->dts = get_pts(r);
  616. r += 5;
  617. }
  618. /* we got the full header. We parse it and get the payload */
  619. pes->state = MPEGTS_PAYLOAD;
  620. }
  621. break;
  622. case MPEGTS_PAYLOAD:
  623. if (pes->total_size) {
  624. len = pes->total_size - pes->data_index;
  625. if (len > buf_size)
  626. len = buf_size;
  627. } else {
  628. len = buf_size;
  629. }
  630. if (len > 0) {
  631. AVPacket *pkt = ts->pkt;
  632. if (pes->st && av_new_packet(pkt, len) == 0) {
  633. memcpy(pkt->data, p, len);
  634. pkt->stream_index = pes->st->index;
  635. pkt->pts = pes->pts;
  636. /* reset pts values */
  637. pes->pts = AV_NOPTS_VALUE;
  638. pes->dts = AV_NOPTS_VALUE;
  639. ts->stop_parse = 1;
  640. return;
  641. }
  642. }
  643. buf_size = 0;
  644. break;
  645. case MPEGTS_SKIP:
  646. buf_size = 0;
  647. break;
  648. }
  649. }
  650. }
  651. static int add_pes_stream(AVFormatContext *s, int pid)
  652. {
  653. MpegTSContext *ts = s->priv_data;
  654. MpegTSFilter *tss;
  655. PESContext *pes;
  656. /* if no pid found, then add a pid context */
  657. pes = av_mallocz(sizeof(PESContext));
  658. if (!pes)
  659. return -1;
  660. pes->stream = s;
  661. pes->pid = pid;
  662. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  663. if (!tss) {
  664. av_free(pes);
  665. return -1;
  666. }
  667. return 0;
  668. }
  669. /* handle one TS packet */
  670. static void handle_packet(AVFormatContext *s, uint8_t *packet)
  671. {
  672. MpegTSContext *ts = s->priv_data;
  673. MpegTSFilter *tss;
  674. int len, pid, cc, cc_ok, afc, is_start;
  675. const uint8_t *p, *p_end;
  676. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  677. is_start = packet[1] & 0x40;
  678. tss = ts->pids[pid];
  679. if (ts->auto_guess && tss == NULL && is_start) {
  680. add_pes_stream(s, pid);
  681. tss = ts->pids[pid];
  682. }
  683. if (!tss)
  684. return;
  685. /* continuity check (currently not used) */
  686. cc = (packet[3] & 0xf);
  687. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  688. tss->last_cc = cc;
  689. /* skip adaptation field */
  690. afc = (packet[3] >> 4) & 3;
  691. p = packet + 4;
  692. if (afc == 0) /* reserved value */
  693. return;
  694. if (afc == 2) /* adaptation field only */
  695. return;
  696. if (afc == 3) {
  697. /* skip adapation field */
  698. p += p[0] + 1;
  699. }
  700. /* if past the end of packet, ignore */
  701. p_end = packet + TS_PACKET_SIZE;
  702. if (p >= p_end)
  703. return;
  704. if (tss->type == MPEGTS_SECTION) {
  705. if (is_start) {
  706. /* pointer field present */
  707. len = *p++;
  708. if (p + len > p_end)
  709. return;
  710. if (len && cc_ok) {
  711. /* write remaning section bytes */
  712. write_section_data(s, tss,
  713. p, len, 0);
  714. }
  715. p += len;
  716. if (p < p_end) {
  717. write_section_data(s, tss,
  718. p, p_end - p, 1);
  719. }
  720. } else {
  721. if (cc_ok) {
  722. write_section_data(s, tss,
  723. p, p_end - p, 0);
  724. }
  725. }
  726. } else {
  727. tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
  728. p, p_end - p, is_start);
  729. }
  730. }
  731. static int handle_packets(AVFormatContext *s, int nb_packets)
  732. {
  733. MpegTSContext *ts = s->priv_data;
  734. ByteIOContext *pb = &s->pb;
  735. uint8_t packet[TS_FEC_PACKET_SIZE];
  736. int packet_num, len;
  737. int i, found = 0;
  738. int64_t pos;
  739. ts->stop_parse = 0;
  740. packet_num = 0;
  741. for(;;) {
  742. if (ts->stop_parse)
  743. break;
  744. packet_num++;
  745. if (nb_packets != 0 && packet_num >= nb_packets)
  746. break;
  747. pos = url_ftell(pb);
  748. len = get_buffer(pb, packet, ts->raw_packet_size);
  749. if (len != ts->raw_packet_size)
  750. return AVERROR_IO;
  751. /* check paquet sync byte */
  752. if (packet[0] != 0x47)
  753. {
  754. //printf("bad packet: 0x%x\n", packet[0]);
  755. found = 0;
  756. for (i = 0; i < ts->raw_packet_size; i++)
  757. {
  758. if (packet[i] == 0x47)
  759. {
  760. found = 1;
  761. //printf("packet start at: %d\n", i);
  762. break;
  763. }
  764. }
  765. if (found)
  766. {
  767. url_fseek(pb, pos + i, SEEK_SET);
  768. continue;
  769. }
  770. return AVERROR_INVALIDDATA;
  771. }
  772. handle_packet(s, packet);
  773. }
  774. return 0;
  775. }
  776. static int mpegts_probe(AVProbeData *p)
  777. {
  778. int size;
  779. size = get_packet_size(p->buf, p->buf_size);
  780. if (size < 0)
  781. return 0;
  782. return AVPROBE_SCORE_MAX - 1;
  783. }
  784. void set_service_cb(void *opaque, int ret)
  785. {
  786. MpegTSContext *ts = opaque;
  787. ts->set_service_ret = ret;
  788. ts->stop_parse = 1;
  789. }
  790. static int mpegts_read_header(AVFormatContext *s,
  791. AVFormatParameters *ap)
  792. {
  793. MpegTSContext *ts = s->priv_data;
  794. ByteIOContext *pb = &s->pb;
  795. uint8_t buf[1024];
  796. int len;
  797. int64_t pos;
  798. MpegTSService *service;
  799. /* read the first 1024 bytes to get packet size */
  800. pos = url_ftell(pb);
  801. len = get_buffer(pb, buf, sizeof(buf));
  802. if (len != sizeof(buf))
  803. goto fail;
  804. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  805. if (ts->raw_packet_size <= 0)
  806. goto fail;
  807. ts->auto_guess = 0;
  808. if (!ts->auto_guess) {
  809. int sid = -1;
  810. ts->set_service_ret = -1;
  811. /* first do a scaning to get all the services */
  812. url_fseek(pb, pos, SEEK_SET);
  813. mpegts_scan_sdt(ts);
  814. handle_packets(s, MAX_SCAN_PACKETS);
  815. if (ts->nb_services > 0)
  816. {
  817. /* tune to first service found */
  818. service = ts->services[0];
  819. sid = service->sid;
  820. #ifdef DEBUG_SI
  821. printf("tuning to '%s'\n", service->name);
  822. #endif
  823. }
  824. /* now find the info for the first service if we found any,
  825. otherwise try to filter all PATs */
  826. url_fseek(pb, pos, SEEK_SET);
  827. ts->stream = s;
  828. mpegts_set_service(ts, sid, set_service_cb, ts);
  829. handle_packets(s, MAX_SCAN_PACKETS);
  830. /* if could not find service, exit */
  831. if (ts->set_service_ret != 0)
  832. return -1;
  833. #ifdef DEBUG_SI
  834. printf("tuning done\n");
  835. #endif
  836. }
  837. url_fseek(pb, pos, SEEK_SET);
  838. return 0;
  839. fail:
  840. return -1;
  841. }
  842. static int mpegts_read_packet(AVFormatContext *s,
  843. AVPacket *pkt)
  844. {
  845. MpegTSContext *ts = s->priv_data;
  846. ts->pkt = pkt;
  847. return handle_packets(s, 0);
  848. }
  849. static int mpegts_read_close(AVFormatContext *s)
  850. {
  851. MpegTSContext *ts = s->priv_data;
  852. int i;
  853. for(i=0;i<NB_PID_MAX;i++)
  854. av_free(ts->pids[i]);
  855. return 0;
  856. }
  857. AVInputFormat mpegts_demux = {
  858. "mpegts",
  859. "MPEG2 transport stream format",
  860. sizeof(MpegTSContext),
  861. mpegts_probe,
  862. mpegts_read_header,
  863. mpegts_read_packet,
  864. mpegts_read_close,
  865. .flags = AVFMT_NOHEADER | AVFMT_SHOW_IDS,
  866. };
  867. int mpegts_init(void)
  868. {
  869. av_register_input_format(&mpegts_demux);
  870. av_register_output_format(&mpegts_mux);
  871. return 0;
  872. }