libavformat/swfdec.c
Go to the documentation of this file.
00001 /*
00002  * Flash Compatible Streaming Format demuxer
00003  * Copyright (c) 2000 Fabrice Bellard
00004  * Copyright (c) 2003 Tinic Uro
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "libavutil/intreadwrite.h"
00024 #include "swf.h"
00025 
00026 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
00027 {
00028     int tag, len;
00029 
00030     if (pb->eof_reached)
00031         return -1;
00032 
00033     tag = avio_rl16(pb);
00034     len = tag & 0x3f;
00035     tag = tag >> 6;
00036     if (len == 0x3f) {
00037         len = avio_rl32(pb);
00038     }
00039 //    av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
00040     *len_ptr = len;
00041     return tag;
00042 }
00043 
00044 
00045 static int swf_probe(AVProbeData *p)
00046 {
00047     /* check file header */
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     AVIOContext *pb = s->pb;
00059     int nbits, len, tag;
00060 
00061     tag = avio_rb32(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     avio_rl32(pb);
00070     /* skip rectangle size */
00071     nbits = avio_r8(pb) >> 3;
00072     len = (4 * nbits - 3 + 7) / 8;
00073     avio_skip(pb, len);
00074     swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
00075     avio_rl16(pb); /* frame count */
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     AVIOContext *pb = s->pb;
00086     AVStream *vst = NULL, *ast = NULL, *st = 0;
00087     int tag, len, i, frame, v, res;
00088 
00089     for(;;) {
00090         uint64_t pos = avio_tell(pb);
00091         tag = get_swf_tag(pb, &len);
00092         if (tag < 0)
00093             return AVERROR(EIO);
00094         if (len < 0) {
00095             av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
00096             return AVERROR_INVALIDDATA;
00097         }
00098         if (tag == TAG_VIDEOSTREAM) {
00099             int ch_id = avio_rl16(pb);
00100             len -= 2;
00101 
00102             for (i=0; i<s->nb_streams; i++) {
00103                 st = s->streams[i];
00104                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
00105                     goto skip;
00106             }
00107 
00108             avio_rl16(pb);
00109             avio_rl16(pb);
00110             avio_rl16(pb);
00111             avio_r8(pb);
00112             /* Check for FLV1 */
00113             vst = avformat_new_stream(s, NULL);
00114             if (!vst)
00115                 return -1;
00116             vst->id = ch_id;
00117             vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00118             vst->codec->codec_id = ff_codec_get_id(swf_codec_tags, avio_r8(pb));
00119             avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
00120             vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
00121             len -= 8;
00122         } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
00123             /* streaming found */
00124             int sample_rate_code;
00125 
00126             for (i=0; i<s->nb_streams; i++) {
00127                 st = s->streams[i];
00128                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
00129                     goto skip;
00130             }
00131 
00132             avio_r8(pb);
00133             v = avio_r8(pb);
00134             swf->samples_per_frame = avio_rl16(pb);
00135             ast = avformat_new_stream(s, NULL);
00136             if (!ast)
00137                 return -1;
00138             ast->id = -1; /* -1 to avoid clash with video stream ch_id */
00139             ast->codec->channels = 1 + (v&1);
00140             ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00141             ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
00142             ast->need_parsing = AVSTREAM_PARSE_FULL;
00143             sample_rate_code= (v>>2) & 3;
00144             if (!sample_rate_code)
00145                 ast->codec->sample_rate = 5512;
00146             else
00147                 ast->codec->sample_rate = 11025 << (sample_rate_code-1);
00148             avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
00149             len -= 4;
00150         } else if (tag == TAG_VIDEOFRAME) {
00151             int ch_id = avio_rl16(pb);
00152             len -= 2;
00153             for(i=0; i<s->nb_streams; i++) {
00154                 st = s->streams[i];
00155                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
00156                     frame = avio_rl16(pb);
00157                     len -= 2;
00158                     if (len <= 0)
00159                         goto skip;
00160                     if ((res = av_get_packet(pb, pkt, len)) < 0)
00161                         return res;
00162                     pkt->pos = pos;
00163                     pkt->pts = frame;
00164                     pkt->stream_index = st->index;
00165                     return pkt->size;
00166                 }
00167             }
00168         } else if (tag == TAG_STREAMBLOCK) {
00169             for (i = 0; i < s->nb_streams; i++) {
00170                 st = s->streams[i];
00171                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
00172             if (st->codec->codec_id == CODEC_ID_MP3) {
00173                 avio_skip(pb, 4);
00174                 len -= 4;
00175                 if (len <= 0)
00176                     goto skip;
00177                 if ((res = av_get_packet(pb, pkt, len)) < 0)
00178                     return res;
00179             } else { // ADPCM, PCM
00180                 if (len <= 0)
00181                     goto skip;
00182                 if ((res = av_get_packet(pb, pkt, len)) < 0)
00183                     return res;
00184             }
00185             pkt->pos = pos;
00186             pkt->stream_index = st->index;
00187             return pkt->size;
00188                 }
00189             }
00190         } else if (tag == TAG_JPEG2) {
00191             for (i=0; i<s->nb_streams; i++) {
00192                 st = s->streams[i];
00193                 if (st->codec->codec_id == CODEC_ID_MJPEG && st->id == -2)
00194                     break;
00195             }
00196             if (i == s->nb_streams) {
00197                 vst = avformat_new_stream(s, NULL);
00198                 if (!vst)
00199                     return -1;
00200                 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
00201                 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00202                 vst->codec->codec_id = CODEC_ID_MJPEG;
00203                 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
00204                 vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
00205                 st = vst;
00206             }
00207             avio_rl16(pb); /* BITMAP_ID */
00208             len -= 2;
00209             if (len < 4)
00210                 goto skip;
00211             if ((res = av_new_packet(pkt, len)) < 0)
00212                 return res;
00213             avio_read(pb, pkt->data, 4);
00214             if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
00215                 AV_RB32(pkt->data) == 0xffd9ffd8) {
00216                 /* old SWF files containing SOI/EOI as data start */
00217                 /* files created by swink have reversed tag */
00218                 pkt->size -= 4;
00219                 avio_read(pb, pkt->data, pkt->size);
00220             } else {
00221                 avio_read(pb, pkt->data + 4, pkt->size - 4);
00222             }
00223             pkt->pos = pos;
00224             pkt->stream_index = st->index;
00225             return pkt->size;
00226         }
00227     skip:
00228         len = FFMAX(0, len);
00229         avio_skip(pb, len);
00230     }
00231 }
00232 
00233 AVInputFormat ff_swf_demuxer = {
00234     .name           = "swf",
00235     .long_name      = NULL_IF_CONFIG_SMALL("Flash format"),
00236     .priv_data_size = sizeof(SWFContext),
00237     .read_probe     = swf_probe,
00238     .read_header    = swf_read_header,
00239     .read_packet    = swf_read_packet,
00240 };