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.

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