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.

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