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.

247 lines
6.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 "bitstream.h"
  23. #include "bswap.h"
  24. #include "bmp.h"
  25. #define read16(bits) bswap_16(get_bits(bits, 16))
  26. #define read32(bits) bswap_32(get_bits_long(bits, 32))
  27. static int bmp_decode_init(AVCodecContext *avctx){
  28. BMPContext *s = avctx->priv_data;
  29. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  30. avctx->coded_frame = (AVFrame*)&s->picture;
  31. return 0;
  32. }
  33. static int bmp_decode_frame(AVCodecContext *avctx,
  34. void *data, int *data_size,
  35. uint8_t *buf, int buf_size)
  36. {
  37. BMPContext *s = avctx->priv_data;
  38. AVFrame *picture = data;
  39. AVFrame *p = &s->picture;
  40. GetBitContext bits;
  41. unsigned int fsize, hsize;
  42. int width, height;
  43. unsigned int depth;
  44. BiCompression comp;
  45. unsigned int ihsize;
  46. int i, j, n, linesize;
  47. uint32_t rgb[3];
  48. uint8_t *ptr;
  49. int dsize;
  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. init_get_bits(&bits, buf, buf_size);
  55. if(get_bits(&bits, 16) != 0x424d){ /* 'BM' */
  56. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  57. return -1;
  58. }
  59. fsize = read32(&bits);
  60. if(buf_size < fsize){
  61. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  62. buf_size, fsize);
  63. return -1;
  64. }
  65. skip_bits(&bits, 16); /* reserved1 */
  66. skip_bits(&bits, 16); /* reserved2 */
  67. hsize = read32(&bits); /* header size */
  68. if(fsize <= hsize){
  69. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  70. fsize, hsize);
  71. return -1;
  72. }
  73. ihsize = read32(&bits); /* more header size */
  74. if(ihsize + 14 > hsize){
  75. av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
  76. return -1;
  77. }
  78. width = read32(&bits);
  79. height = read32(&bits);
  80. if(read16(&bits) != 1){ /* planes */
  81. av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
  82. return -1;
  83. }
  84. depth = read16(&bits);
  85. if(ihsize > 16)
  86. comp = read32(&bits);
  87. else
  88. comp = BMP_RGB;
  89. if(comp != BMP_RGB && comp != BMP_BITFIELDS){
  90. av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
  91. return -1;
  92. }
  93. if(comp == BMP_BITFIELDS){
  94. skip_bits(&bits, 20 * 8);
  95. rgb[0] = read32(&bits);
  96. rgb[1] = read32(&bits);
  97. rgb[2] = read32(&bits);
  98. }
  99. avctx->codec_id = CODEC_ID_BMP;
  100. avctx->width = width;
  101. avctx->height = height > 0? height: -height;
  102. avctx->pix_fmt = PIX_FMT_NONE;
  103. switch(depth){
  104. case 32:
  105. if(comp == BMP_BITFIELDS){
  106. rgb[0] = (rgb[0] >> 15) & 3;
  107. rgb[1] = (rgb[1] >> 15) & 3;
  108. rgb[2] = (rgb[2] >> 15) & 3;
  109. if(rgb[0] + rgb[1] + rgb[2] != 3 ||
  110. rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
  111. break;
  112. }
  113. } else {
  114. rgb[0] = 2;
  115. rgb[1] = 1;
  116. rgb[2] = 0;
  117. }
  118. avctx->pix_fmt = PIX_FMT_BGR24;
  119. break;
  120. case 24:
  121. avctx->pix_fmt = PIX_FMT_BGR24;
  122. break;
  123. case 16:
  124. if(comp == BMP_RGB)
  125. avctx->pix_fmt = PIX_FMT_RGB555;
  126. break;
  127. default:
  128. av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
  129. return -1;
  130. }
  131. if(avctx->pix_fmt == PIX_FMT_NONE){
  132. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  133. return -1;
  134. }
  135. p->reference = 0;
  136. if(avctx->get_buffer(avctx, p) < 0){
  137. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  138. return -1;
  139. }
  140. p->pict_type = FF_I_TYPE;
  141. p->key_frame = 1;
  142. buf += hsize;
  143. dsize = buf_size - hsize;
  144. /* Line size in file multiple of 4 */
  145. n = (avctx->width * (depth / 8) + 3) & ~3;
  146. if(n * avctx->height > dsize){
  147. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  148. dsize, n * avctx->height);
  149. return -1;
  150. }
  151. if(height > 0){
  152. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  153. linesize = -p->linesize[0];
  154. } else {
  155. ptr = p->data[0];
  156. linesize = p->linesize[0];
  157. }
  158. switch(depth){
  159. case 24:
  160. for(i = 0; i < avctx->height; i++){
  161. memcpy(ptr, buf, n);
  162. buf += n;
  163. ptr += linesize;
  164. }
  165. break;
  166. case 16:
  167. for(i = 0; i < avctx->height; i++){
  168. uint16_t *src = (uint16_t *) buf;
  169. uint16_t *dst = (uint16_t *) ptr;
  170. for(j = 0; j < avctx->width; j++)
  171. *dst++ = le2me_16(*src++);
  172. buf += n;
  173. ptr += linesize;
  174. }
  175. break;
  176. case 32:
  177. for(i = 0; i < avctx->height; i++){
  178. uint8_t *src = buf;
  179. uint8_t *dst = ptr;
  180. for(j = 0; j < avctx->width; j++){
  181. dst[0] = src[rgb[2]];
  182. dst[1] = src[rgb[1]];
  183. dst[2] = src[rgb[0]];
  184. dst += 3;
  185. src += 4;
  186. }
  187. buf += n;
  188. ptr += linesize;
  189. }
  190. break;
  191. default:
  192. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  193. return -1;
  194. }
  195. *picture = s->picture;
  196. *data_size = sizeof(AVPicture);
  197. return buf_size;
  198. }
  199. AVCodec bmp_decoder = {
  200. "bmp",
  201. CODEC_TYPE_VIDEO,
  202. CODEC_ID_BMP,
  203. sizeof(BMPContext),
  204. bmp_decode_init,
  205. NULL,
  206. NULL,
  207. bmp_decode_frame
  208. };