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.

260 lines
8.1KB

  1. /*
  2. * PNM image format
  3. * Copyright (c) 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "bytestream.h"
  23. #include "internal.h"
  24. #include "put_bits.h"
  25. #include "pnm.h"
  26. static int pnm_decode_frame(AVCodecContext *avctx, void *data,
  27. int *got_frame, AVPacket *avpkt)
  28. {
  29. const uint8_t *buf = avpkt->data;
  30. int buf_size = avpkt->size;
  31. PNMContext * const s = avctx->priv_data;
  32. AVFrame *picture = data;
  33. AVFrame * const p = &s->picture;
  34. int i, j, n, linesize, h, upgrade = 0;
  35. unsigned char *ptr;
  36. int components, sample_len;
  37. s->bytestream_start =
  38. s->bytestream = buf;
  39. s->bytestream_end = buf + buf_size;
  40. if (ff_pnm_decode_header(avctx, s) < 0)
  41. return -1;
  42. if (p->data[0])
  43. avctx->release_buffer(avctx, p);
  44. p->reference = 0;
  45. if (ff_get_buffer(avctx, p) < 0) {
  46. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  47. return -1;
  48. }
  49. p->pict_type = AV_PICTURE_TYPE_I;
  50. p->key_frame = 1;
  51. switch (avctx->pix_fmt) {
  52. default:
  53. return -1;
  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_RGB24:
  60. n = avctx->width * 3;
  61. components=3;
  62. sample_len=8;
  63. goto do_read;
  64. case AV_PIX_FMT_GRAY8:
  65. n = avctx->width;
  66. components=1;
  67. sample_len=8;
  68. if (s->maxval < 255)
  69. upgrade = 1;
  70. goto do_read;
  71. case AV_PIX_FMT_GRAY16BE:
  72. case AV_PIX_FMT_GRAY16LE:
  73. n = avctx->width * 2;
  74. components=1;
  75. sample_len=16;
  76. if (s->maxval < 65535)
  77. upgrade = 2;
  78. goto do_read;
  79. case AV_PIX_FMT_MONOWHITE:
  80. case AV_PIX_FMT_MONOBLACK:
  81. n = (avctx->width + 7) >> 3;
  82. components=1;
  83. sample_len=1;
  84. do_read:
  85. ptr = p->data[0];
  86. linesize = p->linesize[0];
  87. if (s->bytestream + n * avctx->height > s->bytestream_end)
  88. return -1;
  89. if(s->type < 4){
  90. for (i=0; i<avctx->height; i++) {
  91. PutBitContext pb;
  92. init_put_bits(&pb, ptr, linesize);
  93. for(j=0; j<avctx->width * components; j++){
  94. unsigned int c=0;
  95. int v=0;
  96. while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
  97. s->bytestream++;
  98. if(s->bytestream >= s->bytestream_end)
  99. return -1;
  100. do{
  101. v= 10*v + c;
  102. c= (*s->bytestream++) - '0';
  103. }while(c <= 9);
  104. put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
  105. }
  106. flush_put_bits(&pb);
  107. ptr+= linesize;
  108. }
  109. }else{
  110. for (i = 0; i < avctx->height; i++) {
  111. if (!upgrade)
  112. memcpy(ptr, s->bytestream, n);
  113. else if (upgrade == 1) {
  114. unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
  115. for (j = 0; j < n; j++)
  116. ptr[j] = (s->bytestream[j] * f + 64) >> 7;
  117. } else if (upgrade == 2) {
  118. unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  119. for (j = 0; j < n / 2; j++) {
  120. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  121. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  122. }
  123. }
  124. s->bytestream += n;
  125. ptr += linesize;
  126. }
  127. }
  128. break;
  129. case AV_PIX_FMT_YUV420P:
  130. {
  131. unsigned char *ptr1, *ptr2;
  132. n = avctx->width;
  133. ptr = p->data[0];
  134. linesize = p->linesize[0];
  135. if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
  136. return -1;
  137. for (i = 0; i < avctx->height; i++) {
  138. memcpy(ptr, s->bytestream, n);
  139. s->bytestream += n;
  140. ptr += linesize;
  141. }
  142. ptr1 = p->data[1];
  143. ptr2 = p->data[2];
  144. n >>= 1;
  145. h = avctx->height >> 1;
  146. for (i = 0; i < h; i++) {
  147. memcpy(ptr1, s->bytestream, n);
  148. s->bytestream += n;
  149. memcpy(ptr2, s->bytestream, n);
  150. s->bytestream += n;
  151. ptr1 += p->linesize[1];
  152. ptr2 += p->linesize[2];
  153. }
  154. }
  155. break;
  156. case AV_PIX_FMT_RGB32:
  157. ptr = p->data[0];
  158. linesize = p->linesize[0];
  159. if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
  160. return -1;
  161. for (i = 0; i < avctx->height; i++) {
  162. int j, r, g, b, a;
  163. for (j = 0; j < avctx->width; j++) {
  164. r = *s->bytestream++;
  165. g = *s->bytestream++;
  166. b = *s->bytestream++;
  167. a = *s->bytestream++;
  168. ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
  169. }
  170. ptr += linesize;
  171. }
  172. break;
  173. }
  174. *picture = s->picture;
  175. *got_frame = 1;
  176. return s->bytestream - s->bytestream_start;
  177. }
  178. #if CONFIG_PGM_DECODER
  179. AVCodec ff_pgm_decoder = {
  180. .name = "pgm",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .id = AV_CODEC_ID_PGM,
  183. .priv_data_size = sizeof(PNMContext),
  184. .init = ff_pnm_init,
  185. .close = ff_pnm_end,
  186. .decode = pnm_decode_frame,
  187. .capabilities = CODEC_CAP_DR1,
  188. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  189. };
  190. #endif
  191. #if CONFIG_PGMYUV_DECODER
  192. AVCodec ff_pgmyuv_decoder = {
  193. .name = "pgmyuv",
  194. .type = AVMEDIA_TYPE_VIDEO,
  195. .id = AV_CODEC_ID_PGMYUV,
  196. .priv_data_size = sizeof(PNMContext),
  197. .init = ff_pnm_init,
  198. .close = ff_pnm_end,
  199. .decode = pnm_decode_frame,
  200. .capabilities = CODEC_CAP_DR1,
  201. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  202. };
  203. #endif
  204. #if CONFIG_PPM_DECODER
  205. AVCodec ff_ppm_decoder = {
  206. .name = "ppm",
  207. .type = AVMEDIA_TYPE_VIDEO,
  208. .id = AV_CODEC_ID_PPM,
  209. .priv_data_size = sizeof(PNMContext),
  210. .init = ff_pnm_init,
  211. .close = ff_pnm_end,
  212. .decode = pnm_decode_frame,
  213. .capabilities = CODEC_CAP_DR1,
  214. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  215. };
  216. #endif
  217. #if CONFIG_PBM_DECODER
  218. AVCodec ff_pbm_decoder = {
  219. .name = "pbm",
  220. .type = AVMEDIA_TYPE_VIDEO,
  221. .id = AV_CODEC_ID_PBM,
  222. .priv_data_size = sizeof(PNMContext),
  223. .init = ff_pnm_init,
  224. .close = ff_pnm_end,
  225. .decode = pnm_decode_frame,
  226. .capabilities = CODEC_CAP_DR1,
  227. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  228. };
  229. #endif
  230. #if CONFIG_PAM_DECODER
  231. AVCodec ff_pam_decoder = {
  232. .name = "pam",
  233. .type = AVMEDIA_TYPE_VIDEO,
  234. .id = AV_CODEC_ID_PAM,
  235. .priv_data_size = sizeof(PNMContext),
  236. .init = ff_pnm_init,
  237. .close = ff_pnm_end,
  238. .decode = pnm_decode_frame,
  239. .capabilities = CODEC_CAP_DR1,
  240. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  241. };
  242. #endif