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.

913 lines
29KB

  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 "internal.h"
  23. #include "mpeg.h"
  24. #if CONFIG_VOBSUB_DEMUXER
  25. # include "subtitles.h"
  26. # include "libavutil/bprint.h"
  27. #endif
  28. #undef NDEBUG
  29. #include <assert.h>
  30. #include "libavutil/avassert.h"
  31. /*********************************************/
  32. /* demux code */
  33. #define MAX_SYNC_SIZE 100000
  34. static int check_pes(const uint8_t *p, const uint8_t *end){
  35. int pes1;
  36. int pes2= (p[3] & 0xC0) == 0x80
  37. && (p[4] & 0xC0) != 0x40
  38. &&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
  39. for(p+=3; p<end && *p == 0xFF; p++);
  40. if((*p&0xC0) == 0x40) p+=2;
  41. if((*p&0xF0) == 0x20){
  42. pes1= p[0]&p[2]&p[4]&1;
  43. }else if((*p&0xF0) == 0x30){
  44. pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
  45. }else
  46. pes1 = *p == 0x0F;
  47. return pes1||pes2;
  48. }
  49. static int check_pack_header(const uint8_t *buf) {
  50. return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
  51. }
  52. static int mpegps_probe(AVProbeData *p)
  53. {
  54. uint32_t code= -1;
  55. int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
  56. int i;
  57. int score=0;
  58. for(i=0; i<p->buf_size; i++){
  59. code = (code<<8) + p->buf[i];
  60. if ((code & 0xffffff00) == 0x100) {
  61. int len= p->buf[i+1] << 8 | p->buf[i+2];
  62. int pes= check_pes(p->buf+i, p->buf+p->buf_size);
  63. int pack = check_pack_header(p->buf+i);
  64. if(code == SYSTEM_HEADER_START_CODE) sys++;
  65. else if(code == PACK_START_CODE && pack) pspack++;
  66. else if((code & 0xf0) == VIDEO_ID && pes) vid++;
  67. // skip pes payload to avoid start code emulation for private
  68. // and audio streams
  69. else if((code & 0xe0) == AUDIO_ID && pes) {audio++; i+=len;}
  70. else if(code == PRIVATE_STREAM_1 && pes) {priv1++; i+=len;}
  71. else if(code == 0x1fd && pes) vid++; //VC1
  72. else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
  73. else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
  74. else if(code == PRIVATE_STREAM_1 && !pes) invalid++;
  75. }
  76. }
  77. if(vid+audio > invalid+1) /* invalid VDR files nd short PES streams */
  78. score= AVPROBE_SCORE_MAX/4;
  79. if(sys>invalid && sys*9 <= pspack*10)
  80. return (audio > 12 || vid > 3 || pspack > 2) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
  81. if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
  82. return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
  83. if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */
  84. return (audio > 12 || vid > 3 + 2*invalid) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
  85. //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
  86. //mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
  87. //Have\ Yourself\ a\ Merry\ Little\ Christmas.mp3 0 0 0 5 0 1 len:21618
  88. return score;
  89. }
  90. typedef struct MpegDemuxContext {
  91. int32_t header_state;
  92. unsigned char psm_es_type[256];
  93. int sofdec;
  94. int dvd;
  95. #if CONFIG_VOBSUB_DEMUXER
  96. AVFormatContext *sub_ctx;
  97. FFDemuxSubtitlesQueue q;
  98. #endif
  99. } MpegDemuxContext;
  100. static int mpegps_read_header(AVFormatContext *s)
  101. {
  102. MpegDemuxContext *m = s->priv_data;
  103. const char *sofdec = "Sofdec";
  104. int v, i = 0;
  105. int64_t last_pos = avio_tell(s->pb);
  106. m->header_state = 0xff;
  107. s->ctx_flags |= AVFMTCTX_NOHEADER;
  108. m->sofdec = -1;
  109. do {
  110. v = avio_r8(s->pb);
  111. m->sofdec++;
  112. } while (v == sofdec[i] && i++ < 6);
  113. m->sofdec = (m->sofdec == 6) ? 1 : 0;
  114. if (!m->sofdec)
  115. avio_seek(s->pb, last_pos, SEEK_SET);
  116. /* no need to do more */
  117. return 0;
  118. }
  119. static int64_t get_pts(AVIOContext *pb, int c)
  120. {
  121. uint8_t buf[5];
  122. buf[0] = c<0 ? avio_r8(pb) : c;
  123. avio_read(pb, buf+1, 4);
  124. return ff_parse_pes_pts(buf);
  125. }
  126. static int find_next_start_code(AVIOContext *pb, int *size_ptr,
  127. int32_t *header_state)
  128. {
  129. unsigned int state, v;
  130. int val, n;
  131. state = *header_state;
  132. n = *size_ptr;
  133. while (n > 0) {
  134. if (url_feof(pb))
  135. break;
  136. v = avio_r8(pb);
  137. n--;
  138. if (state == 0x000001) {
  139. state = ((state << 8) | v) & 0xffffff;
  140. val = state;
  141. goto found;
  142. }
  143. state = ((state << 8) | v) & 0xffffff;
  144. }
  145. val = -1;
  146. found:
  147. *header_state = state;
  148. *size_ptr = n;
  149. return val;
  150. }
  151. /**
  152. * Extract stream types from a program stream map
  153. * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
  154. *
  155. * @return number of bytes occupied by PSM in the bitstream
  156. */
  157. static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
  158. {
  159. int psm_length, ps_info_length, es_map_length;
  160. psm_length = avio_rb16(pb);
  161. avio_r8(pb);
  162. avio_r8(pb);
  163. ps_info_length = avio_rb16(pb);
  164. /* skip program_stream_info */
  165. avio_skip(pb, ps_info_length);
  166. es_map_length = avio_rb16(pb);
  167. /* at least one es available? */
  168. while (es_map_length >= 4){
  169. unsigned char type = avio_r8(pb);
  170. unsigned char es_id = avio_r8(pb);
  171. uint16_t es_info_length = avio_rb16(pb);
  172. /* remember mapping from stream id to stream type */
  173. m->psm_es_type[es_id] = type;
  174. /* skip program_stream_info */
  175. avio_skip(pb, es_info_length);
  176. es_map_length -= 4 + es_info_length;
  177. }
  178. avio_rb32(pb); /* crc32 */
  179. return 2 + psm_length;
  180. }
  181. /* read the next PES header. Return its position in ppos
  182. (if not NULL), and its start code, pts and dts.
  183. */
  184. static int mpegps_read_pes_header(AVFormatContext *s,
  185. int64_t *ppos, int *pstart_code,
  186. int64_t *ppts, int64_t *pdts)
  187. {
  188. MpegDemuxContext *m = s->priv_data;
  189. int len, size, startcode, c, flags, header_len;
  190. int pes_ext, ext2_len, id_ext, skip;
  191. int64_t pts, dts;
  192. int64_t last_sync= avio_tell(s->pb);
  193. error_redo:
  194. avio_seek(s->pb, last_sync, SEEK_SET);
  195. redo:
  196. /* next start code (should be immediately after) */
  197. m->header_state = 0xff;
  198. size = MAX_SYNC_SIZE;
  199. startcode = find_next_start_code(s->pb, &size, &m->header_state);
  200. last_sync = avio_tell(s->pb);
  201. if (startcode < 0){
  202. if(url_feof(s->pb))
  203. return AVERROR_EOF;
  204. //FIXME we should remember header_state
  205. return AVERROR(EAGAIN);
  206. }
  207. if (startcode == PACK_START_CODE)
  208. goto redo;
  209. if (startcode == SYSTEM_HEADER_START_CODE)
  210. goto redo;
  211. if (startcode == PADDING_STREAM) {
  212. avio_skip(s->pb, avio_rb16(s->pb));
  213. goto redo;
  214. }
  215. if (startcode == PRIVATE_STREAM_2) {
  216. if (!m->sofdec) {
  217. /* Need to detect whether this from a DVD or a 'Sofdec' stream */
  218. int len = avio_rb16(s->pb);
  219. int bytesread = 0;
  220. uint8_t *ps2buf = av_malloc(len);
  221. if (ps2buf) {
  222. bytesread = avio_read(s->pb, ps2buf, len);
  223. if (bytesread != len) {
  224. avio_skip(s->pb, len - bytesread);
  225. } else {
  226. uint8_t *p = 0;
  227. if (len >= 6)
  228. p = memchr(ps2buf, 'S', len - 5);
  229. if (p)
  230. m->sofdec = !memcmp(p+1, "ofdec", 5);
  231. m->sofdec -= !m->sofdec;
  232. if (m->sofdec < 0) {
  233. if (len == 980 && ps2buf[0] == 0) {
  234. /* PCI structure? */
  235. uint32_t startpts = AV_RB32(ps2buf + 0x0d);
  236. uint32_t endpts = AV_RB32(ps2buf + 0x11);
  237. uint8_t hours = ((ps2buf[0x19] >> 4) * 10) + (ps2buf[0x19] & 0x0f);
  238. uint8_t mins = ((ps2buf[0x1a] >> 4) * 10) + (ps2buf[0x1a] & 0x0f);
  239. uint8_t secs = ((ps2buf[0x1b] >> 4) * 10) + (ps2buf[0x1b] & 0x0f);
  240. m->dvd = (hours <= 23 &&
  241. mins <= 59 &&
  242. secs <= 59 &&
  243. (ps2buf[0x19] & 0x0f) < 10 &&
  244. (ps2buf[0x1a] & 0x0f) < 10 &&
  245. (ps2buf[0x1b] & 0x0f) < 10 &&
  246. endpts >= startpts);
  247. } else if (len == 1018 && ps2buf[0] == 1) {
  248. /* DSI structure? */
  249. uint8_t hours = ((ps2buf[0x1d] >> 4) * 10) + (ps2buf[0x1d] & 0x0f);
  250. uint8_t mins = ((ps2buf[0x1e] >> 4) * 10) + (ps2buf[0x1e] & 0x0f);
  251. uint8_t secs = ((ps2buf[0x1f] >> 4) * 10) + (ps2buf[0x1f] & 0x0f);
  252. m->dvd = (hours <= 23 &&
  253. mins <= 59 &&
  254. secs <= 59 &&
  255. (ps2buf[0x1d] & 0x0f) < 10 &&
  256. (ps2buf[0x1e] & 0x0f) < 10 &&
  257. (ps2buf[0x1f] & 0x0f) < 10);
  258. }
  259. }
  260. }
  261. av_free(ps2buf);
  262. /* If this isn't a DVD packet or no memory
  263. * could be allocated, just ignore it.
  264. * If we did, move back to the start of the
  265. * packet (plus 'length' field) */
  266. if (!m->dvd || avio_skip(s->pb, -(len + 2)) < 0) {
  267. /* Skip back failed.
  268. * This packet will be lost but that can't be helped
  269. * if we can't skip back
  270. */
  271. goto redo;
  272. }
  273. } else {
  274. /* No memory */
  275. avio_skip(s->pb, len);
  276. goto redo;
  277. }
  278. } else if (!m->dvd) {
  279. int len = avio_rb16(s->pb);
  280. avio_skip(s->pb, len);
  281. goto redo;
  282. }
  283. }
  284. if (startcode == PROGRAM_STREAM_MAP) {
  285. mpegps_psm_parse(m, s->pb);
  286. goto redo;
  287. }
  288. /* find matching stream */
  289. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  290. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  291. (startcode == 0x1bd) ||
  292. (startcode == PRIVATE_STREAM_2) ||
  293. (startcode == 0x1fd)))
  294. goto redo;
  295. if (ppos) {
  296. *ppos = avio_tell(s->pb) - 4;
  297. }
  298. len = avio_rb16(s->pb);
  299. pts =
  300. dts = AV_NOPTS_VALUE;
  301. if (startcode != PRIVATE_STREAM_2)
  302. {
  303. /* stuffing */
  304. for(;;) {
  305. if (len < 1)
  306. goto error_redo;
  307. c = avio_r8(s->pb);
  308. len--;
  309. /* XXX: for mpeg1, should test only bit 7 */
  310. if (c != 0xff)
  311. break;
  312. }
  313. if ((c & 0xc0) == 0x40) {
  314. /* buffer scale & size */
  315. avio_r8(s->pb);
  316. c = avio_r8(s->pb);
  317. len -= 2;
  318. }
  319. if ((c & 0xe0) == 0x20) {
  320. dts = pts = get_pts(s->pb, c);
  321. len -= 4;
  322. if (c & 0x10){
  323. dts = get_pts(s->pb, -1);
  324. len -= 5;
  325. }
  326. } else if ((c & 0xc0) == 0x80) {
  327. /* mpeg 2 PES */
  328. flags = avio_r8(s->pb);
  329. header_len = avio_r8(s->pb);
  330. len -= 2;
  331. if (header_len > len)
  332. goto error_redo;
  333. len -= header_len;
  334. if (flags & 0x80) {
  335. dts = pts = get_pts(s->pb, -1);
  336. header_len -= 5;
  337. if (flags & 0x40) {
  338. dts = get_pts(s->pb, -1);
  339. header_len -= 5;
  340. }
  341. }
  342. if (flags & 0x3f && header_len == 0){
  343. flags &= 0xC0;
  344. av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
  345. }
  346. if (flags & 0x01) { /* PES extension */
  347. pes_ext = avio_r8(s->pb);
  348. header_len--;
  349. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  350. skip = (pes_ext >> 4) & 0xb;
  351. skip += skip & 0x9;
  352. if (pes_ext & 0x40 || skip > header_len){
  353. av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
  354. pes_ext=skip=0;
  355. }
  356. avio_skip(s->pb, skip);
  357. header_len -= skip;
  358. if (pes_ext & 0x01) { /* PES extension 2 */
  359. ext2_len = avio_r8(s->pb);
  360. header_len--;
  361. if ((ext2_len & 0x7f) > 0) {
  362. id_ext = avio_r8(s->pb);
  363. if ((id_ext & 0x80) == 0)
  364. startcode = ((startcode & 0xff) << 8) | id_ext;
  365. header_len--;
  366. }
  367. }
  368. }
  369. if(header_len < 0)
  370. goto error_redo;
  371. avio_skip(s->pb, header_len);
  372. }
  373. else if( c!= 0xf )
  374. goto redo;
  375. }
  376. if (startcode == PRIVATE_STREAM_1) {
  377. startcode = avio_r8(s->pb);
  378. len--;
  379. }
  380. if(len<0)
  381. goto error_redo;
  382. if(dts != AV_NOPTS_VALUE && ppos){
  383. int i;
  384. for(i=0; i<s->nb_streams; i++){
  385. if(startcode == s->streams[i]->id &&
  386. s->pb->seekable /* index useless on streams anyway */) {
  387. ff_reduce_index(s, i);
  388. av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
  389. }
  390. }
  391. }
  392. *pstart_code = startcode;
  393. *ppts = pts;
  394. *pdts = dts;
  395. return len;
  396. }
  397. static int mpegps_read_packet(AVFormatContext *s,
  398. AVPacket *pkt)
  399. {
  400. MpegDemuxContext *m = s->priv_data;
  401. AVStream *st;
  402. int len, startcode, i, es_type, ret;
  403. int lpcm_header_len = -1; //Init to supress warning
  404. int request_probe= 0;
  405. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  406. enum AVMediaType type;
  407. int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
  408. redo:
  409. len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
  410. if (len < 0)
  411. return len;
  412. if (startcode >= 0x80 && startcode <= 0xcf) {
  413. if(len < 4)
  414. goto skip;
  415. /* audio: skip header */
  416. avio_r8(s->pb);
  417. lpcm_header_len = avio_rb16(s->pb);
  418. len -= 3;
  419. if (startcode >= 0xb0 && startcode <= 0xbf) {
  420. /* MLP/TrueHD audio has a 4-byte header */
  421. avio_r8(s->pb);
  422. len--;
  423. }
  424. }
  425. /* now find stream */
  426. for(i=0;i<s->nb_streams;i++) {
  427. st = s->streams[i];
  428. if (st->id == startcode)
  429. goto found;
  430. }
  431. es_type = m->psm_es_type[startcode & 0xff];
  432. if(es_type == STREAM_TYPE_VIDEO_MPEG1){
  433. codec_id = AV_CODEC_ID_MPEG2VIDEO;
  434. type = AVMEDIA_TYPE_VIDEO;
  435. } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
  436. codec_id = AV_CODEC_ID_MPEG2VIDEO;
  437. type = AVMEDIA_TYPE_VIDEO;
  438. } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
  439. es_type == STREAM_TYPE_AUDIO_MPEG2){
  440. codec_id = AV_CODEC_ID_MP3;
  441. type = AVMEDIA_TYPE_AUDIO;
  442. } else if(es_type == STREAM_TYPE_AUDIO_AAC){
  443. codec_id = AV_CODEC_ID_AAC;
  444. type = AVMEDIA_TYPE_AUDIO;
  445. } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
  446. codec_id = AV_CODEC_ID_MPEG4;
  447. type = AVMEDIA_TYPE_VIDEO;
  448. } else if(es_type == STREAM_TYPE_VIDEO_H264){
  449. codec_id = AV_CODEC_ID_H264;
  450. type = AVMEDIA_TYPE_VIDEO;
  451. } else if(es_type == STREAM_TYPE_AUDIO_AC3){
  452. codec_id = AV_CODEC_ID_AC3;
  453. type = AVMEDIA_TYPE_AUDIO;
  454. } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
  455. static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
  456. unsigned char buf[8];
  457. avio_read(s->pb, buf, 8);
  458. avio_seek(s->pb, -8, SEEK_CUR);
  459. if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
  460. codec_id = AV_CODEC_ID_CAVS;
  461. else
  462. request_probe= 1;
  463. type = AVMEDIA_TYPE_VIDEO;
  464. } else if (startcode == PRIVATE_STREAM_2) {
  465. type = AVMEDIA_TYPE_DATA;
  466. codec_id = AV_CODEC_ID_DVD_NAV;
  467. } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
  468. type = AVMEDIA_TYPE_AUDIO;
  469. codec_id = m->sofdec > 0 ? AV_CODEC_ID_ADPCM_ADX : AV_CODEC_ID_MP2;
  470. } else if (startcode >= 0x80 && startcode <= 0x87) {
  471. type = AVMEDIA_TYPE_AUDIO;
  472. codec_id = AV_CODEC_ID_AC3;
  473. } else if ( ( startcode >= 0x88 && startcode <= 0x8f)
  474. ||( startcode >= 0x98 && startcode <= 0x9f)) {
  475. /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
  476. type = AVMEDIA_TYPE_AUDIO;
  477. codec_id = AV_CODEC_ID_DTS;
  478. } else if (startcode >= 0xa0 && startcode <= 0xaf) {
  479. type = AVMEDIA_TYPE_AUDIO;
  480. if(lpcm_header_len == 6) {
  481. codec_id = AV_CODEC_ID_MLP;
  482. } else {
  483. /* 16 bit form will be handled as AV_CODEC_ID_PCM_S16BE */
  484. codec_id = AV_CODEC_ID_PCM_DVD;
  485. }
  486. } else if (startcode >= 0xb0 && startcode <= 0xbf) {
  487. type = AVMEDIA_TYPE_AUDIO;
  488. codec_id = AV_CODEC_ID_TRUEHD;
  489. } else if (startcode >= 0xc0 && startcode <= 0xcf) {
  490. /* Used for both AC-3 and E-AC-3 in EVOB files */
  491. type = AVMEDIA_TYPE_AUDIO;
  492. codec_id = AV_CODEC_ID_AC3;
  493. } else if (startcode >= 0x20 && startcode <= 0x3f) {
  494. type = AVMEDIA_TYPE_SUBTITLE;
  495. codec_id = AV_CODEC_ID_DVD_SUBTITLE;
  496. } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
  497. type = AVMEDIA_TYPE_VIDEO;
  498. codec_id = AV_CODEC_ID_VC1;
  499. } else {
  500. skip:
  501. /* skip packet */
  502. avio_skip(s->pb, len);
  503. goto redo;
  504. }
  505. /* no stream found: add a new stream */
  506. st = avformat_new_stream(s, NULL);
  507. if (!st)
  508. goto skip;
  509. st->id = startcode;
  510. st->codec->codec_type = type;
  511. st->codec->codec_id = codec_id;
  512. st->request_probe = request_probe;
  513. if (codec_id != AV_CODEC_ID_PCM_S16BE)
  514. st->need_parsing = AVSTREAM_PARSE_FULL;
  515. found:
  516. if(st->discard >= AVDISCARD_ALL)
  517. goto skip;
  518. if (startcode >= 0xa0 && startcode <= 0xaf) {
  519. if (lpcm_header_len == 6 && st->codec->codec_id == AV_CODEC_ID_MLP) {
  520. if (len < 6)
  521. goto skip;
  522. avio_skip(s->pb, 6);
  523. len -=6;
  524. } else {
  525. int b1, freq;
  526. /* for LPCM, we just skip the header and consider it is raw
  527. audio data */
  528. if (len <= 3)
  529. goto skip;
  530. avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
  531. b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
  532. avio_r8(s->pb); /* dynamic range control (0x80 = off) */
  533. len -= 3;
  534. freq = (b1 >> 4) & 3;
  535. st->codec->sample_rate = lpcm_freq_tab[freq];
  536. st->codec->channels = 1 + (b1 & 7);
  537. st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
  538. st->codec->bit_rate = st->codec->channels *
  539. st->codec->sample_rate *
  540. st->codec->bits_per_coded_sample;
  541. if (st->codec->bits_per_coded_sample == 16)
  542. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  543. else if (st->codec->bits_per_coded_sample == 28)
  544. return AVERROR(EINVAL);
  545. }
  546. }
  547. ret = av_get_packet(s->pb, pkt, len);
  548. pkt->pts = pts;
  549. pkt->dts = dts;
  550. pkt->pos = dummy_pos;
  551. pkt->stream_index = st->index;
  552. av_dlog(s, "%d: pts=%0.3f dts=%0.3f size=%d\n",
  553. pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
  554. pkt->size);
  555. return (ret < 0) ? ret : 0;
  556. }
  557. static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
  558. int64_t *ppos, int64_t pos_limit)
  559. {
  560. int len, startcode;
  561. int64_t pos, pts, dts;
  562. pos = *ppos;
  563. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  564. return AV_NOPTS_VALUE;
  565. for(;;) {
  566. len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
  567. if (len < 0) {
  568. av_dlog(s, "none (ret=%d)\n", len);
  569. return AV_NOPTS_VALUE;
  570. }
  571. if (startcode == s->streams[stream_index]->id &&
  572. dts != AV_NOPTS_VALUE) {
  573. break;
  574. }
  575. avio_skip(s->pb, len);
  576. }
  577. av_dlog(s, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
  578. pos, dts, dts / 90000.0);
  579. *ppos = pos;
  580. return dts;
  581. }
  582. AVInputFormat ff_mpegps_demuxer = {
  583. .name = "mpeg",
  584. .long_name = NULL_IF_CONFIG_SMALL("MPEG-PS (MPEG-2 Program Stream)"),
  585. .priv_data_size = sizeof(MpegDemuxContext),
  586. .read_probe = mpegps_probe,
  587. .read_header = mpegps_read_header,
  588. .read_packet = mpegps_read_packet,
  589. .read_timestamp = mpegps_read_dts,
  590. .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
  591. };
  592. #if CONFIG_VOBSUB_DEMUXER
  593. #define REF_STRING "# VobSub index file,"
  594. static int vobsub_probe(AVProbeData *p)
  595. {
  596. if (!strncmp(p->buf, REF_STRING, sizeof(REF_STRING) - 1))
  597. return AVPROBE_SCORE_MAX;
  598. return 0;
  599. }
  600. static int vobsub_read_header(AVFormatContext *s)
  601. {
  602. int i, ret = 0, header_parsed = 0, langidx = 0;
  603. MpegDemuxContext *vobsub = s->priv_data;
  604. char *sub_name = NULL;
  605. size_t fname_len;
  606. char *ext, *header_str;
  607. AVBPrint header;
  608. int64_t delay = 0;
  609. AVStream *st = NULL;
  610. sub_name = av_strdup(s->filename);
  611. fname_len = strlen(sub_name);
  612. ext = sub_name - 3 + fname_len;
  613. if (fname_len < 4 || *(ext - 1) != '.') {
  614. av_log(s, AV_LOG_ERROR, "The input index filename is too short "
  615. "to guess the associated .SUB file\n");
  616. ret = AVERROR_INVALIDDATA;
  617. goto end;
  618. }
  619. memcpy(ext, !strncmp(ext, "IDX", 3) ? "SUB" : "sub", 3);
  620. av_log(s, AV_LOG_VERBOSE, "IDX/SUB: %s -> %s\n", s->filename, sub_name);
  621. ret = avformat_open_input(&vobsub->sub_ctx, sub_name, &ff_mpegps_demuxer, NULL);
  622. if (ret < 0) {
  623. av_log(s, AV_LOG_ERROR, "Unable to open %s as MPEG subtitles\n", sub_name);
  624. goto end;
  625. }
  626. av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
  627. while (!url_feof(s->pb)) {
  628. char line[2048];
  629. int len = ff_get_line(s->pb, line, sizeof(line));
  630. if (!len)
  631. break;
  632. line[strcspn(line, "\r\n")] = 0;
  633. if (!strncmp(line, "id:", 3)) {
  634. int n, stream_id = 0;
  635. char id[64] = {0};
  636. n = sscanf(line, "id: %63[^,], index: %u", id, &stream_id);
  637. if (n != 2) {
  638. av_log(s, AV_LOG_WARNING, "Unable to parse index line '%s', "
  639. "assuming 'id: und, index: 0'\n", line);
  640. strcpy(id, "und");
  641. stream_id = 0;
  642. }
  643. st = avformat_new_stream(s, NULL);
  644. if (!st) {
  645. ret = AVERROR(ENOMEM);
  646. goto end;
  647. }
  648. st->id = stream_id;
  649. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  650. st->codec->codec_id = AV_CODEC_ID_DVD_SUBTITLE;
  651. av_dict_set(&st->metadata, "language", id, 0);
  652. av_log(s, AV_LOG_DEBUG, "IDX stream[%d] id=%s\n", stream_id, id);
  653. header_parsed = 1;
  654. } else if (st && !strncmp(line, "timestamp:", 10)) {
  655. AVPacket *sub;
  656. int hh, mm, ss, ms;
  657. int64_t pos, timestamp;
  658. const char *p = line + 10;
  659. if (sscanf(p, "%02d:%02d:%02d:%03d, filepos: %"PRIx64,
  660. &hh, &mm, &ss, &ms, &pos) != 5) {
  661. av_log(s, AV_LOG_ERROR, "Unable to parse timestamp line '%s', "
  662. "abort parsing\n", line);
  663. break;
  664. }
  665. timestamp = (hh*3600LL + mm*60LL + ss) * 1000LL + ms + delay;
  666. timestamp = av_rescale_q(timestamp, (AVRational){1,1000}, st->time_base);
  667. sub = ff_subtitles_queue_insert(&vobsub->q, "", 0, 0);
  668. if (!sub) {
  669. ret = AVERROR(ENOMEM);
  670. goto end;
  671. }
  672. sub->pos = pos;
  673. sub->pts = timestamp;
  674. sub->stream_index = s->nb_streams - 1;
  675. } else if (st && !strncmp(line, "alt:", 4)) {
  676. const char *p = line + 4;
  677. while (*p == ' ')
  678. p++;
  679. av_dict_set(&st->metadata, "title", p, 0);
  680. av_log(s, AV_LOG_DEBUG, "IDX stream[%d] name=%s\n", st->id, p);
  681. header_parsed = 1;
  682. } else if (!strncmp(line, "delay:", 6)) {
  683. int sign = 1, hh = 0, mm = 0, ss = 0, ms = 0;
  684. const char *p = line + 6;
  685. while (*p == ' ')
  686. p++;
  687. if (*p == '-' || *p == '+') {
  688. sign = *p == '-' ? -1 : 1;
  689. p++;
  690. }
  691. sscanf(p, "%d:%d:%d:%d", &hh, &mm, &ss, &ms);
  692. delay = ((hh*3600LL + mm*60LL + ss) * 1000LL + ms) * sign;
  693. } else if (!strncmp(line, "langidx:", 8)) {
  694. const char *p = line + 8;
  695. if (sscanf(p, "%d", &langidx) != 1)
  696. av_log(s, AV_LOG_ERROR, "Invalid langidx specified\n");
  697. } else if (!header_parsed) {
  698. if (line[0] && line[0] != '#')
  699. av_bprintf(&header, "%s\n", line);
  700. }
  701. }
  702. if (langidx < s->nb_streams)
  703. s->streams[langidx]->disposition |= AV_DISPOSITION_DEFAULT;
  704. ff_subtitles_queue_finalize(&vobsub->q);
  705. if (!av_bprint_is_complete(&header)) {
  706. av_bprint_finalize(&header, NULL);
  707. ret = AVERROR(ENOMEM);
  708. goto end;
  709. }
  710. av_bprint_finalize(&header, &header_str);
  711. for (i = 0; i < s->nb_streams; i++) {
  712. AVStream *sub_st = s->streams[i];
  713. sub_st->codec->extradata = av_strdup(header_str);
  714. sub_st->codec->extradata_size = header.len;
  715. }
  716. av_free(header_str);
  717. end:
  718. av_free(sub_name);
  719. return ret;
  720. }
  721. #define FAIL(r) do { ret = r; goto fail; } while (0)
  722. static int vobsub_read_packet(AVFormatContext *s, AVPacket *pkt)
  723. {
  724. MpegDemuxContext *vobsub = s->priv_data;
  725. FFDemuxSubtitlesQueue *q = &vobsub->q;
  726. AVIOContext *pb = vobsub->sub_ctx->pb;
  727. int ret, psize, len16 = -1;
  728. AVPacket idx_pkt;
  729. ret = ff_subtitles_queue_read_packet(q, &idx_pkt);
  730. if (ret < 0)
  731. return ret;
  732. /* compute maximum packet size using the next packet position. This is
  733. * useful when the len in the header is non-sense */
  734. if (q->current_sub_idx < q->nb_subs) {
  735. psize = q->subs[q->current_sub_idx].pos - idx_pkt.pos;
  736. } else {
  737. int64_t fsize = avio_size(pb);
  738. psize = fsize < 0 ? 0xffff : fsize - idx_pkt.pos;
  739. }
  740. avio_seek(pb, idx_pkt.pos, SEEK_SET);
  741. av_init_packet(pkt);
  742. pkt->size = 0;
  743. pkt->data = NULL;
  744. do {
  745. int n, to_read, startcode;
  746. int64_t pts, dts;
  747. ret = mpegps_read_pes_header(vobsub->sub_ctx, NULL, &startcode, &pts, &dts);
  748. if (ret < 0)
  749. FAIL(ret);
  750. to_read = ret & 0xffff;
  751. /* this prevents reads above the current packet */
  752. if (pkt->size + to_read > psize)
  753. break;
  754. /* if the len is computed, we check for overread */
  755. if (len16 != -1 && pkt->size + to_read > len16)
  756. break;
  757. /* the current chunk doesn't match the stream index (unlikely) */
  758. if ((startcode & 0x1f) != idx_pkt.stream_index)
  759. break;
  760. ret = av_grow_packet(pkt, to_read);
  761. if (ret < 0)
  762. FAIL(ret);
  763. n = avio_read(pb, pkt->data + (pkt->size - to_read), to_read);
  764. if (n < to_read)
  765. pkt->size -= to_read - n;
  766. /* first chunk contains the total len of the packet to raise */
  767. if (len16 == -1 && n > 2)
  768. len16 = AV_RB16(pkt->data);
  769. } while (len16 != -1 && pkt->size != len16);
  770. pkt->pts = pkt->dts = idx_pkt.pts;
  771. pkt->pos = idx_pkt.pos;
  772. pkt->stream_index = idx_pkt.stream_index;
  773. av_free_packet(&idx_pkt);
  774. return 0;
  775. fail:
  776. av_free_packet(&idx_pkt);
  777. return ret;
  778. }
  779. static int vobsub_read_seek(AVFormatContext *s, int stream_index,
  780. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  781. {
  782. MpegDemuxContext *vobsub = s->priv_data;
  783. return ff_subtitles_queue_seek(&vobsub->q, s, stream_index,
  784. min_ts, ts, max_ts, flags);
  785. }
  786. static int vobsub_read_close(AVFormatContext *s)
  787. {
  788. MpegDemuxContext *vobsub = s->priv_data;
  789. ff_subtitles_queue_clean(&vobsub->q);
  790. if (vobsub->sub_ctx)
  791. avformat_close_input(&vobsub->sub_ctx);
  792. return 0;
  793. }
  794. AVInputFormat ff_vobsub_demuxer = {
  795. .name = "vobsub",
  796. .long_name = NULL_IF_CONFIG_SMALL("VobSub subtitle format"),
  797. .priv_data_size = sizeof(MpegDemuxContext),
  798. .read_probe = vobsub_probe,
  799. .read_header = vobsub_read_header,
  800. .read_packet = vobsub_read_packet,
  801. .read_seek2 = vobsub_read_seek,
  802. .read_close = vobsub_read_close,
  803. .flags = AVFMT_SHOW_IDS,
  804. .extensions = "idx",
  805. };
  806. #endif