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.0KB

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