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.

330 lines
9.1KB

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