FFmpeg
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 "libavutil/avassert.h"
23 #include "avcodec.h"
24 #include "codec_internal.h"
25 #include "encode.h"
26 
28  const AVFrame *p, int *got_packet)
29 {
30  int i, h, w, n, linesize, depth, maxval, ret, header_size;
31  uint8_t *bytestream;
32  const uint8_t *ptr;
33  const char *tuple_type;
34  char header[100];
35 
36  h = avctx->height;
37  w = avctx->width;
38  switch (avctx->pix_fmt) {
40  n = w;
41  depth = 1;
42  maxval = 1;
43  tuple_type = "BLACKANDWHITE";
44  break;
45  case AV_PIX_FMT_GRAY8:
46  n = w;
47  depth = 1;
48  maxval = 255;
49  tuple_type = "GRAYSCALE";
50  break;
52  n = w * 2;
53  depth = 1;
54  maxval = 0xFFFF;
55  tuple_type = "GRAYSCALE";
56  break;
57  case AV_PIX_FMT_GRAY8A:
58  n = w * 2;
59  depth = 2;
60  maxval = 255;
61  tuple_type = "GRAYSCALE_ALPHA";
62  break;
63  case AV_PIX_FMT_YA16BE:
64  n = w * 4;
65  depth = 2;
66  maxval = 0xFFFF;
67  tuple_type = "GRAYSCALE_ALPHA";
68  break;
69  case AV_PIX_FMT_RGB24:
70  n = w * 3;
71  depth = 3;
72  maxval = 255;
73  tuple_type = "RGB";
74  break;
75  case AV_PIX_FMT_RGBA:
76  n = w * 4;
77  depth = 4;
78  maxval = 255;
79  tuple_type = "RGB_ALPHA";
80  break;
81  case AV_PIX_FMT_RGB48BE:
82  n = w * 6;
83  depth = 3;
84  maxval = 0xFFFF;
85  tuple_type = "RGB";
86  break;
88  n = w * 8;
89  depth = 4;
90  maxval = 0xFFFF;
91  tuple_type = "RGB_ALPHA";
92  break;
93  default:
94  return -1;
95  }
96 
97  header_size = snprintf(header, sizeof(header),
98  "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
99  w, h, depth, maxval, tuple_type);
100  av_assert1(header_size < sizeof(header));
101 
102  if ((ret = ff_get_encode_buffer(avctx, pkt, n*h + header_size, 0)) < 0)
103  return ret;
104 
105  bytestream = pkt->data;
106  memcpy(bytestream, header, header_size);
107  bytestream += header_size;
108 
109  ptr = p->data[0];
110  linesize = p->linesize[0];
111 
112  if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK){
113  int j;
114  for (i = 0; i < h; i++) {
115  for (j = 0; j < w; j++)
116  *bytestream++ = ptr[j >> 3] >> (7 - j & 7) & 1;
117  ptr += linesize;
118  }
119  } else {
120  for (i = 0; i < h; i++) {
121  memcpy(bytestream, ptr, n);
122  bytestream += n;
123  ptr += linesize;
124  }
125  }
126 
127  *got_packet = 1;
128  return 0;
129 }
130 
132  .p.name = "pam",
133  CODEC_LONG_NAME("PAM (Portable AnyMap) image"),
134  .p.type = AVMEDIA_TYPE_VIDEO,
135  .p.id = AV_CODEC_ID_PAM,
138  .p.pix_fmts = (const enum AVPixelFormat[]){
144  },
145 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AV_PIX_FMT_RGBA64BE
@ AV_PIX_FMT_RGBA64BE
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: pixfmt.h:195
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:491
encode.h
FFCodec
Definition: codec_internal.h:127
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:315
avassert.h
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
ff_pam_encoder
const FFCodec ff_pam_encoder
Definition: pamenc.c:131
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
AV_PIX_FMT_GRAY8A
@ AV_PIX_FMT_GRAY8A
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:136
AV_PIX_FMT_MONOBLACK
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:76
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AV_CODEC_ID_PAM
@ AV_CODEC_ID_PAM
Definition: codec_id.h:118
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
codec_internal.h
AV_PIX_FMT_YA16BE
@ AV_PIX_FMT_YA16BE
16 bits gray, 16 bits alpha (big-endian)
Definition: pixfmt.h:202
header
static const uint8_t header[24]
Definition: sdr2.c:67
AV_PIX_FMT_RGB48BE
@ AV_PIX_FMT_RGB48BE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:102
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:441
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:105
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
h
h
Definition: vp9dsp_template.c:2038
snprintf
#define snprintf
Definition: snprintf.h:34
pam_encode_frame
static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: pamenc.c:27