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.

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