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.

1053 lines
28KB

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