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.

168 lines
6.3KB

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