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. * WebP parser
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * WebP parser
  23. */
  24. #include "libavutil/bswap.h"
  25. #include "libavutil/common.h"
  26. #include "parser.h"
  27. typedef struct WebPParseContext {
  28. ParseContext pc;
  29. uint32_t fsize;
  30. uint32_t remaining_size;
  31. } WebPParseContext;
  32. static int webp_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  33. const uint8_t **poutbuf, int *poutbuf_size,
  34. const uint8_t *buf, int buf_size)
  35. {
  36. WebPParseContext *ctx = s->priv_data;
  37. uint64_t state = ctx->pc.state64;
  38. int next = END_NOT_FOUND;
  39. int i = 0;
  40. *poutbuf = NULL;
  41. *poutbuf_size = 0;
  42. restart:
  43. if (ctx->pc.frame_start_found <= 8) {
  44. for (; i < buf_size; i++) {
  45. state = (state << 8) | buf[i];
  46. if (ctx->pc.frame_start_found == 0) {
  47. if ((state >> 32) == MKBETAG('R', 'I', 'F', 'F')) {
  48. ctx->fsize = av_bswap32(state);
  49. if (ctx->fsize > 15 && ctx->fsize <= UINT32_MAX - 10) {
  50. ctx->pc.frame_start_found = 1;
  51. ctx->fsize += 8;
  52. }
  53. }
  54. } else if (ctx->pc.frame_start_found == 8) {
  55. if ((state >> 32) != MKBETAG('W', 'E', 'B', 'P')) {
  56. ctx->pc.frame_start_found = 0;
  57. continue;
  58. }
  59. ctx->pc.frame_start_found++;
  60. ctx->remaining_size = ctx->fsize + i - 15;
  61. if (ctx->pc.index + i > 15) {
  62. next = i - 15;
  63. state = 0;
  64. break;
  65. } else {
  66. ctx->pc.state64 = 0;
  67. goto restart;
  68. }
  69. } else if (ctx->pc.frame_start_found)
  70. ctx->pc.frame_start_found++;
  71. }
  72. ctx->pc.state64 = state;
  73. } else {
  74. if (ctx->remaining_size) {
  75. i = FFMIN(ctx->remaining_size, buf_size);
  76. ctx->remaining_size -= i;
  77. if (ctx->remaining_size)
  78. goto flush;
  79. ctx->pc.frame_start_found = 0;
  80. goto restart;
  81. }
  82. }
  83. flush:
  84. if (ff_combine_frame(&ctx->pc, next, &buf, &buf_size) < 0)
  85. return buf_size;
  86. if (next != END_NOT_FOUND && next < 0)
  87. ctx->pc.frame_start_found = FFMAX(ctx->pc.frame_start_found - i - 1, 0);
  88. else
  89. ctx->pc.frame_start_found = 0;
  90. *poutbuf = buf;
  91. *poutbuf_size = buf_size;
  92. return next;
  93. }
  94. AVCodecParser ff_webp_parser = {
  95. .codec_ids = { AV_CODEC_ID_WEBP },
  96. .priv_data_size = sizeof(WebPParseContext),
  97. .parser_parse = webp_parse,
  98. .parser_close = ff_parse_close,
  99. };