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.

491 lines
16KB

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