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
11KB

  1. /*
  2. * BMP image format decoder
  3. * Copyright (c) 2005 Mans Rullgard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. BMPContext *s = avctx->priv_data;
  28. avcodec_get_frame_defaults(&s->picture);
  29. avctx->coded_frame = &s->picture;
  30. return 0;
  31. }
  32. static int bmp_decode_frame(AVCodecContext *avctx,
  33. void *data, int *got_frame,
  34. AVPacket *avpkt)
  35. {
  36. const uint8_t *buf = avpkt->data;
  37. int buf_size = avpkt->size;
  38. BMPContext *s = avctx->priv_data;
  39. AVFrame *picture = data;
  40. AVFrame *p = &s->picture;
  41. unsigned int fsize, hsize;
  42. int width, height;
  43. unsigned int depth;
  44. BiCompression comp;
  45. unsigned int ihsize;
  46. int i, j, n, linesize;
  47. uint32_t rgb[3];
  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 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)
  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. }
  118. avctx->width = width;
  119. avctx->height = height > 0? height: -height;
  120. avctx->pix_fmt = AV_PIX_FMT_NONE;
  121. switch(depth){
  122. case 32:
  123. if(comp == BMP_BITFIELDS){
  124. rgb[0] = (rgb[0] >> 15) & 3;
  125. rgb[1] = (rgb[1] >> 15) & 3;
  126. rgb[2] = (rgb[2] >> 15) & 3;
  127. if(rgb[0] + rgb[1] + rgb[2] != 3 ||
  128. rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
  129. break;
  130. }
  131. } else {
  132. rgb[0] = 2;
  133. rgb[1] = 1;
  134. rgb[2] = 0;
  135. }
  136. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  137. break;
  138. case 24:
  139. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  140. break;
  141. case 16:
  142. if(comp == BMP_RGB)
  143. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  144. else if (comp == BMP_BITFIELDS) {
  145. if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
  146. avctx->pix_fmt = AV_PIX_FMT_RGB565;
  147. else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
  148. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  149. else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
  150. avctx->pix_fmt = AV_PIX_FMT_RGB444;
  151. else {
  152. av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
  153. return AVERROR(EINVAL);
  154. }
  155. }
  156. break;
  157. case 8:
  158. if(hsize - ihsize - 14 > 0)
  159. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  160. else
  161. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  162. break;
  163. case 1:
  164. case 4:
  165. if(hsize - ihsize - 14 > 0){
  166. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  167. }else{
  168. av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
  169. return -1;
  170. }
  171. break;
  172. default:
  173. av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
  174. return -1;
  175. }
  176. if(avctx->pix_fmt == AV_PIX_FMT_NONE){
  177. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  178. return -1;
  179. }
  180. if(p->data[0])
  181. avctx->release_buffer(avctx, p);
  182. p->reference = 0;
  183. if(ff_get_buffer(avctx, p) < 0){
  184. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  185. return -1;
  186. }
  187. p->pict_type = AV_PICTURE_TYPE_I;
  188. p->key_frame = 1;
  189. buf = buf0 + hsize;
  190. dsize = buf_size - hsize;
  191. /* Line size in file multiple of 4 */
  192. n = ((avctx->width * depth) / 8 + 3) & ~3;
  193. if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
  194. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  195. dsize, n * avctx->height);
  196. return -1;
  197. }
  198. // RLE may skip decoding some picture areas, so blank picture before decoding
  199. if(comp == BMP_RLE4 || comp == BMP_RLE8)
  200. memset(p->data[0], 0, avctx->height * p->linesize[0]);
  201. if(height > 0){
  202. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  203. linesize = -p->linesize[0];
  204. } else {
  205. ptr = p->data[0];
  206. linesize = p->linesize[0];
  207. }
  208. if(avctx->pix_fmt == AV_PIX_FMT_PAL8){
  209. int colors = 1 << depth;
  210. memset(p->data[1], 0, 1024);
  211. if(ihsize >= 36){
  212. int t;
  213. buf = buf0 + 46;
  214. t = bytestream_get_le32(&buf);
  215. if(t < 0 || t > (1 << depth)){
  216. av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
  217. }else if(t){
  218. colors = t;
  219. }
  220. }
  221. buf = buf0 + 14 + ihsize; //palette location
  222. if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
  223. for(i = 0; i < colors; i++)
  224. ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
  225. }else{
  226. for(i = 0; i < colors; i++)
  227. ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
  228. }
  229. buf = buf0 + hsize;
  230. }
  231. if(comp == BMP_RLE4 || comp == BMP_RLE8){
  232. if(height < 0){
  233. p->data[0] += p->linesize[0] * (avctx->height - 1);
  234. p->linesize[0] = -p->linesize[0];
  235. }
  236. bytestream2_init(&gb, buf, dsize);
  237. ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
  238. if(height < 0){
  239. p->data[0] += p->linesize[0] * (avctx->height - 1);
  240. p->linesize[0] = -p->linesize[0];
  241. }
  242. }else{
  243. switch(depth){
  244. case 1:
  245. for (i = 0; i < avctx->height; i++) {
  246. int j;
  247. for (j = 0; j < n; j++) {
  248. ptr[j*8+0] = buf[j] >> 7;
  249. ptr[j*8+1] = (buf[j] >> 6) & 1;
  250. ptr[j*8+2] = (buf[j] >> 5) & 1;
  251. ptr[j*8+3] = (buf[j] >> 4) & 1;
  252. ptr[j*8+4] = (buf[j] >> 3) & 1;
  253. ptr[j*8+5] = (buf[j] >> 2) & 1;
  254. ptr[j*8+6] = (buf[j] >> 1) & 1;
  255. ptr[j*8+7] = buf[j] & 1;
  256. }
  257. buf += n;
  258. ptr += linesize;
  259. }
  260. break;
  261. case 8:
  262. case 24:
  263. for(i = 0; i < avctx->height; i++){
  264. memcpy(ptr, buf, n);
  265. buf += n;
  266. ptr += linesize;
  267. }
  268. break;
  269. case 4:
  270. for(i = 0; i < avctx->height; i++){
  271. int j;
  272. for(j = 0; j < n; j++){
  273. ptr[j*2+0] = (buf[j] >> 4) & 0xF;
  274. ptr[j*2+1] = buf[j] & 0xF;
  275. }
  276. buf += n;
  277. ptr += linesize;
  278. }
  279. break;
  280. case 16:
  281. for(i = 0; i < avctx->height; i++){
  282. const uint16_t *src = (const uint16_t *) buf;
  283. uint16_t *dst = (uint16_t *) ptr;
  284. for(j = 0; j < avctx->width; j++)
  285. *dst++ = av_le2ne16(*src++);
  286. buf += n;
  287. ptr += linesize;
  288. }
  289. break;
  290. case 32:
  291. for(i = 0; i < avctx->height; i++){
  292. const uint8_t *src = buf;
  293. uint8_t *dst = ptr;
  294. for(j = 0; j < avctx->width; j++){
  295. dst[0] = src[rgb[2]];
  296. dst[1] = src[rgb[1]];
  297. dst[2] = src[rgb[0]];
  298. dst += 3;
  299. src += 4;
  300. }
  301. buf += n;
  302. ptr += linesize;
  303. }
  304. break;
  305. default:
  306. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  307. return -1;
  308. }
  309. }
  310. *picture = s->picture;
  311. *got_frame = 1;
  312. return buf_size;
  313. }
  314. static av_cold int bmp_decode_end(AVCodecContext *avctx)
  315. {
  316. BMPContext* c = avctx->priv_data;
  317. if (c->picture.data[0])
  318. avctx->release_buffer(avctx, &c->picture);
  319. return 0;
  320. }
  321. AVCodec ff_bmp_decoder = {
  322. .name = "bmp",
  323. .type = AVMEDIA_TYPE_VIDEO,
  324. .id = AV_CODEC_ID_BMP,
  325. .priv_data_size = sizeof(BMPContext),
  326. .init = bmp_decode_init,
  327. .close = bmp_decode_end,
  328. .decode = bmp_decode_frame,
  329. .capabilities = CODEC_CAP_DR1,
  330. .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
  331. };