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.

331 lines
9.2KB

  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. 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. if(comp == BMP_BITFIELDS)
  143. avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
  144. break;
  145. case 8:
  146. if(hsize - ihsize - 14 > 0)
  147. avctx->pix_fmt = PIX_FMT_PAL8;
  148. else
  149. avctx->pix_fmt = PIX_FMT_GRAY8;
  150. break;
  151. case 4:
  152. if(hsize - ihsize - 14 > 0){
  153. avctx->pix_fmt = PIX_FMT_PAL8;
  154. }else{
  155. av_log(avctx, AV_LOG_ERROR, "Unknown palette for 16-colour BMP\n");
  156. return -1;
  157. }
  158. break;
  159. case 1:
  160. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  161. break;
  162. default:
  163. av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
  164. return -1;
  165. }
  166. if(avctx->pix_fmt == PIX_FMT_NONE){
  167. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  168. return -1;
  169. }
  170. if(p->data[0])
  171. avctx->release_buffer(avctx, p);
  172. p->reference = 0;
  173. if(avctx->get_buffer(avctx, p) < 0){
  174. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  175. return -1;
  176. }
  177. p->pict_type = FF_I_TYPE;
  178. p->key_frame = 1;
  179. buf = buf0 + hsize;
  180. dsize = buf_size - hsize;
  181. /* Line size in file multiple of 4 */
  182. n = ((avctx->width * depth) / 8 + 3) & ~3;
  183. if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
  184. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  185. dsize, n * avctx->height);
  186. return -1;
  187. }
  188. // RLE may skip decoding some picture areas, so blank picture before decoding
  189. if(comp == BMP_RLE4 || comp == BMP_RLE8)
  190. memset(p->data[0], 0, avctx->height * p->linesize[0]);
  191. if(depth == 4 || depth == 8)
  192. memset(p->data[1], 0, 1024);
  193. if(height > 0){
  194. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  195. linesize = -p->linesize[0];
  196. } else {
  197. ptr = p->data[0];
  198. linesize = p->linesize[0];
  199. }
  200. if(avctx->pix_fmt == PIX_FMT_PAL8){
  201. buf = buf0 + 14 + ihsize; //palette location
  202. if((hsize-ihsize-14)>>depth < 4){ // OS/2 bitmap, 3 bytes per palette entry
  203. for(i = 0; i < (1 << depth); i++)
  204. ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
  205. }else{
  206. for(i = 0; i < (1 << depth); i++)
  207. ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
  208. }
  209. buf = buf0 + hsize;
  210. }
  211. if(comp == BMP_RLE4 || comp == BMP_RLE8){
  212. ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
  213. }else{
  214. switch(depth){
  215. case 1:
  216. case 8:
  217. case 24:
  218. for(i = 0; i < avctx->height; i++){
  219. memcpy(ptr, buf, n);
  220. buf += n;
  221. ptr += linesize;
  222. }
  223. break;
  224. case 4:
  225. for(i = 0; i < avctx->height; i++){
  226. int j;
  227. for(j = 0; j < n; j++){
  228. ptr[j*2+0] = (buf[j] >> 4) & 0xF;
  229. ptr[j*2+1] = buf[j] & 0xF;
  230. }
  231. buf += n;
  232. ptr += linesize;
  233. }
  234. break;
  235. case 16:
  236. for(i = 0; i < avctx->height; i++){
  237. const uint16_t *src = (const uint16_t *) buf;
  238. uint16_t *dst = (uint16_t *) ptr;
  239. for(j = 0; j < avctx->width; j++)
  240. *dst++ = le2me_16(*src++);
  241. buf += n;
  242. ptr += linesize;
  243. }
  244. break;
  245. case 32:
  246. for(i = 0; i < avctx->height; i++){
  247. const uint8_t *src = buf;
  248. uint8_t *dst = ptr;
  249. for(j = 0; j < avctx->width; j++){
  250. dst[0] = src[rgb[2]];
  251. dst[1] = src[rgb[1]];
  252. dst[2] = src[rgb[0]];
  253. dst += 3;
  254. src += 4;
  255. }
  256. buf += n;
  257. ptr += linesize;
  258. }
  259. break;
  260. default:
  261. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  262. return -1;
  263. }
  264. }
  265. *picture = s->picture;
  266. *data_size = sizeof(AVPicture);
  267. return buf_size;
  268. }
  269. static av_cold int bmp_decode_end(AVCodecContext *avctx)
  270. {
  271. BMPContext* c = avctx->priv_data;
  272. if (c->picture.data[0])
  273. avctx->release_buffer(avctx, &c->picture);
  274. return 0;
  275. }
  276. AVCodec bmp_decoder = {
  277. "bmp",
  278. CODEC_TYPE_VIDEO,
  279. CODEC_ID_BMP,
  280. sizeof(BMPContext),
  281. bmp_decode_init,
  282. NULL,
  283. bmp_decode_end,
  284. bmp_decode_frame,
  285. CODEC_CAP_DR1,
  286. .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
  287. };