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.

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