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.

263 lines
8.2KB

  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 "put_bits.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 = &s->picture;
  32. int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
  33. unsigned char *ptr;
  34. int components, sample_len;
  35. s->bytestream_start =
  36. s->bytestream = buf;
  37. s->bytestream_end = buf + buf_size;
  38. if (ff_pnm_decode_header(avctx, s) < 0)
  39. return -1;
  40. if (p->data[0])
  41. avctx->release_buffer(avctx, p);
  42. p->reference = 0;
  43. if (avctx->get_buffer(avctx, p) < 0) {
  44. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  45. return -1;
  46. }
  47. p->pict_type = AV_PICTURE_TYPE_I;
  48. p->key_frame = 1;
  49. switch (avctx->pix_fmt) {
  50. default:
  51. return -1;
  52. case PIX_FMT_RGBA64BE:
  53. n = avctx->width * 8;
  54. components=4;
  55. sample_len=16;
  56. goto do_read;
  57. case PIX_FMT_RGB48BE:
  58. n = avctx->width * 6;
  59. components=3;
  60. sample_len=16;
  61. goto do_read;
  62. case PIX_FMT_RGBA:
  63. n = avctx->width * 4;
  64. components=4;
  65. sample_len=8;
  66. goto do_read;
  67. case PIX_FMT_RGB24:
  68. n = avctx->width * 3;
  69. components=3;
  70. sample_len=8;
  71. goto do_read;
  72. case PIX_FMT_GRAY8:
  73. n = avctx->width;
  74. components=1;
  75. sample_len=8;
  76. if (s->maxval < 255)
  77. upgrade = 1;
  78. goto do_read;
  79. case PIX_FMT_GRAY8A:
  80. n = avctx->width * 2;
  81. components=2;
  82. sample_len=8;
  83. goto do_read;
  84. case PIX_FMT_GRAY16BE:
  85. case PIX_FMT_GRAY16LE:
  86. n = avctx->width * 2;
  87. components=1;
  88. sample_len=16;
  89. if (s->maxval < 65535)
  90. upgrade = 2;
  91. goto do_read;
  92. case PIX_FMT_MONOWHITE:
  93. case PIX_FMT_MONOBLACK:
  94. n = (avctx->width + 7) >> 3;
  95. components=1;
  96. sample_len=1;
  97. is_mono = 1;
  98. do_read:
  99. ptr = p->data[0];
  100. linesize = p->linesize[0];
  101. if (s->bytestream + n * avctx->height > s->bytestream_end)
  102. return -1;
  103. if(s->type < 4 || (is_mono && s->type==7)){
  104. for (i=0; i<avctx->height; i++) {
  105. PutBitContext pb;
  106. init_put_bits(&pb, ptr, linesize);
  107. for(j=0; j<avctx->width * components; j++){
  108. unsigned int c=0;
  109. int v=0;
  110. if(s->type < 4)
  111. while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
  112. s->bytestream++;
  113. if(s->bytestream >= s->bytestream_end)
  114. return -1;
  115. if (is_mono) {
  116. /* read a single digit */
  117. v = (*s->bytestream++)&1;
  118. } else {
  119. /* read a sequence of digits */
  120. do {
  121. v = 10*v + c;
  122. c = (*s->bytestream++) - '0';
  123. } while (c <= 9);
  124. }
  125. put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
  126. }
  127. flush_put_bits(&pb);
  128. ptr+= linesize;
  129. }
  130. }else{
  131. for (i = 0; i < avctx->height; i++) {
  132. if (!upgrade)
  133. memcpy(ptr, s->bytestream, n);
  134. else if (upgrade == 1) {
  135. unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
  136. for (j = 0; j < n; j++)
  137. ptr[j] = (s->bytestream[j] * f + 64) >> 7;
  138. } else if (upgrade == 2) {
  139. unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  140. for (j = 0; j < n / 2; j++) {
  141. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  142. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  143. }
  144. }
  145. s->bytestream += n;
  146. ptr += linesize;
  147. }
  148. }
  149. break;
  150. case PIX_FMT_YUV420P:
  151. {
  152. unsigned char *ptr1, *ptr2;
  153. n = avctx->width;
  154. ptr = p->data[0];
  155. linesize = p->linesize[0];
  156. if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
  157. return -1;
  158. for (i = 0; i < avctx->height; i++) {
  159. memcpy(ptr, s->bytestream, n);
  160. s->bytestream += n;
  161. ptr += linesize;
  162. }
  163. ptr1 = p->data[1];
  164. ptr2 = p->data[2];
  165. n >>= 1;
  166. h = avctx->height >> 1;
  167. for (i = 0; i < h; i++) {
  168. memcpy(ptr1, s->bytestream, n);
  169. s->bytestream += n;
  170. memcpy(ptr2, s->bytestream, n);
  171. s->bytestream += n;
  172. ptr1 += p->linesize[1];
  173. ptr2 += p->linesize[2];
  174. }
  175. }
  176. break;
  177. }
  178. *picture = s->picture;
  179. *data_size = sizeof(AVPicture);
  180. return s->bytestream - s->bytestream_start;
  181. }
  182. #if CONFIG_PGM_DECODER
  183. AVCodec ff_pgm_decoder = {
  184. .name = "pgm",
  185. .type = AVMEDIA_TYPE_VIDEO,
  186. .id = AV_CODEC_ID_PGM,
  187. .priv_data_size = sizeof(PNMContext),
  188. .init = ff_pnm_init,
  189. .close = ff_pnm_end,
  190. .decode = pnm_decode_frame,
  191. .capabilities = CODEC_CAP_DR1,
  192. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  193. };
  194. #endif
  195. #if CONFIG_PGMYUV_DECODER
  196. AVCodec ff_pgmyuv_decoder = {
  197. .name = "pgmyuv",
  198. .type = AVMEDIA_TYPE_VIDEO,
  199. .id = AV_CODEC_ID_PGMYUV,
  200. .priv_data_size = sizeof(PNMContext),
  201. .init = ff_pnm_init,
  202. .close = ff_pnm_end,
  203. .decode = pnm_decode_frame,
  204. .capabilities = CODEC_CAP_DR1,
  205. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  206. };
  207. #endif
  208. #if CONFIG_PPM_DECODER
  209. AVCodec ff_ppm_decoder = {
  210. .name = "ppm",
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .id = AV_CODEC_ID_PPM,
  213. .priv_data_size = sizeof(PNMContext),
  214. .init = ff_pnm_init,
  215. .close = ff_pnm_end,
  216. .decode = pnm_decode_frame,
  217. .capabilities = CODEC_CAP_DR1,
  218. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  219. };
  220. #endif
  221. #if CONFIG_PBM_DECODER
  222. AVCodec ff_pbm_decoder = {
  223. .name = "pbm",
  224. .type = AVMEDIA_TYPE_VIDEO,
  225. .id = AV_CODEC_ID_PBM,
  226. .priv_data_size = sizeof(PNMContext),
  227. .init = ff_pnm_init,
  228. .close = ff_pnm_end,
  229. .decode = pnm_decode_frame,
  230. .capabilities = CODEC_CAP_DR1,
  231. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  232. };
  233. #endif
  234. #if CONFIG_PAM_DECODER
  235. AVCodec ff_pam_decoder = {
  236. .name = "pam",
  237. .type = AVMEDIA_TYPE_VIDEO,
  238. .id = AV_CODEC_ID_PAM,
  239. .priv_data_size = sizeof(PNMContext),
  240. .init = ff_pnm_init,
  241. .close = ff_pnm_end,
  242. .decode = pnm_decode_frame,
  243. .capabilities = CODEC_CAP_DR1,
  244. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  245. };
  246. #endif