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.

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