00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavutil/intreadwrite.h"
00024 #include "swf.h"
00025
00026 static int get_swf_tag(ByteIOContext *pb, int *len_ptr)
00027 {
00028 int tag, len;
00029
00030 if (url_feof(pb))
00031 return -1;
00032
00033 tag = get_le16(pb);
00034 len = tag & 0x3f;
00035 tag = tag >> 6;
00036 if (len == 0x3f) {
00037 len = get_le32(pb);
00038 }
00039
00040 *len_ptr = len;
00041 return tag;
00042 }
00043
00044
00045 static int swf_probe(AVProbeData *p)
00046 {
00047
00048 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
00049 p->buf[2] == 'S')
00050 return AVPROBE_SCORE_MAX;
00051 else
00052 return 0;
00053 }
00054
00055 static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
00056 {
00057 SWFContext *swf = s->priv_data;
00058 ByteIOContext *pb = s->pb;
00059 int nbits, len, tag;
00060
00061 tag = get_be32(pb) & 0xffffff00;
00062
00063 if (tag == MKBETAG('C', 'W', 'S', 0)) {
00064 av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
00065 return AVERROR(EIO);
00066 }
00067 if (tag != MKBETAG('F', 'W', 'S', 0))
00068 return AVERROR(EIO);
00069 get_le32(pb);
00070
00071 nbits = get_byte(pb) >> 3;
00072 len = (4 * nbits - 3 + 7) / 8;
00073 url_fskip(pb, len);
00074 swf->frame_rate = get_le16(pb);
00075 get_le16(pb);
00076
00077 swf->samples_per_frame = 0;
00078 s->ctx_flags |= AVFMTCTX_NOHEADER;
00079 return 0;
00080 }
00081
00082 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
00083 {
00084 SWFContext *swf = s->priv_data;
00085 ByteIOContext *pb = s->pb;
00086 AVStream *vst = NULL, *ast = NULL, *st = 0;
00087 int tag, len, i, frame, v;
00088
00089 for(;;) {
00090 tag = get_swf_tag(pb, &len);
00091 if (tag < 0)
00092 return AVERROR(EIO);
00093 if (tag == TAG_VIDEOSTREAM && !vst) {
00094 int ch_id = get_le16(pb);
00095 get_le16(pb);
00096 get_le16(pb);
00097 get_le16(pb);
00098 get_byte(pb);
00099
00100 vst = av_new_stream(s, ch_id);
00101 if (!vst)
00102 return -1;
00103 vst->codec->codec_type = CODEC_TYPE_VIDEO;
00104 vst->codec->codec_id = codec_get_id(swf_codec_tags, get_byte(pb));
00105 av_set_pts_info(vst, 64, 256, swf->frame_rate);
00106 vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
00107 len -= 10;
00108 } else if ((tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) && !ast) {
00109
00110 int sample_rate_code;
00111 get_byte(pb);
00112 v = get_byte(pb);
00113 swf->samples_per_frame = get_le16(pb);
00114 ast = av_new_stream(s, -1);
00115 if (!ast)
00116 return -1;
00117 swf->audio_stream_index = ast->index;
00118 ast->codec->channels = 1 + (v&1);
00119 ast->codec->codec_type = CODEC_TYPE_AUDIO;
00120 ast->codec->codec_id = codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
00121 ast->need_parsing = AVSTREAM_PARSE_FULL;
00122 sample_rate_code= (v>>2) & 3;
00123 if (!sample_rate_code)
00124 return AVERROR(EIO);
00125 ast->codec->sample_rate = 11025 << (sample_rate_code-1);
00126 av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
00127 len -= 4;
00128 } else if (tag == TAG_VIDEOFRAME) {
00129 int ch_id = get_le16(pb);
00130 len -= 2;
00131 for(i=0; i<s->nb_streams; i++) {
00132 st = s->streams[i];
00133 if (st->codec->codec_type == CODEC_TYPE_VIDEO && st->id == ch_id) {
00134 frame = get_le16(pb);
00135 av_get_packet(pb, pkt, len-2);
00136 pkt->pts = frame;
00137 pkt->stream_index = st->index;
00138 return pkt->size;
00139 }
00140 }
00141 } else if (tag == TAG_STREAMBLOCK) {
00142 st = s->streams[swf->audio_stream_index];
00143 if (st->codec->codec_id == CODEC_ID_MP3) {
00144 url_fskip(pb, 4);
00145 av_get_packet(pb, pkt, len-4);
00146 } else {
00147 av_get_packet(pb, pkt, len);
00148 }
00149 pkt->stream_index = st->index;
00150 return pkt->size;
00151 } else if (tag == TAG_JPEG2) {
00152 for (i=0; i<s->nb_streams; i++) {
00153 st = s->streams[i];
00154 if (st->id == -2)
00155 break;
00156 }
00157 if (i == s->nb_streams) {
00158 vst = av_new_stream(s, -2);
00159 if (!vst)
00160 return -1;
00161 vst->codec->codec_type = CODEC_TYPE_VIDEO;
00162 vst->codec->codec_id = CODEC_ID_MJPEG;
00163 av_set_pts_info(vst, 64, 256, swf->frame_rate);
00164 vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
00165 st = vst;
00166 }
00167 get_le16(pb);
00168 av_new_packet(pkt, len-2);
00169 get_buffer(pb, pkt->data, 4);
00170 if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
00171 AV_RB32(pkt->data) == 0xffd9ffd8) {
00172
00173
00174 pkt->size -= 4;
00175 get_buffer(pb, pkt->data, pkt->size);
00176 } else {
00177 get_buffer(pb, pkt->data + 4, pkt->size - 4);
00178 }
00179 pkt->stream_index = st->index;
00180 return pkt->size;
00181 }
00182 url_fskip(pb, len);
00183 }
00184 return 0;
00185 }
00186
00187 AVInputFormat swf_demuxer = {
00188 "swf",
00189 NULL_IF_CONFIG_SMALL("Flash format"),
00190 sizeof(SWFContext),
00191 swf_probe,
00192 swf_read_header,
00193 swf_read_packet,
00194 };