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.

141 lines
4.1KB

  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 "avcodec.h"
  22. #include "bytestream.h"
  23. #include "internal.h"
  24. static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  25. const AVFrame *pict, int *got_packet)
  26. {
  27. uint8_t *bytestream_start, *bytestream, *bytestream_end;
  28. const AVFrame * const p = pict;
  29. int i, h, w, n, linesize, depth, maxval, ret;
  30. const char *tuple_type;
  31. uint8_t *ptr;
  32. if ((ret = ff_alloc_packet(pkt, avpicture_get_size(avctx->pix_fmt,
  33. avctx->width,
  34. avctx->height) + 200)) < 0) {
  35. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  36. return ret;
  37. }
  38. bytestream_start =
  39. bytestream = pkt->data;
  40. bytestream_end = pkt->data + pkt->size;
  41. h = avctx->height;
  42. w = avctx->width;
  43. switch (avctx->pix_fmt) {
  44. case AV_PIX_FMT_MONOWHITE:
  45. n = (w + 7) >> 3;
  46. depth = 1;
  47. maxval = 1;
  48. tuple_type = "BLACKANDWHITE";
  49. break;
  50. case AV_PIX_FMT_GRAY8:
  51. n = w;
  52. depth = 1;
  53. maxval = 255;
  54. tuple_type = "GRAYSCALE";
  55. break;
  56. case AV_PIX_FMT_RGB24:
  57. n = w * 3;
  58. depth = 3;
  59. maxval = 255;
  60. tuple_type = "RGB";
  61. break;
  62. case AV_PIX_FMT_RGB32:
  63. n = w * 4;
  64. depth = 4;
  65. maxval = 255;
  66. tuple_type = "RGB_ALPHA";
  67. break;
  68. default:
  69. return -1;
  70. }
  71. snprintf(bytestream, bytestream_end - bytestream,
  72. "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
  73. w, h, depth, maxval, tuple_type);
  74. bytestream += strlen(bytestream);
  75. ptr = p->data[0];
  76. linesize = p->linesize[0];
  77. if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  78. int j;
  79. unsigned int v;
  80. for (i = 0; i < h; i++) {
  81. for (j = 0; j < w; j++) {
  82. v = ((uint32_t *)ptr)[j];
  83. bytestream_put_be24(&bytestream, v);
  84. *bytestream++ = v >> 24;
  85. }
  86. ptr += linesize;
  87. }
  88. } else {
  89. for (i = 0; i < h; i++) {
  90. memcpy(bytestream, ptr, n);
  91. bytestream += n;
  92. ptr += linesize;
  93. }
  94. }
  95. pkt->size = bytestream - bytestream_start;
  96. pkt->flags |= AV_PKT_FLAG_KEY;
  97. *got_packet = 1;
  98. return 0;
  99. }
  100. static av_cold int pam_encode_init(AVCodecContext *avctx)
  101. {
  102. avctx->coded_frame = av_frame_alloc();
  103. if (!avctx->coded_frame)
  104. return AVERROR(ENOMEM);
  105. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  106. avctx->coded_frame->key_frame = 1;
  107. return 0;
  108. }
  109. static av_cold int pam_encode_close(AVCodecContext *avctx)
  110. {
  111. av_frame_free(&avctx->coded_frame);
  112. return 0;
  113. }
  114. AVCodec ff_pam_encoder = {
  115. .name = "pam",
  116. .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
  117. .type = AVMEDIA_TYPE_VIDEO,
  118. .id = AV_CODEC_ID_PAM,
  119. .init = pam_encode_init,
  120. .close = pam_encode_close,
  121. .encode2 = pam_encode_frame,
  122. .pix_fmts = (const enum AVPixelFormat[]){
  123. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_GRAY8, AV_PIX_FMT_MONOWHITE,
  124. AV_PIX_FMT_NONE
  125. },
  126. };