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.

113 lines
3.4KB

  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. if (bpc->fsize > 17)
  50. bpc->pc.frame_start_found = 1;
  51. }
  52. } else if (bpc->pc.frame_start_found == 2+4+4) {
  53. // unsigned hsize = av_bswap32(state>>32);
  54. unsigned ihsize = av_bswap32(state);
  55. if (ihsize < 12 || ihsize > 200) {
  56. bpc->pc.frame_start_found = 0;
  57. continue;
  58. }
  59. bpc->pc.frame_start_found++;
  60. bpc->remaining_size = bpc->fsize + i - 17;
  61. if (bpc->pc.index + i > 17) {
  62. next = i - 17;
  63. state = 0;
  64. break;
  65. } else {
  66. bpc->pc.state64 = 0;
  67. goto restart;
  68. }
  69. } else if (bpc->pc.frame_start_found)
  70. bpc->pc.frame_start_found++;
  71. }
  72. bpc->pc.state64 = state;
  73. } else {
  74. if (bpc->remaining_size) {
  75. i = FFMIN(bpc->remaining_size, buf_size);
  76. bpc->remaining_size -= i;
  77. if (bpc->remaining_size)
  78. goto flush;
  79. bpc->pc.frame_start_found = 0;
  80. goto restart;
  81. }
  82. }
  83. flush:
  84. if (ff_combine_frame(&bpc->pc, next, &buf, &buf_size) < 0)
  85. return buf_size;
  86. if (next != END_NOT_FOUND && next < 0)
  87. bpc->pc.frame_start_found = FFMAX(bpc->pc.frame_start_found - i - 1, 0);
  88. else
  89. bpc->pc.frame_start_found = 0;
  90. *poutbuf = buf;
  91. *poutbuf_size = buf_size;
  92. return next;
  93. }
  94. AVCodecParser ff_bmp_parser = {
  95. .codec_ids = { AV_CODEC_ID_BMP },
  96. .priv_data_size = sizeof(BMPParseContext),
  97. .parser_parse = bmp_parse,
  98. .parser_close = ff_parse_close,
  99. };