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.

105 lines
3.1KB

  1. /*
  2. * BMP parser
  3. * Copyright (c) 2012 Paul B Mahol
  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. /**
  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. restart:
  43. if (bpc->pc.frame_start_found <= 2+4+4) {
  44. for (; i < buf_size; i++) {
  45. state = (state << 8) | buf[i];
  46. if (bpc->pc.frame_start_found == 0) {
  47. if ((state >> 48) == (('B' << 8) | 'M')) {
  48. bpc->fsize = av_bswap32(state >> 16);
  49. bpc->pc.frame_start_found = 1;
  50. }
  51. } else if (bpc->pc.frame_start_found == 2+4+4) {
  52. // unsigned hsize = av_bswap32(state>>32);
  53. unsigned ihsize = av_bswap32(state);
  54. if (ihsize < 12 || ihsize > 200) {
  55. bpc->pc.frame_start_found = 0;
  56. continue;
  57. }
  58. bpc->pc.frame_start_found++;
  59. bpc->remaining_size = bpc->fsize + i - 17;
  60. if (bpc->pc.index + i > 17) {
  61. next = i - 17;
  62. } else
  63. goto restart;
  64. } else if (bpc->pc.frame_start_found)
  65. bpc->pc.frame_start_found++;
  66. }
  67. bpc->pc.state64 = state;
  68. } else {
  69. if (bpc->remaining_size) {
  70. i = FFMIN(bpc->remaining_size, buf_size);
  71. bpc->remaining_size -= i;
  72. if (bpc->remaining_size)
  73. goto flush;
  74. bpc->pc.frame_start_found = 0;
  75. goto restart;
  76. }
  77. }
  78. flush:
  79. if (ff_combine_frame(&bpc->pc, next, &buf, &buf_size) < 0)
  80. return buf_size;
  81. bpc->pc.frame_start_found = 0;
  82. *poutbuf = buf;
  83. *poutbuf_size = buf_size;
  84. return next;
  85. }
  86. AVCodecParser ff_bmp_parser = {
  87. .codec_ids = { AV_CODEC_ID_BMP },
  88. .priv_data_size = sizeof(BMPParseContext),
  89. .parser_parse = bmp_parse,
  90. .parser_close = ff_parse_close,
  91. };