bmp.c 9.4 KB

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