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.

199 lines
6.5KB

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