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.

2454 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('K','L','V','A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV },
  556. { MKTAG('V','C','-','1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
  557. { 0 },
  558. };
  559. /* descriptor present */
  560. static const StreamType DESC_types[] = {
  561. { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
  562. { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
  563. { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
  564. { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
  565. { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
  566. { 0 },
  567. };
  568. static void mpegts_find_stream_type(AVStream *st,
  569. uint32_t stream_type, const StreamType *types)
  570. {
  571. if (avcodec_is_open(st->codec)) {
  572. av_log(NULL, AV_LOG_DEBUG, "cannot set stream info, codec is open\n");
  573. return;
  574. }
  575. for (; types->stream_type; types++) {
  576. if (stream_type == types->stream_type) {
  577. st->codec->codec_type = types->codec_type;
  578. st->codec->codec_id = types->codec_id;
  579. st->request_probe = 0;
  580. return;
  581. }
  582. }
  583. }
  584. static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
  585. uint32_t stream_type, uint32_t prog_reg_desc)
  586. {
  587. int old_codec_type= st->codec->codec_type;
  588. int old_codec_id = st->codec->codec_id;
  589. if (avcodec_is_open(st->codec)) {
  590. av_log(pes->stream, AV_LOG_DEBUG, "cannot set stream info, codec is open\n");
  591. return 0;
  592. }
  593. avpriv_set_pts_info(st, 33, 1, 90000);
  594. st->priv_data = pes;
  595. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  596. st->codec->codec_id = AV_CODEC_ID_NONE;
  597. st->need_parsing = AVSTREAM_PARSE_FULL;
  598. pes->st = st;
  599. pes->stream_type = stream_type;
  600. av_log(pes->stream, AV_LOG_DEBUG,
  601. "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
  602. st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
  603. st->codec->codec_tag = pes->stream_type;
  604. mpegts_find_stream_type(st, pes->stream_type, ISO_types);
  605. if ((prog_reg_desc == AV_RL32("HDMV") ||
  606. prog_reg_desc == AV_RL32("HDPR")) &&
  607. st->codec->codec_id == AV_CODEC_ID_NONE) {
  608. mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
  609. if (pes->stream_type == 0x83) {
  610. // HDMV TrueHD streams also contain an AC3 coded version of the
  611. // audio track - add a second stream for this
  612. AVStream *sub_st;
  613. // priv_data cannot be shared between streams
  614. PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
  615. if (!sub_pes)
  616. return AVERROR(ENOMEM);
  617. memcpy(sub_pes, pes, sizeof(*sub_pes));
  618. sub_st = avformat_new_stream(pes->stream, NULL);
  619. if (!sub_st) {
  620. av_free(sub_pes);
  621. return AVERROR(ENOMEM);
  622. }
  623. sub_st->id = pes->pid;
  624. avpriv_set_pts_info(sub_st, 33, 1, 90000);
  625. sub_st->priv_data = sub_pes;
  626. sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  627. sub_st->codec->codec_id = AV_CODEC_ID_AC3;
  628. sub_st->need_parsing = AVSTREAM_PARSE_FULL;
  629. sub_pes->sub_st = pes->sub_st = sub_st;
  630. }
  631. }
  632. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  633. mpegts_find_stream_type(st, pes->stream_type, MISC_types);
  634. if (st->codec->codec_id == AV_CODEC_ID_NONE){
  635. st->codec->codec_id = old_codec_id;
  636. st->codec->codec_type= old_codec_type;
  637. }
  638. return 0;
  639. }
  640. static void new_pes_packet(PESContext *pes, AVPacket *pkt)
  641. {
  642. av_init_packet(pkt);
  643. pkt->buf = pes->buffer;
  644. pkt->data = pes->buffer->data;
  645. pkt->size = pes->data_index;
  646. if(pes->total_size != MAX_PES_PAYLOAD &&
  647. pes->pes_header_size + pes->data_index != pes->total_size + PES_START_SIZE) {
  648. av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
  649. pes->flags |= AV_PKT_FLAG_CORRUPT;
  650. }
  651. memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  652. // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
  653. if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
  654. pkt->stream_index = pes->sub_st->index;
  655. else
  656. pkt->stream_index = pes->st->index;
  657. pkt->pts = pes->pts;
  658. pkt->dts = pes->dts;
  659. /* store position of first TS packet of this PES packet */
  660. pkt->pos = pes->ts_packet_pos;
  661. pkt->flags = pes->flags;
  662. /* reset pts values */
  663. pes->pts = AV_NOPTS_VALUE;
  664. pes->dts = AV_NOPTS_VALUE;
  665. pes->buffer = NULL;
  666. pes->data_index = 0;
  667. pes->flags = 0;
  668. }
  669. static uint64_t get_ts64(GetBitContext *gb, int bits)
  670. {
  671. if (get_bits_left(gb) < bits)
  672. return AV_NOPTS_VALUE;
  673. return get_bits64(gb, bits);
  674. }
  675. static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
  676. {
  677. GetBitContext gb;
  678. int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
  679. int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
  680. int dts_flag = -1, cts_flag = -1;
  681. int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
  682. init_get_bits(&gb, buf, buf_size*8);
  683. if (sl->use_au_start)
  684. au_start_flag = get_bits1(&gb);
  685. if (sl->use_au_end)
  686. au_end_flag = get_bits1(&gb);
  687. if (!sl->use_au_start && !sl->use_au_end)
  688. au_start_flag = au_end_flag = 1;
  689. if (sl->ocr_len > 0)
  690. ocr_flag = get_bits1(&gb);
  691. if (sl->use_idle)
  692. idle_flag = get_bits1(&gb);
  693. if (sl->use_padding)
  694. padding_flag = get_bits1(&gb);
  695. if (padding_flag)
  696. padding_bits = get_bits(&gb, 3);
  697. if (!idle_flag && (!padding_flag || padding_bits != 0)) {
  698. if (sl->packet_seq_num_len)
  699. skip_bits_long(&gb, sl->packet_seq_num_len);
  700. if (sl->degr_prior_len)
  701. if (get_bits1(&gb))
  702. skip_bits(&gb, sl->degr_prior_len);
  703. if (ocr_flag)
  704. skip_bits_long(&gb, sl->ocr_len);
  705. if (au_start_flag) {
  706. if (sl->use_rand_acc_pt)
  707. get_bits1(&gb);
  708. if (sl->au_seq_num_len > 0)
  709. skip_bits_long(&gb, sl->au_seq_num_len);
  710. if (sl->use_timestamps) {
  711. dts_flag = get_bits1(&gb);
  712. cts_flag = get_bits1(&gb);
  713. }
  714. }
  715. if (sl->inst_bitrate_len)
  716. inst_bitrate_flag = get_bits1(&gb);
  717. if (dts_flag == 1)
  718. dts = get_ts64(&gb, sl->timestamp_len);
  719. if (cts_flag == 1)
  720. cts = get_ts64(&gb, sl->timestamp_len);
  721. if (sl->au_len > 0)
  722. skip_bits_long(&gb, sl->au_len);
  723. if (inst_bitrate_flag)
  724. skip_bits_long(&gb, sl->inst_bitrate_len);
  725. }
  726. if (dts != AV_NOPTS_VALUE)
  727. pes->dts = dts;
  728. if (cts != AV_NOPTS_VALUE)
  729. pes->pts = cts;
  730. if (sl->timestamp_len && sl->timestamp_res)
  731. avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
  732. return (get_bits_count(&gb) + 7) >> 3;
  733. }
  734. /* return non zero if a packet could be constructed */
  735. static int mpegts_push_data(MpegTSFilter *filter,
  736. const uint8_t *buf, int buf_size, int is_start,
  737. int64_t pos, int64_t pcr)
  738. {
  739. PESContext *pes = filter->u.pes_filter.opaque;
  740. MpegTSContext *ts = pes->ts;
  741. const uint8_t *p;
  742. int len, code;
  743. if(!ts->pkt)
  744. return 0;
  745. if (pcr != -1)
  746. pes->last_pcr = pcr;
  747. if (is_start) {
  748. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  749. new_pes_packet(pes, ts->pkt);
  750. ts->stop_parse = 1;
  751. }
  752. pes->state = MPEGTS_HEADER;
  753. pes->data_index = 0;
  754. pes->ts_packet_pos = pos;
  755. }
  756. p = buf;
  757. while (buf_size > 0) {
  758. switch(pes->state) {
  759. case MPEGTS_HEADER:
  760. len = PES_START_SIZE - pes->data_index;
  761. if (len > buf_size)
  762. len = buf_size;
  763. memcpy(pes->header + pes->data_index, p, len);
  764. pes->data_index += len;
  765. p += len;
  766. buf_size -= len;
  767. if (pes->data_index == PES_START_SIZE) {
  768. /* we got all the PES or section header. We can now
  769. decide */
  770. if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
  771. pes->header[2] == 0x01) {
  772. /* it must be an mpeg2 PES stream */
  773. code = pes->header[3] | 0x100;
  774. av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
  775. if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
  776. (!pes->sub_st || pes->sub_st->discard == AVDISCARD_ALL)) ||
  777. code == 0x1be) /* padding_stream */
  778. goto skip;
  779. /* stream not present in PMT */
  780. if (!pes->st) {
  781. pes->st = avformat_new_stream(ts->stream, NULL);
  782. if (!pes->st)
  783. return AVERROR(ENOMEM);
  784. pes->st->id = pes->pid;
  785. mpegts_set_stream_info(pes->st, pes, 0, 0);
  786. }
  787. pes->total_size = AV_RB16(pes->header + 4);
  788. /* NOTE: a zero total size means the PES size is
  789. unbounded */
  790. if (!pes->total_size)
  791. pes->total_size = MAX_PES_PAYLOAD;
  792. /* allocate pes buffer */
  793. pes->buffer = av_buffer_alloc(pes->total_size +
  794. FF_INPUT_BUFFER_PADDING_SIZE);
  795. if (!pes->buffer)
  796. return AVERROR(ENOMEM);
  797. if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
  798. code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
  799. code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
  800. code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
  801. pes->state = MPEGTS_PESHEADER;
  802. if (pes->st->codec->codec_id == AV_CODEC_ID_NONE && !pes->st->request_probe) {
  803. av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
  804. pes->pid, pes->stream_type);
  805. pes->st->request_probe= 1;
  806. }
  807. } else {
  808. pes->state = MPEGTS_PAYLOAD;
  809. pes->data_index = 0;
  810. }
  811. } else {
  812. /* otherwise, it should be a table */
  813. /* skip packet */
  814. skip:
  815. pes->state = MPEGTS_SKIP;
  816. continue;
  817. }
  818. }
  819. break;
  820. /**********************************************/
  821. /* PES packing parsing */
  822. case MPEGTS_PESHEADER:
  823. len = PES_HEADER_SIZE - pes->data_index;
  824. if (len < 0)
  825. return -1;
  826. if (len > buf_size)
  827. len = buf_size;
  828. memcpy(pes->header + pes->data_index, p, len);
  829. pes->data_index += len;
  830. p += len;
  831. buf_size -= len;
  832. if (pes->data_index == PES_HEADER_SIZE) {
  833. pes->pes_header_size = pes->header[8] + 9;
  834. pes->state = MPEGTS_PESHEADER_FILL;
  835. }
  836. break;
  837. case MPEGTS_PESHEADER_FILL:
  838. len = pes->pes_header_size - pes->data_index;
  839. if (len < 0)
  840. return -1;
  841. if (len > buf_size)
  842. len = buf_size;
  843. memcpy(pes->header + pes->data_index, p, len);
  844. pes->data_index += len;
  845. p += len;
  846. buf_size -= len;
  847. if (pes->data_index == pes->pes_header_size) {
  848. const uint8_t *r;
  849. unsigned int flags, pes_ext, skip;
  850. flags = pes->header[7];
  851. r = pes->header + 9;
  852. pes->pts = AV_NOPTS_VALUE;
  853. pes->dts = AV_NOPTS_VALUE;
  854. if ((flags & 0xc0) == 0x80) {
  855. pes->dts = pes->pts = ff_parse_pes_pts(r);
  856. r += 5;
  857. } else if ((flags & 0xc0) == 0xc0) {
  858. pes->pts = ff_parse_pes_pts(r);
  859. r += 5;
  860. pes->dts = ff_parse_pes_pts(r);
  861. r += 5;
  862. }
  863. pes->extended_stream_id = -1;
  864. if (flags & 0x01) { /* PES extension */
  865. pes_ext = *r++;
  866. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  867. skip = (pes_ext >> 4) & 0xb;
  868. skip += skip & 0x9;
  869. r += skip;
  870. if ((pes_ext & 0x41) == 0x01 &&
  871. (r + 2) <= (pes->header + pes->pes_header_size)) {
  872. /* PES extension 2 */
  873. if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
  874. pes->extended_stream_id = r[1];
  875. }
  876. }
  877. /* we got the full header. We parse it and get the payload */
  878. pes->state = MPEGTS_PAYLOAD;
  879. pes->data_index = 0;
  880. if (pes->stream_type == 0x12 && buf_size > 0) {
  881. int sl_header_bytes = read_sl_header(pes, &pes->sl, p, buf_size);
  882. pes->pes_header_size += sl_header_bytes;
  883. p += sl_header_bytes;
  884. buf_size -= sl_header_bytes;
  885. }
  886. if (pes->ts->fix_teletext_pts && pes->st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
  887. AVProgram *p = NULL;
  888. while ((p = av_find_program_from_stream(pes->stream, p, pes->st->index))) {
  889. if (p->pcr_pid != -1 && p->discard != AVDISCARD_ALL) {
  890. MpegTSFilter *f = pes->ts->pids[p->pcr_pid];
  891. if (f && f->type == MPEGTS_PES) {
  892. PESContext *pcrpes = f->u.pes_filter.opaque;
  893. if (pcrpes && pcrpes->last_pcr != -1 && pcrpes->st && pcrpes->st->discard != AVDISCARD_ALL) {
  894. // teletext packets do not always have correct timestamps,
  895. // the standard says they should be handled after 40.6 ms at most,
  896. // and the pcr error to this packet should be no more than 100 ms.
  897. // TODO: we should interpolate the PCR, not just use the last one
  898. int64_t pcr = pcrpes->last_pcr / 300;
  899. pes->st->pts_wrap_reference = pcrpes->st->pts_wrap_reference;
  900. pes->st->pts_wrap_behavior = pcrpes->st->pts_wrap_behavior;
  901. if (pes->dts == AV_NOPTS_VALUE || pes->dts < pcr) {
  902. pes->pts = pes->dts = pcr;
  903. } else if (pes->dts > pcr + 3654 + 9000) {
  904. pes->pts = pes->dts = pcr + 3654 + 9000;
  905. }
  906. break;
  907. }
  908. }
  909. }
  910. }
  911. }
  912. }
  913. break;
  914. case MPEGTS_PAYLOAD:
  915. if (buf_size > 0 && pes->buffer) {
  916. if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
  917. new_pes_packet(pes, ts->pkt);
  918. pes->total_size = MAX_PES_PAYLOAD;
  919. pes->buffer = av_buffer_alloc(pes->total_size + FF_INPUT_BUFFER_PADDING_SIZE);
  920. if (!pes->buffer)
  921. return AVERROR(ENOMEM);
  922. ts->stop_parse = 1;
  923. } else if (pes->data_index == 0 && buf_size > pes->total_size) {
  924. // pes packet size is < ts size packet and pes data is padded with 0xff
  925. // not sure if this is legal in ts but see issue #2392
  926. buf_size = pes->total_size;
  927. }
  928. memcpy(pes->buffer->data + pes->data_index, p, buf_size);
  929. pes->data_index += buf_size;
  930. }
  931. buf_size = 0;
  932. /* emit complete packets with known packet size
  933. * decreases demuxer delay for infrequent packets like subtitles from
  934. * a couple of seconds to milliseconds for properly muxed files.
  935. * total_size is the number of bytes following pes_packet_length
  936. * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
  937. if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
  938. pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
  939. ts->stop_parse = 1;
  940. new_pes_packet(pes, ts->pkt);
  941. }
  942. break;
  943. case MPEGTS_SKIP:
  944. buf_size = 0;
  945. break;
  946. }
  947. }
  948. return 0;
  949. }
  950. static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
  951. {
  952. MpegTSFilter *tss;
  953. PESContext *pes;
  954. /* if no pid found, then add a pid context */
  955. pes = av_mallocz(sizeof(PESContext));
  956. if (!pes)
  957. return 0;
  958. pes->ts = ts;
  959. pes->stream = ts->stream;
  960. pes->pid = pid;
  961. pes->pcr_pid = pcr_pid;
  962. pes->state = MPEGTS_SKIP;
  963. pes->pts = AV_NOPTS_VALUE;
  964. pes->dts = AV_NOPTS_VALUE;
  965. pes->last_pcr = -1;
  966. tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
  967. if (!tss) {
  968. av_free(pes);
  969. return 0;
  970. }
  971. return pes;
  972. }
  973. #define MAX_LEVEL 4
  974. typedef struct {
  975. AVFormatContext *s;
  976. AVIOContext pb;
  977. Mp4Descr *descr;
  978. Mp4Descr *active_descr;
  979. int descr_count;
  980. int max_descr_count;
  981. int level;
  982. } MP4DescrParseContext;
  983. static int init_MP4DescrParseContext(
  984. MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf,
  985. unsigned size, Mp4Descr *descr, int max_descr_count)
  986. {
  987. int ret;
  988. if (size > (1<<30))
  989. return AVERROR_INVALIDDATA;
  990. if ((ret = ffio_init_context(&d->pb, (unsigned char*)buf, size, 0,
  991. NULL, NULL, NULL, NULL)) < 0)
  992. return ret;
  993. d->s = s;
  994. d->level = 0;
  995. d->descr_count = 0;
  996. d->descr = descr;
  997. d->active_descr = NULL;
  998. d->max_descr_count = max_descr_count;
  999. return 0;
  1000. }
  1001. static void update_offsets(AVIOContext *pb, int64_t *off, int *len) {
  1002. int64_t new_off = avio_tell(pb);
  1003. (*len) -= new_off - *off;
  1004. *off = new_off;
  1005. }
  1006. static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
  1007. int target_tag);
  1008. static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
  1009. {
  1010. while (len > 0) {
  1011. if (parse_mp4_descr(d, off, len, 0) < 0)
  1012. return -1;
  1013. update_offsets(&d->pb, &off, &len);
  1014. }
  1015. return 0;
  1016. }
  1017. static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1018. {
  1019. avio_rb16(&d->pb); // ID
  1020. avio_r8(&d->pb);
  1021. avio_r8(&d->pb);
  1022. avio_r8(&d->pb);
  1023. avio_r8(&d->pb);
  1024. avio_r8(&d->pb);
  1025. update_offsets(&d->pb, &off, &len);
  1026. return parse_mp4_descr_arr(d, off, len);
  1027. }
  1028. static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1029. {
  1030. int id_flags;
  1031. if (len < 2)
  1032. return 0;
  1033. id_flags = avio_rb16(&d->pb);
  1034. if (!(id_flags & 0x0020)) { //URL_Flag
  1035. update_offsets(&d->pb, &off, &len);
  1036. return parse_mp4_descr_arr(d, off, len); //ES_Descriptor[]
  1037. } else {
  1038. return 0;
  1039. }
  1040. }
  1041. static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1042. {
  1043. int es_id = 0;
  1044. if (d->descr_count >= d->max_descr_count)
  1045. return -1;
  1046. ff_mp4_parse_es_descr(&d->pb, &es_id);
  1047. d->active_descr = d->descr + (d->descr_count++);
  1048. d->active_descr->es_id = es_id;
  1049. update_offsets(&d->pb, &off, &len);
  1050. parse_mp4_descr(d, off, len, MP4DecConfigDescrTag);
  1051. update_offsets(&d->pb, &off, &len);
  1052. if (len > 0)
  1053. parse_mp4_descr(d, off, len, MP4SLDescrTag);
  1054. d->active_descr = NULL;
  1055. return 0;
  1056. }
  1057. static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1058. {
  1059. Mp4Descr *descr = d->active_descr;
  1060. if (!descr)
  1061. return -1;
  1062. d->active_descr->dec_config_descr = av_malloc(len);
  1063. if (!descr->dec_config_descr)
  1064. return AVERROR(ENOMEM);
  1065. descr->dec_config_descr_len = len;
  1066. avio_read(&d->pb, descr->dec_config_descr, len);
  1067. return 0;
  1068. }
  1069. static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
  1070. {
  1071. Mp4Descr *descr = d->active_descr;
  1072. int predefined;
  1073. if (!descr)
  1074. return -1;
  1075. predefined = avio_r8(&d->pb);
  1076. if (!predefined) {
  1077. int lengths;
  1078. int flags = avio_r8(&d->pb);
  1079. descr->sl.use_au_start = !!(flags & 0x80);
  1080. descr->sl.use_au_end = !!(flags & 0x40);
  1081. descr->sl.use_rand_acc_pt = !!(flags & 0x20);
  1082. descr->sl.use_padding = !!(flags & 0x08);
  1083. descr->sl.use_timestamps = !!(flags & 0x04);
  1084. descr->sl.use_idle = !!(flags & 0x02);
  1085. descr->sl.timestamp_res = avio_rb32(&d->pb);
  1086. avio_rb32(&d->pb);
  1087. descr->sl.timestamp_len = avio_r8(&d->pb);
  1088. descr->sl.ocr_len = avio_r8(&d->pb);
  1089. descr->sl.au_len = avio_r8(&d->pb);
  1090. descr->sl.inst_bitrate_len = avio_r8(&d->pb);
  1091. lengths = avio_rb16(&d->pb);
  1092. descr->sl.degr_prior_len = lengths >> 12;
  1093. descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
  1094. descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
  1095. } else {
  1096. avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
  1097. }
  1098. return 0;
  1099. }
  1100. static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
  1101. int target_tag) {
  1102. int tag;
  1103. int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
  1104. update_offsets(&d->pb, &off, &len);
  1105. if (len < 0 || len1 > len || len1 <= 0) {
  1106. av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
  1107. return -1;
  1108. }
  1109. if (d->level++ >= MAX_LEVEL) {
  1110. av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
  1111. goto done;
  1112. }
  1113. if (target_tag && tag != target_tag) {
  1114. av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag);
  1115. goto done;
  1116. }
  1117. switch (tag) {
  1118. case MP4IODescrTag:
  1119. parse_MP4IODescrTag(d, off, len1);
  1120. break;
  1121. case MP4ODescrTag:
  1122. parse_MP4ODescrTag(d, off, len1);
  1123. break;
  1124. case MP4ESDescrTag:
  1125. parse_MP4ESDescrTag(d, off, len1);
  1126. break;
  1127. case MP4DecConfigDescrTag:
  1128. parse_MP4DecConfigDescrTag(d, off, len1);
  1129. break;
  1130. case MP4SLDescrTag:
  1131. parse_MP4SLDescrTag(d, off, len1);
  1132. break;
  1133. }
  1134. done:
  1135. d->level--;
  1136. avio_seek(&d->pb, off + len1, SEEK_SET);
  1137. return 0;
  1138. }
  1139. static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
  1140. Mp4Descr *descr, int *descr_count, int max_descr_count)
  1141. {
  1142. MP4DescrParseContext d;
  1143. if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
  1144. return -1;
  1145. parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
  1146. *descr_count = d.descr_count;
  1147. return 0;
  1148. }
  1149. static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
  1150. Mp4Descr *descr, int *descr_count, int max_descr_count)
  1151. {
  1152. MP4DescrParseContext d;
  1153. if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
  1154. return -1;
  1155. parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
  1156. *descr_count = d.descr_count;
  1157. return 0;
  1158. }
  1159. static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1160. {
  1161. MpegTSContext *ts = filter->u.section_filter.opaque;
  1162. SectionHeader h;
  1163. const uint8_t *p, *p_end;
  1164. AVIOContext pb;
  1165. Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
  1166. int mp4_descr_count = 0;
  1167. int i, pid;
  1168. AVFormatContext *s = ts->stream;
  1169. p_end = section + section_len - 4;
  1170. p = section;
  1171. if (parse_section_header(&h, &p, p_end) < 0)
  1172. return;
  1173. if (h.tid != M4OD_TID)
  1174. return;
  1175. mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT);
  1176. for (pid = 0; pid < NB_PID_MAX; pid++) {
  1177. if (!ts->pids[pid])
  1178. continue;
  1179. for (i = 0; i < mp4_descr_count; i++) {
  1180. PESContext *pes;
  1181. AVStream *st;
  1182. if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
  1183. continue;
  1184. if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
  1185. av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
  1186. continue;
  1187. }
  1188. pes = ts->pids[pid]->u.pes_filter.opaque;
  1189. st = pes->st;
  1190. if (!st) {
  1191. continue;
  1192. }
  1193. pes->sl = mp4_descr[i].sl;
  1194. ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
  1195. mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1196. ff_mp4_read_dec_config_descr(s, st, &pb);
  1197. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1198. st->codec->extradata_size > 0)
  1199. st->need_parsing = 0;
  1200. if (st->codec->codec_id == AV_CODEC_ID_H264 &&
  1201. st->codec->extradata_size > 0)
  1202. st->need_parsing = 0;
  1203. if (st->codec->codec_id <= AV_CODEC_ID_NONE) {
  1204. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO) {
  1205. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1206. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE) {
  1207. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1208. } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN) {
  1209. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1210. }
  1211. }
  1212. }
  1213. for (i = 0; i < mp4_descr_count; i++)
  1214. av_free(mp4_descr[i].dec_config_descr);
  1215. }
  1216. int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
  1217. const uint8_t **pp, const uint8_t *desc_list_end,
  1218. Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
  1219. MpegTSContext *ts)
  1220. {
  1221. const uint8_t *desc_end;
  1222. int desc_len, desc_tag, desc_es_id;
  1223. char language[252];
  1224. int i;
  1225. desc_tag = get8(pp, desc_list_end);
  1226. if (desc_tag < 0)
  1227. return -1;
  1228. desc_len = get8(pp, desc_list_end);
  1229. if (desc_len < 0)
  1230. return -1;
  1231. desc_end = *pp + desc_len;
  1232. if (desc_end > desc_list_end)
  1233. return -1;
  1234. av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
  1235. if (st->codec->codec_id == AV_CODEC_ID_NONE &&
  1236. stream_type == STREAM_TYPE_PRIVATE_DATA)
  1237. mpegts_find_stream_type(st, desc_tag, DESC_types);
  1238. switch(desc_tag) {
  1239. case 0x1E: /* SL descriptor */
  1240. desc_es_id = get16(pp, desc_end);
  1241. if (ts && ts->pids[pid])
  1242. ts->pids[pid]->es_id = desc_es_id;
  1243. for (i = 0; i < mp4_descr_count; i++)
  1244. if (mp4_descr[i].dec_config_descr_len &&
  1245. mp4_descr[i].es_id == desc_es_id) {
  1246. AVIOContext pb;
  1247. ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
  1248. mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1249. ff_mp4_read_dec_config_descr(fc, st, &pb);
  1250. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1251. st->codec->extradata_size > 0)
  1252. st->need_parsing = 0;
  1253. if (st->codec->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
  1254. mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
  1255. }
  1256. break;
  1257. case 0x1F: /* FMC descriptor */
  1258. get16(pp, desc_end);
  1259. if (mp4_descr_count > 0 && (st->codec->codec_id == AV_CODEC_ID_AAC_LATM || st->request_probe>0) &&
  1260. mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
  1261. AVIOContext pb;
  1262. ffio_init_context(&pb, mp4_descr->dec_config_descr,
  1263. mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
  1264. ff_mp4_read_dec_config_descr(fc, st, &pb);
  1265. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  1266. st->codec->extradata_size > 0){
  1267. st->request_probe= st->need_parsing = 0;
  1268. st->codec->codec_type= AVMEDIA_TYPE_AUDIO;
  1269. }
  1270. }
  1271. break;
  1272. case 0x56: /* DVB teletext descriptor */
  1273. language[0] = get8(pp, desc_end);
  1274. language[1] = get8(pp, desc_end);
  1275. language[2] = get8(pp, desc_end);
  1276. language[3] = 0;
  1277. av_dict_set(&st->metadata, "language", language, 0);
  1278. break;
  1279. case 0x59: /* subtitling descriptor */
  1280. language[0] = get8(pp, desc_end);
  1281. language[1] = get8(pp, desc_end);
  1282. language[2] = get8(pp, desc_end);
  1283. language[3] = 0;
  1284. /* hearing impaired subtitles detection */
  1285. switch(get8(pp, desc_end)) {
  1286. case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
  1287. case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
  1288. case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
  1289. case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
  1290. case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
  1291. case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
  1292. st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
  1293. break;
  1294. }
  1295. if (st->codec->extradata) {
  1296. if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
  1297. avpriv_request_sample(fc, "DVB sub with multiple IDs");
  1298. } else {
  1299. if (!ff_alloc_extradata(st->codec, 4)) {
  1300. memcpy(st->codec->extradata, *pp, 4);
  1301. }
  1302. }
  1303. *pp += 4;
  1304. av_dict_set(&st->metadata, "language", language, 0);
  1305. break;
  1306. case 0x0a: /* ISO 639 language descriptor */
  1307. for (i = 0; i + 4 <= desc_len; i += 4) {
  1308. language[i + 0] = get8(pp, desc_end);
  1309. language[i + 1] = get8(pp, desc_end);
  1310. language[i + 2] = get8(pp, desc_end);
  1311. language[i + 3] = ',';
  1312. switch (get8(pp, desc_end)) {
  1313. case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
  1314. case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
  1315. case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
  1316. }
  1317. }
  1318. if (i) {
  1319. language[i - 1] = 0;
  1320. av_dict_set(&st->metadata, "language", language, 0);
  1321. }
  1322. break;
  1323. case 0x05: /* registration descriptor */
  1324. st->codec->codec_tag = bytestream_get_le32(pp);
  1325. av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
  1326. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  1327. mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
  1328. break;
  1329. case 0x52: /* stream identifier descriptor */
  1330. st->stream_identifier = 1 + get8(pp, desc_end);
  1331. break;
  1332. default:
  1333. break;
  1334. }
  1335. *pp = desc_end;
  1336. return 0;
  1337. }
  1338. static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1339. {
  1340. MpegTSContext *ts = filter->u.section_filter.opaque;
  1341. SectionHeader h1, *h = &h1;
  1342. PESContext *pes;
  1343. AVStream *st;
  1344. const uint8_t *p, *p_end, *desc_list_end;
  1345. int program_info_length, pcr_pid, pid, stream_type;
  1346. int desc_list_len;
  1347. uint32_t prog_reg_desc = 0; /* registration descriptor */
  1348. Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
  1349. int mp4_descr_count = 0;
  1350. int i;
  1351. av_dlog(ts->stream, "PMT: len %i\n", section_len);
  1352. hex_dump_debug(ts->stream, section, section_len);
  1353. p_end = section + section_len - 4;
  1354. p = section;
  1355. if (parse_section_header(h, &p, p_end) < 0)
  1356. return;
  1357. av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
  1358. h->id, h->sec_num, h->last_sec_num);
  1359. if (h->tid != PMT_TID)
  1360. return;
  1361. clear_program(ts, h->id);
  1362. pcr_pid = get16(&p, p_end);
  1363. if (pcr_pid < 0)
  1364. return;
  1365. pcr_pid &= 0x1fff;
  1366. add_pid_to_pmt(ts, h->id, pcr_pid);
  1367. set_pcr_pid(ts->stream, h->id, pcr_pid);
  1368. av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
  1369. program_info_length = get16(&p, p_end);
  1370. if (program_info_length < 0)
  1371. return;
  1372. program_info_length &= 0xfff;
  1373. while(program_info_length >= 2) {
  1374. uint8_t tag, len;
  1375. tag = get8(&p, p_end);
  1376. len = get8(&p, p_end);
  1377. av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
  1378. if(len > program_info_length - 2)
  1379. //something else is broken, exit the program_descriptors_loop
  1380. break;
  1381. program_info_length -= len + 2;
  1382. if (tag == 0x1d) { // IOD descriptor
  1383. get8(&p, p_end); // scope
  1384. get8(&p, p_end); // label
  1385. len -= 2;
  1386. mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
  1387. &mp4_descr_count, MAX_MP4_DESCR_COUNT);
  1388. } else if (tag == 0x05 && len >= 4) { // registration descriptor
  1389. prog_reg_desc = bytestream_get_le32(&p);
  1390. len -= 4;
  1391. }
  1392. p += len;
  1393. }
  1394. p += program_info_length;
  1395. if (p >= p_end)
  1396. goto out;
  1397. // stop parsing after pmt, we found header
  1398. if (!ts->stream->nb_streams)
  1399. ts->stop_parse = 2;
  1400. for(;;) {
  1401. st = 0;
  1402. pes = NULL;
  1403. stream_type = get8(&p, p_end);
  1404. if (stream_type < 0)
  1405. break;
  1406. pid = get16(&p, p_end);
  1407. if (pid < 0)
  1408. break;
  1409. pid &= 0x1fff;
  1410. if (pid == ts->current_pid)
  1411. break;
  1412. /* now create stream */
  1413. if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
  1414. pes = ts->pids[pid]->u.pes_filter.opaque;
  1415. if (!pes->st) {
  1416. pes->st = avformat_new_stream(pes->stream, NULL);
  1417. if (!pes->st)
  1418. goto out;
  1419. pes->st->id = pes->pid;
  1420. }
  1421. st = pes->st;
  1422. } else if (stream_type != 0x13) {
  1423. if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
  1424. pes = add_pes_stream(ts, pid, pcr_pid);
  1425. if (pes) {
  1426. st = avformat_new_stream(pes->stream, NULL);
  1427. if (!st)
  1428. goto out;
  1429. st->id = pes->pid;
  1430. }
  1431. } else {
  1432. int idx = ff_find_stream_index(ts->stream, pid);
  1433. if (idx >= 0) {
  1434. st = ts->stream->streams[idx];
  1435. } else {
  1436. st = avformat_new_stream(ts->stream, NULL);
  1437. if (!st)
  1438. goto out;
  1439. st->id = pid;
  1440. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1441. }
  1442. }
  1443. if (!st)
  1444. goto out;
  1445. if (pes && !pes->stream_type)
  1446. mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
  1447. add_pid_to_pmt(ts, h->id, pid);
  1448. ff_program_add_stream_index(ts->stream, h->id, st->index);
  1449. desc_list_len = get16(&p, p_end);
  1450. if (desc_list_len < 0)
  1451. break;
  1452. desc_list_len &= 0xfff;
  1453. desc_list_end = p + desc_list_len;
  1454. if (desc_list_end > p_end)
  1455. break;
  1456. for(;;) {
  1457. if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
  1458. mp4_descr, mp4_descr_count, pid, ts) < 0)
  1459. break;
  1460. if (pes && prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
  1461. ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
  1462. pes->sub_st->codec->codec_tag = st->codec->codec_tag;
  1463. }
  1464. }
  1465. p = desc_list_end;
  1466. }
  1467. out:
  1468. for (i = 0; i < mp4_descr_count; i++)
  1469. av_free(mp4_descr[i].dec_config_descr);
  1470. }
  1471. static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1472. {
  1473. MpegTSContext *ts = filter->u.section_filter.opaque;
  1474. SectionHeader h1, *h = &h1;
  1475. const uint8_t *p, *p_end;
  1476. int sid, pmt_pid;
  1477. AVProgram *program;
  1478. av_dlog(ts->stream, "PAT:\n");
  1479. hex_dump_debug(ts->stream, section, section_len);
  1480. p_end = section + section_len - 4;
  1481. p = section;
  1482. if (parse_section_header(h, &p, p_end) < 0)
  1483. return;
  1484. if (h->tid != PAT_TID)
  1485. return;
  1486. ts->stream->ts_id = h->id;
  1487. clear_programs(ts);
  1488. for(;;) {
  1489. sid = get16(&p, p_end);
  1490. if (sid < 0)
  1491. break;
  1492. pmt_pid = get16(&p, p_end);
  1493. if (pmt_pid < 0)
  1494. break;
  1495. pmt_pid &= 0x1fff;
  1496. if (pmt_pid == ts->current_pid)
  1497. break;
  1498. av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
  1499. if (sid == 0x0000) {
  1500. /* NIT info */
  1501. } else {
  1502. MpegTSFilter *fil = ts->pids[pmt_pid];
  1503. program = av_new_program(ts->stream, sid);
  1504. program->program_num = sid;
  1505. program->pmt_pid = pmt_pid;
  1506. if (fil)
  1507. if ( fil->type != MPEGTS_SECTION
  1508. || fil->pid != pmt_pid
  1509. || fil->u.section_filter.section_cb != pmt_cb)
  1510. mpegts_close_filter(ts, ts->pids[pmt_pid]);
  1511. if (!ts->pids[pmt_pid])
  1512. mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
  1513. add_pat_entry(ts, sid);
  1514. add_pid_to_pmt(ts, sid, 0); //add pat pid to program
  1515. add_pid_to_pmt(ts, sid, pmt_pid);
  1516. }
  1517. }
  1518. if (sid < 0) {
  1519. int i,j;
  1520. for (j=0; j<ts->stream->nb_programs; j++) {
  1521. for (i=0; i<ts->nb_prg; i++)
  1522. if (ts->prg[i].id == ts->stream->programs[j]->id)
  1523. break;
  1524. if (i==ts->nb_prg)
  1525. clear_avprogram(ts, ts->stream->programs[j]->id);
  1526. }
  1527. }
  1528. }
  1529. static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
  1530. {
  1531. MpegTSContext *ts = filter->u.section_filter.opaque;
  1532. SectionHeader h1, *h = &h1;
  1533. const uint8_t *p, *p_end, *desc_list_end, *desc_end;
  1534. int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
  1535. char *name, *provider_name;
  1536. av_dlog(ts->stream, "SDT:\n");
  1537. hex_dump_debug(ts->stream, section, section_len);
  1538. p_end = section + section_len - 4;
  1539. p = section;
  1540. if (parse_section_header(h, &p, p_end) < 0)
  1541. return;
  1542. if (h->tid != SDT_TID)
  1543. return;
  1544. onid = get16(&p, p_end);
  1545. if (onid < 0)
  1546. return;
  1547. val = get8(&p, p_end);
  1548. if (val < 0)
  1549. return;
  1550. for(;;) {
  1551. sid = get16(&p, p_end);
  1552. if (sid < 0)
  1553. break;
  1554. val = get8(&p, p_end);
  1555. if (val < 0)
  1556. break;
  1557. desc_list_len = get16(&p, p_end);
  1558. if (desc_list_len < 0)
  1559. break;
  1560. desc_list_len &= 0xfff;
  1561. desc_list_end = p + desc_list_len;
  1562. if (desc_list_end > p_end)
  1563. break;
  1564. for(;;) {
  1565. desc_tag = get8(&p, desc_list_end);
  1566. if (desc_tag < 0)
  1567. break;
  1568. desc_len = get8(&p, desc_list_end);
  1569. desc_end = p + desc_len;
  1570. if (desc_end > desc_list_end)
  1571. break;
  1572. av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
  1573. desc_tag, desc_len);
  1574. switch(desc_tag) {
  1575. case 0x48:
  1576. service_type = get8(&p, p_end);
  1577. if (service_type < 0)
  1578. break;
  1579. provider_name = getstr8(&p, p_end);
  1580. if (!provider_name)
  1581. break;
  1582. name = getstr8(&p, p_end);
  1583. if (name) {
  1584. AVProgram *program = av_new_program(ts->stream, sid);
  1585. if(program) {
  1586. av_dict_set(&program->metadata, "service_name", name, 0);
  1587. av_dict_set(&program->metadata, "service_provider", provider_name, 0);
  1588. }
  1589. }
  1590. av_free(name);
  1591. av_free(provider_name);
  1592. break;
  1593. default:
  1594. break;
  1595. }
  1596. p = desc_end;
  1597. }
  1598. p = desc_list_end;
  1599. }
  1600. }
  1601. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1602. const uint8_t *packet);
  1603. /* handle one TS packet */
  1604. static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
  1605. {
  1606. AVFormatContext *s = ts->stream;
  1607. MpegTSFilter *tss;
  1608. int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
  1609. has_adaptation, has_payload;
  1610. const uint8_t *p, *p_end;
  1611. int64_t pos;
  1612. pid = AV_RB16(packet + 1) & 0x1fff;
  1613. if(pid && discard_pid(ts, pid))
  1614. return 0;
  1615. is_start = packet[1] & 0x40;
  1616. tss = ts->pids[pid];
  1617. if (ts->auto_guess && tss == NULL && is_start) {
  1618. add_pes_stream(ts, pid, -1);
  1619. tss = ts->pids[pid];
  1620. }
  1621. if (!tss)
  1622. return 0;
  1623. ts->current_pid = pid;
  1624. afc = (packet[3] >> 4) & 3;
  1625. if (afc == 0) /* reserved value */
  1626. return 0;
  1627. has_adaptation = afc & 2;
  1628. has_payload = afc & 1;
  1629. is_discontinuity = has_adaptation
  1630. && packet[4] != 0 /* with length > 0 */
  1631. && (packet[5] & 0x80); /* and discontinuity indicated */
  1632. /* continuity check (currently not used) */
  1633. cc = (packet[3] & 0xf);
  1634. expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
  1635. cc_ok = pid == 0x1FFF // null packet PID
  1636. || is_discontinuity
  1637. || tss->last_cc < 0
  1638. || expected_cc == cc;
  1639. tss->last_cc = cc;
  1640. if (!cc_ok) {
  1641. av_log(ts->stream, AV_LOG_DEBUG,
  1642. "Continuity check failed for pid %d expected %d got %d\n",
  1643. pid, expected_cc, cc);
  1644. if(tss->type == MPEGTS_PES) {
  1645. PESContext *pc = tss->u.pes_filter.opaque;
  1646. pc->flags |= AV_PKT_FLAG_CORRUPT;
  1647. }
  1648. }
  1649. if (!has_payload)
  1650. return 0;
  1651. p = packet + 4;
  1652. if (has_adaptation) {
  1653. /* skip adaptation field */
  1654. p += p[0] + 1;
  1655. }
  1656. /* if past the end of packet, ignore */
  1657. p_end = packet + TS_PACKET_SIZE;
  1658. if (p >= p_end)
  1659. return 0;
  1660. pos = avio_tell(ts->stream->pb);
  1661. if (pos >= 0) {
  1662. av_assert0(pos >= TS_PACKET_SIZE);
  1663. ts->pos47_full = pos - TS_PACKET_SIZE;
  1664. }
  1665. if (tss->type == MPEGTS_SECTION) {
  1666. if (is_start) {
  1667. /* pointer field present */
  1668. len = *p++;
  1669. if (p + len > p_end)
  1670. return 0;
  1671. if (len && cc_ok) {
  1672. /* write remaining section bytes */
  1673. write_section_data(s, tss,
  1674. p, len, 0);
  1675. /* check whether filter has been closed */
  1676. if (!ts->pids[pid])
  1677. return 0;
  1678. }
  1679. p += len;
  1680. if (p < p_end) {
  1681. write_section_data(s, tss,
  1682. p, p_end - p, 1);
  1683. }
  1684. } else {
  1685. if (cc_ok) {
  1686. write_section_data(s, tss,
  1687. p, p_end - p, 0);
  1688. }
  1689. }
  1690. } else {
  1691. int ret;
  1692. int64_t pcr = -1;
  1693. int64_t pcr_h;
  1694. int pcr_l;
  1695. if (parse_pcr(&pcr_h, &pcr_l, packet) == 0)
  1696. pcr = pcr_h * 300 + pcr_l;
  1697. // Note: The position here points actually behind the current packet.
  1698. if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
  1699. pos - ts->raw_packet_size, pcr)) < 0)
  1700. return ret;
  1701. }
  1702. return 0;
  1703. }
  1704. static void reanalyze(MpegTSContext *ts) {
  1705. AVIOContext *pb = ts->stream->pb;
  1706. int64_t pos = avio_tell(pb);
  1707. if(pos < 0)
  1708. return;
  1709. pos -= ts->pos47_full;
  1710. if (pos == TS_PACKET_SIZE) {
  1711. ts->size_stat[0] ++;
  1712. } else if (pos == TS_DVHS_PACKET_SIZE) {
  1713. ts->size_stat[1] ++;
  1714. } else if (pos == TS_FEC_PACKET_SIZE) {
  1715. ts->size_stat[2] ++;
  1716. }
  1717. ts->size_stat_count ++;
  1718. if(ts->size_stat_count > SIZE_STAT_THRESHOLD) {
  1719. int newsize = 0;
  1720. if (ts->size_stat[0] > SIZE_STAT_THRESHOLD) {
  1721. newsize = TS_PACKET_SIZE;
  1722. } else if (ts->size_stat[1] > SIZE_STAT_THRESHOLD) {
  1723. newsize = TS_DVHS_PACKET_SIZE;
  1724. } else if (ts->size_stat[2] > SIZE_STAT_THRESHOLD) {
  1725. newsize = TS_FEC_PACKET_SIZE;
  1726. }
  1727. if (newsize && newsize != ts->raw_packet_size) {
  1728. av_log(ts->stream, AV_LOG_WARNING, "changing packet size to %d\n", newsize);
  1729. ts->raw_packet_size = newsize;
  1730. }
  1731. ts->size_stat_count = 0;
  1732. memset(ts->size_stat, 0, sizeof(ts->size_stat));
  1733. }
  1734. }
  1735. /* XXX: try to find a better synchro over several packets (use
  1736. get_packet_size() ?) */
  1737. static int mpegts_resync(AVFormatContext *s)
  1738. {
  1739. AVIOContext *pb = s->pb;
  1740. int c, i;
  1741. for(i = 0;i < MAX_RESYNC_SIZE; i++) {
  1742. c = avio_r8(pb);
  1743. if (url_feof(pb))
  1744. return -1;
  1745. if (c == 0x47) {
  1746. avio_seek(pb, -1, SEEK_CUR);
  1747. reanalyze(s->priv_data);
  1748. return 0;
  1749. }
  1750. }
  1751. av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
  1752. /* no sync found */
  1753. return -1;
  1754. }
  1755. /* return -1 if error or EOF. Return 0 if OK. */
  1756. static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, const uint8_t **data)
  1757. {
  1758. AVIOContext *pb = s->pb;
  1759. int len;
  1760. for(;;) {
  1761. len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data);
  1762. if (len != TS_PACKET_SIZE)
  1763. return len < 0 ? len : AVERROR_EOF;
  1764. /* check packet sync byte */
  1765. if ((*data)[0] != 0x47) {
  1766. /* find a new packet start */
  1767. avio_seek(pb, -raw_packet_size, SEEK_CUR);
  1768. if (mpegts_resync(s) < 0)
  1769. return AVERROR(EAGAIN);
  1770. else
  1771. continue;
  1772. } else {
  1773. break;
  1774. }
  1775. }
  1776. return 0;
  1777. }
  1778. static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
  1779. {
  1780. AVIOContext *pb = s->pb;
  1781. int skip = raw_packet_size - TS_PACKET_SIZE;
  1782. if (skip > 0)
  1783. avio_skip(pb, skip);
  1784. }
  1785. static int handle_packets(MpegTSContext *ts, int nb_packets)
  1786. {
  1787. AVFormatContext *s = ts->stream;
  1788. uint8_t packet[TS_PACKET_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  1789. const uint8_t *data;
  1790. int packet_num, ret = 0;
  1791. if (avio_tell(s->pb) != ts->last_pos) {
  1792. int i;
  1793. av_dlog(ts->stream, "Skipping after seek\n");
  1794. /* seek detected, flush pes buffer */
  1795. for (i = 0; i < NB_PID_MAX; i++) {
  1796. if (ts->pids[i]) {
  1797. if (ts->pids[i]->type == MPEGTS_PES) {
  1798. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  1799. av_buffer_unref(&pes->buffer);
  1800. pes->data_index = 0;
  1801. pes->state = MPEGTS_SKIP; /* skip until pes header */
  1802. pes->last_pcr = -1;
  1803. }
  1804. ts->pids[i]->last_cc = -1;
  1805. }
  1806. }
  1807. }
  1808. ts->stop_parse = 0;
  1809. packet_num = 0;
  1810. memset(packet + TS_PACKET_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  1811. for(;;) {
  1812. packet_num++;
  1813. if (nb_packets != 0 && packet_num >= nb_packets ||
  1814. ts->stop_parse > 1) {
  1815. ret = AVERROR(EAGAIN);
  1816. break;
  1817. }
  1818. if (ts->stop_parse > 0)
  1819. break;
  1820. ret = read_packet(s, packet, ts->raw_packet_size, &data);
  1821. if (ret != 0)
  1822. break;
  1823. ret = handle_packet(ts, data);
  1824. finished_reading_packet(s, ts->raw_packet_size);
  1825. if (ret != 0)
  1826. break;
  1827. }
  1828. ts->last_pos = avio_tell(s->pb);
  1829. return ret;
  1830. }
  1831. static int mpegts_probe(AVProbeData *p)
  1832. {
  1833. const int size= p->buf_size;
  1834. int maxscore=0;
  1835. int sumscore=0;
  1836. int i;
  1837. int check_count= size / TS_FEC_PACKET_SIZE;
  1838. #define CHECK_COUNT 10
  1839. #define CHECK_BLOCK 100
  1840. if (check_count < CHECK_COUNT)
  1841. return -1;
  1842. for (i=0; i<check_count; i+=CHECK_BLOCK){
  1843. int left = FFMIN(check_count - i, CHECK_BLOCK);
  1844. int score = analyze(p->buf + TS_PACKET_SIZE *i, TS_PACKET_SIZE *left, TS_PACKET_SIZE , NULL);
  1845. int dvhs_score= analyze(p->buf + TS_DVHS_PACKET_SIZE*i, TS_DVHS_PACKET_SIZE*left, TS_DVHS_PACKET_SIZE, NULL);
  1846. int fec_score = analyze(p->buf + TS_FEC_PACKET_SIZE *i, TS_FEC_PACKET_SIZE *left, TS_FEC_PACKET_SIZE , NULL);
  1847. score = FFMAX3(score, dvhs_score, fec_score);
  1848. sumscore += score;
  1849. maxscore = FFMAX(maxscore, score);
  1850. }
  1851. sumscore = sumscore*CHECK_COUNT/check_count;
  1852. maxscore = maxscore*CHECK_COUNT/CHECK_BLOCK;
  1853. av_dlog(0, "TS score: %d %d\n", sumscore, maxscore);
  1854. if (sumscore > 6) return AVPROBE_SCORE_MAX + sumscore - CHECK_COUNT;
  1855. else if (maxscore > 6) return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT;
  1856. else return -1;
  1857. }
  1858. /* return the 90kHz PCR and the extension for the 27MHz PCR. return
  1859. (-1) if not available */
  1860. static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
  1861. const uint8_t *packet)
  1862. {
  1863. int afc, len, flags;
  1864. const uint8_t *p;
  1865. unsigned int v;
  1866. afc = (packet[3] >> 4) & 3;
  1867. if (afc <= 1)
  1868. return -1;
  1869. p = packet + 4;
  1870. len = p[0];
  1871. p++;
  1872. if (len == 0)
  1873. return -1;
  1874. flags = *p++;
  1875. len--;
  1876. if (!(flags & 0x10))
  1877. return -1;
  1878. if (len < 6)
  1879. return -1;
  1880. v = AV_RB32(p);
  1881. *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
  1882. *ppcr_low = ((p[4] & 1) << 8) | p[5];
  1883. return 0;
  1884. }
  1885. static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos) {
  1886. /* NOTE: We attempt to seek on non-seekable files as well, as the
  1887. * probe buffer usually is big enough. Only warn if the seek failed
  1888. * on files where the seek should work. */
  1889. if (avio_seek(pb, pos, SEEK_SET) < 0)
  1890. av_log(s, pb->seekable ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
  1891. }
  1892. static int mpegts_read_header(AVFormatContext *s)
  1893. {
  1894. MpegTSContext *ts = s->priv_data;
  1895. AVIOContext *pb = s->pb;
  1896. uint8_t buf[8*1024]={0};
  1897. int len;
  1898. int64_t pos;
  1899. ffio_ensure_seekback(pb, s->probesize);
  1900. /* read the first 8192 bytes to get packet size */
  1901. pos = avio_tell(pb);
  1902. len = avio_read(pb, buf, sizeof(buf));
  1903. ts->raw_packet_size = get_packet_size(buf, len);
  1904. if (ts->raw_packet_size <= 0) {
  1905. av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
  1906. ts->raw_packet_size = TS_PACKET_SIZE;
  1907. }
  1908. ts->stream = s;
  1909. ts->auto_guess = 0;
  1910. if (s->iformat == &ff_mpegts_demuxer) {
  1911. /* normal demux */
  1912. /* first do a scan to get all the services */
  1913. seek_back(s, pb, pos);
  1914. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  1915. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  1916. handle_packets(ts, s->probesize / ts->raw_packet_size);
  1917. /* if could not find service, enable auto_guess */
  1918. ts->auto_guess = 1;
  1919. av_dlog(ts->stream, "tuning done\n");
  1920. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1921. } else {
  1922. AVStream *st;
  1923. int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
  1924. int64_t pcrs[2], pcr_h;
  1925. int packet_count[2];
  1926. uint8_t packet[TS_PACKET_SIZE];
  1927. const uint8_t *data;
  1928. /* only read packets */
  1929. st = avformat_new_stream(s, NULL);
  1930. if (!st)
  1931. goto fail;
  1932. avpriv_set_pts_info(st, 60, 1, 27000000);
  1933. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1934. st->codec->codec_id = AV_CODEC_ID_MPEG2TS;
  1935. /* we iterate until we find two PCRs to estimate the bitrate */
  1936. pcr_pid = -1;
  1937. nb_pcrs = 0;
  1938. nb_packets = 0;
  1939. for(;;) {
  1940. ret = read_packet(s, packet, ts->raw_packet_size, &data);
  1941. if (ret < 0)
  1942. goto fail;
  1943. pid = AV_RB16(data + 1) & 0x1fff;
  1944. if ((pcr_pid == -1 || pcr_pid == pid) &&
  1945. parse_pcr(&pcr_h, &pcr_l, data) == 0) {
  1946. finished_reading_packet(s, ts->raw_packet_size);
  1947. pcr_pid = pid;
  1948. packet_count[nb_pcrs] = nb_packets;
  1949. pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
  1950. nb_pcrs++;
  1951. if (nb_pcrs >= 2)
  1952. break;
  1953. } else {
  1954. finished_reading_packet(s, ts->raw_packet_size);
  1955. }
  1956. nb_packets++;
  1957. }
  1958. /* NOTE1: the bitrate is computed without the FEC */
  1959. /* NOTE2: it is only the bitrate of the start of the stream */
  1960. ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
  1961. ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
  1962. s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
  1963. st->codec->bit_rate = s->bit_rate;
  1964. st->start_time = ts->cur_pcr;
  1965. av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
  1966. st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
  1967. }
  1968. seek_back(s, pb, pos);
  1969. return 0;
  1970. fail:
  1971. return -1;
  1972. }
  1973. #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
  1974. static int mpegts_raw_read_packet(AVFormatContext *s,
  1975. AVPacket *pkt)
  1976. {
  1977. MpegTSContext *ts = s->priv_data;
  1978. int ret, i;
  1979. int64_t pcr_h, next_pcr_h, pos;
  1980. int pcr_l, next_pcr_l;
  1981. uint8_t pcr_buf[12];
  1982. const uint8_t *data;
  1983. if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
  1984. return AVERROR(ENOMEM);
  1985. pkt->pos= avio_tell(s->pb);
  1986. ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
  1987. if (ret < 0) {
  1988. av_free_packet(pkt);
  1989. return ret;
  1990. }
  1991. if (data != pkt->data)
  1992. memcpy(pkt->data, data, ts->raw_packet_size);
  1993. finished_reading_packet(s, ts->raw_packet_size);
  1994. if (ts->mpeg2ts_compute_pcr) {
  1995. /* compute exact PCR for each packet */
  1996. if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
  1997. /* we read the next PCR (XXX: optimize it by using a bigger buffer */
  1998. pos = avio_tell(s->pb);
  1999. for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
  2000. avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
  2001. avio_read(s->pb, pcr_buf, 12);
  2002. if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
  2003. /* XXX: not precise enough */
  2004. ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
  2005. (i + 1);
  2006. break;
  2007. }
  2008. }
  2009. avio_seek(s->pb, pos, SEEK_SET);
  2010. /* no next PCR found: we use previous increment */
  2011. ts->cur_pcr = pcr_h * 300 + pcr_l;
  2012. }
  2013. pkt->pts = ts->cur_pcr;
  2014. pkt->duration = ts->pcr_incr;
  2015. ts->cur_pcr += ts->pcr_incr;
  2016. }
  2017. pkt->stream_index = 0;
  2018. return 0;
  2019. }
  2020. static int mpegts_read_packet(AVFormatContext *s,
  2021. AVPacket *pkt)
  2022. {
  2023. MpegTSContext *ts = s->priv_data;
  2024. int ret, i;
  2025. pkt->size = -1;
  2026. ts->pkt = pkt;
  2027. ret = handle_packets(ts, 0);
  2028. if (ret < 0) {
  2029. av_free_packet(ts->pkt);
  2030. /* flush pes data left */
  2031. for (i = 0; i < NB_PID_MAX; i++) {
  2032. if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
  2033. PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
  2034. if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
  2035. new_pes_packet(pes, pkt);
  2036. pes->state = MPEGTS_SKIP;
  2037. ret = 0;
  2038. break;
  2039. }
  2040. }
  2041. }
  2042. }
  2043. if (!ret && pkt->size < 0)
  2044. ret = AVERROR(EINTR);
  2045. return ret;
  2046. }
  2047. static void mpegts_free(MpegTSContext *ts)
  2048. {
  2049. int i;
  2050. clear_programs(ts);
  2051. for(i=0;i<NB_PID_MAX;i++)
  2052. if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
  2053. }
  2054. static int mpegts_read_close(AVFormatContext *s)
  2055. {
  2056. MpegTSContext *ts = s->priv_data;
  2057. mpegts_free(ts);
  2058. return 0;
  2059. }
  2060. static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
  2061. int64_t *ppos, int64_t pos_limit)
  2062. {
  2063. MpegTSContext *ts = s->priv_data;
  2064. int64_t pos, timestamp;
  2065. uint8_t buf[TS_PACKET_SIZE];
  2066. int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
  2067. int pos47 = ts->pos47_full % ts->raw_packet_size;
  2068. pos = ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * ts->raw_packet_size + pos47;
  2069. while(pos < pos_limit) {
  2070. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  2071. return AV_NOPTS_VALUE;
  2072. if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
  2073. return AV_NOPTS_VALUE;
  2074. if (buf[0] != 0x47) {
  2075. avio_seek(s->pb, -TS_PACKET_SIZE, SEEK_CUR);
  2076. if (mpegts_resync(s) < 0)
  2077. return AV_NOPTS_VALUE;
  2078. pos = avio_tell(s->pb);
  2079. continue;
  2080. }
  2081. if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
  2082. parse_pcr(&timestamp, &pcr_l, buf) == 0) {
  2083. *ppos = pos;
  2084. return timestamp;
  2085. }
  2086. pos += ts->raw_packet_size;
  2087. }
  2088. return AV_NOPTS_VALUE;
  2089. }
  2090. static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
  2091. int64_t *ppos, int64_t pos_limit)
  2092. {
  2093. MpegTSContext *ts = s->priv_data;
  2094. int64_t pos;
  2095. int pos47 = ts->pos47_full % ts->raw_packet_size;
  2096. pos = ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * ts->raw_packet_size + pos47;
  2097. ff_read_frame_flush(s);
  2098. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  2099. return AV_NOPTS_VALUE;
  2100. while(pos < pos_limit) {
  2101. int ret;
  2102. AVPacket pkt;
  2103. av_init_packet(&pkt);
  2104. ret= av_read_frame(s, &pkt);
  2105. if(ret < 0)
  2106. return AV_NOPTS_VALUE;
  2107. av_free_packet(&pkt);
  2108. if(pkt.dts != AV_NOPTS_VALUE && pkt.pos >= 0){
  2109. ff_reduce_index(s, pkt.stream_index);
  2110. av_add_index_entry(s->streams[pkt.stream_index], pkt.pos, pkt.dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
  2111. if(pkt.stream_index == stream_index && pkt.pos >= *ppos){
  2112. *ppos= pkt.pos;
  2113. return pkt.dts;
  2114. }
  2115. }
  2116. pos = pkt.pos;
  2117. }
  2118. return AV_NOPTS_VALUE;
  2119. }
  2120. /**************************************************************/
  2121. /* parsing functions - called from other demuxers such as RTP */
  2122. MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
  2123. {
  2124. MpegTSContext *ts;
  2125. ts = av_mallocz(sizeof(MpegTSContext));
  2126. if (!ts)
  2127. return NULL;
  2128. /* no stream case, currently used by RTP */
  2129. ts->raw_packet_size = TS_PACKET_SIZE;
  2130. ts->stream = s;
  2131. ts->auto_guess = 1;
  2132. mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
  2133. mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
  2134. return ts;
  2135. }
  2136. /* return the consumed length if a packet was output, or -1 if no
  2137. packet is output */
  2138. int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
  2139. const uint8_t *buf, int len)
  2140. {
  2141. int len1;
  2142. len1 = len;
  2143. ts->pkt = pkt;
  2144. for(;;) {
  2145. ts->stop_parse = 0;
  2146. if (len < TS_PACKET_SIZE)
  2147. return -1;
  2148. if (buf[0] != 0x47) {
  2149. buf++;
  2150. len--;
  2151. } else {
  2152. handle_packet(ts, buf);
  2153. buf += TS_PACKET_SIZE;
  2154. len -= TS_PACKET_SIZE;
  2155. if (ts->stop_parse == 1)
  2156. break;
  2157. }
  2158. }
  2159. return len1 - len;
  2160. }
  2161. void ff_mpegts_parse_close(MpegTSContext *ts)
  2162. {
  2163. mpegts_free(ts);
  2164. av_free(ts);
  2165. }
  2166. AVInputFormat ff_mpegts_demuxer = {
  2167. .name = "mpegts",
  2168. .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
  2169. .priv_data_size = sizeof(MpegTSContext),
  2170. .read_probe = mpegts_probe,
  2171. .read_header = mpegts_read_header,
  2172. .read_packet = mpegts_read_packet,
  2173. .read_close = mpegts_read_close,
  2174. .read_timestamp = mpegts_get_dts,
  2175. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  2176. .priv_class = &mpegts_class,
  2177. };
  2178. AVInputFormat ff_mpegtsraw_demuxer = {
  2179. .name = "mpegtsraw",
  2180. .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
  2181. .priv_data_size = sizeof(MpegTSContext),
  2182. .read_header = mpegts_read_header,
  2183. .read_packet = mpegts_raw_read_packet,
  2184. .read_close = mpegts_read_close,
  2185. .read_timestamp = mpegts_get_dts,
  2186. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  2187. .priv_class = &mpegtsraw_class,
  2188. };