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.

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