FFmpeg
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 "internal.h"
25 #include "libavutil/intreadwrite.h"
27 #include "libavutil/internal.h"
28 
29 #define IMX_TAG MKTAG('I', 'M', 'A', 'X')
30 
31 typedef struct SimbiosisIMXDemuxContext {
32  uint8_t pal[AVPALETTE_SIZE];
36 
37 static int simbiosis_imx_probe(const AVProbeData *p)
38 {
39  if (AV_RL32(p->buf) != IMX_TAG)
40  return 0;
41  if (AV_RN32(p->buf+4) == 0)
42  return 0;
43  if (AV_RN16(p->buf+8) == 0)
44  return 0;
45  if (AV_RL16(p->buf+10) != 0x102)
46  return 0;
47 
48  return AVPROBE_SCORE_EXTENSION + 10;
49 }
50 
52 {
53  AVIOContext *pb = s->pb;
54  AVStream *vst, *ast;
55  int rate;
56 
57  vst = avformat_new_stream(s, NULL);
58  ast = avformat_new_stream(s, NULL);
59  if (!vst || !ast)
60  return AVERROR(ENOMEM);
61 
62  avio_skip(pb, 4);
63 
65  vst->codecpar->codec_tag = 0;
68  vst->start_time = 0;
69  vst->duration =
70  vst->nb_frames = avio_rl32(pb);
71  rate = avio_rl16(pb);
72  avio_skip(pb, 12);
73 
74  avpriv_set_pts_info(vst, 64, 1, rate);
75 
77  ast->codecpar->codec_tag = 0;
80  ast->codecpar->sample_rate = 22050;
81  ast->start_time = 0;
82 
83  avpriv_set_pts_info(ast, 64, 1, 22050);
84 
85  return 0;
86 }
87 
89 {
90  AVIOContext *pb = s->pb;
91  SimbiosisIMXDemuxContext *imx = s->priv_data;
92  uint32_t chunk_size, chunk_type;
93  int64_t pos = avio_tell(pb);
94  int ret, idx = -1;
95 
96 retry:
97  if (avio_feof(pb))
98  return AVERROR_EOF;
99 
100  chunk_size = avio_rl32(pb);
101  chunk_type = avio_rl32(pb);
102 
103  switch (chunk_type) {
104  case 0xAAFF:
105  return AVERROR_EOF;
106  case 0xAA99:
107  idx = 1;
108  break;
109  case 0xAA97:
110  idx = 0;
111  if (!imx->first_video_packet_pos)
113  break;
114  case 0xAA98:
115  if (chunk_size > 256 * 3)
116  return AVERROR_INVALIDDATA;
117  for (int i = 0; i < chunk_size / 3; i++) {
118  unsigned r = avio_r8(pb) << 18;
119  unsigned g = avio_r8(pb) << 10;
120  unsigned b = avio_r8(pb) << 2;
121 
122  AV_WL32(imx->pal + i * 4, (0xFFU << 24) | r | g | b);
123  }
124  imx->pal_changed = 1;
125  idx = -1;
126  break;
127  default:
128  return AVERROR_INVALIDDATA;
129  }
130 
131  if (idx == -1)
132  goto retry;
133 
134  ret = av_get_packet(pb, pkt, chunk_size);
135  if (ret < 0)
136  return ret;
137 
138  if (imx->pal_changed && idx == 0) {
141  if (!pal)
142  return AVERROR(ENOMEM);
143  memcpy(pal, imx->pal, AVPALETTE_SIZE);
144  imx->pal_changed = 0;
145  if (pos <= imx->first_video_packet_pos)
147  } else if (idx == 1) {
149  }
150 
151  pkt->pos = pos;
152  pkt->stream_index = idx;
153  pkt->duration = idx ? chunk_size : 1;
154 
155  return ret;
156 }
157 
159  .name = "simbiosis_imx",
160  .long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX"),
161  .priv_data_size = sizeof(SimbiosisIMXDemuxContext),
165  .extensions = "imx",
167 };
r
const char * r
Definition: vf_curves.c:126
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: options.c:243
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:58
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_RN16
#define AV_RN16(p)
Definition: intreadwrite.h:360
b
#define b
Definition: input.c:41
AV_PKT_DATA_PALETTE
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:47
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:66
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:429
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: avformat.c:771
IMX_TAG
#define IMX_TAG
Definition: imx.c:29
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:500
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:897
SimbiosisIMXDemuxContext::pal_changed
int pal_changed
Definition: imx.c:33
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:735
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:546
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:551
g
const char * g
Definition: vf_curves.c:127
ff_simbiosis_imx_demuxer
const AVInputFormat ff_simbiosis_imx_demuxer
Definition: imx.c:158
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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:1104
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
NULL
#define NULL
Definition: coverity.c:32
simbiosis_imx_probe
static int simbiosis_imx_probe(const AVProbeData *p)
Definition: imx.c:37
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_RN32
#define AV_RN32(p)
Definition: intreadwrite.h:364
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
SimbiosisIMXDemuxContext::first_video_packet_pos
int64_t first_video_packet_pos
Definition: imx.c:34
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:178
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:899
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:751
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
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:115
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:380
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
SimbiosisIMXDemuxContext
Definition: imx.c:31
internal.h
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:102
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:838
simbiosis_imx_read_packet
static int simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: imx.c:88
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
SimbiosisIMXDemuxContext::pal
uint8_t pal[AVPALETTE_SIZE]
Definition: imx.c:32
channel_layout.h
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: avpacket.c:230
simbiosis_imx_read_header
static int simbiosis_imx_read_header(AVFormatContext *s)
Definition: imx.c:51
AV_CODEC_ID_SIMBIOSIS_IMX
@ AV_CODEC_ID_SIMBIOSIS_IMX
Definition: codec_id.h:312
AVPacket::stream_index
int stream_index
Definition: packet.h:376
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:339
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:29
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:331
AVCodecParameters::format
int format
Definition: codec_par.h:86
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:368
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:394
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:887
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:367