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.

291 lines
9.4KB

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