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.

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