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.

2377 lines
76KB

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