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.

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