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.

289 lines
9.4KB

  1. /*
  2. * PNM image format
  3. * Copyright (c) 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "internal.h"
  24. #include "put_bits.h"
  25. #include "pnm.h"
  26. static int pnm_decode_frame(AVCodecContext *avctx, void *data,
  27. int *got_frame, AVPacket *avpkt)
  28. {
  29. const uint8_t *buf = avpkt->data;
  30. int buf_size = avpkt->size;
  31. PNMContext * const s = avctx->priv_data;
  32. AVFrame * const p = data;
  33. int i, j, n, linesize, h, upgrade = 0;
  34. unsigned char *ptr;
  35. int components, sample_len, ret;
  36. s->bytestream_start =
  37. s->bytestream = buf;
  38. s->bytestream_end = buf + buf_size;
  39. if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
  40. return ret;
  41. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  42. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  43. return ret;
  44. }
  45. p->pict_type = AV_PICTURE_TYPE_I;
  46. p->key_frame = 1;
  47. switch (avctx->pix_fmt) {
  48. default:
  49. return AVERROR(EINVAL);
  50. case AV_PIX_FMT_RGB48BE:
  51. n = avctx->width * 6;
  52. components=3;
  53. sample_len=16;
  54. goto do_read;
  55. case AV_PIX_FMT_RGB24:
  56. n = avctx->width * 3;
  57. components=3;
  58. sample_len=8;
  59. goto do_read;
  60. case AV_PIX_FMT_GRAY8:
  61. n = avctx->width;
  62. components=1;
  63. sample_len=8;
  64. if (s->maxval < 255)
  65. upgrade = 1;
  66. goto do_read;
  67. case AV_PIX_FMT_GRAY16BE:
  68. case AV_PIX_FMT_GRAY16LE:
  69. n = avctx->width * 2;
  70. components=1;
  71. sample_len=16;
  72. if (s->maxval < 65535)
  73. upgrade = 2;
  74. goto do_read;
  75. case AV_PIX_FMT_MONOWHITE:
  76. case AV_PIX_FMT_MONOBLACK:
  77. n = (avctx->width + 7) >> 3;
  78. components=1;
  79. sample_len=1;
  80. do_read:
  81. ptr = p->data[0];
  82. linesize = p->linesize[0];
  83. if (s->bytestream + n * avctx->height > s->bytestream_end)
  84. return AVERROR_INVALIDDATA;
  85. if(s->type < 4){
  86. for (i=0; i<avctx->height; i++) {
  87. PutBitContext pb;
  88. init_put_bits(&pb, ptr, linesize);
  89. for(j=0; j<avctx->width * components; j++){
  90. unsigned int c=0;
  91. int v=0;
  92. while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
  93. s->bytestream++;
  94. if(s->bytestream >= s->bytestream_end)
  95. return AVERROR_INVALIDDATA;
  96. do{
  97. v= 10*v + c;
  98. c= (*s->bytestream++) - '0';
  99. }while(c <= 9);
  100. put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
  101. }
  102. flush_put_bits(&pb);
  103. ptr+= linesize;
  104. }
  105. }else{
  106. for (i = 0; i < avctx->height; i++) {
  107. if (!upgrade)
  108. memcpy(ptr, s->bytestream, n);
  109. else if (upgrade == 1) {
  110. unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
  111. for (j = 0; j < n; j++)
  112. ptr[j] = (s->bytestream[j] * f + 64) >> 7;
  113. } else if (upgrade == 2) {
  114. unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  115. for (j = 0; j < n / 2; j++) {
  116. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  117. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  118. }
  119. }
  120. s->bytestream += n;
  121. ptr += linesize;
  122. }
  123. }
  124. break;
  125. case AV_PIX_FMT_YUV420P:
  126. case AV_PIX_FMT_YUV420P9BE:
  127. case AV_PIX_FMT_YUV420P10BE:
  128. {
  129. unsigned char *ptr1, *ptr2;
  130. n = avctx->width;
  131. ptr = p->data[0];
  132. linesize = p->linesize[0];
  133. if (s->maxval >= 256)
  134. n *= 2;
  135. if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
  136. return AVERROR_INVALIDDATA;
  137. for (i = 0; i < avctx->height; i++) {
  138. memcpy(ptr, s->bytestream, n);
  139. s->bytestream += n;
  140. ptr += linesize;
  141. }
  142. ptr1 = p->data[1];
  143. ptr2 = p->data[2];
  144. n >>= 1;
  145. h = avctx->height >> 1;
  146. for (i = 0; i < h; i++) {
  147. memcpy(ptr1, s->bytestream, n);
  148. s->bytestream += n;
  149. memcpy(ptr2, s->bytestream, n);
  150. s->bytestream += n;
  151. ptr1 += p->linesize[1];
  152. ptr2 += p->linesize[2];
  153. }
  154. }
  155. break;
  156. case AV_PIX_FMT_YUV420P16:
  157. {
  158. uint16_t *ptr1, *ptr2;
  159. const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  160. unsigned int j, v;
  161. n = avctx->width * 2;
  162. ptr = p->data[0];
  163. linesize = p->linesize[0];
  164. if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
  165. return AVERROR_INVALIDDATA;
  166. for (i = 0; i < avctx->height; i++) {
  167. for (j = 0; j < n / 2; j++) {
  168. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  169. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  170. }
  171. s->bytestream += n;
  172. ptr += linesize;
  173. }
  174. ptr1 = (uint16_t*)p->data[1];
  175. ptr2 = (uint16_t*)p->data[2];
  176. n >>= 1;
  177. h = avctx->height >> 1;
  178. for (i = 0; i < h; i++) {
  179. for (j = 0; j < n / 2; j++) {
  180. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  181. ptr1[j] = (v * f + 16384) >> 15;
  182. }
  183. s->bytestream += n;
  184. for (j = 0; j < n / 2; j++) {
  185. v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
  186. ptr2[j] = (v * f + 16384) >> 15;
  187. }
  188. s->bytestream += n;
  189. ptr1 += p->linesize[1] / 2;
  190. ptr2 += p->linesize[2] / 2;
  191. }
  192. }
  193. break;
  194. case AV_PIX_FMT_RGB32:
  195. ptr = p->data[0];
  196. linesize = p->linesize[0];
  197. if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
  198. return AVERROR_INVALIDDATA;
  199. for (i = 0; i < avctx->height; i++) {
  200. int j, r, g, b, a;
  201. for (j = 0; j < avctx->width; j++) {
  202. r = *s->bytestream++;
  203. g = *s->bytestream++;
  204. b = *s->bytestream++;
  205. a = *s->bytestream++;
  206. ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
  207. }
  208. ptr += linesize;
  209. }
  210. break;
  211. }
  212. *got_frame = 1;
  213. return s->bytestream - s->bytestream_start;
  214. }
  215. #if CONFIG_PGM_DECODER
  216. AVCodec ff_pgm_decoder = {
  217. .name = "pgm",
  218. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  219. .type = AVMEDIA_TYPE_VIDEO,
  220. .id = AV_CODEC_ID_PGM,
  221. .priv_data_size = sizeof(PNMContext),
  222. .decode = pnm_decode_frame,
  223. .capabilities = AV_CODEC_CAP_DR1,
  224. };
  225. #endif
  226. #if CONFIG_PGMYUV_DECODER
  227. AVCodec ff_pgmyuv_decoder = {
  228. .name = "pgmyuv",
  229. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .id = AV_CODEC_ID_PGMYUV,
  232. .priv_data_size = sizeof(PNMContext),
  233. .decode = pnm_decode_frame,
  234. .capabilities = AV_CODEC_CAP_DR1,
  235. };
  236. #endif
  237. #if CONFIG_PPM_DECODER
  238. AVCodec ff_ppm_decoder = {
  239. .name = "ppm",
  240. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  241. .type = AVMEDIA_TYPE_VIDEO,
  242. .id = AV_CODEC_ID_PPM,
  243. .priv_data_size = sizeof(PNMContext),
  244. .decode = pnm_decode_frame,
  245. .capabilities = AV_CODEC_CAP_DR1,
  246. };
  247. #endif
  248. #if CONFIG_PBM_DECODER
  249. AVCodec ff_pbm_decoder = {
  250. .name = "pbm",
  251. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  252. .type = AVMEDIA_TYPE_VIDEO,
  253. .id = AV_CODEC_ID_PBM,
  254. .priv_data_size = sizeof(PNMContext),
  255. .decode = pnm_decode_frame,
  256. .capabilities = AV_CODEC_CAP_DR1,
  257. };
  258. #endif
  259. #if CONFIG_PAM_DECODER
  260. AVCodec ff_pam_decoder = {
  261. .name = "pam",
  262. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  263. .type = AVMEDIA_TYPE_VIDEO,
  264. .id = AV_CODEC_ID_PAM,
  265. .priv_data_size = sizeof(PNMContext),
  266. .decode = pnm_decode_frame,
  267. .capabilities = AV_CODEC_CAP_DR1,
  268. };
  269. #endif