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.

1586 lines
46KB

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