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(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 + 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_EXTENSION + 1;
  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 == Z_STREAM_END)
  103. return AVERROR_EOF;
  104. if (ret != Z_OK)
  105. return AVERROR(EINVAL);
  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. av_freep(&swf->zbuf_in);
  131. av_freep(&swf->zbuf_out);
  132. return AVERROR(EINVAL);
  133. }
  134. pb = swf->zpb;
  135. #else
  136. av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n");
  137. return AVERROR(EIO);
  138. #endif
  139. } else if (tag != MKBETAG('F', 'W', 'S', 0))
  140. return AVERROR(EIO);
  141. /* skip rectangle size */
  142. nbits = avio_r8(pb) >> 3;
  143. len = (4 * nbits - 3 + 7) / 8;
  144. avio_skip(pb, len);
  145. swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
  146. avio_rl16(pb); /* frame count */
  147. swf->samples_per_frame = 0;
  148. s->ctx_flags |= AVFMTCTX_NOHEADER;
  149. return 0;
  150. }
  151. static AVStream *create_new_audio_stream(AVFormatContext *s, int id, int info)
  152. {
  153. int sample_rate_code, sample_size_code;
  154. AVStream *ast = avformat_new_stream(s, NULL);
  155. if (!ast)
  156. return NULL;
  157. ast->id = id;
  158. if (info & 1) {
  159. ast->codecpar->channels = 2;
  160. ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  161. } else {
  162. ast->codecpar->channels = 1;
  163. ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  164. }
  165. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  166. ast->codecpar->codec_id = ff_codec_get_id(swf_audio_codec_tags, info>>4 & 15);
  167. ast->need_parsing = AVSTREAM_PARSE_FULL;
  168. sample_rate_code = info>>2 & 3;
  169. sample_size_code = info>>1 & 1;
  170. if (!sample_size_code && ast->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE)
  171. ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  172. ast->codecpar->sample_rate = 44100 >> (3 - sample_rate_code);
  173. avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
  174. return ast;
  175. }
  176. static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
  177. {
  178. SWFContext *swf = s->priv_data;
  179. AVIOContext *pb = s->pb;
  180. AVStream *vst = NULL, *ast = NULL, *st = 0;
  181. int tag, len, i, frame, v, res;
  182. #if CONFIG_ZLIB
  183. if (swf->zpb)
  184. pb = swf->zpb;
  185. #endif
  186. for(;;) {
  187. uint64_t pos = avio_tell(pb);
  188. tag = get_swf_tag(pb, &len);
  189. if (tag < 0)
  190. return tag;
  191. if (len < 0) {
  192. av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
  193. return AVERROR_INVALIDDATA;
  194. }
  195. if (tag == TAG_VIDEOSTREAM) {
  196. int ch_id = avio_rl16(pb);
  197. len -= 2;
  198. for (i=0; i<s->nb_streams; i++) {
  199. st = s->streams[i];
  200. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
  201. goto skip;
  202. }
  203. avio_rl16(pb);
  204. avio_rl16(pb);
  205. avio_rl16(pb);
  206. avio_r8(pb);
  207. /* Check for FLV1 */
  208. vst = avformat_new_stream(s, NULL);
  209. if (!vst)
  210. return AVERROR(ENOMEM);
  211. vst->id = ch_id;
  212. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  213. vst->codecpar->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
  214. avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
  215. len -= 8;
  216. } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
  217. /* streaming found */
  218. for (i=0; i<s->nb_streams; i++) {
  219. st = s->streams[i];
  220. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
  221. goto skip;
  222. }
  223. avio_r8(pb);
  224. v = avio_r8(pb);
  225. swf->samples_per_frame = avio_rl16(pb);
  226. ast = create_new_audio_stream(s, -1, v); /* -1 to avoid clash with video stream ch_id */
  227. if (!ast)
  228. return AVERROR(ENOMEM);
  229. len -= 4;
  230. } else if (tag == TAG_DEFINESOUND) {
  231. /* audio stream */
  232. int ch_id = avio_rl16(pb);
  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 == ch_id)
  236. goto skip;
  237. }
  238. // FIXME: The entire audio stream is stored in a single chunk/tag. Normally,
  239. // these are smaller audio streams in DEFINESOUND tags, but it's technically
  240. // possible they could be huge. Break it up into multiple packets if it's big.
  241. v = avio_r8(pb);
  242. ast = create_new_audio_stream(s, ch_id, v);
  243. if (!ast)
  244. return AVERROR(ENOMEM);
  245. ast->duration = avio_rl32(pb); // number of samples
  246. if (((v>>4) & 15) == 2) { // MP3 sound data record
  247. ast->skip_samples = avio_rl16(pb);
  248. len -= 2;
  249. }
  250. len -= 7;
  251. if ((res = av_get_packet(pb, pkt, len)) < 0)
  252. return res;
  253. pkt->pos = pos;
  254. pkt->stream_index = ast->index;
  255. return pkt->size;
  256. } else if (tag == TAG_VIDEOFRAME) {
  257. int ch_id = avio_rl16(pb);
  258. len -= 2;
  259. for(i=0; i<s->nb_streams; i++) {
  260. st = s->streams[i];
  261. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
  262. frame = avio_rl16(pb);
  263. len -= 2;
  264. if (len <= 0)
  265. goto skip;
  266. if ((res = av_get_packet(pb, pkt, len)) < 0)
  267. return res;
  268. pkt->pos = pos;
  269. pkt->pts = frame;
  270. pkt->stream_index = st->index;
  271. return pkt->size;
  272. }
  273. }
  274. } else if (tag == TAG_DEFINEBITSLOSSLESS || tag == TAG_DEFINEBITSLOSSLESS2) {
  275. #if CONFIG_ZLIB
  276. long out_len;
  277. uint8_t *buf = NULL, *zbuf = NULL, *pal;
  278. uint32_t colormap[AVPALETTE_COUNT] = {0};
  279. const int alpha_bmp = tag == TAG_DEFINEBITSLOSSLESS2;
  280. const int colormapbpp = 3 + alpha_bmp;
  281. int linesize, colormapsize = 0;
  282. const int ch_id = avio_rl16(pb);
  283. const int bmp_fmt = avio_r8(pb);
  284. const int width = avio_rl16(pb);
  285. const int height = avio_rl16(pb);
  286. int pix_fmt;
  287. len -= 2+1+2+2;
  288. switch (bmp_fmt) {
  289. case 3: // PAL-8
  290. linesize = width;
  291. colormapsize = avio_r8(pb) + 1;
  292. len--;
  293. break;
  294. case 4: // RGB15
  295. linesize = width * 2;
  296. break;
  297. case 5: // RGB24 (0RGB)
  298. linesize = width * 4;
  299. break;
  300. default:
  301. av_log(s, AV_LOG_ERROR, "invalid bitmap format %d, skipped\n", bmp_fmt);
  302. goto bitmap_end_skip;
  303. }
  304. linesize = FFALIGN(linesize, 4);
  305. if (av_image_check_size(width, height, 0, s) < 0 ||
  306. linesize >= INT_MAX / height ||
  307. linesize * height >= INT_MAX - colormapsize * colormapbpp) {
  308. av_log(s, AV_LOG_ERROR, "invalid frame size %dx%d\n", width, height);
  309. goto bitmap_end_skip;
  310. }
  311. out_len = colormapsize * colormapbpp + linesize * height;
  312. ff_dlog(s, "bitmap: ch=%d fmt=%d %dx%d (linesize=%d) len=%d->%ld pal=%d\n",
  313. ch_id, bmp_fmt, width, height, linesize, len, out_len, colormapsize);
  314. zbuf = av_malloc(len);
  315. buf = av_malloc(out_len);
  316. if (!zbuf || !buf) {
  317. res = AVERROR(ENOMEM);
  318. goto bitmap_end;
  319. }
  320. len = avio_read(pb, zbuf, len);
  321. if (len < 0 || (res = uncompress(buf, &out_len, zbuf, len)) != Z_OK) {
  322. av_log(s, AV_LOG_WARNING, "Failed to uncompress one bitmap\n");
  323. goto bitmap_end_skip;
  324. }
  325. for (i = 0; i < s->nb_streams; i++) {
  326. st = s->streams[i];
  327. if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO && st->id == -3)
  328. break;
  329. }
  330. if (i == s->nb_streams) {
  331. vst = avformat_new_stream(s, NULL);
  332. if (!vst) {
  333. res = AVERROR(ENOMEM);
  334. goto bitmap_end;
  335. }
  336. vst->id = -3; /* -3 to avoid clash with video stream and audio stream */
  337. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  338. vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  339. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  340. st = vst;
  341. }
  342. if ((res = av_new_packet(pkt, out_len - colormapsize * colormapbpp)) < 0)
  343. goto bitmap_end;
  344. if (!st->codecpar->width && !st->codecpar->height) {
  345. st->codecpar->width = width;
  346. st->codecpar->height = height;
  347. } else {
  348. ff_add_param_change(pkt, 0, 0, 0, width, height);
  349. }
  350. pkt->pos = pos;
  351. pkt->stream_index = st->index;
  352. if (linesize * height > pkt->size) {
  353. res = AVERROR_INVALIDDATA;
  354. goto bitmap_end;
  355. }
  356. switch (bmp_fmt) {
  357. case 3:
  358. pix_fmt = AV_PIX_FMT_PAL8;
  359. for (i = 0; i < colormapsize; i++)
  360. if (alpha_bmp) colormap[i] = buf[3]<<24 | AV_RB24(buf + 4*i);
  361. else colormap[i] = 0xffU <<24 | AV_RB24(buf + 3*i);
  362. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  363. if (!pal) {
  364. res = AVERROR(ENOMEM);
  365. goto bitmap_end;
  366. }
  367. memcpy(pal, colormap, AVPALETTE_SIZE);
  368. break;
  369. case 4:
  370. pix_fmt = AV_PIX_FMT_RGB555;
  371. break;
  372. case 5:
  373. pix_fmt = alpha_bmp ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
  374. break;
  375. default:
  376. av_assert0(0);
  377. }
  378. if (st->codecpar->format != AV_PIX_FMT_NONE && st->codecpar->format != pix_fmt) {
  379. av_log(s, AV_LOG_ERROR, "pixel format change unsupported\n");
  380. } else
  381. st->codecpar->format = pix_fmt;
  382. memcpy(pkt->data, buf + colormapsize*colormapbpp, linesize * height);
  383. res = pkt->size;
  384. bitmap_end:
  385. av_freep(&zbuf);
  386. av_freep(&buf);
  387. return res;
  388. bitmap_end_skip:
  389. av_freep(&zbuf);
  390. av_freep(&buf);
  391. #else
  392. av_log(s, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  393. #endif
  394. } else if (tag == TAG_STREAMBLOCK) {
  395. for (i = 0; i < s->nb_streams; i++) {
  396. st = s->streams[i];
  397. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
  398. if (st->codecpar->codec_id == AV_CODEC_ID_MP3) {
  399. avio_skip(pb, 4);
  400. len -= 4;
  401. if (len <= 0)
  402. goto skip;
  403. if ((res = av_get_packet(pb, pkt, len)) < 0)
  404. return res;
  405. } else { // ADPCM, PCM
  406. if (len <= 0)
  407. goto skip;
  408. if ((res = av_get_packet(pb, pkt, len)) < 0)
  409. return res;
  410. }
  411. pkt->pos = pos;
  412. pkt->stream_index = st->index;
  413. return pkt->size;
  414. }
  415. }
  416. } else if (tag == TAG_JPEG2) {
  417. for (i=0; i<s->nb_streams; i++) {
  418. st = s->streams[i];
  419. if (st->codecpar->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
  420. break;
  421. }
  422. if (i == s->nb_streams) {
  423. vst = avformat_new_stream(s, NULL);
  424. if (!vst)
  425. return AVERROR(ENOMEM);
  426. vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
  427. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  428. vst->codecpar->codec_id = AV_CODEC_ID_MJPEG;
  429. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  430. st = vst;
  431. }
  432. avio_rl16(pb); /* BITMAP_ID */
  433. len -= 2;
  434. if (len < 4)
  435. goto skip;
  436. if ((res = av_new_packet(pkt, len)) < 0)
  437. return res;
  438. if (avio_read(pb, pkt->data, 4) != 4) {
  439. return AVERROR_INVALIDDATA;
  440. }
  441. if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
  442. AV_RB32(pkt->data) == 0xffd9ffd8) {
  443. /* old SWF files containing SOI/EOI as data start */
  444. /* files created by swink have reversed tag */
  445. pkt->size -= 4;
  446. memset(pkt->data+pkt->size, 0, 4);
  447. res = avio_read(pb, pkt->data, pkt->size);
  448. } else {
  449. res = avio_read(pb, pkt->data + 4, pkt->size - 4);
  450. if (res >= 0)
  451. res += 4;
  452. }
  453. if (res != pkt->size) {
  454. if (res < 0) {
  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, "Clipping 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. avio_context_free(&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. };