Libav
pamenc.c
Go to the documentation of this file.
1 /*
2  * PAM 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 
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "internal.h"
25 
26 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
27  const AVFrame *pict, int *got_packet)
28 {
29  uint8_t *bytestream_start, *bytestream, *bytestream_end;
30  const AVFrame * const p = pict;
31  int i, h, w, n, linesize, depth, maxval, ret;
32  const char *tuple_type;
33  uint8_t *ptr;
34 
35  if ((ret = ff_alloc_packet(pkt, avpicture_get_size(avctx->pix_fmt,
36  avctx->width,
37  avctx->height) + 200)) < 0) {
38  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
39  return ret;
40  }
41 
42  bytestream_start =
43  bytestream = pkt->data;
44  bytestream_end = pkt->data + pkt->size;
45 
46  h = avctx->height;
47  w = avctx->width;
48  switch (avctx->pix_fmt) {
50  n = (w + 7) >> 3;
51  depth = 1;
52  maxval = 1;
53  tuple_type = "BLACKANDWHITE";
54  break;
55  case AV_PIX_FMT_GRAY8:
56  n = w;
57  depth = 1;
58  maxval = 255;
59  tuple_type = "GRAYSCALE";
60  break;
61  case AV_PIX_FMT_RGB24:
62  n = w * 3;
63  depth = 3;
64  maxval = 255;
65  tuple_type = "RGB";
66  break;
67  case AV_PIX_FMT_RGB32:
68  n = w * 4;
69  depth = 4;
70  maxval = 255;
71  tuple_type = "RGB_ALPHA";
72  break;
73  default:
74  return -1;
75  }
76  snprintf(bytestream, bytestream_end - bytestream,
77  "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
78  w, h, depth, maxval, tuple_type);
79  bytestream += strlen(bytestream);
80 
81  ptr = p->data[0];
82  linesize = p->linesize[0];
83 
84  if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
85  int j;
86  unsigned int v;
87 
88  for (i = 0; i < h; i++) {
89  for (j = 0; j < w; j++) {
90  v = ((uint32_t *)ptr)[j];
91  bytestream_put_be24(&bytestream, v);
92  *bytestream++ = v >> 24;
93  }
94  ptr += linesize;
95  }
96  } else {
97  for (i = 0; i < h; i++) {
98  memcpy(bytestream, ptr, n);
99  bytestream += n;
100  ptr += linesize;
101  }
102  }
103 
104  pkt->size = bytestream - bytestream_start;
105  pkt->flags |= AV_PKT_FLAG_KEY;
106  *got_packet = 1;
107  return 0;
108 }
109 
111 {
112  avctx->coded_frame = av_frame_alloc();
113  if (!avctx->coded_frame)
114  return AVERROR(ENOMEM);
115 
117  avctx->coded_frame->key_frame = 1;
118 
119  return 0;
120 }
121 
123 {
124  av_frame_free(&avctx->coded_frame);
125  return 0;
126 }
127 
129  .name = "pam",
130  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
131  .type = AVMEDIA_TYPE_VIDEO,
132  .id = AV_CODEC_ID_PAM,
133  .init = pam_encode_init,
134  .close = pam_encode_close,
135  .encode2 = pam_encode_frame,
136  .pix_fmts = (const enum AVPixelFormat[]){
139  },
140 };