FFmpeg  2.1.1
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 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 
22 #include "avcodec.h"
23 #include "internal.h"
24 #include "pnm.h"
25 
26 
28  const AVFrame *p, int *got_packet)
29 {
30  PNMContext *s = avctx->priv_data;
31  int i, h, w, n, linesize, depth, maxval, ret;
32  const char *tuple_type;
33  uint8_t *ptr;
34 
35  h = avctx->height;
36  w = avctx->width;
37  switch (avctx->pix_fmt) {
39  n = w;
40  depth = 1;
41  maxval = 1;
42  tuple_type = "BLACKANDWHITE";
43  break;
44  case AV_PIX_FMT_GRAY8:
45  n = w;
46  depth = 1;
47  maxval = 255;
48  tuple_type = "GRAYSCALE";
49  break;
51  n = w * 2;
52  depth = 1;
53  maxval = 0xFFFF;
54  tuple_type = "GRAYSCALE";
55  break;
56  case AV_PIX_FMT_GRAY8A:
57  n = w * 2;
58  depth = 2;
59  maxval = 255;
60  tuple_type = "GRAYSCALE_ALPHA";
61  break;
62  case AV_PIX_FMT_RGB24:
63  n = w * 3;
64  depth = 3;
65  maxval = 255;
66  tuple_type = "RGB";
67  break;
68  case AV_PIX_FMT_RGBA:
69  n = w * 4;
70  depth = 4;
71  maxval = 255;
72  tuple_type = "RGB_ALPHA";
73  break;
74  case AV_PIX_FMT_RGB48BE:
75  n = w * 6;
76  depth = 3;
77  maxval = 0xFFFF;
78  tuple_type = "RGB";
79  break;
81  n = w * 8;
82  depth = 4;
83  maxval = 0xFFFF;
84  tuple_type = "RGB_ALPHA";
85  break;
86  default:
87  return -1;
88  }
89 
90  if ((ret = ff_alloc_packet2(avctx, pkt, n*h + 200)) < 0)
91  return ret;
92 
93  s->bytestream_start =
94  s->bytestream = pkt->data;
95  s->bytestream_end = pkt->data + pkt->size;
96 
98  "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
99  w, h, depth, maxval, tuple_type);
100  s->bytestream += strlen(s->bytestream);
101 
102  ptr = p->data[0];
103  linesize = p->linesize[0];
104 
105  if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
106  int j;
107  for (i = 0; i < h; i++) {
108  for (j = 0; j < w; j++)
109  *s->bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
110  ptr += linesize;
111  }
112  } else {
113  for (i = 0; i < h; i++) {
114  memcpy(s->bytestream, ptr, n);
115  s->bytestream += n;
116  ptr += linesize;
117  }
118  }
119 
120  pkt->size = s->bytestream - s->bytestream_start;
121  pkt->flags |= AV_PKT_FLAG_KEY;
122  *got_packet = 1;
123  return 0;
124 }
125 
126 
128  .name = "pam",
129  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
130  .type = AVMEDIA_TYPE_VIDEO,
131  .id = AV_CODEC_ID_PAM,
132  .priv_data_size = sizeof(PNMContext),
133  .encode2 = pam_encode_frame,
134  .pix_fmts = (const enum AVPixelFormat[]){
136  },
137 };
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1500
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: avcodec.h:4536
int size
Definition: avcodec.h:1064
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
uint8_t * bytestream
Definition: pnm.h:28
8bit gray, 8bit alpha
Definition: avcodec.h:4611
Pixel format.
Definition: avcodec.h:4533
AVCodec.
Definition: avcodec.h:2922
Y , 8bpp.
Definition: avcodec.h:4542
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: avcodec.h:4579
uint8_t
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
uint8_t * bytestream_end
Definition: pnm.h:30
Libavcodec external API header.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
int depth
Definition: v4l.c:61
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
int n
Definition: avisynth_c.h:588
Definition: pnm.h:27
main external API structure.
Definition: avcodec.h:1146
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: avcodec.h:4684
uint8_t * data
Definition: avcodec.h:1063
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
#define snprintf
Definition: snprintf.h:34
static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: pamenc.c:27
void * priv_data
Definition: avcodec.h:1182
common internal api header.
uint8_t * bytestream_start
Definition: pnm.h:29
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: avcodec.h:4544
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: avcodec.h:4563
static AVPacket pkt
Definition: demuxing.c:52
This structure stores compressed data.
Definition: avcodec.h:1040
AVCodec ff_pam_encoder
Definition: pamenc.c:127
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
Y , 16bpp, big-endian.
Definition: avcodec.h:4567