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.

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