]> git.sesse.net Git - ffmpeg/blob - libavformat/imx.c
avformat/imx: remove unused header
[ffmpeg] / libavformat / imx.c
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"
26 #include "libavutil/avassert.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];
33     int pal_changed;
34 } SimbiosisIMXDemuxContext;
35
36 static int simbiosis_imx_probe(const AVProbeData *p)
37 {
38     if (AV_RL32(p->buf) != IMX_TAG)
39         return 0;
40     if (AV_RN32(p->buf+4) == 0)
41         return 0;
42     if (AV_RN16(p->buf+8) == 0)
43         return 0;
44     if (AV_RL16(p->buf+10) != 0x102)
45         return 0;
46
47     return AVPROBE_SCORE_EXTENSION + 10;
48 }
49
50 static int simbiosis_imx_read_header(AVFormatContext *s)
51 {
52     AVIOContext *pb = s->pb;
53     AVStream *vst, *ast;
54     int rate;
55
56     vst = avformat_new_stream(s, NULL);
57     ast = avformat_new_stream(s, NULL);
58     if (!vst || !ast)
59         return AVERROR(ENOMEM);
60
61     avio_skip(pb, 4);
62
63     vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
64     vst->codecpar->codec_tag  = 0;
65     vst->codecpar->format     = AV_PIX_FMT_PAL8;
66     vst->codecpar->codec_id   = AV_CODEC_ID_SIMBIOSIS_IMX;
67     vst->start_time = 0;
68     vst->nb_frames = avio_rl32(pb);
69     rate = avio_rl16(pb);
70     avio_skip(pb, 12);
71
72     avpriv_set_pts_info(vst, 64, 1, rate);
73
74     ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
75     ast->codecpar->codec_tag  = 0;
76     ast->codecpar->codec_id   = AV_CODEC_ID_PCM_U8;
77     ast->codecpar->channels   = 1;
78     ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
79     ast->codecpar->sample_rate = 22050;
80     ast->start_time = 0;
81
82     avpriv_set_pts_info(ast, 64, 1, 22050);
83
84     return 0;
85 }
86
87 static int simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt)
88 {
89     AVIOContext *pb = s->pb;
90     SimbiosisIMXDemuxContext *imx = s->priv_data;
91     uint32_t chunk_size, chunk_type;
92     int64_t pos = avio_tell(pb);
93     int ret, idx = -1;
94
95 retry:
96     if (avio_feof(pb))
97         return AVERROR_EOF;
98
99     chunk_size = avio_rl32(pb);
100     chunk_type = avio_rl32(pb);
101
102     switch (chunk_type) {
103     case 0xAAFF:
104         return AVERROR_EOF;
105     case 0xAA99:
106         idx = 1;
107         break;
108     case 0xAA97:
109         idx = 0;
110         break;
111     case 0xAA98:
112         for (int i = 0; i < chunk_size / 3; i++) {
113             unsigned r = avio_r8(pb) << 18;
114             unsigned g = avio_r8(pb) << 10;
115             unsigned b = avio_r8(pb) <<  2;
116
117             AV_WL32(imx->pal + i * 4, (0xFFU << 24) | r | g | b);
118         }
119         imx->pal_changed = 1;
120         idx = -1;
121         break;
122     default:
123         return AVERROR_INVALIDDATA;
124     }
125
126     if (idx == -1)
127         goto retry;
128
129     ret = av_get_packet(pb, pkt, chunk_size);
130     if (ret < 0)
131         return ret;
132
133     if (imx->pal_changed && idx == 0) {
134         uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
135                                                AVPALETTE_SIZE);
136         if (!pal)
137             return AVERROR(ENOMEM);
138         memcpy(pal, imx->pal, AVPALETTE_SIZE);
139         imx->pal_changed = 0;
140         pkt->flags |= AV_PKT_FLAG_KEY;
141     }
142
143     pkt->pos = pos;
144     pkt->stream_index = idx;
145     pkt->duration = idx ? chunk_size : 1;
146
147     return ret;
148 }
149
150 AVInputFormat ff_simbiosis_imx_demuxer = {
151     .name           = "simbiosis_imx",
152     .long_name      = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX"),
153     .priv_data_size = sizeof(SimbiosisIMXDemuxContext),
154     .read_probe     = simbiosis_imx_probe,
155     .read_header    = simbiosis_imx_read_header,
156     .read_packet    = simbiosis_imx_read_packet,
157     .extensions     = "imx",
158     .flags          = AVFMT_GENERIC_INDEX,
159 };