Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Microsoft Windows ICO muxer |
3 |
|
|
* Copyright (c) 2012 Michael Bradshaw <mjbshaw gmail com> |
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 |
|
|
* Microsoft Windows ICO muxer |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
#include "libavutil/intreadwrite.h" |
28 |
|
|
#include "libavutil/mem.h" |
29 |
|
|
|
30 |
|
|
#include "libavcodec/codec_id.h" |
31 |
|
|
|
32 |
|
|
#include "avformat.h" |
33 |
|
|
#include "avio_internal.h" |
34 |
|
|
#include "mux.h" |
35 |
|
|
|
36 |
|
|
typedef struct { |
37 |
|
|
int offset; |
38 |
|
|
int size; |
39 |
|
|
unsigned char width; |
40 |
|
|
unsigned char height; |
41 |
|
|
short bits; |
42 |
|
|
} IcoImage; |
43 |
|
|
|
44 |
|
|
typedef struct { |
45 |
|
|
int current_image; |
46 |
|
|
int nb_images; |
47 |
|
|
IcoImage *images; |
48 |
|
|
} IcoMuxContext; |
49 |
|
|
|
50 |
|
✗ |
static int ico_check_attributes(AVFormatContext *s, const AVCodecParameters *p) |
51 |
|
|
{ |
52 |
|
✗ |
if (p->codec_id == AV_CODEC_ID_BMP) { |
53 |
|
|
if (p->format == AV_PIX_FMT_PAL8 && AV_PIX_FMT_RGB32 != AV_PIX_FMT_BGRA) { |
54 |
|
|
av_log(s, AV_LOG_ERROR, "Wrong endianness for bmp pixel format\n"); |
55 |
|
|
return AVERROR(EINVAL); |
56 |
|
✗ |
} else if (p->format != AV_PIX_FMT_PAL8 && |
57 |
|
✗ |
p->format != AV_PIX_FMT_RGB555LE && |
58 |
|
✗ |
p->format != AV_PIX_FMT_BGR24 && |
59 |
|
✗ |
p->format != AV_PIX_FMT_BGRA) { |
60 |
|
✗ |
av_log(s, AV_LOG_ERROR, "BMP must be 1bit, 4bit, 8bit, 16bit, 24bit, or 32bit\n"); |
61 |
|
✗ |
return AVERROR(EINVAL); |
62 |
|
|
} |
63 |
|
✗ |
} else if (p->codec_id == AV_CODEC_ID_PNG) { |
64 |
|
✗ |
if (p->format != AV_PIX_FMT_RGBA) { |
65 |
|
✗ |
av_log(s, AV_LOG_ERROR, "PNG in ico requires pixel format to be rgba\n"); |
66 |
|
✗ |
return AVERROR(EINVAL); |
67 |
|
|
} |
68 |
|
|
} else { |
69 |
|
✗ |
av_log(s, AV_LOG_ERROR, "Unsupported codec %s\n", avcodec_get_name(p->codec_id)); |
70 |
|
✗ |
return AVERROR(EINVAL); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
✗ |
if (p->width > 256 || |
74 |
|
✗ |
p->height > 256) { |
75 |
|
✗ |
av_log(s, AV_LOG_ERROR, "Unsupported dimensions %dx%d (dimensions cannot exceed 256x256)\n", p->width, p->height); |
76 |
|
✗ |
return AVERROR(EINVAL); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
✗ |
return 0; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
✗ |
static int ico_write_header(AVFormatContext *s) |
83 |
|
|
{ |
84 |
|
✗ |
IcoMuxContext *ico = s->priv_data; |
85 |
|
✗ |
AVIOContext *pb = s->pb; |
86 |
|
|
int ret; |
87 |
|
|
int i; |
88 |
|
|
|
89 |
|
✗ |
if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) { |
90 |
|
✗ |
av_log(s, AV_LOG_ERROR, "Output is not seekable\n"); |
91 |
|
✗ |
return AVERROR(EINVAL); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
✗ |
ico->current_image = 0; |
95 |
|
✗ |
ico->nb_images = s->nb_streams; |
96 |
|
|
|
97 |
|
✗ |
avio_wl16(pb, 0); // reserved |
98 |
|
✗ |
avio_wl16(pb, 1); // 1 == icon |
99 |
|
✗ |
avio_skip(pb, 2); // skip the number of images |
100 |
|
|
|
101 |
|
✗ |
for (i = 0; i < s->nb_streams; i++) { |
102 |
|
✗ |
if (ret = ico_check_attributes(s, s->streams[i]->codecpar)) |
103 |
|
✗ |
return ret; |
104 |
|
|
|
105 |
|
|
// Fill in later when writing trailer... |
106 |
|
✗ |
avio_skip(pb, 16); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
✗ |
ico->images = av_calloc(ico->nb_images, sizeof(*ico->images)); |
110 |
|
✗ |
if (!ico->images) |
111 |
|
✗ |
return AVERROR(ENOMEM); |
112 |
|
|
|
113 |
|
✗ |
return 0; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
✗ |
static int ico_write_packet(AVFormatContext *s, AVPacket *pkt) |
117 |
|
|
{ |
118 |
|
✗ |
IcoMuxContext *ico = s->priv_data; |
119 |
|
|
IcoImage *image; |
120 |
|
✗ |
AVIOContext *pb = s->pb; |
121 |
|
✗ |
AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar; |
122 |
|
|
|
123 |
|
✗ |
if (ico->current_image >= ico->nb_images) { |
124 |
|
✗ |
av_log(s, AV_LOG_ERROR, "ICO already contains %d images\n", ico->current_image); |
125 |
|
✗ |
return AVERROR(EIO); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
✗ |
image = &ico->images[ico->current_image++]; |
129 |
|
|
|
130 |
|
✗ |
image->offset = avio_tell(pb); |
131 |
|
✗ |
image->width = (par->width == 256) ? 0 : par->width; |
132 |
|
✗ |
image->height = (par->height == 256) ? 0 : par->height; |
133 |
|
|
|
134 |
|
✗ |
if (par->codec_id == AV_CODEC_ID_PNG) { |
135 |
|
✗ |
image->bits = par->bits_per_coded_sample; |
136 |
|
✗ |
image->size = pkt->size; |
137 |
|
|
|
138 |
|
✗ |
avio_write(pb, pkt->data, pkt->size); |
139 |
|
|
} else { // BMP |
140 |
|
✗ |
if (AV_RL32(pkt->data + 14) != 40) { // must be BITMAPINFOHEADER |
141 |
|
✗ |
av_log(s, AV_LOG_ERROR, "Invalid BMP\n"); |
142 |
|
✗ |
return AVERROR(EINVAL); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
✗ |
image->bits = AV_RL16(pkt->data + 28); // allows things like 1bit and 4bit images to be preserved |
146 |
|
✗ |
image->size = pkt->size - 14 + par->height * (par->width + 7) / 8; |
147 |
|
|
|
148 |
|
✗ |
avio_write(pb, pkt->data + 14, 8); // Skip the BITMAPFILEHEADER header |
149 |
|
✗ |
avio_wl32(pb, AV_RL32(pkt->data + 22) * 2); // rewrite height as 2 * height |
150 |
|
✗ |
avio_write(pb, pkt->data + 26, pkt->size - 26); |
151 |
|
|
|
152 |
|
|
// Write bitmask (opaque) |
153 |
|
✗ |
ffio_fill(pb, 0x00, par->height * (par->width + 7) / 8); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
✗ |
return 0; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
✗ |
static int ico_write_trailer(AVFormatContext *s) |
160 |
|
|
{ |
161 |
|
✗ |
IcoMuxContext *ico = s->priv_data; |
162 |
|
✗ |
AVIOContext *pb = s->pb; |
163 |
|
|
int i; |
164 |
|
|
|
165 |
|
✗ |
avio_seek(pb, 4, SEEK_SET); |
166 |
|
|
|
167 |
|
✗ |
avio_wl16(pb, ico->current_image); |
168 |
|
|
|
169 |
|
✗ |
for (i = 0; i < ico->nb_images; i++) { |
170 |
|
✗ |
avio_w8(pb, ico->images[i].width); |
171 |
|
✗ |
avio_w8(pb, ico->images[i].height); |
172 |
|
|
|
173 |
|
✗ |
if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_BMP && |
174 |
|
✗ |
s->streams[i]->codecpar->format == AV_PIX_FMT_PAL8) { |
175 |
|
✗ |
avio_w8(pb, (ico->images[i].bits >= 8) ? 0 : 1 << ico->images[i].bits); |
176 |
|
|
} else { |
177 |
|
✗ |
avio_w8(pb, 0); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
✗ |
avio_w8(pb, 0); // reserved |
181 |
|
✗ |
avio_wl16(pb, 1); // color planes |
182 |
|
✗ |
avio_wl16(pb, ico->images[i].bits); |
183 |
|
✗ |
avio_wl32(pb, ico->images[i].size); |
184 |
|
✗ |
avio_wl32(pb, ico->images[i].offset); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
✗ |
return 0; |
188 |
|
|
} |
189 |
|
|
|
190 |
|
✗ |
static void ico_deinit(AVFormatContext *s) |
191 |
|
|
{ |
192 |
|
✗ |
IcoMuxContext *ico = s->priv_data; |
193 |
|
|
|
194 |
|
✗ |
av_freep(&ico->images); |
195 |
|
✗ |
} |
196 |
|
|
|
197 |
|
|
const FFOutputFormat ff_ico_muxer = { |
198 |
|
|
.p.name = "ico", |
199 |
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"), |
200 |
|
|
.priv_data_size = sizeof(IcoMuxContext), |
201 |
|
|
.p.mime_type = "image/vnd.microsoft.icon", |
202 |
|
|
.p.extensions = "ico", |
203 |
|
|
.p.audio_codec = AV_CODEC_ID_NONE, |
204 |
|
|
.p.video_codec = AV_CODEC_ID_BMP, |
205 |
|
|
.write_header = ico_write_header, |
206 |
|
|
.write_packet = ico_write_packet, |
207 |
|
|
.write_trailer = ico_write_trailer, |
208 |
|
|
.deinit = ico_deinit, |
209 |
|
|
.p.flags = AVFMT_NOTIMESTAMPS, |
210 |
|
|
}; |
211 |
|
|
|