You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.3KB

  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. * @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 AVCodecParameters *p)
  41. {
  42. if (p->codec_id == AV_CODEC_ID_BMP) {
  43. if (p->format == AV_PIX_FMT_PAL8 && AV_PIX_FMT_RGB32 != AV_PIX_FMT_BGRA) {
  44. av_log(s, AV_LOG_ERROR, "Wrong endianness for bmp pixel format\n");
  45. return AVERROR(EINVAL);
  46. } else if (p->format != AV_PIX_FMT_PAL8 &&
  47. p->format != AV_PIX_FMT_RGB555LE &&
  48. p->format != AV_PIX_FMT_BGR24 &&
  49. p->format != AV_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 (p->codec_id == AV_CODEC_ID_PNG) {
  54. if (p->format != AV_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. const AVCodecDescriptor *codesc = avcodec_descriptor_get(p->codec_id);
  60. av_log(s, AV_LOG_ERROR, "Unsupported codec %s\n", codesc ? codesc->name : "");
  61. return AVERROR(EINVAL);
  62. }
  63. if (p->width > 256 ||
  64. p->height > 256) {
  65. av_log(s, AV_LOG_ERROR, "Unsupported dimensions %dx%d (dimensions cannot exceed 256x256)\n", p->width, p->height);
  66. return AVERROR(EINVAL);
  67. }
  68. return 0;
  69. }
  70. static int ico_write_header(AVFormatContext *s)
  71. {
  72. IcoMuxContext *ico = s->priv_data;
  73. AVIOContext *pb = s->pb;
  74. int ret;
  75. int i;
  76. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
  77. av_log(s, AV_LOG_ERROR, "Output is not seekable\n");
  78. return AVERROR(EINVAL);
  79. }
  80. ico->current_image = 0;
  81. ico->nb_images = s->nb_streams;
  82. avio_wl16(pb, 0); // reserved
  83. avio_wl16(pb, 1); // 1 == icon
  84. avio_skip(pb, 2); // skip the number of images
  85. for (i = 0; i < s->nb_streams; i++) {
  86. if (ret = ico_check_attributes(s, s->streams[i]->codecpar))
  87. return ret;
  88. // Fill in later when writing trailer...
  89. avio_skip(pb, 16);
  90. }
  91. ico->images = av_mallocz_array(ico->nb_images, sizeof(IcoMuxContext));
  92. if (!ico->images)
  93. return AVERROR(ENOMEM);
  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. AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
  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 = (par->width == 256) ? 0 : par->width;
  110. image->height = (par->height == 256) ? 0 : par->height;
  111. if (par->codec_id == AV_CODEC_ID_PNG) {
  112. image->bits = par->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 + par->height * (par->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 < par->height * (par->width + 7) / 8; ++i)
  126. avio_w8(pb, 0x00); // Write bitmask (opaque)
  127. }
  128. return 0;
  129. }
  130. static int ico_write_trailer(AVFormatContext *s)
  131. {
  132. IcoMuxContext *ico = s->priv_data;
  133. AVIOContext *pb = s->pb;
  134. int i;
  135. avio_seek(pb, 4, SEEK_SET);
  136. avio_wl16(pb, ico->current_image);
  137. for (i = 0; i < ico->nb_images; i++) {
  138. avio_w8(pb, ico->images[i].width);
  139. avio_w8(pb, ico->images[i].height);
  140. if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_BMP &&
  141. s->streams[i]->codecpar->format == AV_PIX_FMT_PAL8) {
  142. avio_w8(pb, (ico->images[i].bits >= 8) ? 0 : 1 << ico->images[i].bits);
  143. } else {
  144. avio_w8(pb, 0);
  145. }
  146. avio_w8(pb, 0); // reserved
  147. avio_wl16(pb, 1); // color planes
  148. avio_wl16(pb, ico->images[i].bits);
  149. avio_wl32(pb, ico->images[i].size);
  150. avio_wl32(pb, ico->images[i].offset);
  151. }
  152. return 0;
  153. }
  154. static void ico_deinit(AVFormatContext *s)
  155. {
  156. IcoMuxContext *ico = s->priv_data;
  157. av_freep(&ico->images);
  158. }
  159. AVOutputFormat ff_ico_muxer = {
  160. .name = "ico",
  161. .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
  162. .priv_data_size = sizeof(IcoMuxContext),
  163. .mime_type = "image/vnd.microsoft.icon",
  164. .extensions = "ico",
  165. .audio_codec = AV_CODEC_ID_NONE,
  166. .video_codec = AV_CODEC_ID_BMP,
  167. .write_header = ico_write_header,
  168. .write_packet = ico_write_packet,
  169. .write_trailer = ico_write_trailer,
  170. .deinit = ico_deinit,
  171. .flags = AVFMT_NOTIMESTAMPS,
  172. };