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.

181 lines
6.9KB

  1. /*
  2. * BMP image format encoder
  3. * Copyright (c) 2006, 2007 Michel Bardiaux
  4. * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/avassert.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "bmp.h"
  27. #include "internal.h"
  28. static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
  29. static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
  30. static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F };
  31. static av_cold int bmp_encode_init(AVCodecContext *avctx){
  32. switch (avctx->pix_fmt) {
  33. case AV_PIX_FMT_BGRA:
  34. avctx->bits_per_coded_sample = 32;
  35. break;
  36. case AV_PIX_FMT_BGR24:
  37. avctx->bits_per_coded_sample = 24;
  38. break;
  39. case AV_PIX_FMT_RGB555:
  40. case AV_PIX_FMT_RGB565:
  41. case AV_PIX_FMT_RGB444:
  42. avctx->bits_per_coded_sample = 16;
  43. break;
  44. case AV_PIX_FMT_RGB8:
  45. case AV_PIX_FMT_BGR8:
  46. case AV_PIX_FMT_RGB4_BYTE:
  47. case AV_PIX_FMT_BGR4_BYTE:
  48. case AV_PIX_FMT_GRAY8:
  49. case AV_PIX_FMT_PAL8:
  50. avctx->bits_per_coded_sample = 8;
  51. break;
  52. case AV_PIX_FMT_MONOBLACK:
  53. avctx->bits_per_coded_sample = 1;
  54. break;
  55. default:
  56. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  57. return AVERROR(EINVAL);
  58. }
  59. return 0;
  60. }
  61. static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  62. const AVFrame *pict, int *got_packet)
  63. {
  64. const AVFrame * const p = pict;
  65. int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize, ret;
  66. const uint32_t *pal = NULL;
  67. uint32_t palette256[256];
  68. int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
  69. int bit_count = avctx->bits_per_coded_sample;
  70. uint8_t *ptr, *buf;
  71. #if FF_API_CODED_FRAME
  72. FF_DISABLE_DEPRECATION_WARNINGS
  73. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  74. avctx->coded_frame->key_frame = 1;
  75. FF_ENABLE_DEPRECATION_WARNINGS
  76. #endif
  77. switch (avctx->pix_fmt) {
  78. case AV_PIX_FMT_RGB444:
  79. compression = BMP_BITFIELDS;
  80. pal = rgb444_masks; // abuse pal to hold color masks
  81. pal_entries = 3;
  82. break;
  83. case AV_PIX_FMT_RGB565:
  84. compression = BMP_BITFIELDS;
  85. pal = rgb565_masks; // abuse pal to hold color masks
  86. pal_entries = 3;
  87. break;
  88. case AV_PIX_FMT_RGB8:
  89. case AV_PIX_FMT_BGR8:
  90. case AV_PIX_FMT_RGB4_BYTE:
  91. case AV_PIX_FMT_BGR4_BYTE:
  92. case AV_PIX_FMT_GRAY8:
  93. av_assert1(bit_count == 8);
  94. avpriv_set_systematic_pal2(palette256, avctx->pix_fmt);
  95. pal = palette256;
  96. break;
  97. case AV_PIX_FMT_PAL8:
  98. pal = (uint32_t *)p->data[1];
  99. break;
  100. case AV_PIX_FMT_MONOBLACK:
  101. pal = monoblack_pal;
  102. break;
  103. }
  104. if (pal && !pal_entries) pal_entries = 1 << bit_count;
  105. n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
  106. pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
  107. n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
  108. // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
  109. // and related pages.
  110. #define SIZE_BITMAPFILEHEADER 14
  111. #define SIZE_BITMAPINFOHEADER 40
  112. hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
  113. n_bytes = n_bytes_image + hsize;
  114. if ((ret = ff_alloc_packet2(avctx, pkt, n_bytes, 0)) < 0)
  115. return ret;
  116. buf = pkt->data;
  117. bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
  118. bytestream_put_byte(&buf, 'M'); // do.
  119. bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
  120. bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
  121. bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
  122. bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
  123. bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
  124. bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
  125. bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
  126. bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
  127. bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
  128. bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
  129. bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
  130. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
  131. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
  132. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
  133. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
  134. for (i = 0; i < pal_entries; i++)
  135. bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
  136. // BMP files are bottom-to-top so we start from the end...
  137. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  138. buf = pkt->data + hsize;
  139. for(i = 0; i < avctx->height; i++) {
  140. if (bit_count == 16) {
  141. const uint16_t *src = (const uint16_t *) ptr;
  142. uint16_t *dst = (uint16_t *) buf;
  143. for(n = 0; n < avctx->width; n++)
  144. AV_WL16(dst + n, src[n]);
  145. } else {
  146. memcpy(buf, ptr, n_bytes_per_row);
  147. }
  148. buf += n_bytes_per_row;
  149. memset(buf, 0, pad_bytes_per_row);
  150. buf += pad_bytes_per_row;
  151. ptr -= p->linesize[0]; // ... and go back
  152. }
  153. pkt->flags |= AV_PKT_FLAG_KEY;
  154. *got_packet = 1;
  155. return 0;
  156. }
  157. AVCodec ff_bmp_encoder = {
  158. .name = "bmp",
  159. .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .id = AV_CODEC_ID_BMP,
  162. .init = bmp_encode_init,
  163. .encode2 = bmp_encode_frame,
  164. .pix_fmts = (const enum AVPixelFormat[]){
  165. AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR24,
  166. AV_PIX_FMT_RGB565, AV_PIX_FMT_RGB555, AV_PIX_FMT_RGB444,
  167. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE, AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8,
  168. AV_PIX_FMT_MONOBLACK,
  169. AV_PIX_FMT_NONE
  170. },
  171. };