FFmpeg
anm.c
Go to the documentation of this file.
1 /*
2  * Deluxe Paint Animation demuxer
3  * Copyright (c) 2009 Peter Ross
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Deluxe Paint Animation demuxer
25  */
26 
27 #include "libavutil/intreadwrite.h"
28 #include "avformat.h"
29 #include "internal.h"
30 
31 typedef struct Page {
33  unsigned int nb_records;
34  int size;
35 } Page;
36 
37 typedef struct AnmDemuxContext {
38  unsigned int nb_pages; /**< total pages in file */
39  unsigned int nb_records; /**< total records in file */
41 #define MAX_PAGES 256 /**< Deluxe Paint hardcoded value */
42  Page pt[MAX_PAGES]; /**< page table */
43  int page; /**< current page (or AVERROR_xxx code) */
44  int record; /**< current record (with in page) */
46 
47 #define LPF_TAG MKTAG('L','P','F',' ')
48 #define ANIM_TAG MKTAG('A','N','I','M')
49 
50 static int probe(const AVProbeData *p)
51 {
52  /* verify tags and video dimensions */
53  if (AV_RL32(&p->buf[0]) == LPF_TAG &&
54  AV_RL32(&p->buf[16]) == ANIM_TAG &&
55  AV_RL16(&p->buf[20]) && AV_RL16(&p->buf[22]))
56  return AVPROBE_SCORE_MAX;
57  return 0;
58 }
59 
60 /**
61  * @return page containing the requested record or AVERROR_XXX
62  */
63 static int find_record(const AnmDemuxContext *anm, int record)
64 {
65  int i;
66 
67  if (record >= anm->nb_records)
68  return AVERROR_EOF;
69 
70  for (i = 0; i < MAX_PAGES; i++) {
71  const Page *p = &anm->pt[i];
72  if (p->nb_records > 0 && record >= p->base_record && record < p->base_record + p->nb_records)
73  return i;
74  }
75 
76  return AVERROR_INVALIDDATA;
77 }
78 
80 {
81  AnmDemuxContext *anm = s->priv_data;
82  AVIOContext *pb = s->pb;
83  AVStream *st;
84  int i, ret;
85 
86  avio_skip(pb, 4); /* magic number */
87  if (avio_rl16(pb) != MAX_PAGES) {
88  avpriv_request_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES));
89  return AVERROR_PATCHWELCOME;
90  }
91 
92  anm->nb_pages = avio_rl16(pb);
93  anm->nb_records = avio_rl32(pb);
94  avio_skip(pb, 2); /* max records per page */
95  anm->page_table_offset = avio_rl16(pb);
96  if (avio_rl32(pb) != ANIM_TAG)
97  return AVERROR_INVALIDDATA;
98 
99  /* video stream */
100  st = avformat_new_stream(s, NULL);
101  if (!st)
102  return AVERROR(ENOMEM);
105  st->codecpar->codec_tag = 0; /* no fourcc */
106  st->codecpar->width = avio_rl16(pb);
107  st->codecpar->height = avio_rl16(pb);
108  if (avio_r8(pb) != 0)
109  goto invalid;
110  avio_skip(pb, 1); /* frame rate multiplier info */
111 
112  /* ignore last delta record (used for looping) */
113  if (avio_r8(pb)) /* has_last_delta */
114  anm->nb_records = FFMAX(anm->nb_records - 1, 0);
115 
116  avio_skip(pb, 1); /* last_delta_valid */
117 
118  if (avio_r8(pb) != 0)
119  goto invalid;
120 
121  if (avio_r8(pb) != 1)
122  goto invalid;
123 
124  avio_skip(pb, 1); /* other recs per frame */
125 
126  if (avio_r8(pb) != 1)
127  goto invalid;
128 
129  avio_skip(pb, 32); /* record_types */
130  st->nb_frames = avio_rl32(pb);
131  avpriv_set_pts_info(st, 64, 1, avio_rl16(pb));
132  avio_skip(pb, 58);
133 
134  /* color cycling and palette data */
135  ret = ff_get_extradata(s, st->codecpar, s->pb, 16*8 + 4*256);
136  if (ret < 0)
137  return ret;
138 
139  /* read page table */
140  ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
141  if (ret < 0)
142  return ret;
143 
144  for (i = 0; i < MAX_PAGES; i++) {
145  Page *p = &anm->pt[i];
146  p->base_record = avio_rl16(pb);
147  p->nb_records = avio_rl16(pb);
148  p->size = avio_rl16(pb);
149  }
150 
151  /* find page of first frame */
152  anm->page = find_record(anm, 0);
153  if (anm->page < 0) {
154  return anm->page;
155  }
156 
157  anm->record = -1;
158  return 0;
159 
160 invalid:
161  avpriv_request_sample(s, "Invalid header element");
162  return AVERROR_PATCHWELCOME;
163 }
164 
166  AVPacket *pkt)
167 {
168  AnmDemuxContext *anm = s->priv_data;
169  AVIOContext *pb = s->pb;
170  Page *p;
171  int tmp, record_size;
172 
173  if (avio_feof(s->pb))
174  return AVERROR(EIO);
175 
176  if (anm->page < 0)
177  return anm->page;
178 
179 repeat:
180  p = &anm->pt[anm->page];
181 
182  /* parse page header */
183  if (anm->record < 0) {
184  avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
185  avio_skip(pb, 8 + 2*p->nb_records);
186  anm->record = 0;
187  }
188 
189  /* if we have fetched all records in this page, then find the
190  next page and repeat */
191  if (anm->record >= p->nb_records) {
192  anm->page = find_record(anm, p->base_record + p->nb_records);
193  if (anm->page < 0)
194  return anm->page;
195  anm->record = -1;
196  goto repeat;
197  }
198 
199  /* fetch record size */
200  tmp = avio_tell(pb);
201  avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
202  8 + anm->record * 2, SEEK_SET);
203  record_size = avio_rl16(pb);
204  avio_seek(pb, tmp, SEEK_SET);
205 
206  /* fetch record */
207  pkt->size = av_get_packet(s->pb, pkt, record_size);
208  if (pkt->size < 0)
209  return pkt->size;
210  if (p->base_record + anm->record == 0)
212 
213  anm->record++;
214  return 0;
215 }
216 
218  .name = "anm",
219  .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
220  .priv_data_size = sizeof(AnmDemuxContext),
221  .read_probe = probe,
224 };
AnmDemuxContext
Definition: anm.c:37
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
ff_get_extradata
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:469
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
read_header
static int read_header(AVFormatContext *s)
Definition: anm.c:79
AnmDemuxContext::pt
Page pt[MAX_PAGES]
page table
Definition: anm.c:42
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:428
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:504
Page::size
int size
Definition: anm.c:34
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
Page::base_record
int base_record
Definition: anm.c:32
MAX_PAGES
#define MAX_PAGES
Deluxe Paint hardcoded value.
Definition: anm.c:41
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:650
AnmDemuxContext::page_table_offset
int page_table_offset
Definition: anm.c:40
AnmDemuxContext::record
int record
current record (with in page)
Definition: anm.c:44
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AnmDemuxContext::page
int page
current page (or AVERROR_xxx code)
Definition: anm.c:43
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
find_record
static int find_record(const AnmDemuxContext *anm, int record)
Definition: anm.c:63
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
ANIM_TAG
#define ANIM_TAG
Definition: anm.c:48
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
AV_CODEC_ID_ANM
@ AV_CODEC_ID_ANM
Definition: codec_id.h:184
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:987
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: anm.c:165
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
Page
Definition: anm.c:31
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodecParameters::height
int height
Definition: codec_par.h:127
AV_STRINGIFY
#define AV_STRINGIFY(s)
Definition: macros.h:66
AnmDemuxContext::nb_pages
unsigned int nb_pages
total pages in file
Definition: anm.c:38
av_get_packet
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:197
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
ff_anm_demuxer
const AVInputFormat ff_anm_demuxer
Definition: anm.c:217
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:1196
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:347
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AnmDemuxContext::nb_records
unsigned int nb_records
total records in file
Definition: anm.c:39
Page::nb_records
unsigned int nb_records
Definition: anm.c:33
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
LPF_TAG
#define LPF_TAG
Definition: anm.c:47
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
probe
static int probe(const AVProbeData *p)
Definition: anm.c:50
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:375