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.

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