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.

575 lines
18KB

  1. /*
  2. * MPEG-1/2 demuxer
  3. * Copyright (c) 2000, 2001, 2002 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 "avformat.h"
  22. #include "internal.h"
  23. #include "mpeg.h"
  24. /*********************************************/
  25. /* demux code */
  26. #define MAX_SYNC_SIZE 100000
  27. static int check_pes(uint8_t *p, uint8_t *end)
  28. {
  29. int pes1;
  30. int pes2 = (p[3] & 0xC0) == 0x80 &&
  31. (p[4] & 0xC0) != 0x40 &&
  32. ((p[4] & 0xC0) == 0x00 ||
  33. (p[4] & 0xC0) >> 2 == (p[6] & 0xF0));
  34. for (p += 3; p < end && *p == 0xFF; p++) ;
  35. if ((*p & 0xC0) == 0x40)
  36. p += 2;
  37. if ((*p & 0xF0) == 0x20)
  38. pes1 = p[0] & p[2] & p[4] & 1;
  39. else if ((*p & 0xF0) == 0x30)
  40. pes1 = p[0] & p[2] & p[4] & p[5] & p[7] & p[9] & 1;
  41. else
  42. pes1 = *p == 0x0F;
  43. return pes1 || pes2;
  44. }
  45. static int check_pack_header(const uint8_t *buf)
  46. {
  47. return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
  48. }
  49. static int mpegps_probe(AVProbeData *p)
  50. {
  51. uint32_t code = -1;
  52. int i;
  53. int sys = 0, pspack = 0, priv1 = 0, vid = 0;
  54. int audio = 0, invalid = 0, score = 0;
  55. for (i = 0; i < p->buf_size; i++) {
  56. code = (code << 8) + p->buf[i];
  57. if ((code & 0xffffff00) == 0x100) {
  58. int len = p->buf[i + 1] << 8 | p->buf[i + 2];
  59. int pes = check_pes(p->buf + i, p->buf + p->buf_size);
  60. int pack = check_pack_header(p->buf + i);
  61. if (code == SYSTEM_HEADER_START_CODE)
  62. sys++;
  63. else if (code == PACK_START_CODE && pack)
  64. pspack++;
  65. else if ((code & 0xf0) == VIDEO_ID && pes)
  66. vid++;
  67. // skip pes payload to avoid start code emulation for private
  68. // and audio streams
  69. else if ((code & 0xe0) == AUDIO_ID && pes) {
  70. audio++;
  71. i += len;
  72. } else if (code == PRIVATE_STREAM_1 && pes) {
  73. priv1++;
  74. i += len;
  75. } else if ((code & 0xf0) == VIDEO_ID && !pes)
  76. invalid++;
  77. else if ((code & 0xe0) == AUDIO_ID && !pes)
  78. invalid++;
  79. else if (code == PRIVATE_STREAM_1 && !pes)
  80. invalid++;
  81. }
  82. }
  83. if (vid + audio > invalid) /* invalid VDR files nd short PES streams */
  84. score = AVPROBE_SCORE_EXTENSION / 2;
  85. if (sys > invalid && sys * 9 <= pspack * 10)
  86. return pspack > 2 ? AVPROBE_SCORE_EXTENSION + 2
  87. : AVPROBE_SCORE_EXTENSION / 2; // 1 more than .mpg
  88. if (pspack > invalid && (priv1 + vid + audio) * 10 >= pspack * 9)
  89. return pspack > 2 ? AVPROBE_SCORE_EXTENSION + 2
  90. : AVPROBE_SCORE_EXTENSION / 2; // 1 more than .mpg
  91. if ((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys &&
  92. !pspack && p->buf_size > 2048 && vid + audio > invalid) /* PES stream */
  93. return (audio > 12 || vid > 3) ? AVPROBE_SCORE_EXTENSION + 2
  94. : AVPROBE_SCORE_EXTENSION / 2;
  95. // 02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
  96. // mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
  97. return score;
  98. }
  99. typedef struct MpegDemuxContext {
  100. int32_t header_state;
  101. unsigned char psm_es_type[256];
  102. int sofdec;
  103. } MpegDemuxContext;
  104. static int mpegps_read_header(AVFormatContext *s)
  105. {
  106. MpegDemuxContext *m = s->priv_data;
  107. const char *sofdec = "Sofdec";
  108. int v, i = 0;
  109. m->header_state = 0xff;
  110. s->ctx_flags |= AVFMTCTX_NOHEADER;
  111. m->sofdec = -1;
  112. do {
  113. v = avio_r8(s->pb);
  114. m->header_state = m->header_state << 8 | v;
  115. m->sofdec++;
  116. } while (v == sofdec[i] && i++ < 6);
  117. m->sofdec = (m->sofdec == 6) ? 1 : 0;
  118. /* no need to do more */
  119. return 0;
  120. }
  121. static int64_t get_pts(AVIOContext *pb, int c)
  122. {
  123. uint8_t buf[5];
  124. buf[0] = c < 0 ? avio_r8(pb) : c;
  125. avio_read(pb, buf + 1, 4);
  126. return ff_parse_pes_pts(buf);
  127. }
  128. static int find_next_start_code(AVIOContext *pb, int *size_ptr,
  129. int32_t *header_state)
  130. {
  131. unsigned int state, v;
  132. int val, n;
  133. state = *header_state;
  134. n = *size_ptr;
  135. while (n > 0) {
  136. if (pb->eof_reached)
  137. break;
  138. v = avio_r8(pb);
  139. n--;
  140. if (state == 0x000001) {
  141. state = ((state << 8) | v) & 0xffffff;
  142. val = state;
  143. goto found;
  144. }
  145. state = ((state << 8) | v) & 0xffffff;
  146. }
  147. val = -1;
  148. found:
  149. *header_state = state;
  150. *size_ptr = n;
  151. return val;
  152. }
  153. /**
  154. * Extract stream types from a program stream map
  155. * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
  156. *
  157. * @return number of bytes occupied by PSM in the bitstream
  158. */
  159. static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
  160. {
  161. int psm_length, ps_info_length, es_map_length;
  162. psm_length = avio_rb16(pb);
  163. avio_r8(pb);
  164. avio_r8(pb);
  165. ps_info_length = avio_rb16(pb);
  166. /* skip program_stream_info */
  167. avio_skip(pb, ps_info_length);
  168. es_map_length = avio_rb16(pb);
  169. /* at least one es available? */
  170. while (es_map_length >= 4) {
  171. unsigned char type = avio_r8(pb);
  172. unsigned char es_id = avio_r8(pb);
  173. uint16_t es_info_length = avio_rb16(pb);
  174. /* remember mapping from stream id to stream type */
  175. m->psm_es_type[es_id] = type;
  176. /* skip program_stream_info */
  177. avio_skip(pb, es_info_length);
  178. es_map_length -= 4 + es_info_length;
  179. }
  180. avio_rb32(pb); /* crc32 */
  181. return 2 + psm_length;
  182. }
  183. /* read the next PES header. Return its position in ppos
  184. * (if not NULL), and its start code, pts and dts.
  185. */
  186. static int mpegps_read_pes_header(AVFormatContext *s,
  187. int64_t *ppos, int *pstart_code,
  188. int64_t *ppts, int64_t *pdts)
  189. {
  190. MpegDemuxContext *m = s->priv_data;
  191. int len, size, startcode, c, flags, header_len;
  192. int pes_ext, ext2_len, id_ext, skip;
  193. int64_t pts, dts;
  194. int64_t last_sync = avio_tell(s->pb);
  195. error_redo:
  196. avio_seek(s->pb, last_sync, SEEK_SET);
  197. redo:
  198. /* next start code (should be immediately after) */
  199. m->header_state = 0xff;
  200. size = MAX_SYNC_SIZE;
  201. startcode = find_next_start_code(s->pb, &size, &m->header_state);
  202. last_sync = avio_tell(s->pb);
  203. if (startcode < 0) {
  204. if (s->pb->eof_reached)
  205. return AVERROR_EOF;
  206. // FIXME we should remember header_state
  207. return AVERROR(EAGAIN);
  208. }
  209. if (startcode == PACK_START_CODE)
  210. goto redo;
  211. if (startcode == SYSTEM_HEADER_START_CODE)
  212. goto redo;
  213. if (startcode == PADDING_STREAM) {
  214. avio_skip(s->pb, avio_rb16(s->pb));
  215. goto redo;
  216. }
  217. if (startcode == PRIVATE_STREAM_2) {
  218. len = avio_rb16(s->pb);
  219. if (!m->sofdec) {
  220. while (len-- >= 6) {
  221. if (avio_r8(s->pb) == 'S') {
  222. uint8_t buf[5];
  223. avio_read(s->pb, buf, sizeof(buf));
  224. m->sofdec = !memcmp(buf, "ofdec", 5);
  225. len -= sizeof(buf);
  226. break;
  227. }
  228. }
  229. m->sofdec -= !m->sofdec;
  230. }
  231. avio_skip(s->pb, len);
  232. goto redo;
  233. }
  234. if (startcode == PROGRAM_STREAM_MAP) {
  235. mpegps_psm_parse(m, s->pb);
  236. goto redo;
  237. }
  238. /* find matching stream */
  239. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  240. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  241. (startcode == 0x1bd) || (startcode == 0x1fd)))
  242. goto redo;
  243. if (ppos) {
  244. *ppos = avio_tell(s->pb) - 4;
  245. }
  246. len = avio_rb16(s->pb);
  247. pts =
  248. dts = AV_NOPTS_VALUE;
  249. /* stuffing */
  250. for (;;) {
  251. if (len < 1)
  252. goto error_redo;
  253. c = avio_r8(s->pb);
  254. len--;
  255. /* XXX: for MPEG-1, should test only bit 7 */
  256. if (c != 0xff)
  257. break;
  258. }
  259. if ((c & 0xc0) == 0x40) {
  260. /* buffer scale & size */
  261. avio_r8(s->pb);
  262. c = avio_r8(s->pb);
  263. len -= 2;
  264. }
  265. if ((c & 0xe0) == 0x20) {
  266. dts =
  267. pts = get_pts(s->pb, c);
  268. len -= 4;
  269. if (c & 0x10) {
  270. dts = get_pts(s->pb, -1);
  271. len -= 5;
  272. }
  273. } else if ((c & 0xc0) == 0x80) {
  274. /* mpeg 2 PES */
  275. flags = avio_r8(s->pb);
  276. header_len = avio_r8(s->pb);
  277. len -= 2;
  278. if (header_len > len)
  279. goto error_redo;
  280. len -= header_len;
  281. if (flags & 0x80) {
  282. dts = pts = get_pts(s->pb, -1);
  283. header_len -= 5;
  284. if (flags & 0x40) {
  285. dts = get_pts(s->pb, -1);
  286. header_len -= 5;
  287. }
  288. }
  289. if (flags & 0x3f && header_len == 0) {
  290. flags &= 0xC0;
  291. av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
  292. }
  293. if (flags & 0x01) { /* PES extension */
  294. pes_ext = avio_r8(s->pb);
  295. header_len--;
  296. /* Skip PES private data, program packet sequence counter
  297. * and P-STD buffer */
  298. skip = (pes_ext >> 4) & 0xb;
  299. skip += skip & 0x9;
  300. if (pes_ext & 0x40 || skip > header_len) {
  301. av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
  302. pes_ext = skip = 0;
  303. }
  304. avio_skip(s->pb, skip);
  305. header_len -= skip;
  306. if (pes_ext & 0x01) { /* PES extension 2 */
  307. ext2_len = avio_r8(s->pb);
  308. header_len--;
  309. if ((ext2_len & 0x7f) > 0) {
  310. id_ext = avio_r8(s->pb);
  311. if ((id_ext & 0x80) == 0)
  312. startcode = ((startcode & 0xff) << 8) | id_ext;
  313. header_len--;
  314. }
  315. }
  316. }
  317. if (header_len < 0)
  318. goto error_redo;
  319. avio_skip(s->pb, header_len);
  320. } else if (c != 0xf)
  321. goto redo;
  322. if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
  323. startcode = avio_r8(s->pb);
  324. len--;
  325. if (startcode >= 0x80 && startcode <= 0xcf) {
  326. /* audio: skip header */
  327. avio_r8(s->pb);
  328. avio_r8(s->pb);
  329. avio_r8(s->pb);
  330. len -= 3;
  331. if (startcode >= 0xb0 && startcode <= 0xbf) {
  332. /* MLP/TrueHD audio has a 4-byte header */
  333. avio_r8(s->pb);
  334. len--;
  335. }
  336. }
  337. }
  338. if (len < 0)
  339. goto error_redo;
  340. if (dts != AV_NOPTS_VALUE && ppos) {
  341. int i;
  342. for (i = 0; i < s->nb_streams; i++) {
  343. if (startcode == s->streams[i]->id &&
  344. (s->pb->seekable & AVIO_SEEKABLE_NORMAL) /* index useless on streams anyway */) {
  345. ff_reduce_index(s, i);
  346. av_add_index_entry(s->streams[i], *ppos, dts, 0, 0,
  347. AVINDEX_KEYFRAME /* FIXME keyframe? */);
  348. }
  349. }
  350. }
  351. *pstart_code = startcode;
  352. *ppts = pts;
  353. *pdts = dts;
  354. return len;
  355. }
  356. static int mpegps_read_packet(AVFormatContext *s,
  357. AVPacket *pkt)
  358. {
  359. MpegDemuxContext *m = s->priv_data;
  360. AVStream *st;
  361. int len, startcode, i, es_type, ret;
  362. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  363. enum AVMediaType type;
  364. int64_t pts, dts, dummy_pos; // dummy_pos is needed for the index building to work
  365. uint8_t av_uninit(dvdaudio_substream_type);
  366. redo:
  367. len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
  368. if (len < 0)
  369. return len;
  370. if (startcode == 0x1bd) {
  371. dvdaudio_substream_type = avio_r8(s->pb);
  372. avio_skip(s->pb, 3);
  373. len -= 4;
  374. }
  375. /* now find stream */
  376. for (i = 0; i < s->nb_streams; i++) {
  377. st = s->streams[i];
  378. if (st->id == startcode)
  379. goto found;
  380. }
  381. es_type = m->psm_es_type[startcode & 0xff];
  382. if (es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA) {
  383. if (es_type == STREAM_TYPE_VIDEO_MPEG1) {
  384. codec_id = AV_CODEC_ID_MPEG2VIDEO;
  385. type = AVMEDIA_TYPE_VIDEO;
  386. } else if (es_type == STREAM_TYPE_VIDEO_MPEG2) {
  387. codec_id = AV_CODEC_ID_MPEG2VIDEO;
  388. type = AVMEDIA_TYPE_VIDEO;
  389. } else if (es_type == STREAM_TYPE_AUDIO_MPEG1 ||
  390. es_type == STREAM_TYPE_AUDIO_MPEG2) {
  391. codec_id = AV_CODEC_ID_MP3;
  392. type = AVMEDIA_TYPE_AUDIO;
  393. } else if (es_type == STREAM_TYPE_AUDIO_AAC) {
  394. codec_id = AV_CODEC_ID_AAC;
  395. type = AVMEDIA_TYPE_AUDIO;
  396. } else if (es_type == STREAM_TYPE_VIDEO_MPEG4) {
  397. codec_id = AV_CODEC_ID_MPEG4;
  398. type = AVMEDIA_TYPE_VIDEO;
  399. } else if (es_type == STREAM_TYPE_VIDEO_H264) {
  400. codec_id = AV_CODEC_ID_H264;
  401. type = AVMEDIA_TYPE_VIDEO;
  402. } else if (es_type == STREAM_TYPE_AUDIO_AC3) {
  403. codec_id = AV_CODEC_ID_AC3;
  404. type = AVMEDIA_TYPE_AUDIO;
  405. } else {
  406. goto skip;
  407. }
  408. } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
  409. static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
  410. unsigned char buf[8];
  411. avio_read(s->pb, buf, 8);
  412. avio_seek(s->pb, -8, SEEK_CUR);
  413. if (!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
  414. codec_id = AV_CODEC_ID_CAVS;
  415. else
  416. codec_id = AV_CODEC_ID_PROBE;
  417. type = AVMEDIA_TYPE_VIDEO;
  418. } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
  419. type = AVMEDIA_TYPE_AUDIO;
  420. codec_id = m->sofdec > 0 ? AV_CODEC_ID_ADPCM_ADX : AV_CODEC_ID_MP2;
  421. } else if (startcode >= 0x80 && startcode <= 0x87) {
  422. type = AVMEDIA_TYPE_AUDIO;
  423. codec_id = AV_CODEC_ID_AC3;
  424. } else if ((startcode >= 0x88 && startcode <= 0x8f) ||
  425. (startcode >= 0x98 && startcode <= 0x9f)) {
  426. /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
  427. type = AVMEDIA_TYPE_AUDIO;
  428. codec_id = AV_CODEC_ID_DTS;
  429. } else if (startcode >= 0xa0 && startcode <= 0xaf) {
  430. type = AVMEDIA_TYPE_AUDIO;
  431. codec_id = AV_CODEC_ID_PCM_DVD;
  432. } else if (startcode >= 0xb0 && startcode <= 0xbf) {
  433. type = AVMEDIA_TYPE_AUDIO;
  434. codec_id = AV_CODEC_ID_TRUEHD;
  435. } else if (startcode >= 0xc0 && startcode <= 0xcf) {
  436. /* Used for both AC-3 and E-AC-3 in EVOB files */
  437. type = AVMEDIA_TYPE_AUDIO;
  438. codec_id = AV_CODEC_ID_AC3;
  439. } else if (startcode >= 0x20 && startcode <= 0x3f) {
  440. type = AVMEDIA_TYPE_SUBTITLE;
  441. codec_id = AV_CODEC_ID_DVD_SUBTITLE;
  442. } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
  443. type = AVMEDIA_TYPE_VIDEO;
  444. codec_id = AV_CODEC_ID_VC1;
  445. } else if (startcode == 0x1bd) {
  446. // check dvd audio substream type
  447. type = AVMEDIA_TYPE_AUDIO;
  448. switch (dvdaudio_substream_type & 0xe0) {
  449. case 0xa0:
  450. codec_id = AV_CODEC_ID_PCM_DVD;
  451. break;
  452. case 0x80:
  453. if ((dvdaudio_substream_type & 0xf8) == 0x88)
  454. codec_id = AV_CODEC_ID_DTS;
  455. else
  456. codec_id = AV_CODEC_ID_AC3;
  457. break;
  458. default:
  459. av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n");
  460. goto skip;
  461. }
  462. } else {
  463. skip:
  464. /* skip packet */
  465. avio_skip(s->pb, len);
  466. goto redo;
  467. }
  468. /* no stream found: add a new stream */
  469. st = avformat_new_stream(s, NULL);
  470. if (!st)
  471. goto skip;
  472. st->id = startcode;
  473. st->codecpar->codec_type = type;
  474. st->codecpar->codec_id = codec_id;
  475. st->need_parsing = AVSTREAM_PARSE_FULL;
  476. found:
  477. if (st->discard >= AVDISCARD_ALL)
  478. goto skip;
  479. ret = av_get_packet(s->pb, pkt, len);
  480. pkt->pts = pts;
  481. pkt->dts = dts;
  482. pkt->pos = dummy_pos;
  483. pkt->stream_index = st->index;
  484. av_log(s, AV_LOG_TRACE, "%d: pts=%0.3f dts=%0.3f size=%d\n",
  485. pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
  486. pkt->size);
  487. return (ret < 0) ? ret : 0;
  488. }
  489. static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
  490. int64_t *ppos, int64_t pos_limit)
  491. {
  492. int len, startcode;
  493. int64_t pos, pts, dts;
  494. pos = *ppos;
  495. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  496. return AV_NOPTS_VALUE;
  497. for (;;) {
  498. len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
  499. if (len < 0) {
  500. av_log(s, AV_LOG_TRACE, "none (ret=%d)\n", len);
  501. return AV_NOPTS_VALUE;
  502. }
  503. if (startcode == s->streams[stream_index]->id &&
  504. dts != AV_NOPTS_VALUE) {
  505. break;
  506. }
  507. avio_skip(s->pb, len);
  508. }
  509. av_log(s, AV_LOG_TRACE, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
  510. pos, dts, dts / 90000.0);
  511. *ppos = pos;
  512. return dts;
  513. }
  514. AVInputFormat ff_mpegps_demuxer = {
  515. .name = "mpeg",
  516. .long_name = NULL_IF_CONFIG_SMALL("MPEG-PS (MPEG-2 Program Stream)"),
  517. .priv_data_size = sizeof(MpegDemuxContext),
  518. .read_probe = mpegps_probe,
  519. .read_header = mpegps_read_header,
  520. .read_packet = mpegps_read_packet,
  521. .read_timestamp = mpegps_read_dts,
  522. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  523. };