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.

340 lines
9.4KB

  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. 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(depth == 4 || depth == 8)
  190. memset(p->data[1], 0, 1024);
  191. if(height > 0){
  192. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  193. linesize = -p->linesize[0];
  194. } else {
  195. ptr = p->data[0];
  196. linesize = p->linesize[0];
  197. }
  198. if(avctx->pix_fmt == PIX_FMT_PAL8){
  199. buf = buf0 + 14 + ihsize; //palette location
  200. if((hsize-ihsize-14)>>depth < 4){ // OS/2 bitmap, 3 bytes per palette entry
  201. for(i = 0; i < (1 << depth); i++)
  202. ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
  203. }else{
  204. for(i = 0; i < (1 << depth); i++)
  205. ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
  206. }
  207. buf = buf0 + hsize;
  208. }
  209. if(comp == BMP_RLE4 || comp == BMP_RLE8){
  210. ff_msrle_decode(avctx, p, depth, buf, dsize);
  211. }else{
  212. switch(depth){
  213. case 1:
  214. for(i = 0; i < avctx->height; i++){
  215. memcpy(ptr, buf, n);
  216. buf += n;
  217. ptr += linesize;
  218. }
  219. break;
  220. case 4:
  221. for(i = 0; i < avctx->height; i++){
  222. int j;
  223. for(j = 0; j < n; j++){
  224. ptr[j*2+0] = (buf[j] >> 4) & 0xF;
  225. ptr[j*2+1] = buf[j] & 0xF;
  226. }
  227. buf += n;
  228. ptr += linesize;
  229. }
  230. break;
  231. case 8:
  232. for(i = 0; i < avctx->height; i++){
  233. memcpy(ptr, buf, avctx->width);
  234. buf += n;
  235. ptr += linesize;
  236. }
  237. break;
  238. case 24:
  239. for(i = 0; i < avctx->height; i++){
  240. memcpy(ptr, buf, avctx->width*(depth>>3));
  241. buf += n;
  242. ptr += linesize;
  243. }
  244. break;
  245. case 16:
  246. for(i = 0; i < avctx->height; i++){
  247. const uint16_t *src = (const uint16_t *) buf;
  248. uint16_t *dst = (uint16_t *) ptr;
  249. for(j = 0; j < avctx->width; j++)
  250. *dst++ = le2me_16(*src++);
  251. buf += n;
  252. ptr += linesize;
  253. }
  254. break;
  255. case 32:
  256. for(i = 0; i < avctx->height; i++){
  257. const uint8_t *src = buf;
  258. uint8_t *dst = ptr;
  259. for(j = 0; j < avctx->width; j++){
  260. dst[0] = src[rgb[2]];
  261. dst[1] = src[rgb[1]];
  262. dst[2] = src[rgb[0]];
  263. dst += 3;
  264. src += 4;
  265. }
  266. buf += n;
  267. ptr += linesize;
  268. }
  269. break;
  270. default:
  271. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  272. return -1;
  273. }
  274. }
  275. *picture = s->picture;
  276. *data_size = sizeof(AVPicture);
  277. return buf_size;
  278. }
  279. static av_cold int bmp_decode_end(AVCodecContext *avctx)
  280. {
  281. BMPContext* c = avctx->priv_data;
  282. if (c->picture.data[0])
  283. avctx->release_buffer(avctx, &c->picture);
  284. return 0;
  285. }
  286. AVCodec bmp_decoder = {
  287. "bmp",
  288. CODEC_TYPE_VIDEO,
  289. CODEC_ID_BMP,
  290. sizeof(BMPContext),
  291. bmp_decode_init,
  292. NULL,
  293. bmp_decode_end,
  294. bmp_decode_frame,
  295. .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
  296. };