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.

267 lines
8.6KB

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