1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-10 06:10:52 +02:00

avcodec/bmpenc: Avoid copying packet data, allow user-supplied buffers

When the packet size is known in advance like here, one can avoid
an intermediate buffer for the packet data; this also makes it easy
to allow user-supplied buffers. Only one thing needed to be changed:
One can no longer use a pointer to uint16_t for the destination buffer
because its alignment is unknown.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2021-04-25 01:43:26 +02:00
parent 1106f20645
commit 6c69251b03

View File

@@ -25,6 +25,7 @@
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h" #include "bytestream.h"
#include "bmp.h" #include "bmp.h"
#include "encode.h"
#include "internal.h" #include "internal.h"
static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF }; static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
@@ -112,7 +113,7 @@ static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
#define SIZE_BITMAPINFOHEADER 40 #define SIZE_BITMAPINFOHEADER 40
hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2); hsize = SIZE_BITMAPFILEHEADER + SIZE_BITMAPINFOHEADER + (pal_entries << 2);
n_bytes = n_bytes_image + hsize; n_bytes = n_bytes_image + hsize;
if ((ret = ff_alloc_packet2(avctx, pkt, n_bytes, 0)) < 0) if ((ret = ff_get_encode_buffer(avctx, pkt, n_bytes, 0)) < 0)
return ret; return ret;
buf = pkt->data; buf = pkt->data;
bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType bytestream_put_byte(&buf, 'B'); // BITMAPFILEHEADER.bfType
@@ -140,9 +141,8 @@ static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
for(i = 0; i < avctx->height; i++) { for(i = 0; i < avctx->height; i++) {
if (bit_count == 16) { if (bit_count == 16) {
const uint16_t *src = (const uint16_t *) ptr; const uint16_t *src = (const uint16_t *) ptr;
uint16_t *dst = (uint16_t *) buf;
for(n = 0; n < avctx->width; n++) for(n = 0; n < avctx->width; n++)
AV_WL16(dst + n, src[n]); AV_WL16(buf + 2 * n, src[n]);
} else { } else {
memcpy(buf, ptr, n_bytes_per_row); memcpy(buf, ptr, n_bytes_per_row);
} }
@@ -162,6 +162,7 @@ const AVCodec ff_bmp_encoder = {
.long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"), .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_BMP, .id = AV_CODEC_ID_BMP,
.capabilities = AV_CODEC_CAP_DR1,
.init = bmp_encode_init, .init = bmp_encode_init,
.encode2 = bmp_encode_frame, .encode2 = bmp_encode_frame,
.pix_fmts = (const enum AVPixelFormat[]){ .pix_fmts = (const enum AVPixelFormat[]){