annotate bmp.c @ 8117:a0f9045e0a82 libavcodec

Promote inv_aanscales array to global scope (ff_inv_aanscales)
author pross
date Sat, 08 Nov 2008 00:15:08 +0000
parents a7b88e5846f6
children 5af44bd71254
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
1 /*
4415
f792b146869b Segregate code common to BMP decoder and future encoder
diego
parents: 4394
diff changeset
2 * BMP image format decoder
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
3 * Copyright (c) 2005 Mans Rullgard
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
15 * Lesser General Public License for more details.
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
16 *
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
20 */
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
21
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
22 #include "avcodec.h"
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
23 #include "bytestream.h"
4415
f792b146869b Segregate code common to BMP decoder and future encoder
diego
parents: 4394
diff changeset
24 #include "bmp.h"
7910
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
25 #include "msrledec.h"
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
26
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6265
diff changeset
27 static av_cold int bmp_decode_init(AVCodecContext *avctx){
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
28 BMPContext *s = avctx->priv_data;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
29
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
30 avcodec_get_frame_defaults((AVFrame*)&s->picture);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
31 avctx->coded_frame = (AVFrame*)&s->picture;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
32
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
33 return 0;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
34 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
35
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2949
diff changeset
36 static int bmp_decode_frame(AVCodecContext *avctx,
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
37 void *data, int *data_size,
6265
michael
parents: 5327
diff changeset
38 const uint8_t *buf, int buf_size)
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
39 {
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
40 BMPContext *s = avctx->priv_data;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
41 AVFrame *picture = data;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
42 AVFrame *p = &s->picture;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
43 unsigned int fsize, hsize;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
44 int width, height;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
45 unsigned int depth;
4393
michael
parents: 4109
diff changeset
46 BiCompression comp;
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
47 unsigned int ihsize;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
48 int i, j, n, linesize;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
49 uint32_t rgb[3];
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
50 uint8_t *ptr;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
51 int dsize;
6265
michael
parents: 5327
diff changeset
52 const uint8_t *buf0 = buf;
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
53
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
54 if(buf_size < 14){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
55 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
56 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
57 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
58
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
59 if(bytestream_get_byte(&buf) != 'B' ||
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
60 bytestream_get_byte(&buf) != 'M') {
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
61 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
62 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
63 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
64
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
65 fsize = bytestream_get_le32(&buf);
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
66 if(buf_size < fsize){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
67 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
68 buf_size, fsize);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
69 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
70 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
71
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
72 buf += 2; /* reserved1 */
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
73 buf += 2; /* reserved2 */
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
74
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
75 hsize = bytestream_get_le32(&buf); /* header size */
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
76 if(fsize <= hsize){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
77 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
78 fsize, hsize);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
79 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
80 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
81
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
82 ihsize = bytestream_get_le32(&buf); /* more header size */
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
83 if(ihsize + 14 > hsize){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
84 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
85 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
86 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
87
6594
e094b1b11e1d Support for BMP os2v1.
benoit
parents: 6517
diff changeset
88 if (ihsize == 40) {
6595
92eb6af449dc Indentation
benoit
parents: 6594
diff changeset
89 width = bytestream_get_le32(&buf);
92eb6af449dc Indentation
benoit
parents: 6594
diff changeset
90 height = bytestream_get_le32(&buf);
6594
e094b1b11e1d Support for BMP os2v1.
benoit
parents: 6517
diff changeset
91 } else if (ihsize == 12) {
e094b1b11e1d Support for BMP os2v1.
benoit
parents: 6517
diff changeset
92 width = bytestream_get_le16(&buf);
e094b1b11e1d Support for BMP os2v1.
benoit
parents: 6517
diff changeset
93 height = bytestream_get_le16(&buf);
e094b1b11e1d Support for BMP os2v1.
benoit
parents: 6517
diff changeset
94 } else {
7887
44da2504ce69 add newline at the end of message
kostya
parents: 7040
diff changeset
95 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
6594
e094b1b11e1d Support for BMP os2v1.
benoit
parents: 6517
diff changeset
96 return -1;
e094b1b11e1d Support for BMP os2v1.
benoit
parents: 6517
diff changeset
97 }
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
98
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
99 if(bytestream_get_le16(&buf) != 1){ /* planes */
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
100 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
101 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
102 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
103
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
104 depth = bytestream_get_le16(&buf);
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
105
6594
e094b1b11e1d Support for BMP os2v1.
benoit
parents: 6517
diff changeset
106 if(ihsize == 40)
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
107 comp = bytestream_get_le32(&buf);
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
108 else
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
109 comp = BMP_RGB;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
110
7910
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
111 if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
112 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
113 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
114 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
115
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
116 if(comp == BMP_BITFIELDS){
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
117 buf += 20;
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
118 rgb[0] = bytestream_get_le32(&buf);
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
119 rgb[1] = bytestream_get_le32(&buf);
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
120 rgb[2] = bytestream_get_le32(&buf);
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
121 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
122
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
123 avctx->width = width;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
124 avctx->height = height > 0? height: -height;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
125
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
126 avctx->pix_fmt = PIX_FMT_NONE;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
127
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
128 switch(depth){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
129 case 32:
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
130 if(comp == BMP_BITFIELDS){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
131 rgb[0] = (rgb[0] >> 15) & 3;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
132 rgb[1] = (rgb[1] >> 15) & 3;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
133 rgb[2] = (rgb[2] >> 15) & 3;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
134
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
135 if(rgb[0] + rgb[1] + rgb[2] != 3 ||
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
136 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
137 break;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
138 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
139 } else {
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
140 rgb[0] = 2;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
141 rgb[1] = 1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
142 rgb[2] = 0;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
143 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
144
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
145 avctx->pix_fmt = PIX_FMT_BGR24;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
146 break;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
147 case 24:
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
148 avctx->pix_fmt = PIX_FMT_BGR24;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
149 break;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
150 case 16:
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
151 if(comp == BMP_RGB)
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
152 avctx->pix_fmt = PIX_FMT_RGB555;
7909
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
153 if(comp == BMP_BITFIELDS)
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
154 avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
155 break;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
156 case 8:
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
157 if(hsize - ihsize - 14 > 0)
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
158 avctx->pix_fmt = PIX_FMT_PAL8;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
159 else
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
160 avctx->pix_fmt = PIX_FMT_GRAY8;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
161 break;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
162 case 4:
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
163 if(hsize - ihsize - 14 > 0){
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
164 avctx->pix_fmt = PIX_FMT_PAL8;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
165 }else{
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
166 av_log(avctx, AV_LOG_ERROR, "Unknown palette for 16-colour BMP\n");
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
167 return -1;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
168 }
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
169 break;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
170 case 1:
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
171 avctx->pix_fmt = PIX_FMT_MONOBLACK;
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
172 break;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
173 default:
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
174 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
175 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
176 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
177
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
178 if(avctx->pix_fmt == PIX_FMT_NONE){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
179 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
180 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
181 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
182
4432
a848b652f0ac Fix segfault in bmp decoder. Patch by Michel Bardiaux mbardiaux mediaxim dot be.
takis
parents: 4415
diff changeset
183 if(p->data[0])
a848b652f0ac Fix segfault in bmp decoder. Patch by Michel Bardiaux mbardiaux mediaxim dot be.
takis
parents: 4415
diff changeset
184 avctx->release_buffer(avctx, p);
a848b652f0ac Fix segfault in bmp decoder. Patch by Michel Bardiaux mbardiaux mediaxim dot be.
takis
parents: 4415
diff changeset
185
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
186 p->reference = 0;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
187 if(avctx->get_buffer(avctx, p) < 0){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
188 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
189 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
190 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
191 p->pict_type = FF_I_TYPE;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
192 p->key_frame = 1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
193
4439
3af19e140d67 Make BMP decoder use bytestream. Patch by Michel Bardiaux
takis
parents: 4432
diff changeset
194 buf = buf0 + hsize;
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
195 dsize = buf_size - hsize;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
196
4109
a90490a13ac4 coded line size is a multiple of 4
mru
parents: 3947
diff changeset
197 /* Line size in file multiple of 4 */
7908
dc825905f93f Calculate line size correctly for bit depths < 8
kostya
parents: 7887
diff changeset
198 n = ((avctx->width * depth) / 8 + 3) & ~3;
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
199
7910
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
200 if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
201 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
202 dsize, n * avctx->height);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
203 return -1;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
204 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
205
7910
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
206 // RLE may skip decoding some picture areas, so blank picture before decoding
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
207 if(comp == BMP_RLE4 || comp == BMP_RLE8)
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
208 memset(p->data[0], 0, avctx->height * p->linesize[0]);
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
209
7909
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
210 if(depth == 4 || depth == 8)
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
211 memset(p->data[1], 0, 1024);
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
212
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
213 if(height > 0){
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
214 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
215 linesize = -p->linesize[0];
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
216 } else {
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
217 ptr = p->data[0];
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
218 linesize = p->linesize[0];
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
219 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
220
7909
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
221 if(avctx->pix_fmt == PIX_FMT_PAL8){
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
222 buf = buf0 + 14 + ihsize; //palette location
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
223 if((hsize-ihsize-14)>>depth < 4){ // OS/2 bitmap, 3 bytes per palette entry
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
224 for(i = 0; i < (1 << depth); i++)
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
225 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
226 }else{
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
227 for(i = 0; i < (1 << depth); i++)
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
228 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
229 }
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
230 buf = buf0 + hsize;
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
231 }
7910
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
232 if(comp == BMP_RLE4 || comp == BMP_RLE8){
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
233 ff_msrle_decode(avctx, p, depth, buf, dsize);
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
234 }else{
7911
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
235 switch(depth){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
236 case 1:
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
237 for(i = 0; i < avctx->height; i++){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
238 memcpy(ptr, buf, n);
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
239 buf += n;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
240 ptr += linesize;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
241 }
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
242 break;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
243 case 4:
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
244 for(i = 0; i < avctx->height; i++){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
245 int j;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
246 for(j = 0; j < n; j++){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
247 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
248 ptr[j*2+1] = buf[j] & 0xF;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
249 }
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
250 buf += n;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
251 ptr += linesize;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
252 }
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
253 break;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
254 case 8:
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
255 for(i = 0; i < avctx->height; i++){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
256 memcpy(ptr, buf, avctx->width);
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
257 buf += n;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
258 ptr += linesize;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
259 }
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
260 break;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
261 case 24:
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
262 for(i = 0; i < avctx->height; i++){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
263 memcpy(ptr, buf, avctx->width*(depth>>3));
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
264 buf += n;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
265 ptr += linesize;
7909
9fd3f57e4c16 Add support for 1-bit, 4-bit, 8-bit and some 16-bit raw BMP
kostya
parents: 7908
diff changeset
266 }
7911
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
267 break;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
268 case 16:
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
269 for(i = 0; i < avctx->height; i++){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
270 const uint16_t *src = (const uint16_t *) buf;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
271 uint16_t *dst = (uint16_t *) ptr;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
272
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
273 for(j = 0; j < avctx->width; j++)
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
274 *dst++ = le2me_16(*src++);
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
275
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
276 buf += n;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
277 ptr += linesize;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
278 }
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
279 break;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
280 case 32:
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
281 for(i = 0; i < avctx->height; i++){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
282 const uint8_t *src = buf;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
283 uint8_t *dst = ptr;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
284
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
285 for(j = 0; j < avctx->width; j++){
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
286 dst[0] = src[rgb[2]];
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
287 dst[1] = src[rgb[1]];
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
288 dst[2] = src[rgb[0]];
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
289 dst += 3;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
290 src += 4;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
291 }
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
292
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
293 buf += n;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
294 ptr += linesize;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
295 }
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
296 break;
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
297 default:
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
298 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
a7b88e5846f6 Cosmetics: reindent after last commit
kostya
parents: 7910
diff changeset
299 return -1;
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
300 }
7910
87bbd8322244 Add RLE4 and RLE8 decoding support for BMP
kostya
parents: 7909
diff changeset
301 }
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
302
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
303 *picture = s->picture;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
304 *data_size = sizeof(AVPicture);
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
305
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
306 return buf_size;
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
307 }
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
308
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6265
diff changeset
309 static av_cold int bmp_decode_end(AVCodecContext *avctx)
4455
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
310 {
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
311 BMPContext* c = avctx->priv_data;
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
312
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
313 if (c->picture.data[0])
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
314 avctx->release_buffer(avctx, &c->picture);
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
315
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
316 return 0;
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
317 }
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
318
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
319 AVCodec bmp_decoder = {
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
320 "bmp",
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
321 CODEC_TYPE_VIDEO,
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
322 CODEC_ID_BMP,
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
323 sizeof(BMPContext),
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
324 bmp_decode_init,
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
325 NULL,
4455
8ecfb7ecbb53 Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
takis
parents: 4445
diff changeset
326 bmp_decode_end,
6722
6eeb19edcee3 Add long names to some AVCodec declarations.
diego
parents: 6595
diff changeset
327 bmp_decode_frame,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6722
diff changeset
328 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
2949
e5a10ae14ffb BMP image decoder
mru
parents:
diff changeset
329 };