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.

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