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.

341 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 "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] = {0};
  40. uint32_t alpha = 0;
  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 < %d), 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 %d\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, "declared file size is less than header size (%d < %d)\n",
  73. fsize, hsize);
  74. return AVERROR_INVALIDDATA;
  75. }
  76. switch (ihsize) {
  77. case 40: // windib
  78. case 56: // 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. alpha = 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. if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
  122. avctx->pix_fmt = alpha ? AV_PIX_FMT_ABGR : AV_PIX_FMT_0BGR;
  123. else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
  124. avctx->pix_fmt = alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_BGR0;
  125. else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
  126. avctx->pix_fmt = alpha ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
  127. else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
  128. avctx->pix_fmt = alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB0;
  129. else {
  130. av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
  131. return AVERROR(EINVAL);
  132. }
  133. } else {
  134. avctx->pix_fmt = AV_PIX_FMT_BGRA;
  135. }
  136. break;
  137. case 24:
  138. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  139. break;
  140. case 16:
  141. if (comp == BMP_RGB)
  142. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  143. else if (comp == BMP_BITFIELDS) {
  144. if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
  145. avctx->pix_fmt = AV_PIX_FMT_RGB565;
  146. else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
  147. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  148. else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
  149. avctx->pix_fmt = AV_PIX_FMT_RGB444;
  150. else {
  151. av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", 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 %d-colour BMP\n", 1<<depth);
  168. return AVERROR_INVALIDDATA;
  169. }
  170. break;
  171. default:
  172. av_log(avctx, AV_LOG_ERROR, "depth %d 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 + 31) / 8) & ~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, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
  213. } else if (t) {
  214. colors = t;
  215. }
  216. }
  217. buf = buf0 + 14 + ihsize; //palette location
  218. // OS/2 bitmap, 3 bytes per palette entry
  219. if ((hsize-ihsize-14) < (colors << 2)) {
  220. if ((hsize-ihsize-14) < colors * 3) {
  221. av_log(avctx, AV_LOG_ERROR, "palette doesnt fit in packet\n");
  222. return AVERROR_INVALIDDATA;
  223. }
  224. for (i = 0; i < colors; i++)
  225. ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
  226. } else {
  227. for (i = 0; i < colors; i++)
  228. ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
  229. }
  230. buf = buf0 + hsize;
  231. }
  232. if (comp == BMP_RLE4 || comp == BMP_RLE8) {
  233. if (height < 0) {
  234. p->data[0] += p->linesize[0] * (avctx->height - 1);
  235. p->linesize[0] = -p->linesize[0];
  236. }
  237. bytestream2_init(&gb, buf, dsize);
  238. ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
  239. if (height < 0) {
  240. p->data[0] += p->linesize[0] * (avctx->height - 1);
  241. p->linesize[0] = -p->linesize[0];
  242. }
  243. } else {
  244. switch (depth) {
  245. case 1:
  246. for (i = 0; i < avctx->height; i++) {
  247. int j;
  248. for (j = 0; j < n; j++) {
  249. ptr[j*8+0] = buf[j] >> 7;
  250. ptr[j*8+1] = (buf[j] >> 6) & 1;
  251. ptr[j*8+2] = (buf[j] >> 5) & 1;
  252. ptr[j*8+3] = (buf[j] >> 4) & 1;
  253. ptr[j*8+4] = (buf[j] >> 3) & 1;
  254. ptr[j*8+5] = (buf[j] >> 2) & 1;
  255. ptr[j*8+6] = (buf[j] >> 1) & 1;
  256. ptr[j*8+7] = buf[j] & 1;
  257. }
  258. buf += n;
  259. ptr += linesize;
  260. }
  261. break;
  262. case 8:
  263. case 24:
  264. case 32:
  265. for (i = 0; i < avctx->height; i++) {
  266. memcpy(ptr, buf, n);
  267. buf += n;
  268. ptr += linesize;
  269. }
  270. break;
  271. case 4:
  272. for (i = 0; i < avctx->height; i++) {
  273. int j;
  274. for (j = 0; j < n; j++) {
  275. ptr[j*2+0] = (buf[j] >> 4) & 0xF;
  276. ptr[j*2+1] = buf[j] & 0xF;
  277. }
  278. buf += n;
  279. ptr += linesize;
  280. }
  281. break;
  282. case 16:
  283. for (i = 0; i < avctx->height; i++) {
  284. const uint16_t *src = (const uint16_t *) buf;
  285. uint16_t *dst = (uint16_t *) ptr;
  286. for (j = 0; j < avctx->width; j++)
  287. *dst++ = av_le2ne16(*src++);
  288. buf += n;
  289. ptr += linesize;
  290. }
  291. break;
  292. default:
  293. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  294. return AVERROR_INVALIDDATA;
  295. }
  296. }
  297. *got_frame = 1;
  298. return buf_size;
  299. }
  300. AVCodec ff_bmp_decoder = {
  301. .name = "bmp",
  302. .type = AVMEDIA_TYPE_VIDEO,
  303. .id = AV_CODEC_ID_BMP,
  304. .decode = bmp_decode_frame,
  305. .capabilities = CODEC_CAP_DR1,
  306. .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
  307. };