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.

254 lines
6.3KB

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