libavformat/filmstripdec.c
83ab7f18
 /*
  * Adobe Filmstrip demuxer
  * Copyright (c) 2010 Peter Ross
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
83ab7f18
  * Adobe Filmstrip demuxer
  */
 
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
83ab7f18
 
 #define RAND_TAG MKBETAG('R','a','n','d')
 
 typedef struct {
     int leading;
 } FilmstripDemuxContext;
 
6e9651d1
 static int read_header(AVFormatContext *s)
83ab7f18
 {
     FilmstripDemuxContext *film = s->priv_data;
471fe57e
     AVIOContext *pb = s->pb;
83ab7f18
     AVStream *st;
 
8978feda
     if (!s->pb->seekable)
83ab7f18
         return AVERROR(EIO);
 
db44ea96
     avio_seek(pb, avio_size(pb) - 36, SEEK_SET);
e63a3628
     if (avio_rb32(pb) != RAND_TAG) {
d2b927d4
         av_log(s, AV_LOG_ERROR, "magic number not found\n");
83ab7f18
         return AVERROR_INVALIDDATA;
     }
 
3b3bbdd3
     st = avformat_new_stream(s, NULL);
83ab7f18
     if (!st)
         return AVERROR(ENOMEM);
 
e63a3628
     st->nb_frames = avio_rb32(pb);
     if (avio_rb16(pb) != 0) {
1ecdf891
         avpriv_request_sample(s, "Unsupported packing method");
f3298f12
         return AVERROR_PATCHWELCOME;
83ab7f18
     }
 
45a8a02a
     avio_skip(pb, 2);
72415b2a
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
36ef5369
     st->codec->codec_id   = AV_CODEC_ID_RAWVIDEO;
716d413c
     st->codec->pix_fmt    = AV_PIX_FMT_RGBA;
83ab7f18
     st->codec->codec_tag  = 0; /* no fourcc */
e63a3628
     st->codec->width      = avio_rb16(pb);
     st->codec->height     = avio_rb16(pb);
     film->leading         = avio_rb16(pb);
c3f9ebf7
     avpriv_set_pts_info(st, 64, 1, avio_rb16(pb));
83ab7f18
 
f59d8ff8
     avio_seek(pb, 0, SEEK_SET);
83ab7f18
 
     return 0;
 }
 
 static int read_packet(AVFormatContext *s,
                        AVPacket *pkt)
 {
     FilmstripDemuxContext *film = s->priv_data;
     AVStream *st = s->streams[0];
 
     if (url_feof(s->pb))
         return AVERROR(EIO);
384c9c2f
     pkt->dts = avio_tell(s->pb) / (st->codec->width * (st->codec->height + film->leading) * 4);
83ab7f18
     pkt->size = av_get_packet(s->pb, pkt, st->codec->width * st->codec->height * 4);
45a8a02a
     avio_skip(s->pb, st->codec->width * film->leading * 4);
83ab7f18
     if (pkt->size < 0)
         return pkt->size;
cc947f04
     pkt->flags |= AV_PKT_FLAG_KEY;
83ab7f18
     return 0;
 }
 
 static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
 {
     AVStream *st = s->streams[stream_index];
58540d7b
     if (avio_seek(s->pb, FFMAX(timestamp, 0) * st->codec->width * st->codec->height * 4, SEEK_SET) < 0)
         return -1;
83ab7f18
     return 0;
 }
 
66355be3
 AVInputFormat ff_filmstrip_demuxer = {
dfc2c4d9
     .name           = "filmstrip",
     .long_name      = NULL_IF_CONFIG_SMALL("Adobe Filmstrip"),
     .priv_data_size = sizeof(FilmstripDemuxContext),
     .read_header    = read_header,
     .read_packet    = read_packet,
     .read_seek      = read_seek,
20234a4b
     .extensions     = "flm",
83ab7f18
 };