FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 "put_bits.h"
25 #include "pnm.h"
26 
27 
28 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
29  int *got_frame, AVPacket *avpkt)
30 {
31  const uint8_t *buf = avpkt->data;
32  int buf_size = avpkt->size;
33  PNMContext * const s = avctx->priv_data;
34  AVFrame * const p = data;
35  int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
36  unsigned char *ptr;
37  int components, sample_len, ret;
38 
39  s->bytestream_start =
40  s->bytestream = (uint8_t *)buf;
41  s->bytestream_end = (uint8_t *)buf + buf_size;
42 
43  if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
44  return ret;
45 
46  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
47  return ret;
49  p->key_frame = 1;
50 
51  switch (avctx->pix_fmt) {
52  default:
53  return AVERROR(EINVAL);
55  n = avctx->width * 8;
56  components=4;
57  sample_len=16;
58  goto do_read;
59  case AV_PIX_FMT_RGB48BE:
60  n = avctx->width * 6;
61  components=3;
62  sample_len=16;
63  goto do_read;
64  case AV_PIX_FMT_RGBA:
65  n = avctx->width * 4;
66  components=4;
67  sample_len=8;
68  goto do_read;
69  case AV_PIX_FMT_RGB24:
70  n = avctx->width * 3;
71  components=3;
72  sample_len=8;
73  goto do_read;
74  case AV_PIX_FMT_GRAY8:
75  n = avctx->width;
76  components=1;
77  sample_len=8;
78  if (s->maxval < 255)
79  upgrade = 1;
80  goto do_read;
81  case AV_PIX_FMT_GRAY8A:
82  n = avctx->width * 2;
83  components=2;
84  sample_len=8;
85  goto do_read;
88  n = avctx->width * 2;
89  components=1;
90  sample_len=16;
91  if (s->maxval < 65535)
92  upgrade = 2;
93  goto do_read;
96  n = (avctx->width + 7) >> 3;
97  components=1;
98  sample_len=1;
99  is_mono = 1;
100  do_read:
101  ptr = p->data[0];
102  linesize = p->linesize[0];
103  if (s->bytestream + n * avctx->height > s->bytestream_end)
104  return AVERROR_INVALIDDATA;
105  if(s->type < 4 || (is_mono && s->type==7)){
106  for (i=0; i<avctx->height; i++) {
107  PutBitContext pb;
108  init_put_bits(&pb, ptr, linesize);
109  for(j=0; j<avctx->width * components; j++){
110  unsigned int c=0;
111  int v=0;
112  if(s->type < 4)
113  while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
114  s->bytestream++;
115  if(s->bytestream >= s->bytestream_end)
116  return AVERROR_INVALIDDATA;
117  if (is_mono) {
118  /* read a single digit */
119  v = (*s->bytestream++)&1;
120  } else {
121  /* read a sequence of digits */
122  do {
123  v = 10*v + c;
124  c = (*s->bytestream++) - '0';
125  } while (c <= 9);
126  }
127  put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
128  }
129  flush_put_bits(&pb);
130  ptr+= linesize;
131  }
132  }else{
133  for (i = 0; i < avctx->height; i++) {
134  if (!upgrade)
135  memcpy(ptr, s->bytestream, n);
136  else if (upgrade == 1) {
137  unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
138  for (j = 0; j < n; j++)
139  ptr[j] = (s->bytestream[j] * f + 64) >> 7;
140  } else if (upgrade == 2) {
141  unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
142  for (j = 0; j < n / 2; j++) {
143  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
144  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
145  }
146  }
147  s->bytestream += n;
148  ptr += linesize;
149  }
150  }
151  break;
152  case AV_PIX_FMT_YUV420P:
155  {
156  unsigned char *ptr1, *ptr2;
157 
158  n = avctx->width;
159  ptr = p->data[0];
160  linesize = p->linesize[0];
161  if (s->maxval >= 256)
162  n *= 2;
163  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
164  return AVERROR_INVALIDDATA;
165  for (i = 0; i < avctx->height; i++) {
166  memcpy(ptr, s->bytestream, n);
167  s->bytestream += n;
168  ptr += linesize;
169  }
170  ptr1 = p->data[1];
171  ptr2 = p->data[2];
172  n >>= 1;
173  h = avctx->height >> 1;
174  for (i = 0; i < h; i++) {
175  memcpy(ptr1, s->bytestream, n);
176  s->bytestream += n;
177  memcpy(ptr2, s->bytestream, n);
178  s->bytestream += n;
179  ptr1 += p->linesize[1];
180  ptr2 += p->linesize[2];
181  }
182  }
183  break;
185  {
186  uint16_t *ptr1, *ptr2;
187  const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
188  unsigned int j, v;
189 
190  n = avctx->width * 2;
191  ptr = p->data[0];
192  linesize = p->linesize[0];
193  if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
194  return AVERROR_INVALIDDATA;
195  for (i = 0; i < avctx->height; i++) {
196  for (j = 0; j < n / 2; j++) {
197  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
198  ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
199  }
200  s->bytestream += n;
201  ptr += linesize;
202  }
203  ptr1 = (uint16_t*)p->data[1];
204  ptr2 = (uint16_t*)p->data[2];
205  n >>= 1;
206  h = avctx->height >> 1;
207  for (i = 0; i < h; i++) {
208  for (j = 0; j < n / 2; j++) {
209  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
210  ptr1[j] = (v * f + 16384) >> 15;
211  }
212  s->bytestream += n;
213 
214  for (j = 0; j < n / 2; j++) {
215  v = av_be2ne16(((uint16_t *)s->bytestream)[j]);
216  ptr2[j] = (v * f + 16384) >> 15;
217  }
218  s->bytestream += n;
219 
220  ptr1 += p->linesize[1] / 2;
221  ptr2 += p->linesize[2] / 2;
222  }
223  }
224  break;
225  }
226  *got_frame = 1;
227 
228  return s->bytestream - s->bytestream_start;
229 }
230 
231 
232 #if CONFIG_PGM_DECODER
233 AVCodec ff_pgm_decoder = {
234  .name = "pgm",
235  .type = AVMEDIA_TYPE_VIDEO,
236  .id = AV_CODEC_ID_PGM,
237  .priv_data_size = sizeof(PNMContext),
239  .capabilities = CODEC_CAP_DR1,
240  .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
241 };
242 #endif
243 
244 #if CONFIG_PGMYUV_DECODER
245 AVCodec ff_pgmyuv_decoder = {
246  .name = "pgmyuv",
247  .type = AVMEDIA_TYPE_VIDEO,
248  .id = AV_CODEC_ID_PGMYUV,
249  .priv_data_size = sizeof(PNMContext),
251  .capabilities = CODEC_CAP_DR1,
252  .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
253 };
254 #endif
255 
256 #if CONFIG_PPM_DECODER
257 AVCodec ff_ppm_decoder = {
258  .name = "ppm",
259  .type = AVMEDIA_TYPE_VIDEO,
260  .id = AV_CODEC_ID_PPM,
261  .priv_data_size = sizeof(PNMContext),
263  .capabilities = CODEC_CAP_DR1,
264  .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
265 };
266 #endif
267 
268 #if CONFIG_PBM_DECODER
269 AVCodec ff_pbm_decoder = {
270  .name = "pbm",
271  .type = AVMEDIA_TYPE_VIDEO,
272  .id = AV_CODEC_ID_PBM,
273  .priv_data_size = sizeof(PNMContext),
275  .capabilities = CODEC_CAP_DR1,
276  .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
277 };
278 #endif
279 
280 #if CONFIG_PAM_DECODER
281 AVCodec ff_pam_decoder = {
282  .name = "pam",
283  .type = AVMEDIA_TYPE_VIDEO,
284  .id = AV_CODEC_ID_PAM,
285  .priv_data_size = sizeof(PNMContext),
287  .capabilities = CODEC_CAP_DR1,
288  .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
289 };
290 #endif