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.

2455 lines
78KB

  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/buffer.h"
  22. #include "libavutil/crc.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/dict.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavcodec/bytestream.h"
  30. #include "libavcodec/get_bits.h"
  31. #include "libavcodec/mathops.h"
  32. #include "avformat.h"
  33. #include "mpegts.h"
  34. #include "internal.h"
  35. #include "avio_internal.h"
  36. #include "seek.h"
  37. #include "mpeg.h"
  38. #include "isom.h"
  39. /* maximum size in which we look for synchronisation if
  40. synchronisation is lost */
  41. #define MAX_RESYNC_SIZE 65536
  42. #define MAX_PES_PAYLOAD 200*1024
  43. #define MAX_MP4_DESCR_COUNT 16
  44. enum MpegTSFilterType {
  45. MPEGTS_PES,
  46. MPEGTS_SECTION,
  47. };
  48. typedef struct MpegTSFilter MpegTSFilter;
  49. typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos, int64_t cur_pcr);
  50. typedef struct MpegTSPESFilter {
  51. PESCallback *pes_cb;
  52. void *opaque;
  53. } MpegTSPESFilter;
  54. typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
  55. typedef void SetServiceCallback(void *opaque, int ret);
  56. typedef struct MpegTSSectionFilter {
  57. int section_index;
  58. int section_h_size;
  59. uint8_t *section_buf;
  60. unsigned int check_crc:1;
  61. unsigned int end_of_section_reached:1;
  62. SectionCallback *section_cb;
  63. void *opaque;
  64. } MpegTSSectionFilter;
  65. struct MpegTSFilter {
  66. int pid;
  67. int es_id;
  68. int last_cc; /* last cc code (-1 if first packet) */
  69. enum MpegTSFilterType type;
  70. union {
  71. MpegTSPESFilter pes_filter;
  72. MpegTSSectionFilter section_filter;
  73. } u;
  74. };
  75. #define MAX_PIDS_PER_PROGRAM 64
  76. struct Program {
  77. unsigned int id; //program id/service id
  78. unsigned int nb_pids;
  79. unsigned int pids[MAX_PIDS_PER_PROGRAM];
  80. };
  81. struct MpegTSContext {
  82. const AVClass *class;
  83. /* user data */
  84. AVFormatContext *stream;
  85. /** raw packet size, including FEC if present */
  86. int raw_packet_size;
  87. int size_stat[3];
  88. int size_stat_count;
  89. #define SIZE_STAT_THRESHOLD 10
  90. int64_t pos47_full;
  91. /** if true, all pids are analyzed to find streams */
  92. int auto_guess;
  93. /** compute exact PCR for each transport stream packet */
  94. int mpeg2ts_compute_pcr;
  95. /** fix dvb teletext pts */
  96. int fix_teletext_pts;
  97. int64_t cur_pcr; /**< used to estimate the exact PCR */
  98. int pcr_incr; /**< used to estimate the exact PCR */
  99. /* data needed to handle file based ts */
  100. /** stop parsing loop */
  101. int stop_parse;
  102. /** packet containing Audio/Video data */
  103. AVPacket *pkt;
  104. /** to detect seek */
  105. int64_t last_pos;
  106. /******************************************/
  107. /* private mpegts data */
  108. /* scan context */
  109. /** structure to keep track of Program->pids mapping */
  110. unsigned int nb_prg;
  111. struct Program *prg;
  112. int8_t crc_validity[NB_PID_MAX];
  113. /** filters for various streams specified by PMT + for the PAT and PMT */
  114. MpegTSFilter *pids[NB_PID_MAX];
  115. int current_pid;
  116. };
  117. static const AVOption mpegtsraw_options[] = {
  118. {"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT,
  119. {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  120. { NULL },
  121. };
  122. static const AVClass mpegtsraw_class = {
  123. .class_name = "mpegtsraw demuxer",
  124. .item_name = av_default_item_name,
  125. .option = mpegtsraw_options,
  126. .version = LIBAVUTIL_VERSION_INT,
  127. };
  128. static const AVOption mpegts_options[] = {
  129. {"fix_teletext_pts", "Try to fix pts values of dvb teletext streams.", offsetof(MpegTSContext, fix_teletext_pts), AV_OPT_TYPE_INT,
  130. {.i64 = 1}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  131. { NULL },
  132. };
  133. static const AVClass mpegts_class = {
  134. .class_name = "mpegts demuxer",
  135. .item_name = av_default_item_name,
  136. .option = mpegts_options,
  137. .version = LIBAVUTIL_VERSION_INT,
  138. };
  139. /* TS stream handling */
  140. enum MpegTSState {
  141. MPEGTS_HEADER = 0,
  142. MPEGTS_PESHEADER,
  143. MPEGTS_PESHEADER_FILL,
  144. MPEGTS_PAYLOAD,
  145. MPEGTS_SKIP,
  146. };
  147. /* enough for PES header + length */
  148. #define PES_START_SIZE 6
  149. #define PES_HEADER_SIZE 9
  150. #define MAX_PES_HEADER_SIZE (9 + 255)
  151. typedef struct PESContext {
  152. int pid;
  153. int pcr_pid; /**< if -1 then all packets containing PCR are considered */
  154. int stream_type;
  155. MpegTSContext *ts;
  156. AVFormatContext *stream;
  157. AVStream *st;
  158. AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
  159. enum MpegTSState state;
  160. /* used to get the format */
  161. int data_index;
  162. int flags; /**< copied to the AVPacket flags */
  163. int total_size;
  164. int pes_header_size;
  165. int extended_stream_id;
  166. int64_t pts, dts;
  167. int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
  168. uint8_t header[MAX_PES_HEADER_SIZE];
  169. AVBufferRef *buffer;
  170. SLConfigDescr sl;
  171. int64_t last_pcr;
  172. } PESContext;
  173. extern AVInputFormat ff_mpegts_demuxer;
  174. static void clear_avprogram(MpegTSContext *ts, unsigned int programid)
  175. {
  176. AVProgram *prg = NULL;
  177. int i;
  178. for(i=0; i<ts->stream->nb_programs; i++)
  179. if(ts->stream->programs[i]->id == programid){
  180. prg = ts->stream->programs[i];
  181. break;
  182. }
  183. if (!prg)
  184. return;
  185. prg->nb_stream_indexes = 0;
  186. }
  187. static void clear_program(MpegTSContext *ts, unsigned int programid)
  188. {
  189. int i;
  190. clear_avprogram(ts, programid);
  191. for(i=0; i<ts->nb_prg; i++)
  192. if(ts->prg[i].id == programid)
  193. ts->prg[i].nb_pids = 0;
  194. }
  195. static void clear_programs(MpegTSContext *ts)
  196. {
  197. av_freep(&ts->prg);
  198. ts->nb_prg=0;
  199. }
  200. static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
  201. {
  202. struct Program *p;
  203. if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) {
  204. ts->nb_prg = 0;
  205. return;
  206. }
  207. p = &ts->prg[ts->nb_prg];
  208. p->id = programid;
  209. p->nb_pids = 0;
  210. ts->nb_prg++;
  211. }
  212. static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
  213. {
  214. int i;
  215. struct Program *p = NULL;
  216. for(i=0; i<ts->nb_prg; i++) {
  217. if(ts->prg[i].id == programid) {
  218. p = &ts->prg[i];
  219. break;
  220. }
  221. }
  222. if(!p)
  223. return;
  224. if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
  225. return;
  226. p->pids[p->nb_pids++] = pid;
  227. }
  228. static void set_pcr_pid(AVFormatContext *s, unsigned int programid, unsigned int pid)
  229. {
  230. int i;
  231. for(i=0; i<s->nb_programs; i++) {
  232. if(s->programs[i]->id == programid) {
  233. s->programs[i]->pcr_pid = pid;
  234. break;
  235. }
  236. }
  237. }
  238. /**
  239. * @brief discard_pid() decides if the pid is to be discarded according
  240. * to caller's programs selection
  241. * @param ts : - TS context
  242. * @param pid : - pid
  243. * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
  244. * 0 otherwise
  245. */
  246. static int discard_pid(MpegTSContext *ts, unsigned int pid)
  247. {
  248. int i, j, k;
  249. int used = 0, discarded = 0;
  250. struct Program *p;
  251. /* If none of the programs have .discard=AVDISCARD_ALL then there's
  252. * no way we have to discard this packet
  253. */
  254. for (k = 0; k < ts->stream->nb_programs; k++) {
  255. if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
  256. break;
  257. }
  258. if (k == ts->stream->nb_programs)
  259. return 0;
  260. for(i=0; i<ts->nb_prg; i++) {
  261. p = &ts->prg[i];
  262. for(j=0; j<p->nb_pids; j++) {
  263. if(p->pids[j] != pid)
  264. continue;
  265. //is program with id p->id set to be discarded?
  266. for(k=0; k<ts->stream->nb_programs; k++) {
  267. if(ts->stream->programs[k]->id == p->id) {
  268. if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
  269. discarded++;
  270. else
  271. used++;
  272. }
  273. }
  274. }
  275. }
  276. return !used && discarded;
  277. }
  278. /**
  279. * Assemble PES packets out of TS packets, and then call the "section_cb"
  280. * function when they are complete.
  281. */
  282. static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
  283. const uint8_t *buf, int buf_size, int is_start)
  284. {
  285. MpegTSContext *ts = s->priv_data;
  286. MpegTSSectionFilter *tss = &tss1->u.section_filter;
  287. int len;
  288. if (is_start) {
  289. memcpy(tss->section_buf, buf, buf_size);
  290. tss->section_index = buf_size;
  291. tss->section_h_size = -1;
  292. tss->end_of_section_reached = 0;
  293. } else {
  294. if (tss->end_of_section_reached)
  295. return;
  296. len = 4096 - tss->section_index;
  297. if (buf_size < len)
  298. len = buf_size;
  299. memcpy(tss->section_buf + tss->section_index, buf, len);
  300. tss->section_index += len;
  301. }
  302. /* compute section length if possible */
  303. if (tss->section_h_size == -1 && tss->section_index >= 3) {
  304. len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
  305. if (len > 4096)
  306. return;
  307. tss->section_h_size = len;
  308. }
  309. if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
  310. int crc_valid = 1;
  311. tss->end_of_section_reached = 1;
  312. if (tss->check_crc){
  313. crc_valid = !av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, tss->section_buf, tss->section_h_size);
  314. if (crc_valid){
  315. ts->crc_validity[ tss1->pid ] = 100;
  316. }else if(ts->crc_validity[ tss1->pid ] > -10){
  317. ts->crc_validity[ tss1->pid ]--;
  318. }else
  319. crc_valid = 2;
  320. }
  321. if (crc_valid)
  322. tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
  323. }
  324. }
  325. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
  326. SectionCallback *section_cb, void *opaque,
  327. int check_crc)
  328. {
  329. MpegTSFilter *filter;
  330. MpegTSSectionFilter *sec;
  331. av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
  332. if (pid >= NB_PID_MAX || ts->pids[pid])
  333. return NULL;
  334. filter = av_mallocz(sizeof(MpegTSFilter));
  335. if (!filter)
  336. return NULL;
  337. ts->pids[pid] = filter;
  338. filter->type = MPEGTS_SECTION;
  339. filter->pid = pid;
  340. filter->es_id = -1;
  341. filter->last_cc = -1;
  342. sec = &filter->u.section_filter;
  343. sec->section_cb = section_cb;
  344. sec->opaque = opaque;
  345. sec->section_buf = av_malloc(MAX_SECTION_SIZE);
  346. sec->check_crc = check_crc;
  347. if (!sec->section_buf) {
  348. av_free(filter);
  349. return NULL;
  350. }
  351. return filter;
  352. }
  353. static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
  354. PESCallback *pes_cb,
  355. void *opaque)
  356. {
  357. MpegTSFilter *filter;
  358. MpegTSPESFilter *pes;
  359. if (pid >= NB_PID_MAX || ts->pids[pid])
  360. return NULL;
  361. filter = av_mallocz(sizeof(MpegTSFilter));
  362. if (!filter)
  363. return NULL;
  364. ts->pids[pid] = filter;
  365. filter->type = MPEGTS_PES;
  366. filter->pid = pid;
  367. filter->es_id = -1;
  368. filter->last_cc = -1;
  369. pes = &filter->u.pes_filter;
  370. pes->pes_cb = pes_cb;
  371. pes->opaque = opaque;
  372. return filter;
  373. }
  374. static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
  375. {
  376. int pid;
  377. pid = filter->pid;
  378. if (filter->type == MPEGTS_SECTION)
  379. av_freep(&filter->u.section_filter.section_buf);
  380. else if (filter->type == MPEGTS_PES) {
  381. PESContext *pes = filter->u.pes_filter.opaque;
  382. av_buffer_unref(&pes->buffer);
  383. /* referenced private data will be freed later in
  384. * avformat_close_input */
  385. if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
  386. av_freep(&filter->u.pes_filter.opaque);
  387. }
  388. }
  389. av_free(filter);
  390. ts->pids[pid] = NULL;
  391. }
  392. static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
  393. int stat[TS_MAX_PACKET_SIZE];
  394. int i;
  395. int x=0;
  396. int best_score=0;
  397. memset(stat, 0, packet_size*sizeof(int));
  398. for(x=i=0; i<size-3; i++){
  399. if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && buf[i+3] != 0x47){
  400. stat[x]++;
  401. if(stat[x] > best_score){
  402. best_score= stat[x];
  403. if(index) *index= x;
  404. }
  405. }
  406. x++;
  407. if(x == packet_size) x= 0;
  408. }
  409. return best_score;
  410. }
  411. /* autodetect fec presence. Must have at least 1024 bytes */
  412. static int get_packet_size(const uint8_t *buf, int size)
  413. {
  414. int score, fec_score, dvhs_score;
  415. if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
  416. return -1;
  417. score = analyze(buf, size, TS_PACKET_SIZE, NULL);
  418. dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
  419. fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
  420. av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n",
  421. score, dvhs_score, fec_score);
  422. if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
  423. else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
  424. else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
  425. else return -1;
  426. }
  427. typedef struct SectionHeader {
  428. uint8_t tid;
  429. uint16_t id;
  430. uint8_t version;
  431. uint8_t sec_num;
  432. uint8_t last_sec_num;
  433. } SectionHeader;
  434. static inline int get8(const uint8_t **pp, const uint8_t *p_end)
  435. {
  436. const uint8_t *p;
  437. int c;
  438. p = *pp;
  439. if (p >= p_end)
  440. return -1;
  441. c = *p++;
  442. *pp = p;
  443. return c;
  444. }
  445. static inline int get16(const uint8_t **pp, const uint8_t *p_end)
  446. {
  447. const uint8_t *p;
  448. int c;
  449. p = *pp;
  450. if ((p + 1) >= p_end)
  451. return -1;
  452. c = AV_RB16(p);
  453. p += 2;
  454. *pp = p;
  455. return c;
  456. }
  457. /* read and allocate a DVB string preceded by its length */
  458. static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
  459. {
  460. int len;
  461. const uint8_t *p;
  462. char *str;
  463. p = *pp;
  464. len = get8(&p, p_end);
  465. if (len < 0)
  466. return NULL;
  467. if ((p + len) > p_end)
  468. return NULL;
  469. str = av_malloc(len + 1);
  470. if (!str)
  471. return NULL;
  472. memcpy(str, p, len);
  473. str[len] = '\0';
  474. p += len;
  475. *pp = p;
  476. return str;
  477. }
  478. static int parse_section_header(SectionHeader *h,
  479. const uint8_t **pp, const uint8_t *p_end)
  480. {
  481. int val;
  482. val = get8(pp, p_end);
  483. if (val < 0)
  484. return -1;
  485. h->tid = val;
  486. *pp += 2;
  487. val = get16(pp, p_end);
  488. if (val < 0)
  489. return -1;
  490. h->id = val;
  491. val = get8(pp, p_end);
  492. if (val < 0)
  493. return -1;
  494. h->version = (val >> 1) & 0x1f;
  495. val = get8(pp, p_end);
  496. if (val < 0)
  497. return -1;
  498. h->sec_num = val;
  499. val = get8(pp, p_end);
  500. if (val < 0)
  501. return -1;
  502. h->last_sec_num = val;
  503. return 0;
  504. }
  505. typedef struct {
  506. uint32_t stream_type;
  507. enum AVMediaType codec_type;
  508. enum AVCodecID codec_id;
  509. } StreamType;
  510. static const StreamType ISO_types[] = {
  511. { 0x01, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
  512. { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
  513. { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
  514. { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
  515. { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
  516. { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 },
  517. /* Makito encoder sets stream type 0x11 for AAC,
  518. * so auto-detect LOAS/LATM instead of hardcoding it. */
  519. #if !CONFIG_LOAS_DEMUXER
  520. { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
  521. #endif
  522. { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
  523. { 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
  524. { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS },
  525. { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
  526. { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
  527. { 0 },
  528. };
  529. static const StreamType HDMV_types[] = {
  530. { 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY },
  531. { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  532. { 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  533. { 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD },
  534. { 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
  535. { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
  536. { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
  537. { 0xa1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC3 Secondary Audio */
  538. { 0xa2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS Express Secondary Audio */
  539. { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
  540. { 0 },
  541. };
  542. /* ATSC ? */
  543. static const StreamType MISC_types[] = {
  544. { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  545. { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  546. { 0 },
  547. };
  548. static const StreamType REGD_types[] = {
  549. { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
  550. { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
  551. { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
  552. { MKTAG('D','T','S','1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  553. { MKTAG('D','T','S','2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  554. { MKTAG('D','T','S','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  555. { MKTAG('H','E','V','C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
  556. { MKTAG('K','L','V','A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV },
  557. { MKTAG('V','C','-','1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
  558. { 0 },
  559. };
  560. /* descriptor present */
  561. static const StreamType DESC_types[] = {
  562. { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
  563. { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
  564. { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  565. { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
  566. { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
  567. { 0 },
  568. };
  569. static void mpegts_find_stream_type(AVStream *st,
  570. uint32_t stream_type, const StreamType *types)
  571. {
  572. if (avcodec_is_open(st->codec)) {
  573. av_log(NULL, AV_LOG_DEBUG, "cannot set stream info, codec is open\n");
  574. return;
  575. }
  576. for (; types->stream_type; types++) {
  577. if (stream_type == types->stream_type) {
  578. st->codec->codec_type = types->codec_type;
  579. st->codec->codec_id = types->codec_id;
  580. st->request_probe = 0;
  581. return;
  582. }
  583. }
  584. }
  585. static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
  586. uint32_t stream_type, uint32_t prog_reg_desc)
  587. {
  588. int old_codec_type= st->codec->codec_type;
  589. int old_codec_id = st->codec->codec_id;
  590. if (avcodec_is_open(st->codec)) {
  591. av_log(pes->stream, AV_LOG_DEBUG, "cannot set stream info, codec is open\n");
  592. return 0;
  593. }
  594. avpriv_set_pts_info(st, 33, 1, 90000);
  595. st->priv_data = pes;
  596. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  597. st->codec->codec_id = AV_CODEC_ID_NONE;
  598. st->need_parsing = AVSTREAM_PARSE_FULL;
  599. pes->st = st;
  600. pes->stream_type = stream_type;
  601. av_log(pes->stream, AV_LOG_DEBUG,
  602. "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
  603. st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
  604. st->codec->codec_tag = pes->stream_type;
  605. mpegts_find_stream_type(st, pes->stream_type, ISO_types);
  606. if ((prog_reg_desc == AV_RL32("HDMV") ||
  607. prog_reg_desc == AV_RL32("HDPR")) &&
  608. st->codec->codec_id == AV_CODEC_ID_NONE) {
  609. mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
  610. if (pes->stream_type == 0x83) {
  611. // HDMV TrueHD streams also contain an AC3 coded version of the
  612. // audio track - add a second stream for this
  613. AVStream *sub_st;
  614. // priv_data cannot be shared between streams
  615. PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
  616. if (!sub_pes)
  617. return AVERROR(ENOMEM);
  618. memcpy(sub_pes, pes, sizeof(*sub_pes));
  619. sub_st = avformat_new_stream(pes->stream, NULL);
  620. if (!sub_st) {
  621. av_free(sub_pes);
  622. return AVERROR(ENOMEM);
  623. }
  624. sub_st->id = pes->pid;
  625. avpriv_set_pts_info(sub_st, 33, 1, 90000);
  626. sub_st->priv_data = sub_pes;
  627. sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  628. sub_st->codec->codec_id = AV_CODEC_ID_AC3;
  629. sub_st->need_parsing = AVSTREAM_PARSE_FULL;
  630. sub_pes->sub_st = pes->sub_st = sub_st;
  631. }
  632. }
  633. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  634. mpegts_find_stream_type(st, pes->stream_type, MISC_types);
  635. if (st->codec->codec_id == AV_CODEC_ID_NONE){
  636. st->codec->codec_id = old_codec_id;
  637. st->codec->codec_type= old_codec_type;
  638. }
  639. return 0;
  640. }
  641. static void new_pes_packet(PESContext *pes, AVPacket *pkt)
  642. {
  643. av_init_packet(pkt);
  644. pkt->buf = pes->buffer;
  645. pkt->data = pes->buffer->data;
  646. pkt->size = pes->data_index;
  647. if(pes->total_size != MAX_PES_PAYLOAD &&
  648. pes->pes_header_size + pes->data_index != pes->total_size + PES_START_SIZE) {
  649. av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
  650. pes->flags |= AV_PKT_FLAG_CORRUPT;
  651. }
  652. memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  653. // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
  654. if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
  655. pkt->stream_index = pes->sub_st->index;
  656. else
  657. pkt->stream_index = pes->st->index;
  658. pkt->pts = pes->pts;
  659. pkt->dts = pes->dts;
  660. /* store position of first TS packet of this PES packet */
  661. pkt->pos = pes->ts_packet_pos;
  662. pkt->flags = pes->flags;
  663. /* reset pts values */
  664. pes->pts = AV_NOPTS_VALUE;
  665. pes->dts = AV_NOPTS_VALUE;
  666. pes->buffer = NULL;
  667. pes->data_index = 0;
  668. pes->flags = 0;
  669. }
  670. static uint64_t get_ts64(GetBitContext *gb, int bits)
  671. {
  672. if (get_bits_left(gb) < bits)
  673. return AV_NOPTS_VALUE;
  674. return get_bits64(gb, bits);
  675. }
  676. static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
  677. {
  678. GetBitContext gb;
  679. int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
  680. int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
  681. int dts_flag = -1, cts_flag = -1;
  682. int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
  683. init_get_bits(&gb, buf, buf_size*8);
  684. if (sl->use_au_start)
  685. au_start_flag = get_bits1(&gb);
  686. if (sl->use_au_end)
  687. au_end_flag = get_bits1(&gb);
  688. if (!sl->use_au_start && !sl->use_au_end)
  689. au_start_flag = au_end_flag = 1;
  690. if (sl->ocr_len > 0)
  691. ocr_flag = get_bits1(&gb);
  692. if (sl->use_idle)
  693. idle_flag = get_bits1(&gb);
  694. if (sl->use_padding)
  695. padding_flag = get_bits1(&gb);
  696. if (padding_flag)
  697. padding_bits = get_bits(&gb, 3);
  698. if (!idle_flag && (!padding_flag || padding_bits != 0)) {
  699. if (sl->packet_seq_num_len)
  700. skip_bits_long(&gb, sl->packet_seq_num_len);
  701. if (sl->degr_prior_len)
  702. if (get_bits1(&gb))
  703. skip_bits(&gb, sl->degr_prior_len);
  704. if (ocr_flag)
  705. skip_bits_long(&gb, sl->ocr_len);
  706. if (au_start_flag) {
  707. if (sl->use_rand_acc_pt)
  708. get_bits1(&gb);
  709. if (sl->au_seq_num_len > 0)
  710. skip_bits_long(&gb, sl->au_seq_num_len);
  711. if (sl->use_timestamps) {
  712. dts_flag = get_bits1(&gb);
  713. cts_flag = get_bits1(&gb);
  714. }
  715. }
  716. if (sl->inst_bitrate_len)
  717. inst_bitrate_flag = get_bits1(&gb);
  718. if (dts_flag == 1)
  719. dts = get_ts64(&gb, sl->timestamp_len);
  720. if (cts_flag == 1)
  721. cts = get_ts64(&gb, sl->timestamp_len);
  722. if (sl->au_len > 0)
  723. skip_bits_long(&gb, sl->au_len);
  724. if (inst_bitrate_flag)
  725. skip_bits_long(&gb, sl->inst_bitrate_len);
  726. }
  727. if (dts != AV_NOPTS_VALUE)
  728. pes->dts = dts;
  729. if (cts != AV_NOPTS_VALUE)
  730. pes->pts = cts;
  731. if (sl->timestamp_len && sl->timestamp_res)
  732. avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
  733. return (get_bits_count(&gb) + 7) >> 3;
  734. }
  735. /* return non zero if a packet could be constructed */
  736. static int mpegts_push_data(MpegTSFilter *filter,
  737. const uint8_t *buf, int buf_size, int is_start,
  738. int64_t pos, int64_t pcr)
  739. {
  740. PESContext *pes = filter->u.pes_filter.opaque;
  741. MpegTSContext *ts = pes->ts;
  742. const uint8_t *p;
  743. int len, code;
  744. if(!ts->pkt)
  745. return 0;
  746. if (pcr != -1)
  747. pes->last_pcr = pcr;
  748. if (is_start) {
  749. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  750. new_pes_packet(pes, ts->pkt);
  751. ts->stop_parse = 1;
  752. }
  753. pes->state = MPEGTS_HEADER;
  754. pes->data_index = 0;
  755. pes->ts_packet_pos = pos;
  756. }
  757. p = buf;
  758. while (buf_size > 0) {
  759. switch(pes->state) {
  760. case MPEGTS_HEADER:
  761. len = PES_START_SIZE - pes->data_index;
  762. if (len > buf_size)
  763. len = buf_size;
  764. memcpy(pes->header + pes->data_index, p, len);
  765. pes->data_index += len;
  766. p += len;
  767. buf_size -= len;
  768. if (pes->data_index == PES_START_SIZE) {
  769. /* we got all the PES or section header. We can now
  770. decide */
  771. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  772. pes->header[2] == 0x01) {
  773. /* it must be an mpeg2 PES stream */
  774. code = pes->header[3] | 0x100;
  775. av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
  776. if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
  777. (!pes->sub_st || pes->sub_st->discard == AVDISCARD_ALL)) ||
  778. code == 0x1be) /* padding_stream */
  779. goto skip;
  780. /* stream not present in PMT */
  781. if (!pes->st) {
  782. pes->st = avformat_new_stream(ts->stream, NULL);
  783. if (!pes->st)
  784. return AVERROR(ENOMEM);
  785. pes->st->id = pes->pid;
  786. mpegts_set_stream_info(pes->st, pes, 0, 0);
  787. }
  788. pes->total_size = AV_RB16(pes->header + 4);
  789. /* NOTE: a zero total size means the PES size is
  790. unbounded */
  791. if (!pes->total_size)
  792. pes->total_size = MAX_PES_PAYLOAD;
  793. /* allocate pes buffer */
  794. pes->buffer = av_buffer_alloc(pes->total_size +
  795. FF_INPUT_BUFFER_PADDING_SIZE);
  796. if (!pes->buffer)
  797. return AVERROR(ENOMEM);
  798. if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
  799. code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
  800. code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
  801. code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
  802. pes->state = MPEGTS_PESHEADER;
  803. if (pes->st->codec->codec_id == AV_CODEC_ID_NONE && !pes->st->request_probe) {
  804. av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
  805. pes->pid, pes->stream_type);
  806. pes->st->request_probe= 1;
  807. }
  808. } else {
  809. pes->state = MPEGTS_PAYLOAD;
  810. pes->data_index = 0;
  811. }
  812. } else {
  813. /* otherwise, it should be a table */
  814. /* skip packet */
  815. skip:
  816. pes->state = MPEGTS_SKIP;
  817. continue;
  818. }
  819. }
  820. break;
  821. /**********************************************/
  822. /* PES packing parsing */
  823. case MPEGTS_PESHEADER:
  824. len = PES_HEADER_SIZE - pes->data_index;
  825. if (len < 0)
  826. return -1;
  827. if (len > buf_size)
  828. len = buf_size;
  829. memcpy(pes->header + pes->data_index, p, len);
  830. pes->data_index += len;
  831. p += len;
  832. buf_size -= len;
  833. if (pes->data_index == PES_HEADER_SIZE) {
  834. pes->pes_header_size = pes->header[8] + 9;
  835. pes->state = MPEGTS_PESHEADER_FILL;
  836. }
  837. break;
  838. case MPEGTS_PESHEADER_FILL:
  839. len = pes->pes_header_size - pes->data_index;
  840. if (len < 0)
  841. return -1;
  842. if (len > buf_size)
  843. len = buf_size;
  844. memcpy(pes->header + pes->data_index, p, len);
  845. pes->data_index += len;
  846. p += len;
  847. buf_size -= len;
  848. if (pes->data_index == pes->pes_header_size) {
  849. const uint8_t *r;
  850. unsigned int flags, pes_ext, skip;
  851. flags = pes->header[7];
  852. r = pes->header + 9;
  853. pes->pts = AV_NOPTS_VALUE;
  854. pes->dts = AV_NOPTS_VALUE;
  855. if ((flags & 0xc0) == 0x80) {
  856. pes->dts = pes->pts = ff_parse_pes_pts(r);
  857. r += 5;
  858. } else if ((flags & 0xc0) == 0xc0) {
  859. pes->pts = ff_parse_pes_pts(r);
  860. r += 5;
  861. pes->dts = ff_parse_pes_pts(r);
  862. r += 5;
  863. }
  864. pes->extended_stream_id = -1;
  865. if (flags & 0x01) { /* PES extension */
  866. pes_ext = *r++;
  867. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  868. skip = (pes_ext >> 4) & 0xb;
  869. skip += skip & 0x9;
  870. r += skip;
  871. if ((pes_ext & 0x41) == 0x01 &&
  872. (r + 2) <= (pes->header + pes->pes_header_size)) {
  873. /* PES extension 2 */
  874. if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
  875. pes->extended_stream_id = r[1];
  876. }
  877. }
  878. /* we got the full header. We parse it and get the payload */
  879. pes->state = MPEGTS_PAYLOAD;
  880. pes->data_index = 0;
  881. if (pes->stream_type == 0x12 && buf_size > 0) {
  882. int sl_header_bytes = read_sl_header(pes, &pes->sl, p, buf_size);
  883. pes->pes_header_size += sl_header_bytes;
  884. p += sl_header_bytes;
  885. buf_size -= sl_header_bytes;
  886. }
  887. if (pes->ts->fix_teletext_pts && pes->st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
  888. AVProgram *p = NULL;
  889. while ((p = av_find_program_from_stream(pes->stream, p, pes->st->index))) {
  890. if (p->pcr_pid != -1 && p->discard != AVDISCARD_ALL) {
  891. MpegTSFilter *f = pes->ts->pids[p->pcr_pid];
  892. if (f && f->type == MPEGTS_PES) {
  893. PESContext *pcrpes = f->u.pes_filter.opaque;
  894. if (pcrpes && pcrpes->last_pcr != -1 && pcrpes->st && pcrpes->st->discard != AVDISCARD_ALL) {
  895. // teletext packets do not always have correct timestamps,
  896. // the standard says they should be handled after 40.6 ms at most,
  897. // and the pcr error to this packet should be no more than 100 ms.
  898. // TODO: we should interpolate the PCR, not just use the last one
  899. int64_t pcr = pcrpes->last_pcr / 300;
  900. pes->st->pts_wrap_reference = pcrpes->st->pts_wrap_reference;
  901. pes->st->pts_wrap_behavior = pcrpes->st->pts_wrap_behavior;
  902. if (pes->dts == AV_NOPTS_VALUE || pes->dts < pcr) {
  903. pes->pts = pes->dts = pcr;
  904. } else if (pes->dts > pcr + 3654 + 9000) {
  905. pes->pts = pes->dts = pcr + 3654 + 9000;
  906. }
  907. break;
  908. }
  909. }
  910. }
  911. }
  912. }
  913. }
  914. break;
  915. case MPEGTS_PAYLOAD:
  916. if (buf_size > 0 && pes->buffer) {
  917. if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
  918. new_pes_packet(pes, ts->pkt);
  919. pes->total_size = MAX_PES_PAYLOAD;
  920. pes->buffer = av_buffer_alloc(pes->total_size + FF_INPUT_BUFFER_PADDING_SIZE);
  921. if (!pes->buffer)
  922. return AVERROR(ENOMEM);
  923. ts->stop_parse = 1;
  924. } else if (pes->data_index == 0 && buf_size > pes->total_size) {
  925. // pes packet size is < ts size packet and pes data is padded with 0xff
  926. // not sure if this is legal in ts but see issue #2392
  927. buf_size = pes->total_size;
  928. }
  929. memcpy(pes->buffer->data + pes->data_index, p, buf_size);
  930. pes->data_index += buf_size;
  931. }
  932. buf_size = 0;
  933. /* emit complete packets with known packet size
  934. * decreases demuxer delay for infrequent packets like subtitles from
  935. * a couple of seconds to milliseconds for properly muxed files.
  936. * total_size is the number of bytes following pes_packet_length
  937. * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
  938. if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
  939. pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
  940. ts->stop_parse = 1;
  941. new_pes_packet(pes, ts->pkt);
  942. }
  943. break;
  944. case MPEGTS_SKIP:
  945. buf_size = 0;
  946. break;
  947. }
  948. }
  949. return 0;
  950. }
  951. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
  952. {
  953. MpegTSFilter *tss;
  954. PESContext *pes;
  955. /* if no pid found, then add a pid context */
  956. pes = av_mallocz(sizeof(PESContext));
  957. if (!pes)
  958. return 0;
  959. pes->ts = ts;
  960. pes->stream = ts->stream;
  961. pes->pid = pid;
  962. pes->pcr_pid = pcr_pid;
  963. pes->state = MPEGTS_SKIP;
  964. pes->pts = AV_NOPTS_VALUE;
  965. pes->dts = AV_NOPTS_VALUE;
  966. pes->last_pcr = -1;
  967. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  968. if (!tss) {
  969. av_free(pes);
  970. return 0;
  971. }
  972. return pes;
  973. }
  974. #define MAX_LEVEL 4
  975. typedef struct {
  976. AVFormatContext *s;
  977. AVIOContext pb;
  978. Mp4Descr *descr;
  979. Mp4Descr *active_descr;
  980. int descr_count;
  981. int max_descr_count;
  982. int level;
  983. } MP4DescrParseContext;
  984. static int init_MP4DescrParseContext(
  985. MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf,
  986. unsigned size, Mp4Descr *descr, int max_descr_count)
  987. {
  988. int ret;
  989. if (size > (1<<30))
  990. return AVERROR_INVALIDDATA;
  991. if ((ret = ffio_init_context(&d->pb, (unsigned char*)buf, size, 0,
  992. NULL, NULL, NULL, NULL)) < 0)
  993. return ret;
  994. d->s = s;
  995. d->level = 0;
  996. d->descr_count = 0;
  997. d->descr = descr;
  998. d->active_descr = NULL;
  999. d->max_descr_count = max_descr_count;
  1000. return 0;
  1001. }
  1002. static void update_offsets(AVIOContext *pb, int64_t *off, int *len) {
  1003. int64_t new_off = avio_tell(pb);
  1004. (*len) -= new_off - *off;
  1005. *off = new_off;
  1006. }
  1007. static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
  1008. int target_tag);
  1009. static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
  1010. {
  1011. while (len > 0) {
  1012. if (parse_mp4_descr(d, off, len, 0) < 0)
  1013. return -1;
  1014. update_offsets(&d->pb, &off, &len);
  1015. }
  1016. return 0;
  1017. }
  1018. static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1019. {
  1020. avio_rb16(&d->pb); // ID
  1021. avio_r8(&d->pb);
  1022. avio_r8(&d->pb);
  1023. avio_r8(&d->pb);
  1024. avio_r8(&d->pb);
  1025. avio_r8(&d->pb);
  1026. update_offsets(&d->pb, &off, &len);
  1027. return parse_mp4_descr_arr(d, off, len);
  1028. }
  1029. static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1030. {
  1031. int id_flags;
  1032. if (len < 2)
  1033. return 0;
  1034. id_flags = avio_rb16(&d->pb);
  1035. if (!(id_flags & 0x0020)) { //URL_Flag
  1036. update_offsets(&d->pb, &off, &len);
  1037. return parse_mp4_descr_arr(d, off, len); //ES_Descriptor[]
  1038. } else {
  1039. return 0;
  1040. }
  1041. }
  1042. static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1043. {
  1044. int es_id = 0;
  1045. if (d->descr_count >= d->max_descr_count)
  1046. return -1;
  1047. ff_mp4_parse_es_descr(&d->pb, &es_id);
  1048. d->active_descr = d->descr + (d->descr_count++);
  1049. d->active_descr->es_id = es_id;
  1050. update_offsets(&d->pb, &off, &len);
  1051. parse_mp4_descr(d, off, len, MP4DecConfigDescrTag);
  1052. update_offsets(&d->pb, &off, &len);
  1053. if (len > 0)
  1054. parse_mp4_descr(d, off, len, MP4SLDescrTag);
  1055. d->active_descr = NULL;
  1056. return 0;
  1057. }
  1058. static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1059. {
  1060. Mp4Descr *descr = d->active_descr;
  1061. if (!descr)
  1062. return -1;
  1063. d->active_descr->dec_config_descr = av_malloc(len);
  1064. if (!descr->dec_config_descr)
  1065. return AVERROR(ENOMEM);
  1066. descr->dec_config_descr_len = len;
  1067. avio_read(&d->pb, descr->dec_config_descr, len);
  1068. return 0;
  1069. }
  1070. static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1071. {
  1072. Mp4Descr *descr = d->active_descr;
  1073. int predefined;
  1074. if (!descr)
  1075. return -1;
  1076. predefined = avio_r8(&d->pb);
  1077. if (!predefined) {
  1078. int lengths;
  1079. int flags = avio_r8(&d->pb);
  1080. descr->sl.use_au_start = !!(flags & 0x80);
  1081. descr->sl.use_au_end = !!(flags & 0x40);
  1082. descr->sl.use_rand_acc_pt = !!(flags & 0x20);
  1083. descr->sl.use_padding = !!(flags & 0x08);
  1084. descr->sl.use_timestamps = !!(flags & 0x04);
  1085. descr->sl.use_idle = !!(flags & 0x02);
  1086. descr->sl.timestamp_res = avio_rb32(&d->pb);
  1087. avio_rb32(&d->pb);
  1088. descr->sl.timestamp_len = avio_r8(&d->pb);
  1089. descr->sl.ocr_len = avio_r8(&d->pb);
  1090. descr->sl.au_len = avio_r8(&d->pb);
  1091. descr->sl.inst_bitrate_len = avio_r8(&d->pb);
  1092. lengths = avio_rb16(&d->pb);
  1093. descr->sl.degr_prior_len = lengths >> 12;
  1094. descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
  1095. descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
  1096. } else {
  1097. avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
  1098. }
  1099. return 0;
  1100. }
  1101. static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
  1102. int target_tag) {
  1103. int tag;
  1104. int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
  1105. update_offsets(&d->pb, &off, &len);
  1106. if (len < 0 || len1 > len || len1 <= 0) {
  1107. av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
  1108. return -1;
  1109. }
  1110. if (d->level++ >= MAX_LEVEL) {
  1111. av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
  1112. goto done;
  1113. }
  1114. if (target_tag && tag != target_tag) {
  1115. av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag);
  1116. goto done;
  1117. }
  1118. switch (tag) {
  1119. case MP4IODescrTag:
  1120. parse_MP4IODescrTag(d, off, len1);
  1121. break;
  1122. case MP4ODescrTag:
  1123. parse_MP4ODescrTag(d, off, len1);
  1124. break;
  1125. case MP4ESDescrTag:
  1126. parse_MP4ESDescrTag(d, off, len1);
  1127. break;
  1128. case MP4DecConfigDescrTag:
  1129. parse_MP4DecConfigDescrTag(d, off, len1);
  1130. break;
  1131. case MP4SLDescrTag:
  1132. parse_MP4SLDescrTag(d, off, len1);
  1133. break;
  1134. }
  1135. done:
  1136. d->level--;
  1137. avio_seek(&d->pb, off + len1, SEEK_SET);
  1138. return 0;
  1139. }
  1140. static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
  1141. Mp4Descr *descr, int *descr_count, int max_descr_count)
  1142. {
  1143. MP4DescrParseContext d;
  1144. if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
  1145. return -1;
  1146. parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
  1147. *descr_count = d.descr_count;
  1148. return 0;
  1149. }
  1150. static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
  1151. Mp4Descr *descr, int *descr_count, int max_descr_count)
  1152. {
  1153. MP4DescrParseContext d;
  1154. if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
  1155. return -1;
  1156. parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
  1157. *descr_count = d.descr_count;
  1158. return 0;
  1159. }
  1160. static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1161. {
  1162. MpegTSContext *ts = filter->u.section_filter.opaque;
  1163. SectionHeader h;
  1164. const uint8_t *p, *p_end;
  1165. AVIOContext pb;
  1166. Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
  1167. int mp4_descr_count = 0;
  1168. int i, pid;
  1169. AVFormatContext *s = ts->stream;
  1170. p_end = section + section_len - 4;
  1171. p = section;
  1172. if (parse_section_header(&h, &p, p_end) < 0)
  1173. return;
  1174. if (h.tid != M4OD_TID)
  1175. return;
  1176. mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT);
  1177. for (pid = 0; pid < NB_PID_MAX; pid++) {
  1178. if (!ts->pids[pid])
  1179. continue;
  1180. for (i = 0; i < mp4_descr_count; i++) {
  1181. PESContext *pes;
  1182. AVStream *st;
  1183. if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
  1184. continue;
  1185. if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
  1186. av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
  1187. continue;
  1188. }
  1189. pes = ts->pids[pid]->u.pes_filter.opaque;
  1190. st = pes->st;
  1191. if (!st) {
  1192. continue;
  1193. }
  1194. pes->sl = mp4_descr[i].sl;
  1195. ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
  1196. mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1197. ff_mp4_read_dec_config_descr(s, st, &pb);
  1198. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1199. st->codec->extradata_size > 0)
  1200. st->need_parsing = 0;
  1201. if (st->codec->codec_id == AV_CODEC_ID_H264 &&
  1202. st->codec->extradata_size > 0)
  1203. st->need_parsing = 0;
  1204. if (st->codec->codec_id <= AV_CODEC_ID_NONE) {
  1205. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO) {
  1206. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1207. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE) {
  1208. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1209. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN) {
  1210. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1211. }
  1212. }
  1213. }
  1214. for (i = 0; i < mp4_descr_count; i++)
  1215. av_free(mp4_descr[i].dec_config_descr);
  1216. }
  1217. int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
  1218. const uint8_t **pp, const uint8_t *desc_list_end,
  1219. Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
  1220. MpegTSContext *ts)
  1221. {
  1222. const uint8_t *desc_end;
  1223. int desc_len, desc_tag, desc_es_id;
  1224. char language[252];
  1225. int i;
  1226. desc_tag = get8(pp, desc_list_end);
  1227. if (desc_tag < 0)
  1228. return -1;
  1229. desc_len = get8(pp, desc_list_end);
  1230. if (desc_len < 0)
  1231. return -1;
  1232. desc_end = *pp + desc_len;
  1233. if (desc_end > desc_list_end)
  1234. return -1;
  1235. av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
  1236. if (st->codec->codec_id == AV_CODEC_ID_NONE &&
  1237. stream_type == STREAM_TYPE_PRIVATE_DATA)
  1238. mpegts_find_stream_type(st, desc_tag, DESC_types);
  1239. switch(desc_tag) {
  1240. case 0x1E: /* SL descriptor */
  1241. desc_es_id = get16(pp, desc_end);
  1242. if (ts && ts->pids[pid])
  1243. ts->pids[pid]->es_id = desc_es_id;
  1244. for (i = 0; i < mp4_descr_count; i++)
  1245. if (mp4_descr[i].dec_config_descr_len &&
  1246. mp4_descr[i].es_id == desc_es_id) {
  1247. AVIOContext pb;
  1248. ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
  1249. mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1250. ff_mp4_read_dec_config_descr(fc, st, &pb);
  1251. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1252. st->codec->extradata_size > 0)
  1253. st->need_parsing = 0;
  1254. if (st->codec->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
  1255. mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
  1256. }
  1257. break;
  1258. case 0x1F: /* FMC descriptor */
  1259. get16(pp, desc_end);
  1260. if (mp4_descr_count > 0 && (st->codec->codec_id == AV_CODEC_ID_AAC_LATM || st->request_probe>0) &&
  1261. mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
  1262. AVIOContext pb;
  1263. ffio_init_context(&pb, mp4_descr->dec_config_descr,
  1264. mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1265. ff_mp4_read_dec_config_descr(fc, st, &pb);
  1266. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1267. st->codec->extradata_size > 0){
  1268. st->request_probe= st->need_parsing = 0;
  1269. st->codec->codec_type= AVMEDIA_TYPE_AUDIO;
  1270. }
  1271. }
  1272. break;
  1273. case 0x56: /* DVB teletext descriptor */
  1274. language[0] = get8(pp, desc_end);
  1275. language[1] = get8(pp, desc_end);
  1276. language[2] = get8(pp, desc_end);
  1277. language[3] = 0;
  1278. av_dict_set(&st->metadata, "language", language, 0);
  1279. break;
  1280. case 0x59: /* subtitling descriptor */
  1281. language[0] = get8(pp, desc_end);
  1282. language[1] = get8(pp, desc_end);
  1283. language[2] = get8(pp, desc_end);
  1284. language[3] = 0;
  1285. /* hearing impaired subtitles detection */
  1286. switch(get8(pp, desc_end)) {
  1287. case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
  1288. case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
  1289. case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
  1290. case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
  1291. case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
  1292. case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
  1293. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  1294. break;
  1295. }
  1296. if (st->codec->extradata) {
  1297. if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
  1298. avpriv_request_sample(fc, "DVB sub with multiple IDs");
  1299. } else {
  1300. if (!ff_alloc_extradata(st->codec, 4)) {
  1301. memcpy(st->codec->extradata, *pp, 4);
  1302. }
  1303. }
  1304. *pp += 4;
  1305. av_dict_set(&st->metadata, "language", language, 0);
  1306. break;
  1307. case 0x0a: /* ISO 639 language descriptor */
  1308. for (i = 0; i + 4 <= desc_len; i += 4) {
  1309. language[i + 0] = get8(pp, desc_end);
  1310. language[i + 1] = get8(pp, desc_end);
  1311. language[i + 2] = get8(pp, desc_end);
  1312. language[i + 3] = ',';
  1313. switch (get8(pp, desc_end)) {
  1314. case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
  1315. case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
  1316. case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
  1317. }
  1318. }
  1319. if (i) {
  1320. language[i - 1] = 0;
  1321. av_dict_set(&st->metadata, "language", language, 0);
  1322. }
  1323. break;
  1324. case 0x05: /* registration descriptor */
  1325. st->codec->codec_tag = bytestream_get_le32(pp);
  1326. av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
  1327. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  1328. mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
  1329. break;
  1330. case 0x52: /* stream identifier descriptor */
  1331. st->stream_identifier = 1 + get8(pp, desc_end);
  1332. break;
  1333. default:
  1334. break;
  1335. }
  1336. *pp = desc_end;
  1337. return 0;
  1338. }
  1339. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1340. {
  1341. MpegTSContext *ts = filter->u.section_filter.opaque;
  1342. SectionHeader h1, *h = &h1;
  1343. PESContext *pes;
  1344. AVStream *st;
  1345. const uint8_t *p, *p_end, *desc_list_end;
  1346. int program_info_length, pcr_pid, pid, stream_type;
  1347. int desc_list_len;
  1348. uint32_t prog_reg_desc = 0; /* registration descriptor */
  1349. Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
  1350. int mp4_descr_count = 0;
  1351. int i;
  1352. av_dlog(ts->stream, "PMT: len %i\n", section_len);
  1353. hex_dump_debug(ts->stream, section, section_len);
  1354. p_end = section + section_len - 4;
  1355. p = section;
  1356. if (parse_section_header(h, &p, p_end) < 0)
  1357. return;
  1358. av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
  1359. h->id, h->sec_num, h->last_sec_num);
  1360. if (h->tid != PMT_TID)
  1361. return;
  1362. clear_program(ts, h->id);
  1363. pcr_pid = get16(&p, p_end);
  1364. if (pcr_pid < 0)
  1365. return;
  1366. pcr_pid &= 0x1fff;
  1367. add_pid_to_pmt(ts, h->id, pcr_pid);
  1368. set_pcr_pid(ts->stream, h->id, pcr_pid);
  1369. av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
  1370. program_info_length = get16(&p, p_end);
  1371. if (program_info_length < 0)
  1372. return;
  1373. program_info_length &= 0xfff;
  1374. while(program_info_length >= 2) {
  1375. uint8_t tag, len;
  1376. tag = get8(&p, p_end);
  1377. len = get8(&p, p_end);
  1378. av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
  1379. if(len > program_info_length - 2)
  1380. //something else is broken, exit the program_descriptors_loop
  1381. break;
  1382. program_info_length -= len + 2;
  1383. if (tag == 0x1d) { // IOD descriptor
  1384. get8(&p, p_end); // scope
  1385. get8(&p, p_end); // label
  1386. len -= 2;
  1387. mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
  1388. &mp4_descr_count, MAX_MP4_DESCR_COUNT);
  1389. } else if (tag == 0x05 && len >= 4) { // registration descriptor
  1390. prog_reg_desc = bytestream_get_le32(&p);
  1391. len -= 4;
  1392. }
  1393. p += len;
  1394. }
  1395. p += program_info_length;
  1396. if (p >= p_end)
  1397. goto out;
  1398. // stop parsing after pmt, we found header
  1399. if (!ts->stream->nb_streams)
  1400. ts->stop_parse = 2;
  1401. for(;;) {
  1402. st = 0;
  1403. pes = NULL;
  1404. stream_type = get8(&p, p_end);
  1405. if (stream_type < 0)
  1406. break;
  1407. pid = get16(&p, p_end);
  1408. if (pid < 0)
  1409. break;
  1410. pid &= 0x1fff;
  1411. if (pid == ts->current_pid)
  1412. break;
  1413. /* now create stream */
  1414. if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
  1415. pes = ts->pids[pid]->u.pes_filter.opaque;
  1416. if (!pes->st) {
  1417. pes->st = avformat_new_stream(pes->stream, NULL);
  1418. if (!pes->st)
  1419. goto out;
  1420. pes->st->id = pes->pid;
  1421. }
  1422. st = pes->st;
  1423. } else if (stream_type != 0x13) {
  1424. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  1425. pes = add_pes_stream(ts, pid, pcr_pid);
  1426. if (pes) {
  1427. st = avformat_new_stream(pes->stream, NULL);
  1428. if (!st)
  1429. goto out;
  1430. st->id = pes->pid;
  1431. }
  1432. } else {
  1433. int idx = ff_find_stream_index(ts->stream, pid);
  1434. if (idx >= 0) {
  1435. st = ts->stream->streams[idx];
  1436. } else {
  1437. st = avformat_new_stream(ts->stream, NULL);
  1438. if (!st)
  1439. goto out;
  1440. st->id = pid;
  1441. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1442. }
  1443. }
  1444. if (!st)
  1445. goto out;
  1446. if (pes && !pes->stream_type)
  1447. mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
  1448. add_pid_to_pmt(ts, h->id, pid);
  1449. ff_program_add_stream_index(ts->stream, h->id, st->index);
  1450. desc_list_len = get16(&p, p_end);
  1451. if (desc_list_len < 0)
  1452. break;
  1453. desc_list_len &= 0xfff;
  1454. desc_list_end = p + desc_list_len;
  1455. if (desc_list_end > p_end)
  1456. break;
  1457. for(;;) {
  1458. if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
  1459. mp4_descr, mp4_descr_count, pid, ts) < 0)
  1460. break;
  1461. if (pes && prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
  1462. ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
  1463. pes->sub_st->codec->codec_tag = st->codec->codec_tag;
  1464. }
  1465. }
  1466. p = desc_list_end;
  1467. }
  1468. out:
  1469. for (i = 0; i < mp4_descr_count; i++)
  1470. av_free(mp4_descr[i].dec_config_descr);
  1471. }
  1472. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1473. {
  1474. MpegTSContext *ts = filter->u.section_filter.opaque;
  1475. SectionHeader h1, *h = &h1;
  1476. const uint8_t *p, *p_end;
  1477. int sid, pmt_pid;
  1478. AVProgram *program;
  1479. av_dlog(ts->stream, "PAT:\n");
  1480. hex_dump_debug(ts->stream, section, section_len);
  1481. p_end = section + section_len - 4;
  1482. p = section;
  1483. if (parse_section_header(h, &p, p_end) < 0)
  1484. return;
  1485. if (h->tid != PAT_TID)
  1486. return;
  1487. ts->stream->ts_id = h->id;
  1488. clear_programs(ts);
  1489. for(;;) {
  1490. sid = get16(&p, p_end);
  1491. if (sid < 0)
  1492. break;
  1493. pmt_pid = get16(&p, p_end);
  1494. if (pmt_pid < 0)
  1495. break;
  1496. pmt_pid &= 0x1fff;
  1497. if (pmt_pid == ts->current_pid)
  1498. break;
  1499. av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  1500. if (sid == 0x0000) {
  1501. /* NIT info */
  1502. } else {
  1503. MpegTSFilter *fil = ts->pids[pmt_pid];
  1504. program = av_new_program(ts->stream, sid);
  1505. program->program_num = sid;
  1506. program->pmt_pid = pmt_pid;
  1507. if (fil)
  1508. if ( fil->type != MPEGTS_SECTION
  1509. || fil->pid != pmt_pid
  1510. || fil->u.section_filter.section_cb != pmt_cb)
  1511. mpegts_close_filter(ts, ts->pids[pmt_pid]);
  1512. if (!ts->pids[pmt_pid])
  1513. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  1514. add_pat_entry(ts, sid);
  1515. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  1516. add_pid_to_pmt(ts, sid, pmt_pid);
  1517. }
  1518. }
  1519. if (sid < 0) {
  1520. int i,j;
  1521. for (j=0; j<ts->stream->nb_programs; j++) {
  1522. for (i=0; i<ts->nb_prg; i++)
  1523. if (ts->prg[i].id == ts->stream->programs[j]->id)
  1524. break;
  1525. if (i==ts->nb_prg)
  1526. clear_avprogram(ts, ts->stream->programs[j]->id);
  1527. }
  1528. }
  1529. }
  1530. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1531. {
  1532. MpegTSContext *ts = filter->u.section_filter.opaque;
  1533. SectionHeader h1, *h = &h1;
  1534. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  1535. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  1536. char *name, *provider_name;
  1537. av_dlog(ts->stream, "SDT:\n");
  1538. hex_dump_debug(ts->stream, section, section_len);
  1539. p_end = section + section_len - 4;
  1540. p = section;
  1541. if (parse_section_header(h, &p, p_end) < 0)
  1542. return;
  1543. if (h->tid != SDT_TID)
  1544. return;
  1545. onid = get16(&p, p_end);
  1546. if (onid < 0)
  1547. return;
  1548. val = get8(&p, p_end);
  1549. if (val < 0)
  1550. return;
  1551. for(;;) {
  1552. sid = get16(&p, p_end);
  1553. if (sid < 0)
  1554. break;
  1555. val = get8(&p, p_end);
  1556. if (val < 0)
  1557. break;
  1558. desc_list_len = get16(&p, p_end);
  1559. if (desc_list_len < 0)
  1560. break;
  1561. desc_list_len &= 0xfff;
  1562. desc_list_end = p + desc_list_len;
  1563. if (desc_list_end > p_end)
  1564. break;
  1565. for(;;) {
  1566. desc_tag = get8(&p, desc_list_end);
  1567. if (desc_tag < 0)
  1568. break;
  1569. desc_len = get8(&p, desc_list_end);
  1570. desc_end = p + desc_len;
  1571. if (desc_end > desc_list_end)
  1572. break;
  1573. av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
  1574. desc_tag, desc_len);
  1575. switch(desc_tag) {
  1576. case 0x48:
  1577. service_type = get8(&p, p_end);
  1578. if (service_type < 0)
  1579. break;
  1580. provider_name = getstr8(&p, p_end);
  1581. if (!provider_name)
  1582. break;
  1583. name = getstr8(&p, p_end);
  1584. if (name) {
  1585. AVProgram *program = av_new_program(ts->stream, sid);
  1586. if(program) {
  1587. av_dict_set(&program->metadata, "service_name", name, 0);
  1588. av_dict_set(&program->metadata, "service_provider", provider_name, 0);
  1589. }
  1590. }
  1591. av_free(name);
  1592. av_free(provider_name);
  1593. break;
  1594. default:
  1595. break;
  1596. }
  1597. p = desc_end;
  1598. }
  1599. p = desc_list_end;
  1600. }
  1601. }
  1602. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1603. const uint8_t *packet);
  1604. /* handle one TS packet */
  1605. static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  1606. {
  1607. AVFormatContext *s = ts->stream;
  1608. MpegTSFilter *tss;
  1609. int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
  1610. has_adaptation, has_payload;
  1611. const uint8_t *p, *p_end;
  1612. int64_t pos;
  1613. pid = AV_RB16(packet + 1) & 0x1fff;
  1614. if(pid && discard_pid(ts, pid))
  1615. return 0;
  1616. is_start = packet[1] & 0x40;
  1617. tss = ts->pids[pid];
  1618. if (ts->auto_guess && tss == NULL && is_start) {
  1619. add_pes_stream(ts, pid, -1);
  1620. tss = ts->pids[pid];
  1621. }
  1622. if (!tss)
  1623. return 0;
  1624. ts->current_pid = pid;
  1625. afc = (packet[3] >> 4) & 3;
  1626. if (afc == 0) /* reserved value */
  1627. return 0;
  1628. has_adaptation = afc & 2;
  1629. has_payload = afc & 1;
  1630. is_discontinuity = has_adaptation
  1631. && packet[4] != 0 /* with length > 0 */
  1632. && (packet[5] & 0x80); /* and discontinuity indicated */
  1633. /* continuity check (currently not used) */
  1634. cc = (packet[3] & 0xf);
  1635. expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
  1636. cc_ok = pid == 0x1FFF // null packet PID
  1637. || is_discontinuity
  1638. || tss->last_cc < 0
  1639. || expected_cc == cc;
  1640. tss->last_cc = cc;
  1641. if (!cc_ok) {
  1642. av_log(ts->stream, AV_LOG_DEBUG,
  1643. "Continuity check failed for pid %d expected %d got %d\n",
  1644. pid, expected_cc, cc);
  1645. if(tss->type == MPEGTS_PES) {
  1646. PESContext *pc = tss->u.pes_filter.opaque;
  1647. pc->flags |= AV_PKT_FLAG_CORRUPT;
  1648. }
  1649. }
  1650. if (!has_payload)
  1651. return 0;
  1652. p = packet + 4;
  1653. if (has_adaptation) {
  1654. /* skip adaptation field */
  1655. p += p[0] + 1;
  1656. }
  1657. /* if past the end of packet, ignore */
  1658. p_end = packet + TS_PACKET_SIZE;
  1659. if (p >= p_end)
  1660. return 0;
  1661. pos = avio_tell(ts->stream->pb);
  1662. if (pos >= 0) {
  1663. av_assert0(pos >= TS_PACKET_SIZE);
  1664. ts->pos47_full = pos - TS_PACKET_SIZE;
  1665. }
  1666. if (tss->type == MPEGTS_SECTION) {
  1667. if (is_start) {
  1668. /* pointer field present */
  1669. len = *p++;
  1670. if (p + len > p_end)
  1671. return 0;
  1672. if (len && cc_ok) {
  1673. /* write remaining section bytes */
  1674. write_section_data(s, tss,
  1675. p, len, 0);
  1676. /* check whether filter has been closed */
  1677. if (!ts->pids[pid])
  1678. return 0;
  1679. }
  1680. p += len;
  1681. if (p < p_end) {
  1682. write_section_data(s, tss,
  1683. p, p_end - p, 1);
  1684. }
  1685. } else {
  1686. if (cc_ok) {
  1687. write_section_data(s, tss,
  1688. p, p_end - p, 0);
  1689. }
  1690. }
  1691. } else {
  1692. int ret;
  1693. int64_t pcr = -1;
  1694. int64_t pcr_h;
  1695. int pcr_l;
  1696. if (parse_pcr(&pcr_h, &pcr_l, packet) == 0)
  1697. pcr = pcr_h * 300 + pcr_l;
  1698. // Note: The position here points actually behind the current packet.
  1699. if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
  1700. pos - ts->raw_packet_size, pcr)) < 0)
  1701. return ret;
  1702. }
  1703. return 0;
  1704. }
  1705. static void reanalyze(MpegTSContext *ts) {
  1706. AVIOContext *pb = ts->stream->pb;
  1707. int64_t pos = avio_tell(pb);
  1708. if(pos < 0)
  1709. return;
  1710. pos -= ts->pos47_full;
  1711. if (pos == TS_PACKET_SIZE) {
  1712. ts->size_stat[0] ++;
  1713. } else if (pos == TS_DVHS_PACKET_SIZE) {
  1714. ts->size_stat[1] ++;
  1715. } else if (pos == TS_FEC_PACKET_SIZE) {
  1716. ts->size_stat[2] ++;
  1717. }
  1718. ts->size_stat_count ++;
  1719. if(ts->size_stat_count > SIZE_STAT_THRESHOLD) {
  1720. int newsize = 0;
  1721. if (ts->size_stat[0] > SIZE_STAT_THRESHOLD) {
  1722. newsize = TS_PACKET_SIZE;
  1723. } else if (ts->size_stat[1] > SIZE_STAT_THRESHOLD) {
  1724. newsize = TS_DVHS_PACKET_SIZE;
  1725. } else if (ts->size_stat[2] > SIZE_STAT_THRESHOLD) {
  1726. newsize = TS_FEC_PACKET_SIZE;
  1727. }
  1728. if (newsize && newsize != ts->raw_packet_size) {
  1729. av_log(ts->stream, AV_LOG_WARNING, "changing packet size to %d\n", newsize);
  1730. ts->raw_packet_size = newsize;
  1731. }
  1732. ts->size_stat_count = 0;
  1733. memset(ts->size_stat, 0, sizeof(ts->size_stat));
  1734. }
  1735. }
  1736. /* XXX: try to find a better synchro over several packets (use
  1737. get_packet_size() ?) */
  1738. static int mpegts_resync(AVFormatContext *s)
  1739. {
  1740. AVIOContext *pb = s->pb;
  1741. int c, i;
  1742. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1743. c = avio_r8(pb);
  1744. if (url_feof(pb))
  1745. return -1;
  1746. if (c == 0x47) {
  1747. avio_seek(pb, -1, SEEK_CUR);
  1748. reanalyze(s->priv_data);
  1749. return 0;
  1750. }
  1751. }
  1752. av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
  1753. /* no sync found */
  1754. return -1;
  1755. }
  1756. /* return -1 if error or EOF. Return 0 if OK. */
  1757. static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, const uint8_t **data)
  1758. {
  1759. AVIOContext *pb = s->pb;
  1760. int len;
  1761. for(;;) {
  1762. len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data);
  1763. if (len != TS_PACKET_SIZE)
  1764. return len < 0 ? len : AVERROR_EOF;
  1765. /* check packet sync byte */
  1766. if ((*data)[0] != 0x47) {
  1767. /* find a new packet start */
  1768. avio_seek(pb, -raw_packet_size, SEEK_CUR);
  1769. if (mpegts_resync(s) < 0)
  1770. return AVERROR(EAGAIN);
  1771. else
  1772. continue;
  1773. } else {
  1774. break;
  1775. }
  1776. }
  1777. return 0;
  1778. }
  1779. static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
  1780. {
  1781. AVIOContext *pb = s->pb;
  1782. int skip = raw_packet_size - TS_PACKET_SIZE;
  1783. if (skip > 0)
  1784. avio_skip(pb, skip);
  1785. }
  1786. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1787. {
  1788. AVFormatContext *s = ts->stream;
  1789. uint8_t packet[TS_PACKET_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  1790. const uint8_t *data;
  1791. int packet_num, ret = 0;
  1792. if (avio_tell(s->pb) != ts->last_pos) {
  1793. int i;
  1794. av_dlog(ts->stream, "Skipping after seek\n");
  1795. /* seek detected, flush pes buffer */
  1796. for (i = 0; i < NB_PID_MAX; i++) {
  1797. if (ts->pids[i]) {
  1798. if (ts->pids[i]->type == MPEGTS_PES) {
  1799. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1800. av_buffer_unref(&pes->buffer);
  1801. pes->data_index = 0;
  1802. pes->state = MPEGTS_SKIP; /* skip until pes header */
  1803. pes->last_pcr = -1;
  1804. }
  1805. ts->pids[i]->last_cc = -1;
  1806. }
  1807. }
  1808. }
  1809. ts->stop_parse = 0;
  1810. packet_num = 0;
  1811. memset(packet + TS_PACKET_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  1812. for(;;) {
  1813. packet_num++;
  1814. if (nb_packets != 0 && packet_num >= nb_packets ||
  1815. ts->stop_parse > 1) {
  1816. ret = AVERROR(EAGAIN);
  1817. break;
  1818. }
  1819. if (ts->stop_parse > 0)
  1820. break;
  1821. ret = read_packet(s, packet, ts->raw_packet_size, &data);
  1822. if (ret != 0)
  1823. break;
  1824. ret = handle_packet(ts, data);
  1825. finished_reading_packet(s, ts->raw_packet_size);
  1826. if (ret != 0)
  1827. break;
  1828. }
  1829. ts->last_pos = avio_tell(s->pb);
  1830. return ret;
  1831. }
  1832. static int mpegts_probe(AVProbeData *p)
  1833. {
  1834. const int size= p->buf_size;
  1835. int maxscore=0;
  1836. int sumscore=0;
  1837. int i;
  1838. int check_count= size / TS_FEC_PACKET_SIZE;
  1839. #define CHECK_COUNT 10
  1840. #define CHECK_BLOCK 100
  1841. if (check_count < CHECK_COUNT)
  1842. return -1;
  1843. for (i=0; i<check_count; i+=CHECK_BLOCK){
  1844. int left = FFMIN(check_count - i, CHECK_BLOCK);
  1845. int score = analyze(p->buf + TS_PACKET_SIZE *i, TS_PACKET_SIZE *left, TS_PACKET_SIZE , NULL);
  1846. int dvhs_score= analyze(p->buf + TS_DVHS_PACKET_SIZE*i, TS_DVHS_PACKET_SIZE*left, TS_DVHS_PACKET_SIZE, NULL);
  1847. int fec_score = analyze(p->buf + TS_FEC_PACKET_SIZE *i, TS_FEC_PACKET_SIZE *left, TS_FEC_PACKET_SIZE , NULL);
  1848. score = FFMAX3(score, dvhs_score, fec_score);
  1849. sumscore += score;
  1850. maxscore = FFMAX(maxscore, score);
  1851. }
  1852. sumscore = sumscore*CHECK_COUNT/check_count;
  1853. maxscore = maxscore*CHECK_COUNT/CHECK_BLOCK;
  1854. av_dlog(0, "TS score: %d %d\n", sumscore, maxscore);
  1855. if (sumscore > 6) return AVPROBE_SCORE_MAX + sumscore - CHECK_COUNT;
  1856. else if (maxscore > 6) return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT;
  1857. else return -1;
  1858. }
  1859. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1860. (-1) if not available */
  1861. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1862. const uint8_t *packet)
  1863. {
  1864. int afc, len, flags;
  1865. const uint8_t *p;
  1866. unsigned int v;
  1867. afc = (packet[3] >> 4) & 3;
  1868. if (afc <= 1)
  1869. return -1;
  1870. p = packet + 4;
  1871. len = p[0];
  1872. p++;
  1873. if (len == 0)
  1874. return -1;
  1875. flags = *p++;
  1876. len--;
  1877. if (!(flags & 0x10))
  1878. return -1;
  1879. if (len < 6)
  1880. return -1;
  1881. v = AV_RB32(p);
  1882. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1883. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1884. return 0;
  1885. }
  1886. static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos) {
  1887. /* NOTE: We attempt to seek on non-seekable files as well, as the
  1888. * probe buffer usually is big enough. Only warn if the seek failed
  1889. * on files where the seek should work. */
  1890. if (avio_seek(pb, pos, SEEK_SET) < 0)
  1891. av_log(s, pb->seekable ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
  1892. }
  1893. static int mpegts_read_header(AVFormatContext *s)
  1894. {
  1895. MpegTSContext *ts = s->priv_data;
  1896. AVIOContext *pb = s->pb;
  1897. uint8_t buf[8*1024]={0};
  1898. int len;
  1899. int64_t pos;
  1900. ffio_ensure_seekback(pb, s->probesize);
  1901. /* read the first 8192 bytes to get packet size */
  1902. pos = avio_tell(pb);
  1903. len = avio_read(pb, buf, sizeof(buf));
  1904. ts->raw_packet_size = get_packet_size(buf, len);
  1905. if (ts->raw_packet_size <= 0) {
  1906. av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
  1907. ts->raw_packet_size = TS_PACKET_SIZE;
  1908. }
  1909. ts->stream = s;
  1910. ts->auto_guess = 0;
  1911. if (s->iformat == &ff_mpegts_demuxer) {
  1912. /* normal demux */
  1913. /* first do a scan to get all the services */
  1914. seek_back(s, pb, pos);
  1915. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1916. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1917. handle_packets(ts, s->probesize / ts->raw_packet_size);
  1918. /* if could not find service, enable auto_guess */
  1919. ts->auto_guess = 1;
  1920. av_dlog(ts->stream, "tuning done\n");
  1921. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1922. } else {
  1923. AVStream *st;
  1924. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1925. int64_t pcrs[2], pcr_h;
  1926. int packet_count[2];
  1927. uint8_t packet[TS_PACKET_SIZE];
  1928. const uint8_t *data;
  1929. /* only read packets */
  1930. st = avformat_new_stream(s, NULL);
  1931. if (!st)
  1932. goto fail;
  1933. avpriv_set_pts_info(st, 60, 1, 27000000);
  1934. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1935. st->codec->codec_id = AV_CODEC_ID_MPEG2TS;
  1936. /* we iterate until we find two PCRs to estimate the bitrate */
  1937. pcr_pid = -1;
  1938. nb_pcrs = 0;
  1939. nb_packets = 0;
  1940. for(;;) {
  1941. ret = read_packet(s, packet, ts->raw_packet_size, &data);
  1942. if (ret < 0)
  1943. goto fail;
  1944. pid = AV_RB16(data + 1) & 0x1fff;
  1945. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1946. parse_pcr(&pcr_h, &pcr_l, data) == 0) {
  1947. finished_reading_packet(s, ts->raw_packet_size);
  1948. pcr_pid = pid;
  1949. packet_count[nb_pcrs] = nb_packets;
  1950. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1951. nb_pcrs++;
  1952. if (nb_pcrs >= 2)
  1953. break;
  1954. } else {
  1955. finished_reading_packet(s, ts->raw_packet_size);
  1956. }
  1957. nb_packets++;
  1958. }
  1959. /* NOTE1: the bitrate is computed without the FEC */
  1960. /* NOTE2: it is only the bitrate of the start of the stream */
  1961. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1962. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1963. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1964. st->codec->bit_rate = s->bit_rate;
  1965. st->start_time = ts->cur_pcr;
  1966. av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
  1967. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1968. }
  1969. seek_back(s, pb, pos);
  1970. return 0;
  1971. fail:
  1972. return -1;
  1973. }
  1974. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1975. static int mpegts_raw_read_packet(AVFormatContext *s,
  1976. AVPacket *pkt)
  1977. {
  1978. MpegTSContext *ts = s->priv_data;
  1979. int ret, i;
  1980. int64_t pcr_h, next_pcr_h, pos;
  1981. int pcr_l, next_pcr_l;
  1982. uint8_t pcr_buf[12];
  1983. const uint8_t *data;
  1984. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1985. return AVERROR(ENOMEM);
  1986. pkt->pos= avio_tell(s->pb);
  1987. ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
  1988. if (ret < 0) {
  1989. av_free_packet(pkt);
  1990. return ret;
  1991. }
  1992. if (data != pkt->data)
  1993. memcpy(pkt->data, data, ts->raw_packet_size);
  1994. finished_reading_packet(s, ts->raw_packet_size);
  1995. if (ts->mpeg2ts_compute_pcr) {
  1996. /* compute exact PCR for each packet */
  1997. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1998. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1999. pos = avio_tell(s->pb);
  2000. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  2001. avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  2002. avio_read(s->pb, pcr_buf, 12);
  2003. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  2004. /* XXX: not precise enough */
  2005. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  2006. (i + 1);
  2007. break;
  2008. }
  2009. }
  2010. avio_seek(s->pb, pos, SEEK_SET);
  2011. /* no next PCR found: we use previous increment */
  2012. ts->cur_pcr = pcr_h * 300 + pcr_l;
  2013. }
  2014. pkt->pts = ts->cur_pcr;
  2015. pkt->duration = ts->pcr_incr;
  2016. ts->cur_pcr += ts->pcr_incr;
  2017. }
  2018. pkt->stream_index = 0;
  2019. return 0;
  2020. }
  2021. static int mpegts_read_packet(AVFormatContext *s,
  2022. AVPacket *pkt)
  2023. {
  2024. MpegTSContext *ts = s->priv_data;
  2025. int ret, i;
  2026. pkt->size = -1;
  2027. ts->pkt = pkt;
  2028. ret = handle_packets(ts, 0);
  2029. if (ret < 0) {
  2030. av_free_packet(ts->pkt);
  2031. /* flush pes data left */
  2032. for (i = 0; i < NB_PID_MAX; i++) {
  2033. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  2034. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  2035. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  2036. new_pes_packet(pes, pkt);
  2037. pes->state = MPEGTS_SKIP;
  2038. ret = 0;
  2039. break;
  2040. }
  2041. }
  2042. }
  2043. }
  2044. if (!ret && pkt->size < 0)
  2045. ret = AVERROR(EINTR);
  2046. return ret;
  2047. }
  2048. static void mpegts_free(MpegTSContext *ts)
  2049. {
  2050. int i;
  2051. clear_programs(ts);
  2052. for(i=0;i<NB_PID_MAX;i++)
  2053. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  2054. }
  2055. static int mpegts_read_close(AVFormatContext *s)
  2056. {
  2057. MpegTSContext *ts = s->priv_data;
  2058. mpegts_free(ts);
  2059. return 0;
  2060. }
  2061. static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  2062. int64_t *ppos, int64_t pos_limit)
  2063. {
  2064. MpegTSContext *ts = s->priv_data;
  2065. int64_t pos, timestamp;
  2066. uint8_t buf[TS_PACKET_SIZE];
  2067. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  2068. int pos47 = ts->pos47_full % ts->raw_packet_size;
  2069. pos = ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * ts->raw_packet_size + pos47;
  2070. while(pos < pos_limit) {
  2071. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  2072. return AV_NOPTS_VALUE;
  2073. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  2074. return AV_NOPTS_VALUE;
  2075. if (buf[0] != 0x47) {
  2076. avio_seek(s->pb, -TS_PACKET_SIZE, SEEK_CUR);
  2077. if (mpegts_resync(s) < 0)
  2078. return AV_NOPTS_VALUE;
  2079. pos = avio_tell(s->pb);
  2080. continue;
  2081. }
  2082. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  2083. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  2084. *ppos = pos;
  2085. return timestamp;
  2086. }
  2087. pos += ts->raw_packet_size;
  2088. }
  2089. return AV_NOPTS_VALUE;
  2090. }
  2091. static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
  2092. int64_t *ppos, int64_t pos_limit)
  2093. {
  2094. MpegTSContext *ts = s->priv_data;
  2095. int64_t pos;
  2096. int pos47 = ts->pos47_full % ts->raw_packet_size;
  2097. pos = ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * ts->raw_packet_size + pos47;
  2098. ff_read_frame_flush(s);
  2099. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  2100. return AV_NOPTS_VALUE;
  2101. while(pos < pos_limit) {
  2102. int ret;
  2103. AVPacket pkt;
  2104. av_init_packet(&pkt);
  2105. ret= av_read_frame(s, &pkt);
  2106. if(ret < 0)
  2107. return AV_NOPTS_VALUE;
  2108. av_free_packet(&pkt);
  2109. if(pkt.dts != AV_NOPTS_VALUE && pkt.pos >= 0){
  2110. ff_reduce_index(s, pkt.stream_index);
  2111. av_add_index_entry(s->streams[pkt.stream_index], pkt.pos, pkt.dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
  2112. if(pkt.stream_index == stream_index && pkt.pos >= *ppos){
  2113. *ppos= pkt.pos;
  2114. return pkt.dts;
  2115. }
  2116. }
  2117. pos = pkt.pos;
  2118. }
  2119. return AV_NOPTS_VALUE;
  2120. }
  2121. /**************************************************************/
  2122. /* parsing functions - called from other demuxers such as RTP */
  2123. MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
  2124. {
  2125. MpegTSContext *ts;
  2126. ts = av_mallocz(sizeof(MpegTSContext));
  2127. if (!ts)
  2128. return NULL;
  2129. /* no stream case, currently used by RTP */
  2130. ts->raw_packet_size = TS_PACKET_SIZE;
  2131. ts->stream = s;
  2132. ts->auto_guess = 1;
  2133. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  2134. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  2135. return ts;
  2136. }
  2137. /* return the consumed length if a packet was output, or -1 if no
  2138. packet is output */
  2139. int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  2140. const uint8_t *buf, int len)
  2141. {
  2142. int len1;
  2143. len1 = len;
  2144. ts->pkt = pkt;
  2145. for(;;) {
  2146. ts->stop_parse = 0;
  2147. if (len < TS_PACKET_SIZE)
  2148. return -1;
  2149. if (buf[0] != 0x47) {
  2150. buf++;
  2151. len--;
  2152. } else {
  2153. handle_packet(ts, buf);
  2154. buf += TS_PACKET_SIZE;
  2155. len -= TS_PACKET_SIZE;
  2156. if (ts->stop_parse == 1)
  2157. break;
  2158. }
  2159. }
  2160. return len1 - len;
  2161. }
  2162. void ff_mpegts_parse_close(MpegTSContext *ts)
  2163. {
  2164. mpegts_free(ts);
  2165. av_free(ts);
  2166. }
  2167. AVInputFormat ff_mpegts_demuxer = {
  2168. .name = "mpegts",
  2169. .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
  2170. .priv_data_size = sizeof(MpegTSContext),
  2171. .read_probe = mpegts_probe,
  2172. .read_header = mpegts_read_header,
  2173. .read_packet = mpegts_read_packet,
  2174. .read_close = mpegts_read_close,
  2175. .read_timestamp = mpegts_get_dts,
  2176. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  2177. .priv_class = &mpegts_class,
  2178. };
  2179. AVInputFormat ff_mpegtsraw_demuxer = {
  2180. .name = "mpegtsraw",
  2181. .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
  2182. .priv_data_size = sizeof(MpegTSContext),
  2183. .read_header = mpegts_read_header,
  2184. .read_packet = mpegts_raw_read_packet,
  2185. .read_close = mpegts_read_close,
  2186. .read_timestamp = mpegts_get_dts,
  2187. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  2188. .priv_class = &mpegtsraw_class,
  2189. };