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.

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