Libav
pnmdec.c
Go to the documentation of this file.
1 /*
2  * PNM 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 "put_bits.h"
26 #include "pnm.h"
27 
28 
29 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
30  int *got_frame, AVPacket *avpkt)
31 {
32  const uint8_t *buf = avpkt->data;
33  int buf_size = avpkt->size;
34  PNMContext * const s = avctx->priv_data;
35  AVFrame * const p = data;
36  int i, j, n, linesize, h, upgrade = 0;
37  unsigned char *ptr;
38  int components, sample_len, ret;
39 
40  s->bytestream_start =
41  s->bytestream = buf;
42  s->bytestream_end = buf + buf_size;
43 
44  if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
45  return ret;
46 
47  if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
48  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
49  return ret;
50  }
52  p->key_frame = 1;
53 
54  switch (avctx->pix_fmt) {
55  default:
56  return AVERROR(EINVAL);
57  case AV_PIX_FMT_RGB48BE:
58  n = avctx->width * 6;
59  components=3;
60  sample_len=16;
61  goto do_read;
62  case AV_PIX_FMT_RGB24:
63  n = avctx->width * 3;
64  components=3;
65  sample_len=8;
66  goto do_read;
67  case AV_PIX_FMT_GRAY8:
68  n = avctx->width;
69  components=1;
70  sample_len=8;
71  if (s->maxval < 255)
72  upgrade = 1;
73  goto do_read;
76  n = avctx->width * 2;
77  components=1;
78  sample_len=16;
79  if (s->maxval < 65535)
80  upgrade = 2;
81  goto do_read;
84  n = (avctx->width + 7) >> 3;
85  components=1;
86  sample_len=1;
87  do_read:
88  ptr = p->data[0];
89  linesize = p->linesize[0];
90  if (s->bytestream + n * avctx->height > s->bytestream_end)
91  return AVERROR_INVALIDDATA;
92  if(s->type < 4){
93  for (i=0; i<avctx->height; i++) {
94  PutBitContext pb;
95  init_put_bits(&pb, ptr, linesize);
96  for(j=0; j<avctx->width * components; j++){
97  unsigned int c=0;
98  int v=0;
99  while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
100  s->bytestream++;
101  if(s->bytestream >= s->bytestream_end)
102  return AVERROR_INVALIDDATA;
103  do{
104  v= 10*v + c;
105  c= (*s->bytestream++) - '0';
106  }while(c <= 9);
107  put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
108  }
109  flush_put_bits(&pb);
110  ptr+= linesize;
111  }
112  }else{
113  for (i = 0; i < avctx->height; i++) {
114  if (!upgrade)
115  memcpy(ptr, s->bytestream, n);
116  else if (upgrade == 1) {
117  unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
118  for (j = 0; j < n; j++)
119  ptr[j] = (s->bytestream[j] * f + 64) >> 7;
120  } else if (upgrade == 2) {
121  unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
122  for (j = 0; j < n / 2; j++) {
123  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
124  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
125  }
126  }
127  s->bytestream += n;
128  ptr += linesize;
129  }
130  }
131  break;
132  case AV_PIX_FMT_YUV420P:
135  {
136  unsigned char *ptr1, *ptr2;
137 
138  n = avctx->width;
139  ptr = p->data[0];
140  linesize = p->linesize[0];
141  if (s->maxval >= 256)
142  n *= 2;
143  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
144  return AVERROR_INVALIDDATA;
145  for (i = 0; i < avctx->height; i++) {
146  memcpy(ptr, s->bytestream, n);
147  s->bytestream += n;
148  ptr += linesize;
149  }
150  ptr1 = p->data[1];
151  ptr2 = p->data[2];
152  n >>= 1;
153  h = avctx->height >> 1;
154  for (i = 0; i < h; i++) {
155  memcpy(ptr1, s->bytestream, n);
156  s->bytestream += n;
157  memcpy(ptr2, s->bytestream, n);
158  s->bytestream += n;
159  ptr1 += p->linesize[1];
160  ptr2 += p->linesize[2];
161  }
162  }
163  break;
165  {
166  uint16_t *ptr1, *ptr2;
167  const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
168  unsigned int j, v;
169 
170  n = avctx->width * 2;
171  ptr = p->data[0];
172  linesize = p->linesize[0];
173  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
174  return AVERROR_INVALIDDATA;
175  for (i = 0; i < avctx->height; i++) {
176  for (j = 0; j < n / 2; j++) {
177  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
178  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
179  }
180  s->bytestream += n;
181  ptr += linesize;
182  }
183  ptr1 = (uint16_t*)p->data[1];
184  ptr2 = (uint16_t*)p->data[2];
185  n >>= 1;
186  h = avctx->height >> 1;
187  for (i = 0; i < h; i++) {
188  for (j = 0; j < n / 2; j++) {
189  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
190  ptr1[j] = (v * f + 16384) >> 15;
191  }
192  s->bytestream += n;
193 
194  for (j = 0; j < n / 2; j++) {
195  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
196  ptr2[j] = (v * f + 16384) >> 15;
197  }
198  s->bytestream += n;
199 
200  ptr1 += p->linesize[1] / 2;
201  ptr2 += p->linesize[2] / 2;
202  }
203  }
204  break;
205  case AV_PIX_FMT_RGB32:
206  ptr = p->data[0];
207  linesize = p->linesize[0];
208  if (s->bytestream + avctx->width * avctx->height * 4 > s->bytestream_end)
209  return AVERROR_INVALIDDATA;
210  for (i = 0; i < avctx->height; i++) {
211  int j, r, g, b, a;
212 
213  for (j = 0; j < avctx->width; j++) {
214  r = *s->bytestream++;
215  g = *s->bytestream++;
216  b = *s->bytestream++;
217  a = *s->bytestream++;
218  ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
219  }
220  ptr += linesize;
221  }
222  break;
223  }
224  *got_frame = 1;
225 
226  return s->bytestream - s->bytestream_start;
227 }
228 
229 
230 #if CONFIG_PGM_DECODER
231 AVCodec ff_pgm_decoder = {
232  .name = "pgm",
233  .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
234  .type = AVMEDIA_TYPE_VIDEO,
235  .id = AV_CODEC_ID_PGM,
236  .priv_data_size = sizeof(PNMContext),
238  .capabilities = CODEC_CAP_DR1,
239 };
240 #endif
241 
242 #if CONFIG_PGMYUV_DECODER
243 AVCodec ff_pgmyuv_decoder = {
244  .name = "pgmyuv",
245  .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
246  .type = AVMEDIA_TYPE_VIDEO,
247  .id = AV_CODEC_ID_PGMYUV,
248  .priv_data_size = sizeof(PNMContext),
250  .capabilities = CODEC_CAP_DR1,
251 };
252 #endif
253 
254 #if CONFIG_PPM_DECODER
255 AVCodec ff_ppm_decoder = {
256  .name = "ppm",
257  .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
258  .type = AVMEDIA_TYPE_VIDEO,
259  .id = AV_CODEC_ID_PPM,
260  .priv_data_size = sizeof(PNMContext),
262  .capabilities = CODEC_CAP_DR1,
263 };
264 #endif
265 
266 #if CONFIG_PBM_DECODER
267 AVCodec ff_pbm_decoder = {
268  .name = "pbm",
269  .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
270  .type = AVMEDIA_TYPE_VIDEO,
271  .id = AV_CODEC_ID_PBM,
272  .priv_data_size = sizeof(PNMContext),
274  .capabilities = CODEC_CAP_DR1,
275 };
276 #endif
277 
278 #if CONFIG_PAM_DECODER
279 AVCodec ff_pam_decoder = {
280  .name = "pam",
281  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
282  .type = AVMEDIA_TYPE_VIDEO,
283  .id = AV_CODEC_ID_PAM,
284  .priv_data_size = sizeof(PNMContext),
286  .capabilities = CODEC_CAP_DR1,
287 };
288 #endif
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int maxval
maximum value of a pixel
Definition: pnm.h:31
int size
Definition: avcodec.h:974
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
uint8_t * bytestream
Definition: pnm.h:28
AVCodec.
Definition: avcodec.h:2796
Y , 16bpp, big-endian.
Definition: pixfmt.h:100
planar YUV 4:2:0, 15bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:152
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
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:112
uint8_t
#define b
Definition: input.c:52
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
planar YUV 4:2:0, 13.5bpp, (1 Cr & Cb sample per 2x2 Y samples), big-endian
Definition: pixfmt.h:150
#define r
Definition: input.c:51
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
g
Definition: yuv2rgb.c:535
uint8_t * bytestream_end
Definition: pnm.h:30
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
Libavcodec external API header.
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
int width
picture width / height.
Definition: avcodec.h:1224
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
if(ac->has_optimized_func)
Definition: pnm.h:27
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext *const s)
Definition: pnm.c:60
main external API structure.
Definition: avcodec.h:1050
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
uint8_t * data
Definition: avcodec.h:973
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:74
void * priv_data
Definition: avcodec.h:1092
#define av_be2ne16(x)
Definition: bswap.h:94
static int pnm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: pnmdec.c:29
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:222
uint8_t * bytestream_start
Definition: pnm.h:29
Y , 16bpp, little-endian.
Definition: pixfmt.h:101
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:246
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:75
Y , 8bpp.
Definition: pixfmt.h:73
This structure stores compressed data.
Definition: avcodec.h:950
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
for(j=16;j >0;--j)
int type
Definition: pnm.h:32
bitstream writer API