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.

175 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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "internal.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. switch (avctx->pix_fmt) {
  32. case AV_PIX_FMT_BGR24:
  33. avctx->bits_per_coded_sample = 24;
  34. break;
  35. case AV_PIX_FMT_RGB555:
  36. case AV_PIX_FMT_RGB565:
  37. case AV_PIX_FMT_RGB444:
  38. avctx->bits_per_coded_sample = 16;
  39. break;
  40. case AV_PIX_FMT_RGB8:
  41. case AV_PIX_FMT_BGR8:
  42. case AV_PIX_FMT_RGB4_BYTE:
  43. case AV_PIX_FMT_BGR4_BYTE:
  44. case AV_PIX_FMT_GRAY8:
  45. case AV_PIX_FMT_PAL8:
  46. avctx->bits_per_coded_sample = 8;
  47. break;
  48. case AV_PIX_FMT_MONOBLACK:
  49. avctx->bits_per_coded_sample = 1;
  50. break;
  51. default:
  52. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  58. const AVFrame *pict, int *got_packet)
  59. {
  60. const AVFrame * const p = pict;
  61. int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize, ret;
  62. const uint32_t *pal = NULL;
  63. int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
  64. int bit_count = avctx->bits_per_coded_sample;
  65. uint8_t *ptr, *buf;
  66. #if FF_API_CODED_FRAME
  67. FF_DISABLE_DEPRECATION_WARNINGS
  68. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  69. avctx->coded_frame->key_frame = 1;
  70. FF_ENABLE_DEPRECATION_WARNINGS
  71. #endif
  72. switch (avctx->pix_fmt) {
  73. case AV_PIX_FMT_RGB444:
  74. compression = BMP_BITFIELDS;
  75. pal = rgb444_masks; // abuse pal to hold color masks
  76. pal_entries = 3;
  77. break;
  78. case AV_PIX_FMT_RGB565:
  79. compression = BMP_BITFIELDS;
  80. pal = rgb565_masks; // abuse pal to hold color masks
  81. pal_entries = 3;
  82. break;
  83. case AV_PIX_FMT_RGB8:
  84. case AV_PIX_FMT_BGR8:
  85. case AV_PIX_FMT_RGB4_BYTE:
  86. case AV_PIX_FMT_BGR4_BYTE:
  87. case AV_PIX_FMT_GRAY8:
  88. avpriv_set_systematic_pal2((uint32_t*)p->data[1], avctx->pix_fmt);
  89. case AV_PIX_FMT_PAL8:
  90. pal = (uint32_t *)p->data[1];
  91. break;
  92. case AV_PIX_FMT_MONOBLACK:
  93. pal = monoblack_pal;
  94. break;
  95. }
  96. if (pal && !pal_entries) pal_entries = 1 << bit_count;
  97. n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
  98. pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
  99. n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
  100. // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
  101. // and related pages.
  102. #define SIZE_BITMAPFILEHEADER 14
  103. #define SIZE_BITMAPINFOHEADER 40
  104. hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
  105. n_bytes = n_bytes_image + hsize;
  106. if ((ret = ff_alloc_packet(pkt, n_bytes)) < 0) {
  107. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  108. return ret;
  109. }
  110. buf = pkt->data;
  111. bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
  112. bytestream_put_byte(&buf, 'M'); // do.
  113. bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
  114. bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
  115. bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
  116. bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
  117. bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
  118. bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
  119. bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
  120. bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
  121. bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
  122. bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
  123. bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
  124. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
  125. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
  126. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
  127. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
  128. for (i = 0; i < pal_entries; i++)
  129. bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
  130. // BMP files are bottom-to-top so we start from the end...
  131. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  132. buf = pkt->data + hsize;
  133. for(i = 0; i < avctx->height; i++) {
  134. if (bit_count == 16) {
  135. const uint16_t *src = (const uint16_t *) ptr;
  136. uint16_t *dst = (uint16_t *) buf;
  137. for(n = 0; n < avctx->width; n++)
  138. AV_WL16(dst + n, src[n]);
  139. } else {
  140. memcpy(buf, ptr, n_bytes_per_row);
  141. }
  142. buf += n_bytes_per_row;
  143. memset(buf, 0, pad_bytes_per_row);
  144. buf += pad_bytes_per_row;
  145. ptr -= p->linesize[0]; // ... and go back
  146. }
  147. pkt->flags |= AV_PKT_FLAG_KEY;
  148. *got_packet = 1;
  149. return 0;
  150. }
  151. AVCodec ff_bmp_encoder = {
  152. .name = "bmp",
  153. .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
  154. .type = AVMEDIA_TYPE_VIDEO,
  155. .id = AV_CODEC_ID_BMP,
  156. .init = bmp_encode_init,
  157. .encode2 = bmp_encode_frame,
  158. .pix_fmts = (const enum AVPixelFormat[]){
  159. AV_PIX_FMT_BGR24,
  160. AV_PIX_FMT_RGB555, AV_PIX_FMT_RGB444, AV_PIX_FMT_RGB565,
  161. 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,
  162. AV_PIX_FMT_MONOBLACK,
  163. AV_PIX_FMT_NONE
  164. },
  165. };