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.

171 lines
5.2KB

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