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.

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