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.

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