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.

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