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.

177 lines
5.3KB

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