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.

157 lines
4.4KB

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