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.

550 lines
16KB

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