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.

1440 lines
41KB

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