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 #include "pnm.h"
26 
27 
28 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29  const AVFrame *pict, int *got_packet)
30 {
31  PNMContext *s = avctx->priv_data;
32  AVFrame * const p = &s->picture;
33  int i, h, w, n, linesize, depth, maxval, ret;
34  const char *tuple_type;
35  uint8_t *ptr;
36 
37  if ((ret = ff_alloc_packet(pkt, avpicture_get_size(avctx->pix_fmt,
38  avctx->width,
39  avctx->height) + 200)) < 0) {
40  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
41  return ret;
42  }
43 
44  *p = *pict;
46  p->key_frame = 1;
47 
48  s->bytestream_start =
49  s->bytestream = pkt->data;
50  s->bytestream_end = pkt->data + pkt->size;
51 
52  h = avctx->height;
53  w = avctx->width;
54  switch (avctx->pix_fmt) {
56  n = (w + 7) >> 3;
57  depth = 1;
58  maxval = 1;
59  tuple_type = "BLACKANDWHITE";
60  break;
61  case AV_PIX_FMT_GRAY8:
62  n = w;
63  depth = 1;
64  maxval = 255;
65  tuple_type = "GRAYSCALE";
66  break;
67  case AV_PIX_FMT_RGB24:
68  n = w * 3;
69  depth = 3;
70  maxval = 255;
71  tuple_type = "RGB";
72  break;
73  case AV_PIX_FMT_RGB32:
74  n = w * 4;
75  depth = 4;
76  maxval = 255;
77  tuple_type = "RGB_ALPHA";
78  break;
79  default:
80  return -1;
81  }
82  snprintf(s->bytestream, s->bytestream_end - s->bytestream,
83  "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
84  w, h, depth, maxval, tuple_type);
85  s->bytestream += strlen(s->bytestream);
86 
87  ptr = p->data[0];
88  linesize = p->linesize[0];
89 
90  if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
91  int j;
92  unsigned int v;
93 
94  for (i = 0; i < h; i++) {
95  for (j = 0; j < w; j++) {
96  v = ((uint32_t *)ptr)[j];
97  bytestream_put_be24(&s->bytestream, v);
98  *s->bytestream++ = v >> 24;
99  }
100  ptr += linesize;
101  }
102  } else {
103  for (i = 0; i < h; i++) {
104  memcpy(s->bytestream, ptr, n);
105  s->bytestream += n;
106  ptr += linesize;
107  }
108  }
109 
110  pkt->size = s->bytestream - s->bytestream_start;
111  pkt->flags |= AV_PKT_FLAG_KEY;
112  *got_packet = 1;
113  return 0;
114 }
115 
116 
118  .name = "pam",
119  .type = AVMEDIA_TYPE_VIDEO,
120  .id = AV_CODEC_ID_PAM,
121  .priv_data_size = sizeof(PNMContext),
122  .init = ff_pnm_init,
123  .encode2 = pam_encode_frame,
124  .pix_fmts = (const enum AVPixelFormat[]){
127  },
128  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
129 };
struct PNMContext PNMContext
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame picture
Definition: pnm.h:31
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
uint8_t * bytestream
Definition: pnm.h:28
AVCodec.
Definition: avcodec.h:2960
uint8_t
uint8_t * data
Definition: avcodec.h:915
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
uint8_t * bytestream_end
Definition: pnm.h:30
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:880
Definition: pnm.h:27
static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: pamenc.c:28
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:74
uint8_t * bytestream_start
Definition: pnm.h:29
av_cold int ff_pnm_init(AVCodecContext *avctx)
Definition: pnm.c:191
void * priv_data
Definition: avcodec.h:1382
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:85
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
AVCodec ff_pam_encoder
Definition: pamenc.c:117