pnmdec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 "config_components.h"
  22. #include "avcodec.h"
  23. #include "codec_internal.h"
  24. #include "internal.h"
  25. #include "put_bits.h"
  26. #include "pnm.h"
  27. #include "half2float.h"
  28. static void samplecpy(uint8_t *dst, const uint8_t *src, int n, int maxval)
  29. {
  30. if (maxval <= 255) {
  31. memcpy(dst, src, n);
  32. } else {
  33. int i;
  34. for (i=0; i<n/2; i++) {
  35. ((uint16_t *)dst)[i] = AV_RB16(src+2*i);
  36. }
  37. }
  38. }
  39. static int pnm_decode_frame(AVCodecContext *avctx, AVFrame *p,
  40. int *got_frame, AVPacket *avpkt)
  41. {
  42. const uint8_t *buf = avpkt->data;
  43. int buf_size = avpkt->size;
  44. PNMContext * const s = avctx->priv_data;
  45. int i, j, k, n, linesize, h, upgrade = 0, is_mono = 0;
  46. unsigned char *ptr;
  47. int components, sample_len, ret;
  48. float scale;
  49. s->bytestream_start =
  50. s->bytestream = (uint8_t *)buf;
  51. s->bytestream_end = (uint8_t *)buf + buf_size;
  52. if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
  53. return ret;
  54. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  55. return ret;
  56. p->pict_type = AV_PICTURE_TYPE_I;
  57. p->key_frame = 1;
  58. avctx->bits_per_raw_sample = av_log2(s->maxval) + 1;
  59. switch (avctx->pix_fmt) {
  60. default:
  61. return AVERROR(EINVAL);
  62. case AV_PIX_FMT_RGBA64:
  63. n = avctx->width * 8;
  64. components=4;
  65. sample_len=16;
  66. if (s->maxval < 65535)
  67. upgrade = 2;
  68. goto do_read;
  69. case AV_PIX_FMT_RGB48:
  70. n = avctx->width * 6;
  71. components=3;
  72. sample_len=16;
  73. if (s->maxval < 65535)
  74. upgrade = 2;
  75. goto do_read;
  76. case AV_PIX_FMT_RGBA:
  77. n = avctx->width * 4;
  78. components=4;
  79. sample_len=8;
  80. goto do_read;
  81. case AV_PIX_FMT_RGB24:
  82. n = avctx->width * 3;
  83. components=3;
  84. sample_len=8;
  85. if (s->maxval < 255)
  86. upgrade = 1;
  87. goto do_read;
  88. case AV_PIX_FMT_GRAY8:
  89. n = avctx->width;
  90. components=1;
  91. sample_len=8;
  92. if (s->maxval < 255)
  93. upgrade = 1;
  94. goto do_read;
  95. case AV_PIX_FMT_GRAY8A:
  96. n = avctx->width * 2;
  97. components=2;
  98. sample_len=8;
  99. goto do_read;
  100. case AV_PIX_FMT_GRAY16:
  101. n = avctx->width * 2;
  102. components=1;
  103. sample_len=16;
  104. if (s->maxval < 65535)
  105. upgrade = 2;
  106. goto do_read;
  107. case AV_PIX_FMT_YA16:
  108. n = avctx->width * 4;
  109. components=2;
  110. sample_len=16;
  111. if (s->maxval < 65535)
  112. upgrade = 2;
  113. goto do_read;
  114. case AV_PIX_FMT_MONOWHITE:
  115. case AV_PIX_FMT_MONOBLACK:
  116. n = (avctx->width + 7) >> 3;
  117. components=1;
  118. sample_len=1;
  119. is_mono = 1;
  120. do_read:
  121. ptr = p->data[0];
  122. linesize = p->linesize[0];
  123. if (n * avctx->height > s->bytestream_end - s->bytestream)
  124. return AVERROR_INVALIDDATA;
  125. if(s->type < 4 || (is_mono && s->type==7)){
  126. for (i=0; i<avctx->height; i++) {
  127. PutBitContext pb;
  128. init_put_bits(&pb, ptr, linesize);
  129. for(j=0; j<avctx->width * components; j++){
  130. unsigned int c=0;
  131. unsigned v=0;
  132. if(s->type < 4)
  133. while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
  134. s->bytestream++;
  135. if(s->bytestream >= s->bytestream_end)
  136. return AVERROR_INVALIDDATA;
  137. if (is_mono) {
  138. /* read a single digit */
  139. v = (*s->bytestream++)&1;
  140. } else {
  141. /* read a sequence of digits */
  142. for (k = 0; k < 6 && c <= 9; k += 1) {
  143. v = 10*v + c;
  144. c = (*s->bytestream++) - '0';
  145. }
  146. if (v > s->maxval) {
  147. av_log(avctx, AV_LOG_ERROR, "value %d larger than maxval %d\n", v, s->maxval);
  148. return AVERROR_INVALIDDATA;
  149. }
  150. }
  151. if (sample_len == 16) {
  152. ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
  153. } else
  154. put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
  155. }
  156. if (sample_len != 16)
  157. flush_put_bits(&pb);
  158. ptr+= linesize;
  159. }
  160. }else{
  161. for (i = 0; i < avctx->height; i++) {
  162. if (!upgrade)
  163. samplecpy(ptr, s->bytestream, n, s->maxval);
  164. else if (upgrade == 1) {
  165. unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
  166. for (j = 0; j < n; j++)
  167. ptr[j] = (s->bytestream[j] * f + 64) >> 7;
  168. } else if (upgrade == 2) {
  169. unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  170. for (j = 0; j < n / 2; j++) {
  171. v = AV_RB16(s->bytestream + 2*j);
  172. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  173. }
  174. }
  175. s->bytestream += n;
  176. ptr += linesize;
  177. }
  178. }
  179. break;
  180. case AV_PIX_FMT_YUV420P:
  181. case AV_PIX_FMT_YUV420P9:
  182. case AV_PIX_FMT_YUV420P10:
  183. {
  184. unsigned char *ptr1, *ptr2;
  185. n = avctx->width;
  186. ptr = p->data[0];
  187. linesize = p->linesize[0];
  188. if (s->maxval >= 256)
  189. n *= 2;
  190. if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
  191. return AVERROR_INVALIDDATA;
  192. for (i = 0; i < avctx->height; i++) {
  193. samplecpy(ptr, s->bytestream, n, s->maxval);
  194. s->bytestream += n;
  195. ptr += linesize;
  196. }
  197. ptr1 = p->data[1];
  198. ptr2 = p->data[2];
  199. n >>= 1;
  200. h = avctx->height >> 1;
  201. for (i = 0; i < h; i++) {
  202. samplecpy(ptr1, s->bytestream, n, s->maxval);
  203. s->bytestream += n;
  204. samplecpy(ptr2, s->bytestream, n, s->maxval);
  205. s->bytestream += n;
  206. ptr1 += p->linesize[1];
  207. ptr2 += p->linesize[2];
  208. }
  209. }
  210. break;
  211. case AV_PIX_FMT_YUV420P16:
  212. {
  213. uint16_t *ptr1, *ptr2;
  214. const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
  215. unsigned int j, v;
  216. n = avctx->width * 2;
  217. ptr = p->data[0];
  218. linesize = p->linesize[0];
  219. if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
  220. return AVERROR_INVALIDDATA;
  221. for (i = 0; i < avctx->height; i++) {
  222. for (j = 0; j < n / 2; j++) {
  223. v = AV_RB16(s->bytestream + 2*j);
  224. ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
  225. }
  226. s->bytestream += n;
  227. ptr += linesize;
  228. }
  229. ptr1 = (uint16_t*)p->data[1];
  230. ptr2 = (uint16_t*)p->data[2];
  231. n >>= 1;
  232. h = avctx->height >> 1;
  233. for (i = 0; i < h; i++) {
  234. for (j = 0; j < n / 2; j++) {
  235. v = AV_RB16(s->bytestream + 2*j);
  236. ptr1[j] = (v * f + 16384) >> 15;
  237. }
  238. s->bytestream += n;
  239. for (j = 0; j < n / 2; j++) {
  240. v = AV_RB16(s->bytestream + 2*j);
  241. ptr2[j] = (v * f + 16384) >> 15;
  242. }
  243. s->bytestream += n;
  244. ptr1 += p->linesize[1] / 2;
  245. ptr2 += p->linesize[2] / 2;
  246. }
  247. }
  248. break;
  249. case AV_PIX_FMT_GBRPF32:
  250. if (!s->half) {
  251. if (avctx->width * avctx->height * 12 > s->bytestream_end - s->bytestream)
  252. return AVERROR_INVALIDDATA;
  253. scale = 1.f / s->scale;
  254. if (s->endian) {
  255. float *r, *g, *b;
  256. r = (float *)p->data[2];
  257. g = (float *)p->data[0];
  258. b = (float *)p->data[1];
  259. for (int i = 0; i < avctx->height; i++) {
  260. for (int j = 0; j < avctx->width; j++) {
  261. r[j] = av_int2float(AV_RL32(s->bytestream+0)) * scale;
  262. g[j] = av_int2float(AV_RL32(s->bytestream+4)) * scale;
  263. b[j] = av_int2float(AV_RL32(s->bytestream+8)) * scale;
  264. s->bytestream += 12;
  265. }
  266. r += p->linesize[2] / 4;
  267. g += p->linesize[0] / 4;
  268. b += p->linesize[1] / 4;
  269. }
  270. } else {
  271. float *r, *g, *b;
  272. r = (float *)p->data[2];
  273. g = (float *)p->data[0];
  274. b = (float *)p->data[1];
  275. for (int i = 0; i < avctx->height; i++) {
  276. for (int j = 0; j < avctx->width; j++) {
  277. r[j] = av_int2float(AV_RB32(s->bytestream+0)) * scale;
  278. g[j] = av_int2float(AV_RB32(s->bytestream+4)) * scale;
  279. b[j] = av_int2float(AV_RB32(s->bytestream+8)) * scale;
  280. s->bytestream += 12;
  281. }
  282. r += p->linesize[2] / 4;
  283. g += p->linesize[0] / 4;
  284. b += p->linesize[1] / 4;
  285. }
  286. }
  287. } else {
  288. if (avctx->width * avctx->height * 6 > s->bytestream_end - s->bytestream)
  289. return AVERROR_INVALIDDATA;
  290. scale = 1.f / s->scale;
  291. if (s->endian) {
  292. float *r, *g, *b;
  293. r = (float *)p->data[2];
  294. g = (float *)p->data[0];
  295. b = (float *)p->data[1];
  296. for (int i = 0; i < avctx->height; i++) {
  297. for (int j = 0; j < avctx->width; j++) {
  298. r[j] = av_int2float(half2float(AV_RL16(s->bytestream+0),
  299. s->mantissatable,
  300. s->exponenttable,
  301. s->offsettable)) * scale;
  302. g[j] = av_int2float(half2float(AV_RL16(s->bytestream+2),
  303. s->mantissatable,
  304. s->exponenttable,
  305. s->offsettable)) * scale;
  306. b[j] = av_int2float(half2float(AV_RL16(s->bytestream+4),
  307. s->mantissatable,
  308. s->exponenttable,
  309. s->offsettable)) * scale;
  310. s->bytestream += 6;
  311. }
  312. r += p->linesize[2] / 4;
  313. g += p->linesize[0] / 4;
  314. b += p->linesize[1] / 4;
  315. }
  316. } else {
  317. float *r, *g, *b;
  318. r = (float *)p->data[2];
  319. g = (float *)p->data[0];
  320. b = (float *)p->data[1];
  321. for (int i = 0; i < avctx->height; i++) {
  322. for (int j = 0; j < avctx->width; j++) {
  323. r[j] = av_int2float(half2float(AV_RB16(s->bytestream+0),
  324. s->mantissatable,
  325. s->exponenttable,
  326. s->offsettable)) * scale;
  327. g[j] = av_int2float(half2float(AV_RB16(s->bytestream+2),
  328. s->mantissatable,
  329. s->exponenttable,
  330. s->offsettable)) * scale;
  331. b[j] = av_int2float(half2float(AV_RB16(s->bytestream+4),
  332. s->mantissatable,
  333. s->exponenttable,
  334. s->offsettable)) * scale;
  335. s->bytestream += 6;
  336. }
  337. r += p->linesize[2] / 4;
  338. g += p->linesize[0] / 4;
  339. b += p->linesize[1] / 4;
  340. }
  341. } }
  342. break;
  343. case AV_PIX_FMT_GRAYF32:
  344. if (!s->half) {
  345. if (avctx->width * avctx->height * 4 > s->bytestream_end - s->bytestream)
  346. return AVERROR_INVALIDDATA;
  347. scale = 1.f / s->scale;
  348. if (s->endian) {
  349. float *g = (float *)p->data[0];
  350. for (int i = 0; i < avctx->height; i++) {
  351. for (int j = 0; j < avctx->width; j++) {
  352. g[j] = av_int2float(AV_RL32(s->bytestream)) * scale;
  353. s->bytestream += 4;
  354. }
  355. g += p->linesize[0] / 4;
  356. }
  357. } else {
  358. float *g = (float *)p->data[0];
  359. for (int i = 0; i < avctx->height; i++) {
  360. for (int j = 0; j < avctx->width; j++) {
  361. g[j] = av_int2float(AV_RB32(s->bytestream)) * scale;
  362. s->bytestream += 4;
  363. }
  364. g += p->linesize[0] / 4;
  365. }
  366. }
  367. } else {
  368. if (avctx->width * avctx->height * 2 > s->bytestream_end - s->bytestream)
  369. return AVERROR_INVALIDDATA;
  370. scale = 1.f / s->scale;
  371. if (s->endian) {
  372. float *g = (float *)p->data[0];
  373. for (int i = 0; i < avctx->height; i++) {
  374. for (int j = 0; j < avctx->width; j++) {
  375. g[j] = av_int2float(half2float(AV_RL16(s->bytestream),
  376. s->mantissatable,
  377. s->exponenttable,
  378. s->offsettable)) * scale;
  379. s->bytestream += 2;
  380. }
  381. g += p->linesize[0] / 4;
  382. }
  383. } else {
  384. float *g = (float *)p->data[0];
  385. for (int i = 0; i < avctx->height; i++) {
  386. for (int j = 0; j < avctx->width; j++) {
  387. g[j] = av_int2float(half2float(AV_RB16(s->bytestream),
  388. s->mantissatable,
  389. s->exponenttable,
  390. s->offsettable)) * scale;
  391. s->bytestream += 2;
  392. }
  393. g += p->linesize[0] / 4;
  394. }
  395. }
  396. }
  397. break;
  398. }
  399. *got_frame = 1;
  400. return s->bytestream - s->bytestream_start;
  401. }
  402. #if CONFIG_PGM_DECODER
  403. const FFCodec ff_pgm_decoder = {
  404. .p.name = "pgm",
  405. .p.long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  406. .p.type = AVMEDIA_TYPE_VIDEO,
  407. .p.id = AV_CODEC_ID_PGM,
  408. .p.capabilities = AV_CODEC_CAP_DR1,
  409. .priv_data_size = sizeof(PNMContext),
  410. FF_CODEC_DECODE_CB(pnm_decode_frame),
  411. };
  412. #endif
  413. #if CONFIG_PGMYUV_DECODER
  414. const FFCodec ff_pgmyuv_decoder = {
  415. .p.name = "pgmyuv",
  416. .p.long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  417. .p.type = AVMEDIA_TYPE_VIDEO,
  418. .p.id = AV_CODEC_ID_PGMYUV,
  419. .p.capabilities = AV_CODEC_CAP_DR1,
  420. .priv_data_size = sizeof(PNMContext),
  421. FF_CODEC_DECODE_CB(pnm_decode_frame),
  422. };
  423. #endif
  424. #if CONFIG_PPM_DECODER
  425. const FFCodec ff_ppm_decoder = {
  426. .p.name = "ppm",
  427. .p.long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  428. .p.type = AVMEDIA_TYPE_VIDEO,
  429. .p.id = AV_CODEC_ID_PPM,
  430. .p.capabilities = AV_CODEC_CAP_DR1,
  431. .priv_data_size = sizeof(PNMContext),
  432. FF_CODEC_DECODE_CB(pnm_decode_frame),
  433. };
  434. #endif
  435. #if CONFIG_PBM_DECODER
  436. const FFCodec ff_pbm_decoder = {
  437. .p.name = "pbm",
  438. .p.long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  439. .p.type = AVMEDIA_TYPE_VIDEO,
  440. .p.id = AV_CODEC_ID_PBM,
  441. .p.capabilities = AV_CODEC_CAP_DR1,
  442. .priv_data_size = sizeof(PNMContext),
  443. FF_CODEC_DECODE_CB(pnm_decode_frame),
  444. };
  445. #endif
  446. #if CONFIG_PAM_DECODER
  447. const FFCodec ff_pam_decoder = {
  448. .p.name = "pam",
  449. .p.long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  450. .p.type = AVMEDIA_TYPE_VIDEO,
  451. .p.id = AV_CODEC_ID_PAM,
  452. .p.capabilities = AV_CODEC_CAP_DR1,
  453. .priv_data_size = sizeof(PNMContext),
  454. FF_CODEC_DECODE_CB(pnm_decode_frame),
  455. };
  456. #endif
  457. #if CONFIG_PFM_DECODER
  458. const FFCodec ff_pfm_decoder = {
  459. .p.name = "pfm",
  460. .p.long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
  461. .p.type = AVMEDIA_TYPE_VIDEO,
  462. .p.id = AV_CODEC_ID_PFM,
  463. .p.capabilities = AV_CODEC_CAP_DR1,
  464. .priv_data_size = sizeof(PNMContext),
  465. FF_CODEC_DECODE_CB(pnm_decode_frame),
  466. };
  467. #endif
  468. #if CONFIG_PHM_DECODER
  469. static av_cold int phm_dec_init(AVCodecContext *avctx)
  470. {
  471. PNMContext *s = avctx->priv_data;
  472. half2float_table(s->mantissatable, s->exponenttable, s->offsettable);
  473. return 0;
  474. }
  475. const FFCodec ff_phm_decoder = {
  476. .p.name = "phm",
  477. .p.long_name = NULL_IF_CONFIG_SMALL("PHM (Portable HalfFloatMap) image"),
  478. .p.type = AVMEDIA_TYPE_VIDEO,
  479. .p.id = AV_CODEC_ID_PHM,
  480. .p.capabilities = AV_CODEC_CAP_DR1,
  481. .priv_data_size = sizeof(PNMContext),
  482. .init = phm_dec_init,
  483. FF_CODEC_DECODE_CB(pnm_decode_frame),
  484. };
  485. #endif