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.

197 lines
5.9KB

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