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.

1448 lines
41KB

  1. /*
  2. * MPEG2 transport stream (aka DVB) demuxer
  3. * Copyright (c) 2002-2003 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "crc.h"
  23. #include "mpegts.h"
  24. #include "allformats.h"
  25. //#define DEBUG_SI
  26. //#define DEBUG_SEEK
  27. /* 1.0 second at 24Mbit/s */
  28. #define MAX_SCAN_PACKETS 32000
  29. /* maximum size in which we look for synchronisation if
  30. synchronisation is lost */
  31. #define MAX_RESYNC_SIZE 4096
  32. typedef struct PESContext PESContext;
  33. static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int stream_type);
  34. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
  35. enum MpegTSFilterType {
  36. MPEGTS_PES,
  37. MPEGTS_SECTION,
  38. };
  39. typedef void PESCallback(void *opaque, const uint8_t *buf, int len, int is_start);
  40. typedef struct MpegTSPESFilter {
  41. PESCallback *pes_cb;
  42. void *opaque;
  43. } MpegTSPESFilter;
  44. typedef void SectionCallback(void *opaque, const uint8_t *buf, int len);
  45. typedef void SetServiceCallback(void *opaque, int ret);
  46. typedef struct MpegTSSectionFilter {
  47. int section_index;
  48. int section_h_size;
  49. uint8_t *section_buf;
  50. int check_crc:1;
  51. int end_of_section_reached:1;
  52. SectionCallback *section_cb;
  53. void *opaque;
  54. } MpegTSSectionFilter;
  55. typedef struct MpegTSFilter {
  56. int pid;
  57. int last_cc; /* last cc code (-1 if first packet) */
  58. enum MpegTSFilterType type;
  59. union {
  60. MpegTSPESFilter pes_filter;
  61. MpegTSSectionFilter section_filter;
  62. } u;
  63. } MpegTSFilter;
  64. typedef struct MpegTSService {
  65. int running:1;
  66. int sid; /**< MPEG Program Number of stream */
  67. char *provider_name; /**< DVB Network name, "" if not DVB stream */
  68. char *name; /**< DVB Service name, "MPEG Program [sid]" if not DVB stream*/
  69. } MpegTSService;
  70. struct MpegTSContext {
  71. /* user data */
  72. AVFormatContext *stream;
  73. /** raw packet size, including FEC if present */
  74. int raw_packet_size;
  75. /** if true, all pids are analyzed to find streams */
  76. int auto_guess;
  77. /** compute exact PCR for each transport stream packet */
  78. int mpeg2ts_compute_pcr;
  79. int64_t cur_pcr; /**< used to estimate the exact PCR */
  80. int pcr_incr; /**< used to estimate the exact PCR */
  81. int pcr_pid; /**< used to estimate the exact PCR */
  82. /* data needed to handle file based ts */
  83. /** stop parsing loop */
  84. int stop_parse;
  85. /** packet containing Audio/Video data */
  86. AVPacket *pkt;
  87. /******************************************/
  88. /* private mpegts data */
  89. /* scan context */
  90. /** number of PMTs in the last PAT seen */
  91. int nb_services;
  92. /** list of PMTs in the last PAT seen */
  93. MpegTSService **services;
  94. /** filters for various streams specified by PMT + for the PAT and PMT */
  95. MpegTSFilter *pids[NB_PID_MAX];
  96. };
  97. /* TS stream handling */
  98. enum MpegTSState {
  99. MPEGTS_HEADER = 0,
  100. MPEGTS_PESHEADER_FILL,
  101. MPEGTS_PAYLOAD,
  102. MPEGTS_SKIP,
  103. };
  104. /* enough for PES header + length */
  105. #define PES_START_SIZE 9
  106. #define MAX_PES_HEADER_SIZE (9 + 255)
  107. struct PESContext {
  108. int pid;
  109. int stream_type;
  110. MpegTSContext *ts;
  111. AVFormatContext *stream;
  112. AVStream *st;
  113. enum MpegTSState state;
  114. /* used to get the format */
  115. int data_index;
  116. int total_size;
  117. int pes_header_size;
  118. int64_t pts, dts;
  119. uint8_t header[MAX_PES_HEADER_SIZE];
  120. };
  121. /**
  122. * Assembles PES packets out of TS packets, and then calls the "section_cb"
  123. * function when they are complete.
  124. */
  125. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  126. const uint8_t *buf, int buf_size, int is_start)
  127. {
  128. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  129. int len;
  130. if (is_start) {
  131. memcpy(tss->section_buf, buf, buf_size);
  132. tss->section_index = buf_size;
  133. tss->section_h_size = -1;
  134. tss->end_of_section_reached = 0;
  135. } else {
  136. if (tss->end_of_section_reached)
  137. return;
  138. len = 4096 - tss->section_index;
  139. if (buf_size < len)
  140. len = buf_size;
  141. memcpy(tss->section_buf + tss->section_index, buf, len);
  142. tss->section_index += len;
  143. }
  144. /* compute section length if possible */
  145. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  146. len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
  147. if (len > 4096)
  148. return;
  149. tss->section_h_size = len;
  150. }
  151. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  152. tss->end_of_section_reached = 1;
  153. if (!tss->check_crc ||
  154. av_crc(av_crc04C11DB7, -1, tss->section_buf, tss->section_h_size) == 0)
  155. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  156. }
  157. }
  158. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  159. SectionCallback *section_cb, void *opaque,
  160. int check_crc)
  161. {
  162. MpegTSFilter *filter;
  163. MpegTSSectionFilter *sec;
  164. #ifdef DEBUG_SI
  165. av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
  166. #endif
  167. if (pid >= NB_PID_MAX || ts->pids[pid])
  168. return NULL;
  169. filter = av_mallocz(sizeof(MpegTSFilter));
  170. if (!filter)
  171. return NULL;
  172. ts->pids[pid] = filter;
  173. filter->type = MPEGTS_SECTION;
  174. filter->pid = pid;
  175. filter->last_cc = -1;
  176. sec = &filter->u.section_filter;
  177. sec->section_cb = section_cb;
  178. sec->opaque = opaque;
  179. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  180. sec->check_crc = check_crc;
  181. if (!sec->section_buf) {
  182. av_free(filter);
  183. return NULL;
  184. }
  185. return filter;
  186. }
  187. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  188. PESCallback *pes_cb,
  189. void *opaque)
  190. {
  191. MpegTSFilter *filter;
  192. MpegTSPESFilter *pes;
  193. if (pid >= NB_PID_MAX || ts->pids[pid])
  194. return NULL;
  195. filter = av_mallocz(sizeof(MpegTSFilter));
  196. if (!filter)
  197. return NULL;
  198. ts->pids[pid] = filter;
  199. filter->type = MPEGTS_PES;
  200. filter->pid = pid;
  201. filter->last_cc = -1;
  202. pes = &filter->u.pes_filter;
  203. pes->pes_cb = pes_cb;
  204. pes->opaque = opaque;
  205. return filter;
  206. }
  207. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  208. {
  209. int pid;
  210. pid = filter->pid;
  211. if (filter->type == MPEGTS_SECTION)
  212. av_freep(&filter->u.section_filter.section_buf);
  213. else if (filter->type == MPEGTS_PES)
  214. av_freep(&filter->u.pes_filter.opaque);
  215. av_free(filter);
  216. ts->pids[pid] = NULL;
  217. }
  218. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  219. int stat[packet_size];
  220. int i;
  221. int x=0;
  222. int best_score=0;
  223. memset(stat, 0, packet_size*sizeof(int));
  224. for(x=i=0; i<size; i++){
  225. if(buf[i] == 0x47){
  226. stat[x]++;
  227. if(stat[x] > best_score){
  228. best_score= stat[x];
  229. if(index) *index= x;
  230. }
  231. }
  232. x++;
  233. if(x == packet_size) x= 0;
  234. }
  235. return best_score;
  236. }
  237. /* autodetect fec presence. Must have at least 1024 bytes */
  238. static int get_packet_size(const uint8_t *buf, int size)
  239. {
  240. int score, fec_score, dvhs_score;
  241. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  242. return -1;
  243. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  244. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  245. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  246. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  247. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  248. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  249. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  250. else return -1;
  251. }
  252. typedef struct SectionHeader {
  253. uint8_t tid;
  254. uint16_t id;
  255. uint8_t version;
  256. uint8_t sec_num;
  257. uint8_t last_sec_num;
  258. } SectionHeader;
  259. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  260. {
  261. const uint8_t *p;
  262. int c;
  263. p = *pp;
  264. if (p >= p_end)
  265. return -1;
  266. c = *p++;
  267. *pp = p;
  268. return c;
  269. }
  270. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  271. {
  272. const uint8_t *p;
  273. int c;
  274. p = *pp;
  275. if ((p + 1) >= p_end)
  276. return -1;
  277. c = (p[0] << 8) | p[1];
  278. p += 2;
  279. *pp = p;
  280. return c;
  281. }
  282. /* read and allocate a DVB string preceeded by its length */
  283. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  284. {
  285. int len;
  286. const uint8_t *p;
  287. char *str;
  288. p = *pp;
  289. len = get8(&p, p_end);
  290. if (len < 0)
  291. return NULL;
  292. if ((p + len) > p_end)
  293. return NULL;
  294. str = av_malloc(len + 1);
  295. if (!str)
  296. return NULL;
  297. memcpy(str, p, len);
  298. str[len] = '\0';
  299. p += len;
  300. *pp = p;
  301. return str;
  302. }
  303. static int parse_section_header(SectionHeader *h,
  304. const uint8_t **pp, const uint8_t *p_end)
  305. {
  306. int val;
  307. val = get8(pp, p_end);
  308. if (val < 0)
  309. return -1;
  310. h->tid = val;
  311. *pp += 2;
  312. val = get16(pp, p_end);
  313. if (val < 0)
  314. return -1;
  315. h->id = val;
  316. val = get8(pp, p_end);
  317. if (val < 0)
  318. return -1;
  319. h->version = (val >> 1) & 0x1f;
  320. val = get8(pp, p_end);
  321. if (val < 0)
  322. return -1;
  323. h->sec_num = val;
  324. val = get8(pp, p_end);
  325. if (val < 0)
  326. return -1;
  327. h->last_sec_num = val;
  328. return 0;
  329. }
  330. static MpegTSService *new_service(MpegTSContext *ts, int sid,
  331. char *provider_name, char *name)
  332. {
  333. MpegTSService *service=NULL;
  334. int i;
  335. #ifdef DEBUG_SI
  336. av_log(ts->stream, AV_LOG_DEBUG, "new_service: "
  337. "sid=0x%04x provider='%s' name='%s'\n",
  338. sid, provider_name, name);
  339. #endif
  340. for(i=0; i<ts->nb_services; i++)
  341. if(ts->services[i]->sid == sid)
  342. service= ts->services[i];
  343. if(!service){
  344. service = av_mallocz(sizeof(MpegTSService));
  345. if (!service)
  346. return NULL;
  347. dynarray_add(&ts->services, &ts->nb_services, service);
  348. }
  349. service->sid = sid;
  350. assert((!provider_name) == (!name));
  351. if(name){
  352. av_free(service->provider_name);
  353. av_free(service-> name);
  354. service->provider_name = provider_name;
  355. service-> name = name;
  356. }
  357. return service;
  358. }
  359. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  360. {
  361. MpegTSContext *ts = filter->u.section_filter.opaque;
  362. SectionHeader h1, *h = &h1;
  363. PESContext *pes;
  364. AVStream *st;
  365. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  366. int program_info_length, pcr_pid, pid, stream_type;
  367. int desc_list_len, desc_len, desc_tag;
  368. int comp_page = 0, anc_page = 0; /* initialize to kill warnings */
  369. char language[4] = {0}; /* initialize to kill warnings */
  370. #ifdef DEBUG_SI
  371. av_log(ts->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
  372. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  373. #endif
  374. p_end = section + section_len - 4;
  375. p = section;
  376. if (parse_section_header(h, &p, p_end) < 0)
  377. return;
  378. #ifdef DEBUG_SI
  379. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n",
  380. h->id, h->sec_num, h->last_sec_num);
  381. #endif
  382. if (h->tid != PMT_TID)
  383. return;
  384. pcr_pid = get16(&p, p_end) & 0x1fff;
  385. if (pcr_pid < 0)
  386. return;
  387. ts->pcr_pid = pcr_pid;
  388. #ifdef DEBUG_SI
  389. av_log(ts->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
  390. #endif
  391. program_info_length = get16(&p, p_end) & 0xfff;
  392. if (program_info_length < 0)
  393. return;
  394. p += program_info_length;
  395. if (p >= p_end)
  396. return;
  397. for(;;) {
  398. language[0] = 0;
  399. st = 0;
  400. stream_type = get8(&p, p_end);
  401. if (stream_type < 0)
  402. break;
  403. pid = get16(&p, p_end) & 0x1fff;
  404. if (pid < 0)
  405. break;
  406. desc_list_len = get16(&p, p_end) & 0xfff;
  407. if (desc_list_len < 0)
  408. break;
  409. desc_list_end = p + desc_list_len;
  410. if (desc_list_end > p_end)
  411. break;
  412. for(;;) {
  413. desc_tag = get8(&p, desc_list_end);
  414. if (desc_tag < 0)
  415. break;
  416. if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
  417. if((desc_tag == 0x6A) || (desc_tag == 0x7A)) {
  418. /*assume DVB AC-3 Audio*/
  419. stream_type = STREAM_TYPE_AUDIO_AC3;
  420. } else if(desc_tag == 0x7B) {
  421. /* DVB DTS audio */
  422. stream_type = STREAM_TYPE_AUDIO_DTS;
  423. }
  424. }
  425. desc_len = get8(&p, desc_list_end);
  426. desc_end = p + desc_len;
  427. if (desc_end > desc_list_end)
  428. break;
  429. #ifdef DEBUG_SI
  430. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  431. desc_tag, desc_len);
  432. #endif
  433. switch(desc_tag) {
  434. case DVB_SUBT_DESCID:
  435. if (stream_type == STREAM_TYPE_PRIVATE_DATA)
  436. stream_type = STREAM_TYPE_SUBTITLE_DVB;
  437. language[0] = get8(&p, desc_end);
  438. language[1] = get8(&p, desc_end);
  439. language[2] = get8(&p, desc_end);
  440. language[3] = 0;
  441. get8(&p, desc_end);
  442. comp_page = get16(&p, desc_end);
  443. anc_page = get16(&p, desc_end);
  444. break;
  445. case 0x0a: /* ISO 639 language descriptor */
  446. language[0] = get8(&p, desc_end);
  447. language[1] = get8(&p, desc_end);
  448. language[2] = get8(&p, desc_end);
  449. language[3] = 0;
  450. break;
  451. default:
  452. break;
  453. }
  454. p = desc_end;
  455. }
  456. p = desc_list_end;
  457. #ifdef DEBUG_SI
  458. av_log(ts->stream, AV_LOG_DEBUG, "stream_type=%d pid=0x%x\n",
  459. stream_type, pid);
  460. #endif
  461. /* now create ffmpeg stream */
  462. switch(stream_type) {
  463. case STREAM_TYPE_AUDIO_MPEG1:
  464. case STREAM_TYPE_AUDIO_MPEG2:
  465. case STREAM_TYPE_VIDEO_MPEG1:
  466. case STREAM_TYPE_VIDEO_MPEG2:
  467. case STREAM_TYPE_VIDEO_MPEG4:
  468. case STREAM_TYPE_VIDEO_H264:
  469. case STREAM_TYPE_VIDEO_VC1:
  470. case STREAM_TYPE_AUDIO_AAC:
  471. case STREAM_TYPE_AUDIO_AC3:
  472. case STREAM_TYPE_AUDIO_DTS:
  473. case STREAM_TYPE_SUBTITLE_DVB:
  474. if(ts->pids[pid]){
  475. assert(ts->pids[pid].type == MPEGTS_PES);
  476. pes= ts->pids[pid]->u.pes_filter.opaque;
  477. st= pes->st;
  478. }else{
  479. pes = add_pes_stream(ts, pid, stream_type);
  480. if (pes)
  481. st = new_pes_av_stream(pes, 0);
  482. }
  483. break;
  484. default:
  485. /* we ignore the other streams */
  486. break;
  487. }
  488. if (st) {
  489. if (language[0] != 0) {
  490. st->language[0] = language[0];
  491. st->language[1] = language[1];
  492. st->language[2] = language[2];
  493. st->language[3] = language[3];
  494. }
  495. if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
  496. st->codec->sub_id = (anc_page << 16) | comp_page;
  497. }
  498. }
  499. }
  500. /* all parameters are there */
  501. ts->stop_parse++;
  502. mpegts_close_filter(ts, filter);
  503. }
  504. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  505. {
  506. MpegTSContext *ts = filter->u.section_filter.opaque;
  507. SectionHeader h1, *h = &h1;
  508. const uint8_t *p, *p_end;
  509. int sid, pmt_pid;
  510. #ifdef DEBUG_SI
  511. av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
  512. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  513. #endif
  514. p_end = section + section_len - 4;
  515. p = section;
  516. if (parse_section_header(h, &p, p_end) < 0)
  517. return;
  518. if (h->tid != PAT_TID)
  519. return;
  520. for(;;) {
  521. sid = get16(&p, p_end);
  522. if (sid < 0)
  523. break;
  524. pmt_pid = get16(&p, p_end) & 0x1fff;
  525. if (pmt_pid < 0)
  526. break;
  527. #ifdef DEBUG_SI
  528. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  529. #endif
  530. if (sid == 0x0000) {
  531. /* NIT info */
  532. } else {
  533. new_service(ts, sid, NULL, NULL);
  534. ts->stop_parse--;
  535. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  536. }
  537. }
  538. /* not found */
  539. ts->stop_parse++;
  540. mpegts_close_filter(ts, filter);
  541. }
  542. static void mpegts_set_service(MpegTSContext *ts)
  543. {
  544. mpegts_open_section_filter(ts, PAT_PID,
  545. pat_cb, ts, 1);
  546. }
  547. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  548. {
  549. MpegTSContext *ts = filter->u.section_filter.opaque;
  550. SectionHeader h1, *h = &h1;
  551. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  552. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  553. char *name, *provider_name;
  554. #ifdef DEBUG_SI
  555. av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
  556. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  557. #endif
  558. p_end = section + section_len - 4;
  559. p = section;
  560. if (parse_section_header(h, &p, p_end) < 0)
  561. return;
  562. if (h->tid != SDT_TID)
  563. return;
  564. onid = get16(&p, p_end);
  565. if (onid < 0)
  566. return;
  567. val = get8(&p, p_end);
  568. if (val < 0)
  569. return;
  570. for(;;) {
  571. sid = get16(&p, p_end);
  572. if (sid < 0)
  573. break;
  574. val = get8(&p, p_end);
  575. if (val < 0)
  576. break;
  577. desc_list_len = get16(&p, p_end) & 0xfff;
  578. if (desc_list_len < 0)
  579. break;
  580. desc_list_end = p + desc_list_len;
  581. if (desc_list_end > p_end)
  582. break;
  583. for(;;) {
  584. desc_tag = get8(&p, desc_list_end);
  585. if (desc_tag < 0)
  586. break;
  587. desc_len = get8(&p, desc_list_end);
  588. desc_end = p + desc_len;
  589. if (desc_end > desc_list_end)
  590. break;
  591. #ifdef DEBUG_SI
  592. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  593. desc_tag, desc_len);
  594. #endif
  595. switch(desc_tag) {
  596. case 0x48:
  597. service_type = get8(&p, p_end);
  598. if (service_type < 0)
  599. break;
  600. provider_name = getstr8(&p, p_end);
  601. if (!provider_name)
  602. break;
  603. name = getstr8(&p, p_end);
  604. if (!name)
  605. break;
  606. new_service(ts, sid, provider_name, name);
  607. break;
  608. default:
  609. break;
  610. }
  611. p = desc_end;
  612. }
  613. p = desc_list_end;
  614. }
  615. }
  616. /* scan services in a transport stream by looking at the SDT */
  617. static void mpegts_scan_sdt(MpegTSContext *ts)
  618. {
  619. mpegts_open_section_filter(ts, SDT_PID,
  620. sdt_cb, ts, 1);
  621. }
  622. static int64_t get_pts(const uint8_t *p)
  623. {
  624. int64_t pts;
  625. int val;
  626. pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  627. val = (p[1] << 8) | p[2];
  628. pts |= (int64_t)(val >> 1) << 15;
  629. val = (p[3] << 8) | p[4];
  630. pts |= (int64_t)(val >> 1);
  631. return pts;
  632. }
  633. /* return non zero if a packet could be constructed */
  634. static void mpegts_push_data(MpegTSFilter *filter,
  635. const uint8_t *buf, int buf_size, int is_start)
  636. {
  637. PESContext *pes = filter->u.pes_filter.opaque;
  638. MpegTSContext *ts = pes->ts;
  639. const uint8_t *p;
  640. int len, code;
  641. if(!ts->pkt)
  642. return;
  643. if (is_start) {
  644. pes->state = MPEGTS_HEADER;
  645. pes->data_index = 0;
  646. }
  647. p = buf;
  648. while (buf_size > 0) {
  649. switch(pes->state) {
  650. case MPEGTS_HEADER:
  651. len = PES_START_SIZE - pes->data_index;
  652. if (len > buf_size)
  653. len = buf_size;
  654. memcpy(pes->header + pes->data_index, p, len);
  655. pes->data_index += len;
  656. p += len;
  657. buf_size -= len;
  658. if (pes->data_index == PES_START_SIZE) {
  659. /* we got all the PES or section header. We can now
  660. decide */
  661. #if 0
  662. av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
  663. #endif
  664. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  665. pes->header[2] == 0x01) {
  666. /* it must be an mpeg2 PES stream */
  667. code = pes->header[3] | 0x100;
  668. if (!((code >= 0x1c0 && code <= 0x1df) ||
  669. (code >= 0x1e0 && code <= 0x1ef) ||
  670. (code == 0x1bd) || (code == 0x1fd)))
  671. goto skip;
  672. if (!pes->st) {
  673. /* allocate stream */
  674. new_pes_av_stream(pes, code);
  675. }
  676. pes->state = MPEGTS_PESHEADER_FILL;
  677. pes->total_size = (pes->header[4] << 8) | pes->header[5];
  678. /* NOTE: a zero total size means the PES size is
  679. unbounded */
  680. if (pes->total_size)
  681. pes->total_size += 6;
  682. pes->pes_header_size = pes->header[8] + 9;
  683. } else {
  684. /* otherwise, it should be a table */
  685. /* skip packet */
  686. skip:
  687. pes->state = MPEGTS_SKIP;
  688. continue;
  689. }
  690. }
  691. break;
  692. /**********************************************/
  693. /* PES packing parsing */
  694. case MPEGTS_PESHEADER_FILL:
  695. len = pes->pes_header_size - pes->data_index;
  696. if (len > buf_size)
  697. len = buf_size;
  698. memcpy(pes->header + pes->data_index, p, len);
  699. pes->data_index += len;
  700. p += len;
  701. buf_size -= len;
  702. if (pes->data_index == pes->pes_header_size) {
  703. const uint8_t *r;
  704. unsigned int flags;
  705. flags = pes->header[7];
  706. r = pes->header + 9;
  707. pes->pts = AV_NOPTS_VALUE;
  708. pes->dts = AV_NOPTS_VALUE;
  709. if ((flags & 0xc0) == 0x80) {
  710. pes->pts = get_pts(r);
  711. r += 5;
  712. } else if ((flags & 0xc0) == 0xc0) {
  713. pes->pts = get_pts(r);
  714. r += 5;
  715. pes->dts = get_pts(r);
  716. r += 5;
  717. }
  718. /* we got the full header. We parse it and get the payload */
  719. pes->state = MPEGTS_PAYLOAD;
  720. }
  721. break;
  722. case MPEGTS_PAYLOAD:
  723. if (pes->total_size) {
  724. len = pes->total_size - pes->data_index;
  725. if (len > buf_size)
  726. len = buf_size;
  727. } else {
  728. len = buf_size;
  729. }
  730. if (len > 0) {
  731. AVPacket *pkt = ts->pkt;
  732. if (pes->st && av_new_packet(pkt, len) == 0) {
  733. memcpy(pkt->data, p, len);
  734. pkt->stream_index = pes->st->index;
  735. pkt->pts = pes->pts;
  736. pkt->dts = pes->dts;
  737. /* reset pts values */
  738. pes->pts = AV_NOPTS_VALUE;
  739. pes->dts = AV_NOPTS_VALUE;
  740. ts->stop_parse = 1;
  741. return;
  742. }
  743. }
  744. buf_size = 0;
  745. break;
  746. case MPEGTS_SKIP:
  747. buf_size = 0;
  748. break;
  749. }
  750. }
  751. }
  752. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
  753. {
  754. AVStream *st;
  755. int codec_type, codec_id;
  756. switch(pes->stream_type){
  757. case STREAM_TYPE_AUDIO_MPEG1:
  758. case STREAM_TYPE_AUDIO_MPEG2:
  759. codec_type = CODEC_TYPE_AUDIO;
  760. codec_id = CODEC_ID_MP3;
  761. break;
  762. case STREAM_TYPE_VIDEO_MPEG1:
  763. case STREAM_TYPE_VIDEO_MPEG2:
  764. codec_type = CODEC_TYPE_VIDEO;
  765. codec_id = CODEC_ID_MPEG2VIDEO;
  766. break;
  767. case STREAM_TYPE_VIDEO_MPEG4:
  768. codec_type = CODEC_TYPE_VIDEO;
  769. codec_id = CODEC_ID_MPEG4;
  770. break;
  771. case STREAM_TYPE_VIDEO_H264:
  772. codec_type = CODEC_TYPE_VIDEO;
  773. codec_id = CODEC_ID_H264;
  774. break;
  775. case STREAM_TYPE_VIDEO_VC1:
  776. codec_type = CODEC_TYPE_VIDEO;
  777. codec_id = CODEC_ID_VC1;
  778. break;
  779. case STREAM_TYPE_AUDIO_AAC:
  780. codec_type = CODEC_TYPE_AUDIO;
  781. codec_id = CODEC_ID_AAC;
  782. break;
  783. case STREAM_TYPE_AUDIO_AC3:
  784. codec_type = CODEC_TYPE_AUDIO;
  785. codec_id = CODEC_ID_AC3;
  786. break;
  787. case STREAM_TYPE_AUDIO_DTS:
  788. codec_type = CODEC_TYPE_AUDIO;
  789. codec_id = CODEC_ID_DTS;
  790. break;
  791. case STREAM_TYPE_SUBTITLE_DVB:
  792. codec_type = CODEC_TYPE_SUBTITLE;
  793. codec_id = CODEC_ID_DVB_SUBTITLE;
  794. break;
  795. default:
  796. if (code >= 0x1c0 && code <= 0x1df) {
  797. codec_type = CODEC_TYPE_AUDIO;
  798. codec_id = CODEC_ID_MP2;
  799. } else if (code == 0x1bd) {
  800. codec_type = CODEC_TYPE_AUDIO;
  801. codec_id = CODEC_ID_AC3;
  802. } else {
  803. codec_type = CODEC_TYPE_VIDEO;
  804. codec_id = CODEC_ID_MPEG1VIDEO;
  805. }
  806. break;
  807. }
  808. st = av_new_stream(pes->stream, pes->pid);
  809. if (st) {
  810. av_set_pts_info(st, 33, 1, 90000);
  811. st->priv_data = pes;
  812. st->codec->codec_type = codec_type;
  813. st->codec->codec_id = codec_id;
  814. st->need_parsing = AVSTREAM_PARSE_FULL;
  815. pes->st = st;
  816. }
  817. return st;
  818. }
  819. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int stream_type)
  820. {
  821. MpegTSFilter *tss;
  822. PESContext *pes;
  823. /* if no pid found, then add a pid context */
  824. pes = av_mallocz(sizeof(PESContext));
  825. if (!pes)
  826. return 0;
  827. pes->ts = ts;
  828. pes->stream = ts->stream;
  829. pes->pid = pid;
  830. pes->stream_type = stream_type;
  831. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  832. if (!tss) {
  833. av_free(pes);
  834. return 0;
  835. }
  836. return pes;
  837. }
  838. /* handle one TS packet */
  839. static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
  840. {
  841. AVFormatContext *s = ts->stream;
  842. MpegTSFilter *tss;
  843. int len, pid, cc, cc_ok, afc, is_start;
  844. const uint8_t *p, *p_end;
  845. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  846. is_start = packet[1] & 0x40;
  847. tss = ts->pids[pid];
  848. if (ts->auto_guess && tss == NULL && is_start) {
  849. add_pes_stream(ts, pid, 0);
  850. tss = ts->pids[pid];
  851. }
  852. if (!tss)
  853. return;
  854. /* continuity check (currently not used) */
  855. cc = (packet[3] & 0xf);
  856. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  857. tss->last_cc = cc;
  858. /* skip adaptation field */
  859. afc = (packet[3] >> 4) & 3;
  860. p = packet + 4;
  861. if (afc == 0) /* reserved value */
  862. return;
  863. if (afc == 2) /* adaptation field only */
  864. return;
  865. if (afc == 3) {
  866. /* skip adapation field */
  867. p += p[0] + 1;
  868. }
  869. /* if past the end of packet, ignore */
  870. p_end = packet + TS_PACKET_SIZE;
  871. if (p >= p_end)
  872. return;
  873. if (tss->type == MPEGTS_SECTION) {
  874. if (is_start) {
  875. /* pointer field present */
  876. len = *p++;
  877. if (p + len > p_end)
  878. return;
  879. if (len && cc_ok) {
  880. /* write remaining section bytes */
  881. write_section_data(s, tss,
  882. p, len, 0);
  883. /* check whether filter has been closed */
  884. if (!ts->pids[pid])
  885. return;
  886. }
  887. p += len;
  888. if (p < p_end) {
  889. write_section_data(s, tss,
  890. p, p_end - p, 1);
  891. }
  892. } else {
  893. if (cc_ok) {
  894. write_section_data(s, tss,
  895. p, p_end - p, 0);
  896. }
  897. }
  898. } else {
  899. tss->u.pes_filter.pes_cb(tss,
  900. p, p_end - p, is_start);
  901. }
  902. }
  903. /* XXX: try to find a better synchro over several packets (use
  904. get_packet_size() ?) */
  905. static int mpegts_resync(ByteIOContext *pb)
  906. {
  907. int c, i;
  908. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  909. c = url_fgetc(pb);
  910. if (c < 0)
  911. return -1;
  912. if (c == 0x47) {
  913. url_fseek(pb, -1, SEEK_CUR);
  914. return 0;
  915. }
  916. }
  917. /* no sync found */
  918. return -1;
  919. }
  920. /* return -1 if error or EOF. Return 0 if OK. */
  921. static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
  922. {
  923. int skip, len;
  924. for(;;) {
  925. len = get_buffer(pb, buf, TS_PACKET_SIZE);
  926. if (len != TS_PACKET_SIZE)
  927. return AVERROR_IO;
  928. /* check paquet sync byte */
  929. if (buf[0] != 0x47) {
  930. /* find a new packet start */
  931. url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  932. if (mpegts_resync(pb) < 0)
  933. return AVERROR_INVALIDDATA;
  934. else
  935. continue;
  936. } else {
  937. skip = raw_packet_size - TS_PACKET_SIZE;
  938. if (skip > 0)
  939. url_fskip(pb, skip);
  940. break;
  941. }
  942. }
  943. return 0;
  944. }
  945. static int handle_packets(MpegTSContext *ts, int nb_packets)
  946. {
  947. AVFormatContext *s = ts->stream;
  948. ByteIOContext *pb = &s->pb;
  949. uint8_t packet[TS_PACKET_SIZE];
  950. int packet_num, ret;
  951. ts->stop_parse = 0;
  952. packet_num = 0;
  953. for(;;) {
  954. if (ts->stop_parse>0)
  955. break;
  956. packet_num++;
  957. if (nb_packets != 0 && packet_num >= nb_packets)
  958. break;
  959. ret = read_packet(pb, packet, ts->raw_packet_size);
  960. if (ret != 0)
  961. return ret;
  962. handle_packet(ts, packet);
  963. }
  964. return 0;
  965. }
  966. static int mpegts_probe(AVProbeData *p)
  967. {
  968. #if 1
  969. const int size= p->buf_size;
  970. int score, fec_score, dvhs_score;
  971. #define CHECK_COUNT 10
  972. if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
  973. return -1;
  974. score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
  975. dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE *CHECK_COUNT, TS_DVHS_PACKET_SIZE, NULL);
  976. fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
  977. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  978. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  979. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  980. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  981. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  982. else return -1;
  983. #else
  984. /* only use the extension for safer guess */
  985. if (match_ext(p->filename, "ts"))
  986. return AVPROBE_SCORE_MAX;
  987. else
  988. return 0;
  989. #endif
  990. }
  991. /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
  992. (-1) if not available */
  993. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  994. const uint8_t *packet)
  995. {
  996. int afc, len, flags;
  997. const uint8_t *p;
  998. unsigned int v;
  999. afc = (packet[3] >> 4) & 3;
  1000. if (afc <= 1)
  1001. return -1;
  1002. p = packet + 4;
  1003. len = p[0];
  1004. p++;
  1005. if (len == 0)
  1006. return -1;
  1007. flags = *p++;
  1008. len--;
  1009. if (!(flags & 0x10))
  1010. return -1;
  1011. if (len < 6)
  1012. return -1;
  1013. v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  1014. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1015. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1016. return 0;
  1017. }
  1018. static int mpegts_read_header(AVFormatContext *s,
  1019. AVFormatParameters *ap)
  1020. {
  1021. MpegTSContext *ts = s->priv_data;
  1022. ByteIOContext *pb = &s->pb;
  1023. uint8_t buf[1024];
  1024. int len;
  1025. int64_t pos;
  1026. if (ap) {
  1027. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1028. if(ap->mpeg2ts_raw){
  1029. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1030. return -1;
  1031. }
  1032. }
  1033. /* read the first 1024 bytes to get packet size */
  1034. pos = url_ftell(pb);
  1035. len = get_buffer(pb, buf, sizeof(buf));
  1036. if (len != sizeof(buf))
  1037. goto fail;
  1038. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1039. if (ts->raw_packet_size <= 0)
  1040. goto fail;
  1041. ts->stream = s;
  1042. ts->auto_guess = 0;
  1043. if (s->iformat == &mpegts_demuxer) {
  1044. /* normal demux */
  1045. /* first do a scaning to get all the services */
  1046. url_fseek(pb, pos, SEEK_SET);
  1047. mpegts_scan_sdt(ts);
  1048. if (ts->nb_services <= 0 && 0) {
  1049. /* raw transport stream */
  1050. ts->auto_guess = 1;
  1051. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1052. goto do_pcr;
  1053. }
  1054. mpegts_set_service(ts);
  1055. handle_packets(ts, s->probesize);
  1056. /* if could not find service, enable auto_guess */
  1057. ts->auto_guess = 1;
  1058. #ifdef DEBUG_SI
  1059. av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
  1060. #endif
  1061. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1062. } else {
  1063. AVStream *st;
  1064. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1065. int64_t pcrs[2], pcr_h;
  1066. int packet_count[2];
  1067. uint8_t packet[TS_PACKET_SIZE];
  1068. /* only read packets */
  1069. do_pcr:
  1070. st = av_new_stream(s, 0);
  1071. if (!st)
  1072. goto fail;
  1073. av_set_pts_info(st, 60, 1, 27000000);
  1074. st->codec->codec_type = CODEC_TYPE_DATA;
  1075. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1076. /* we iterate until we find two PCRs to estimate the bitrate */
  1077. pcr_pid = -1;
  1078. nb_pcrs = 0;
  1079. nb_packets = 0;
  1080. for(;;) {
  1081. ret = read_packet(&s->pb, packet, ts->raw_packet_size);
  1082. if (ret < 0)
  1083. return -1;
  1084. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  1085. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1086. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1087. pcr_pid = pid;
  1088. packet_count[nb_pcrs] = nb_packets;
  1089. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1090. nb_pcrs++;
  1091. if (nb_pcrs >= 2)
  1092. break;
  1093. }
  1094. nb_packets++;
  1095. }
  1096. ts->pcr_pid = pcr_pid;
  1097. /* NOTE1: the bitrate is computed without the FEC */
  1098. /* NOTE2: it is only the bitrate of the start of the stream */
  1099. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1100. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1101. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1102. st->codec->bit_rate = s->bit_rate;
  1103. st->start_time = ts->cur_pcr;
  1104. #if 0
  1105. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1106. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1107. #endif
  1108. }
  1109. url_fseek(pb, pos, SEEK_SET);
  1110. return 0;
  1111. fail:
  1112. return -1;
  1113. }
  1114. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1115. static int mpegts_raw_read_packet(AVFormatContext *s,
  1116. AVPacket *pkt)
  1117. {
  1118. MpegTSContext *ts = s->priv_data;
  1119. int ret, i;
  1120. int64_t pcr_h, next_pcr_h, pos;
  1121. int pcr_l, next_pcr_l;
  1122. uint8_t pcr_buf[12];
  1123. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1124. return AVERROR(ENOMEM);
  1125. pkt->pos= url_ftell(&s->pb);
  1126. ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
  1127. if (ret < 0) {
  1128. av_free_packet(pkt);
  1129. return ret;
  1130. }
  1131. if (ts->mpeg2ts_compute_pcr) {
  1132. /* compute exact PCR for each packet */
  1133. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1134. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1135. pos = url_ftell(&s->pb);
  1136. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1137. url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1138. get_buffer(&s->pb, pcr_buf, 12);
  1139. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1140. /* XXX: not precise enough */
  1141. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1142. (i + 1);
  1143. break;
  1144. }
  1145. }
  1146. url_fseek(&s->pb, pos, SEEK_SET);
  1147. /* no next PCR found: we use previous increment */
  1148. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1149. }
  1150. pkt->pts = ts->cur_pcr;
  1151. pkt->duration = ts->pcr_incr;
  1152. ts->cur_pcr += ts->pcr_incr;
  1153. }
  1154. pkt->stream_index = 0;
  1155. return 0;
  1156. }
  1157. static int mpegts_read_packet(AVFormatContext *s,
  1158. AVPacket *pkt)
  1159. {
  1160. MpegTSContext *ts = s->priv_data;
  1161. ts->pkt = pkt;
  1162. return handle_packets(ts, 0);
  1163. }
  1164. static int mpegts_read_close(AVFormatContext *s)
  1165. {
  1166. MpegTSContext *ts = s->priv_data;
  1167. int i;
  1168. for(i=0;i<NB_PID_MAX;i++)
  1169. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1170. for(i = 0; i < ts->nb_services; i++){
  1171. av_free(ts->services[i]->provider_name);
  1172. av_free(ts->services[i]->name);
  1173. av_free(ts->services[i]);
  1174. }
  1175. av_freep(&ts->services);
  1176. return 0;
  1177. }
  1178. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1179. int64_t *ppos, int64_t pos_limit)
  1180. {
  1181. MpegTSContext *ts = s->priv_data;
  1182. int64_t pos, timestamp;
  1183. uint8_t buf[TS_PACKET_SIZE];
  1184. int pcr_l, pid;
  1185. const int find_next= 1;
  1186. pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
  1187. if (find_next) {
  1188. for(;;) {
  1189. url_fseek(&s->pb, pos, SEEK_SET);
  1190. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1191. return AV_NOPTS_VALUE;
  1192. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1193. if (pid == ts->pcr_pid &&
  1194. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1195. break;
  1196. }
  1197. pos += ts->raw_packet_size;
  1198. }
  1199. } else {
  1200. for(;;) {
  1201. pos -= ts->raw_packet_size;
  1202. if (pos < 0)
  1203. return AV_NOPTS_VALUE;
  1204. url_fseek(&s->pb, pos, SEEK_SET);
  1205. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1206. return AV_NOPTS_VALUE;
  1207. pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1208. if (pid == ts->pcr_pid &&
  1209. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1210. break;
  1211. }
  1212. }
  1213. }
  1214. *ppos = pos;
  1215. return timestamp;
  1216. }
  1217. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1218. MpegTSContext *ts = s->priv_data;
  1219. uint8_t buf[TS_PACKET_SIZE];
  1220. int64_t pos;
  1221. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1222. return -1;
  1223. pos= url_ftell(&s->pb);
  1224. for(;;) {
  1225. url_fseek(&s->pb, pos, SEEK_SET);
  1226. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1227. return -1;
  1228. // pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1229. if(buf[1] & 0x40) break;
  1230. pos += ts->raw_packet_size;
  1231. }
  1232. url_fseek(&s->pb, pos, SEEK_SET);
  1233. return 0;
  1234. }
  1235. /**************************************************************/
  1236. /* parsing functions - called from other demuxers such as RTP */
  1237. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1238. {
  1239. MpegTSContext *ts;
  1240. ts = av_mallocz(sizeof(MpegTSContext));
  1241. if (!ts)
  1242. return NULL;
  1243. /* no stream case, currently used by RTP */
  1244. ts->raw_packet_size = TS_PACKET_SIZE;
  1245. ts->stream = s;
  1246. ts->auto_guess = 1;
  1247. return ts;
  1248. }
  1249. /* return the consumed length if a packet was output, or -1 if no
  1250. packet is output */
  1251. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1252. const uint8_t *buf, int len)
  1253. {
  1254. int len1;
  1255. len1 = len;
  1256. ts->pkt = pkt;
  1257. ts->stop_parse = 0;
  1258. for(;;) {
  1259. if (ts->stop_parse>0)
  1260. break;
  1261. if (len < TS_PACKET_SIZE)
  1262. return -1;
  1263. if (buf[0] != 0x47) {
  1264. buf++;
  1265. len--;
  1266. } else {
  1267. handle_packet(ts, buf);
  1268. buf += TS_PACKET_SIZE;
  1269. len -= TS_PACKET_SIZE;
  1270. }
  1271. }
  1272. return len1 - len;
  1273. }
  1274. void mpegts_parse_close(MpegTSContext *ts)
  1275. {
  1276. int i;
  1277. for(i=0;i<NB_PID_MAX;i++)
  1278. av_free(ts->pids[i]);
  1279. av_free(ts);
  1280. }
  1281. AVInputFormat mpegts_demuxer = {
  1282. "mpegts",
  1283. "MPEG2 transport stream format",
  1284. sizeof(MpegTSContext),
  1285. mpegts_probe,
  1286. mpegts_read_header,
  1287. mpegts_read_packet,
  1288. mpegts_read_close,
  1289. read_seek,
  1290. mpegts_get_pcr,
  1291. .flags = AVFMT_SHOW_IDS,
  1292. };
  1293. AVInputFormat mpegtsraw_demuxer = {
  1294. "mpegtsraw",
  1295. "MPEG2 raw transport stream format",
  1296. sizeof(MpegTSContext),
  1297. mpegts_probe,
  1298. mpegts_read_header,
  1299. mpegts_raw_read_packet,
  1300. mpegts_read_close,
  1301. read_seek,
  1302. mpegts_get_pcr,
  1303. .flags = AVFMT_SHOW_IDS,
  1304. };