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.

457 lines
15KB

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