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.

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