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.

274 lines
8.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 "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;
  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 = FF_I_TYPE;
  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 = *(AVFrame*)&s->picture;
  174. *data_size = sizeof(AVPicture);
  175. return s->bytestream - s->bytestream_start;
  176. }
  177. #if CONFIG_PGM_DECODER
  178. AVCodec pgm_decoder = {
  179. "pgm",
  180. AVMEDIA_TYPE_VIDEO,
  181. CODEC_ID_PGM,
  182. sizeof(PNMContext),
  183. ff_pnm_init,
  184. NULL,
  185. ff_pnm_end,
  186. pnm_decode_frame,
  187. CODEC_CAP_DR1,
  188. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
  189. .max_lowres = 5,
  190. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  191. };
  192. #endif
  193. #if CONFIG_PGMYUV_DECODER
  194. AVCodec pgmyuv_decoder = {
  195. "pgmyuv",
  196. AVMEDIA_TYPE_VIDEO,
  197. CODEC_ID_PGMYUV,
  198. sizeof(PNMContext),
  199. ff_pnm_init,
  200. NULL,
  201. ff_pnm_end,
  202. pnm_decode_frame,
  203. CODEC_CAP_DR1,
  204. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  205. .max_lowres = 5,
  206. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  207. };
  208. #endif
  209. #if CONFIG_PPM_DECODER
  210. AVCodec ppm_decoder = {
  211. "ppm",
  212. AVMEDIA_TYPE_VIDEO,
  213. CODEC_ID_PPM,
  214. sizeof(PNMContext),
  215. ff_pnm_init,
  216. NULL,
  217. ff_pnm_end,
  218. pnm_decode_frame,
  219. CODEC_CAP_DR1,
  220. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
  221. .max_lowres = 5,
  222. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  223. };
  224. #endif
  225. #if CONFIG_PBM_DECODER
  226. AVCodec pbm_decoder = {
  227. "pbm",
  228. AVMEDIA_TYPE_VIDEO,
  229. CODEC_ID_PBM,
  230. sizeof(PNMContext),
  231. ff_pnm_init,
  232. NULL,
  233. ff_pnm_end,
  234. pnm_decode_frame,
  235. CODEC_CAP_DR1,
  236. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  237. .max_lowres = 5,
  238. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  239. };
  240. #endif
  241. #if CONFIG_PAM_DECODER
  242. AVCodec pam_decoder = {
  243. "pam",
  244. AVMEDIA_TYPE_VIDEO,
  245. CODEC_ID_PAM,
  246. sizeof(PNMContext),
  247. ff_pnm_init,
  248. NULL,
  249. ff_pnm_end,
  250. pnm_decode_frame,
  251. CODEC_CAP_DR1,
  252. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  253. .max_lowres = 5,
  254. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  255. };
  256. #endif