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.

358 lines
11KB

  1. /*
  2. * BMP image format decoder
  3. * Copyright (c) 2005 Mans Rullgard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <inttypes.h>
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "bmp.h"
  25. #include "internal.h"
  26. #include "msrledec.h"
  27. static int bmp_decode_frame(AVCodecContext *avctx,
  28. void *data, int *got_frame,
  29. AVPacket *avpkt)
  30. {
  31. const uint8_t *buf = avpkt->data;
  32. int buf_size = avpkt->size;
  33. AVFrame *p = data;
  34. unsigned int fsize, hsize;
  35. int width, height;
  36. unsigned int depth;
  37. BiCompression comp;
  38. unsigned int ihsize;
  39. int i, j, n, linesize, ret;
  40. uint32_t rgb[3];
  41. uint8_t *ptr;
  42. int dsize;
  43. const uint8_t *buf0 = buf;
  44. GetByteContext gb;
  45. if (buf_size < 14) {
  46. av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
  47. return AVERROR_INVALIDDATA;
  48. }
  49. if (bytestream_get_byte(&buf) != 'B' ||
  50. bytestream_get_byte(&buf) != 'M') {
  51. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  52. return AVERROR_INVALIDDATA;
  53. }
  54. fsize = bytestream_get_le32(&buf);
  55. if (buf_size < fsize) {
  56. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %u), trying to decode anyway\n",
  57. buf_size, fsize);
  58. fsize = buf_size;
  59. }
  60. buf += 2; /* reserved1 */
  61. buf += 2; /* reserved2 */
  62. hsize = bytestream_get_le32(&buf); /* header size */
  63. ihsize = bytestream_get_le32(&buf); /* more header size */
  64. if (ihsize + 14 > hsize) {
  65. av_log(avctx, AV_LOG_ERROR, "invalid header size %u\n", hsize);
  66. return AVERROR_INVALIDDATA;
  67. }
  68. /* sometimes file size is set to some headers size, set a real size in that case */
  69. if (fsize == 14 || fsize == ihsize + 14)
  70. fsize = buf_size - 2;
  71. if (fsize <= hsize) {
  72. av_log(avctx, AV_LOG_ERROR,
  73. "Declared file size is less than header size (%u < %u)\n",
  74. fsize, hsize);
  75. return AVERROR_INVALIDDATA;
  76. }
  77. switch (ihsize) {
  78. case 40: // windib v3
  79. case 64: // OS/2 v2
  80. case 108: // windib v4
  81. case 124: // windib v5
  82. width = bytestream_get_le32(&buf);
  83. height = bytestream_get_le32(&buf);
  84. break;
  85. case 12: // OS/2 v1
  86. width = bytestream_get_le16(&buf);
  87. height = bytestream_get_le16(&buf);
  88. break;
  89. default:
  90. av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
  91. return AVERROR_PATCHWELCOME;
  92. }
  93. /* planes */
  94. if (bytestream_get_le16(&buf) != 1) {
  95. av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
  96. return AVERROR_INVALIDDATA;
  97. }
  98. depth = bytestream_get_le16(&buf);
  99. if (ihsize == 40)
  100. comp = bytestream_get_le32(&buf);
  101. else
  102. comp = BMP_RGB;
  103. if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
  104. comp != BMP_RLE8) {
  105. av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
  106. return AVERROR_INVALIDDATA;
  107. }
  108. if (comp == BMP_BITFIELDS) {
  109. buf += 20;
  110. rgb[0] = bytestream_get_le32(&buf);
  111. rgb[1] = bytestream_get_le32(&buf);
  112. rgb[2] = bytestream_get_le32(&buf);
  113. }
  114. avctx->width = width;
  115. avctx->height = height > 0 ? height : -height;
  116. avctx->pix_fmt = AV_PIX_FMT_NONE;
  117. switch (depth) {
  118. case 32:
  119. if (comp == BMP_BITFIELDS) {
  120. rgb[0] = (rgb[0] >> 15) & 3;
  121. rgb[1] = (rgb[1] >> 15) & 3;
  122. rgb[2] = (rgb[2] >> 15) & 3;
  123. if (rgb[0] + rgb[1] + rgb[2] != 3 ||
  124. rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]) {
  125. break;
  126. }
  127. } else {
  128. rgb[0] = 2;
  129. rgb[1] = 1;
  130. rgb[2] = 0;
  131. }
  132. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  133. break;
  134. case 24:
  135. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  136. break;
  137. case 16:
  138. if (comp == BMP_RGB)
  139. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  140. else if (comp == BMP_BITFIELDS) {
  141. if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
  142. avctx->pix_fmt = AV_PIX_FMT_RGB565;
  143. else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
  144. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  145. else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
  146. avctx->pix_fmt = AV_PIX_FMT_RGB444;
  147. else {
  148. av_log(avctx, AV_LOG_ERROR,
  149. "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
  150. rgb[0], rgb[1], rgb[2]);
  151. return AVERROR(EINVAL);
  152. }
  153. }
  154. break;
  155. case 8:
  156. if (hsize - ihsize - 14 > 0)
  157. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  158. else
  159. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  160. break;
  161. case 1:
  162. case 4:
  163. if (hsize - ihsize - 14 > 0) {
  164. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  165. } else {
  166. av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
  167. 1 << depth);
  168. return AVERROR_INVALIDDATA;
  169. }
  170. break;
  171. default:
  172. av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
  173. return AVERROR_INVALIDDATA;
  174. }
  175. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  176. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  177. return AVERROR_INVALIDDATA;
  178. }
  179. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  180. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  181. return ret;
  182. }
  183. p->pict_type = AV_PICTURE_TYPE_I;
  184. p->key_frame = 1;
  185. buf = buf0 + hsize;
  186. dsize = buf_size - hsize;
  187. /* Line size in file multiple of 4 */
  188. n = ((avctx->width * depth) / 8 + 3) & ~3;
  189. if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
  190. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  191. dsize, n * avctx->height);
  192. return AVERROR_INVALIDDATA;
  193. }
  194. // RLE may skip decoding some picture areas, so blank picture before decoding
  195. if (comp == BMP_RLE4 || comp == BMP_RLE8)
  196. memset(p->data[0], 0, avctx->height * p->linesize[0]);
  197. if (height > 0) {
  198. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  199. linesize = -p->linesize[0];
  200. } else {
  201. ptr = p->data[0];
  202. linesize = p->linesize[0];
  203. }
  204. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  205. int colors = 1 << depth;
  206. memset(p->data[1], 0, 1024);
  207. if (ihsize >= 36) {
  208. int t;
  209. buf = buf0 + 46;
  210. t = bytestream_get_le32(&buf);
  211. if (t < 0 || t > (1 << depth)) {
  212. av_log(avctx, AV_LOG_ERROR,
  213. "Incorrect number of colors - %X for bitdepth %u\n",
  214. t, depth);
  215. } else if (t) {
  216. colors = t;
  217. }
  218. }
  219. buf = buf0 + 14 + ihsize; //palette location
  220. // OS/2 bitmap, 3 bytes per palette entry
  221. if ((hsize-ihsize-14) < (colors << 2)) {
  222. for (i = 0; i < colors; i++)
  223. ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
  224. } else {
  225. for (i = 0; i < colors; i++)
  226. ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
  227. }
  228. buf = buf0 + hsize;
  229. }
  230. if (comp == BMP_RLE4 || comp == BMP_RLE8) {
  231. if (height < 0) {
  232. p->data[0] += p->linesize[0] * (avctx->height - 1);
  233. p->linesize[0] = -p->linesize[0];
  234. }
  235. bytestream2_init(&gb, buf, dsize);
  236. ff_msrle_decode(avctx, p, depth, &gb);
  237. if (height < 0) {
  238. p->data[0] += p->linesize[0] * (avctx->height - 1);
  239. p->linesize[0] = -p->linesize[0];
  240. }
  241. } else {
  242. switch (depth) {
  243. case 1:
  244. for (i = 0; i < avctx->height; i++) {
  245. int j;
  246. for (j = 0; j < n; j++) {
  247. ptr[j*8+0] = buf[j] >> 7;
  248. ptr[j*8+1] = (buf[j] >> 6) & 1;
  249. ptr[j*8+2] = (buf[j] >> 5) & 1;
  250. ptr[j*8+3] = (buf[j] >> 4) & 1;
  251. ptr[j*8+4] = (buf[j] >> 3) & 1;
  252. ptr[j*8+5] = (buf[j] >> 2) & 1;
  253. ptr[j*8+6] = (buf[j] >> 1) & 1;
  254. ptr[j*8+7] = buf[j] & 1;
  255. }
  256. buf += n;
  257. ptr += linesize;
  258. }
  259. break;
  260. case 8:
  261. case 24:
  262. for (i = 0; i < avctx->height; i++) {
  263. memcpy(ptr, buf, n);
  264. buf += n;
  265. ptr += linesize;
  266. }
  267. break;
  268. case 4:
  269. for (i = 0; i < avctx->height; i++) {
  270. int j;
  271. for (j = 0; j < n; j++) {
  272. ptr[j*2+0] = (buf[j] >> 4) & 0xF;
  273. ptr[j*2+1] = buf[j] & 0xF;
  274. }
  275. buf += n;
  276. ptr += linesize;
  277. }
  278. break;
  279. case 16:
  280. for (i = 0; i < avctx->height; i++) {
  281. const uint16_t *src = (const uint16_t *) buf;
  282. uint16_t *dst = (uint16_t *) ptr;
  283. for (j = 0; j < avctx->width; j++)
  284. *dst++ = av_le2ne16(*src++);
  285. buf += n;
  286. ptr += linesize;
  287. }
  288. break;
  289. case 32:
  290. for (i = 0; i < avctx->height; i++) {
  291. const uint8_t *src = buf;
  292. uint8_t *dst = ptr;
  293. for (j = 0; j < avctx->width; j++) {
  294. dst[0] = src[rgb[2]];
  295. dst[1] = src[rgb[1]];
  296. dst[2] = src[rgb[0]];
  297. dst += 3;
  298. src += 4;
  299. }
  300. buf += n;
  301. ptr += linesize;
  302. }
  303. break;
  304. default:
  305. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  306. return AVERROR_INVALIDDATA;
  307. }
  308. }
  309. *got_frame = 1;
  310. return buf_size;
  311. }
  312. AVCodec ff_bmp_decoder = {
  313. .name = "bmp",
  314. .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
  315. .type = AVMEDIA_TYPE_VIDEO,
  316. .id = AV_CODEC_ID_BMP,
  317. .decode = bmp_decode_frame,
  318. .capabilities = AV_CODEC_CAP_DR1,
  319. };