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.

92 lines
2.6KB

  1. /*
  2. * BMP parser
  3. * Copyright (c) 2012 Paul B Mahol
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * BMP parser
  24. */
  25. #include "libavutil/bswap.h"
  26. #include "libavutil/common.h"
  27. #include "parser.h"
  28. typedef struct BMPParseContext {
  29. ParseContext pc;
  30. uint32_t fsize;
  31. uint32_t remaining_size;
  32. } BMPParseContext;
  33. static int bmp_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  34. const uint8_t **poutbuf, int *poutbuf_size,
  35. const uint8_t *buf, int buf_size)
  36. {
  37. BMPParseContext *bpc = s->priv_data;
  38. uint64_t state = bpc->pc.state64;
  39. int next = END_NOT_FOUND;
  40. int i = 0;
  41. *poutbuf_size = 0;
  42. if (buf_size == 0)
  43. return 0;
  44. if (!bpc->pc.frame_start_found) {
  45. for (; i < buf_size; i++) {
  46. state = (state << 8) | buf[i];
  47. if ((state >> 48) == (('B' << 8) | 'M')) {
  48. bpc->fsize = av_bswap32(state >> 16);
  49. bpc->pc.frame_start_found = 1;
  50. if (bpc->fsize > buf_size - i + 7)
  51. bpc->remaining_size = bpc->fsize - buf_size + i - 7;
  52. else
  53. next = bpc->fsize + i - 7;
  54. break;
  55. }
  56. }
  57. bpc->pc.state64 = state;
  58. } else {
  59. if (bpc->remaining_size) {
  60. i = FFMIN(bpc->remaining_size, buf_size);
  61. bpc->remaining_size -= i;
  62. if (bpc->remaining_size)
  63. goto flush;
  64. next = i;
  65. }
  66. }
  67. flush:
  68. if (ff_combine_frame(&bpc->pc, next, &buf, &buf_size) < 0)
  69. return buf_size;
  70. bpc->pc.frame_start_found = 0;
  71. *poutbuf = buf;
  72. *poutbuf_size = buf_size;
  73. return next;
  74. }
  75. AVCodecParser ff_bmp_parser = {
  76. .codec_ids = { AV_CODEC_ID_BMP },
  77. .priv_data_size = sizeof(BMPParseContext),
  78. .parser_parse = bmp_parse,
  79. .parser_close = ff_parse_close,
  80. };