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 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2506
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
AVCodec.
Definition: avcodec.h:2755
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
uint8_t * data
Definition: avcodec.h:973
static av_cold int pam_encode_init(AVCodecContext *avctx)
Definition: pamenc.c:110
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:168
int width
picture width / height.
Definition: avcodec.h:1217
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1125
static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: pamenc.c:26
Libavcodec external API header.
static av_cold int pam_encode_close(AVCodecContext *avctx)
Definition: pamenc.c:122
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
main external API structure.
Definition: avcodec.h:1054
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:206
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
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
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: frame.h:163
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
AVCodec ff_pam_encoder
Definition: pamenc.c:128