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.

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