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.

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