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.

228 lines
7.5KB

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