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.

510 lines
17KB

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