filmstripdec.c
Go to the documentation of this file.
1 /*
2  * Adobe Filmstrip demuxer
3  * Copyright (c) 2010 Peter Ross
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "libavutil/intreadwrite.h"
28 #include "avformat.h"
29 #include "internal.h"
30 
31 #define RAND_TAG MKBETAG('R','a','n','d')
32 
33 typedef struct {
34  int leading;
36 
39 {
41  AVIOContext *pb = s->pb;
42  AVStream *st;
43 
44  if (!s->pb->seekable)
45  return AVERROR(EIO);
46 
47  avio_seek(pb, avio_size(pb) - 36, SEEK_SET);
48  if (avio_rb32(pb) != RAND_TAG) {
49  av_log(s, AV_LOG_ERROR, "magic number not found");
50  return AVERROR_INVALIDDATA;
51  }
52 
53  st = avformat_new_stream(s, NULL);
54  if (!st)
55  return AVERROR(ENOMEM);
56 
57  st->nb_frames = avio_rb32(pb);
58  if (avio_rb16(pb) != 0) {
59  av_log_ask_for_sample(s, "unsupported packing method\n");
60  return AVERROR_INVALIDDATA;
61  }
62 
63  avio_skip(pb, 2);
66  st->codec->pix_fmt = PIX_FMT_RGBA;
67  st->codec->codec_tag = 0; /* no fourcc */
68  st->codec->width = avio_rb16(pb);
69  st->codec->height = avio_rb16(pb);
70  film->leading = avio_rb16(pb);
71  avpriv_set_pts_info(st, 64, 1, avio_rb16(pb));
72 
73  avio_seek(pb, 0, SEEK_SET);
74 
75  return 0;
76 }
77 
79  AVPacket *pkt)
80 {
82  AVStream *st = s->streams[0];
83 
84  if (s->pb->eof_reached)
85  return AVERROR(EIO);
86  pkt->dts = avio_tell(s->pb) / (st->codec->width * (st->codec->height + film->leading) * 4);
87  pkt->size = av_get_packet(s->pb, pkt, st->codec->width * st->codec->height * 4);
88  avio_skip(s->pb, st->codec->width * film->leading * 4);
89  if (pkt->size < 0)
90  return pkt->size;
91  pkt->flags |= AV_PKT_FLAG_KEY;
92  return 0;
93 }
94 
95 static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
96 {
97  AVStream *st = s->streams[stream_index];
98  avio_seek(s->pb, FFMAX(timestamp, 0) * st->codec->width * st->codec->height * 4, SEEK_SET);
99  return 0;
100 }
101 
103  .name = "filmstrip",
104  .long_name = NULL_IF_CONFIG_SMALL("Adobe Filmstrip"),
105  .priv_data_size = sizeof(FilmstripDemuxContext),
108  .read_seek = read_seek,
109  .extensions = "flm",
110 };
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:281
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int size
Definition: avcodec.h:909
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:211
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:754
Format I/O context.
Definition: avformat.h:863
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:769
AVStream ** streams
Definition: avformat.h:908
static int flags
Definition: log.c:34
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:269
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
#define FFMAX(a, b)
Definition: common.h:53
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
AVCodecContext * codec
codec context
Definition: avformat.h:623
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:122
int width
picture width / height.
Definition: avcodec.h:1408
#define RAND_TAG
Definition: filmstripdec.c:31
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: filmstripdec.c:95
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: filmstripdec.c:78
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
AVIOContext * pb
Definition: avformat.h:896
void av_log_ask_for_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message asking for a sample.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
AVInputFormat ff_filmstrip_demuxer
Definition: filmstripdec.c:102
Main libavformat public API header.
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:701
static int read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: filmstripdec.c:37
int eof_reached
true if eof reached
Definition: avio.h:98
void * priv_data
Format private data.
Definition: avformat.h:883
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:907
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
enum CodecID codec_id
Definition: avcodec.h:1575