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.

597 lines
17KB

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