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.

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