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.

232 lines
7.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 <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 += s->bytestream_end > s->bytestream;
  69. s->bytestream += s->bytestream_end > s->bytestream;
  70. return AVERROR_INVALIDDATA;
  71. }
  72. pnm_get(s, buf1, sizeof(buf1));
  73. s->type= buf1[1]-'0';
  74. if (buf1[1] == 'F') {
  75. avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
  76. } else if (s->type==1 || s->type==4) {
  77. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  78. } else if (s->type==2 || s->type==5) {
  79. if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
  80. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  81. else
  82. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  83. } else if (s->type==3 || s->type==6) {
  84. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  85. } else if (s->type==7) {
  86. w = -1;
  87. h = -1;
  88. maxval = -1;
  89. depth = -1;
  90. tuple_type[0] = '\0';
  91. for (;;) {
  92. pnm_get(s, buf1, sizeof(buf1));
  93. if (!strcmp(buf1, "WIDTH")) {
  94. pnm_get(s, buf1, sizeof(buf1));
  95. w = strtol(buf1, NULL, 10);
  96. } else if (!strcmp(buf1, "HEIGHT")) {
  97. pnm_get(s, buf1, sizeof(buf1));
  98. h = strtol(buf1, NULL, 10);
  99. } else if (!strcmp(buf1, "DEPTH")) {
  100. pnm_get(s, buf1, sizeof(buf1));
  101. depth = strtol(buf1, NULL, 10);
  102. } else if (!strcmp(buf1, "MAXVAL")) {
  103. pnm_get(s, buf1, sizeof(buf1));
  104. maxval = strtol(buf1, NULL, 10);
  105. } else if (!strcmp(buf1, "TUPLTYPE") ||
  106. /* libavcodec used to write invalid files */
  107. !strcmp(buf1, "TUPLETYPE")) {
  108. pnm_get(s, tuple_type, sizeof(tuple_type));
  109. } else if (!strcmp(buf1, "ENDHDR")) {
  110. break;
  111. } else {
  112. return AVERROR_INVALIDDATA;
  113. }
  114. }
  115. if (!pnm_space(s->bytestream[-1]))
  116. return AVERROR_INVALIDDATA;
  117. /* check that all tags are present */
  118. if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
  119. av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
  120. return AVERROR_INVALIDDATA;
  121. ret = ff_set_dimensions(avctx, w, h);
  122. if (ret < 0)
  123. return ret;
  124. s->maxval = maxval;
  125. if (depth == 1) {
  126. if (maxval == 1) {
  127. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  128. } else if (maxval < 256) {
  129. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  130. } else {
  131. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  132. }
  133. } else if (depth == 2) {
  134. if (maxval < 256) {
  135. avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
  136. } else {
  137. avctx->pix_fmt = AV_PIX_FMT_YA16;
  138. }
  139. } else if (depth == 3) {
  140. if (maxval < 256) {
  141. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  142. } else {
  143. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  144. }
  145. } else if (depth == 4) {
  146. if (maxval < 256) {
  147. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  148. } else {
  149. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  150. }
  151. } else {
  152. return AVERROR_INVALIDDATA;
  153. }
  154. return 0;
  155. } else {
  156. av_assert0(0);
  157. }
  158. pnm_get(s, buf1, sizeof(buf1));
  159. w = atoi(buf1);
  160. pnm_get(s, buf1, sizeof(buf1));
  161. h = atoi(buf1);
  162. if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
  163. return AVERROR_INVALIDDATA;
  164. ret = ff_set_dimensions(avctx, w, h);
  165. if (ret < 0)
  166. return ret;
  167. if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32) {
  168. pnm_get(s, buf1, sizeof(buf1));
  169. if (av_sscanf(buf1, "%f", &s->scale) != 1 || s->scale == 0.0 || !isfinite(s->scale)) {
  170. av_log(avctx, AV_LOG_ERROR, "Invalid scale.\n");
  171. return AVERROR_INVALIDDATA;
  172. }
  173. s->endian = s->scale < 0.f;
  174. s->scale = fabsf(s->scale);
  175. s->maxval = (1ULL << 32) - 1;
  176. } else if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
  177. pnm_get(s, buf1, sizeof(buf1));
  178. s->maxval = atoi(buf1);
  179. if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
  180. av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
  181. s->maxval = 255;
  182. }
  183. if (s->maxval >= 256) {
  184. if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  185. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  186. } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
  187. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  188. } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
  189. if (s->maxval < 512)
  190. avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
  191. else if (s->maxval < 1024)
  192. avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
  193. else
  194. avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
  195. } else {
  196. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  197. avctx->pix_fmt = AV_PIX_FMT_NONE;
  198. return AVERROR_INVALIDDATA;
  199. }
  200. }
  201. }else
  202. s->maxval=1;
  203. if (!pnm_space(s->bytestream[-1]))
  204. return AVERROR_INVALIDDATA;
  205. /* more check if YUV420 */
  206. if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) {
  207. if ((avctx->width & 1) != 0)
  208. return AVERROR_INVALIDDATA;
  209. h = (avctx->height * 2);
  210. if ((h % 3) != 0)
  211. return AVERROR_INVALIDDATA;
  212. h /= 3;
  213. avctx->height = h;
  214. }
  215. return 0;
  216. }