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.

211 lines
6.3KB

  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. s->type= buf1[1]-'0';
  59. if(buf1[0] != 'P')
  60. return -1;
  61. if (s->type==1 || s->type==4) {
  62. avctx->pix_fmt = 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 = PIX_FMT_YUV420P;
  66. else
  67. avctx->pix_fmt = PIX_FMT_GRAY8;
  68. } else if (s->type==3 || s->type==6) {
  69. avctx->pix_fmt = 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 -1;
  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 -1;
  103. avctx->width = w;
  104. avctx->height = h;
  105. s->maxval = maxval;
  106. if (depth == 1) {
  107. if (maxval == 1) {
  108. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  109. } else if (maxval == 255) {
  110. avctx->pix_fmt = PIX_FMT_GRAY8;
  111. } else {
  112. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  113. }
  114. } else if (depth == 2) {
  115. if (maxval == 255)
  116. avctx->pix_fmt = PIX_FMT_GRAY8A;
  117. } else if (depth == 3) {
  118. if (maxval < 256) {
  119. avctx->pix_fmt = PIX_FMT_RGB24;
  120. } else {
  121. avctx->pix_fmt = PIX_FMT_RGB48BE;
  122. }
  123. } else if (depth == 4) {
  124. if (maxval < 256) {
  125. avctx->pix_fmt = PIX_FMT_RGBA;
  126. } else {
  127. avctx->pix_fmt = PIX_FMT_RGBA64BE;
  128. }
  129. } else {
  130. return -1;
  131. }
  132. return 0;
  133. } else {
  134. return -1;
  135. }
  136. pnm_get(s, buf1, sizeof(buf1));
  137. w = atoi(buf1);
  138. pnm_get(s, buf1, sizeof(buf1));
  139. h = atoi(buf1);
  140. if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
  141. return -1;
  142. avctx->width = w;
  143. avctx->height = h;
  144. if (avctx->pix_fmt != PIX_FMT_MONOWHITE && avctx->pix_fmt != PIX_FMT_MONOBLACK) {
  145. pnm_get(s, buf1, sizeof(buf1));
  146. s->maxval = atoi(buf1);
  147. if (s->maxval <= 0) {
  148. av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
  149. s->maxval = 255;
  150. }
  151. if (s->maxval >= 256) {
  152. if (avctx->pix_fmt == PIX_FMT_GRAY8) {
  153. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  154. if (s->maxval != 65535)
  155. avctx->pix_fmt = PIX_FMT_GRAY16;
  156. } else if (avctx->pix_fmt == PIX_FMT_RGB24) {
  157. if (s->maxval > 255)
  158. avctx->pix_fmt = PIX_FMT_RGB48BE;
  159. } else {
  160. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  161. avctx->pix_fmt = PIX_FMT_NONE;
  162. return -1;
  163. }
  164. }
  165. }else
  166. s->maxval=1;
  167. /* more check if YUV420 */
  168. if (avctx->pix_fmt == PIX_FMT_YUV420P) {
  169. if ((avctx->width & 1) != 0)
  170. return -1;
  171. h = (avctx->height * 2);
  172. if ((h % 3) != 0)
  173. return -1;
  174. h /= 3;
  175. avctx->height = h;
  176. }
  177. return 0;
  178. }
  179. av_cold int ff_pnm_end(AVCodecContext *avctx)
  180. {
  181. PNMContext *s = avctx->priv_data;
  182. if (s->picture.data[0])
  183. avctx->release_buffer(avctx, &s->picture);
  184. return 0;
  185. }
  186. av_cold int ff_pnm_init(AVCodecContext *avctx)
  187. {
  188. PNMContext *s = avctx->priv_data;
  189. avcodec_get_frame_defaults(&s->picture);
  190. avctx->coded_frame = &s->picture;
  191. return 0;
  192. }