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.

323 lines
11KB

  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, k, 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 (n * avctx->height > s->bytestream_end - s->bytestream)
  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. for (k = 0; k < 5 && c <= 9; k += 1) {
  140. v = 10*v + c;
  141. c = (*s->bytestream++) - '0';
  142. }
  143. if (v > s->maxval) {
  144. av_log(avctx, AV_LOG_ERROR, "value %d larger than maxval %d\n", v, s->maxval);
  145. return AVERROR_INVALIDDATA;
  146. }
  147. }
  148. if (sample_len == 16) {
  149. ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
  150. } else
  151. put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
  152. }
  153. if (sample_len != 16)
  154. flush_put_bits(&pb);
  155. ptr+= linesize;
  156. }
  157. }else{
  158. for (i = 0; i < avctx->height; i++) {
  159. if (!upgrade)
  160. samplecpy(ptr, s->bytestream, n, s->maxval);
  161. else if (upgrade == 1) {
  162. unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
  163. for (j = 0; j < n; j++)
  164. ptr[j] = (s->bytestream[j] * f + 64) >> 7;
  165. } else if (upgrade == 2) {
  166. unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  167. for (j = 0; j < n / 2; j++) {
  168. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  169. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  170. }
  171. }
  172. s->bytestream += n;
  173. ptr += linesize;
  174. }
  175. }
  176. break;
  177. case AV_PIX_FMT_YUV420P:
  178. case AV_PIX_FMT_YUV420P9:
  179. case AV_PIX_FMT_YUV420P10:
  180. {
  181. unsigned char *ptr1, *ptr2;
  182. n = avctx->width;
  183. ptr = p->data[0];
  184. linesize = p->linesize[0];
  185. if (s->maxval >= 256)
  186. n *= 2;
  187. if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
  188. return AVERROR_INVALIDDATA;
  189. for (i = 0; i < avctx->height; i++) {
  190. samplecpy(ptr, s->bytestream, n, s->maxval);
  191. s->bytestream += n;
  192. ptr += linesize;
  193. }
  194. ptr1 = p->data[1];
  195. ptr2 = p->data[2];
  196. n >>= 1;
  197. h = avctx->height >> 1;
  198. for (i = 0; i < h; i++) {
  199. samplecpy(ptr1, s->bytestream, n, s->maxval);
  200. s->bytestream += n;
  201. samplecpy(ptr2, s->bytestream, n, s->maxval);
  202. s->bytestream += n;
  203. ptr1 += p->linesize[1];
  204. ptr2 += p->linesize[2];
  205. }
  206. }
  207. break;
  208. case AV_PIX_FMT_YUV420P16:
  209. {
  210. uint16_t *ptr1, *ptr2;
  211. const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  212. unsigned int j, v;
  213. n = avctx->width * 2;
  214. ptr = p->data[0];
  215. linesize = p->linesize[0];
  216. if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
  217. return AVERROR_INVALIDDATA;
  218. for (i = 0; i < avctx->height; i++) {
  219. for (j = 0; j < n / 2; j++) {
  220. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  221. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  222. }
  223. s->bytestream += n;
  224. ptr += linesize;
  225. }
  226. ptr1 = (uint16_t*)p->data[1];
  227. ptr2 = (uint16_t*)p->data[2];
  228. n >>= 1;
  229. h = avctx->height >> 1;
  230. for (i = 0; i < h; i++) {
  231. for (j = 0; j < n / 2; j++) {
  232. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  233. ptr1[j] = (v * f + 16384) >> 15;
  234. }
  235. s->bytestream += n;
  236. for (j = 0; j < n / 2; j++) {
  237. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  238. ptr2[j] = (v * f + 16384) >> 15;
  239. }
  240. s->bytestream += n;
  241. ptr1 += p->linesize[1] / 2;
  242. ptr2 += p->linesize[2] / 2;
  243. }
  244. }
  245. break;
  246. }
  247. *got_frame = 1;
  248. return s->bytestream - s->bytestream_start;
  249. }
  250. #if CONFIG_PGM_DECODER
  251. AVCodec ff_pgm_decoder = {
  252. .name = "pgm",
  253. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  254. .type = AVMEDIA_TYPE_VIDEO,
  255. .id = AV_CODEC_ID_PGM,
  256. .priv_data_size = sizeof(PNMContext),
  257. .decode = pnm_decode_frame,
  258. .capabilities = AV_CODEC_CAP_DR1,
  259. };
  260. #endif
  261. #if CONFIG_PGMYUV_DECODER
  262. AVCodec ff_pgmyuv_decoder = {
  263. .name = "pgmyuv",
  264. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  265. .type = AVMEDIA_TYPE_VIDEO,
  266. .id = AV_CODEC_ID_PGMYUV,
  267. .priv_data_size = sizeof(PNMContext),
  268. .decode = pnm_decode_frame,
  269. .capabilities = AV_CODEC_CAP_DR1,
  270. };
  271. #endif
  272. #if CONFIG_PPM_DECODER
  273. AVCodec ff_ppm_decoder = {
  274. .name = "ppm",
  275. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  276. .type = AVMEDIA_TYPE_VIDEO,
  277. .id = AV_CODEC_ID_PPM,
  278. .priv_data_size = sizeof(PNMContext),
  279. .decode = pnm_decode_frame,
  280. .capabilities = AV_CODEC_CAP_DR1,
  281. };
  282. #endif
  283. #if CONFIG_PBM_DECODER
  284. AVCodec ff_pbm_decoder = {
  285. .name = "pbm",
  286. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  287. .type = AVMEDIA_TYPE_VIDEO,
  288. .id = AV_CODEC_ID_PBM,
  289. .priv_data_size = sizeof(PNMContext),
  290. .decode = pnm_decode_frame,
  291. .capabilities = AV_CODEC_CAP_DR1,
  292. };
  293. #endif
  294. #if CONFIG_PAM_DECODER
  295. AVCodec ff_pam_decoder = {
  296. .name = "pam",
  297. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  298. .type = AVMEDIA_TYPE_VIDEO,
  299. .id = AV_CODEC_ID_PAM,
  300. .priv_data_size = sizeof(PNMContext),
  301. .decode = pnm_decode_frame,
  302. .capabilities = AV_CODEC_CAP_DR1,
  303. };
  304. #endif