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.

359 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. avpriv_report_missing_feature(avctx, "Information header size %u",
  91. ihsize);
  92. return AVERROR_PATCHWELCOME;
  93. }
  94. /* planes */
  95. if (bytestream_get_le16(&buf) != 1) {
  96. av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
  97. return AVERROR_INVALIDDATA;
  98. }
  99. depth = bytestream_get_le16(&buf);
  100. if (ihsize == 40)
  101. comp = bytestream_get_le32(&buf);
  102. else
  103. comp = BMP_RGB;
  104. if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
  105. comp != BMP_RLE8) {
  106. av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
  107. return AVERROR_INVALIDDATA;
  108. }
  109. if (comp == BMP_BITFIELDS) {
  110. buf += 20;
  111. rgb[0] = bytestream_get_le32(&buf);
  112. rgb[1] = bytestream_get_le32(&buf);
  113. rgb[2] = bytestream_get_le32(&buf);
  114. }
  115. avctx->width = width;
  116. avctx->height = height > 0 ? height : -height;
  117. avctx->pix_fmt = AV_PIX_FMT_NONE;
  118. switch (depth) {
  119. case 32:
  120. if (comp == BMP_BITFIELDS) {
  121. rgb[0] = (rgb[0] >> 15) & 3;
  122. rgb[1] = (rgb[1] >> 15) & 3;
  123. rgb[2] = (rgb[2] >> 15) & 3;
  124. if (rgb[0] + rgb[1] + rgb[2] != 3 ||
  125. rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]) {
  126. break;
  127. }
  128. } else {
  129. rgb[0] = 2;
  130. rgb[1] = 1;
  131. rgb[2] = 0;
  132. }
  133. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  134. break;
  135. case 24:
  136. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  137. break;
  138. case 16:
  139. if (comp == BMP_RGB)
  140. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  141. else if (comp == BMP_BITFIELDS) {
  142. if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
  143. avctx->pix_fmt = AV_PIX_FMT_RGB565;
  144. else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
  145. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  146. else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
  147. avctx->pix_fmt = AV_PIX_FMT_RGB444;
  148. else {
  149. av_log(avctx, AV_LOG_ERROR,
  150. "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
  151. rgb[0], rgb[1], rgb[2]);
  152. return AVERROR(EINVAL);
  153. }
  154. }
  155. break;
  156. case 8:
  157. if (hsize - ihsize - 14 > 0)
  158. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  159. else
  160. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  161. break;
  162. case 1:
  163. case 4:
  164. if (hsize - ihsize - 14 > 0) {
  165. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  166. } else {
  167. av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
  168. 1 << depth);
  169. return AVERROR_INVALIDDATA;
  170. }
  171. break;
  172. default:
  173. av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
  174. return AVERROR_INVALIDDATA;
  175. }
  176. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  177. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  178. return AVERROR_INVALIDDATA;
  179. }
  180. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  181. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  182. return ret;
  183. }
  184. p->pict_type = AV_PICTURE_TYPE_I;
  185. p->key_frame = 1;
  186. buf = buf0 + hsize;
  187. dsize = buf_size - hsize;
  188. /* Line size in file multiple of 4 */
  189. n = ((avctx->width * depth) / 8 + 3) & ~3;
  190. if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
  191. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  192. dsize, n * avctx->height);
  193. return AVERROR_INVALIDDATA;
  194. }
  195. // RLE may skip decoding some picture areas, so blank picture before decoding
  196. if (comp == BMP_RLE4 || comp == BMP_RLE8)
  197. memset(p->data[0], 0, avctx->height * p->linesize[0]);
  198. if (height > 0) {
  199. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  200. linesize = -p->linesize[0];
  201. } else {
  202. ptr = p->data[0];
  203. linesize = p->linesize[0];
  204. }
  205. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  206. int colors = 1 << depth;
  207. memset(p->data[1], 0, 1024);
  208. if (ihsize >= 36) {
  209. int t;
  210. buf = buf0 + 46;
  211. t = bytestream_get_le32(&buf);
  212. if (t < 0 || t > (1 << depth)) {
  213. av_log(avctx, AV_LOG_ERROR,
  214. "Incorrect number of colors - %X for bitdepth %u\n",
  215. t, depth);
  216. } else if (t) {
  217. colors = t;
  218. }
  219. }
  220. buf = buf0 + 14 + ihsize; //palette location
  221. // OS/2 bitmap, 3 bytes per palette entry
  222. if ((hsize-ihsize-14) < (colors << 2)) {
  223. for (i = 0; i < colors; i++)
  224. ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
  225. } else {
  226. for (i = 0; i < colors; i++)
  227. ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
  228. }
  229. buf = buf0 + hsize;
  230. }
  231. if (comp == BMP_RLE4 || comp == BMP_RLE8) {
  232. if (height < 0) {
  233. p->data[0] += p->linesize[0] * (avctx->height - 1);
  234. p->linesize[0] = -p->linesize[0];
  235. }
  236. bytestream2_init(&gb, buf, dsize);
  237. ff_msrle_decode(avctx, p, depth, &gb);
  238. if (height < 0) {
  239. p->data[0] += p->linesize[0] * (avctx->height - 1);
  240. p->linesize[0] = -p->linesize[0];
  241. }
  242. } else {
  243. switch (depth) {
  244. case 1:
  245. for (i = 0; i < avctx->height; i++) {
  246. int j;
  247. for (j = 0; j < n; j++) {
  248. ptr[j*8+0] = buf[j] >> 7;
  249. ptr[j*8+1] = (buf[j] >> 6) & 1;
  250. ptr[j*8+2] = (buf[j] >> 5) & 1;
  251. ptr[j*8+3] = (buf[j] >> 4) & 1;
  252. ptr[j*8+4] = (buf[j] >> 3) & 1;
  253. ptr[j*8+5] = (buf[j] >> 2) & 1;
  254. ptr[j*8+6] = (buf[j] >> 1) & 1;
  255. ptr[j*8+7] = buf[j] & 1;
  256. }
  257. buf += n;
  258. ptr += linesize;
  259. }
  260. break;
  261. case 8:
  262. case 24:
  263. for (i = 0; i < avctx->height; i++) {
  264. memcpy(ptr, buf, n);
  265. buf += n;
  266. ptr += linesize;
  267. }
  268. break;
  269. case 4:
  270. for (i = 0; i < avctx->height; i++) {
  271. int j;
  272. for (j = 0; j < n; j++) {
  273. ptr[j*2+0] = (buf[j] >> 4) & 0xF;
  274. ptr[j*2+1] = buf[j] & 0xF;
  275. }
  276. buf += n;
  277. ptr += linesize;
  278. }
  279. break;
  280. case 16:
  281. for (i = 0; i < avctx->height; i++) {
  282. const uint16_t *src = (const uint16_t *) buf;
  283. uint16_t *dst = (uint16_t *) ptr;
  284. for (j = 0; j < avctx->width; j++)
  285. *dst++ = av_le2ne16(*src++);
  286. buf += n;
  287. ptr += linesize;
  288. }
  289. break;
  290. case 32:
  291. for (i = 0; i < avctx->height; i++) {
  292. const uint8_t *src = buf;
  293. uint8_t *dst = ptr;
  294. for (j = 0; j < avctx->width; j++) {
  295. dst[0] = src[rgb[2]];
  296. dst[1] = src[rgb[1]];
  297. dst[2] = src[rgb[0]];
  298. dst += 3;
  299. src += 4;
  300. }
  301. buf += n;
  302. ptr += linesize;
  303. }
  304. break;
  305. default:
  306. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  307. return AVERROR_INVALIDDATA;
  308. }
  309. }
  310. *got_frame = 1;
  311. return buf_size;
  312. }
  313. AVCodec ff_bmp_decoder = {
  314. .name = "bmp",
  315. .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. .id = AV_CODEC_ID_BMP,
  318. .decode = bmp_decode_frame,
  319. .capabilities = AV_CODEC_CAP_DR1,
  320. };