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.

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