3 * Copyright (c) 2002, 2003 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "mpegvideo.h" //only for ParseContext
22 typedef struct PNMContext {
24 uint8_t *bytestream_start;
25 uint8_t *bytestream_end;
29 static inline int pnm_space(int c)
31 return (c == ' ' || c == '\n' || c == '\r' || c == '\t');
34 static void pnm_get(PNMContext *sc, char *str, int buf_size)
39 /* skip spaces and comments */
41 c = *sc->bytestream++;
44 c = *sc->bytestream++;
45 } while (c != '\n' && sc->bytestream < sc->bytestream_end);
46 } else if (!pnm_space(c)) {
52 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
53 if ((s - str) < buf_size - 1)
55 c = *sc->bytestream++;
60 static int common_init(AVCodecContext *avctx){
61 PNMContext *s = avctx->priv_data;
63 avcodec_get_frame_defaults((AVFrame*)&s->picture);
64 avctx->coded_frame= (AVFrame*)&s->picture;
69 static int pnm_decode_header(AVCodecContext *avctx, PNMContext * const s){
70 char buf1[32], tuple_type[32];
71 int h, w, depth, maxval;;
73 pnm_get(s, buf1, sizeof(buf1));
74 if (!strcmp(buf1, "P4")) {
75 avctx->pix_fmt = PIX_FMT_MONOWHITE;
76 } else if (!strcmp(buf1, "P5")) {
77 if (avctx->codec_id == CODEC_ID_PGMYUV)
78 avctx->pix_fmt = PIX_FMT_YUV420P;
80 avctx->pix_fmt = PIX_FMT_GRAY8;
81 } else if (!strcmp(buf1, "P6")) {
82 avctx->pix_fmt = PIX_FMT_RGB24;
83 } else if (!strcmp(buf1, "P7")) {
90 pnm_get(s, buf1, sizeof(buf1));
91 if (!strcmp(buf1, "WIDTH")) {
92 pnm_get(s, buf1, sizeof(buf1));
93 w = strtol(buf1, NULL, 10);
94 } else if (!strcmp(buf1, "HEIGHT")) {
95 pnm_get(s, buf1, sizeof(buf1));
96 h = strtol(buf1, NULL, 10);
97 } else if (!strcmp(buf1, "DEPTH")) {
98 pnm_get(s, buf1, sizeof(buf1));
99 depth = strtol(buf1, NULL, 10);
100 } else if (!strcmp(buf1, "MAXVAL")) {
101 pnm_get(s, buf1, sizeof(buf1));
102 maxval = strtol(buf1, NULL, 10);
103 } else if (!strcmp(buf1, "TUPLETYPE")) {
104 pnm_get(s, tuple_type, sizeof(tuple_type));
105 } else if (!strcmp(buf1, "ENDHDR")) {
111 /* check that all tags are present */
112 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0')
118 avctx->pix_fmt = PIX_FMT_MONOWHITE;
120 avctx->pix_fmt = PIX_FMT_GRAY8;
121 } else if (depth == 3) {
122 avctx->pix_fmt = PIX_FMT_RGB24;
123 } else if (depth == 4) {
124 avctx->pix_fmt = PIX_FMT_RGBA32;
132 pnm_get(s, buf1, sizeof(buf1));
133 avctx->width = atoi(buf1);
134 if (avctx->width <= 0)
136 pnm_get(s, buf1, sizeof(buf1));
137 avctx->height = atoi(buf1);
138 if (avctx->height <= 0)
140 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
141 pnm_get(s, buf1, sizeof(buf1));
144 /* more check if YUV420 */
145 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
146 if ((avctx->width & 1) != 0)
148 h = (avctx->height * 2);
157 static int pnm_decode_frame(AVCodecContext *avctx,
158 void *data, int *data_size,
159 uint8_t *buf, int buf_size)
161 PNMContext * const s = avctx->priv_data;
162 AVFrame *picture = data;
163 AVFrame * const p= (AVFrame*)&s->picture;
164 int i, n, linesize, h;
167 /* special case for last picture */
174 s->bytestream_end= buf + buf_size;
176 if(pnm_decode_header(avctx, s) < 0)
180 avctx->release_buffer(avctx, p);
183 if(avctx->get_buffer(avctx, p) < 0){
184 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
187 p->pict_type= FF_I_TYPE;
190 switch(avctx->pix_fmt) {
194 n = avctx->width * 3;
199 case PIX_FMT_MONOWHITE:
200 case PIX_FMT_MONOBLACK:
201 n = (avctx->width + 7) >> 3;
204 linesize = p->linesize[0];
205 for(i = 0; i < avctx->height; i++) {
206 memcpy(ptr, s->bytestream, n);
211 case PIX_FMT_YUV420P:
213 unsigned char *ptr1, *ptr2;
217 linesize = p->linesize[0];
218 for(i = 0; i < avctx->height; i++) {
219 memcpy(ptr, s->bytestream, n);
226 h = avctx->height >> 1;
227 for(i = 0; i < h; i++) {
228 memcpy(ptr1, s->bytestream, n);
230 memcpy(ptr2, s->bytestream, n);
232 ptr1 += p->linesize[1];
233 ptr2 += p->linesize[2];
239 linesize = p->linesize[0];
240 for(i = 0; i < avctx->height; i++) {
243 for(j = 0;j < avctx->width; j++) {
244 r = *s->bytestream++;
245 g = *s->bytestream++;
246 b = *s->bytestream++;
247 a = *s->bytestream++;
248 ((uint32_t *)ptr)[j] = (a << 24) | (r << 16) | (g << 8) | b;
254 *picture= *(AVFrame*)&s->picture;
255 *data_size = sizeof(AVPicture);
257 return s->bytestream - s->bytestream_start;
260 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
261 PNMContext *s = avctx->priv_data;
262 AVFrame *pict = data;
263 AVFrame * const p= (AVFrame*)&s->picture;
264 int i, h, h1, c, n, linesize;
265 uint8_t *ptr, *ptr1, *ptr2;
268 p->pict_type= FF_I_TYPE;
272 s->bytestream= outbuf;
273 s->bytestream_end= outbuf+buf_size;
277 switch(avctx->pix_fmt) {
278 case PIX_FMT_MONOWHITE:
280 n = (avctx->width + 7) >> 3;
288 n = avctx->width * 3;
290 case PIX_FMT_YUV420P:
298 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
300 c, avctx->width, h1);
301 s->bytestream += strlen(s->bytestream);
302 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
303 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
305 s->bytestream += strlen(s->bytestream);
309 linesize = p->linesize[0];
311 memcpy(s->bytestream, ptr, n);
316 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
322 memcpy(s->bytestream, ptr1, n);
324 memcpy(s->bytestream, ptr2, n);
326 ptr1 += p->linesize[1];
327 ptr2 += p->linesize[2];
330 return s->bytestream - s->bytestream_start;
333 static int pam_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data){
334 PNMContext *s = avctx->priv_data;
335 AVFrame *pict = data;
336 AVFrame * const p= (AVFrame*)&s->picture;
337 int i, h, w, n, linesize, depth, maxval;
338 const char *tuple_type;
342 p->pict_type= FF_I_TYPE;
346 s->bytestream= outbuf;
347 s->bytestream_end= outbuf+buf_size;
351 switch(avctx->pix_fmt) {
352 case PIX_FMT_MONOWHITE:
356 tuple_type = "BLACKANDWHITE";
362 tuple_type = "GRAYSCALE";
374 tuple_type = "RGB_ALPHA";
379 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
380 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLETYPE %s\nENDHDR\n",
381 w, h, depth, maxval, tuple_type);
382 s->bytestream += strlen(s->bytestream);
385 linesize = p->linesize[0];
387 if (avctx->pix_fmt == PIX_FMT_RGBA32) {
393 v = ((uint32_t *)ptr)[j];
394 *s->bytestream++ = v >> 16;
395 *s->bytestream++ = v >> 8;
396 *s->bytestream++ = v;
397 *s->bytestream++ = v >> 24;
403 memcpy(s->bytestream, ptr, n);
408 return s->bytestream - s->bytestream_start;
412 static int pnm_probe(AVProbeData *pd)
414 const char *p = pd->buf;
415 if (pd->buf_size >= 8 &&
417 p[1] >= '4' && p[1] <= '6' &&
419 return AVPROBE_SCORE_MAX - 1; /* to permit pgmyuv probe */
424 static int pgmyuv_probe(AVProbeData *pd)
426 if (match_ext(pd->filename, "pgmyuv"))
427 return AVPROBE_SCORE_MAX;
432 static int pam_probe(AVProbeData *pd)
434 const char *p = pd->buf;
435 if (pd->buf_size >= 8 &&
439 return AVPROBE_SCORE_MAX;
445 static int pnm_parse(AVCodecParserContext *s,
446 AVCodecContext *avctx,
447 uint8_t **poutbuf, int *poutbuf_size,
448 const uint8_t *buf, int buf_size)
450 ParseContext *pc = s->priv_data;
454 for(; pc->overread>0; pc->overread--){
455 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
459 pnmctx.bytestream_start=
460 pnmctx.bytestream= pc->buffer;
461 pnmctx.bytestream_end= pc->buffer + pc->index;
463 pnmctx.bytestream_start=
464 pnmctx.bytestream= buf;
465 pnmctx.bytestream_end= buf + buf_size;
467 if(pnm_decode_header(avctx, &pnmctx) < 0){
468 if(pnmctx.bytestream < pnmctx.bytestream_end){
478 if(pc->index && pc->index*2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index){
479 memcpy(pc->buffer + pc->index, buf, pc->index);
480 pc->index += pc->index;
482 buf_size -= pc->index;
488 next= pnmctx.bytestream - pnmctx.bytestream_start
489 + avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
490 if(pnmctx.bytestream_start!=buf)
496 if(ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size)<0){
501 *poutbuf = (uint8_t *)buf;
502 *poutbuf_size = buf_size;
506 AVCodecParser pnm_parser = {
507 { CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
508 sizeof(ParseContext),
514 AVCodec pgm_encoder = {
523 .pix_fmts= (enum PixelFormat[]){PIX_FMT_GRAY8, -1},
526 AVCodec pgmyuv_encoder = {
535 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
538 AVCodec ppm_encoder = {
547 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, -1},
550 AVCodec pbm_encoder = {
559 .pix_fmts= (enum PixelFormat[]){PIX_FMT_MONOWHITE, -1},
562 AVCodec pam_encoder = {
571 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, -1},