icoenc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Microsoft Windows ICO muxer
  3. * Copyright (c) 2012 Michael Bradshaw <mbradshaw@sorensonmedia.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. * @file
  23. * Microsoft Windows ICO muxer
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avformat.h"
  28. typedef struct {
  29. int offset;
  30. int size;
  31. unsigned char width;
  32. unsigned char height;
  33. short bits;
  34. } IcoImage;
  35. typedef struct {
  36. int current_image;
  37. int nb_images;
  38. IcoImage *images;
  39. } IcoMuxContext;
  40. static int ico_check_attributes(AVFormatContext *s, const AVCodecContext *c)
  41. {
  42. if (c->codec_id == CODEC_ID_BMP) {
  43. if (c->pix_fmt == PIX_FMT_PAL8 && PIX_FMT_RGB32 != PIX_FMT_BGRA) {
  44. av_log(s, AV_LOG_ERROR, "Wrong endianness for bmp pixel format\n");
  45. return AVERROR(EINVAL);
  46. } else if (c->pix_fmt != PIX_FMT_PAL8 &&
  47. c->pix_fmt != PIX_FMT_RGB555LE &&
  48. c->pix_fmt != PIX_FMT_BGR24 &&
  49. c->pix_fmt != PIX_FMT_BGRA) {
  50. av_log(s, AV_LOG_ERROR, "BMP must be 1bit, 4bit, 8bit, 16bit, 24bit, or 32bit\n");
  51. return AVERROR(EINVAL);
  52. }
  53. } else if (c->codec_id == CODEC_ID_PNG) {
  54. if (c->pix_fmt != PIX_FMT_RGBA) {
  55. av_log(s, AV_LOG_ERROR, "PNG in ico requires pixel format to be rgba\n");
  56. return AVERROR(EINVAL);
  57. }
  58. } else {
  59. av_log(s, AV_LOG_ERROR, "Unsupported codec %s\n", c->codec_name);
  60. return AVERROR(EINVAL);
  61. }
  62. if (c->width > 256 ||
  63. c->height > 256) {
  64. av_log(s, AV_LOG_ERROR, "Unsupported dimensions %dx%d (dimensions cannot exceed 256x256)\n", c->width, c->height);
  65. return AVERROR(EINVAL);
  66. }
  67. return 0;
  68. }
  69. static int ico_write_header(AVFormatContext *s)
  70. {
  71. IcoMuxContext *ico = s->priv_data;
  72. AVIOContext *pb = s->pb;
  73. int ret;
  74. int i;
  75. if (!pb->seekable) {
  76. av_log(s, AV_LOG_ERROR, "Output is not seekable\n");
  77. return AVERROR(EINVAL);
  78. }
  79. ico->current_image = 0;
  80. ico->nb_images = s->nb_streams;
  81. avio_wl16(pb, 0); // reserved
  82. avio_wl16(pb, 1); // 1 == icon
  83. avio_skip(pb, 2); // skip the number of images
  84. for (i = 0; i < s->nb_streams; i++) {
  85. if (ret = ico_check_attributes(s, s->streams[i]->codec))
  86. return ret;
  87. // Fill in later when writing trailer...
  88. avio_skip(pb, 16);
  89. }
  90. ico->images = av_mallocz(ico->nb_images * sizeof(IcoMuxContext));
  91. if (!ico->images)
  92. return AVERROR(ENOMEM);
  93. avio_flush(pb);
  94. return 0;
  95. }
  96. static int ico_write_packet(AVFormatContext *s, AVPacket *pkt)
  97. {
  98. IcoMuxContext *ico = s->priv_data;
  99. IcoImage *image;
  100. AVIOContext *pb = s->pb;
  101. AVCodecContext *c = s->streams[pkt->stream_index]->codec;
  102. int i;
  103. if (ico->current_image >= ico->nb_images) {
  104. av_log(s, AV_LOG_ERROR, "ICO already contains %d images\n", ico->current_image);
  105. return AVERROR(EIO);
  106. }
  107. image = &ico->images[ico->current_image++];
  108. image->offset = avio_tell(pb);
  109. image->width = (c->width == 256) ? 0 : c->width;
  110. image->height = (c->height == 256) ? 0 : c->height;
  111. if (c->codec_id == CODEC_ID_PNG) {
  112. image->bits = c->bits_per_coded_sample;
  113. image->size = pkt->size;
  114. avio_write(pb, pkt->data, pkt->size);
  115. } else { // BMP
  116. if (AV_RL32(pkt->data + 14) != 40) { // must be BITMAPINFOHEADER
  117. av_log(s, AV_LOG_ERROR, "Invalid BMP\n");
  118. return AVERROR(EINVAL);
  119. }
  120. image->bits = AV_RL16(pkt->data + 28); // allows things like 1bit and 4bit images to be preserved
  121. image->size = pkt->size - 14 + c->height * (c->width + 7) / 8;
  122. avio_write(pb, pkt->data + 14, 8); // Skip the BITMAPFILEHEADER header
  123. avio_wl32(pb, AV_RL32(pkt->data + 22) * 2); // rewrite height as 2 * height
  124. avio_write(pb, pkt->data + 26, pkt->size - 26);
  125. for (i = 0; i < c->height * (c->width + 7) / 8; ++i)
  126. avio_w8(pb, 0x00); // Write bitmask (opaque)
  127. }
  128. avio_flush(pb);
  129. return 0;
  130. }
  131. static int ico_write_trailer(AVFormatContext *s)
  132. {
  133. IcoMuxContext *ico = s->priv_data;
  134. AVIOContext *pb = s->pb;
  135. int i;
  136. avio_seek(pb, 4, SEEK_SET);
  137. avio_wl16(pb, ico->current_image);
  138. for (i = 0; i < ico->nb_images; i++) {
  139. avio_w8(pb, ico->images[i].width);
  140. avio_w8(pb, ico->images[i].height);
  141. if (s->streams[i]->codec->codec_id == CODEC_ID_BMP &&
  142. s->streams[i]->codec->pix_fmt == PIX_FMT_PAL8) {
  143. avio_w8(pb, (ico->images[i].bits >= 8) ? 0 : 1 << ico->images[i].bits);
  144. } else {
  145. avio_w8(pb, 0);
  146. }
  147. avio_w8(pb, 0); // reserved
  148. avio_wl16(pb, 1); // color planes
  149. avio_wl16(pb, ico->images[i].bits);
  150. avio_wl32(pb, ico->images[i].size);
  151. avio_wl32(pb, ico->images[i].offset);
  152. }
  153. av_freep(&ico->images);
  154. return 0;
  155. }
  156. AVOutputFormat ff_ico_muxer = {
  157. .name = "ico",
  158. .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
  159. .priv_data_size = sizeof(IcoMuxContext),
  160. .mime_type = "image/vnd.microsoft.icon",
  161. .extensions = "ico",
  162. .audio_codec = CODEC_ID_NONE,
  163. .video_codec = CODEC_ID_BMP,
  164. .write_header = ico_write_header,
  165. .write_packet = ico_write_packet,
  166. .write_trailer = ico_write_trailer,
  167. .flags = AVFMT_NOTIMESTAMPS,
  168. };