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.

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