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.

1437 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 pcr_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. /* data needed to handle file based ts */
  82. /** stop parsing loop */
  83. int stop_parse;
  84. /** packet containing Audio/Video data */
  85. AVPacket *pkt;
  86. /******************************************/
  87. /* private mpegts data */
  88. /* scan context */
  89. /** number of PMTs in the last PAT seen */
  90. int nb_services;
  91. /** list of PMTs in the last PAT seen */
  92. MpegTSService **services;
  93. /** filters for various streams specified by PMT + for the PAT and PMT */
  94. MpegTSFilter *pids[NB_PID_MAX];
  95. };
  96. /* TS stream handling */
  97. enum MpegTSState {
  98. MPEGTS_HEADER = 0,
  99. MPEGTS_PESHEADER_FILL,
  100. MPEGTS_PAYLOAD,
  101. MPEGTS_SKIP,
  102. };
  103. /* enough for PES header + length */
  104. #define PES_START_SIZE 9
  105. #define MAX_PES_HEADER_SIZE (9 + 255)
  106. struct PESContext {
  107. int pid;
  108. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  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. #ifdef DEBUG_SI
  388. av_log(ts->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
  389. #endif
  390. program_info_length = get16(&p, p_end) & 0xfff;
  391. if (program_info_length < 0)
  392. return;
  393. p += program_info_length;
  394. if (p >= p_end)
  395. return;
  396. for(;;) {
  397. language[0] = 0;
  398. st = 0;
  399. stream_type = get8(&p, p_end);
  400. if (stream_type < 0)
  401. break;
  402. pid = get16(&p, p_end) & 0x1fff;
  403. if (pid < 0)
  404. break;
  405. desc_list_len = get16(&p, p_end) & 0xfff;
  406. if (desc_list_len < 0)
  407. break;
  408. desc_list_end = p + desc_list_len;
  409. if (desc_list_end > p_end)
  410. break;
  411. for(;;) {
  412. desc_tag = get8(&p, desc_list_end);
  413. if (desc_tag < 0)
  414. break;
  415. if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
  416. if((desc_tag == 0x6A) || (desc_tag == 0x7A)) {
  417. /*assume DVB AC-3 Audio*/
  418. stream_type = STREAM_TYPE_AUDIO_AC3;
  419. } else if(desc_tag == 0x7B) {
  420. /* DVB DTS audio */
  421. stream_type = STREAM_TYPE_AUDIO_DTS;
  422. }
  423. }
  424. desc_len = get8(&p, desc_list_end);
  425. desc_end = p + desc_len;
  426. if (desc_end > desc_list_end)
  427. break;
  428. #ifdef DEBUG_SI
  429. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  430. desc_tag, desc_len);
  431. #endif
  432. switch(desc_tag) {
  433. case DVB_SUBT_DESCID:
  434. if (stream_type == STREAM_TYPE_PRIVATE_DATA)
  435. stream_type = STREAM_TYPE_SUBTITLE_DVB;
  436. language[0] = get8(&p, desc_end);
  437. language[1] = get8(&p, desc_end);
  438. language[2] = get8(&p, desc_end);
  439. language[3] = 0;
  440. get8(&p, desc_end);
  441. comp_page = get16(&p, desc_end);
  442. anc_page = get16(&p, desc_end);
  443. break;
  444. case 0x0a: /* ISO 639 language descriptor */
  445. language[0] = get8(&p, desc_end);
  446. language[1] = get8(&p, desc_end);
  447. language[2] = get8(&p, desc_end);
  448. language[3] = 0;
  449. break;
  450. default:
  451. break;
  452. }
  453. p = desc_end;
  454. }
  455. p = desc_list_end;
  456. #ifdef DEBUG_SI
  457. av_log(ts->stream, AV_LOG_DEBUG, "stream_type=%d pid=0x%x\n",
  458. stream_type, pid);
  459. #endif
  460. /* now create ffmpeg stream */
  461. switch(stream_type) {
  462. case STREAM_TYPE_AUDIO_MPEG1:
  463. case STREAM_TYPE_AUDIO_MPEG2:
  464. case STREAM_TYPE_VIDEO_MPEG1:
  465. case STREAM_TYPE_VIDEO_MPEG2:
  466. case STREAM_TYPE_VIDEO_MPEG4:
  467. case STREAM_TYPE_VIDEO_H264:
  468. case STREAM_TYPE_VIDEO_VC1:
  469. case STREAM_TYPE_AUDIO_AAC:
  470. case STREAM_TYPE_AUDIO_AC3:
  471. case STREAM_TYPE_AUDIO_DTS:
  472. case STREAM_TYPE_SUBTITLE_DVB:
  473. if(ts->pids[pid]){
  474. assert(ts->pids[pid].type == MPEGTS_PES);
  475. pes= ts->pids[pid]->u.pes_filter.opaque;
  476. st= pes->st;
  477. }else{
  478. pes = add_pes_stream(ts, pid, pcr_pid, stream_type);
  479. if (pes)
  480. st = new_pes_av_stream(pes, 0);
  481. }
  482. break;
  483. default:
  484. /* we ignore the other streams */
  485. break;
  486. }
  487. if (st) {
  488. if (language[0] != 0) {
  489. st->language[0] = language[0];
  490. st->language[1] = language[1];
  491. st->language[2] = language[2];
  492. st->language[3] = language[3];
  493. }
  494. if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
  495. st->codec->sub_id = (anc_page << 16) | comp_page;
  496. }
  497. }
  498. }
  499. /* all parameters are there */
  500. ts->stop_parse++;
  501. mpegts_close_filter(ts, filter);
  502. }
  503. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  504. {
  505. MpegTSContext *ts = filter->u.section_filter.opaque;
  506. SectionHeader h1, *h = &h1;
  507. const uint8_t *p, *p_end;
  508. int sid, pmt_pid;
  509. #ifdef DEBUG_SI
  510. av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
  511. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  512. #endif
  513. p_end = section + section_len - 4;
  514. p = section;
  515. if (parse_section_header(h, &p, p_end) < 0)
  516. return;
  517. if (h->tid != PAT_TID)
  518. return;
  519. for(;;) {
  520. sid = get16(&p, p_end);
  521. if (sid < 0)
  522. break;
  523. pmt_pid = get16(&p, p_end) & 0x1fff;
  524. if (pmt_pid < 0)
  525. break;
  526. #ifdef DEBUG_SI
  527. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  528. #endif
  529. if (sid == 0x0000) {
  530. /* NIT info */
  531. } else {
  532. new_service(ts, sid, NULL, NULL);
  533. ts->stop_parse--;
  534. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  535. }
  536. }
  537. /* not found */
  538. ts->stop_parse++;
  539. mpegts_close_filter(ts, filter);
  540. }
  541. static void mpegts_set_service(MpegTSContext *ts)
  542. {
  543. mpegts_open_section_filter(ts, PAT_PID,
  544. pat_cb, ts, 1);
  545. }
  546. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  547. {
  548. MpegTSContext *ts = filter->u.section_filter.opaque;
  549. SectionHeader h1, *h = &h1;
  550. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  551. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  552. char *name, *provider_name;
  553. #ifdef DEBUG_SI
  554. av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
  555. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  556. #endif
  557. p_end = section + section_len - 4;
  558. p = section;
  559. if (parse_section_header(h, &p, p_end) < 0)
  560. return;
  561. if (h->tid != SDT_TID)
  562. return;
  563. onid = get16(&p, p_end);
  564. if (onid < 0)
  565. return;
  566. val = get8(&p, p_end);
  567. if (val < 0)
  568. return;
  569. for(;;) {
  570. sid = get16(&p, p_end);
  571. if (sid < 0)
  572. break;
  573. val = get8(&p, p_end);
  574. if (val < 0)
  575. break;
  576. desc_list_len = get16(&p, p_end) & 0xfff;
  577. if (desc_list_len < 0)
  578. break;
  579. desc_list_end = p + desc_list_len;
  580. if (desc_list_end > p_end)
  581. break;
  582. for(;;) {
  583. desc_tag = get8(&p, desc_list_end);
  584. if (desc_tag < 0)
  585. break;
  586. desc_len = get8(&p, desc_list_end);
  587. desc_end = p + desc_len;
  588. if (desc_end > desc_list_end)
  589. break;
  590. #ifdef DEBUG_SI
  591. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  592. desc_tag, desc_len);
  593. #endif
  594. switch(desc_tag) {
  595. case 0x48:
  596. service_type = get8(&p, p_end);
  597. if (service_type < 0)
  598. break;
  599. provider_name = getstr8(&p, p_end);
  600. if (!provider_name)
  601. break;
  602. name = getstr8(&p, p_end);
  603. if (!name)
  604. break;
  605. new_service(ts, sid, provider_name, name);
  606. break;
  607. default:
  608. break;
  609. }
  610. p = desc_end;
  611. }
  612. p = desc_list_end;
  613. }
  614. }
  615. /* scan services in a transport stream by looking at the SDT */
  616. static void mpegts_scan_sdt(MpegTSContext *ts)
  617. {
  618. mpegts_open_section_filter(ts, SDT_PID,
  619. sdt_cb, ts, 1);
  620. }
  621. static int64_t get_pts(const uint8_t *p)
  622. {
  623. int64_t pts;
  624. int val;
  625. pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  626. val = (p[1] << 8) | p[2];
  627. pts |= (int64_t)(val >> 1) << 15;
  628. val = (p[3] << 8) | p[4];
  629. pts |= (int64_t)(val >> 1);
  630. return pts;
  631. }
  632. /* return non zero if a packet could be constructed */
  633. static void mpegts_push_data(MpegTSFilter *filter,
  634. const uint8_t *buf, int buf_size, int is_start)
  635. {
  636. PESContext *pes = filter->u.pes_filter.opaque;
  637. MpegTSContext *ts = pes->ts;
  638. const uint8_t *p;
  639. int len, code;
  640. if(!ts->pkt)
  641. return;
  642. if (is_start) {
  643. pes->state = MPEGTS_HEADER;
  644. pes->data_index = 0;
  645. }
  646. p = buf;
  647. while (buf_size > 0) {
  648. switch(pes->state) {
  649. case MPEGTS_HEADER:
  650. len = PES_START_SIZE - pes->data_index;
  651. if (len > buf_size)
  652. len = buf_size;
  653. memcpy(pes->header + pes->data_index, p, len);
  654. pes->data_index += len;
  655. p += len;
  656. buf_size -= len;
  657. if (pes->data_index == PES_START_SIZE) {
  658. /* we got all the PES or section header. We can now
  659. decide */
  660. #if 0
  661. av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
  662. #endif
  663. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  664. pes->header[2] == 0x01) {
  665. /* it must be an mpeg2 PES stream */
  666. code = pes->header[3] | 0x100;
  667. if (!((code >= 0x1c0 && code <= 0x1df) ||
  668. (code >= 0x1e0 && code <= 0x1ef) ||
  669. (code == 0x1bd) || (code == 0x1fd)))
  670. goto skip;
  671. if (!pes->st) {
  672. /* allocate stream */
  673. new_pes_av_stream(pes, code);
  674. }
  675. pes->state = MPEGTS_PESHEADER_FILL;
  676. pes->total_size = (pes->header[4] << 8) | pes->header[5];
  677. /* NOTE: a zero total size means the PES size is
  678. unbounded */
  679. if (pes->total_size)
  680. pes->total_size += 6;
  681. pes->pes_header_size = pes->header[8] + 9;
  682. } else {
  683. /* otherwise, it should be a table */
  684. /* skip packet */
  685. skip:
  686. pes->state = MPEGTS_SKIP;
  687. continue;
  688. }
  689. }
  690. break;
  691. /**********************************************/
  692. /* PES packing parsing */
  693. case MPEGTS_PESHEADER_FILL:
  694. len = pes->pes_header_size - pes->data_index;
  695. if (len > buf_size)
  696. len = buf_size;
  697. memcpy(pes->header + pes->data_index, p, len);
  698. pes->data_index += len;
  699. p += len;
  700. buf_size -= len;
  701. if (pes->data_index == pes->pes_header_size) {
  702. const uint8_t *r;
  703. unsigned int flags;
  704. flags = pes->header[7];
  705. r = pes->header + 9;
  706. pes->pts = AV_NOPTS_VALUE;
  707. pes->dts = AV_NOPTS_VALUE;
  708. if ((flags & 0xc0) == 0x80) {
  709. pes->pts = get_pts(r);
  710. r += 5;
  711. } else if ((flags & 0xc0) == 0xc0) {
  712. pes->pts = get_pts(r);
  713. r += 5;
  714. pes->dts = get_pts(r);
  715. r += 5;
  716. }
  717. /* we got the full header. We parse it and get the payload */
  718. pes->state = MPEGTS_PAYLOAD;
  719. }
  720. break;
  721. case MPEGTS_PAYLOAD:
  722. if (pes->total_size) {
  723. len = pes->total_size - pes->data_index;
  724. if (len > buf_size)
  725. len = buf_size;
  726. } else {
  727. len = buf_size;
  728. }
  729. if (len > 0) {
  730. AVPacket *pkt = ts->pkt;
  731. if (pes->st && av_new_packet(pkt, len) == 0) {
  732. memcpy(pkt->data, p, len);
  733. pkt->stream_index = pes->st->index;
  734. pkt->pts = pes->pts;
  735. pkt->dts = pes->dts;
  736. /* reset pts values */
  737. pes->pts = AV_NOPTS_VALUE;
  738. pes->dts = AV_NOPTS_VALUE;
  739. ts->stop_parse = 1;
  740. return;
  741. }
  742. }
  743. buf_size = 0;
  744. break;
  745. case MPEGTS_SKIP:
  746. buf_size = 0;
  747. break;
  748. }
  749. }
  750. }
  751. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
  752. {
  753. AVStream *st;
  754. int codec_type, codec_id;
  755. switch(pes->stream_type){
  756. case STREAM_TYPE_AUDIO_MPEG1:
  757. case STREAM_TYPE_AUDIO_MPEG2:
  758. codec_type = CODEC_TYPE_AUDIO;
  759. codec_id = CODEC_ID_MP3;
  760. break;
  761. case STREAM_TYPE_VIDEO_MPEG1:
  762. case STREAM_TYPE_VIDEO_MPEG2:
  763. codec_type = CODEC_TYPE_VIDEO;
  764. codec_id = CODEC_ID_MPEG2VIDEO;
  765. break;
  766. case STREAM_TYPE_VIDEO_MPEG4:
  767. codec_type = CODEC_TYPE_VIDEO;
  768. codec_id = CODEC_ID_MPEG4;
  769. break;
  770. case STREAM_TYPE_VIDEO_H264:
  771. codec_type = CODEC_TYPE_VIDEO;
  772. codec_id = CODEC_ID_H264;
  773. break;
  774. case STREAM_TYPE_VIDEO_VC1:
  775. codec_type = CODEC_TYPE_VIDEO;
  776. codec_id = CODEC_ID_VC1;
  777. break;
  778. case STREAM_TYPE_AUDIO_AAC:
  779. codec_type = CODEC_TYPE_AUDIO;
  780. codec_id = CODEC_ID_AAC;
  781. break;
  782. case STREAM_TYPE_AUDIO_AC3:
  783. codec_type = CODEC_TYPE_AUDIO;
  784. codec_id = CODEC_ID_AC3;
  785. break;
  786. case STREAM_TYPE_AUDIO_DTS:
  787. codec_type = CODEC_TYPE_AUDIO;
  788. codec_id = CODEC_ID_DTS;
  789. break;
  790. case STREAM_TYPE_SUBTITLE_DVB:
  791. codec_type = CODEC_TYPE_SUBTITLE;
  792. codec_id = CODEC_ID_DVB_SUBTITLE;
  793. break;
  794. default:
  795. if (code >= 0x1c0 && code <= 0x1df) {
  796. codec_type = CODEC_TYPE_AUDIO;
  797. codec_id = CODEC_ID_MP2;
  798. } else if (code == 0x1bd) {
  799. codec_type = CODEC_TYPE_AUDIO;
  800. codec_id = CODEC_ID_AC3;
  801. } else {
  802. codec_type = CODEC_TYPE_VIDEO;
  803. codec_id = CODEC_ID_MPEG1VIDEO;
  804. }
  805. break;
  806. }
  807. st = av_new_stream(pes->stream, pes->pid);
  808. if (st) {
  809. av_set_pts_info(st, 33, 1, 90000);
  810. st->priv_data = pes;
  811. st->codec->codec_type = codec_type;
  812. st->codec->codec_id = codec_id;
  813. st->need_parsing = AVSTREAM_PARSE_FULL;
  814. pes->st = st;
  815. }
  816. return st;
  817. }
  818. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
  819. {
  820. MpegTSFilter *tss;
  821. PESContext *pes;
  822. /* if no pid found, then add a pid context */
  823. pes = av_mallocz(sizeof(PESContext));
  824. if (!pes)
  825. return 0;
  826. pes->ts = ts;
  827. pes->stream = ts->stream;
  828. pes->pid = pid;
  829. pes->pcr_pid = pcr_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, -1, 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. mpegts_set_service(ts);
  1049. handle_packets(ts, s->probesize);
  1050. /* if could not find service, enable auto_guess */
  1051. ts->auto_guess = 1;
  1052. #ifdef DEBUG_SI
  1053. av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
  1054. #endif
  1055. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1056. } else {
  1057. AVStream *st;
  1058. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1059. int64_t pcrs[2], pcr_h;
  1060. int packet_count[2];
  1061. uint8_t packet[TS_PACKET_SIZE];
  1062. /* only read packets */
  1063. st = av_new_stream(s, 0);
  1064. if (!st)
  1065. goto fail;
  1066. av_set_pts_info(st, 60, 1, 27000000);
  1067. st->codec->codec_type = CODEC_TYPE_DATA;
  1068. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1069. /* we iterate until we find two PCRs to estimate the bitrate */
  1070. pcr_pid = -1;
  1071. nb_pcrs = 0;
  1072. nb_packets = 0;
  1073. for(;;) {
  1074. ret = read_packet(&s->pb, packet, ts->raw_packet_size);
  1075. if (ret < 0)
  1076. return -1;
  1077. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  1078. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1079. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1080. pcr_pid = pid;
  1081. packet_count[nb_pcrs] = nb_packets;
  1082. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1083. nb_pcrs++;
  1084. if (nb_pcrs >= 2)
  1085. break;
  1086. }
  1087. nb_packets++;
  1088. }
  1089. /* NOTE1: the bitrate is computed without the FEC */
  1090. /* NOTE2: it is only the bitrate of the start of the stream */
  1091. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1092. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1093. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1094. st->codec->bit_rate = s->bit_rate;
  1095. st->start_time = ts->cur_pcr;
  1096. #if 0
  1097. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1098. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1099. #endif
  1100. }
  1101. url_fseek(pb, pos, SEEK_SET);
  1102. return 0;
  1103. fail:
  1104. return -1;
  1105. }
  1106. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1107. static int mpegts_raw_read_packet(AVFormatContext *s,
  1108. AVPacket *pkt)
  1109. {
  1110. MpegTSContext *ts = s->priv_data;
  1111. int ret, i;
  1112. int64_t pcr_h, next_pcr_h, pos;
  1113. int pcr_l, next_pcr_l;
  1114. uint8_t pcr_buf[12];
  1115. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1116. return AVERROR(ENOMEM);
  1117. pkt->pos= url_ftell(&s->pb);
  1118. ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
  1119. if (ret < 0) {
  1120. av_free_packet(pkt);
  1121. return ret;
  1122. }
  1123. if (ts->mpeg2ts_compute_pcr) {
  1124. /* compute exact PCR for each packet */
  1125. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1126. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1127. pos = url_ftell(&s->pb);
  1128. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1129. url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1130. get_buffer(&s->pb, pcr_buf, 12);
  1131. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1132. /* XXX: not precise enough */
  1133. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1134. (i + 1);
  1135. break;
  1136. }
  1137. }
  1138. url_fseek(&s->pb, pos, SEEK_SET);
  1139. /* no next PCR found: we use previous increment */
  1140. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1141. }
  1142. pkt->pts = ts->cur_pcr;
  1143. pkt->duration = ts->pcr_incr;
  1144. ts->cur_pcr += ts->pcr_incr;
  1145. }
  1146. pkt->stream_index = 0;
  1147. return 0;
  1148. }
  1149. static int mpegts_read_packet(AVFormatContext *s,
  1150. AVPacket *pkt)
  1151. {
  1152. MpegTSContext *ts = s->priv_data;
  1153. ts->pkt = pkt;
  1154. return handle_packets(ts, 0);
  1155. }
  1156. static int mpegts_read_close(AVFormatContext *s)
  1157. {
  1158. MpegTSContext *ts = s->priv_data;
  1159. int i;
  1160. for(i=0;i<NB_PID_MAX;i++)
  1161. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1162. for(i = 0; i < ts->nb_services; i++){
  1163. av_free(ts->services[i]->provider_name);
  1164. av_free(ts->services[i]->name);
  1165. av_free(ts->services[i]);
  1166. }
  1167. av_freep(&ts->services);
  1168. return 0;
  1169. }
  1170. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1171. int64_t *ppos, int64_t pos_limit)
  1172. {
  1173. MpegTSContext *ts = s->priv_data;
  1174. int64_t pos, timestamp;
  1175. uint8_t buf[TS_PACKET_SIZE];
  1176. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1177. const int find_next= 1;
  1178. pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
  1179. if (find_next) {
  1180. for(;;) {
  1181. url_fseek(&s->pb, pos, SEEK_SET);
  1182. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1183. return AV_NOPTS_VALUE;
  1184. if ((pcr_pid < 0 || (((buf[1] & 0x1f) << 8) | buf[2]) == pcr_pid) &&
  1185. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1186. break;
  1187. }
  1188. pos += ts->raw_packet_size;
  1189. }
  1190. } else {
  1191. for(;;) {
  1192. pos -= ts->raw_packet_size;
  1193. if (pos < 0)
  1194. return AV_NOPTS_VALUE;
  1195. url_fseek(&s->pb, pos, SEEK_SET);
  1196. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1197. return AV_NOPTS_VALUE;
  1198. if ((pcr_pid < 0 || (((buf[1] & 0x1f) << 8) | buf[2]) == pcr_pid) &&
  1199. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1200. break;
  1201. }
  1202. }
  1203. }
  1204. *ppos = pos;
  1205. return timestamp;
  1206. }
  1207. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1208. MpegTSContext *ts = s->priv_data;
  1209. uint8_t buf[TS_PACKET_SIZE];
  1210. int64_t pos;
  1211. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1212. return -1;
  1213. pos= url_ftell(&s->pb);
  1214. for(;;) {
  1215. url_fseek(&s->pb, pos, SEEK_SET);
  1216. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1217. return -1;
  1218. // pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1219. if(buf[1] & 0x40) break;
  1220. pos += ts->raw_packet_size;
  1221. }
  1222. url_fseek(&s->pb, pos, SEEK_SET);
  1223. return 0;
  1224. }
  1225. /**************************************************************/
  1226. /* parsing functions - called from other demuxers such as RTP */
  1227. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1228. {
  1229. MpegTSContext *ts;
  1230. ts = av_mallocz(sizeof(MpegTSContext));
  1231. if (!ts)
  1232. return NULL;
  1233. /* no stream case, currently used by RTP */
  1234. ts->raw_packet_size = TS_PACKET_SIZE;
  1235. ts->stream = s;
  1236. ts->auto_guess = 1;
  1237. return ts;
  1238. }
  1239. /* return the consumed length if a packet was output, or -1 if no
  1240. packet is output */
  1241. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1242. const uint8_t *buf, int len)
  1243. {
  1244. int len1;
  1245. len1 = len;
  1246. ts->pkt = pkt;
  1247. ts->stop_parse = 0;
  1248. for(;;) {
  1249. if (ts->stop_parse>0)
  1250. break;
  1251. if (len < TS_PACKET_SIZE)
  1252. return -1;
  1253. if (buf[0] != 0x47) {
  1254. buf++;
  1255. len--;
  1256. } else {
  1257. handle_packet(ts, buf);
  1258. buf += TS_PACKET_SIZE;
  1259. len -= TS_PACKET_SIZE;
  1260. }
  1261. }
  1262. return len1 - len;
  1263. }
  1264. void mpegts_parse_close(MpegTSContext *ts)
  1265. {
  1266. int i;
  1267. for(i=0;i<NB_PID_MAX;i++)
  1268. av_free(ts->pids[i]);
  1269. av_free(ts);
  1270. }
  1271. AVInputFormat mpegts_demuxer = {
  1272. "mpegts",
  1273. "MPEG2 transport stream format",
  1274. sizeof(MpegTSContext),
  1275. mpegts_probe,
  1276. mpegts_read_header,
  1277. mpegts_read_packet,
  1278. mpegts_read_close,
  1279. read_seek,
  1280. mpegts_get_pcr,
  1281. .flags = AVFMT_SHOW_IDS,
  1282. };
  1283. AVInputFormat mpegtsraw_demuxer = {
  1284. "mpegtsraw",
  1285. "MPEG2 raw transport stream format",
  1286. sizeof(MpegTSContext),
  1287. mpegts_probe,
  1288. mpegts_read_header,
  1289. mpegts_raw_read_packet,
  1290. mpegts_read_close,
  1291. read_seek,
  1292. mpegts_get_pcr,
  1293. .flags = AVFMT_SHOW_IDS,
  1294. };