bmp.c 6.4 KB

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