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.

577 lines
18KB

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