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 
30 #include "libavcodec/codec_desc.h"
31 #include "libavcodec/codec_id.h"
32 
33 #include "avformat.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 {
45  int nb_images;
48 
50 {
51  if (p->codec_id == AV_CODEC_ID_BMP) {
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 &&
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 {
69  av_log(s, AV_LOG_ERROR, "Unsupported codec %s\n", codesc ? codesc->name : "");
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 
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_mallocz_array(ico->nb_images, sizeof(IcoMuxContext));
110  if (!ico->images)
111  return AVERROR(ENOMEM);
112 
113  return 0;
114 }
115 
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  int i;
123 
124  if (ico->current_image >= ico->nb_images) {
125  av_log(s, AV_LOG_ERROR, "ICO already contains %d images\n", ico->current_image);
126  return AVERROR(EIO);
127  }
128 
129  image = &ico->images[ico->current_image++];
130 
131  image->offset = avio_tell(pb);
132  image->width = (par->width == 256) ? 0 : par->width;
133  image->height = (par->height == 256) ? 0 : par->height;
134 
135  if (par->codec_id == AV_CODEC_ID_PNG) {
136  image->bits = par->bits_per_coded_sample;
137  image->size = pkt->size;
138 
139  avio_write(pb, pkt->data, pkt->size);
140  } else { // BMP
141  if (AV_RL32(pkt->data + 14) != 40) { // must be BITMAPINFOHEADER
142  av_log(s, AV_LOG_ERROR, "Invalid BMP\n");
143  return AVERROR(EINVAL);
144  }
145 
146  image->bits = AV_RL16(pkt->data + 28); // allows things like 1bit and 4bit images to be preserved
147  image->size = pkt->size - 14 + par->height * (par->width + 7) / 8;
148 
149  avio_write(pb, pkt->data + 14, 8); // Skip the BITMAPFILEHEADER header
150  avio_wl32(pb, AV_RL32(pkt->data + 22) * 2); // rewrite height as 2 * height
151  avio_write(pb, pkt->data + 26, pkt->size - 26);
152 
153  for (i = 0; i < par->height * (par->width + 7) / 8; ++i)
154  avio_w8(pb, 0x00); // Write bitmask (opaque)
155  }
156 
157  return 0;
158 }
159 
161 {
162  IcoMuxContext *ico = s->priv_data;
163  AVIOContext *pb = s->pb;
164  int i;
165 
166  avio_seek(pb, 4, SEEK_SET);
167 
168  avio_wl16(pb, ico->current_image);
169 
170  for (i = 0; i < ico->nb_images; i++) {
171  avio_w8(pb, ico->images[i].width);
172  avio_w8(pb, ico->images[i].height);
173 
174  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_BMP &&
175  s->streams[i]->codecpar->format == AV_PIX_FMT_PAL8) {
176  avio_w8(pb, (ico->images[i].bits >= 8) ? 0 : 1 << ico->images[i].bits);
177  } else {
178  avio_w8(pb, 0);
179  }
180 
181  avio_w8(pb, 0); // reserved
182  avio_wl16(pb, 1); // color planes
183  avio_wl16(pb, ico->images[i].bits);
184  avio_wl32(pb, ico->images[i].size);
185  avio_wl32(pb, ico->images[i].offset);
186  }
187 
188  return 0;
189 }
190 
192 {
193  IcoMuxContext *ico = s->priv_data;
194 
195  av_freep(&ico->images);
196 }
197 
199  .name = "ico",
200  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
201  .priv_data_size = sizeof(IcoMuxContext),
202  .mime_type = "image/vnd.microsoft.icon",
203  .extensions = "ico",
204  .audio_codec = AV_CODEC_ID_NONE,
205  .video_codec = AV_CODEC_ID_BMP,
209  .deinit = ico_deinit,
211 };
AVOutputFormat::name
const char * name
Definition: avformat.h:496
AVERROR
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
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
AVCodecDescriptor::name
const char * name
Name of the codec described by this descriptor.
Definition: codec_desc.h:46
IcoImage::width
unsigned char width
Definition: icoenc.c:38
pixdesc.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
IcoMuxContext::current_image
int current_image
Definition: icoenc.c:44
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:197
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:445
ico_deinit
static void ico_deinit(AVFormatContext *s)
Definition: icoenc.c:191
ff_ico_muxer
const AVOutputFormat ff_ico_muxer
Definition: icoenc.c:198
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:538
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
codec_id.h
AVCodecDescriptor
This struct describes the properties of a single codec described by an AVCodecID.
Definition: codec_desc.h:38
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_CODEC_ID_BMP
@ AV_CODEC_ID_BMP
Definition: codec_id.h:128
ico_write_trailer
static int ico_write_trailer(AVFormatContext *s)
Definition: icoenc.c:160
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:111
AVFormatContext
Format I/O context.
Definition: avformat.h:1097
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:193
IcoMuxContext
Definition: icoenc.c:43
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: packet.h:366
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
size
int size
Definition: twinvq_data.h:10344
ico_write_packet
static int ico_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: icoenc.c:116
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:215
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:362
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:365
offset
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
Definition: writing_filters.txt:86
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:729
AV_PIX_FMT_RGB555LE
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:108
i
int i
Definition: input.c:407
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:495
AVCodecParameters::height
int height
Definition: codec_par.h:127
IcoImage
Definition: icodec.c:34
IcoMuxContext::images
IcoImage * images
Definition: icoenc.c:46
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:243
avformat.h
IcoImage::offset
int offset
Definition: icodec.c:35
IcoImage::size
int size
Definition: icodec.c:36
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
IcoImage::height
unsigned char height
Definition: icoenc.c:39
AVPacket::stream_index
int stream_index
Definition: packet.h:367
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:327
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:342
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ico_check_attributes
static int ico_check_attributes(AVFormatContext *s, const AVCodecParameters *p)
Definition: icoenc.c:49
avcodec_descriptor_get
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3508
IcoImage::bits
short bits
Definition: icoenc.c:40
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:347
IcoMuxContext::nb_images
int nb_images
Definition: icoenc.c:45
codec_desc.h
ico_write_header
static int ico_write_header(AVFormatContext *s)
Definition: icoenc.c:82