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.

271 lines
8.8KB

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