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.

257 lines
6.4KB

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