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.

263 lines
6.8KB

  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 av_cold 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. const 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. const 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. if (ihsize == 40) {
  76. width = bytestream_get_le32(&buf);
  77. height = bytestream_get_le32(&buf);
  78. } else if (ihsize == 12) {
  79. width = bytestream_get_le16(&buf);
  80. height = bytestream_get_le16(&buf);
  81. } else {
  82. av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome");
  83. return -1;
  84. }
  85. if(bytestream_get_le16(&buf) != 1){ /* planes */
  86. av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
  87. return -1;
  88. }
  89. depth = bytestream_get_le16(&buf);
  90. if(ihsize == 40)
  91. comp = bytestream_get_le32(&buf);
  92. else
  93. comp = BMP_RGB;
  94. if(comp != BMP_RGB && comp != BMP_BITFIELDS){
  95. av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
  96. return -1;
  97. }
  98. if(comp == BMP_BITFIELDS){
  99. buf += 20;
  100. rgb[0] = bytestream_get_le32(&buf);
  101. rgb[1] = bytestream_get_le32(&buf);
  102. rgb[2] = bytestream_get_le32(&buf);
  103. }
  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. if(p->data[0])
  140. avctx->release_buffer(avctx, p);
  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 = buf0 + hsize;
  149. dsize = buf_size - hsize;
  150. /* Line size in file multiple of 4 */
  151. n = (avctx->width * (depth / 8) + 3) & ~3;
  152. if(n * avctx->height > dsize){
  153. av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
  154. dsize, n * avctx->height);
  155. return -1;
  156. }
  157. if(height > 0){
  158. ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
  159. linesize = -p->linesize[0];
  160. } else {
  161. ptr = p->data[0];
  162. linesize = p->linesize[0];
  163. }
  164. switch(depth){
  165. case 24:
  166. for(i = 0; i < avctx->height; i++){
  167. memcpy(ptr, buf, avctx->width*(depth>>3));
  168. buf += n;
  169. ptr += linesize;
  170. }
  171. break;
  172. case 16:
  173. for(i = 0; i < avctx->height; i++){
  174. const uint16_t *src = (const uint16_t *) buf;
  175. uint16_t *dst = (uint16_t *) ptr;
  176. for(j = 0; j < avctx->width; j++)
  177. *dst++ = le2me_16(*src++);
  178. buf += n;
  179. ptr += linesize;
  180. }
  181. break;
  182. case 32:
  183. for(i = 0; i < avctx->height; i++){
  184. const uint8_t *src = buf;
  185. uint8_t *dst = ptr;
  186. for(j = 0; j < avctx->width; j++){
  187. dst[0] = src[rgb[2]];
  188. dst[1] = src[rgb[1]];
  189. dst[2] = src[rgb[0]];
  190. dst += 3;
  191. src += 4;
  192. }
  193. buf += n;
  194. ptr += linesize;
  195. }
  196. break;
  197. default:
  198. av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
  199. return -1;
  200. }
  201. *picture = s->picture;
  202. *data_size = sizeof(AVPicture);
  203. return buf_size;
  204. }
  205. static av_cold int bmp_decode_end(AVCodecContext *avctx)
  206. {
  207. BMPContext* c = avctx->priv_data;
  208. if (c->picture.data[0])
  209. avctx->release_buffer(avctx, &c->picture);
  210. return 0;
  211. }
  212. AVCodec bmp_decoder = {
  213. "bmp",
  214. CODEC_TYPE_VIDEO,
  215. CODEC_ID_BMP,
  216. sizeof(BMPContext),
  217. bmp_decode_init,
  218. NULL,
  219. bmp_decode_end,
  220. bmp_decode_frame,
  221. .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
  222. };