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.

204 lines
6.1KB

  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 AVCodecContext *c)
  41. {
  42. if (c->codec_id == AV_CODEC_ID_BMP) {
  43. if (c->pix_fmt == 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 (c->pix_fmt != AV_PIX_FMT_PAL8 &&
  47. c->pix_fmt != AV_PIX_FMT_RGB555LE &&
  48. c->pix_fmt != AV_PIX_FMT_BGR24 &&
  49. c->pix_fmt != 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 (c->codec_id == AV_CODEC_ID_PNG) {
  54. if (c->pix_fmt != 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(c->codec_id);
  60. av_log(s, AV_LOG_ERROR, "Unsupported codec %s\n", codesc ? codesc->name : "");
  61. return AVERROR(EINVAL);
  62. }
  63. if (c->width > 256 ||
  64. c->height > 256) {
  65. av_log(s, AV_LOG_ERROR, "Unsupported dimensions %dx%d (dimensions cannot exceed 256x256)\n", c->width, c->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) {
  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]->codec))
  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. avio_flush(pb);
  95. return 0;
  96. }
  97. static int ico_write_packet(AVFormatContext *s, AVPacket *pkt)
  98. {
  99. IcoMuxContext *ico = s->priv_data;
  100. IcoImage *image;
  101. AVIOContext *pb = s->pb;
  102. AVCodecContext *c = s->streams[pkt->stream_index]->codec;
  103. int i;
  104. if (ico->current_image >= ico->nb_images) {
  105. av_log(s, AV_LOG_ERROR, "ICO already contains %d images\n", ico->current_image);
  106. return AVERROR(EIO);
  107. }
  108. image = &ico->images[ico->current_image++];
  109. image->offset = avio_tell(pb);
  110. image->width = (c->width == 256) ? 0 : c->width;
  111. image->height = (c->height == 256) ? 0 : c->height;
  112. if (c->codec_id == AV_CODEC_ID_PNG) {
  113. image->bits = c->bits_per_coded_sample;
  114. image->size = pkt->size;
  115. avio_write(pb, pkt->data, pkt->size);
  116. } else { // BMP
  117. if (AV_RL32(pkt->data + 14) != 40) { // must be BITMAPINFOHEADER
  118. av_log(s, AV_LOG_ERROR, "Invalid BMP\n");
  119. return AVERROR(EINVAL);
  120. }
  121. image->bits = AV_RL16(pkt->data + 28); // allows things like 1bit and 4bit images to be preserved
  122. image->size = pkt->size - 14 + c->height * (c->width + 7) / 8;
  123. avio_write(pb, pkt->data + 14, 8); // Skip the BITMAPFILEHEADER header
  124. avio_wl32(pb, AV_RL32(pkt->data + 22) * 2); // rewrite height as 2 * height
  125. avio_write(pb, pkt->data + 26, pkt->size - 26);
  126. for (i = 0; i < c->height * (c->width + 7) / 8; ++i)
  127. avio_w8(pb, 0x00); // Write bitmask (opaque)
  128. }
  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 == AV_CODEC_ID_BMP &&
  142. s->streams[i]->codec->pix_fmt == AV_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 = AV_CODEC_ID_NONE,
  163. .video_codec = AV_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. };