Browse code

Add ICO muxer

Signed-off-by: Michael Bradshaw <mbradshaw@sorensonmedia.com>
Reviewed-by: Peter Ross <pross@xvid.org>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Michael Bradshaw authored on 2012/08/13 01:29:36
Showing 6 changed files
... ...
@@ -45,6 +45,7 @@ version next:
45 45
 - smptebars source
46 46
 - asetpts filter
47 47
 - hue filter
48
+- ICO muxer
48 49
 
49 50
 
50 51
 version 0.11:
... ...
@@ -212,7 +212,7 @@ library:
212 212
     @tab General eXchange Format SMPTE 360M, used by Thomson Grass Valley
213 213
          playout servers.
214 214
 @item iCEDraw File              @tab   @tab X
215
-@item ICO                       @tab   @tab X
215
+@item ICO                       @tab X @tab X
216 216
     @tab Microsoft Windows ICO
217 217
 @item id Quake II CIN video     @tab   @tab X
218 218
 @item id RoQ                    @tab X @tab X
... ...
@@ -129,6 +129,39 @@ ffmpeg -i INPUT -f framemd5 -
129 129
 
130 130
 See also the @ref{md5} muxer.
131 131
 
132
+@anchor{ico}
133
+@section ico
134
+
135
+ICO file muxer.
136
+
137
+Microsoft's icon file format (ICO) has some strict limitations that should be noted:
138
+
139
+@itemize
140
+@item
141
+Size cannot exceed 256 pixels in any dimension
142
+
143
+@item
144
+Only BMP and PNG images can be stored
145
+
146
+@item
147
+If a BMP image is used, it must be one of the following pixel formats:
148
+@example
149
+BMP Bit Depth      FFmpeg Pixel Format
150
+1bit               pal8
151
+4bit               pal8
152
+8bit               pal8
153
+16bit              rgb555le
154
+24bit              bgr24
155
+32bit              bgra
156
+@end example
157
+
158
+@item
159
+If a BMP image is used, it must use the BITMAPINFOHEADER DIB header
160
+
161
+@item
162
+If a PNG image is used, it must use the rgba pixel format
163
+@end itemize
164
+
132 165
 @anchor{image2}
133 166
 @section image2
134 167
 
... ...
@@ -122,6 +122,7 @@ OBJS-$(CONFIG_H264_DEMUXER)              += h264dec.o rawdec.o
122 122
 OBJS-$(CONFIG_H264_MUXER)                += rawenc.o
123 123
 OBJS-$(CONFIG_HLS_DEMUXER)               += hls.o
124 124
 OBJS-$(CONFIG_ICO_DEMUXER)               += icodec.o
125
+OBJS-$(CONFIG_ICO_MUXER)                 += icoenc.o
125 126
 OBJS-$(CONFIG_IDCIN_DEMUXER)             += idcin.o
126 127
 OBJS-$(CONFIG_IDF_DEMUXER)               += bintext.o
127 128
 OBJS-$(CONFIG_IFF_DEMUXER)               += iff.o
... ...
@@ -112,7 +112,7 @@ void av_register_all(void)
112 112
     REGISTER_MUXDEMUX (H263, h263);
113 113
     REGISTER_MUXDEMUX (H264, h264);
114 114
     REGISTER_DEMUXER  (HLS, hls);
115
-    REGISTER_DEMUXER  (ICO, ico);
115
+    REGISTER_MUXDEMUX (ICO, ico);
116 116
     REGISTER_DEMUXER  (IDCIN, idcin);
117 117
     REGISTER_DEMUXER  (IDF, idf);
118 118
     REGISTER_DEMUXER  (IFF, iff);
119 119
new file mode 100644
... ...
@@ -0,0 +1,204 @@
0
+/*
1
+ * Microsoft Windows ICO muxer
2
+ * Copyright (c) 2012 Michael Bradshaw <mbradshaw@sorensonmedia.com>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * Microsoft Windows ICO muxer
24
+ */
25
+
26
+#include "libavutil/intreadwrite.h"
27
+#include "libavutil/pixdesc.h"
28
+#include "avformat.h"
29
+
30
+typedef struct {
31
+    int offset;
32
+    int size;
33
+    unsigned char width;
34
+    unsigned char height;
35
+    short bits;
36
+} IcoImage;
37
+
38
+typedef struct {
39
+    int current_image;
40
+    int nb_images;
41
+    IcoImage *images;
42
+} IcoMuxContext;
43
+
44
+static int ico_check_attributes(AVFormatContext *s, const AVCodecContext *c)
45
+{
46
+    if (c->codec_id == CODEC_ID_BMP) {
47
+        if (c->pix_fmt == PIX_FMT_PAL8 && PIX_FMT_RGB32 != PIX_FMT_BGRA) {
48
+            av_log(s, AV_LOG_ERROR, "Wrong endianness for bmp pixel format\n");
49
+            return AVERROR(EINVAL);
50
+        } else if (c->pix_fmt != PIX_FMT_PAL8 &&
51
+                   c->pix_fmt != PIX_FMT_RGB555LE &&
52
+                   c->pix_fmt != PIX_FMT_BGR24 &&
53
+                   c->pix_fmt != PIX_FMT_BGRA) {
54
+            av_log(s, AV_LOG_ERROR, "BMP must be 1bit, 4bit, 8bit, 16bit, 24bit, or 32bit\n");
55
+            return AVERROR(EINVAL);
56
+        }
57
+    } else if (c->codec_id == CODEC_ID_PNG) {
58
+        if (c->pix_fmt != PIX_FMT_RGBA) {
59
+            av_log(s, AV_LOG_ERROR, "PNG in ico requires pixel format to be rgba\n");
60
+            return AVERROR(EINVAL);
61
+        }
62
+    } else {
63
+        av_log(s, AV_LOG_ERROR, "Unsupported codec %s\n", c->codec_name);
64
+        return AVERROR(EINVAL);
65
+    }
66
+
67
+    if (c->width > 256 ||
68
+        c->height > 256) {
69
+        av_log(s, AV_LOG_ERROR, "Unsupported dimensions %dx%d (dimensions cannot exceed 256x256)\n", c->width, c->height);
70
+        return AVERROR(EINVAL);
71
+    }
72
+
73
+    return 0;
74
+}
75
+
76
+static int ico_write_header(AVFormatContext *s)
77
+{
78
+    IcoMuxContext *ico = s->priv_data;
79
+    AVIOContext *pb = s->pb;
80
+    int ret;
81
+    int i;
82
+
83
+    if (!pb->seekable) {
84
+        av_log(s, AV_LOG_ERROR, "Output is not seekable\n");
85
+        return AVERROR(EINVAL);
86
+    }
87
+
88
+    ico->current_image = 0;
89
+    ico->nb_images = s->nb_streams;
90
+
91
+    avio_wl16(pb, 0); // reserved
92
+    avio_wl16(pb, 1); // 1 == icon
93
+    avio_skip(pb, 2); // skip the number of images
94
+
95
+    for (i = 0; i < s->nb_streams; i++) {
96
+        if (ret = ico_check_attributes(s, s->streams[i]->codec))
97
+            return ret;
98
+
99
+        // Fill in later when writing trailer...
100
+        avio_skip(pb, 16);
101
+    }
102
+
103
+    ico->images = av_mallocz(ico->nb_images * sizeof(IcoMuxContext));
104
+    if (!ico->images)
105
+        return AVERROR(ENOMEM);
106
+
107
+    avio_flush(pb);
108
+
109
+    return 0;
110
+}
111
+
112
+static int ico_write_packet(AVFormatContext *s, AVPacket *pkt)
113
+{
114
+    IcoMuxContext *ico = s->priv_data;
115
+    IcoImage *image;
116
+    AVIOContext *pb = s->pb;
117
+    AVCodecContext *c = s->streams[pkt->stream_index]->codec;
118
+    int i;
119
+
120
+    if (ico->current_image >= ico->nb_images) {
121
+        av_log(s, AV_LOG_ERROR, "ICO already contains %d images\n", ico->current_image);
122
+        return AVERROR(EIO);
123
+    }
124
+
125
+    image = &ico->images[ico->current_image++];
126
+
127
+    image->offset = avio_tell(pb);
128
+    image->width = (c->width == 256) ? 0 : c->width;
129
+    image->height = (c->height == 256) ? 0 : c->height;
130
+
131
+    if (c->codec_id == CODEC_ID_PNG) {
132
+        image->bits = c->bits_per_coded_sample;
133
+        image->size = pkt->size;
134
+
135
+        avio_write(pb, pkt->data, pkt->size);
136
+    } else { // BMP
137
+        if (AV_RL32(pkt->data + 14) != 40) { // must be BITMAPINFOHEADER
138
+            av_log(s, AV_LOG_ERROR, "Invalid BMP\n");
139
+            return AVERROR(EINVAL);
140
+        }
141
+
142
+        image->bits = AV_RL16(pkt->data + 28); // allows things like 1bit and 4bit images to be preserved
143
+        image->size = pkt->size - 14 + c->height * (c->width + 7) / 8;
144
+
145
+        avio_write(pb, pkt->data + 14, 8); // Skip the BITMAPFILEHEADER header
146
+        avio_wl32(pb, AV_RL32(pkt->data + 22) * 2); // rewrite height as 2 * height
147
+        avio_write(pb, pkt->data + 26, pkt->size - 26);
148
+
149
+        for (i = 0; i < c->height * (c->width + 7) / 8; ++i)
150
+            avio_w8(pb, 0x00); // Write bitmask (opaque)
151
+    }
152
+
153
+    avio_flush(pb);
154
+
155
+    return 0;
156
+}
157
+
158
+static int ico_write_trailer(AVFormatContext *s)
159
+{
160
+    IcoMuxContext *ico = s->priv_data;
161
+    AVIOContext *pb = s->pb;
162
+    int i;
163
+
164
+    avio_seek(pb, 4, SEEK_SET);
165
+
166
+    avio_wl16(pb, ico->current_image);
167
+
168
+    for (i = 0; i < ico->nb_images; i++) {
169
+        avio_w8(pb, ico->images[i].width);
170
+        avio_w8(pb, ico->images[i].height);
171
+
172
+        if (s->streams[i]->codec->codec_id == CODEC_ID_BMP &&
173
+            s->streams[i]->codec->pix_fmt == PIX_FMT_PAL8) {
174
+            avio_w8(pb, (ico->images[i].bits >= 8) ? 0 : 1 << ico->images[i].bits);
175
+        } else {
176
+            avio_w8(pb, 0);
177
+        }
178
+
179
+        avio_w8(pb, 0); // reserved
180
+        avio_wl16(pb, 1); // color planes
181
+        avio_wl16(pb, ico->images[i].bits);
182
+        avio_wl32(pb, ico->images[i].size);
183
+        avio_wl32(pb, ico->images[i].offset);
184
+    }
185
+
186
+    av_freep(&ico->images);
187
+
188
+    return 0;
189
+}
190
+
191
+AVOutputFormat ff_ico_muxer = {
192
+    .name           = "ico",
193
+    .long_name      = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
194
+    .priv_data_size = sizeof(IcoMuxContext),
195
+    .mime_type      = "image/vnd.microsoft.icon",
196
+    .extensions     = "ico",
197
+    .audio_codec    = CODEC_ID_NONE,
198
+    .video_codec    = CODEC_ID_BMP,
199
+    .write_header   = ico_write_header,
200
+    .write_packet   = ico_write_packet,
201
+    .write_trailer  = ico_write_trailer,
202
+    .flags          = AVFMT_NOTIMESTAMPS,
203
+};