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.

199 lines
6.1KB

  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 "libavutil/pixdesc.h"
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  25. const AVFrame *p, int *got_packet)
  26. {
  27. uint8_t *bytestream, *bytestream_start, *bytestream_end;
  28. int i, h, h1, c, n, linesize, ret;
  29. uint8_t *ptr, *ptr1, *ptr2;
  30. if ((ret = ff_alloc_packet2(avctx, pkt, avpicture_get_size(avctx->pix_fmt,
  31. avctx->width,
  32. avctx->height) + 200)) < 0)
  33. return ret;
  34. bytestream_start =
  35. bytestream = pkt->data;
  36. bytestream_end = pkt->data + pkt->size;
  37. h = avctx->height;
  38. h1 = h;
  39. switch (avctx->pix_fmt) {
  40. case AV_PIX_FMT_MONOWHITE:
  41. c = '4';
  42. n = (avctx->width + 7) >> 3;
  43. break;
  44. case AV_PIX_FMT_GRAY8:
  45. c = '5';
  46. n = avctx->width;
  47. break;
  48. case AV_PIX_FMT_GRAY16BE:
  49. c = '5';
  50. n = avctx->width * 2;
  51. break;
  52. case AV_PIX_FMT_RGB24:
  53. c = '6';
  54. n = avctx->width * 3;
  55. break;
  56. case AV_PIX_FMT_RGB48BE:
  57. c = '6';
  58. n = avctx->width * 6;
  59. break;
  60. case AV_PIX_FMT_YUV420P:
  61. if (avctx->width & 1 || avctx->height & 1) {
  62. av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width and height\n");
  63. return AVERROR(EINVAL);
  64. }
  65. c = '5';
  66. n = avctx->width;
  67. h1 = (h * 3) / 2;
  68. break;
  69. case AV_PIX_FMT_YUV420P16BE:
  70. c = '5';
  71. n = avctx->width * 2;
  72. h1 = (h * 3) / 2;
  73. break;
  74. default:
  75. return -1;
  76. }
  77. snprintf(bytestream, bytestream_end - bytestream,
  78. "P%c\n%d %d\n", c, avctx->width, h1);
  79. bytestream += strlen(bytestream);
  80. if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE) {
  81. int maxdepth = (1 << (av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1)) - 1;
  82. if( avctx->bits_per_raw_sample )
  83. maxdepth = (1 << avctx->bits_per_raw_sample) - 1;
  84. snprintf(bytestream, bytestream_end - bytestream,
  85. "%d\n", maxdepth);
  86. bytestream += strlen(bytestream);
  87. }
  88. ptr = p->data[0];
  89. linesize = p->linesize[0];
  90. for (i = 0; i < h; i++) {
  91. memcpy(bytestream, ptr, n);
  92. bytestream += n;
  93. ptr += linesize;
  94. }
  95. if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
  96. h >>= 1;
  97. n >>= 1;
  98. ptr1 = p->data[1];
  99. ptr2 = p->data[2];
  100. for (i = 0; i < h; i++) {
  101. memcpy(bytestream, ptr1, n);
  102. bytestream += n;
  103. memcpy(bytestream, ptr2, n);
  104. bytestream += n;
  105. ptr1 += p->linesize[1];
  106. ptr2 += p->linesize[2];
  107. }
  108. }
  109. pkt->size = bytestream - bytestream_start;
  110. pkt->flags |= AV_PKT_FLAG_KEY;
  111. *got_packet = 1;
  112. return 0;
  113. }
  114. static av_cold int pnm_encode_init(AVCodecContext *avctx)
  115. {
  116. avctx->coded_frame = av_frame_alloc();
  117. if (!avctx->coded_frame)
  118. return AVERROR(ENOMEM);
  119. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  120. avctx->coded_frame->key_frame = 1;
  121. return 0;
  122. }
  123. static av_cold int pnm_encode_close(AVCodecContext *avctx)
  124. {
  125. av_frame_free(&avctx->coded_frame);
  126. return 0;
  127. }
  128. #if CONFIG_PGM_ENCODER
  129. AVCodec ff_pgm_encoder = {
  130. .name = "pgm",
  131. .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
  132. .type = AVMEDIA_TYPE_VIDEO,
  133. .id = AV_CODEC_ID_PGM,
  134. .init = pnm_encode_init,
  135. .close = pnm_encode_close,
  136. .encode2 = pnm_encode_frame,
  137. .pix_fmts = (const enum AVPixelFormat[]){
  138. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE
  139. },
  140. };
  141. #endif
  142. #if CONFIG_PGMYUV_ENCODER
  143. AVCodec ff_pgmyuv_encoder = {
  144. .name = "pgmyuv",
  145. .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
  146. .type = AVMEDIA_TYPE_VIDEO,
  147. .id = AV_CODEC_ID_PGMYUV,
  148. .init = pnm_encode_init,
  149. .close = pnm_encode_close,
  150. .encode2 = pnm_encode_frame,
  151. .pix_fmts = (const enum AVPixelFormat[]){
  152. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_NONE
  153. },
  154. };
  155. #endif
  156. #if CONFIG_PPM_ENCODER
  157. AVCodec ff_ppm_encoder = {
  158. .name = "ppm",
  159. .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .id = AV_CODEC_ID_PPM,
  162. .init = pnm_encode_init,
  163. .close = pnm_encode_close,
  164. .encode2 = pnm_encode_frame,
  165. .pix_fmts = (const enum AVPixelFormat[]){
  166. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_NONE
  167. },
  168. };
  169. #endif
  170. #if CONFIG_PBM_ENCODER
  171. AVCodec ff_pbm_encoder = {
  172. .name = "pbm",
  173. .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
  174. .type = AVMEDIA_TYPE_VIDEO,
  175. .id = AV_CODEC_ID_PBM,
  176. .init = pnm_encode_init,
  177. .close = pnm_encode_close,
  178. .encode2 = pnm_encode_frame,
  179. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
  180. AV_PIX_FMT_NONE },
  181. };
  182. #endif