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.

236 lines
7.8KB

  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 <stdlib.h>
  22. #include <string.h>
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/avstring.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. #include "pnm.h"
  29. static inline int pnm_space(int c)
  30. {
  31. return c == ' ' || c == '\n' || c == '\r' || c == '\t';
  32. }
  33. static void pnm_get(PNMContext *sc, char *str, int buf_size)
  34. {
  35. char *s;
  36. int c;
  37. uint8_t *bs = sc->bytestream;
  38. const uint8_t *end = sc->bytestream_end;
  39. /* skip spaces and comments */
  40. while (bs < end) {
  41. c = *bs++;
  42. if (c == '#') {
  43. while (c != '\n' && bs < end) {
  44. c = *bs++;
  45. }
  46. } else if (!pnm_space(c)) {
  47. break;
  48. }
  49. }
  50. s = str;
  51. while (bs < end && !pnm_space(c) && (s - str) < buf_size - 1) {
  52. *s++ = c;
  53. c = *bs++;
  54. }
  55. *s = '\0';
  56. sc->bytestream = bs;
  57. }
  58. int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
  59. {
  60. char buf1[32], tuple_type[32];
  61. int h, w, depth, maxval;
  62. int ret;
  63. if (s->bytestream_end - s->bytestream < 3 ||
  64. s->bytestream[0] != 'P' ||
  65. (s->bytestream[1] < '1' ||
  66. s->bytestream[1] > '7' &&
  67. s->bytestream[1] != 'f' &&
  68. s->bytestream[1] != 'F')) {
  69. s->bytestream += s->bytestream_end > s->bytestream;
  70. s->bytestream += s->bytestream_end > s->bytestream;
  71. return AVERROR_INVALIDDATA;
  72. }
  73. pnm_get(s, buf1, sizeof(buf1));
  74. s->type= buf1[1]-'0';
  75. if (buf1[1] == 'F') {
  76. avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
  77. } else if (buf1[1] == 'f') {
  78. avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
  79. } else if (s->type==1 || s->type==4) {
  80. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  81. } else if (s->type==2 || s->type==5) {
  82. if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
  83. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  84. else
  85. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  86. } else if (s->type==3 || s->type==6) {
  87. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  88. } else if (s->type==7) {
  89. w = -1;
  90. h = -1;
  91. maxval = -1;
  92. depth = -1;
  93. tuple_type[0] = '\0';
  94. for (;;) {
  95. pnm_get(s, buf1, sizeof(buf1));
  96. if (!strcmp(buf1, "WIDTH")) {
  97. pnm_get(s, buf1, sizeof(buf1));
  98. w = strtol(buf1, NULL, 10);
  99. } else if (!strcmp(buf1, "HEIGHT")) {
  100. pnm_get(s, buf1, sizeof(buf1));
  101. h = strtol(buf1, NULL, 10);
  102. } else if (!strcmp(buf1, "DEPTH")) {
  103. pnm_get(s, buf1, sizeof(buf1));
  104. depth = strtol(buf1, NULL, 10);
  105. } else if (!strcmp(buf1, "MAXVAL")) {
  106. pnm_get(s, buf1, sizeof(buf1));
  107. maxval = strtol(buf1, NULL, 10);
  108. } else if (!strcmp(buf1, "TUPLTYPE") ||
  109. /* libavcodec used to write invalid files */
  110. !strcmp(buf1, "TUPLETYPE")) {
  111. pnm_get(s, tuple_type, sizeof(tuple_type));
  112. } else if (!strcmp(buf1, "ENDHDR")) {
  113. break;
  114. } else {
  115. return AVERROR_INVALIDDATA;
  116. }
  117. }
  118. if (!pnm_space(s->bytestream[-1]))
  119. return AVERROR_INVALIDDATA;
  120. /* check that all tags are present */
  121. if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
  122. av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
  123. return AVERROR_INVALIDDATA;
  124. ret = ff_set_dimensions(avctx, w, h);
  125. if (ret < 0)
  126. return ret;
  127. s->maxval = maxval;
  128. if (depth == 1) {
  129. if (maxval == 1) {
  130. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  131. } else if (maxval < 256) {
  132. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  133. } else {
  134. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  135. }
  136. } else if (depth == 2) {
  137. if (maxval < 256) {
  138. avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
  139. } else {
  140. avctx->pix_fmt = AV_PIX_FMT_YA16;
  141. }
  142. } else if (depth == 3) {
  143. if (maxval < 256) {
  144. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  145. } else {
  146. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  147. }
  148. } else if (depth == 4) {
  149. if (maxval < 256) {
  150. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  151. } else {
  152. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  153. }
  154. } else {
  155. return AVERROR_INVALIDDATA;
  156. }
  157. return 0;
  158. } else {
  159. av_assert0(0);
  160. }
  161. pnm_get(s, buf1, sizeof(buf1));
  162. w = atoi(buf1);
  163. pnm_get(s, buf1, sizeof(buf1));
  164. h = atoi(buf1);
  165. if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
  166. return AVERROR_INVALIDDATA;
  167. ret = ff_set_dimensions(avctx, w, h);
  168. if (ret < 0)
  169. return ret;
  170. if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 || avctx->pix_fmt == AV_PIX_FMT_GRAYF32) {
  171. pnm_get(s, buf1, sizeof(buf1));
  172. if (av_sscanf(buf1, "%f", &s->scale) != 1 || s->scale == 0.0 || !isfinite(s->scale)) {
  173. av_log(avctx, AV_LOG_ERROR, "Invalid scale.\n");
  174. return AVERROR_INVALIDDATA;
  175. }
  176. s->endian = s->scale < 0.f;
  177. s->scale = fabsf(s->scale);
  178. s->maxval = (1ULL << 32) - 1;
  179. } else if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
  180. pnm_get(s, buf1, sizeof(buf1));
  181. s->maxval = atoi(buf1);
  182. if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
  183. av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
  184. s->maxval = 255;
  185. }
  186. if (s->maxval >= 256) {
  187. if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  188. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  189. } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
  190. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  191. } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
  192. if (s->maxval < 512)
  193. avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
  194. else if (s->maxval < 1024)
  195. avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
  196. else
  197. avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
  198. } else {
  199. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  200. avctx->pix_fmt = AV_PIX_FMT_NONE;
  201. return AVERROR_INVALIDDATA;
  202. }
  203. }
  204. }else
  205. s->maxval=1;
  206. if (!pnm_space(s->bytestream[-1]))
  207. return AVERROR_INVALIDDATA;
  208. /* more check if YUV420 */
  209. if ((av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  210. avctx->pix_fmt != AV_PIX_FMT_GBRPF32) {
  211. if ((avctx->width & 1) != 0)
  212. return AVERROR_INVALIDDATA;
  213. h = (avctx->height * 2);
  214. if ((h % 3) != 0)
  215. return AVERROR_INVALIDDATA;
  216. h /= 3;
  217. avctx->height = h;
  218. }
  219. return 0;
  220. }