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.

148 lines
4.6KB

  1. /*
  2. * PNM image format
  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 "avcodec.h"
  22. #include "pnm.h"
  23. static inline int pnm_space(int c)
  24. {
  25. return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
  26. }
  27. static void pnm_get(PNMContext *sc, char *str, int buf_size)
  28. {
  29. char *s;
  30. int c;
  31. /* skip spaces and comments */
  32. for(;;) {
  33. c = *sc->bytestream++;
  34. if (c == '#') {
  35. do {
  36. c = *sc->bytestream++;
  37. } while (c != '\n' && sc->bytestream < sc->bytestream_end);
  38. } else if (!pnm_space(c)) {
  39. break;
  40. }
  41. }
  42. s = str;
  43. while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
  44. if ((s - str) < buf_size - 1)
  45. *s++ = c;
  46. c = *sc->bytestream++;
  47. }
  48. *s = '\0';
  49. }
  50. int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
  51. char buf1[32], tuple_type[32];
  52. int h, w, depth, maxval;
  53. pnm_get(s, buf1, sizeof(buf1));
  54. if (!strcmp(buf1, "P4")) {
  55. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  56. } else if (!strcmp(buf1, "P5")) {
  57. if (avctx->codec_id == CODEC_ID_PGMYUV)
  58. avctx->pix_fmt = PIX_FMT_YUV420P;
  59. else
  60. avctx->pix_fmt = PIX_FMT_GRAY8;
  61. } else if (!strcmp(buf1, "P6")) {
  62. avctx->pix_fmt = PIX_FMT_RGB24;
  63. } else if (!strcmp(buf1, "P7")) {
  64. w = -1;
  65. h = -1;
  66. maxval = -1;
  67. depth = -1;
  68. tuple_type[0] = '\0';
  69. for(;;) {
  70. pnm_get(s, buf1, sizeof(buf1));
  71. if (!strcmp(buf1, "WIDTH")) {
  72. pnm_get(s, buf1, sizeof(buf1));
  73. w = strtol(buf1, NULL, 10);
  74. } else if (!strcmp(buf1, "HEIGHT")) {
  75. pnm_get(s, buf1, sizeof(buf1));
  76. h = strtol(buf1, NULL, 10);
  77. } else if (!strcmp(buf1, "DEPTH")) {
  78. pnm_get(s, buf1, sizeof(buf1));
  79. depth = strtol(buf1, NULL, 10);
  80. } else if (!strcmp(buf1, "MAXVAL")) {
  81. pnm_get(s, buf1, sizeof(buf1));
  82. maxval = strtol(buf1, NULL, 10);
  83. } else if (!strcmp(buf1, "TUPLETYPE")) {
  84. pnm_get(s, tuple_type, sizeof(tuple_type));
  85. } else if (!strcmp(buf1, "ENDHDR")) {
  86. break;
  87. } else {
  88. return -1;
  89. }
  90. }
  91. /* check that all tags are present */
  92. if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
  93. return -1;
  94. avctx->width = w;
  95. avctx->height = h;
  96. if (depth == 1) {
  97. if (maxval == 1)
  98. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  99. else
  100. avctx->pix_fmt = PIX_FMT_GRAY8;
  101. } else if (depth == 3) {
  102. avctx->pix_fmt = PIX_FMT_RGB24;
  103. } else if (depth == 4) {
  104. avctx->pix_fmt = PIX_FMT_RGB32;
  105. } else {
  106. return -1;
  107. }
  108. return 0;
  109. } else {
  110. return -1;
  111. }
  112. pnm_get(s, buf1, sizeof(buf1));
  113. avctx->width = atoi(buf1);
  114. if (avctx->width <= 0)
  115. return -1;
  116. pnm_get(s, buf1, sizeof(buf1));
  117. avctx->height = atoi(buf1);
  118. if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  119. return -1;
  120. if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
  121. pnm_get(s, buf1, sizeof(buf1));
  122. s->maxval = atoi(buf1);
  123. if(s->maxval >= 256 && avctx->pix_fmt == PIX_FMT_GRAY8) {
  124. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  125. if (s->maxval != 65535)
  126. avctx->pix_fmt = PIX_FMT_GRAY16;
  127. }
  128. }
  129. /* more check if YUV420 */
  130. if (avctx->pix_fmt == PIX_FMT_YUV420P) {
  131. if ((avctx->width & 1) != 0)
  132. return -1;
  133. h = (avctx->height * 2);
  134. if ((h % 3) != 0)
  135. return -1;
  136. h /= 3;
  137. avctx->height = h;
  138. }
  139. return 0;
  140. }