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.

376 lines
12KB

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