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.

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