FFmpeg
icoenc.c
Go to the documentation of this file.
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 #include "avformat.h"
30 
31 typedef struct {
32  int offset;
33  int size;
34  unsigned char width;
35  unsigned char height;
36  short bits;
37 } IcoImage;
38 
39 typedef struct {
41  int nb_images;
44 
46 {
47  if (p->codec_id == AV_CODEC_ID_BMP) {
49  av_log(s, AV_LOG_ERROR, "Wrong endianness for bmp pixel format\n");
50  return AVERROR(EINVAL);
51  } else if (p->format != AV_PIX_FMT_PAL8 &&
53  p->format != AV_PIX_FMT_BGR24 &&
54  p->format != AV_PIX_FMT_BGRA) {
55  av_log(s, AV_LOG_ERROR, "BMP must be 1bit, 4bit, 8bit, 16bit, 24bit, or 32bit\n");
56  return AVERROR(EINVAL);
57  }
58  } else if (p->codec_id == AV_CODEC_ID_PNG) {
59  if (p->format != AV_PIX_FMT_RGBA) {
60  av_log(s, AV_LOG_ERROR, "PNG in ico requires pixel format to be rgba\n");
61  return AVERROR(EINVAL);
62  }
63  } else {
65  av_log(s, AV_LOG_ERROR, "Unsupported codec %s\n", codesc ? codesc->name : "");
66  return AVERROR(EINVAL);
67  }
68 
69  if (p->width > 256 ||
70  p->height > 256) {
71  av_log(s, AV_LOG_ERROR, "Unsupported dimensions %dx%d (dimensions cannot exceed 256x256)\n", p->width, p->height);
72  return AVERROR(EINVAL);
73  }
74 
75  return 0;
76 }
77 
79 {
80  IcoMuxContext *ico = s->priv_data;
81  AVIOContext *pb = s->pb;
82  int ret;
83  int i;
84 
85  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
86  av_log(s, AV_LOG_ERROR, "Output is not seekable\n");
87  return AVERROR(EINVAL);
88  }
89 
90  ico->current_image = 0;
91  ico->nb_images = s->nb_streams;
92 
93  avio_wl16(pb, 0); // reserved
94  avio_wl16(pb, 1); // 1 == icon
95  avio_skip(pb, 2); // skip the number of images
96 
97  for (i = 0; i < s->nb_streams; i++) {
98  if (ret = ico_check_attributes(s, s->streams[i]->codecpar))
99  return ret;
100 
101  // Fill in later when writing trailer...
102  avio_skip(pb, 16);
103  }
104 
105  ico->images = av_mallocz_array(ico->nb_images, sizeof(IcoMuxContext));
106  if (!ico->images)
107  return AVERROR(ENOMEM);
108 
109  return 0;
110 }
111 
113 {
114  IcoMuxContext *ico = s->priv_data;
115  IcoImage *image;
116  AVIOContext *pb = s->pb;
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 = (par->width == 256) ? 0 : par->width;
129  image->height = (par->height == 256) ? 0 : par->height;
130 
131  if (par->codec_id == AV_CODEC_ID_PNG) {
132  image->bits = par->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 + par->height * (par->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 < par->height * (par->width + 7) / 8; ++i)
150  avio_w8(pb, 0x00); // Write bitmask (opaque)
151  }
152 
153  return 0;
154 }
155 
157 {
158  IcoMuxContext *ico = s->priv_data;
159  AVIOContext *pb = s->pb;
160  int i;
161 
162  avio_seek(pb, 4, SEEK_SET);
163 
164  avio_wl16(pb, ico->current_image);
165 
166  for (i = 0; i < ico->nb_images; i++) {
167  avio_w8(pb, ico->images[i].width);
168  avio_w8(pb, ico->images[i].height);
169 
170  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_BMP &&
171  s->streams[i]->codecpar->format == AV_PIX_FMT_PAL8) {
172  avio_w8(pb, (ico->images[i].bits >= 8) ? 0 : 1 << ico->images[i].bits);
173  } else {
174  avio_w8(pb, 0);
175  }
176 
177  avio_w8(pb, 0); // reserved
178  avio_wl16(pb, 1); // color planes
179  avio_wl16(pb, ico->images[i].bits);
180  avio_wl32(pb, ico->images[i].size);
181  avio_wl32(pb, ico->images[i].offset);
182  }
183 
184  return 0;
185 }
186 
188 {
189  IcoMuxContext *ico = s->priv_data;
190 
191  av_freep(&ico->images);
192 }
193 
195  .name = "ico",
196  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
197  .priv_data_size = sizeof(IcoMuxContext),
198  .mime_type = "image/vnd.microsoft.icon",
199  .extensions = "ico",
200  .audio_codec = AV_CODEC_ID_NONE,
201  .video_codec = AV_CODEC_ID_BMP,
205  .deinit = ico_deinit,
207 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:698
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:467
Bytestream IO Context.
Definition: avio.h:161
AVOutputFormat ff_ico_muxer
Definition: icoenc.c:194
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4036
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined ...
Definition: pixfmt.h:108
int size
Definition: avcodec.h:1534
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:241
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
int offset
Definition: icodec.c:35
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4028
Format I/O context.
Definition: avformat.h:1353
unsigned char height
Definition: icoenc.c:35
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:367
int width
Video only.
Definition: avcodec.h:4102
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1421
uint8_t * data
Definition: avcodec.h:1533
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
static int ico_write_header(AVFormatContext *s)
Definition: icoenc.c:78
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:213
#define av_log(a,...)
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3364
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1409
static void ico_deinit(AVFormatContext *s)
Definition: icoenc.c:187
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
unsigned char width
Definition: icoenc.c:34
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:500
#define s(width, name)
Definition: cbs_vp9.c:257
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
AVIOContext * pb
I/O context.
Definition: avformat.h:1395
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:191
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:370
static int ico_check_attributes(AVFormatContext *s, const AVCodecParameters *p)
Definition: icoenc.c:45
static int ico_write_trailer(AVFormatContext *s)
Definition: icoenc.c:156
IcoImage * images
Definition: icoenc.c:42
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:742
int nb_images
Definition: icoenc.c:41
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:734
#define flags(name, subs,...)
Definition: cbs_av1.c:564
int current_image
Definition: icoenc.c:40
Main libavformat public API header.
int size
Definition: icodec.c:36
void * priv_data
Format private data.
Definition: avformat.h:1381
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
short bits
Definition: icoenc.c:36
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4078
#define av_freep(p)
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
int stream_index
Definition: avcodec.h:1535
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
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1510
static int ico_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: icoenc.c:112
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191