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.

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