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.

235 lines
6.7KB

  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 "bytestream.h"
  23. #include "pnm.h"
  24. static int pnm_decode_frame(AVCodecContext *avctx, void *data,
  25. int *data_size, AVPacket *avpkt)
  26. {
  27. const uint8_t *buf = avpkt->data;
  28. int buf_size = avpkt->size;
  29. PNMContext * const s = avctx->priv_data;
  30. AVFrame *picture = data;
  31. AVFrame * const p = (AVFrame*)&s->picture;
  32. int i, n, linesize, h, upgrade = 0;
  33. unsigned char *ptr;
  34. s->bytestream_start =
  35. s->bytestream = buf;
  36. s->bytestream_end = buf + buf_size;
  37. if (ff_pnm_decode_header(avctx, s) < 0)
  38. return -1;
  39. if (p->data[0])
  40. avctx->release_buffer(avctx, p);
  41. p->reference = 0;
  42. if (avctx->get_buffer(avctx, p) < 0) {
  43. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  44. return -1;
  45. }
  46. p->pict_type = FF_I_TYPE;
  47. p->key_frame = 1;
  48. switch (avctx->pix_fmt) {
  49. default:
  50. return -1;
  51. case PIX_FMT_RGB48BE:
  52. n = avctx->width * 6;
  53. goto do_read;
  54. case PIX_FMT_RGB24:
  55. n = avctx->width * 3;
  56. goto do_read;
  57. case PIX_FMT_GRAY8:
  58. n = avctx->width;
  59. if (s->maxval < 255)
  60. upgrade = 1;
  61. goto do_read;
  62. case PIX_FMT_GRAY16BE:
  63. case PIX_FMT_GRAY16LE:
  64. n = avctx->width * 2;
  65. if (s->maxval < 65535)
  66. upgrade = 2;
  67. goto do_read;
  68. case PIX_FMT_MONOWHITE:
  69. case PIX_FMT_MONOBLACK:
  70. n = (avctx->width + 7) >> 3;
  71. do_read:
  72. ptr = p->data[0];
  73. linesize = p->linesize[0];
  74. if (s->bytestream + n * avctx->height > s->bytestream_end)
  75. return -1;
  76. for (i = 0; i < avctx->height; i++) {
  77. if (!upgrade)
  78. memcpy(ptr, s->bytestream, n);
  79. else if (upgrade == 1) {
  80. unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
  81. for (j = 0; j < n; j++)
  82. ptr[j] = (s->bytestream[j] * f + 64) >> 7;
  83. } else if (upgrade == 2) {
  84. unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  85. for (j = 0; j < n / 2; j++) {
  86. v = be2me_16(((uint16_t *)s->bytestream)[j]);
  87. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  88. }
  89. }
  90. s->bytestream += n;
  91. ptr += linesize;
  92. }
  93. break;
  94. case PIX_FMT_YUV420P:
  95. {
  96. unsigned char *ptr1, *ptr2;
  97. n = avctx->width;
  98. ptr = p->data[0];
  99. linesize = p->linesize[0];
  100. if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
  101. return -1;
  102. for (i = 0; i < avctx->height; i++) {
  103. memcpy(ptr, s->bytestream, n);
  104. s->bytestream += n;
  105. ptr += linesize;
  106. }
  107. ptr1 = p->data[1];
  108. ptr2 = p->data[2];
  109. n >>= 1;
  110. h = avctx->height >> 1;
  111. for (i = 0; i < h; i++) {
  112. memcpy(ptr1, s->bytestream, n);
  113. s->bytestream += n;
  114. memcpy(ptr2, s->bytestream, n);
  115. s->bytestream += n;
  116. ptr1 += p->linesize[1];
  117. ptr2 += p->linesize[2];
  118. }
  119. }
  120. break;
  121. case PIX_FMT_RGB32:
  122. ptr = p->data[0];
  123. linesize = p->linesize[0];
  124. if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
  125. return -1;
  126. for (i = 0; i < avctx->height; i++) {
  127. int j, r, g, b, a;
  128. for (j = 0; j < avctx->width; j++) {
  129. r = *s->bytestream++;
  130. g = *s->bytestream++;
  131. b = *s->bytestream++;
  132. a = *s->bytestream++;
  133. ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
  134. }
  135. ptr += linesize;
  136. }
  137. break;
  138. }
  139. *picture = *(AVFrame*)&s->picture;
  140. *data_size = sizeof(AVPicture);
  141. return s->bytestream - s->bytestream_start;
  142. }
  143. #if CONFIG_PGM_DECODER
  144. AVCodec pgm_decoder = {
  145. "pgm",
  146. CODEC_TYPE_VIDEO,
  147. CODEC_ID_PGM,
  148. sizeof(PNMContext),
  149. ff_pnm_init,
  150. NULL,
  151. ff_pnm_end,
  152. pnm_decode_frame,
  153. CODEC_CAP_DR1,
  154. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
  155. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  156. };
  157. #endif
  158. #if CONFIG_PGMYUV_DECODER
  159. AVCodec pgmyuv_decoder = {
  160. "pgmyuv",
  161. CODEC_TYPE_VIDEO,
  162. CODEC_ID_PGMYUV,
  163. sizeof(PNMContext),
  164. ff_pnm_init,
  165. NULL,
  166. ff_pnm_end,
  167. pnm_decode_frame,
  168. CODEC_CAP_DR1,
  169. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  170. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  171. };
  172. #endif
  173. #if CONFIG_PPM_DECODER
  174. AVCodec ppm_decoder = {
  175. "ppm",
  176. CODEC_TYPE_VIDEO,
  177. CODEC_ID_PPM,
  178. sizeof(PNMContext),
  179. ff_pnm_init,
  180. NULL,
  181. ff_pnm_end,
  182. pnm_decode_frame,
  183. CODEC_CAP_DR1,
  184. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
  185. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  186. };
  187. #endif
  188. #if CONFIG_PBM_DECODER
  189. AVCodec pbm_decoder = {
  190. "pbm",
  191. CODEC_TYPE_VIDEO,
  192. CODEC_ID_PBM,
  193. sizeof(PNMContext),
  194. ff_pnm_init,
  195. NULL,
  196. ff_pnm_end,
  197. pnm_decode_frame,
  198. CODEC_CAP_DR1,
  199. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  200. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  201. };
  202. #endif
  203. #if CONFIG_PAM_DECODER
  204. AVCodec pam_decoder = {
  205. "pam",
  206. CODEC_TYPE_VIDEO,
  207. CODEC_ID_PAM,
  208. sizeof(PNMContext),
  209. ff_pnm_init,
  210. NULL,
  211. ff_pnm_end,
  212. pnm_decode_frame,
  213. CODEC_CAP_DR1,
  214. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  215. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  216. };
  217. #endif