3 * Copyright (c) 2002, 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
25 static int common_init(AVCodecContext *avctx){
26 PNMContext *s = avctx->priv_data;
28 avcodec_get_frame_defaults((AVFrame*)&s->picture);
29 avctx->coded_frame= (AVFrame*)&s->picture;
34 static int pnm_decode_frame(AVCodecContext *avctx,
35 void *data, int *data_size,
36 uint8_t *buf, int buf_size)
38 PNMContext * const s = avctx->priv_data;
39 AVFrame *picture = data;
40 AVFrame * const p= (AVFrame*)&s->picture;
41 int i, n, linesize, h, upgrade = 0;
46 s->bytestream_end= buf + buf_size;
48 if(ff_pnm_decode_header(avctx, s) < 0)
52 avctx->release_buffer(avctx, p);
55 if(avctx->get_buffer(avctx, p) < 0){
56 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
59 p->pict_type= FF_I_TYPE;
62 switch(avctx->pix_fmt) {
73 case PIX_FMT_GRAY16BE:
74 case PIX_FMT_GRAY16LE:
76 if (s->maxval < 65535)
79 case PIX_FMT_MONOWHITE:
80 case PIX_FMT_MONOBLACK:
81 n = (avctx->width + 7) >> 3;
84 linesize = p->linesize[0];
85 if(s->bytestream + n*avctx->height > s->bytestream_end)
87 for(i = 0; i < avctx->height; i++) {
89 memcpy(ptr, s->bytestream, n);
90 else if (upgrade == 1) {
91 unsigned int j, f = (255*128 + s->maxval/2) / s->maxval;
93 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
94 } else if (upgrade == 2) {
95 unsigned int j, v, f = (65535*32768 + s->maxval/2) / s->maxval;
96 for (j=0; j<n/2; j++) {
97 v = be2me_16(((uint16_t *)s->bytestream)[j]);
98 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
105 case PIX_FMT_YUV420P:
107 unsigned char *ptr1, *ptr2;
111 linesize = p->linesize[0];
112 if(s->bytestream + n*avctx->height*3/2 > s->bytestream_end)
114 for(i = 0; i < avctx->height; i++) {
115 memcpy(ptr, s->bytestream, n);
122 h = avctx->height >> 1;
123 for(i = 0; i < h; i++) {
124 memcpy(ptr1, s->bytestream, n);
126 memcpy(ptr2, s->bytestream, n);
128 ptr1 += p->linesize[1];
129 ptr2 += p->linesize[2];
135 linesize = p->linesize[0];
136 if(s->bytestream + avctx->width*avctx->height*4 > s->bytestream_end)
138 for(i = 0; i < avctx->height; i++) {
141 for(j = 0;j < avctx->width; j++) {
142 r = *s->bytestream++;
143 g = *s->bytestream++;
144 b = *s->bytestream++;
145 a = *s->bytestream++;
146 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
152 *picture= *(AVFrame*)&s->picture;
153 *data_size = sizeof(AVPicture);
155 return s->bytestream - s->bytestream_start;
158 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
159 PNMContext *s = avctx->priv_data;
160 AVFrame *pict = data;
161 AVFrame * const p= (AVFrame*)&s->picture;
162 int i, h, h1, c, n, linesize;
163 uint8_t *ptr, *ptr1, *ptr2;
165 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
166 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
171 p->pict_type= FF_I_TYPE;
175 s->bytestream= outbuf;
176 s->bytestream_end= outbuf+buf_size;
180 switch(avctx->pix_fmt) {
181 case PIX_FMT_MONOWHITE:
183 n = (avctx->width + 7) >> 3;
189 case PIX_FMT_GRAY16BE:
191 n = avctx->width * 2;
195 n = avctx->width * 3;
197 case PIX_FMT_YUV420P:
205 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
207 c, avctx->width, h1);
208 s->bytestream += strlen(s->bytestream);
209 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
210 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
211 "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE) ? 255 : 65535);
212 s->bytestream += strlen(s->bytestream);
216 linesize = p->linesize[0];
218 memcpy(s->bytestream, ptr, n);
223 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
229 memcpy(s->bytestream, ptr1, n);
231 memcpy(s->bytestream, ptr2, n);
233 ptr1 += p->linesize[1];
234 ptr2 += p->linesize[2];
237 return s->bytestream - s->bytestream_start;
240 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
241 PNMContext *s = avctx->priv_data;
242 AVFrame *pict = data;
243 AVFrame * const p= (AVFrame*)&s->picture;
244 int i, h, w, n, linesize, depth, maxval;
245 const char *tuple_type;
248 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200){
249 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
254 p->pict_type= FF_I_TYPE;
258 s->bytestream= outbuf;
259 s->bytestream_end= outbuf+buf_size;
263 switch(avctx->pix_fmt) {
264 case PIX_FMT_MONOWHITE:
268 tuple_type = "BLACKANDWHITE";
274 tuple_type = "GRAYSCALE";
286 tuple_type = "RGB_ALPHA";
291 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
292 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
293 w, h, depth, maxval, tuple_type);
294 s->bytestream += strlen(s->bytestream);
297 linesize = p->linesize[0];
299 if (avctx->pix_fmt == PIX_FMT_RGB32) {
305 v = ((uint32_t *)ptr)[j];
306 *s->bytestream++ = v >> 16;
307 *s->bytestream++ = v >> 8;
308 *s->bytestream++ = v;
309 *s->bytestream++ = v >> 24;
315 memcpy(s->bytestream, ptr, n);
320 return s->bytestream - s->bytestream_start;
324 static int pnm_probe(AVProbeData *pd)
326 const char *p = pd->buf;
327 if (pd->buf_size >= 8 &&
329 p[1] >= '4' && p[1] <= '6' &&
331 return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
336 static int pgmyuv_probe(AVProbeData *pd)
338 if (match_ext(pd->filename, "pgmyuv"))
339 return AVPROBE_SCORE_MAX;
344 static int pam_probe(AVProbeData *pd)
346 const char *p = pd->buf;
347 if (pd->buf_size >= 8 &&
351 return AVPROBE_SCORE_MAX;
358 #ifdef CONFIG_PGM_ENCODER
359 AVCodec pgm_encoder = {
368 .pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, -1},
370 #endif // CONFIG_PGM_ENCODER
372 #ifdef CONFIG_PGMYUV_ENCODER
373 AVCodec pgmyuv_encoder = {
382 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
384 #endif // CONFIG_PGMYUV_ENCODER
386 #ifdef CONFIG_PPM_ENCODER
387 AVCodec ppm_encoder = {
396 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, -1},
398 #endif // CONFIG_PPM_ENCODER
400 #ifdef CONFIG_PBM_ENCODER
401 AVCodec pbm_encoder = {
410 .pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, -1},
412 #endif // CONFIG_PBM_ENCODER
414 #ifdef CONFIG_PAM_ENCODER
415 AVCodec pam_encoder = {
424 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1},
426 #endif // CONFIG_PAM_ENCODER