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.

1434 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. memcpy(st->language, language, 4);
  490. }
  491. if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
  492. st->codec->sub_id = (anc_page << 16) | comp_page;
  493. }
  494. }
  495. }
  496. /* all parameters are there */
  497. ts->stop_parse++;
  498. mpegts_close_filter(ts, filter);
  499. }
  500. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  501. {
  502. MpegTSContext *ts = filter->u.section_filter.opaque;
  503. SectionHeader h1, *h = &h1;
  504. const uint8_t *p, *p_end;
  505. int sid, pmt_pid;
  506. #ifdef DEBUG_SI
  507. av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
  508. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  509. #endif
  510. p_end = section + section_len - 4;
  511. p = section;
  512. if (parse_section_header(h, &p, p_end) < 0)
  513. return;
  514. if (h->tid != PAT_TID)
  515. return;
  516. for(;;) {
  517. sid = get16(&p, p_end);
  518. if (sid < 0)
  519. break;
  520. pmt_pid = get16(&p, p_end) & 0x1fff;
  521. if (pmt_pid < 0)
  522. break;
  523. #ifdef DEBUG_SI
  524. av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  525. #endif
  526. if (sid == 0x0000) {
  527. /* NIT info */
  528. } else {
  529. new_service(ts, sid, NULL, NULL);
  530. ts->stop_parse--;
  531. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  532. }
  533. }
  534. /* not found */
  535. ts->stop_parse++;
  536. mpegts_close_filter(ts, filter);
  537. }
  538. static void mpegts_set_service(MpegTSContext *ts)
  539. {
  540. mpegts_open_section_filter(ts, PAT_PID,
  541. pat_cb, ts, 1);
  542. }
  543. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  544. {
  545. MpegTSContext *ts = filter->u.section_filter.opaque;
  546. SectionHeader h1, *h = &h1;
  547. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  548. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  549. char *name, *provider_name;
  550. #ifdef DEBUG_SI
  551. av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
  552. av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
  553. #endif
  554. p_end = section + section_len - 4;
  555. p = section;
  556. if (parse_section_header(h, &p, p_end) < 0)
  557. return;
  558. if (h->tid != SDT_TID)
  559. return;
  560. onid = get16(&p, p_end);
  561. if (onid < 0)
  562. return;
  563. val = get8(&p, p_end);
  564. if (val < 0)
  565. return;
  566. for(;;) {
  567. sid = get16(&p, p_end);
  568. if (sid < 0)
  569. break;
  570. val = get8(&p, p_end);
  571. if (val < 0)
  572. break;
  573. desc_list_len = get16(&p, p_end) & 0xfff;
  574. if (desc_list_len < 0)
  575. break;
  576. desc_list_end = p + desc_list_len;
  577. if (desc_list_end > p_end)
  578. break;
  579. for(;;) {
  580. desc_tag = get8(&p, desc_list_end);
  581. if (desc_tag < 0)
  582. break;
  583. desc_len = get8(&p, desc_list_end);
  584. desc_end = p + desc_len;
  585. if (desc_end > desc_list_end)
  586. break;
  587. #ifdef DEBUG_SI
  588. av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
  589. desc_tag, desc_len);
  590. #endif
  591. switch(desc_tag) {
  592. case 0x48:
  593. service_type = get8(&p, p_end);
  594. if (service_type < 0)
  595. break;
  596. provider_name = getstr8(&p, p_end);
  597. if (!provider_name)
  598. break;
  599. name = getstr8(&p, p_end);
  600. if (!name)
  601. break;
  602. new_service(ts, sid, provider_name, name);
  603. break;
  604. default:
  605. break;
  606. }
  607. p = desc_end;
  608. }
  609. p = desc_list_end;
  610. }
  611. }
  612. /* scan services in a transport stream by looking at the SDT */
  613. static void mpegts_scan_sdt(MpegTSContext *ts)
  614. {
  615. mpegts_open_section_filter(ts, SDT_PID,
  616. sdt_cb, ts, 1);
  617. }
  618. static int64_t get_pts(const uint8_t *p)
  619. {
  620. int64_t pts;
  621. int val;
  622. pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
  623. val = (p[1] << 8) | p[2];
  624. pts |= (int64_t)(val >> 1) << 15;
  625. val = (p[3] << 8) | p[4];
  626. pts |= (int64_t)(val >> 1);
  627. return pts;
  628. }
  629. /* return non zero if a packet could be constructed */
  630. static void mpegts_push_data(MpegTSFilter *filter,
  631. const uint8_t *buf, int buf_size, int is_start)
  632. {
  633. PESContext *pes = filter->u.pes_filter.opaque;
  634. MpegTSContext *ts = pes->ts;
  635. const uint8_t *p;
  636. int len, code;
  637. if(!ts->pkt)
  638. return;
  639. if (is_start) {
  640. pes->state = MPEGTS_HEADER;
  641. pes->data_index = 0;
  642. }
  643. p = buf;
  644. while (buf_size > 0) {
  645. switch(pes->state) {
  646. case MPEGTS_HEADER:
  647. len = PES_START_SIZE - pes->data_index;
  648. if (len > buf_size)
  649. len = buf_size;
  650. memcpy(pes->header + pes->data_index, p, len);
  651. pes->data_index += len;
  652. p += len;
  653. buf_size -= len;
  654. if (pes->data_index == PES_START_SIZE) {
  655. /* we got all the PES or section header. We can now
  656. decide */
  657. #if 0
  658. av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
  659. #endif
  660. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  661. pes->header[2] == 0x01) {
  662. /* it must be an mpeg2 PES stream */
  663. code = pes->header[3] | 0x100;
  664. if (!((code >= 0x1c0 && code <= 0x1df) ||
  665. (code >= 0x1e0 && code <= 0x1ef) ||
  666. (code == 0x1bd) || (code == 0x1fd)))
  667. goto skip;
  668. if (!pes->st) {
  669. /* allocate stream */
  670. new_pes_av_stream(pes, code);
  671. }
  672. pes->state = MPEGTS_PESHEADER_FILL;
  673. pes->total_size = (pes->header[4] << 8) | pes->header[5];
  674. /* NOTE: a zero total size means the PES size is
  675. unbounded */
  676. if (pes->total_size)
  677. pes->total_size += 6;
  678. pes->pes_header_size = pes->header[8] + 9;
  679. } else {
  680. /* otherwise, it should be a table */
  681. /* skip packet */
  682. skip:
  683. pes->state = MPEGTS_SKIP;
  684. continue;
  685. }
  686. }
  687. break;
  688. /**********************************************/
  689. /* PES packing parsing */
  690. case MPEGTS_PESHEADER_FILL:
  691. len = pes->pes_header_size - pes->data_index;
  692. if (len > buf_size)
  693. len = buf_size;
  694. memcpy(pes->header + pes->data_index, p, len);
  695. pes->data_index += len;
  696. p += len;
  697. buf_size -= len;
  698. if (pes->data_index == pes->pes_header_size) {
  699. const uint8_t *r;
  700. unsigned int flags;
  701. flags = pes->header[7];
  702. r = pes->header + 9;
  703. pes->pts = AV_NOPTS_VALUE;
  704. pes->dts = AV_NOPTS_VALUE;
  705. if ((flags & 0xc0) == 0x80) {
  706. pes->pts = get_pts(r);
  707. r += 5;
  708. } else if ((flags & 0xc0) == 0xc0) {
  709. pes->pts = get_pts(r);
  710. r += 5;
  711. pes->dts = get_pts(r);
  712. r += 5;
  713. }
  714. /* we got the full header. We parse it and get the payload */
  715. pes->state = MPEGTS_PAYLOAD;
  716. }
  717. break;
  718. case MPEGTS_PAYLOAD:
  719. if (pes->total_size) {
  720. len = pes->total_size - pes->data_index;
  721. if (len > buf_size)
  722. len = buf_size;
  723. } else {
  724. len = buf_size;
  725. }
  726. if (len > 0) {
  727. AVPacket *pkt = ts->pkt;
  728. if (pes->st && av_new_packet(pkt, len) == 0) {
  729. memcpy(pkt->data, p, len);
  730. pkt->stream_index = pes->st->index;
  731. pkt->pts = pes->pts;
  732. pkt->dts = pes->dts;
  733. /* reset pts values */
  734. pes->pts = AV_NOPTS_VALUE;
  735. pes->dts = AV_NOPTS_VALUE;
  736. ts->stop_parse = 1;
  737. return;
  738. }
  739. }
  740. buf_size = 0;
  741. break;
  742. case MPEGTS_SKIP:
  743. buf_size = 0;
  744. break;
  745. }
  746. }
  747. }
  748. static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
  749. {
  750. AVStream *st;
  751. int codec_type, codec_id;
  752. switch(pes->stream_type){
  753. case STREAM_TYPE_AUDIO_MPEG1:
  754. case STREAM_TYPE_AUDIO_MPEG2:
  755. codec_type = CODEC_TYPE_AUDIO;
  756. codec_id = CODEC_ID_MP3;
  757. break;
  758. case STREAM_TYPE_VIDEO_MPEG1:
  759. case STREAM_TYPE_VIDEO_MPEG2:
  760. codec_type = CODEC_TYPE_VIDEO;
  761. codec_id = CODEC_ID_MPEG2VIDEO;
  762. break;
  763. case STREAM_TYPE_VIDEO_MPEG4:
  764. codec_type = CODEC_TYPE_VIDEO;
  765. codec_id = CODEC_ID_MPEG4;
  766. break;
  767. case STREAM_TYPE_VIDEO_H264:
  768. codec_type = CODEC_TYPE_VIDEO;
  769. codec_id = CODEC_ID_H264;
  770. break;
  771. case STREAM_TYPE_VIDEO_VC1:
  772. codec_type = CODEC_TYPE_VIDEO;
  773. codec_id = CODEC_ID_VC1;
  774. break;
  775. case STREAM_TYPE_AUDIO_AAC:
  776. codec_type = CODEC_TYPE_AUDIO;
  777. codec_id = CODEC_ID_AAC;
  778. break;
  779. case STREAM_TYPE_AUDIO_AC3:
  780. codec_type = CODEC_TYPE_AUDIO;
  781. codec_id = CODEC_ID_AC3;
  782. break;
  783. case STREAM_TYPE_AUDIO_DTS:
  784. codec_type = CODEC_TYPE_AUDIO;
  785. codec_id = CODEC_ID_DTS;
  786. break;
  787. case STREAM_TYPE_SUBTITLE_DVB:
  788. codec_type = CODEC_TYPE_SUBTITLE;
  789. codec_id = CODEC_ID_DVB_SUBTITLE;
  790. break;
  791. default:
  792. if (code >= 0x1c0 && code <= 0x1df) {
  793. codec_type = CODEC_TYPE_AUDIO;
  794. codec_id = CODEC_ID_MP2;
  795. } else if (code == 0x1bd) {
  796. codec_type = CODEC_TYPE_AUDIO;
  797. codec_id = CODEC_ID_AC3;
  798. } else {
  799. codec_type = CODEC_TYPE_VIDEO;
  800. codec_id = CODEC_ID_MPEG1VIDEO;
  801. }
  802. break;
  803. }
  804. st = av_new_stream(pes->stream, pes->pid);
  805. if (st) {
  806. av_set_pts_info(st, 33, 1, 90000);
  807. st->priv_data = pes;
  808. st->codec->codec_type = codec_type;
  809. st->codec->codec_id = codec_id;
  810. st->need_parsing = AVSTREAM_PARSE_FULL;
  811. pes->st = st;
  812. }
  813. return st;
  814. }
  815. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
  816. {
  817. MpegTSFilter *tss;
  818. PESContext *pes;
  819. /* if no pid found, then add a pid context */
  820. pes = av_mallocz(sizeof(PESContext));
  821. if (!pes)
  822. return 0;
  823. pes->ts = ts;
  824. pes->stream = ts->stream;
  825. pes->pid = pid;
  826. pes->pcr_pid = pcr_pid;
  827. pes->stream_type = stream_type;
  828. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  829. if (!tss) {
  830. av_free(pes);
  831. return 0;
  832. }
  833. return pes;
  834. }
  835. /* handle one TS packet */
  836. static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
  837. {
  838. AVFormatContext *s = ts->stream;
  839. MpegTSFilter *tss;
  840. int len, pid, cc, cc_ok, afc, is_start;
  841. const uint8_t *p, *p_end;
  842. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  843. is_start = packet[1] & 0x40;
  844. tss = ts->pids[pid];
  845. if (ts->auto_guess && tss == NULL && is_start) {
  846. add_pes_stream(ts, pid, -1, 0);
  847. tss = ts->pids[pid];
  848. }
  849. if (!tss)
  850. return;
  851. /* continuity check (currently not used) */
  852. cc = (packet[3] & 0xf);
  853. cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
  854. tss->last_cc = cc;
  855. /* skip adaptation field */
  856. afc = (packet[3] >> 4) & 3;
  857. p = packet + 4;
  858. if (afc == 0) /* reserved value */
  859. return;
  860. if (afc == 2) /* adaptation field only */
  861. return;
  862. if (afc == 3) {
  863. /* skip adapation field */
  864. p += p[0] + 1;
  865. }
  866. /* if past the end of packet, ignore */
  867. p_end = packet + TS_PACKET_SIZE;
  868. if (p >= p_end)
  869. return;
  870. if (tss->type == MPEGTS_SECTION) {
  871. if (is_start) {
  872. /* pointer field present */
  873. len = *p++;
  874. if (p + len > p_end)
  875. return;
  876. if (len && cc_ok) {
  877. /* write remaining section bytes */
  878. write_section_data(s, tss,
  879. p, len, 0);
  880. /* check whether filter has been closed */
  881. if (!ts->pids[pid])
  882. return;
  883. }
  884. p += len;
  885. if (p < p_end) {
  886. write_section_data(s, tss,
  887. p, p_end - p, 1);
  888. }
  889. } else {
  890. if (cc_ok) {
  891. write_section_data(s, tss,
  892. p, p_end - p, 0);
  893. }
  894. }
  895. } else {
  896. tss->u.pes_filter.pes_cb(tss,
  897. p, p_end - p, is_start);
  898. }
  899. }
  900. /* XXX: try to find a better synchro over several packets (use
  901. get_packet_size() ?) */
  902. static int mpegts_resync(ByteIOContext *pb)
  903. {
  904. int c, i;
  905. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  906. c = url_fgetc(pb);
  907. if (c < 0)
  908. return -1;
  909. if (c == 0x47) {
  910. url_fseek(pb, -1, SEEK_CUR);
  911. return 0;
  912. }
  913. }
  914. /* no sync found */
  915. return -1;
  916. }
  917. /* return -1 if error or EOF. Return 0 if OK. */
  918. static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
  919. {
  920. int skip, len;
  921. for(;;) {
  922. len = get_buffer(pb, buf, TS_PACKET_SIZE);
  923. if (len != TS_PACKET_SIZE)
  924. return AVERROR_IO;
  925. /* check paquet sync byte */
  926. if (buf[0] != 0x47) {
  927. /* find a new packet start */
  928. url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
  929. if (mpegts_resync(pb) < 0)
  930. return AVERROR_INVALIDDATA;
  931. else
  932. continue;
  933. } else {
  934. skip = raw_packet_size - TS_PACKET_SIZE;
  935. if (skip > 0)
  936. url_fskip(pb, skip);
  937. break;
  938. }
  939. }
  940. return 0;
  941. }
  942. static int handle_packets(MpegTSContext *ts, int nb_packets)
  943. {
  944. AVFormatContext *s = ts->stream;
  945. ByteIOContext *pb = &s->pb;
  946. uint8_t packet[TS_PACKET_SIZE];
  947. int packet_num, ret;
  948. ts->stop_parse = 0;
  949. packet_num = 0;
  950. for(;;) {
  951. if (ts->stop_parse>0)
  952. break;
  953. packet_num++;
  954. if (nb_packets != 0 && packet_num >= nb_packets)
  955. break;
  956. ret = read_packet(pb, packet, ts->raw_packet_size);
  957. if (ret != 0)
  958. return ret;
  959. handle_packet(ts, packet);
  960. }
  961. return 0;
  962. }
  963. static int mpegts_probe(AVProbeData *p)
  964. {
  965. #if 1
  966. const int size= p->buf_size;
  967. int score, fec_score, dvhs_score;
  968. #define CHECK_COUNT 10
  969. if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
  970. return -1;
  971. score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
  972. dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE *CHECK_COUNT, TS_DVHS_PACKET_SIZE, NULL);
  973. fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
  974. // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
  975. // we need a clear definition for the returned score otherwise things will become messy sooner or later
  976. if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
  977. else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
  978. else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
  979. else return -1;
  980. #else
  981. /* only use the extension for safer guess */
  982. if (match_ext(p->filename, "ts"))
  983. return AVPROBE_SCORE_MAX;
  984. else
  985. return 0;
  986. #endif
  987. }
  988. /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
  989. (-1) if not available */
  990. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  991. const uint8_t *packet)
  992. {
  993. int afc, len, flags;
  994. const uint8_t *p;
  995. unsigned int v;
  996. afc = (packet[3] >> 4) & 3;
  997. if (afc <= 1)
  998. return -1;
  999. p = packet + 4;
  1000. len = p[0];
  1001. p++;
  1002. if (len == 0)
  1003. return -1;
  1004. flags = *p++;
  1005. len--;
  1006. if (!(flags & 0x10))
  1007. return -1;
  1008. if (len < 6)
  1009. return -1;
  1010. v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  1011. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1012. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1013. return 0;
  1014. }
  1015. static int mpegts_read_header(AVFormatContext *s,
  1016. AVFormatParameters *ap)
  1017. {
  1018. MpegTSContext *ts = s->priv_data;
  1019. ByteIOContext *pb = &s->pb;
  1020. uint8_t buf[1024];
  1021. int len;
  1022. int64_t pos;
  1023. if (ap) {
  1024. ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
  1025. if(ap->mpeg2ts_raw){
  1026. av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
  1027. return -1;
  1028. }
  1029. }
  1030. /* read the first 1024 bytes to get packet size */
  1031. pos = url_ftell(pb);
  1032. len = get_buffer(pb, buf, sizeof(buf));
  1033. if (len != sizeof(buf))
  1034. goto fail;
  1035. ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
  1036. if (ts->raw_packet_size <= 0)
  1037. goto fail;
  1038. ts->stream = s;
  1039. ts->auto_guess = 0;
  1040. if (s->iformat == &mpegts_demuxer) {
  1041. /* normal demux */
  1042. /* first do a scaning to get all the services */
  1043. url_fseek(pb, pos, SEEK_SET);
  1044. mpegts_scan_sdt(ts);
  1045. mpegts_set_service(ts);
  1046. handle_packets(ts, s->probesize);
  1047. /* if could not find service, enable auto_guess */
  1048. ts->auto_guess = 1;
  1049. #ifdef DEBUG_SI
  1050. av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
  1051. #endif
  1052. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1053. } else {
  1054. AVStream *st;
  1055. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1056. int64_t pcrs[2], pcr_h;
  1057. int packet_count[2];
  1058. uint8_t packet[TS_PACKET_SIZE];
  1059. /* only read packets */
  1060. st = av_new_stream(s, 0);
  1061. if (!st)
  1062. goto fail;
  1063. av_set_pts_info(st, 60, 1, 27000000);
  1064. st->codec->codec_type = CODEC_TYPE_DATA;
  1065. st->codec->codec_id = CODEC_ID_MPEG2TS;
  1066. /* we iterate until we find two PCRs to estimate the bitrate */
  1067. pcr_pid = -1;
  1068. nb_pcrs = 0;
  1069. nb_packets = 0;
  1070. for(;;) {
  1071. ret = read_packet(&s->pb, packet, ts->raw_packet_size);
  1072. if (ret < 0)
  1073. return -1;
  1074. pid = ((packet[1] & 0x1f) << 8) | packet[2];
  1075. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1076. parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
  1077. pcr_pid = pid;
  1078. packet_count[nb_pcrs] = nb_packets;
  1079. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1080. nb_pcrs++;
  1081. if (nb_pcrs >= 2)
  1082. break;
  1083. }
  1084. nb_packets++;
  1085. }
  1086. /* NOTE1: the bitrate is computed without the FEC */
  1087. /* NOTE2: it is only the bitrate of the start of the stream */
  1088. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1089. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1090. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1091. st->codec->bit_rate = s->bit_rate;
  1092. st->start_time = ts->cur_pcr;
  1093. #if 0
  1094. av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
  1095. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1096. #endif
  1097. }
  1098. url_fseek(pb, pos, SEEK_SET);
  1099. return 0;
  1100. fail:
  1101. return -1;
  1102. }
  1103. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1104. static int mpegts_raw_read_packet(AVFormatContext *s,
  1105. AVPacket *pkt)
  1106. {
  1107. MpegTSContext *ts = s->priv_data;
  1108. int ret, i;
  1109. int64_t pcr_h, next_pcr_h, pos;
  1110. int pcr_l, next_pcr_l;
  1111. uint8_t pcr_buf[12];
  1112. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1113. return AVERROR(ENOMEM);
  1114. pkt->pos= url_ftell(&s->pb);
  1115. ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
  1116. if (ret < 0) {
  1117. av_free_packet(pkt);
  1118. return ret;
  1119. }
  1120. if (ts->mpeg2ts_compute_pcr) {
  1121. /* compute exact PCR for each packet */
  1122. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1123. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1124. pos = url_ftell(&s->pb);
  1125. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  1126. url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  1127. get_buffer(&s->pb, pcr_buf, 12);
  1128. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  1129. /* XXX: not precise enough */
  1130. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  1131. (i + 1);
  1132. break;
  1133. }
  1134. }
  1135. url_fseek(&s->pb, pos, SEEK_SET);
  1136. /* no next PCR found: we use previous increment */
  1137. ts->cur_pcr = pcr_h * 300 + pcr_l;
  1138. }
  1139. pkt->pts = ts->cur_pcr;
  1140. pkt->duration = ts->pcr_incr;
  1141. ts->cur_pcr += ts->pcr_incr;
  1142. }
  1143. pkt->stream_index = 0;
  1144. return 0;
  1145. }
  1146. static int mpegts_read_packet(AVFormatContext *s,
  1147. AVPacket *pkt)
  1148. {
  1149. MpegTSContext *ts = s->priv_data;
  1150. ts->pkt = pkt;
  1151. return handle_packets(ts, 0);
  1152. }
  1153. static int mpegts_read_close(AVFormatContext *s)
  1154. {
  1155. MpegTSContext *ts = s->priv_data;
  1156. int i;
  1157. for(i=0;i<NB_PID_MAX;i++)
  1158. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  1159. for(i = 0; i < ts->nb_services; i++){
  1160. av_free(ts->services[i]->provider_name);
  1161. av_free(ts->services[i]->name);
  1162. av_free(ts->services[i]);
  1163. }
  1164. av_freep(&ts->services);
  1165. return 0;
  1166. }
  1167. static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  1168. int64_t *ppos, int64_t pos_limit)
  1169. {
  1170. MpegTSContext *ts = s->priv_data;
  1171. int64_t pos, timestamp;
  1172. uint8_t buf[TS_PACKET_SIZE];
  1173. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  1174. const int find_next= 1;
  1175. pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
  1176. if (find_next) {
  1177. for(;;) {
  1178. url_fseek(&s->pb, pos, SEEK_SET);
  1179. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1180. return AV_NOPTS_VALUE;
  1181. if ((pcr_pid < 0 || (((buf[1] & 0x1f) << 8) | buf[2]) == pcr_pid) &&
  1182. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1183. break;
  1184. }
  1185. pos += ts->raw_packet_size;
  1186. }
  1187. } else {
  1188. for(;;) {
  1189. pos -= ts->raw_packet_size;
  1190. if (pos < 0)
  1191. return AV_NOPTS_VALUE;
  1192. url_fseek(&s->pb, pos, SEEK_SET);
  1193. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1194. return AV_NOPTS_VALUE;
  1195. if ((pcr_pid < 0 || (((buf[1] & 0x1f) << 8) | buf[2]) == pcr_pid) &&
  1196. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  1197. break;
  1198. }
  1199. }
  1200. }
  1201. *ppos = pos;
  1202. return timestamp;
  1203. }
  1204. static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
  1205. MpegTSContext *ts = s->priv_data;
  1206. uint8_t buf[TS_PACKET_SIZE];
  1207. int64_t pos;
  1208. if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
  1209. return -1;
  1210. pos= url_ftell(&s->pb);
  1211. for(;;) {
  1212. url_fseek(&s->pb, pos, SEEK_SET);
  1213. if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  1214. return -1;
  1215. // pid = ((buf[1] & 0x1f) << 8) | buf[2];
  1216. if(buf[1] & 0x40) break;
  1217. pos += ts->raw_packet_size;
  1218. }
  1219. url_fseek(&s->pb, pos, SEEK_SET);
  1220. return 0;
  1221. }
  1222. /**************************************************************/
  1223. /* parsing functions - called from other demuxers such as RTP */
  1224. MpegTSContext *mpegts_parse_open(AVFormatContext *s)
  1225. {
  1226. MpegTSContext *ts;
  1227. ts = av_mallocz(sizeof(MpegTSContext));
  1228. if (!ts)
  1229. return NULL;
  1230. /* no stream case, currently used by RTP */
  1231. ts->raw_packet_size = TS_PACKET_SIZE;
  1232. ts->stream = s;
  1233. ts->auto_guess = 1;
  1234. return ts;
  1235. }
  1236. /* return the consumed length if a packet was output, or -1 if no
  1237. packet is output */
  1238. int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  1239. const uint8_t *buf, int len)
  1240. {
  1241. int len1;
  1242. len1 = len;
  1243. ts->pkt = pkt;
  1244. ts->stop_parse = 0;
  1245. for(;;) {
  1246. if (ts->stop_parse>0)
  1247. break;
  1248. if (len < TS_PACKET_SIZE)
  1249. return -1;
  1250. if (buf[0] != 0x47) {
  1251. buf++;
  1252. len--;
  1253. } else {
  1254. handle_packet(ts, buf);
  1255. buf += TS_PACKET_SIZE;
  1256. len -= TS_PACKET_SIZE;
  1257. }
  1258. }
  1259. return len1 - len;
  1260. }
  1261. void mpegts_parse_close(MpegTSContext *ts)
  1262. {
  1263. int i;
  1264. for(i=0;i<NB_PID_MAX;i++)
  1265. av_free(ts->pids[i]);
  1266. av_free(ts);
  1267. }
  1268. AVInputFormat mpegts_demuxer = {
  1269. "mpegts",
  1270. "MPEG2 transport stream format",
  1271. sizeof(MpegTSContext),
  1272. mpegts_probe,
  1273. mpegts_read_header,
  1274. mpegts_read_packet,
  1275. mpegts_read_close,
  1276. read_seek,
  1277. mpegts_get_pcr,
  1278. .flags = AVFMT_SHOW_IDS,
  1279. };
  1280. AVInputFormat mpegtsraw_demuxer = {
  1281. "mpegtsraw",
  1282. "MPEG2 raw transport stream format",
  1283. sizeof(MpegTSContext),
  1284. mpegts_probe,
  1285. mpegts_read_header,
  1286. mpegts_raw_read_packet,
  1287. mpegts_read_close,
  1288. read_seek,
  1289. mpegts_get_pcr,
  1290. .flags = AVFMT_SHOW_IDS,
  1291. };