mpeg.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. uint8_t buf[5];
  102. buf[0] = c<0 ? get_byte(pb) : c;
  103. get_buffer(pb, buf+1, 4);
  104. return ff_parse_pes_pts(buf);
  105. }
  106. static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
  107. int32_t *header_state)
  108. {
  109. unsigned int state, v;
  110. int val, n;
  111. state = *header_state;
  112. n = *size_ptr;
  113. while (n > 0) {
  114. if (url_feof(pb))
  115. break;
  116. v = get_byte(pb);
  117. n--;
  118. if (state == 0x000001) {
  119. state = ((state << 8) | v) & 0xffffff;
  120. val = state;
  121. goto found;
  122. }
  123. state = ((state << 8) | v) & 0xffffff;
  124. }
  125. val = -1;
  126. found:
  127. *header_state = state;
  128. *size_ptr = n;
  129. return val;
  130. }
  131. #if 0 /* unused, remove? */
  132. /* XXX: optimize */
  133. static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
  134. {
  135. int64_t pos, pos_start;
  136. int max_size, start_code;
  137. max_size = *size_ptr;
  138. pos_start = url_ftell(pb);
  139. /* in order to go faster, we fill the buffer */
  140. pos = pos_start - 16386;
  141. if (pos < 0)
  142. pos = 0;
  143. url_fseek(pb, pos, SEEK_SET);
  144. get_byte(pb);
  145. pos = pos_start;
  146. for(;;) {
  147. pos--;
  148. if (pos < 0 || (pos_start - pos) >= max_size) {
  149. start_code = -1;
  150. goto the_end;
  151. }
  152. url_fseek(pb, pos, SEEK_SET);
  153. start_code = get_be32(pb);
  154. if ((start_code & 0xffffff00) == 0x100)
  155. break;
  156. }
  157. the_end:
  158. *size_ptr = pos_start - pos;
  159. return start_code;
  160. }
  161. #endif
  162. /**
  163. * Extracts stream types from a program stream map
  164. * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
  165. *
  166. * @return number of bytes occupied by PSM in the bitstream
  167. */
  168. static long mpegps_psm_parse(MpegDemuxContext *m, ByteIOContext *pb)
  169. {
  170. int psm_length, ps_info_length, es_map_length;
  171. psm_length = get_be16(pb);
  172. get_byte(pb);
  173. get_byte(pb);
  174. ps_info_length = get_be16(pb);
  175. /* skip program_stream_info */
  176. url_fskip(pb, ps_info_length);
  177. es_map_length = get_be16(pb);
  178. /* at least one es available? */
  179. while (es_map_length >= 4){
  180. unsigned char type = get_byte(pb);
  181. unsigned char es_id = get_byte(pb);
  182. uint16_t es_info_length = get_be16(pb);
  183. /* remember mapping from stream id to stream type */
  184. m->psm_es_type[es_id] = type;
  185. /* skip program_stream_info */
  186. url_fskip(pb, es_info_length);
  187. es_map_length -= 4 + es_info_length;
  188. }
  189. get_be32(pb); /* crc32 */
  190. return 2 + psm_length;
  191. }
  192. /* read the next PES header. Return its position in ppos
  193. (if not NULL), and its start code, pts and dts.
  194. */
  195. static int mpegps_read_pes_header(AVFormatContext *s,
  196. int64_t *ppos, int *pstart_code,
  197. int64_t *ppts, int64_t *pdts)
  198. {
  199. MpegDemuxContext *m = s->priv_data;
  200. int len, size, startcode, c, flags, header_len;
  201. int pes_ext, ext2_len, id_ext, skip;
  202. int64_t pts, dts;
  203. int64_t last_sync= url_ftell(s->pb);
  204. error_redo:
  205. url_fseek(s->pb, last_sync, SEEK_SET);
  206. redo:
  207. /* next start code (should be immediately after) */
  208. m->header_state = 0xff;
  209. size = MAX_SYNC_SIZE;
  210. startcode = find_next_start_code(s->pb, &size, &m->header_state);
  211. last_sync = url_ftell(s->pb);
  212. //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, url_ftell(s->pb));
  213. if (startcode < 0)
  214. return AVERROR(EIO);
  215. if (startcode == PACK_START_CODE)
  216. goto redo;
  217. if (startcode == SYSTEM_HEADER_START_CODE)
  218. goto redo;
  219. if (startcode == PADDING_STREAM) {
  220. url_fskip(s->pb, get_be16(s->pb));
  221. goto redo;
  222. }
  223. if (startcode == PRIVATE_STREAM_2) {
  224. len = get_be16(s->pb);
  225. if (!m->sofdec) {
  226. while (len-- >= 6) {
  227. if (get_byte(s->pb) == 'S') {
  228. uint8_t buf[5];
  229. get_buffer(s->pb, buf, sizeof(buf));
  230. m->sofdec = !memcmp(buf, "ofdec", 5);
  231. len -= sizeof(buf);
  232. break;
  233. }
  234. }
  235. m->sofdec -= !m->sofdec;
  236. }
  237. url_fskip(s->pb, len);
  238. goto redo;
  239. }
  240. if (startcode == PROGRAM_STREAM_MAP) {
  241. mpegps_psm_parse(m, s->pb);
  242. goto redo;
  243. }
  244. /* find matching stream */
  245. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  246. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  247. (startcode == 0x1bd) || (startcode == 0x1fd)))
  248. goto redo;
  249. if (ppos) {
  250. *ppos = url_ftell(s->pb) - 4;
  251. }
  252. len = get_be16(s->pb);
  253. pts =
  254. dts = AV_NOPTS_VALUE;
  255. /* stuffing */
  256. for(;;) {
  257. if (len < 1)
  258. goto error_redo;
  259. c = get_byte(s->pb);
  260. len--;
  261. /* XXX: for mpeg1, should test only bit 7 */
  262. if (c != 0xff)
  263. break;
  264. }
  265. if ((c & 0xc0) == 0x40) {
  266. /* buffer scale & size */
  267. get_byte(s->pb);
  268. c = get_byte(s->pb);
  269. len -= 2;
  270. }
  271. if ((c & 0xe0) == 0x20) {
  272. dts = pts = get_pts(s->pb, c);
  273. len -= 4;
  274. if (c & 0x10){
  275. dts = get_pts(s->pb, -1);
  276. len -= 5;
  277. }
  278. } else if ((c & 0xc0) == 0x80) {
  279. /* mpeg 2 PES */
  280. #if 0 /* some streams have this field set for no apparent reason */
  281. if ((c & 0x30) != 0) {
  282. /* Encrypted multiplex not handled */
  283. goto redo;
  284. }
  285. #endif
  286. flags = get_byte(s->pb);
  287. header_len = get_byte(s->pb);
  288. len -= 2;
  289. if (header_len > len)
  290. goto error_redo;
  291. len -= header_len;
  292. if (flags & 0x80) {
  293. dts = pts = get_pts(s->pb, -1);
  294. header_len -= 5;
  295. if (flags & 0x40) {
  296. dts = get_pts(s->pb, -1);
  297. header_len -= 5;
  298. }
  299. }
  300. if (flags & 0x3f && header_len == 0){
  301. flags &= 0xC0;
  302. av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
  303. }
  304. if (flags & 0x01) { /* PES extension */
  305. pes_ext = get_byte(s->pb);
  306. header_len--;
  307. if (pes_ext & 0x40) { /* pack header - should be zero in PS */
  308. goto error_redo;
  309. }
  310. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  311. skip = (pes_ext >> 4) & 0xb;
  312. skip += skip & 0x9;
  313. url_fskip(s->pb, skip);
  314. header_len -= skip;
  315. if (pes_ext & 0x01) { /* PES extension 2 */
  316. ext2_len = get_byte(s->pb);
  317. header_len--;
  318. if ((ext2_len & 0x7f) > 0) {
  319. id_ext = get_byte(s->pb);
  320. if ((id_ext & 0x80) == 0)
  321. startcode = ((startcode & 0xff) << 8) | id_ext;
  322. header_len--;
  323. }
  324. }
  325. }
  326. if(header_len < 0)
  327. goto error_redo;
  328. url_fskip(s->pb, header_len);
  329. }
  330. else if( c!= 0xf )
  331. goto redo;
  332. if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
  333. startcode = get_byte(s->pb);
  334. len--;
  335. if (startcode >= 0x80 && startcode <= 0xcf) {
  336. /* audio: skip header */
  337. get_byte(s->pb);
  338. get_byte(s->pb);
  339. get_byte(s->pb);
  340. len -= 3;
  341. if (startcode >= 0xb0 && startcode <= 0xbf) {
  342. /* MLP/TrueHD audio has a 4-byte header */
  343. get_byte(s->pb);
  344. len--;
  345. }
  346. }
  347. }
  348. if(len<0)
  349. goto error_redo;
  350. if(dts != AV_NOPTS_VALUE && ppos){
  351. int i;
  352. for(i=0; i<s->nb_streams; i++){
  353. if(startcode == s->streams[i]->id &&
  354. !url_is_streamed(s->pb) /* index useless on streams anyway */) {
  355. ff_reduce_index(s, i);
  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. };