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.

130 lines
3.8KB

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