libavformat/filmstripdec.c
Go to the documentation of this file.
00001 /*
00002  * Adobe Filmstrip demuxer
00003  * Copyright (c) 2010 Peter Ross
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "libavutil/intreadwrite.h"
00028 #include "avformat.h"
00029 #include "internal.h"
00030 
00031 #define RAND_TAG MKBETAG('R','a','n','d')
00032 
00033 typedef struct {
00034     int leading;
00035 } FilmstripDemuxContext;
00036 
00037 static int read_header(AVFormatContext *s,
00038                        AVFormatParameters *ap)
00039 {
00040     FilmstripDemuxContext *film = s->priv_data;
00041     AVIOContext *pb = s->pb;
00042     AVStream *st;
00043 
00044     if (!s->pb->seekable)
00045         return AVERROR(EIO);
00046 
00047     avio_seek(pb, avio_size(pb) - 36, SEEK_SET);
00048     if (avio_rb32(pb) != RAND_TAG) {
00049         av_log(s, AV_LOG_ERROR, "magic number not found");
00050         return AVERROR_INVALIDDATA;
00051     }
00052 
00053     st = avformat_new_stream(s, NULL);
00054     if (!st)
00055         return AVERROR(ENOMEM);
00056 
00057     st->nb_frames = avio_rb32(pb);
00058     if (avio_rb16(pb) != 0) {
00059         av_log_ask_for_sample(s, "unsupported packing method\n");
00060         return AVERROR_INVALIDDATA;
00061     }
00062 
00063     avio_skip(pb, 2);
00064     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00065     st->codec->codec_id   = CODEC_ID_RAWVIDEO;
00066     st->codec->pix_fmt    = PIX_FMT_RGBA;
00067     st->codec->codec_tag  = 0; /* no fourcc */
00068     st->codec->width      = avio_rb16(pb);
00069     st->codec->height     = avio_rb16(pb);
00070     film->leading         = avio_rb16(pb);
00071     avpriv_set_pts_info(st, 64, 1, avio_rb16(pb));
00072 
00073     avio_seek(pb, 0, SEEK_SET);
00074 
00075     return 0;
00076 }
00077 
00078 static int read_packet(AVFormatContext *s,
00079                        AVPacket *pkt)
00080 {
00081     FilmstripDemuxContext *film = s->priv_data;
00082     AVStream *st = s->streams[0];
00083 
00084     if (s->pb->eof_reached)
00085         return AVERROR(EIO);
00086     pkt->dts = avio_tell(s->pb) / (st->codec->width * (st->codec->height + film->leading) * 4);
00087     pkt->size = av_get_packet(s->pb, pkt, st->codec->width * st->codec->height * 4);
00088     avio_skip(s->pb, st->codec->width * film->leading * 4);
00089     if (pkt->size < 0)
00090         return pkt->size;
00091     pkt->flags |= AV_PKT_FLAG_KEY;
00092     return 0;
00093 }
00094 
00095 static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00096 {
00097     AVStream *st = s->streams[stream_index];
00098     avio_seek(s->pb, FFMAX(timestamp, 0) * st->codec->width * st->codec->height * 4, SEEK_SET);
00099     return 0;
00100 }
00101 
00102 AVInputFormat ff_filmstrip_demuxer = {
00103     .name           = "filmstrip",
00104     .long_name      = NULL_IF_CONFIG_SMALL("Adobe Filmstrip"),
00105     .priv_data_size = sizeof(FilmstripDemuxContext),
00106     .read_header    = read_header,
00107     .read_packet    = read_packet,
00108     .read_seek      = read_seek,
00109     .extensions = "flm",
00110 };