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.

276 lines
8.2KB

  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. "pgm",
  187. AVMEDIA_TYPE_VIDEO,
  188. CODEC_ID_PGM,
  189. sizeof(PNMContext),
  190. ff_pnm_init,
  191. NULL,
  192. ff_pnm_end,
  193. pnm_decode_frame,
  194. CODEC_CAP_DR1,
  195. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
  196. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  197. };
  198. #endif
  199. #if CONFIG_PGMYUV_DECODER
  200. AVCodec ff_pgmyuv_decoder = {
  201. "pgmyuv",
  202. AVMEDIA_TYPE_VIDEO,
  203. CODEC_ID_PGMYUV,
  204. sizeof(PNMContext),
  205. ff_pnm_init,
  206. NULL,
  207. ff_pnm_end,
  208. pnm_decode_frame,
  209. CODEC_CAP_DR1,
  210. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  211. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  212. };
  213. #endif
  214. #if CONFIG_PPM_DECODER
  215. AVCodec ff_ppm_decoder = {
  216. "ppm",
  217. AVMEDIA_TYPE_VIDEO,
  218. CODEC_ID_PPM,
  219. sizeof(PNMContext),
  220. ff_pnm_init,
  221. NULL,
  222. ff_pnm_end,
  223. pnm_decode_frame,
  224. CODEC_CAP_DR1,
  225. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
  226. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  227. };
  228. #endif
  229. #if CONFIG_PBM_DECODER
  230. AVCodec ff_pbm_decoder = {
  231. "pbm",
  232. AVMEDIA_TYPE_VIDEO,
  233. CODEC_ID_PBM,
  234. sizeof(PNMContext),
  235. ff_pnm_init,
  236. NULL,
  237. ff_pnm_end,
  238. pnm_decode_frame,
  239. CODEC_CAP_DR1,
  240. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  241. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  242. };
  243. #endif
  244. #if CONFIG_PAM_DECODER
  245. AVCodec ff_pam_decoder = {
  246. "pam",
  247. AVMEDIA_TYPE_VIDEO,
  248. CODEC_ID_PAM,
  249. sizeof(PNMContext),
  250. ff_pnm_init,
  251. NULL,
  252. ff_pnm_end,
  253. pnm_decode_frame,
  254. CODEC_CAP_DR1,
  255. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  256. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  257. };
  258. #endif