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.

305 lines
9.8KB

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