2 * BMP image format encoder
3 * Copyright (c) 2006, 2007 Michel Bardiaux
5 * This file is part of FFmpeg.
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.
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.
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
23 #include "bytestream.h"
26 static av_cold
int bmp_encode_init(AVCodecContext
*avctx
){
27 BMPContext
*s
= avctx
->priv_data
;
29 avcodec_get_frame_defaults((AVFrame
*)&s
->picture
);
30 avctx
->coded_frame
= (AVFrame
*)&s
->picture
;
35 static int bmp_encode_frame(AVCodecContext
*avctx
, unsigned char *buf
, int buf_size
, void *data
){
36 BMPContext
*s
= avctx
->priv_data
;
38 AVFrame
* const p
= (AVFrame
*)&s
->picture
;
39 int n_bytes_image
, n_bytes_per_row
, n_bytes
, i
, n
, hsize
;
41 unsigned char* buf0
= buf
;
43 p
->pict_type
= FF_I_TYPE
;
45 n_bytes_per_row
= (avctx
->width
*3 + 3) & ~3;
46 n_bytes_image
= avctx
->height
*n_bytes_per_row
;
48 // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
50 #define SIZE_BITMAPFILEHEADER 14
51 #define SIZE_BITMAPINFOHEADER 40
52 hsize
= SIZE_BITMAPFILEHEADER
+ SIZE_BITMAPINFOHEADER
;
53 n_bytes
= n_bytes_image
+ hsize
;
54 if(n_bytes
>buf_size
) {
55 av_log(avctx
, AV_LOG_ERROR
, "buf size too small (need %d, got %d)\n", n_bytes
, buf_size
);
58 bytestream_put_byte(&buf
, 'B'); // BITMAPFILEHEADER.bfType
59 bytestream_put_byte(&buf
, 'M'); // do.
60 bytestream_put_le32(&buf
, n_bytes
); // BITMAPFILEHEADER.bfSize
61 bytestream_put_le16(&buf
, 0); // BITMAPFILEHEADER.bfReserved1
62 bytestream_put_le16(&buf
, 0); // BITMAPFILEHEADER.bfReserved2
63 bytestream_put_le32(&buf
, hsize
); // BITMAPFILEHEADER.bfOffBits
64 bytestream_put_le32(&buf
, SIZE_BITMAPINFOHEADER
); // BITMAPINFOHEADER.biSize
65 bytestream_put_le32(&buf
, avctx
->width
); // BITMAPINFOHEADER.biWidth
66 bytestream_put_le32(&buf
, avctx
->height
); // BITMAPINFOHEADER.biHeight
67 bytestream_put_le16(&buf
, 1); // BITMAPINFOHEADER.biPlanes
68 bytestream_put_le16(&buf
, 24); // BITMAPINFOHEADER.biBitCount
69 bytestream_put_le32(&buf
, BMP_RGB
); // BITMAPINFOHEADER.biCompression
70 bytestream_put_le32(&buf
, n_bytes_image
); // BITMAPINFOHEADER.biSizeImage
71 bytestream_put_le32(&buf
, 0); // BITMAPINFOHEADER.biXPelsPerMeter
72 bytestream_put_le32(&buf
, 0); // BITMAPINFOHEADER.biYPelsPerMeter
73 bytestream_put_le32(&buf
, 0); // BITMAPINFOHEADER.biClrUsed
74 bytestream_put_le32(&buf
, 0); // BITMAPINFOHEADER.biClrImportant
75 // BMP files are bottom-to-top so we start from the end...
76 ptr
= p
->data
[0] + (avctx
->height
- 1) * p
->linesize
[0];
78 for(i
= 0; i
< avctx
->height
; i
++) {
82 memset(buf
, 0, n_bytes_per_row
-n
);
83 buf
+= n_bytes_per_row
-n
;
84 ptr
-= p
->linesize
[0]; // ... and go back
89 AVCodec bmp_encoder
= {
97 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_BGR24
, PIX_FMT_NONE
},
98 .long_name
= "BMP image",