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.

937 lines
25KB

  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 || 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:
  340. case STREAM_TYPE_VIDEO:
  341. add_pes_stream(ts->stream, pid);
  342. break;
  343. default:
  344. /* we ignore the other streams */
  345. break;
  346. }
  347. }
  348. /* all parameters are there */
  349. ts->set_service_cb(ts->set_service_opaque, 0);
  350. mpegts_close_filter(ts, ts->pmt_filter);
  351. ts->pmt_filter = NULL;
  352. }
  353. static void pat_cb(void *opaque, const uint8_t *section, int section_len)
  354. {
  355. MpegTSContext *ts = opaque;
  356. SectionHeader h1, *h = &h1;
  357. const uint8_t *p, *p_end;
  358. int sid, pmt_pid;
  359. #ifdef DEBUG_SI
  360. printf("PAT:\n");
  361. av_hex_dump((uint8_t *)section, section_len);
  362. #endif
  363. p_end = section + section_len - 4;
  364. p = section;
  365. if (parse_section_header(h, &p, p_end) < 0)
  366. return;
  367. if (h->tid != PAT_TID)
  368. return;
  369. for(;;) {
  370. sid = get16(&p, p_end);
  371. if (sid < 0)
  372. break;
  373. pmt_pid = get16(&p, p_end) & 0x1fff;
  374. if (pmt_pid < 0)
  375. break;
  376. #ifdef DEBUG_SI
  377. printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
  378. #endif
  379. if (sid == 0x0000) {
  380. /* NIT info */
  381. } else {
  382. if (ts->req_sid == sid) {
  383. ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,
  384. pmt_cb, ts, 1);
  385. goto found;
  386. }
  387. }
  388. }
  389. /* not found */
  390. ts->set_service_cb(ts->set_service_opaque, -1);
  391. found:
  392. mpegts_close_filter(ts, ts->pat_filter);
  393. ts->pat_filter = NULL;
  394. }
  395. void mpegts_set_service(MpegTSContext *ts, int sid,
  396. SetServiceCallback *set_service_cb, void *opaque)
  397. {
  398. ts->set_service_cb = set_service_cb;
  399. ts->set_service_opaque = opaque;
  400. ts->req_sid = sid;
  401. ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
  402. pat_cb, ts, 1);
  403. }
  404. static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
  405. {
  406. MpegTSContext *ts = opaque;
  407. SectionHeader h1, *h = &h1;
  408. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  409. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  410. char *name, *provider_name;
  411. #ifdef DEBUG_SI
  412. printf("SDT:\n");
  413. av_hex_dump((uint8_t *)section, section_len);
  414. #endif
  415. p_end = section + section_len - 4;
  416. p = section;
  417. if (parse_section_header(h, &p, p_end) < 0)
  418. return;
  419. if (h->tid != SDT_TID)
  420. return;
  421. onid = get16(&p, p_end);
  422. if (onid < 0)
  423. return;
  424. val = get8(&p, p_end);
  425. if (val < 0)
  426. return;
  427. for(;;) {
  428. sid = get16(&p, p_end);
  429. if (sid < 0)
  430. break;
  431. val = get8(&p, p_end);
  432. if (val < 0)
  433. break;
  434. desc_list_len = get16(&p, p_end) & 0xfff;
  435. if (desc_list_len < 0)
  436. break;
  437. desc_list_end = p + desc_list_len;
  438. if (desc_list_end > p_end)
  439. break;
  440. for(;;) {
  441. desc_tag = get8(&p, desc_list_end);
  442. if (desc_tag < 0)
  443. break;
  444. desc_len = get8(&p, desc_list_end);
  445. desc_end = p + desc_len;
  446. if (desc_end > desc_list_end)
  447. break;
  448. #ifdef DEBUG_SI
  449. printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);
  450. #endif
  451. switch(desc_tag) {
  452. case 0x48:
  453. service_type = get8(&p, p_end);
  454. if (service_type < 0)
  455. break;
  456. provider_name = getstr8(&p, p_end);
  457. if (!provider_name)
  458. break;
  459. name = getstr8(&p, p_end);
  460. if (!name)
  461. break;
  462. new_service(ts, sid, provider_name, name);
  463. break;
  464. default:
  465. break;
  466. }
  467. p = desc_end;
  468. }
  469. p = desc_list_end;
  470. }
  471. ts->stop_parse = 1;
  472. /* remove filter */
  473. mpegts_close_filter(ts, ts->sdt_filter);
  474. ts->sdt_filter = NULL;
  475. }
  476. /* scan services an a transport stream by looking at the sdt */
  477. void mpegts_scan_sdt(MpegTSContext *ts)
  478. {
  479. ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
  480. sdt_cb, ts, 1);
  481. }
  482. /* TS stream handling */
  483. enum MpegTSState {
  484. MPEGTS_HEADER = 0,
  485. MPEGTS_PESHEADER_FILL,
  486. MPEGTS_PAYLOAD,
  487. MPEGTS_SKIP,
  488. };
  489. /* enough for PES header + length */
  490. #define PES_START_SIZE 9
  491. #define MAX_PES_HEADER_SIZE (9 + 255)
  492. typedef struct PESContext {
  493. int pid;
  494. AVFormatContext *stream;
  495. AVStream *st;
  496. enum MpegTSState state;
  497. /* used to get the format */
  498. int data_index;
  499. int total_size;
  500. int pes_header_size;
  501. int64_t pts, dts;
  502. uint8_t header[MAX_PES_HEADER_SIZE];
  503. } PESContext;
  504. static int64_t get_pts(const uint8_t *p)
  505. {
  506. int64_t pts;
  507. int val;
  508. pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  509. val = (p[1] << 8) | p[2];
  510. pts |= (int64_t)(val >> 1) << 15;
  511. val = (p[3] << 8) | p[4];
  512. pts |= (int64_t)(val >> 1);
  513. return pts;
  514. }
  515. /* return non zero if a packet could be constructed */
  516. static void mpegts_push_data(void *opaque,
  517. const uint8_t *buf, int buf_size, int is_start)
  518. {
  519. PESContext *pes = opaque;
  520. MpegTSContext *ts = pes->stream->priv_data;
  521. AVStream *st;
  522. const uint8_t *p;
  523. int len, code, codec_type, codec_id;
  524. if (is_start) {
  525. pes->state = MPEGTS_HEADER;
  526. pes->data_index = 0;
  527. }
  528. p = buf;
  529. while (buf_size > 0) {
  530. switch(pes->state) {
  531. case MPEGTS_HEADER:
  532. len = PES_START_SIZE - pes->data_index;
  533. if (len > buf_size)
  534. len = buf_size;
  535. memcpy(pes->header + pes->data_index, p, len);
  536. pes->data_index += len;
  537. p += len;
  538. buf_size -= len;
  539. if (pes->data_index == PES_START_SIZE) {
  540. /* we got all the PES or section header. We can now
  541. decide */
  542. #if 0
  543. av_hex_dump(pes->header, pes->data_index);
  544. #endif
  545. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  546. pes->header[2] == 0x01) {
  547. /* it must be an mpeg2 PES stream */
  548. /* XXX: add AC3 support */
  549. code = pes->header[3] | 0x100;
  550. if (!((code >= 0x1c0 && code <= 0x1df) ||
  551. (code >= 0x1e0 && code <= 0x1ef)))
  552. goto skip;
  553. if (!pes->st) {
  554. /* allocate stream */
  555. if (code >= 0x1c0 && code <= 0x1df) {
  556. codec_type = CODEC_TYPE_AUDIO;
  557. codec_id = CODEC_ID_MP2;
  558. } else {
  559. codec_type = CODEC_TYPE_VIDEO;
  560. codec_id = CODEC_ID_MPEG1VIDEO;
  561. }
  562. st = av_new_stream(pes->stream, pes->pid);
  563. if (st) {
  564. st->priv_data = pes;
  565. st->codec.codec_type = codec_type;
  566. st->codec.codec_id = codec_id;
  567. pes->st = st;
  568. }
  569. }
  570. pes->state = MPEGTS_PESHEADER_FILL;
  571. pes->total_size = (pes->header[4] << 8) | pes->header[5];
  572. /* NOTE: a zero total size means the PES size is
  573. unbounded */
  574. if (pes->total_size)
  575. pes->total_size += 6;
  576. pes->pes_header_size = pes->header[8] + 9;
  577. } else {
  578. /* otherwise, it should be a table */
  579. /* skip packet */
  580. skip:
  581. pes->state = MPEGTS_SKIP;
  582. continue;
  583. }
  584. }
  585. break;
  586. /**********************************************/
  587. /* PES packing parsing */
  588. case MPEGTS_PESHEADER_FILL:
  589. len = pes->pes_header_size - pes->data_index;
  590. if (len > buf_size)
  591. len = buf_size;
  592. memcpy(pes->header + pes->data_index, p, len);
  593. pes->data_index += len;
  594. p += len;
  595. buf_size -= len;
  596. if (pes->data_index == pes->pes_header_size) {
  597. const uint8_t *r;
  598. unsigned int flags;
  599. flags = pes->header[7];
  600. r = pes->header + 9;
  601. pes->pts = AV_NOPTS_VALUE;
  602. pes->dts = AV_NOPTS_VALUE;
  603. if ((flags & 0xc0) == 0x80) {
  604. pes->pts = get_pts(r);
  605. r += 5;
  606. } else if ((flags & 0xc0) == 0xc0) {
  607. pes->pts = get_pts(r);
  608. r += 5;
  609. pes->dts = get_pts(r);
  610. r += 5;
  611. }
  612. /* we got the full header. We parse it and get the payload */
  613. pes->state = MPEGTS_PAYLOAD;
  614. }
  615. break;
  616. case MPEGTS_PAYLOAD:
  617. if (pes->total_size) {
  618. len = pes->total_size - pes->data_index;
  619. if (len > buf_size)
  620. len = buf_size;
  621. } else {
  622. len = buf_size;
  623. }
  624. if (len > 0) {
  625. AVPacket *pkt = ts->pkt;
  626. if (pes->st && av_new_packet(pkt, len) == 0) {
  627. memcpy(pkt->data, p, len);
  628. pkt->stream_index = pes->st->index;
  629. pkt->pts = pes->pts;
  630. /* reset pts values */
  631. pes->pts = AV_NOPTS_VALUE;
  632. pes->dts = AV_NOPTS_VALUE;
  633. ts->stop_parse = 1;
  634. return;
  635. }
  636. }
  637. buf_size = 0;
  638. break;
  639. case MPEGTS_SKIP:
  640. buf_size = 0;
  641. break;
  642. }
  643. }
  644. }
  645. static int add_pes_stream(AVFormatContext *s, int pid)
  646. {
  647. MpegTSContext *ts = s->priv_data;
  648. MpegTSFilter *tss;
  649. PESContext *pes;
  650. /* if no pid found, then add a pid context */
  651. pes = av_mallocz(sizeof(PESContext));
  652. if (!pes)
  653. return -1;
  654. pes->stream = s;
  655. pes->pid = pid;
  656. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  657. if (!tss) {
  658. av_free(pes);
  659. return -1;
  660. }
  661. return 0;
  662. }
  663. /* handle one TS packet */
  664. static void handle_packet(AVFormatContext *s, uint8_t *packet)
  665. {
  666. MpegTSContext *ts = s->priv_data;
  667. MpegTSFilter *tss;
  668. int len, pid, cc, cc_ok, afc, is_start;
  669. const uint8_t *p, *p_end;
  670. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  671. is_start = packet[1] & 0x40;
  672. tss = ts->pids[pid];
  673. if (ts->auto_guess && tss == NULL && is_start) {
  674. add_pes_stream(s, pid);
  675. tss = ts->pids[pid];
  676. }
  677. if (!tss)
  678. return;
  679. /* continuity check (currently not used) */
  680. cc = (packet[3] & 0xf);
  681. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  682. tss->last_cc = cc;
  683. /* skip adaptation field */
  684. afc = (packet[3] >> 4) & 3;
  685. p = packet + 4;
  686. if (afc == 0) /* reserved value */
  687. return;
  688. if (afc == 2) /* adaptation field only */
  689. return;
  690. if (afc == 3) {
  691. /* skip adapation field */
  692. p += p[0] + 1;
  693. }
  694. /* if past the end of packet, ignore */
  695. p_end = packet + TS_PACKET_SIZE;
  696. if (p >= p_end)
  697. return;
  698. if (tss->type == MPEGTS_SECTION) {
  699. if (is_start) {
  700. /* pointer field present */
  701. len = *p++;
  702. if (p + len > p_end)
  703. return;
  704. if (len && cc_ok) {
  705. /* write remaning section bytes */
  706. write_section_data(s, tss,
  707. p, len, 0);
  708. }
  709. p += len;
  710. if (p < p_end) {
  711. write_section_data(s, tss,
  712. p, p_end - p, 1);
  713. }
  714. } else {
  715. if (cc_ok) {
  716. write_section_data(s, tss,
  717. p, p_end - p, 0);
  718. }
  719. }
  720. } else {
  721. tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
  722. p, p_end - p, is_start);
  723. }
  724. }
  725. static int handle_packets(AVFormatContext *s, int nb_packets)
  726. {
  727. MpegTSContext *ts = s->priv_data;
  728. ByteIOContext *pb = &s->pb;
  729. uint8_t packet[TS_FEC_PACKET_SIZE];
  730. int packet_num, len;
  731. ts->stop_parse = 0;
  732. packet_num = 0;
  733. for(;;) {
  734. if (ts->stop_parse)
  735. break;
  736. packet_num++;
  737. if (nb_packets != 0 && packet_num >= nb_packets)
  738. break;
  739. len = get_buffer(pb, packet, ts->raw_packet_size);
  740. if (len != ts->raw_packet_size)
  741. return AVERROR_IO;
  742. /* check paquet sync byte */
  743. /* XXX: accept to resync ? */
  744. if (packet[0] != 0x47)
  745. return AVERROR_INVALIDDATA;
  746. handle_packet(s, packet);
  747. }
  748. return 0;
  749. }
  750. static int mpegts_probe(AVProbeData *p)
  751. {
  752. int size;
  753. size = get_packet_size(p->buf, p->buf_size);
  754. if (size < 0)
  755. return 0;
  756. return AVPROBE_SCORE_MAX - 1;
  757. }
  758. void set_service_cb(void *opaque, int ret)
  759. {
  760. MpegTSContext *ts = opaque;
  761. ts->set_service_ret = ret;
  762. ts->stop_parse = 1;
  763. }
  764. static int mpegts_read_header(AVFormatContext *s,
  765. AVFormatParameters *ap)
  766. {
  767. MpegTSContext *ts = s->priv_data;
  768. ByteIOContext *pb = &s->pb;
  769. uint8_t buf[1024];
  770. int len;
  771. int64_t pos;
  772. MpegTSService *service;
  773. /* read the first 1024 bytes to get packet size */
  774. pos = url_ftell(pb);
  775. len = get_buffer(pb, buf, sizeof(buf));
  776. if (len != sizeof(buf))
  777. goto fail;
  778. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  779. if (ts->raw_packet_size <= 0)
  780. goto fail;
  781. ts->auto_guess = 0;
  782. if (!ts->auto_guess) {
  783. /* first do a scaning to get all the services */
  784. url_fseek(pb, pos, SEEK_SET);
  785. mpegts_scan_sdt(ts);
  786. handle_packets(s, MAX_SCAN_PACKETS);
  787. /* if no service found, no need to do anything */
  788. if (ts->nb_services <= 0)
  789. return -1;
  790. /* now find the info for the first service */
  791. url_fseek(pb, pos, SEEK_SET);
  792. service = ts->services[0];
  793. #ifdef DEBUG_SI
  794. printf("tuning to '%s'\n", service->name);
  795. #endif
  796. /* tune to first service found */
  797. ts->stream = s;
  798. mpegts_set_service(ts, service->sid, set_service_cb, ts);
  799. handle_packets(s, MAX_SCAN_PACKETS);
  800. /* if could not find service, exit */
  801. if (ts->set_service_ret != 0)
  802. return -1;
  803. #ifdef DEBUG_SI
  804. printf("tuning done\n");
  805. #endif
  806. }
  807. url_fseek(pb, pos, SEEK_SET);
  808. return 0;
  809. fail:
  810. return -1;
  811. }
  812. static int mpegts_read_packet(AVFormatContext *s,
  813. AVPacket *pkt)
  814. {
  815. MpegTSContext *ts = s->priv_data;
  816. ts->pkt = pkt;
  817. return handle_packets(s, 0);
  818. }
  819. static int mpegts_read_close(AVFormatContext *s)
  820. {
  821. MpegTSContext *ts = s->priv_data;
  822. int i;
  823. for(i=0;i<NB_PID_MAX;i++)
  824. av_free(ts->pids[i]);
  825. return 0;
  826. }
  827. AVInputFormat mpegts_demux = {
  828. "mpegts",
  829. "MPEG2 transport stream format",
  830. sizeof(MpegTSContext),
  831. mpegts_probe,
  832. mpegts_read_header,
  833. mpegts_read_packet,
  834. mpegts_read_close,
  835. .flags = AVFMT_NOHEADER | AVFMT_SHOW_IDS,
  836. };
  837. int mpegts_init(void)
  838. {
  839. av_register_input_format(&mpegts_demux);
  840. av_register_output_format(&mpegts_mux);
  841. return 0;
  842. }