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.

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