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.

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