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.

190 lines
5.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 "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. s->type= buf1[1]-'0';
  56. if(buf1[0] != 'P')
  57. return -1;
  58. if (s->type==1 || s->type==4) {
  59. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  60. } else if (s->type==2 || s->type==5) {
  61. if (avctx->codec_id == CODEC_ID_PGMYUV)
  62. avctx->pix_fmt = PIX_FMT_YUV420P;
  63. else
  64. avctx->pix_fmt = PIX_FMT_GRAY8;
  65. } else if (s->type==3 || s->type==6) {
  66. avctx->pix_fmt = PIX_FMT_RGB24;
  67. } else if (s->type==7) {
  68. w = -1;
  69. h = -1;
  70. maxval = -1;
  71. depth = -1;
  72. tuple_type[0] = '\0';
  73. for (;;) {
  74. pnm_get(s, buf1, sizeof(buf1));
  75. if (!strcmp(buf1, "WIDTH")) {
  76. pnm_get(s, buf1, sizeof(buf1));
  77. w = strtol(buf1, NULL, 10);
  78. } else if (!strcmp(buf1, "HEIGHT")) {
  79. pnm_get(s, buf1, sizeof(buf1));
  80. h = strtol(buf1, NULL, 10);
  81. } else if (!strcmp(buf1, "DEPTH")) {
  82. pnm_get(s, buf1, sizeof(buf1));
  83. depth = strtol(buf1, NULL, 10);
  84. } else if (!strcmp(buf1, "MAXVAL")) {
  85. pnm_get(s, buf1, sizeof(buf1));
  86. maxval = strtol(buf1, NULL, 10);
  87. } else if (!strcmp(buf1, "TUPLETYPE")) {
  88. pnm_get(s, tuple_type, sizeof(tuple_type));
  89. } else if (!strcmp(buf1, "ENDHDR")) {
  90. break;
  91. } else {
  92. return -1;
  93. }
  94. }
  95. /* check that all tags are present */
  96. if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
  97. return -1;
  98. avctx->width = w;
  99. avctx->height = h;
  100. if (depth == 1) {
  101. if (maxval == 1)
  102. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  103. else
  104. avctx->pix_fmt = PIX_FMT_GRAY8;
  105. } else if (depth == 3) {
  106. if (maxval < 256) {
  107. avctx->pix_fmt = PIX_FMT_RGB24;
  108. } else {
  109. av_log(avctx, AV_LOG_ERROR, "16-bit components are only supported for grayscale\n");
  110. avctx->pix_fmt = PIX_FMT_NONE;
  111. return -1;
  112. }
  113. } else if (depth == 4) {
  114. avctx->pix_fmt = PIX_FMT_RGB32;
  115. } else {
  116. return -1;
  117. }
  118. return 0;
  119. } else {
  120. return -1;
  121. }
  122. pnm_get(s, buf1, sizeof(buf1));
  123. avctx->width = atoi(buf1);
  124. if (avctx->width <= 0)
  125. return -1;
  126. pnm_get(s, buf1, sizeof(buf1));
  127. avctx->height = atoi(buf1);
  128. if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  129. return -1;
  130. if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
  131. pnm_get(s, buf1, sizeof(buf1));
  132. s->maxval = atoi(buf1);
  133. if (s->maxval >= 256) {
  134. if (avctx->pix_fmt == PIX_FMT_GRAY8) {
  135. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  136. if (s->maxval != 65535)
  137. avctx->pix_fmt = PIX_FMT_GRAY16;
  138. } else if (avctx->pix_fmt == PIX_FMT_RGB24) {
  139. if (s->maxval > 255)
  140. avctx->pix_fmt = PIX_FMT_RGB48BE;
  141. } else {
  142. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  143. avctx->pix_fmt = PIX_FMT_NONE;
  144. return -1;
  145. }
  146. }
  147. }else
  148. s->maxval=1;
  149. /* more check if YUV420 */
  150. if (avctx->pix_fmt == PIX_FMT_YUV420P) {
  151. if ((avctx->width & 1) != 0)
  152. return -1;
  153. h = (avctx->height * 2);
  154. if ((h % 3) != 0)
  155. return -1;
  156. h /= 3;
  157. avctx->height = h;
  158. }
  159. return 0;
  160. }
  161. av_cold int ff_pnm_end(AVCodecContext *avctx)
  162. {
  163. PNMContext *s = avctx->priv_data;
  164. if (s->picture.data[0])
  165. avctx->release_buffer(avctx, &s->picture);
  166. return 0;
  167. }
  168. av_cold int ff_pnm_init(AVCodecContext *avctx)
  169. {
  170. PNMContext *s = avctx->priv_data;
  171. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  172. avctx->coded_frame = (AVFrame*)&s->picture;
  173. return 0;
  174. }