You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
11KB

  1. /*
  2. * BMP image format decoder
  3. * Copyright (c) 2005 Mans Rullgard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "bytestream.h"
  23. #include "bmp.h"
  24. #include "msrledec.h"
  25. static av_cold int bmp_decode_init(AVCodecContext *avctx){
  26. BMPContext *s = avctx->priv_data;
  27. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  28. avctx->coded_frame = (AVFrame*)&s->picture;
  29. return 0;
  30. }
  31. static int bmp_decode_frame(AVCodecContext *avctx,
  32. void *data, int *data_size,
  33. AVPacket *avpkt)
  34. {
  35. const uint8_t *buf = avpkt->data;
  36. int buf_size = avpkt->size;
  37. BMPContext *s = avctx->priv_data;
  38. AVFrame *picture = data;
  39. AVFrame *p = &s->picture;
  40. unsigned int fsize, hsize;
  41. int width, height;
  42. unsigned int depth;
  43. BiCompression comp;
  44. unsigned int ihsize;
  45. int i, j, n, linesize;
  46. uint32_t rgb[3];
  47. uint8_t *ptr;
  48. int dsize;
  49. const uint8_t *buf0 = buf;
  50. if(buf_size < 14){
  51. av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
  52. return -1;
  53. }
  54. if(bytestream_get_byte(&buf) != 'B' ||
  55. bytestream_get_byte(&buf) != 'M') {
  56. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  57. return -1;
  58. }
  59. fsize = bytestream_get_le32(&buf);
  60. if(buf_size < fsize){
  61. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
  62. buf_size, fsize);
  63. fsize = buf_size;
  64. }
  65. buf += 2; /* reserved1 */
  66. buf += 2; /* reserved2 */
  67. hsize = bytestream_get_le32(&buf); /* header size */
  68. ihsize = bytestream_get_le32(&buf); /* more header size */
  69. if(ihsize + 14 > hsize){
  70. av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
  71. return -1;
  72. }
  73. /* sometimes file size is set to some headers size, set a real size in that case */
  74. if(fsize == 14 || fsize == ihsize + 14)
  75. fsize = buf_size - 2;
  76. if(fsize <= hsize){
  77. av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
  78. fsize, hsize);
  79. return -1;
  80. }
  81. switch(ihsize){
  82. case 40: // windib
  83. case 56: // windib v3
  84. case 64: // OS/2 v2
  85. case 108: // windib v4
  86. case 124: // windib v5
  87. width = bytestream_get_le32(&buf);
  88. height = bytestream_get_le32(&buf);
  89. break;
  90. case 12: // OS/2 v1
  91. width = bytestream_get_le16(&buf);
  92. height = bytestream_get_le16(&buf);
  93. break;
  94. default:
  95. av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
  96. return -1;
  97. }
  98. if(bytestream_get_le16(&buf) != 1){ /* planes */
  99. av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
  100. return -1;
  101. }
  102. depth = bytestream_get_le16(&buf);
  103. if(ihsize == 40 || ihsize == 64 || ihsize == 56)
  104. comp = bytestream_get_le32(&buf);
  105. else
  106. comp = BMP_RGB;
  107. if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
  108. av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
  109. return -1;
  110. }
  111. if(comp == BMP_BITFIELDS){
  112. buf += 20;
  113. rgb[0] = bytestream_get_le32(&buf);
  114. rgb[1] = bytestream_get_le32(&buf);
  115. rgb[2] = bytestream_get_le32(&buf);
  116. }
  117. avctx->width = width;
  118. avctx->height = height > 0? height: -height;
  119. avctx->pix_fmt = PIX_FMT_NONE;
  120. switch(depth){
  121. case 32:
  122. if(comp == BMP_BITFIELDS){
  123. rgb[0] = (rgb[0] >> 15) & 3;
  124. rgb[1] = (rgb[1] >> 15) & 3;
  125. rgb[2] = (rgb[2] >> 15) & 3;
  126. if(rgb[0] + rgb[1] + rgb[2] != 3 ||
  127. rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
  128. break;
  129. }
  130. } else {
  131. rgb[0] = 2;
  132. rgb[1] = 1;
  133. rgb[2] = 0;
  134. }
  135. avctx->pix_fmt = PIX_FMT_BGRA;
  136. break;
  137. case 24:
  138. avctx->pix_fmt = PIX_FMT_BGR24;
  139. break;
  140. case 16:
  141. if(comp == BMP_RGB)
  142. avctx->pix_fmt = PIX_FMT_RGB555;
  143. if(comp == BMP_BITFIELDS)
  144. avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
  145. break;
  146. case 8:
  147. if(hsize - ihsize - 14 > 0)
  148. avctx->pix_fmt = PIX_FMT_PAL8;
  149. else
  150. avctx->pix_fmt = PIX_FMT_GRAY8;
  151. break;
  152. case 1:
  153. case 4:
  154. if(hsize - ihsize - 14 > 0){
  155. avctx->pix_fmt = PIX_FMT_PAL8;
  156. }else{
  157. av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
  158. return -1;
  159. }
  160. break;
  161. default:
  162. av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
  163. return -1;
  164. }
  165. if(avctx->pix_fmt == PIX_FMT_NONE){
  166. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  167. return -1;
  168. }
  169. if(p->data[0])
  170. avctx->release_buffer(avctx, p);
  171. p->reference = 0;
  172. if(avctx->get_buffer(avctx, p) < 0){
  173. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  174. return -1;
  175. }
  176. p->pict_type = AV_PICTURE_TYPE_I;
  177. p->key_frame = 1;
  178. buf = buf0 + hsize;
  179. dsize = buf_size - hsize;
  180. /* Line size in file multiple of 4 */
  181. n = ((avctx->width * depth + 31) / 8) & ~3;
  182. if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
  183. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  184. dsize, n * avctx->height);
  185. return -1;
  186. }
  187. // RLE may skip decoding some picture areas, so blank picture before decoding
  188. if(comp == BMP_RLE4 || comp == BMP_RLE8)
  189. memset(p->data[0], 0, avctx->height * p->linesize[0]);
  190. if(depth == 4 || depth == 8)
  191. memset(p->data[1], 0, 1024);
  192. if(height > 0){
  193. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  194. linesize = -p->linesize[0];
  195. } else {
  196. ptr = p->data[0];
  197. linesize = p->linesize[0];
  198. }
  199. if(avctx->pix_fmt == PIX_FMT_PAL8){
  200. int colors = 1 << depth;
  201. if(ihsize >= 36){
  202. int t;
  203. buf = buf0 + 46;
  204. t = bytestream_get_le32(&buf);
  205. if(t < 0 || t > (1 << depth)){
  206. av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
  207. }else if(t){
  208. colors = t;
  209. }
  210. }
  211. buf = buf0 + 14 + ihsize; //palette location
  212. if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
  213. for(i = 0; i < colors; i++)
  214. ((uint32_t*)p->data[1])[i] = (0xff<<24) | bytestream_get_le24(&buf);
  215. }else{
  216. for(i = 0; i < colors; i++)
  217. ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
  218. }
  219. buf = buf0 + hsize;
  220. }
  221. if(comp == BMP_RLE4 || comp == BMP_RLE8){
  222. if(height < 0){
  223. p->data[0] += p->linesize[0] * (avctx->height - 1);
  224. p->linesize[0] = -p->linesize[0];
  225. }
  226. ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
  227. if(height < 0){
  228. p->data[0] += p->linesize[0] * (avctx->height - 1);
  229. p->linesize[0] = -p->linesize[0];
  230. }
  231. }else{
  232. switch(depth){
  233. case 1:
  234. for (i = 0; i < avctx->height; i++) {
  235. int j;
  236. for (j = 0; j < n; j++) {
  237. ptr[j*8+0] = buf[j] >> 7;
  238. ptr[j*8+1] = (buf[j] >> 6) & 1;
  239. ptr[j*8+2] = (buf[j] >> 5) & 1;
  240. ptr[j*8+3] = (buf[j] >> 4) & 1;
  241. ptr[j*8+4] = (buf[j] >> 3) & 1;
  242. ptr[j*8+5] = (buf[j] >> 2) & 1;
  243. ptr[j*8+6] = (buf[j] >> 1) & 1;
  244. ptr[j*8+7] = buf[j] & 1;
  245. }
  246. buf += n;
  247. ptr += linesize;
  248. }
  249. break;
  250. case 8:
  251. case 24:
  252. for(i = 0; i < avctx->height; i++){
  253. memcpy(ptr, buf, n);
  254. buf += n;
  255. ptr += linesize;
  256. }
  257. break;
  258. case 4:
  259. for(i = 0; i < avctx->height; i++){
  260. int j;
  261. for(j = 0; j < n; j++){
  262. ptr[j*2+0] = (buf[j] >> 4) & 0xF;
  263. ptr[j*2+1] = buf[j] & 0xF;
  264. }
  265. buf += n;
  266. ptr += linesize;
  267. }
  268. break;
  269. case 16:
  270. for(i = 0; i < avctx->height; i++){
  271. const uint16_t *src = (const uint16_t *) buf;
  272. uint16_t *dst = (uint16_t *) ptr;
  273. for(j = 0; j < avctx->width; j++)
  274. *dst++ = av_le2ne16(*src++);
  275. buf += n;
  276. ptr += linesize;
  277. }
  278. break;
  279. case 32:
  280. for(i = 0; i < avctx->height; i++){
  281. const uint8_t *src = buf;
  282. uint8_t *dst = ptr;
  283. for(j = 0; j < avctx->width; j++){
  284. dst[0] = src[rgb[2]];
  285. dst[1] = src[rgb[1]];
  286. dst[2] = src[rgb[0]];
  287. /* The Microsoft documentation states:
  288. * "The high byte in each DWORD is not used."
  289. * Both GIMP and ImageMagick store the alpha transparency value
  290. * in the high byte for 32bit bmp files.
  291. */
  292. dst[3] = src[3];
  293. dst += 4;
  294. src += 4;
  295. }
  296. buf += n;
  297. ptr += linesize;
  298. }
  299. break;
  300. default:
  301. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  302. return -1;
  303. }
  304. }
  305. *picture = s->picture;
  306. *data_size = sizeof(AVPicture);
  307. return buf_size;
  308. }
  309. static av_cold int bmp_decode_end(AVCodecContext *avctx)
  310. {
  311. BMPContext* c = avctx->priv_data;
  312. if (c->picture.data[0])
  313. avctx->release_buffer(avctx, &c->picture);
  314. return 0;
  315. }
  316. AVCodec ff_bmp_decoder = {
  317. .name = "bmp",
  318. .type = AVMEDIA_TYPE_VIDEO,
  319. .id = CODEC_ID_BMP,
  320. .priv_data_size = sizeof(BMPContext),
  321. .init = bmp_decode_init,
  322. .close = bmp_decode_end,
  323. .decode = bmp_decode_frame,
  324. .capabilities = CODEC_CAP_DR1,
  325. .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
  326. };