FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bmpenc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 49 86 57.0%
Branches: 7 22 31.8%

Line Branch Exec Source
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/imgutils.h"
26 #include "libavutil/avassert.h"
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "bmp.h"
30 #include "encode.h"
31 #include "internal.h"
32
33 static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
34 static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F };
35 static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F };
36
37 3 static av_cold int bmp_encode_init(AVCodecContext *avctx){
38
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3 switch (avctx->pix_fmt) {
39 case AV_PIX_FMT_BGRA:
40 avctx->bits_per_coded_sample = 32;
41 break;
42 3 case AV_PIX_FMT_BGR24:
43 3 avctx->bits_per_coded_sample = 24;
44 3 break;
45 case AV_PIX_FMT_RGB555:
46 case AV_PIX_FMT_RGB565:
47 case AV_PIX_FMT_RGB444:
48 avctx->bits_per_coded_sample = 16;
49 break;
50 case AV_PIX_FMT_RGB8:
51 case AV_PIX_FMT_BGR8:
52 case AV_PIX_FMT_RGB4_BYTE:
53 case AV_PIX_FMT_BGR4_BYTE:
54 case AV_PIX_FMT_GRAY8:
55 case AV_PIX_FMT_PAL8:
56 avctx->bits_per_coded_sample = 8;
57 break;
58 case AV_PIX_FMT_MONOBLACK:
59 avctx->bits_per_coded_sample = 1;
60 break;
61 default:
62 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
63 return AVERROR(EINVAL);
64 }
65
66 3 return 0;
67 }
68
69 15 static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
70 const AVFrame *pict, int *got_packet)
71 {
72 15 const AVFrame * const p = pict;
73 int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize, ret;
74 15 const uint32_t *pal = NULL;
75 uint32_t palette256[256];
76 15 int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
77 15 int bit_count = avctx->bits_per_coded_sample;
78 uint8_t *ptr, *buf;
79
80
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
15 switch (avctx->pix_fmt) {
81 case AV_PIX_FMT_RGB444:
82 compression = BMP_BITFIELDS;
83 pal = rgb444_masks; // abuse pal to hold color masks
84 pal_entries = 3;
85 break;
86 case AV_PIX_FMT_RGB565:
87 compression = BMP_BITFIELDS;
88 pal = rgb565_masks; // abuse pal to hold color masks
89 pal_entries = 3;
90 break;
91 case AV_PIX_FMT_RGB8:
92 case AV_PIX_FMT_BGR8:
93 case AV_PIX_FMT_RGB4_BYTE:
94 case AV_PIX_FMT_BGR4_BYTE:
95 case AV_PIX_FMT_GRAY8:
96 av_assert1(bit_count == 8);
97 avpriv_set_systematic_pal2(palette256, avctx->pix_fmt);
98 pal = palette256;
99 break;
100 case AV_PIX_FMT_PAL8:
101 pal = (uint32_t *)p->data[1];
102 break;
103 case AV_PIX_FMT_MONOBLACK:
104 pal = monoblack_pal;
105 break;
106 }
107
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
15 if (pal && !pal_entries) pal_entries = 1 << bit_count;
108 15 n_bytes_per_row = ((int64_t)avctx->width * (int64_t)bit_count + 7LL) >> 3LL;
109 15 pad_bytes_per_row = (4 - n_bytes_per_row) & 3;
110 15 n_bytes_image = avctx->height * (n_bytes_per_row + pad_bytes_per_row);
111
112 // STRUCTURE.field refer to the MSVC documentation for BITMAPFILEHEADER
113 // and related pages.
114 #define SIZE_BITMAPFILEHEADER 14
115 #define SIZE_BITMAPINFOHEADER 40
116 15 hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
117 15 n_bytes = n_bytes_image + hsize;
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = ff_get_encode_buffer(avctx, pkt, n_bytes, 0)) < 0)
119 return ret;
120 15 buf = pkt->data;
121 15 bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
122 15 bytestream_put_byte(&buf, 'M'); // do.
123 15 bytestream_put_le32(&buf, n_bytes); // BITMAPFILEHEADER.bfSize
124 15 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved1
125 15 bytestream_put_le16(&buf, 0); // BITMAPFILEHEADER.bfReserved2
126 15 bytestream_put_le32(&buf, hsize); // BITMAPFILEHEADER.bfOffBits
127 15 bytestream_put_le32(&buf, SIZE_BITMAPINFOHEADER); // BITMAPINFOHEADER.biSize
128 15 bytestream_put_le32(&buf, avctx->width); // BITMAPINFOHEADER.biWidth
129 15 bytestream_put_le32(&buf, avctx->height); // BITMAPINFOHEADER.biHeight
130 15 bytestream_put_le16(&buf, 1); // BITMAPINFOHEADER.biPlanes
131 15 bytestream_put_le16(&buf, bit_count); // BITMAPINFOHEADER.biBitCount
132 15 bytestream_put_le32(&buf, compression); // BITMAPINFOHEADER.biCompression
133 15 bytestream_put_le32(&buf, n_bytes_image); // BITMAPINFOHEADER.biSizeImage
134 15 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biXPelsPerMeter
135 15 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biYPelsPerMeter
136 15 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrUsed
137 15 bytestream_put_le32(&buf, 0); // BITMAPINFOHEADER.biClrImportant
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 for (i = 0; i < pal_entries; i++)
139 bytestream_put_le32(&buf, pal[i] & 0xFFFFFF);
140 // BMP files are bottom-to-top so we start from the end...
141 15 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
142 15 buf = pkt->data + hsize;
143
2/2
✓ Branch 0 taken 4357 times.
✓ Branch 1 taken 15 times.
4372 for(i = 0; i < avctx->height; i++) {
144 if (HAVE_BIGENDIAN && bit_count == 16) {
145 const uint16_t *src = (const uint16_t *) ptr;
146 for(n = 0; n < avctx->width; n++)
147 AV_WL16(buf + 2 * n, src[n]);
148 } else {
149 4357 memcpy(buf, ptr, n_bytes_per_row);
150 }
151 4357 buf += n_bytes_per_row;
152 4357 memset(buf, 0, pad_bytes_per_row);
153 4357 buf += pad_bytes_per_row;
154 4357 ptr -= p->linesize[0]; // ... and go back
155 }
156
157 15 pkt->flags |= AV_PKT_FLAG_KEY;
158 15 *got_packet = 1;
159 15 return 0;
160 }
161
162 const AVCodec ff_bmp_encoder = {
163 .name = "bmp",
164 .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
165 .type = AVMEDIA_TYPE_VIDEO,
166 .id = AV_CODEC_ID_BMP,
167 .capabilities = AV_CODEC_CAP_DR1,
168 .init = bmp_encode_init,
169 .encode2 = bmp_encode_frame,
170 .pix_fmts = (const enum AVPixelFormat[]){
171 AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR24,
172 AV_PIX_FMT_RGB565, AV_PIX_FMT_RGB555, AV_PIX_FMT_RGB444,
173 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE, AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8,
174 AV_PIX_FMT_MONOBLACK,
175 AV_PIX_FMT_NONE
176 },
177 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
178 };
179