pnm_parser.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * PNM image parser
  3. * Copyright (c) 2002, 2003 Fabrice Bellard
  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. #include "libavutil/avassert.h"
  22. #include "libavutil/imgutils.h"
  23. #include "parser.h" //for ParseContext
  24. #include "pnm.h"
  25. typedef struct PNMParseContext {
  26. ParseContext pc;
  27. int remaining_bytes;
  28. int ascii_scan;
  29. }PNMParseContext;
  30. static int pnm_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  31. const uint8_t **poutbuf, int *poutbuf_size,
  32. const uint8_t *buf, int buf_size)
  33. {
  34. PNMParseContext *pnmpc = s->priv_data;
  35. ParseContext *pc = &pnmpc->pc;
  36. PNMContext pnmctx;
  37. int next = END_NOT_FOUND;
  38. int skip = 0;
  39. if (pc->overread > 0) {
  40. memmove(pc->buffer + pc->index, pc->buffer + pc->overread_index, pc->overread);
  41. pc->index += pc->overread;
  42. pc->overread_index += pc->overread;
  43. pc->overread = 0;
  44. }
  45. if (pnmpc->remaining_bytes) {
  46. int inc = FFMIN(pnmpc->remaining_bytes, buf_size);
  47. skip += inc;
  48. pnmpc->remaining_bytes -= inc;
  49. if (!pnmpc->remaining_bytes)
  50. next = skip;
  51. goto end;
  52. }
  53. retry:
  54. if (pc->index) {
  55. pnmctx.bytestream_start =
  56. pnmctx.bytestream = pc->buffer;
  57. pnmctx.bytestream_end = pc->buffer + pc->index;
  58. } else {
  59. pnmctx.bytestream_start =
  60. pnmctx.bytestream = (uint8_t *) buf + skip; /* casts avoid warnings */
  61. pnmctx.bytestream_end = (uint8_t *) buf + buf_size - skip;
  62. }
  63. if (ff_pnm_decode_header(avctx, &pnmctx) < 0) {
  64. if (pnmctx.bytestream < pnmctx.bytestream_end) {
  65. if (pc->index) {
  66. pc->index = 0;
  67. pnmpc->ascii_scan = 0;
  68. } else {
  69. unsigned step = FFMAX(1, pnmctx.bytestream - pnmctx.bytestream_start);
  70. skip += step;
  71. }
  72. goto retry;
  73. }
  74. } else if (pnmctx.type < 4) {
  75. uint8_t *bs = pnmctx.bytestream;
  76. const uint8_t *end = pnmctx.bytestream_end;
  77. uint8_t *sync = bs;
  78. if (pc->index) {
  79. av_assert0(pnmpc->ascii_scan <= end - bs);
  80. bs += pnmpc->ascii_scan;
  81. }
  82. while (bs < end) {
  83. int c;
  84. sync = bs;
  85. c = *bs++;
  86. if (c == '#') {
  87. uint8_t *match = memchr(bs, '\n', end-bs);
  88. if (match)
  89. bs = match + 1;
  90. else
  91. break;
  92. } else if (c == 'P') {
  93. next = bs - pnmctx.bytestream_start + skip - 1;
  94. pnmpc->ascii_scan = 0;
  95. break;
  96. }
  97. }
  98. if (next == END_NOT_FOUND)
  99. pnmpc->ascii_scan = sync - pnmctx.bytestream + skip;
  100. } else {
  101. int ret = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
  102. next = pnmctx.bytestream - pnmctx.bytestream_start + skip;
  103. if (ret > 0 && pnmctx.half)
  104. ret >>= 1;
  105. if (ret >= 0 && next + (uint64_t)ret <= INT_MAX)
  106. next += ret;
  107. }
  108. if (next != END_NOT_FOUND && pnmctx.bytestream_start != buf + skip)
  109. next -= pc->index;
  110. if (next > buf_size) {
  111. pnmpc->remaining_bytes = next - buf_size;
  112. next = END_NOT_FOUND;
  113. }
  114. end:
  115. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  116. *poutbuf = NULL;
  117. *poutbuf_size = 0;
  118. return buf_size;
  119. }
  120. *poutbuf = buf;
  121. *poutbuf_size = buf_size;
  122. return next;
  123. }
  124. const AVCodecParser ff_pnm_parser = {
  125. .codec_ids = { AV_CODEC_ID_PGM, AV_CODEC_ID_PGMYUV, AV_CODEC_ID_PPM,
  126. AV_CODEC_ID_PBM, AV_CODEC_ID_PAM, AV_CODEC_ID_PFM,
  127. AV_CODEC_ID_PHM },
  128. .priv_data_size = sizeof(PNMParseContext),
  129. .parser_parse = pnm_parse,
  130. .parser_close = ff_parse_close,
  131. };