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.

550 lines
17KB

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