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.

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