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.

378 lines
12KB

  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. float scale;
  47. s->bytestream_start =
  48. s->bytestream = (uint8_t *)buf;
  49. s->bytestream_end = (uint8_t *)buf + buf_size;
  50. if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
  51. return ret;
  52. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  53. return ret;
  54. p->pict_type = AV_PICTURE_TYPE_I;
  55. p->key_frame = 1;
  56. avctx->bits_per_raw_sample = av_log2(s->maxval) + 1;
  57. switch (avctx->pix_fmt) {
  58. default:
  59. return AVERROR(EINVAL);
  60. case AV_PIX_FMT_RGBA64:
  61. n = avctx->width * 8;
  62. components=4;
  63. sample_len=16;
  64. if (s->maxval < 65535)
  65. upgrade = 2;
  66. goto do_read;
  67. case AV_PIX_FMT_RGB48:
  68. n = avctx->width * 6;
  69. components=3;
  70. sample_len=16;
  71. if (s->maxval < 65535)
  72. upgrade = 2;
  73. goto do_read;
  74. case AV_PIX_FMT_RGBA:
  75. n = avctx->width * 4;
  76. components=4;
  77. sample_len=8;
  78. goto do_read;
  79. case AV_PIX_FMT_RGB24:
  80. n = avctx->width * 3;
  81. components=3;
  82. sample_len=8;
  83. if (s->maxval < 255)
  84. upgrade = 1;
  85. goto do_read;
  86. case AV_PIX_FMT_GRAY8:
  87. n = avctx->width;
  88. components=1;
  89. sample_len=8;
  90. if (s->maxval < 255)
  91. upgrade = 1;
  92. goto do_read;
  93. case AV_PIX_FMT_GRAY8A:
  94. n = avctx->width * 2;
  95. components=2;
  96. sample_len=8;
  97. goto do_read;
  98. case AV_PIX_FMT_GRAY16:
  99. n = avctx->width * 2;
  100. components=1;
  101. sample_len=16;
  102. if (s->maxval < 65535)
  103. upgrade = 2;
  104. goto do_read;
  105. case AV_PIX_FMT_YA16:
  106. n = avctx->width * 4;
  107. components=2;
  108. sample_len=16;
  109. if (s->maxval < 65535)
  110. upgrade = 2;
  111. goto do_read;
  112. case AV_PIX_FMT_MONOWHITE:
  113. case AV_PIX_FMT_MONOBLACK:
  114. n = (avctx->width + 7) >> 3;
  115. components=1;
  116. sample_len=1;
  117. is_mono = 1;
  118. do_read:
  119. ptr = p->data[0];
  120. linesize = p->linesize[0];
  121. if (n * avctx->height > s->bytestream_end - s->bytestream)
  122. return AVERROR_INVALIDDATA;
  123. if(s->type < 4 || (is_mono && s->type==7)){
  124. for (i=0; i<avctx->height; i++) {
  125. PutBitContext pb;
  126. init_put_bits(&pb, ptr, linesize);
  127. for(j=0; j<avctx->width * components; j++){
  128. unsigned int c=0;
  129. unsigned v=0;
  130. if(s->type < 4)
  131. while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
  132. s->bytestream++;
  133. if(s->bytestream >= s->bytestream_end)
  134. return AVERROR_INVALIDDATA;
  135. if (is_mono) {
  136. /* read a single digit */
  137. v = (*s->bytestream++)&1;
  138. } else {
  139. /* read a sequence of digits */
  140. for (k = 0; k < 6 && c <= 9; k += 1) {
  141. v = 10*v + c;
  142. c = (*s->bytestream++) - '0';
  143. }
  144. if (v > s->maxval) {
  145. av_log(avctx, AV_LOG_ERROR, "value %d larger than maxval %d\n", v, s->maxval);
  146. return AVERROR_INVALIDDATA;
  147. }
  148. }
  149. if (sample_len == 16) {
  150. ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
  151. } else
  152. put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
  153. }
  154. if (sample_len != 16)
  155. flush_put_bits(&pb);
  156. ptr+= linesize;
  157. }
  158. }else{
  159. for (i = 0; i < avctx->height; i++) {
  160. if (!upgrade)
  161. samplecpy(ptr, s->bytestream, n, s->maxval);
  162. else if (upgrade == 1) {
  163. unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
  164. for (j = 0; j < n; j++)
  165. ptr[j] = (s->bytestream[j] * f + 64) >> 7;
  166. } else if (upgrade == 2) {
  167. unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  168. for (j = 0; j < n / 2; j++) {
  169. v = AV_RB16(s->bytestream + 2*j);
  170. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  171. }
  172. }
  173. s->bytestream += n;
  174. ptr += linesize;
  175. }
  176. }
  177. break;
  178. case AV_PIX_FMT_YUV420P:
  179. case AV_PIX_FMT_YUV420P9:
  180. case AV_PIX_FMT_YUV420P10:
  181. {
  182. unsigned char *ptr1, *ptr2;
  183. n = avctx->width;
  184. ptr = p->data[0];
  185. linesize = p->linesize[0];
  186. if (s->maxval >= 256)
  187. n *= 2;
  188. if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
  189. return AVERROR_INVALIDDATA;
  190. for (i = 0; i < avctx->height; i++) {
  191. samplecpy(ptr, s->bytestream, n, s->maxval);
  192. s->bytestream += n;
  193. ptr += linesize;
  194. }
  195. ptr1 = p->data[1];
  196. ptr2 = p->data[2];
  197. n >>= 1;
  198. h = avctx->height >> 1;
  199. for (i = 0; i < h; i++) {
  200. samplecpy(ptr1, s->bytestream, n, s->maxval);
  201. s->bytestream += n;
  202. samplecpy(ptr2, s->bytestream, n, s->maxval);
  203. s->bytestream += n;
  204. ptr1 += p->linesize[1];
  205. ptr2 += p->linesize[2];
  206. }
  207. }
  208. break;
  209. case AV_PIX_FMT_YUV420P16:
  210. {
  211. uint16_t *ptr1, *ptr2;
  212. const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  213. unsigned int j, v;
  214. n = avctx->width * 2;
  215. ptr = p->data[0];
  216. linesize = p->linesize[0];
  217. if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
  218. return AVERROR_INVALIDDATA;
  219. for (i = 0; i < avctx->height; i++) {
  220. for (j = 0; j < n / 2; j++) {
  221. v = AV_RB16(s->bytestream + 2*j);
  222. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  223. }
  224. s->bytestream += n;
  225. ptr += linesize;
  226. }
  227. ptr1 = (uint16_t*)p->data[1];
  228. ptr2 = (uint16_t*)p->data[2];
  229. n >>= 1;
  230. h = avctx->height >> 1;
  231. for (i = 0; i < h; i++) {
  232. for (j = 0; j < n / 2; j++) {
  233. v = AV_RB16(s->bytestream + 2*j);
  234. ptr1[j] = (v * f + 16384) >> 15;
  235. }
  236. s->bytestream += n;
  237. for (j = 0; j < n / 2; j++) {
  238. v = AV_RB16(s->bytestream + 2*j);
  239. ptr2[j] = (v * f + 16384) >> 15;
  240. }
  241. s->bytestream += n;
  242. ptr1 += p->linesize[1] / 2;
  243. ptr2 += p->linesize[2] / 2;
  244. }
  245. }
  246. break;
  247. case AV_PIX_FMT_GBRPF32:
  248. if (avctx->width * avctx->height * 12 > s->bytestream_end - s->bytestream)
  249. return AVERROR_INVALIDDATA;
  250. scale = 1.f / s->scale;
  251. if (s->endian) {
  252. float *r, *g, *b;
  253. r = (float *)p->data[2];
  254. g = (float *)p->data[0];
  255. b = (float *)p->data[1];
  256. for (int i = 0; i < avctx->height; i++) {
  257. for (int j = 0; j < avctx->width; j++) {
  258. r[j] = av_int2float(AV_RL32(s->bytestream+0)) * scale;
  259. g[j] = av_int2float(AV_RL32(s->bytestream+4)) * scale;
  260. b[j] = av_int2float(AV_RL32(s->bytestream+8)) * scale;
  261. s->bytestream += 12;
  262. }
  263. r += p->linesize[2] / 4;
  264. g += p->linesize[0] / 4;
  265. b += p->linesize[1] / 4;
  266. }
  267. } else {
  268. float *r, *g, *b;
  269. r = (float *)p->data[2];
  270. g = (float *)p->data[0];
  271. b = (float *)p->data[1];
  272. for (int i = 0; i < avctx->height; i++) {
  273. for (int j = 0; j < avctx->width; j++) {
  274. r[j] = av_int2float(AV_RB32(s->bytestream+0)) * scale;
  275. g[j] = av_int2float(AV_RB32(s->bytestream+4)) * scale;
  276. b[j] = av_int2float(AV_RB32(s->bytestream+8)) * scale;
  277. s->bytestream += 12;
  278. }
  279. r += p->linesize[2] / 4;
  280. g += p->linesize[0] / 4;
  281. b += p->linesize[1] / 4;
  282. }
  283. }
  284. break;
  285. }
  286. *got_frame = 1;
  287. return s->bytestream - s->bytestream_start;
  288. }
  289. #if CONFIG_PGM_DECODER
  290. AVCodec ff_pgm_decoder = {
  291. .name = "pgm",
  292. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .id = AV_CODEC_ID_PGM,
  295. .priv_data_size = sizeof(PNMContext),
  296. .decode = pnm_decode_frame,
  297. .capabilities = AV_CODEC_CAP_DR1,
  298. };
  299. #endif
  300. #if CONFIG_PGMYUV_DECODER
  301. AVCodec ff_pgmyuv_decoder = {
  302. .name = "pgmyuv",
  303. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  304. .type = AVMEDIA_TYPE_VIDEO,
  305. .id = AV_CODEC_ID_PGMYUV,
  306. .priv_data_size = sizeof(PNMContext),
  307. .decode = pnm_decode_frame,
  308. .capabilities = AV_CODEC_CAP_DR1,
  309. };
  310. #endif
  311. #if CONFIG_PPM_DECODER
  312. AVCodec ff_ppm_decoder = {
  313. .name = "ppm",
  314. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  315. .type = AVMEDIA_TYPE_VIDEO,
  316. .id = AV_CODEC_ID_PPM,
  317. .priv_data_size = sizeof(PNMContext),
  318. .decode = pnm_decode_frame,
  319. .capabilities = AV_CODEC_CAP_DR1,
  320. };
  321. #endif
  322. #if CONFIG_PBM_DECODER
  323. AVCodec ff_pbm_decoder = {
  324. .name = "pbm",
  325. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  326. .type = AVMEDIA_TYPE_VIDEO,
  327. .id = AV_CODEC_ID_PBM,
  328. .priv_data_size = sizeof(PNMContext),
  329. .decode = pnm_decode_frame,
  330. .capabilities = AV_CODEC_CAP_DR1,
  331. };
  332. #endif
  333. #if CONFIG_PAM_DECODER
  334. AVCodec ff_pam_decoder = {
  335. .name = "pam",
  336. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  337. .type = AVMEDIA_TYPE_VIDEO,
  338. .id = AV_CODEC_ID_PAM,
  339. .priv_data_size = sizeof(PNMContext),
  340. .decode = pnm_decode_frame,
  341. .capabilities = AV_CODEC_CAP_DR1,
  342. };
  343. #endif
  344. #if CONFIG_PFM_DECODER
  345. AVCodec ff_pfm_decoder = {
  346. .name = "pfm",
  347. .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
  348. .type = AVMEDIA_TYPE_VIDEO,
  349. .id = AV_CODEC_ID_PFM,
  350. .priv_data_size = sizeof(PNMContext),
  351. .decode = pnm_decode_frame,
  352. .capabilities = AV_CODEC_CAP_DR1,
  353. };
  354. #endif