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.

219 lines
7.1KB

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