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.

118 lines
3.6KB

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