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