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_packet(pkt, n*h + 200)) < 0) {
  87. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  88. return ret;
  89. }
  90. *p = *pict;
  91. p->pict_type = AV_PICTURE_TYPE_I;
  92. p->key_frame = 1;
  93. s->bytestream_start =
  94. s->bytestream = pkt->data;
  95. s->bytestream_end = pkt->data + pkt->size;
  96. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  97. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  98. w, h, depth, maxval, tuple_type);
  99. s->bytestream += strlen(s->bytestream);
  100. ptr = p->data[0];
  101. linesize = p->linesize[0];
  102. if (avctx->pix_fmt == PIX_FMT_MONOBLACK){
  103. int j;
  104. for (i = 0; i < h; i++) {
  105. for (j = 0; j < w; j++)
  106. *s->bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
  107. ptr += linesize;
  108. }
  109. } else {
  110. for (i = 0; i < h; i++) {
  111. memcpy(s->bytestream, ptr, n);
  112. s->bytestream += n;
  113. ptr += linesize;
  114. }
  115. }
  116. pkt->size = s->bytestream - s->bytestream_start;
  117. pkt->flags |= AV_PKT_FLAG_KEY;
  118. *got_packet = 1;
  119. return 0;
  120. }
  121. AVCodec ff_pam_encoder = {
  122. .name = "pam",
  123. .type = AVMEDIA_TYPE_VIDEO,
  124. .id = CODEC_ID_PAM,
  125. .priv_data_size = sizeof(PNMContext),
  126. .init = ff_pnm_init,
  127. .encode2 = pam_encode_frame,
  128. .pix_fmts = (const enum PixelFormat[]){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},
  129. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  130. };