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.

538 lines
18KB

  1. /*
  2. * Flash Compatible Streaming Format demuxer
  3. * Copyright (c) 2000 Fabrice Bellard
  4. * Copyright (c) 2003 Tinic Uro
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavcodec/get_bits.h"
  27. #include "swf.h"
  28. static const AVCodecTag swf_audio_codec_tags[] = {
  29. { AV_CODEC_ID_PCM_S16LE, 0x00 },
  30. { AV_CODEC_ID_ADPCM_SWF, 0x01 },
  31. { AV_CODEC_ID_MP3, 0x02 },
  32. { AV_CODEC_ID_PCM_S16LE, 0x03 },
  33. // { AV_CODEC_ID_NELLYMOSER, 0x06 },
  34. { AV_CODEC_ID_NONE, 0 },
  35. };
  36. static int get_swf_tag(AVIOContext *pb, int *len_ptr)
  37. {
  38. int tag, len;
  39. if (avio_feof(pb))
  40. return AVERROR_EOF;
  41. tag = avio_rl16(pb);
  42. len = tag & 0x3f;
  43. tag = tag >> 6;
  44. if (len == 0x3f) {
  45. len = avio_rl32(pb);
  46. }
  47. *len_ptr = len;
  48. return tag;
  49. }
  50. static int swf_probe(AVProbeData *p)
  51. {
  52. GetBitContext gb;
  53. int len, xmin, xmax, ymin, ymax;
  54. if(p->buf_size < 15)
  55. return 0;
  56. /* check file header */
  57. if ( AV_RB24(p->buf) != AV_RB24("CWS")
  58. && AV_RB24(p->buf) != AV_RB24("FWS"))
  59. return 0;
  60. init_get_bits8(&gb, p->buf + 3, p->buf_size - 3);
  61. skip_bits(&gb, 40);
  62. len = get_bits(&gb, 5);
  63. if (!len)
  64. return 0;
  65. xmin = get_bits_long(&gb, len);
  66. xmax = get_bits_long(&gb, len);
  67. ymin = get_bits_long(&gb, len);
  68. ymax = get_bits_long(&gb, len);
  69. if (xmin || ymin || !xmax || !ymax)
  70. return 0;
  71. if (p->buf[3] >= 20 || xmax < 16 || ymax < 16)
  72. return AVPROBE_SCORE_MAX / 4;
  73. return AVPROBE_SCORE_MAX;
  74. }
  75. #if CONFIG_ZLIB
  76. static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
  77. {
  78. AVFormatContext *s = opaque;
  79. SWFContext *swf = s->priv_data;
  80. z_stream *z = &swf->zstream;
  81. int ret;
  82. retry:
  83. if (!z->avail_in) {
  84. int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
  85. if (n < 0)
  86. return n;
  87. z->next_in = swf->zbuf_in;
  88. z->avail_in = n;
  89. }
  90. z->next_out = buf;
  91. z->avail_out = buf_size;
  92. ret = inflate(z, Z_NO_FLUSH);
  93. if (ret < 0)
  94. return AVERROR(EINVAL);
  95. if (ret == Z_STREAM_END)
  96. return AVERROR_EOF;
  97. if (buf_size - z->avail_out == 0)
  98. goto retry;
  99. return buf_size - z->avail_out;
  100. }
  101. #endif
  102. static int swf_read_header(AVFormatContext *s)
  103. {
  104. SWFContext *swf = s->priv_data;
  105. AVIOContext *pb = s->pb;
  106. int nbits, len, tag;
  107. tag = avio_rb32(pb) & 0xffffff00;
  108. avio_rl32(pb);
  109. if (tag == MKBETAG('C', 'W', 'S', 0)) {
  110. av_log(s, AV_LOG_INFO, "SWF compressed file detected\n");
  111. #if CONFIG_ZLIB
  112. swf->zbuf_in = av_malloc(ZBUF_SIZE);
  113. swf->zbuf_out = av_malloc(ZBUF_SIZE);
  114. swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
  115. zlib_refill, NULL, NULL);
  116. if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb)
  117. return AVERROR(ENOMEM);
  118. swf->zpb->seekable = 0;
  119. if (inflateInit(&swf->zstream) != Z_OK) {
  120. av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
  121. return AVERROR(EINVAL);
  122. }
  123. pb = swf->zpb;
  124. #else
  125. av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
  126. return AVERROR(EIO);
  127. #endif
  128. } else if (tag != MKBETAG('F', 'W', 'S', 0))
  129. return AVERROR(EIO);
  130. /* skip rectangle size */
  131. nbits = avio_r8(pb) >> 3;
  132. len = (4 * nbits - 3 + 7) / 8;
  133. avio_skip(pb, len);
  134. swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
  135. avio_rl16(pb); /* frame count */
  136. swf->samples_per_frame = 0;
  137. s->ctx_flags |= AVFMTCTX_NOHEADER;
  138. return 0;
  139. }
  140. static AVStream *create_new_audio_stream(AVFormatContext *s, int id, int info)
  141. {
  142. int sample_rate_code, sample_size_code;
  143. AVStream *ast = avformat_new_stream(s, NULL);
  144. if (!ast)
  145. return NULL;
  146. ast->id = id;
  147. if (info & 1) {
  148. ast->codec->channels = 2;
  149. ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  150. } else {
  151. ast->codec->channels = 1;
  152. ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
  153. }
  154. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  155. ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, info>>4 & 15);
  156. ast->need_parsing = AVSTREAM_PARSE_FULL;
  157. sample_rate_code = info>>2 & 3;
  158. sample_size_code = info>>1 & 1;
  159. if (!sample_size_code && ast->codec->codec_id == AV_CODEC_ID_PCM_S16LE)
  160. ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
  161. ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
  162. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  163. return ast;
  164. }
  165. static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
  166. {
  167. SWFContext *swf = s->priv_data;
  168. AVIOContext *pb = s->pb;
  169. AVStream *vst = NULL, *ast = NULL, *st = 0;
  170. int tag, len, i, frame, v, res;
  171. #if CONFIG_ZLIB
  172. if (swf->zpb)
  173. pb = swf->zpb;
  174. #endif
  175. for(;;) {
  176. uint64_t pos = avio_tell(pb);
  177. tag = get_swf_tag(pb, &len);
  178. if (tag < 0)
  179. return tag;
  180. if (len < 0) {
  181. av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
  182. return AVERROR_INVALIDDATA;
  183. }
  184. if (tag == TAG_VIDEOSTREAM) {
  185. int ch_id = avio_rl16(pb);
  186. len -= 2;
  187. for (i=0; i<s->nb_streams; i++) {
  188. st = s->streams[i];
  189. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
  190. goto skip;
  191. }
  192. avio_rl16(pb);
  193. avio_rl16(pb);
  194. avio_rl16(pb);
  195. avio_r8(pb);
  196. /* Check for FLV1 */
  197. vst = avformat_new_stream(s, NULL);
  198. if (!vst)
  199. return AVERROR(ENOMEM);
  200. vst->id = ch_id;
  201. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  202. vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
  203. avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
  204. len -= 8;
  205. } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
  206. /* streaming found */
  207. for (i=0; i<s->nb_streams; i++) {
  208. st = s->streams[i];
  209. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
  210. goto skip;
  211. }
  212. avio_r8(pb);
  213. v = avio_r8(pb);
  214. swf->samples_per_frame = avio_rl16(pb);
  215. ast = create_new_audio_stream(s, -1, v); /* -1 to avoid clash with video stream ch_id */
  216. if (!ast)
  217. return AVERROR(ENOMEM);
  218. len -= 4;
  219. } else if (tag == TAG_DEFINESOUND) {
  220. /* audio stream */
  221. int ch_id = avio_rl16(pb);
  222. for (i=0; i<s->nb_streams; i++) {
  223. st = s->streams[i];
  224. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == ch_id)
  225. goto skip;
  226. }
  227. // FIXME: The entire audio stream is stored in a single chunk/tag. Normally,
  228. // these are smaller audio streams in DEFINESOUND tags, but it's technically
  229. // possible they could be huge. Break it up into multiple packets if it's big.
  230. v = avio_r8(pb);
  231. ast = create_new_audio_stream(s, ch_id, v);
  232. if (!ast)
  233. return AVERROR(ENOMEM);
  234. ast->duration = avio_rl32(pb); // number of samples
  235. if (((v>>4) & 15) == 2) { // MP3 sound data record
  236. ast->skip_samples = avio_rl16(pb);
  237. len -= 2;
  238. }
  239. len -= 7;
  240. if ((res = av_get_packet(pb, pkt, len)) < 0)
  241. return res;
  242. pkt->pos = pos;
  243. pkt->stream_index = ast->index;
  244. return pkt->size;
  245. } else if (tag == TAG_VIDEOFRAME) {
  246. int ch_id = avio_rl16(pb);
  247. len -= 2;
  248. for(i=0; i<s->nb_streams; i++) {
  249. st = s->streams[i];
  250. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
  251. frame = avio_rl16(pb);
  252. len -= 2;
  253. if (len <= 0)
  254. goto skip;
  255. if ((res = av_get_packet(pb, pkt, len)) < 0)
  256. return res;
  257. pkt->pos = pos;
  258. pkt->pts = frame;
  259. pkt->stream_index = st->index;
  260. return pkt->size;
  261. }
  262. }
  263. } else if (tag == TAG_DEFINEBITSLOSSLESS || tag == TAG_DEFINEBITSLOSSLESS2) {
  264. #if CONFIG_ZLIB
  265. long out_len;
  266. uint8_t *buf = NULL, *zbuf = NULL, *pal;
  267. uint32_t colormap[AVPALETTE_COUNT] = {0};
  268. const int alpha_bmp = tag == TAG_DEFINEBITSLOSSLESS2;
  269. const int colormapbpp = 3 + alpha_bmp;
  270. int linesize, colormapsize = 0;
  271. const int ch_id = avio_rl16(pb);
  272. const int bmp_fmt = avio_r8(pb);
  273. const int width = avio_rl16(pb);
  274. const int height = avio_rl16(pb);
  275. int pix_fmt;
  276. len -= 2+1+2+2;
  277. switch (bmp_fmt) {
  278. case 3: // PAL-8
  279. linesize = width;
  280. colormapsize = avio_r8(pb) + 1;
  281. len--;
  282. break;
  283. case 4: // RGB15
  284. linesize = width * 2;
  285. break;
  286. case 5: // RGB24 (0RGB)
  287. linesize = width * 4;
  288. break;
  289. default:
  290. av_log(s, AV_LOG_ERROR, "invalid bitmap format %d, skipped\n", bmp_fmt);
  291. goto bitmap_end_skip;
  292. }
  293. linesize = FFALIGN(linesize, 4);
  294. if (av_image_check_size(width, height, 0, s) < 0 ||
  295. linesize >= INT_MAX / height ||
  296. linesize * height >= INT_MAX - colormapsize * colormapbpp) {
  297. av_log(s, AV_LOG_ERROR, "invalid frame size %dx%d\n", width, height);
  298. goto bitmap_end_skip;
  299. }
  300. out_len = colormapsize * colormapbpp + linesize * height;
  301. av_dlog(s, "bitmap: ch=%d fmt=%d %dx%d (linesize=%d) len=%d->%ld pal=%d\n",
  302. ch_id, bmp_fmt, width, height, linesize, len, out_len, colormapsize);
  303. zbuf = av_malloc(len);
  304. buf = av_malloc(out_len);
  305. if (!zbuf || !buf) {
  306. res = AVERROR(ENOMEM);
  307. goto bitmap_end;
  308. }
  309. len = avio_read(pb, zbuf, len);
  310. if (len < 0 || (res = uncompress(buf, &out_len, zbuf, len)) != Z_OK) {
  311. av_log(s, AV_LOG_WARNING, "Failed to uncompress one bitmap\n");
  312. goto bitmap_end_skip;
  313. }
  314. for (i = 0; i < s->nb_streams; i++) {
  315. st = s->streams[i];
  316. if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && st->id == -3)
  317. break;
  318. }
  319. if (i == s->nb_streams) {
  320. vst = avformat_new_stream(s, NULL);
  321. if (!vst) {
  322. res = AVERROR(ENOMEM);
  323. goto bitmap_end;
  324. }
  325. vst->id = -3; /* -3 to avoid clash with video stream and audio stream */
  326. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  327. vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  328. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  329. st = vst;
  330. }
  331. if ((res = av_new_packet(pkt, out_len - colormapsize * colormapbpp)) < 0)
  332. goto bitmap_end;
  333. if (!st->codec->width && !st->codec->height) {
  334. st->codec->width = width;
  335. st->codec->height = height;
  336. } else {
  337. ff_add_param_change(pkt, 0, 0, 0, width, height);
  338. }
  339. pkt->pos = pos;
  340. pkt->stream_index = st->index;
  341. switch (bmp_fmt) {
  342. case 3:
  343. pix_fmt = AV_PIX_FMT_PAL8;
  344. for (i = 0; i < colormapsize; i++)
  345. if (alpha_bmp) colormap[i] = buf[3]<<24 | AV_RB24(buf + 4*i);
  346. else colormap[i] = 0xffU <<24 | AV_RB24(buf + 3*i);
  347. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  348. if (!pal) {
  349. res = AVERROR(ENOMEM);
  350. goto bitmap_end;
  351. }
  352. memcpy(pal, colormap, AVPALETTE_SIZE);
  353. break;
  354. case 4:
  355. pix_fmt = AV_PIX_FMT_RGB555;
  356. break;
  357. case 5:
  358. pix_fmt = alpha_bmp ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
  359. break;
  360. default:
  361. av_assert0(0);
  362. }
  363. if (st->codec->pix_fmt != AV_PIX_FMT_NONE && st->codec->pix_fmt != pix_fmt) {
  364. av_log(s, AV_LOG_ERROR, "pixel format change unsupported\n");
  365. res = AVERROR_PATCHWELCOME;
  366. goto bitmap_end;
  367. }
  368. st->codec->pix_fmt = pix_fmt;
  369. if (linesize * height > pkt->size) {
  370. res = AVERROR_INVALIDDATA;
  371. goto bitmap_end;
  372. }
  373. memcpy(pkt->data, buf + colormapsize*colormapbpp, linesize * height);
  374. res = pkt->size;
  375. bitmap_end:
  376. av_freep(&zbuf);
  377. av_freep(&buf);
  378. return res;
  379. bitmap_end_skip:
  380. av_freep(&zbuf);
  381. av_freep(&buf);
  382. #else
  383. av_log(s, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  384. #endif
  385. } else if (tag == TAG_STREAMBLOCK) {
  386. for (i = 0; i < s->nb_streams; i++) {
  387. st = s->streams[i];
  388. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
  389. if (st->codec->codec_id == AV_CODEC_ID_MP3) {
  390. avio_skip(pb, 4);
  391. len -= 4;
  392. if (len <= 0)
  393. goto skip;
  394. if ((res = av_get_packet(pb, pkt, len)) < 0)
  395. return res;
  396. } else { // ADPCM, PCM
  397. if (len <= 0)
  398. goto skip;
  399. if ((res = av_get_packet(pb, pkt, len)) < 0)
  400. return res;
  401. }
  402. pkt->pos = pos;
  403. pkt->stream_index = st->index;
  404. return pkt->size;
  405. }
  406. }
  407. } else if (tag == TAG_JPEG2) {
  408. for (i=0; i<s->nb_streams; i++) {
  409. st = s->streams[i];
  410. if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
  411. break;
  412. }
  413. if (i == s->nb_streams) {
  414. vst = avformat_new_stream(s, NULL);
  415. if (!vst)
  416. return AVERROR(ENOMEM);
  417. vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
  418. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  419. vst->codec->codec_id = AV_CODEC_ID_MJPEG;
  420. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  421. st = vst;
  422. }
  423. avio_rl16(pb); /* BITMAP_ID */
  424. len -= 2;
  425. if (len < 4)
  426. goto skip;
  427. if ((res = av_new_packet(pkt, len)) < 0)
  428. return res;
  429. if (avio_read(pb, pkt->data, 4) != 4) {
  430. av_free_packet(pkt);
  431. return AVERROR_INVALIDDATA;
  432. }
  433. if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
  434. AV_RB32(pkt->data) == 0xffd9ffd8) {
  435. /* old SWF files containing SOI/EOI as data start */
  436. /* files created by swink have reversed tag */
  437. pkt->size -= 4;
  438. memset(pkt->data+pkt->size, 0, 4);
  439. res = avio_read(pb, pkt->data, pkt->size);
  440. } else {
  441. res = avio_read(pb, pkt->data + 4, pkt->size - 4);
  442. if (res >= 0)
  443. res += 4;
  444. }
  445. if (res != pkt->size) {
  446. if (res < 0) {
  447. av_free_packet(pkt);
  448. return res;
  449. }
  450. av_shrink_packet(pkt, res);
  451. }
  452. pkt->pos = pos;
  453. pkt->stream_index = st->index;
  454. return pkt->size;
  455. } else {
  456. av_log(s, AV_LOG_DEBUG, "Unknown tag: %d\n", tag);
  457. }
  458. skip:
  459. if(len<0)
  460. av_log(s, AV_LOG_WARNING, "Cliping len %d\n", len);
  461. len = FFMAX(0, len);
  462. avio_skip(pb, len);
  463. }
  464. }
  465. #if CONFIG_ZLIB
  466. static av_cold int swf_read_close(AVFormatContext *avctx)
  467. {
  468. SWFContext *s = avctx->priv_data;
  469. inflateEnd(&s->zstream);
  470. av_freep(&s->zbuf_in);
  471. av_freep(&s->zbuf_out);
  472. av_freep(&s->zpb);
  473. return 0;
  474. }
  475. #endif
  476. AVInputFormat ff_swf_demuxer = {
  477. .name = "swf",
  478. .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
  479. .priv_data_size = sizeof(SWFContext),
  480. .read_probe = swf_probe,
  481. .read_header = swf_read_header,
  482. .read_packet = swf_read_packet,
  483. #if CONFIG_ZLIB
  484. .read_close = swf_read_close,
  485. #endif
  486. };