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.

177 lines
6.7KB

  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 "avcodec.h"
  24. #include "bytestream.h"
  25. #include "bmp.h"
  26. #include <assert.h>
  27. static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
  28. static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
  29. static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F };
  30. static av_cold int bmp_encode_init(AVCodecContext *avctx){
  31. BMPContext *s = avctx->priv_data;
  32. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  33. avctx->coded_frame = (AVFrame*)&s->picture;
  34. switch (avctx->pix_fmt) {
  35. case PIX_FMT_BGR24:
  36. avctx->bits_per_coded_sample = 24;
  37. break;
  38. case PIX_FMT_RGB555:
  39. case PIX_FMT_RGB565:
  40. case PIX_FMT_RGB444:
  41. avctx->bits_per_coded_sample = 16;
  42. break;
  43. case PIX_FMT_RGB8:
  44. case PIX_FMT_BGR8:
  45. case PIX_FMT_RGB4_BYTE:
  46. case PIX_FMT_BGR4_BYTE:
  47. case PIX_FMT_GRAY8:
  48. case PIX_FMT_PAL8:
  49. avctx->bits_per_coded_sample = 8;
  50. break;
  51. case PIX_FMT_MONOBLACK:
  52. avctx->bits_per_coded_sample = 1;
  53. break;
  54. default:
  55. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  61. BMPContext *s = avctx->priv_data;
  62. AVFrame *pict = data;
  63. AVFrame * const p= (AVFrame*)&s->picture;
  64. int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize;
  65. const uint32_t *pal = NULL;
  66. uint32_t palette256[256];
  67. int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
  68. int bit_count = avctx->bits_per_coded_sample;
  69. uint8_t *ptr;
  70. unsigned char* buf0 = buf;
  71. *p = *pict;
  72. p->pict_type= AV_PICTURE_TYPE_I;
  73. p->key_frame= 1;
  74. switch (avctx->pix_fmt) {
  75. case PIX_FMT_RGB444:
  76. compression = BMP_BITFIELDS;
  77. pal = rgb444_masks; // abuse pal to hold color masks
  78. pal_entries = 3;
  79. break;
  80. case PIX_FMT_RGB565:
  81. compression = BMP_BITFIELDS;
  82. pal = rgb565_masks; // abuse pal to hold color masks
  83. pal_entries = 3;
  84. break;
  85. case PIX_FMT_RGB8:
  86. case PIX_FMT_BGR8:
  87. case PIX_FMT_RGB4_BYTE:
  88. case PIX_FMT_BGR4_BYTE:
  89. case PIX_FMT_GRAY8:
  90. assert(bit_count == 8);
  91. ff_set_systematic_pal2(palette256, avctx->pix_fmt);
  92. pal = palette256;
  93. break;
  94. case PIX_FMT_PAL8:
  95. pal = (uint32_t *)p->data[1];
  96. break;
  97. case PIX_FMT_MONOBLACK:
  98. pal = monoblack_pal;
  99. break;
  100. }
  101. if (pal && !pal_entries) pal_entries = 1 << bit_count;
  102. n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
  103. pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
  104. n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
  105. // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
  106. // and related pages.
  107. #define SIZE_BITMAPFILEHEADER 14
  108. #define SIZE_BITMAPINFOHEADER 40
  109. hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
  110. n_bytes = n_bytes_image + hsize;
  111. if(n_bytes>buf_size) {
  112. av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", n_bytes, buf_size);
  113. return -1;
  114. }
  115. bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
  116. bytestream_put_byte(&buf, 'M'); // do.
  117. bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
  118. bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
  119. bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
  120. bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
  121. bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
  122. bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
  123. bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
  124. bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
  125. bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
  126. bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
  127. bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
  128. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
  129. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
  130. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
  131. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
  132. for (i = 0; i < pal_entries; i++)
  133. bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
  134. // BMP files are bottom-to-top so we start from the end...
  135. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  136. buf = buf0 + hsize;
  137. for(i = 0; i < avctx->height; i++) {
  138. if (bit_count == 16) {
  139. const uint16_t *src = (const uint16_t *) ptr;
  140. uint16_t *dst = (uint16_t *) buf;
  141. for(n = 0; n < avctx->width; n++)
  142. AV_WL16(dst + n, src[n]);
  143. } else {
  144. memcpy(buf, ptr, n_bytes_per_row);
  145. }
  146. buf += n_bytes_per_row;
  147. memset(buf, 0, pad_bytes_per_row);
  148. buf += pad_bytes_per_row;
  149. ptr -= p->linesize[0]; // ... and go back
  150. }
  151. return n_bytes;
  152. }
  153. AVCodec ff_bmp_encoder = {
  154. .name = "bmp",
  155. .type = AVMEDIA_TYPE_VIDEO,
  156. .id = CODEC_ID_BMP,
  157. .priv_data_size = sizeof(BMPContext),
  158. .init = bmp_encode_init,
  159. .encode = bmp_encode_frame,
  160. .pix_fmts = (const enum PixelFormat[]){
  161. PIX_FMT_BGR24,
  162. PIX_FMT_RGB555, PIX_FMT_RGB444, PIX_FMT_RGB565,
  163. PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
  164. PIX_FMT_MONOBLACK,
  165. PIX_FMT_NONE},
  166. .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
  167. };