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.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 "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 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
  102. av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
  103. return AVERROR_INVALIDDATA;
  104. avctx->width = w;
  105. avctx->height = h;
  106. s->maxval = maxval;
  107. if (depth == 1) {
  108. if (maxval == 1) {
  109. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  110. } else if (maxval < 256) {
  111. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  112. } else {
  113. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  114. }
  115. } else if (depth == 2) {
  116. if (maxval < 256) {
  117. avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
  118. } else {
  119. avctx->pix_fmt = AV_PIX_FMT_YA16;
  120. }
  121. } else if (depth == 3) {
  122. if (maxval < 256) {
  123. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  124. } else {
  125. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  126. }
  127. } else if (depth == 4) {
  128. if (maxval < 256) {
  129. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  130. } else {
  131. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  132. }
  133. } else {
  134. return AVERROR_INVALIDDATA;
  135. }
  136. return 0;
  137. } else {
  138. return AVERROR_INVALIDDATA;
  139. }
  140. pnm_get(s, buf1, sizeof(buf1));
  141. w = atoi(buf1);
  142. pnm_get(s, buf1, sizeof(buf1));
  143. h = atoi(buf1);
  144. if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
  145. return AVERROR_INVALIDDATA;
  146. avctx->width = w;
  147. avctx->height = h;
  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 || s->maxval > UINT16_MAX) {
  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. }