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.

2413 lines
77KB

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