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.

125 lines
3.7KB

  1. /*
  2. * PNG parser
  3. * Copyright (c) 2009 Peter Holik
  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. * PNG parser
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/common.h"
  27. #include "parser.h"
  28. #define PNG_SIGNATURE UINT64_C(0x89504e470d0a1a0a)
  29. #define MNG_SIGNATURE UINT64_C(0x8a4d4e470d0a1a0a)
  30. typedef struct PNGParseContext {
  31. ParseContext pc;
  32. int chunk_pos; ///< position inside current chunk
  33. uint32_t chunk_length; ///< length of the current chunk
  34. int remaining_size; ///< remaining size of the current chunk
  35. } PNGParseContext;
  36. static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  37. const uint8_t **poutbuf, int *poutbuf_size,
  38. const uint8_t *buf, int buf_size)
  39. {
  40. PNGParseContext *ppc = s->priv_data;
  41. int next = END_NOT_FOUND;
  42. int i = 0;
  43. *poutbuf_size = 0;
  44. if (buf_size == 0)
  45. return 0;
  46. if (!ppc->pc.frame_start_found) {
  47. uint64_t state64 = ppc->pc.state64;
  48. for (; i < buf_size; i++) {
  49. state64 = (state64 << 8) | buf[i];
  50. if (state64 == PNG_SIGNATURE ||
  51. state64 == MNG_SIGNATURE) {
  52. i++;
  53. ppc->pc.frame_start_found = 1;
  54. break;
  55. }
  56. }
  57. ppc->pc.state64 = state64;
  58. } else if (ppc->remaining_size) {
  59. i = FFMIN(ppc->remaining_size, buf_size);
  60. ppc->remaining_size -= i;
  61. if (ppc->remaining_size)
  62. goto flush;
  63. if (ppc->chunk_pos == -1) {
  64. next = i;
  65. goto flush;
  66. }
  67. }
  68. for (; ppc->pc.frame_start_found && i < buf_size; i++) {
  69. ppc->pc.state = (ppc->pc.state << 8) | buf[i];
  70. if (ppc->chunk_pos == 3) {
  71. ppc->chunk_length = ppc->pc.state;
  72. if (ppc->chunk_length > 0x7fffffff) {
  73. ppc->chunk_pos = ppc->pc.frame_start_found = 0;
  74. goto flush;
  75. }
  76. ppc->chunk_length += 4;
  77. } else if (ppc->chunk_pos == 7) {
  78. if (ppc->chunk_length >= buf_size - i)
  79. ppc->remaining_size = ppc->chunk_length - buf_size + i + 1;
  80. if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) {
  81. if (ppc->remaining_size)
  82. ppc->chunk_pos = -1;
  83. else
  84. next = ppc->chunk_length + i + 1;
  85. break;
  86. } else {
  87. ppc->chunk_pos = 0;
  88. if (ppc->remaining_size)
  89. break;
  90. else
  91. i += ppc->chunk_length;
  92. continue;
  93. }
  94. }
  95. ppc->chunk_pos++;
  96. }
  97. flush:
  98. if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
  99. return buf_size;
  100. ppc->chunk_pos = ppc->pc.frame_start_found = 0;
  101. *poutbuf = buf;
  102. *poutbuf_size = buf_size;
  103. return next;
  104. }
  105. AVCodecParser ff_png_parser = {
  106. .codec_ids = { AV_CODEC_ID_PNG },
  107. .priv_data_size = sizeof(PNGParseContext),
  108. .parser_parse = png_parse,
  109. .parser_close = ff_parse_close,
  110. };