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.

1543 lines
44KB

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