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.

280 lines
9.0KB

  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_RGB24:
  63. n = avctx->width * 3;
  64. components=3;
  65. sample_len=8;
  66. goto do_read;
  67. case 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 PIX_FMT_GRAY8A:
  75. n = avctx->width * 2;
  76. components=2;
  77. sample_len=8;
  78. goto do_read;
  79. case PIX_FMT_GRAY16BE:
  80. case 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 PIX_FMT_MONOWHITE:
  88. case 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 -1;
  98. if(s->type < 4){
  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. while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
  106. s->bytestream++;
  107. if(s->bytestream >= s->bytestream_end)
  108. return -1;
  109. if (is_mono) {
  110. /* read a single digit */
  111. v = (*s->bytestream++) - '0';
  112. } else {
  113. /* read a sequence of digits */
  114. do {
  115. v = 10*v + c;
  116. c = (*s->bytestream++) - '0';
  117. } while (c <= 9);
  118. }
  119. put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
  120. }
  121. flush_put_bits(&pb);
  122. ptr+= linesize;
  123. }
  124. }else{
  125. for (i = 0; i < avctx->height; i++) {
  126. if (!upgrade)
  127. memcpy(ptr, s->bytestream, n);
  128. else if (upgrade == 1) {
  129. unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
  130. for (j = 0; j < n; j++)
  131. ptr[j] = (s->bytestream[j] * f + 64) >> 7;
  132. } else if (upgrade == 2) {
  133. unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  134. for (j = 0; j < n / 2; j++) {
  135. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  136. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  137. }
  138. }
  139. s->bytestream += n;
  140. ptr += linesize;
  141. }
  142. }
  143. break;
  144. case PIX_FMT_YUV420P:
  145. {
  146. unsigned char *ptr1, *ptr2;
  147. n = avctx->width;
  148. ptr = p->data[0];
  149. linesize = p->linesize[0];
  150. if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
  151. return -1;
  152. for (i = 0; i < avctx->height; i++) {
  153. memcpy(ptr, s->bytestream, n);
  154. s->bytestream += n;
  155. ptr += linesize;
  156. }
  157. ptr1 = p->data[1];
  158. ptr2 = p->data[2];
  159. n >>= 1;
  160. h = avctx->height >> 1;
  161. for (i = 0; i < h; i++) {
  162. memcpy(ptr1, s->bytestream, n);
  163. s->bytestream += n;
  164. memcpy(ptr2, s->bytestream, n);
  165. s->bytestream += n;
  166. ptr1 += p->linesize[1];
  167. ptr2 += p->linesize[2];
  168. }
  169. }
  170. break;
  171. case PIX_FMT_RGB32:
  172. ptr = p->data[0];
  173. linesize = p->linesize[0];
  174. if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
  175. return -1;
  176. for (i = 0; i < avctx->height; i++) {
  177. int j, r, g, b, a;
  178. for (j = 0; j < avctx->width; j++) {
  179. r = *s->bytestream++;
  180. g = *s->bytestream++;
  181. b = *s->bytestream++;
  182. a = *s->bytestream++;
  183. ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
  184. }
  185. ptr += linesize;
  186. }
  187. break;
  188. }
  189. *picture = *(AVFrame*)&s->picture;
  190. *data_size = sizeof(AVPicture);
  191. return s->bytestream - s->bytestream_start;
  192. }
  193. #if CONFIG_PGM_DECODER
  194. AVCodec ff_pgm_decoder = {
  195. .name = "pgm",
  196. .type = AVMEDIA_TYPE_VIDEO,
  197. .id = CODEC_ID_PGM,
  198. .priv_data_size = sizeof(PNMContext),
  199. .init = ff_pnm_init,
  200. .close = ff_pnm_end,
  201. .decode = pnm_decode_frame,
  202. .capabilities = CODEC_CAP_DR1,
  203. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
  204. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  205. };
  206. #endif
  207. #if CONFIG_PGMYUV_DECODER
  208. AVCodec ff_pgmyuv_decoder = {
  209. .name = "pgmyuv",
  210. .type = AVMEDIA_TYPE_VIDEO,
  211. .id = CODEC_ID_PGMYUV,
  212. .priv_data_size = sizeof(PNMContext),
  213. .init = ff_pnm_init,
  214. .close = ff_pnm_end,
  215. .decode = pnm_decode_frame,
  216. .capabilities = CODEC_CAP_DR1,
  217. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  218. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  219. };
  220. #endif
  221. #if CONFIG_PPM_DECODER
  222. AVCodec ff_ppm_decoder = {
  223. .name = "ppm",
  224. .type = AVMEDIA_TYPE_VIDEO,
  225. .id = CODEC_ID_PPM,
  226. .priv_data_size = sizeof(PNMContext),
  227. .init = ff_pnm_init,
  228. .close = ff_pnm_end,
  229. .decode = pnm_decode_frame,
  230. .capabilities = CODEC_CAP_DR1,
  231. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
  232. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  233. };
  234. #endif
  235. #if CONFIG_PBM_DECODER
  236. AVCodec ff_pbm_decoder = {
  237. .name = "pbm",
  238. .type = AVMEDIA_TYPE_VIDEO,
  239. .id = CODEC_ID_PBM,
  240. .priv_data_size = sizeof(PNMContext),
  241. .init = ff_pnm_init,
  242. .close = ff_pnm_end,
  243. .decode = pnm_decode_frame,
  244. .capabilities = CODEC_CAP_DR1,
  245. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  246. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  247. };
  248. #endif
  249. #if CONFIG_PAM_DECODER
  250. AVCodec ff_pam_decoder = {
  251. .name = "pam",
  252. .type = AVMEDIA_TYPE_VIDEO,
  253. .id = CODEC_ID_PAM,
  254. .priv_data_size = sizeof(PNMContext),
  255. .init = ff_pnm_init,
  256. .close = ff_pnm_end,
  257. .decode = pnm_decode_frame,
  258. .capabilities = CODEC_CAP_DR1,
  259. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  260. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  261. };
  262. #endif