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.

319 lines
10KB

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