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.

252 lines
6.3KB

  1. /*
  2. * BMP image format
  3. * Copyright (c) 2005 Mans Rullgard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avcodec.h"
  20. #include "bitstream.h"
  21. #include "bswap.h"
  22. typedef struct BMPContext {
  23. AVFrame picture;
  24. } BMPContext;
  25. #define BMP_RGB 0
  26. #define BMP_RLE8 1
  27. #define BMP_RLE4 2
  28. #define BMP_BITFIELDS 3
  29. #define read16(bits) bswap_16(get_bits(bits, 16))
  30. #define read32(bits) bswap_32(get_bits_long(bits, 32))
  31. static int bmp_decode_init(AVCodecContext *avctx){
  32. BMPContext *s = avctx->priv_data;
  33. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  34. avctx->coded_frame = (AVFrame*)&s->picture;
  35. return 0;
  36. }
  37. static int bmp_decode_frame(AVCodecContext *avctx,
  38. void *data, int *data_size,
  39. uint8_t *buf, int buf_size)
  40. {
  41. BMPContext *s = avctx->priv_data;
  42. AVFrame *picture = data;
  43. AVFrame *p = &s->picture;
  44. GetBitContext bits;
  45. unsigned int fsize, hsize;
  46. int width, height;
  47. unsigned int depth;
  48. unsigned int comp;
  49. unsigned int ihsize;
  50. int i, j, n, linesize;
  51. uint32_t rgb[3];
  52. uint8_t *ptr;
  53. int dsize;
  54. if(buf_size < 14){
  55. av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
  56. return -1;
  57. }
  58. init_get_bits(&bits, buf, buf_size);
  59. if(get_bits(&bits, 16) != 0x424d){ /* 'BM' */
  60. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  61. return -1;
  62. }
  63. fsize = read32(&bits);
  64. if(buf_size < fsize){
  65. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  66. buf_size, fsize);
  67. return -1;
  68. }
  69. skip_bits(&bits, 16); /* reserved1 */
  70. skip_bits(&bits, 16); /* reserved2 */
  71. hsize = read32(&bits); /* header size */
  72. if(fsize <= hsize){
  73. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  74. fsize, hsize);
  75. return -1;
  76. }
  77. ihsize = read32(&bits); /* more header size */
  78. if(ihsize + 14 > hsize){
  79. av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
  80. return -1;
  81. }
  82. width = read32(&bits);
  83. height = read32(&bits);
  84. if(read16(&bits) != 1){ /* planes */
  85. av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
  86. return -1;
  87. }
  88. depth = read16(&bits);
  89. if(ihsize > 16)
  90. comp = read32(&bits);
  91. else
  92. comp = BMP_RGB;
  93. if(comp != BMP_RGB && comp != BMP_BITFIELDS){
  94. av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
  95. return -1;
  96. }
  97. if(comp == BMP_BITFIELDS){
  98. skip_bits(&bits, 20 * 8);
  99. rgb[0] = read32(&bits);
  100. rgb[1] = read32(&bits);
  101. rgb[2] = read32(&bits);
  102. }
  103. avctx->codec_id = CODEC_ID_BMP;
  104. avctx->width = width;
  105. avctx->height = height > 0? height: -height;
  106. avctx->pix_fmt = PIX_FMT_NONE;
  107. switch(depth){
  108. case 32:
  109. if(comp == BMP_BITFIELDS){
  110. rgb[0] = (rgb[0] >> 15) & 3;
  111. rgb[1] = (rgb[1] >> 15) & 3;
  112. rgb[2] = (rgb[2] >> 15) & 3;
  113. if(rgb[0] + rgb[1] + rgb[2] != 3 ||
  114. rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
  115. break;
  116. }
  117. } else {
  118. rgb[0] = 2;
  119. rgb[1] = 1;
  120. rgb[2] = 0;
  121. }
  122. avctx->pix_fmt = PIX_FMT_BGR24;
  123. break;
  124. case 24:
  125. avctx->pix_fmt = PIX_FMT_BGR24;
  126. break;
  127. case 16:
  128. if(comp == BMP_RGB)
  129. avctx->pix_fmt = PIX_FMT_RGB555;
  130. break;
  131. default:
  132. av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
  133. return -1;
  134. }
  135. if(avctx->pix_fmt == PIX_FMT_NONE){
  136. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  137. return -1;
  138. }
  139. p->reference = 0;
  140. if(avctx->get_buffer(avctx, p) < 0){
  141. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  142. return -1;
  143. }
  144. p->pict_type = FF_I_TYPE;
  145. p->key_frame = 1;
  146. buf += hsize;
  147. dsize = buf_size - hsize;
  148. n = avctx->width * (depth / 8);
  149. if(n * avctx->height > dsize){
  150. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  151. dsize, n * avctx->height);
  152. return -1;
  153. }
  154. if(height > 0){
  155. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  156. linesize = -p->linesize[0];
  157. } else {
  158. ptr = p->data[0];
  159. linesize = p->linesize[0];
  160. }
  161. switch(depth){
  162. case 24:
  163. for(i = 0; i < avctx->height; i++){
  164. memcpy(ptr, buf, n);
  165. buf += n;
  166. ptr += linesize;
  167. }
  168. break;
  169. case 16:
  170. for(i = 0; i < avctx->height; i++){
  171. uint16_t *src = (uint16_t *) buf;
  172. uint16_t *dst = (uint16_t *) ptr;
  173. for(j = 0; j < avctx->width; j++)
  174. *dst++ = le2me_16(*src++);
  175. buf += n;
  176. ptr += linesize;
  177. }
  178. break;
  179. case 32:
  180. for(i = 0; i < avctx->height; i++){
  181. uint8_t *src = buf;
  182. uint8_t *dst = ptr;
  183. for(j = 0; j < avctx->width; j++){
  184. dst[0] = src[rgb[2]];
  185. dst[1] = src[rgb[1]];
  186. dst[2] = src[rgb[0]];
  187. dst += 3;
  188. src += 4;
  189. }
  190. buf += n;
  191. ptr += linesize;
  192. }
  193. break;
  194. default:
  195. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  196. return -1;
  197. }
  198. *picture = s->picture;
  199. *data_size = sizeof(AVPicture);
  200. return buf_size;
  201. }
  202. AVCodec bmp_decoder = {
  203. "bmp",
  204. CODEC_TYPE_VIDEO,
  205. CODEC_ID_BMP,
  206. sizeof(BMPContext),
  207. bmp_decode_init,
  208. NULL,
  209. NULL,
  210. bmp_decode_frame
  211. };