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.

121 lines
3.5KB

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