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.

154 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_RGB24:
  58. n = w * 3;
  59. depth = 3;
  60. maxval = 255;
  61. tuple_type = "RGB";
  62. break;
  63. case AV_PIX_FMT_RGBA:
  64. n = w * 4;
  65. depth = 4;
  66. maxval = 255;
  67. tuple_type = "RGB_ALPHA";
  68. break;
  69. case AV_PIX_FMT_RGB48BE:
  70. n = w * 6;
  71. depth = 3;
  72. maxval = 0xFFFF;
  73. tuple_type = "RGB";
  74. break;
  75. case AV_PIX_FMT_RGBA64BE:
  76. n = w * 8;
  77. depth = 4;
  78. maxval = 0xFFFF;
  79. tuple_type = "RGB_ALPHA";
  80. break;
  81. default:
  82. return -1;
  83. }
  84. if ((ret = ff_alloc_packet2(avctx, pkt, n*h + 200)) < 0)
  85. return ret;
  86. bytestream_start =
  87. bytestream = pkt->data;
  88. bytestream_end = pkt->data + pkt->size;
  89. snprintf(bytestream, bytestream_end - bytestream,
  90. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  91. w, h, depth, maxval, tuple_type);
  92. bytestream += strlen(bytestream);
  93. ptr = p->data[0];
  94. linesize = p->linesize[0];
  95. if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
  96. int j;
  97. for (i = 0; i < h; i++) {
  98. for (j = 0; j < w; j++)
  99. *bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
  100. ptr += linesize;
  101. }
  102. } else {
  103. for (i = 0; i < h; i++) {
  104. memcpy(bytestream, ptr, n);
  105. bytestream += n;
  106. ptr += linesize;
  107. }
  108. }
  109. pkt->size = bytestream - bytestream_start;
  110. pkt->flags |= AV_PKT_FLAG_KEY;
  111. *got_packet = 1;
  112. return 0;
  113. }
  114. static av_cold int pam_encode_init(AVCodecContext *avctx)
  115. {
  116. avctx->coded_frame = av_frame_alloc();
  117. if (!avctx->coded_frame)
  118. return AVERROR(ENOMEM);
  119. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  120. avctx->coded_frame->key_frame = 1;
  121. return 0;
  122. }
  123. static av_cold int pam_encode_close(AVCodecContext *avctx)
  124. {
  125. av_frame_free(&avctx->coded_frame);
  126. return 0;
  127. }
  128. AVCodec ff_pam_encoder = {
  129. .name = "pam",
  130. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  131. .type = AVMEDIA_TYPE_VIDEO,
  132. .id = AV_CODEC_ID_PAM,
  133. .init = pam_encode_init,
  134. .close = pam_encode_close,
  135. .encode2 = pam_encode_frame,
  136. .pix_fmts = (const enum AVPixelFormat[]){
  137. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
  138. },
  139. };