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.

165 lines
5.0KB

  1. /*
  2. * PNM 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 "pnm.h"
  23. static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
  24. int buf_size, void *data)
  25. {
  26. PNMContext *s = avctx->priv_data;
  27. AVFrame *pict = data;
  28. AVFrame * const p = (AVFrame*)&s->picture;
  29. int i, h, h1, c, n, linesize;
  30. uint8_t *ptr, *ptr1, *ptr2;
  31. if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
  32. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  33. return -1;
  34. }
  35. *p = *pict;
  36. p->pict_type = AV_PICTURE_TYPE_I;
  37. p->key_frame = 1;
  38. s->bytestream_start =
  39. s->bytestream = outbuf;
  40. s->bytestream_end = outbuf + buf_size;
  41. h = avctx->height;
  42. h1 = h;
  43. switch (avctx->pix_fmt) {
  44. case PIX_FMT_MONOWHITE:
  45. c = '4';
  46. n = (avctx->width + 7) >> 3;
  47. break;
  48. case PIX_FMT_GRAY8:
  49. c = '5';
  50. n = avctx->width;
  51. break;
  52. case PIX_FMT_GRAY16BE:
  53. c = '5';
  54. n = avctx->width * 2;
  55. break;
  56. case PIX_FMT_RGB24:
  57. c = '6';
  58. n = avctx->width * 3;
  59. break;
  60. case PIX_FMT_RGB48BE:
  61. c = '6';
  62. n = avctx->width * 6;
  63. break;
  64. case PIX_FMT_YUV420P:
  65. c = '5';
  66. n = avctx->width;
  67. h1 = (h * 3) / 2;
  68. break;
  69. default:
  70. return -1;
  71. }
  72. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  73. "P%c\n%d %d\n", c, avctx->width, h1);
  74. s->bytestream += strlen(s->bytestream);
  75. if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
  76. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  77. "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
  78. s->bytestream += strlen(s->bytestream);
  79. }
  80. ptr = p->data[0];
  81. linesize = p->linesize[0];
  82. for (i = 0; i < h; i++) {
  83. memcpy(s->bytestream, ptr, n);
  84. s->bytestream += n;
  85. ptr += linesize;
  86. }
  87. if (avctx->pix_fmt == PIX_FMT_YUV420P) {
  88. h >>= 1;
  89. n >>= 1;
  90. ptr1 = p->data[1];
  91. ptr2 = p->data[2];
  92. for (i = 0; i < h; i++) {
  93. memcpy(s->bytestream, ptr1, n);
  94. s->bytestream += n;
  95. memcpy(s->bytestream, ptr2, n);
  96. s->bytestream += n;
  97. ptr1 += p->linesize[1];
  98. ptr2 += p->linesize[2];
  99. }
  100. }
  101. return s->bytestream - s->bytestream_start;
  102. }
  103. #if CONFIG_PGM_ENCODER
  104. AVCodec ff_pgm_encoder = {
  105. .name = "pgm",
  106. .type = AVMEDIA_TYPE_VIDEO,
  107. .id = CODEC_ID_PGM,
  108. .priv_data_size = sizeof(PNMContext),
  109. .init = ff_pnm_init,
  110. .encode = pnm_encode_frame,
  111. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
  112. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  113. };
  114. #endif
  115. #if CONFIG_PGMYUV_ENCODER
  116. AVCodec ff_pgmyuv_encoder = {
  117. .name = "pgmyuv",
  118. .type = AVMEDIA_TYPE_VIDEO,
  119. .id = CODEC_ID_PGMYUV,
  120. .priv_data_size = sizeof(PNMContext),
  121. .init = ff_pnm_init,
  122. .encode = pnm_encode_frame,
  123. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  124. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  125. };
  126. #endif
  127. #if CONFIG_PPM_ENCODER
  128. AVCodec ff_ppm_encoder = {
  129. .name = "ppm",
  130. .type = AVMEDIA_TYPE_VIDEO,
  131. .id = CODEC_ID_PPM,
  132. .priv_data_size = sizeof(PNMContext),
  133. .init = ff_pnm_init,
  134. .encode = pnm_encode_frame,
  135. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
  136. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  137. };
  138. #endif
  139. #if CONFIG_PBM_ENCODER
  140. AVCodec ff_pbm_encoder = {
  141. .name = "pbm",
  142. .type = AVMEDIA_TYPE_VIDEO,
  143. .id = CODEC_ID_PBM,
  144. .priv_data_size = sizeof(PNMContext),
  145. .init = ff_pnm_init,
  146. .encode = pnm_encode_frame,
  147. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
  148. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  149. };
  150. #endif