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.

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