pnm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if (maxval < 256) {
  103. avctx->pix_fmt = PIX_FMT_RGB24;
  104. } else {
  105. av_log(avctx, AV_LOG_ERROR, "16-bit components are only supported for grayscale\n");
  106. avctx->pix_fmt = PIX_FMT_NONE;
  107. return -1;
  108. }
  109. } else if (depth == 4) {
  110. avctx->pix_fmt = PIX_FMT_RGB32;
  111. } else {
  112. return -1;
  113. }
  114. return 0;
  115. } else {
  116. return -1;
  117. }
  118. pnm_get(s, buf1, sizeof(buf1));
  119. avctx->width = atoi(buf1);
  120. if (avctx->width <= 0)
  121. return -1;
  122. pnm_get(s, buf1, sizeof(buf1));
  123. avctx->height = atoi(buf1);
  124. if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  125. return -1;
  126. if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
  127. pnm_get(s, buf1, sizeof(buf1));
  128. s->maxval = atoi(buf1);
  129. if (s->maxval >= 256) {
  130. if (avctx->pix_fmt == PIX_FMT_GRAY8) {
  131. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  132. if (s->maxval != 65535)
  133. avctx->pix_fmt = PIX_FMT_GRAY16;
  134. } if (avctx->pix_fmt == PIX_FMT_RGB24) {
  135. if (s->maxval > 255)
  136. avctx->pix_fmt = PIX_FMT_RGB48BE;
  137. } else {
  138. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  139. avctx->pix_fmt = PIX_FMT_NONE;
  140. return -1;
  141. }
  142. }
  143. }
  144. /* more check if YUV420 */
  145. if (avctx->pix_fmt == PIX_FMT_YUV420P) {
  146. if ((avctx->width & 1) != 0)
  147. return -1;
  148. h = (avctx->height * 2);
  149. if ((h % 3) != 0)
  150. return -1;
  151. h /= 3;
  152. avctx->height = h;
  153. }
  154. return 0;
  155. }