bmpenc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * BMP image format encoder
  3. * Copyright (c) 2006, 2007 Michel Bardiaux
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "bytestream.h"
  23. #include "bmp.h"
  24. static av_cold int bmp_encode_init(AVCodecContext *avctx){
  25. BMPContext *s = avctx->priv_data;
  26. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  27. avctx->coded_frame = (AVFrame*)&s->picture;
  28. return 0;
  29. }
  30. static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  31. BMPContext *s = avctx->priv_data;
  32. AVFrame *pict = data;
  33. AVFrame * const p= (AVFrame*)&s->picture;
  34. int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize;
  35. uint8_t *ptr;
  36. unsigned char* buf0 = buf;
  37. *p = *pict;
  38. p->pict_type= FF_I_TYPE;
  39. p->key_frame= 1;
  40. n_bytes_per_row = (avctx->width*3 + 3) & ~3;
  41. n_bytes_image = avctx->height*n_bytes_per_row;
  42. // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
  43. // and related pages.
  44. #define SIZE_BITMAPFILEHEADER 14
  45. #define SIZE_BITMAPINFOHEADER 40
  46. hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER;
  47. n_bytes = n_bytes_image + hsize;
  48. if(n_bytes>buf_size) {
  49. av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", n_bytes, buf_size);
  50. return -1;
  51. }
  52. bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
  53. bytestream_put_byte(&buf, 'M'); // do.
  54. bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
  55. bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
  56. bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
  57. bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
  58. bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
  59. bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
  60. bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
  61. bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
  62. bytestream_put_le16(&buf, 24); // BITMAPINFOHEADER.biBitCount
  63. bytestream_put_le32(&buf, BMP_RGB); // BITMAPINFOHEADER.biCompression
  64. bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
  65. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
  66. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
  67. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
  68. bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
  69. // BMP files are bottom-to-top so we start from the end...
  70. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  71. buf = buf0 + hsize;
  72. for(i = 0; i < avctx->height; i++) {
  73. n = 3*avctx->width;
  74. memcpy(buf, ptr, n);
  75. buf += n;
  76. memset(buf, 0, n_bytes_per_row-n);
  77. buf += n_bytes_per_row-n;
  78. ptr -= p->linesize[0]; // ... and go back
  79. }
  80. return n_bytes;
  81. }
  82. AVCodec bmp_encoder = {
  83. "bmp",
  84. CODEC_TYPE_VIDEO,
  85. CODEC_ID_BMP,
  86. sizeof(BMPContext),
  87. bmp_encode_init,
  88. bmp_encode_frame,
  89. NULL, //encode_end,
  90. .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
  91. .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
  92. };