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.

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