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.

251 lines
8.4KB

  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 "libavutil/channel_layout.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "swf.h"
  25. static const AVCodecTag swf_audio_codec_tags[] = {
  26. { AV_CODEC_ID_PCM_S16LE, 0x00 },
  27. { AV_CODEC_ID_ADPCM_SWF, 0x01 },
  28. { AV_CODEC_ID_MP3, 0x02 },
  29. { AV_CODEC_ID_PCM_S16LE, 0x03 },
  30. // { AV_CODEC_ID_NELLYMOSER, 0x06 },
  31. { AV_CODEC_ID_NONE, 0 },
  32. };
  33. static int get_swf_tag(AVIOContext *pb, int *len_ptr)
  34. {
  35. int tag, len;
  36. if (pb->eof_reached)
  37. return -1;
  38. tag = avio_rl16(pb);
  39. len = tag & 0x3f;
  40. tag = tag >> 6;
  41. if (len == 0x3f) {
  42. len = avio_rl32(pb);
  43. }
  44. *len_ptr = len;
  45. return tag;
  46. }
  47. static int swf_probe(AVProbeData *p)
  48. {
  49. /* check file header */
  50. if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
  51. p->buf[2] == 'S')
  52. return AVPROBE_SCORE_MAX;
  53. else
  54. return 0;
  55. }
  56. static int swf_read_header(AVFormatContext *s)
  57. {
  58. SWFContext *swf = s->priv_data;
  59. AVIOContext *pb = s->pb;
  60. int nbits, len, tag;
  61. tag = avio_rb32(pb) & 0xffffff00;
  62. if (tag == MKBETAG('C', 'W', 'S', 0)) {
  63. av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
  64. return AVERROR(EIO);
  65. }
  66. if (tag != MKBETAG('F', 'W', 'S', 0))
  67. return AVERROR(EIO);
  68. avio_rl32(pb);
  69. /* skip rectangle size */
  70. nbits = avio_r8(pb) >> 3;
  71. len = (4 * nbits - 3 + 7) / 8;
  72. avio_skip(pb, len);
  73. swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
  74. avio_rl16(pb); /* frame count */
  75. swf->samples_per_frame = 0;
  76. s->ctx_flags |= AVFMTCTX_NOHEADER;
  77. return 0;
  78. }
  79. static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
  80. {
  81. SWFContext *swf = s->priv_data;
  82. AVIOContext *pb = s->pb;
  83. AVStream *vst = NULL, *ast = NULL, *st = 0;
  84. int tag, len, i, frame, v, res;
  85. for(;;) {
  86. uint64_t pos = avio_tell(pb);
  87. tag = get_swf_tag(pb, &len);
  88. if (tag < 0)
  89. return AVERROR(EIO);
  90. if (len < 0) {
  91. av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
  92. return AVERROR_INVALIDDATA;
  93. }
  94. if (tag == TAG_VIDEOSTREAM) {
  95. int ch_id = avio_rl16(pb);
  96. len -= 2;
  97. for (i=0; i<s->nb_streams; i++) {
  98. st = s->streams[i];
  99. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
  100. goto skip;
  101. }
  102. avio_rl16(pb);
  103. avio_rl16(pb);
  104. avio_rl16(pb);
  105. avio_r8(pb);
  106. /* Check for FLV1 */
  107. vst = avformat_new_stream(s, NULL);
  108. if (!vst)
  109. return -1;
  110. vst->id = ch_id;
  111. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  112. vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
  113. avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
  114. len -= 8;
  115. } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
  116. /* streaming found */
  117. int sample_rate_code;
  118. for (i=0; i<s->nb_streams; i++) {
  119. st = s->streams[i];
  120. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
  121. goto skip;
  122. }
  123. avio_r8(pb);
  124. v = avio_r8(pb);
  125. swf->samples_per_frame = avio_rl16(pb);
  126. ast = avformat_new_stream(s, NULL);
  127. if (!ast)
  128. return -1;
  129. ast->id = -1; /* -1 to avoid clash with video stream ch_id */
  130. if (v & 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, (v>>4) & 15);
  139. ast->need_parsing = AVSTREAM_PARSE_FULL;
  140. sample_rate_code= (v>>2) & 3;
  141. ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
  142. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  143. len -= 4;
  144. } else if (tag == TAG_VIDEOFRAME) {
  145. int ch_id = avio_rl16(pb);
  146. len -= 2;
  147. for(i=0; i<s->nb_streams; i++) {
  148. st = s->streams[i];
  149. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
  150. frame = avio_rl16(pb);
  151. len -= 2;
  152. if (len <= 0)
  153. goto skip;
  154. if ((res = av_get_packet(pb, pkt, len)) < 0)
  155. return res;
  156. pkt->pos = pos;
  157. pkt->pts = frame;
  158. pkt->stream_index = st->index;
  159. return pkt->size;
  160. }
  161. }
  162. } else if (tag == TAG_STREAMBLOCK) {
  163. for (i = 0; i < s->nb_streams; i++) {
  164. st = s->streams[i];
  165. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
  166. if (st->codec->codec_id == AV_CODEC_ID_MP3) {
  167. avio_skip(pb, 4);
  168. len -= 4;
  169. if (len <= 0)
  170. goto skip;
  171. if ((res = av_get_packet(pb, pkt, len)) < 0)
  172. return res;
  173. } else { // ADPCM, PCM
  174. if (len <= 0)
  175. goto skip;
  176. if ((res = av_get_packet(pb, pkt, len)) < 0)
  177. return res;
  178. }
  179. pkt->pos = pos;
  180. pkt->stream_index = st->index;
  181. return pkt->size;
  182. }
  183. }
  184. } else if (tag == TAG_JPEG2) {
  185. for (i=0; i<s->nb_streams; i++) {
  186. st = s->streams[i];
  187. if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
  188. break;
  189. }
  190. if (i == s->nb_streams) {
  191. vst = avformat_new_stream(s, NULL);
  192. if (!vst)
  193. return -1;
  194. vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
  195. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  196. vst->codec->codec_id = AV_CODEC_ID_MJPEG;
  197. avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
  198. st = vst;
  199. }
  200. avio_rl16(pb); /* BITMAP_ID */
  201. len -= 2;
  202. if (len < 4)
  203. goto skip;
  204. if ((res = av_new_packet(pkt, len)) < 0)
  205. return res;
  206. avio_read(pb, pkt->data, 4);
  207. if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
  208. AV_RB32(pkt->data) == 0xffd9ffd8) {
  209. /* old SWF files containing SOI/EOI as data start */
  210. /* files created by swink have reversed tag */
  211. pkt->size -= 4;
  212. avio_read(pb, pkt->data, pkt->size);
  213. } else {
  214. avio_read(pb, pkt->data + 4, pkt->size - 4);
  215. }
  216. pkt->pos = pos;
  217. pkt->stream_index = st->index;
  218. return pkt->size;
  219. }
  220. skip:
  221. len = FFMAX(0, len);
  222. avio_skip(pb, len);
  223. }
  224. }
  225. AVInputFormat ff_swf_demuxer = {
  226. .name = "swf",
  227. .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
  228. .priv_data_size = sizeof(SWFContext),
  229. .read_probe = swf_probe,
  230. .read_header = swf_read_header,
  231. .read_packet = swf_read_packet,
  232. };