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.

331 lines
10KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #if CONFIG_ZLIB
  24. #include <zlib.h>
  25. #endif
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "swf.h"
  29. static const AVCodecTag swf_audio_codec_tags[] = {
  30. { AV_CODEC_ID_PCM_S16LE, 0x00 },
  31. { AV_CODEC_ID_ADPCM_SWF, 0x01 },
  32. { AV_CODEC_ID_MP3, 0x02 },
  33. { AV_CODEC_ID_PCM_S16LE, 0x03 },
  34. // { AV_CODEC_ID_NELLYMOSER, 0x06 },
  35. { AV_CODEC_ID_NONE, 0 },
  36. };
  37. static int get_swf_tag(AVIOContext *pb, int *len_ptr)
  38. {
  39. int tag, len;
  40. if (pb->eof_reached)
  41. return -1;
  42. tag = avio_rl16(pb);
  43. len = tag & 0x3f;
  44. tag = tag >> 6;
  45. if (len == 0x3f) {
  46. len = avio_rl32(pb);
  47. }
  48. *len_ptr = len;
  49. return tag;
  50. }
  51. static int swf_probe(AVProbeData *p)
  52. {
  53. /* check file header */
  54. if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
  55. p->buf[2] == 'S')
  56. return AVPROBE_SCORE_MAX;
  57. else
  58. return 0;
  59. }
  60. #if CONFIG_ZLIB
  61. static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
  62. {
  63. AVFormatContext *s = opaque;
  64. SWFContext *swf = s->priv_data;
  65. z_stream *z = &swf->zstream;
  66. int ret;
  67. retry:
  68. if (!z->avail_in) {
  69. int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
  70. if (n < 0)
  71. return n;
  72. z->next_in = swf->zbuf_in;
  73. z->avail_in = n;
  74. }
  75. z->next_out = buf;
  76. z->avail_out = buf_size;
  77. ret = inflate(z, Z_NO_FLUSH);
  78. if (ret != Z_OK && ret != Z_STREAM_END) {
  79. av_log(s, AV_LOG_ERROR, "Inflate error: %d\n", ret);
  80. return AVERROR_UNKNOWN;
  81. }
  82. if (buf_size - z->avail_out == 0)
  83. goto retry;
  84. return buf_size - z->avail_out;
  85. }
  86. #endif
  87. static int swf_read_header(AVFormatContext *s)
  88. {
  89. SWFContext *swf = s->priv_data;
  90. AVIOContext *pb = s->pb;
  91. int nbits, len, tag;
  92. tag = avio_rb32(pb) & 0xffffff00;
  93. avio_rl32(pb);
  94. if (tag == MKBETAG('C', 'W', 'S', 0)) {
  95. av_log(s, AV_LOG_INFO, "Compressed SWF file detected\n");
  96. #if CONFIG_ZLIB
  97. if (inflateInit(&swf->zstream) != Z_OK) {
  98. av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
  99. return AVERROR(EINVAL);
  100. }
  101. swf->zbuf_in = av_malloc(ZBUF_SIZE);
  102. swf->zbuf_out = av_malloc(ZBUF_SIZE);
  103. swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
  104. zlib_refill, NULL, NULL);
  105. if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb) {
  106. av_freep(&swf->zbuf_in);
  107. av_freep(&swf->zbuf_out);
  108. av_freep(&swf->zpb);
  109. return AVERROR(ENOMEM);
  110. }
  111. swf->zpb->seekable = 0;
  112. pb = swf->zpb;
  113. #else
  114. av_log(s, AV_LOG_ERROR, "missing zlib support, unable to open\n");
  115. return AVERROR(EIO);
  116. #endif
  117. } else if (tag != MKBETAG('F', 'W', 'S', 0))
  118. return AVERROR(EIO);
  119. /* skip rectangle size */
  120. nbits = avio_r8(pb) >> 3;
  121. len = (4 * nbits - 3 + 7) / 8;
  122. avio_skip(pb, len);
  123. swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
  124. avio_rl16(pb); /* frame count */
  125. swf->samples_per_frame = 0;
  126. s->ctx_flags |= AVFMTCTX_NOHEADER;
  127. return 0;
  128. }
  129. static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
  130. {
  131. SWFContext *swf = s->priv_data;
  132. AVIOContext *pb = s->pb;
  133. AVStream *vst = NULL, *ast = NULL, *st = 0;
  134. int tag, len, i, frame, v, res;
  135. #if CONFIG_ZLIB
  136. if (swf->zpb)
  137. pb = swf->zpb;
  138. #endif
  139. for(;;) {
  140. uint64_t pos = avio_tell(pb);
  141. tag = get_swf_tag(pb, &len);
  142. if (tag < 0)
  143. return AVERROR(EIO);
  144. if (len < 0) {
  145. av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
  146. return AVERROR_INVALIDDATA;
  147. }
  148. if (tag == TAG_VIDEOSTREAM) {
  149. int ch_id = avio_rl16(pb);
  150. len -= 2;
  151. for (i=0; i<s->nb_streams; i++) {
  152. st = s->streams[i];
  153. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
  154. goto skip;
  155. }
  156. avio_rl16(pb);
  157. avio_rl16(pb);
  158. avio_rl16(pb);
  159. avio_r8(pb);
  160. /* Check for FLV1 */
  161. vst = avformat_new_stream(s, NULL);
  162. if (!vst)
  163. return -1;
  164. vst->id = ch_id;
  165. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  166. vst->codecpar->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
  167. avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
  168. len -= 8;
  169. } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
  170. /* streaming found */
  171. int sample_rate_code;
  172. for (i=0; i<s->nb_streams; i++) {
  173. st = s->streams[i];
  174. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
  175. goto skip;
  176. }
  177. avio_r8(pb);
  178. v = avio_r8(pb);
  179. swf->samples_per_frame = avio_rl16(pb);
  180. ast = avformat_new_stream(s, NULL);
  181. if (!ast)
  182. return -1;
  183. ast->id = -1; /* -1 to avoid clash with video stream ch_id */
  184. if (v & 1) {
  185. ast->codecpar->channels = 2;
  186. ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  187. } else {
  188. ast->codecpar->channels = 1;
  189. ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  190. }
  191. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  192. ast->codecpar->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
  193. ast->need_parsing = AVSTREAM_PARSE_FULL;
  194. sample_rate_code= (v>>2) & 3;
  195. ast->codecpar->sample_rate = 44100 >> (3 - sample_rate_code);
  196. avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
  197. len -= 4;
  198. } else if (tag == TAG_VIDEOFRAME) {
  199. int ch_id = avio_rl16(pb);
  200. len -= 2;
  201. for(i=0; i<s->nb_streams; i++) {
  202. st = s->streams[i];
  203. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
  204. frame = avio_rl16(pb);
  205. len -= 2;
  206. if (len <= 0)
  207. goto skip;
  208. if ((res = av_get_packet(pb, pkt, len)) < 0)
  209. return res;
  210. pkt->pos = pos;
  211. pkt->pts = frame;
  212. pkt->stream_index = st->index;
  213. return pkt->size;
  214. }
  215. }
  216. } else if (tag == TAG_STREAMBLOCK) {
  217. for (i = 0; i < s->nb_streams; i++) {
  218. st = s->streams[i];
  219. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
  220. if (st->codecpar->codec_id == AV_CODEC_ID_MP3) {
  221. avio_skip(pb, 4);
  222. len -= 4;
  223. if (len <= 0)
  224. goto skip;
  225. if ((res = av_get_packet(pb, pkt, len)) < 0)
  226. return res;
  227. } else { // ADPCM, PCM
  228. if (len <= 0)
  229. goto skip;
  230. if ((res = av_get_packet(pb, pkt, len)) < 0)
  231. return res;
  232. }
  233. pkt->pos = pos;
  234. pkt->stream_index = st->index;
  235. return pkt->size;
  236. }
  237. }
  238. } else if (tag == TAG_JPEG2) {
  239. for (i=0; i<s->nb_streams; i++) {
  240. st = s->streams[i];
  241. if (st->codecpar->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
  242. break;
  243. }
  244. if (i == s->nb_streams) {
  245. vst = avformat_new_stream(s, NULL);
  246. if (!vst)
  247. return -1;
  248. vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
  249. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  250. vst->codecpar->codec_id = AV_CODEC_ID_MJPEG;
  251. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  252. st = vst;
  253. }
  254. avio_rl16(pb); /* BITMAP_ID */
  255. len -= 2;
  256. if (len < 4)
  257. goto skip;
  258. if ((res = av_new_packet(pkt, len)) < 0)
  259. return res;
  260. avio_read(pb, pkt->data, 4);
  261. if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
  262. AV_RB32(pkt->data) == 0xffd9ffd8) {
  263. /* old SWF files containing SOI/EOI as data start */
  264. /* files created by swink have reversed tag */
  265. pkt->size -= 4;
  266. avio_read(pb, pkt->data, pkt->size);
  267. } else {
  268. avio_read(pb, pkt->data + 4, pkt->size - 4);
  269. }
  270. pkt->pos = pos;
  271. pkt->stream_index = st->index;
  272. return pkt->size;
  273. }
  274. skip:
  275. len = FFMAX(0, len);
  276. avio_skip(pb, len);
  277. }
  278. }
  279. #if CONFIG_ZLIB
  280. static av_cold int swf_read_close(AVFormatContext *avctx)
  281. {
  282. SWFContext *s = avctx->priv_data;
  283. inflateEnd(&s->zstream);
  284. av_freep(&s->zbuf_in);
  285. av_freep(&s->zbuf_out);
  286. avio_context_free(&s->zpb);
  287. return 0;
  288. }
  289. #endif
  290. AVInputFormat ff_swf_demuxer = {
  291. .name = "swf",
  292. .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
  293. .priv_data_size = sizeof(SWFContext),
  294. .read_probe = swf_probe,
  295. .read_header = swf_read_header,
  296. .read_packet = swf_read_packet,
  297. #if CONFIG_ZLIB
  298. .read_close = swf_read_close,
  299. #endif
  300. };