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.

136 lines
3.9KB

  1. /*
  2. * PAM image format
  3. * Copyright (c) 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/imgutils.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  26. const AVFrame *pict, int *got_packet)
  27. {
  28. uint8_t *bytestream_start, *bytestream, *bytestream_end;
  29. const AVFrame * const p = pict;
  30. int i, h, w, n, linesize, depth, maxval, ret;
  31. const char *tuple_type;
  32. uint8_t *ptr;
  33. int size = av_image_get_buffer_size(avctx->pix_fmt,
  34. avctx->width, avctx->height, 1);
  35. if ((ret = ff_alloc_packet(pkt, size + 200)) < 0) {
  36. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  37. return ret;
  38. }
  39. bytestream_start =
  40. bytestream = pkt->data;
  41. bytestream_end = pkt->data + pkt->size;
  42. h = avctx->height;
  43. w = avctx->width;
  44. switch (avctx->pix_fmt) {
  45. case AV_PIX_FMT_MONOWHITE:
  46. n = (w + 7) >> 3;
  47. depth = 1;
  48. maxval = 1;
  49. tuple_type = "BLACKANDWHITE";
  50. break;
  51. case AV_PIX_FMT_GRAY8:
  52. n = w;
  53. depth = 1;
  54. maxval = 255;
  55. tuple_type = "GRAYSCALE";
  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_RGB32:
  64. n = w * 4;
  65. depth = 4;
  66. maxval = 255;
  67. tuple_type = "RGB_ALPHA";
  68. break;
  69. default:
  70. return -1;
  71. }
  72. snprintf(bytestream, bytestream_end - bytestream,
  73. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  74. w, h, depth, maxval, tuple_type);
  75. bytestream += strlen(bytestream);
  76. ptr = p->data[0];
  77. linesize = p->linesize[0];
  78. if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  79. int j;
  80. unsigned int v;
  81. for (i = 0; i < h; i++) {
  82. for (j = 0; j < w; j++) {
  83. v = ((uint32_t *)ptr)[j];
  84. bytestream_put_be24(&bytestream, v);
  85. *bytestream++ = v >> 24;
  86. }
  87. ptr += linesize;
  88. }
  89. } else {
  90. for (i = 0; i < h; i++) {
  91. memcpy(bytestream, ptr, n);
  92. bytestream += n;
  93. ptr += linesize;
  94. }
  95. }
  96. pkt->size = bytestream - bytestream_start;
  97. pkt->flags |= AV_PKT_FLAG_KEY;
  98. *got_packet = 1;
  99. return 0;
  100. }
  101. static av_cold int pam_encode_init(AVCodecContext *avctx)
  102. {
  103. #if FF_API_CODED_FRAME
  104. FF_DISABLE_DEPRECATION_WARNINGS
  105. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  106. avctx->coded_frame->key_frame = 1;
  107. FF_ENABLE_DEPRECATION_WARNINGS
  108. #endif
  109. return 0;
  110. }
  111. AVCodec ff_pam_encoder = {
  112. .name = "pam",
  113. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  114. .type = AVMEDIA_TYPE_VIDEO,
  115. .id = AV_CODEC_ID_PAM,
  116. .init = pam_encode_init,
  117. .encode2 = pam_encode_frame,
  118. .pix_fmts = (const enum AVPixelFormat[]){
  119. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_GRAY8, AV_PIX_FMT_MONOWHITE,
  120. AV_PIX_FMT_NONE
  121. },
  122. };