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.

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