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.

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