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.

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