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.

578 lines
18KB

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