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.

178 lines
5.4KB

  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 = &s->picture;
  29. int i, h, h1, c, n, linesize, ret;
  30. uint8_t *ptr, *ptr1, *ptr2;
  31. if ((ret = ff_alloc_packet2(avctx, pkt, avpicture_get_size(avctx->pix_fmt,
  32. avctx->width,
  33. avctx->height) + 200)) < 0)
  34. return ret;
  35. *p = *pict;
  36. p->pict_type = AV_PICTURE_TYPE_I;
  37. p->key_frame = 1;
  38. s->bytestream_start =
  39. s->bytestream = pkt->data;
  40. s->bytestream_end = pkt->data + pkt->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. if (avctx->width & 1) {
  66. av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width\n");
  67. return AVERROR(EINVAL);
  68. }
  69. c = '5';
  70. n = avctx->width;
  71. h1 = (h * 3) / 2;
  72. break;
  73. default:
  74. return -1;
  75. }
  76. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  77. "P%c\n%d %d\n", c, avctx->width, h1);
  78. s->bytestream += strlen(s->bytestream);
  79. if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
  80. snprintf(s->bytestream, s->bytestream_end - s->bytestream,
  81. "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
  82. s->bytestream += strlen(s->bytestream);
  83. }
  84. ptr = p->data[0];
  85. linesize = p->linesize[0];
  86. for (i = 0; i < h; i++) {
  87. memcpy(s->bytestream, ptr, n);
  88. s->bytestream += n;
  89. ptr += linesize;
  90. }
  91. if (avctx->pix_fmt == PIX_FMT_YUV420P) {
  92. h >>= 1;
  93. n >>= 1;
  94. ptr1 = p->data[1];
  95. ptr2 = p->data[2];
  96. for (i = 0; i < h; i++) {
  97. memcpy(s->bytestream, ptr1, n);
  98. s->bytestream += n;
  99. memcpy(s->bytestream, ptr2, n);
  100. s->bytestream += n;
  101. ptr1 += p->linesize[1];
  102. ptr2 += p->linesize[2];
  103. }
  104. }
  105. pkt->size = s->bytestream - s->bytestream_start;
  106. pkt->flags |= AV_PKT_FLAG_KEY;
  107. *got_packet = 1;
  108. return 0;
  109. }
  110. #if CONFIG_PGM_ENCODER
  111. AVCodec ff_pgm_encoder = {
  112. .name = "pgm",
  113. .type = AVMEDIA_TYPE_VIDEO,
  114. .id = AV_CODEC_ID_PGM,
  115. .priv_data_size = sizeof(PNMContext),
  116. .init = ff_pnm_init,
  117. .encode2 = pnm_encode_frame,
  118. .pix_fmts = (const enum PixelFormat[]){
  119. PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE
  120. },
  121. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  122. };
  123. #endif
  124. #if CONFIG_PGMYUV_ENCODER
  125. AVCodec ff_pgmyuv_encoder = {
  126. .name = "pgmyuv",
  127. .type = AVMEDIA_TYPE_VIDEO,
  128. .id = AV_CODEC_ID_PGMYUV,
  129. .priv_data_size = sizeof(PNMContext),
  130. .init = ff_pnm_init,
  131. .encode2 = pnm_encode_frame,
  132. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
  133. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  134. };
  135. #endif
  136. #if CONFIG_PPM_ENCODER
  137. AVCodec ff_ppm_encoder = {
  138. .name = "ppm",
  139. .type = AVMEDIA_TYPE_VIDEO,
  140. .id = AV_CODEC_ID_PPM,
  141. .priv_data_size = sizeof(PNMContext),
  142. .init = ff_pnm_init,
  143. .encode2 = pnm_encode_frame,
  144. .pix_fmts = (const enum PixelFormat[]){
  145. PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE
  146. },
  147. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  148. };
  149. #endif
  150. #if CONFIG_PBM_ENCODER
  151. AVCodec ff_pbm_encoder = {
  152. .name = "pbm",
  153. .type = AVMEDIA_TYPE_VIDEO,
  154. .id = AV_CODEC_ID_PBM,
  155. .priv_data_size = sizeof(PNMContext),
  156. .init = ff_pnm_init,
  157. .encode2 = pnm_encode_frame,
  158. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_MONOWHITE,
  159. PIX_FMT_NONE },
  160. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  161. };
  162. #endif