FFmpeg
Loading...
Searching...
No Matches
imx.c
Go to the documentation of this file.
1/*
2 * Simbiosis game demuxer
3 *
4 * Copyright (C) 2021 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "avformat.h"
24#include "demux.h"
25#include "internal.h"
28#include "libavutil/internal.h"
29
30#define IMX_TAG MKTAG('I', 'M', 'A', 'X')
31
37
38static int simbiosis_imx_probe(const AVProbeData *p)
39{
40 if (AV_RL32(p->buf) != IMX_TAG)
41 return 0;
42 if (AV_RN32(p->buf+4) == 0)
43 return 0;
44 if (AV_RN16(p->buf+8) == 0)
45 return 0;
46 if (AV_RL16(p->buf+10) != 0x102)
47 return 0;
48
49 return AVPROBE_SCORE_EXTENSION + 10;
50}
51
53{
54 AVIOContext *pb = s->pb;
55 AVStream *vst, *ast;
56 int rate;
57
60 if (!vst || !ast)
61 return AVERROR(ENOMEM);
62
63 avio_skip(pb, 4);
64
66 vst->codecpar->codec_tag = 0;
69 vst->start_time = 0;
70 vst->duration =
71 vst->nb_frames = avio_rl32(pb);
72 rate = avio_rl16(pb);
73 avio_skip(pb, 12);
74
75 avpriv_set_pts_info(vst, 64, 1, rate);
76
78 ast->codecpar->codec_tag = 0;
81 ast->codecpar->sample_rate = 22050;
82 ast->start_time = 0;
83
84 avpriv_set_pts_info(ast, 64, 1, 22050);
85
86 return 0;
87}
88
90{
91 AVIOContext *pb = s->pb;
92 SimbiosisIMXDemuxContext *imx = s->priv_data;
93 uint32_t chunk_size, chunk_type;
94 int64_t pos = avio_tell(pb);
95 int ret, idx = -1;
96
97retry:
98 if (avio_feof(pb))
99 return AVERROR_EOF;
100
101 chunk_size = avio_rl32(pb);
102 chunk_type = avio_rl32(pb);
103
104 switch (chunk_type) {
105 case 0xAAFF:
106 return AVERROR_EOF;
107 case 0xAA99:
108 idx = 1;
109 break;
110 case 0xAA97:
111 idx = 0;
112 if (!imx->first_video_packet_pos)
114 break;
115 case 0xAA98:
116 if (chunk_size > 256 * 3)
117 return AVERROR_INVALIDDATA;
118 for (int i = 0; i < chunk_size / 3; i++) {
119 unsigned r = avio_r8(pb) << 18;
120 unsigned g = avio_r8(pb) << 10;
121 unsigned b = avio_r8(pb) << 2;
122
123 AV_WL32(imx->pal + i * 4, (0xFFU << 24) | r | g | b);
124 }
125 imx->pal_changed = 1;
126 idx = -1;
127 break;
128 default:
129 return AVERROR_INVALIDDATA;
130 }
131
132 if (idx == -1)
133 goto retry;
134
135 ret = av_get_packet(pb, pkt, chunk_size);
136 if (ret < 0)
137 return ret;
138
139 if (imx->pal_changed && idx == 0) {
142 if (!pal)
143 return AVERROR(ENOMEM);
144 memcpy(pal, imx->pal, AVPALETTE_SIZE);
145 imx->pal_changed = 0;
146 if (pos <= imx->first_video_packet_pos)
147 pkt->flags |= AV_PKT_FLAG_KEY;
148 } else if (idx == 1) {
149 pkt->flags |= AV_PKT_FLAG_KEY;
150 }
151
152 pkt->pos = pos;
153 pkt->stream_index = idx;
154 pkt->duration = idx ? chunk_size : 1;
155
156 return ret;
157}
158
160 .p.name = "simbiosis_imx",
161 .p.long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX"),
162 .p.extensions = "imx",
163 .p.flags = AVFMT_GENERIC_INDEX,
164 .priv_data_size = sizeof(SimbiosisIMXDemuxContext),
168};
const FFInputFormat ff_simbiosis_imx_demuxer
Definition imx.c:159
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 avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
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:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_SIMBIOSIS_IMX
Definition codec_id.h:304
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RN32(p)
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RL16(p)
#define AV_RN16(p)
static int simbiosis_imx_probe(const AVProbeData *p)
Definition imx.c:38
static int simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition imx.c:89
static int simbiosis_imx_read_header(AVFormatContext *s)
Definition imx.c:52
#define IMX_TAG
Definition imx.c:30
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
#define AVPALETTE_SIZE
Definition pixfmt.h:32
unsigned int pos
Definition spdifenc.c:431
An AVChannelLayout holds information about the channel layout of audio data.
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int64_t first_video_packet_pos
Definition imx.c:35
uint8_t pal[AVPALETTE_SIZE]
Definition imx.c:33
const char * g
Definition vf_curves.c:128