* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <inttypes.h>
+
#include "avcodec.h"
#include "bytestream.h"
#include "bmp.h"
#include "internal.h"
#include "msrledec.h"
-static av_cold int bmp_decode_init(AVCodecContext *avctx)
-{
- BMPContext *s = avctx->priv_data;
-
- avcodec_get_frame_defaults(&s->picture);
- avctx->coded_frame = &s->picture;
-
- return 0;
-}
-
static int bmp_decode_frame(AVCodecContext *avctx,
void *data, int *got_frame,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
- BMPContext *s = avctx->priv_data;
- AVFrame *picture = data;
- AVFrame *p = &s->picture;
+ AVFrame *p = data;
unsigned int fsize, hsize;
int width, height;
unsigned int depth;
BiCompression comp;
unsigned int ihsize;
- int i, j, n, linesize;
+ int i, j, n, linesize, ret;
uint32_t rgb[3];
uint8_t *ptr;
int dsize;
if (buf_size < 14) {
av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
- return -1;
+ return AVERROR_INVALIDDATA;
}
if (bytestream_get_byte(&buf) != 'B' ||
bytestream_get_byte(&buf) != 'M') {
av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
- return -1;
+ return AVERROR_INVALIDDATA;
}
fsize = bytestream_get_le32(&buf);
if (buf_size < fsize) {
- av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
+ av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %u), trying to decode anyway\n",
buf_size, fsize);
fsize = buf_size;
}
hsize = bytestream_get_le32(&buf); /* header size */
ihsize = bytestream_get_le32(&buf); /* more header size */
if (ihsize + 14 > hsize) {
- av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
- return -1;
+ av_log(avctx, AV_LOG_ERROR, "invalid header size %u\n", hsize);
+ return AVERROR_INVALIDDATA;
}
/* sometimes file size is set to some headers size, set a real size in that case */
fsize = buf_size - 2;
if (fsize <= hsize) {
- av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
+ av_log(avctx, AV_LOG_ERROR,
+ "Declared file size is less than header size (%u < %u)\n",
fsize, hsize);
- return -1;
+ return AVERROR_INVALIDDATA;
}
switch (ihsize) {
height = bytestream_get_le16(&buf);
break;
default:
- av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
- return -1;
+ avpriv_report_missing_feature(avctx, "Information header size %u",
+ ihsize);
+ return AVERROR_PATCHWELCOME;
}
/* planes */
if (bytestream_get_le16(&buf) != 1) {
av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
- return -1;
+ return AVERROR_INVALIDDATA;
}
depth = bytestream_get_le16(&buf);
if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
comp != BMP_RLE8) {
av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
- return -1;
+ return AVERROR_INVALIDDATA;
}
if (comp == BMP_BITFIELDS) {
else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
avctx->pix_fmt = AV_PIX_FMT_RGB444;
else {
- av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
+ av_log(avctx, AV_LOG_ERROR,
+ "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
+ rgb[0], rgb[1], rgb[2]);
return AVERROR(EINVAL);
}
}
if (hsize - ihsize - 14 > 0) {
avctx->pix_fmt = AV_PIX_FMT_PAL8;
} else {
- av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
- return -1;
+ av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
+ 1 << depth);
+ return AVERROR_INVALIDDATA;
}
break;
default:
- av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
- return -1;
+ av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
+ return AVERROR_INVALIDDATA;
}
if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
- return -1;
+ return AVERROR_INVALIDDATA;
}
- if (p->data[0])
- avctx->release_buffer(avctx, p);
-
- p->reference = 0;
- if (ff_get_buffer(avctx, p) < 0) {
+ if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
- return -1;
+ return ret;
}
p->pict_type = AV_PICTURE_TYPE_I;
p->key_frame = 1;
if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
dsize, n * avctx->height);
- return -1;
+ return AVERROR_INVALIDDATA;
}
// RLE may skip decoding some picture areas, so blank picture before decoding
buf = buf0 + 46;
t = bytestream_get_le32(&buf);
if (t < 0 || t > (1 << depth)) {
- av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
+ av_log(avctx, AV_LOG_ERROR,
+ "Incorrect number of colors - %X for bitdepth %u\n",
+ t, depth);
} else if (t) {
colors = t;
}
p->linesize[0] = -p->linesize[0];
}
bytestream2_init(&gb, buf, dsize);
- ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
+ ff_msrle_decode(avctx, p, depth, &gb);
if (height < 0) {
p->data[0] += p->linesize[0] * (avctx->height - 1);
p->linesize[0] = -p->linesize[0];
break;
default:
av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
- return -1;
+ return AVERROR_INVALIDDATA;
}
}
- *picture = s->picture;
*got_frame = 1;
return buf_size;
}
-static av_cold int bmp_decode_end(AVCodecContext *avctx)
-{
- BMPContext* c = avctx->priv_data;
-
- if (c->picture.data[0])
- avctx->release_buffer(avctx, &c->picture);
-
- return 0;
-}
-
AVCodec ff_bmp_decoder = {
.name = "bmp",
+ .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_BMP,
- .priv_data_size = sizeof(BMPContext),
- .init = bmp_decode_init,
- .close = bmp_decode_end,
.decode = bmp_decode_frame,
- .capabilities = CODEC_CAP_DR1,
- .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
+ .capabilities = AV_CODEC_CAP_DR1,
};