FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/icoenc.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 0 84 0.0%
Functions: 0 5 0.0%
Branches: 0 44 0.0%

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