FFmpeg
Loading...
Searching...
No Matches
bmpenc.c
Go to the documentation of this file.
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
23#include "config.h"
24
25#include "libavutil/avassert.h"
27#include "avcodec.h"
28#include "bytestream.h"
29#include "bmp.h"
30#include "codec_internal.h"
31#include "encode.h"
32
33static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
34static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
35static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F };
36
38 switch (avctx->pix_fmt) {
39 case AV_PIX_FMT_BGRA:
40 avctx->bits_per_coded_sample = 32;
41 break;
43 avctx->bits_per_coded_sample = 24;
44 break;
48 avctx->bits_per_coded_sample = 16;
49 break;
50 case AV_PIX_FMT_RGB8:
51 case AV_PIX_FMT_BGR8:
55 case AV_PIX_FMT_PAL8:
56 avctx->bits_per_coded_sample = 8;
57 break;
59 avctx->bits_per_coded_sample = 1;
60 break;
61 }
62
63 return 0;
64}
65
67 const AVFrame *pict, int *got_packet)
68{
69 const AVFrame * const p = pict;
70 int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize, ret;
71 const uint32_t *pal = NULL;
72 uint32_t palette256[256];
73 int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
74 int bit_count = avctx->bits_per_coded_sample;
75 const uint8_t *ptr;
76 uint8_t *buf;
77
78 switch (avctx->pix_fmt) {
80 compression = BMP_BITFIELDS;
81 pal = rgb444_masks; // abuse pal to hold color masks
82 pal_entries = 3;
83 break;
85 compression = BMP_BITFIELDS;
86 pal = rgb565_masks; // abuse pal to hold color masks
87 pal_entries = 3;
88 break;
89 case AV_PIX_FMT_RGB8:
90 case AV_PIX_FMT_BGR8:
94 av_assert1(bit_count == 8);
95 avpriv_set_systematic_pal2(palette256, avctx->pix_fmt);
96 pal = palette256;
97 break;
98 case AV_PIX_FMT_PAL8:
99 pal = (uint32_t *)p->data[1];
100 break;
102 pal = monoblack_pal;
103 break;
104 }
105 if (pal && !pal_entries) pal_entries = 1 << bit_count;
106 n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
107 pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
108 n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
109
110 // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
111 // and related pages.
112#define SIZE_BITMAPFILEHEADER 14
113#define SIZE_BITMAPINFOHEADER 40
114 hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
115 n_bytes = n_bytes_image + hsize;
116 if ((ret = ff_get_encode_buffer(avctx, pkt, n_bytes, 0)) < 0)
117 return ret;
118 buf = pkt->data;
119 bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
120 bytestream_put_byte(&buf, 'M'); // do.
121 bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
122 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
123 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
124 bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
125 bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
126 bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
127 bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
128 bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
129 bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
130 bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
131 bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
132 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
133 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
134 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
135 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
136 for (i = 0; i < pal_entries; i++)
137 bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
138 // BMP files are bottom-to-top so we start from the end...
139 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
140 buf = pkt->data + hsize;
141 for(i = 0; i < avctx->height; i++) {
142 if (HAVE_BIGENDIAN && bit_count == 16) {
143 const uint16_t *src = (const uint16_t *) ptr;
144 for(n = 0; n < avctx->width; n++)
145 AV_WL16(buf + 2 * n, src[n]);
146 } else {
147 memcpy(buf, ptr, n_bytes_per_row);
148 }
149 buf += n_bytes_per_row;
150 memset(buf, 0, pad_bytes_per_row);
151 buf += pad_bytes_per_row;
152 ptr -= p->linesize[0]; // ... and go back
153 }
154
155 *got_packet = 1;
156 return 0;
157}
158
const FFCodec ff_bmp_encoder
Definition bmpenc.c:159
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
Libavcodec external API header.
@ BMP_BITFIELDS
Definition bmp.h:29
@ BMP_RGB
Definition bmp.h:26
#define SIZE_BITMAPFILEHEADER
static av_cold int bmp_encode_init(AVCodecContext *avctx)
Definition bmpenc.c:37
static const uint32_t rgb565_masks[]
Definition bmpenc.c:34
static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition bmpenc.c:66
static const uint32_t rgb444_masks[]
Definition bmpenc.c:35
static const uint32_t monoblack_pal[]
Definition bmpenc.c:33
#define SIZE_BITMAPINFOHEADER
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_BMP
Definition codec_id.h:128
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition imgutils.c:178
#define AV_WL16(p, v)
#define av_cold
Definition attributes.h:117
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:83
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_RGB8
packed RGB 3:3:2, 8bpp, (msb)3R 3G 2B(lsb)
Definition pixfmt.h:93
@ AV_PIX_FMT_BGR8
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition pixfmt.h:90
@ AV_PIX_FMT_RGB4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition pixfmt.h:95
@ AV_PIX_FMT_BGR4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition pixfmt.h:92
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
#define AV_PIX_FMT_RGB565
Definition pixfmt.h:532
#define AV_PIX_FMT_RGB444
Definition pixfmt.h:534
#define AV_PIX_FMT_RGB555
Definition pixfmt.h:533
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
#define src
Definition vp8dsp.c:248