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.

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