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.

187 lines
6.2KB

  1. /*
  2. * PNM image format
  3. * Copyright (c) 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "pnm.h"
  26. static inline int pnm_space(int c)
  27. {
  28. return c == ' ' || c == '\n' || c == '\r' || c == '\t';
  29. }
  30. static void pnm_get(PNMContext *sc, char *str, int buf_size)
  31. {
  32. char *s;
  33. int c;
  34. /* skip spaces and comments */
  35. for (;;) {
  36. c = *sc->bytestream++;
  37. if (c == '#') {
  38. do {
  39. c = *sc->bytestream++;
  40. } while (c != '\n' && sc->bytestream < sc->bytestream_end);
  41. } else if (!pnm_space(c)) {
  42. break;
  43. }
  44. }
  45. s = str;
  46. while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
  47. if ((s - str) < buf_size - 1)
  48. *s++ = c;
  49. c = *sc->bytestream++;
  50. }
  51. *s = '\0';
  52. }
  53. int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
  54. {
  55. char buf1[32], tuple_type[32];
  56. int h, w, depth, maxval;
  57. pnm_get(s, buf1, sizeof(buf1));
  58. s->type= buf1[1]-'0';
  59. if(buf1[0] != 'P')
  60. return AVERROR_INVALIDDATA;
  61. if (s->type==1 || s->type==4) {
  62. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  63. } else if (s->type==2 || s->type==5) {
  64. if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
  65. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  66. else
  67. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  68. } else if (s->type==3 || s->type==6) {
  69. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  70. } else if (s->type==7) {
  71. w = -1;
  72. h = -1;
  73. maxval = -1;
  74. depth = -1;
  75. tuple_type[0] = '\0';
  76. for (;;) {
  77. pnm_get(s, buf1, sizeof(buf1));
  78. if (!strcmp(buf1, "WIDTH")) {
  79. pnm_get(s, buf1, sizeof(buf1));
  80. w = strtol(buf1, NULL, 10);
  81. } else if (!strcmp(buf1, "HEIGHT")) {
  82. pnm_get(s, buf1, sizeof(buf1));
  83. h = strtol(buf1, NULL, 10);
  84. } else if (!strcmp(buf1, "DEPTH")) {
  85. pnm_get(s, buf1, sizeof(buf1));
  86. depth = strtol(buf1, NULL, 10);
  87. } else if (!strcmp(buf1, "MAXVAL")) {
  88. pnm_get(s, buf1, sizeof(buf1));
  89. maxval = strtol(buf1, NULL, 10);
  90. } else if (!strcmp(buf1, "TUPLTYPE") ||
  91. /* libavcodec used to write invalid files */
  92. !strcmp(buf1, "TUPLETYPE")) {
  93. pnm_get(s, tuple_type, sizeof(tuple_type));
  94. } else if (!strcmp(buf1, "ENDHDR")) {
  95. break;
  96. } else {
  97. return AVERROR_INVALIDDATA;
  98. }
  99. }
  100. /* check that all tags are present */
  101. if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || av_image_check_size(w, h, 0, avctx))
  102. return AVERROR_INVALIDDATA;
  103. avctx->width = w;
  104. avctx->height = h;
  105. if (depth == 1) {
  106. if (maxval == 1)
  107. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  108. else
  109. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  110. } else if (depth == 3) {
  111. if (maxval < 256) {
  112. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  113. } else {
  114. av_log(avctx, AV_LOG_ERROR, "16-bit components are only supported for grayscale\n");
  115. avctx->pix_fmt = AV_PIX_FMT_NONE;
  116. return AVERROR_INVALIDDATA;
  117. }
  118. } else if (depth == 4) {
  119. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  120. } else {
  121. return AVERROR_INVALIDDATA;
  122. }
  123. return 0;
  124. } else {
  125. return AVERROR_INVALIDDATA;
  126. }
  127. pnm_get(s, buf1, sizeof(buf1));
  128. avctx->width = atoi(buf1);
  129. if (avctx->width <= 0)
  130. return AVERROR_INVALIDDATA;
  131. pnm_get(s, buf1, sizeof(buf1));
  132. avctx->height = atoi(buf1);
  133. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  134. return AVERROR_INVALIDDATA;
  135. if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE) {
  136. pnm_get(s, buf1, sizeof(buf1));
  137. s->maxval = atoi(buf1);
  138. if (s->maxval <= 0) {
  139. av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
  140. s->maxval = 255;
  141. }
  142. if (s->maxval >= 256) {
  143. if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  144. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  145. if (s->maxval != 65535)
  146. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  147. } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
  148. if (s->maxval > 255)
  149. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  150. } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
  151. if (s->maxval < 512)
  152. avctx->pix_fmt = AV_PIX_FMT_YUV420P9BE;
  153. else if (s->maxval < 1024)
  154. avctx->pix_fmt = AV_PIX_FMT_YUV420P10BE;
  155. else
  156. avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
  157. } else {
  158. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  159. avctx->pix_fmt = AV_PIX_FMT_NONE;
  160. return AVERROR_INVALIDDATA;
  161. }
  162. }
  163. }else
  164. s->maxval=1;
  165. /* more check if YUV420 */
  166. if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) {
  167. if ((avctx->width & 1) != 0)
  168. return AVERROR_INVALIDDATA;
  169. h = (avctx->height * 2);
  170. if ((h % 3) != 0)
  171. return AVERROR_INVALIDDATA;
  172. h /= 3;
  173. avctx->height = h;
  174. }
  175. return 0;
  176. }