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.

144 lines
4.1KB

  1. /*
  2. * PAM 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 "internal.h"
  23. #include "pnm.h"
  24. static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  25. const AVFrame *pict, int *got_packet)
  26. {
  27. PNMContext *s = avctx->priv_data;
  28. AVFrame * const p = &s->picture;
  29. int i, h, w, n, linesize, depth, maxval, ret;
  30. const char *tuple_type;
  31. uint8_t *ptr;
  32. h = avctx->height;
  33. w = avctx->width;
  34. switch (avctx->pix_fmt) {
  35. case PIX_FMT_MONOBLACK:
  36. n = w;
  37. depth = 1;
  38. maxval = 1;
  39. tuple_type = "BLACKANDWHITE";
  40. break;
  41. case PIX_FMT_GRAY8:
  42. n = w;
  43. depth = 1;
  44. maxval = 255;
  45. tuple_type = "GRAYSCALE";
  46. break;
  47. case PIX_FMT_GRAY16BE:
  48. n = w * 2;
  49. depth = 1;
  50. maxval = 0xFFFF;
  51. tuple_type = "GRAYSCALE";
  52. break;
  53. case PIX_FMT_GRAY8A:
  54. n = w * 2;
  55. depth = 2;
  56. maxval = 255;
  57. tuple_type = "GRAYSCALE_ALPHA";
  58. break;
  59. case PIX_FMT_RGB24:
  60. n = w * 3;
  61. depth = 3;
  62. maxval = 255;
  63. tuple_type = "RGB";
  64. break;
  65. case PIX_FMT_RGBA:
  66. n = w * 4;
  67. depth = 4;
  68. maxval = 255;
  69. tuple_type = "RGB_ALPHA";
  70. break;
  71. case PIX_FMT_RGB48BE:
  72. n = w * 6;
  73. depth = 3;
  74. maxval = 0xFFFF;
  75. tuple_type = "RGB";
  76. break;
  77. case PIX_FMT_RGBA64BE:
  78. n = w * 8;
  79. depth = 4;
  80. maxval = 0xFFFF;
  81. tuple_type = "RGB_ALPHA";
  82. break;
  83. default:
  84. return -1;
  85. }
  86. if ((ret = ff_alloc_packet2(avctx, pkt, n*h + 200)) < 0)
  87. return ret;
  88. *p = *pict;
  89. p->pict_type = AV_PICTURE_TYPE_I;
  90. p->key_frame = 1;
  91. s->bytestream_start =
  92. s->bytestream = pkt->data;
  93. s->bytestream_end = pkt->data + pkt->size;
  94. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  95. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  96. w, h, depth, maxval, tuple_type);
  97. s->bytestream += strlen(s->bytestream);
  98. ptr = p->data[0];
  99. linesize = p->linesize[0];
  100. if (avctx->pix_fmt == PIX_FMT_MONOBLACK){
  101. int j;
  102. for (i = 0; i < h; i++) {
  103. for (j = 0; j < w; j++)
  104. *s->bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
  105. ptr += linesize;
  106. }
  107. } else {
  108. for (i = 0; i < h; i++) {
  109. memcpy(s->bytestream, ptr, n);
  110. s->bytestream += n;
  111. ptr += linesize;
  112. }
  113. }
  114. pkt->size = s->bytestream - s->bytestream_start;
  115. pkt->flags |= AV_PKT_FLAG_KEY;
  116. *got_packet = 1;
  117. return 0;
  118. }
  119. AVCodec ff_pam_encoder = {
  120. .name = "pam",
  121. .type = AVMEDIA_TYPE_VIDEO,
  122. .id = AV_CODEC_ID_PAM,
  123. .priv_data_size = sizeof(PNMContext),
  124. .init = ff_pnm_init,
  125. .encode2 = pam_encode_frame,
  126. .pix_fmts = (const enum PixelFormat[]){
  127. PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_RGB48BE, PIX_FMT_RGBA64BE, PIX_FMT_GRAY8, PIX_FMT_GRAY8A, PIX_FMT_GRAY16BE, PIX_FMT_MONOBLACK, PIX_FMT_NONE
  128. },
  129. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  130. };