pnmenc.c 4.58 KB
Newer Older
1 2
/*
 * PNM image format
3
 * Copyright (c) 2002, 2003 Fabrice Bellard
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
21

22
#include "avcodec.h"
23
#include "bytestream.h"
24 25 26
#include "pnm.h"


27 28 29 30 31 32
static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
                            int buf_size, void *data)
{
    PNMContext *s     = avctx->priv_data;
    AVFrame *pict     = data;
    AVFrame * const p = (AVFrame*)&s->picture;
33 34 35
    int i, h, h1, c, n, linesize;
    uint8_t *ptr, *ptr1, *ptr2;

36
    if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
37 38 39 40
        av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
        return -1;
    }

41 42 43
    *p           = *pict;
    p->pict_type = FF_I_TYPE;
    p->key_frame = 1;
44

45 46 47
    s->bytestream_start =
    s->bytestream       = outbuf;
    s->bytestream_end   = outbuf + buf_size;
48

49
    h  = avctx->height;
50
    h1 = h;
51
    switch (avctx->pix_fmt) {
52
    case PIX_FMT_MONOWHITE:
53 54
        c  = '4';
        n  = (avctx->width + 7) >> 3;
55 56
        break;
    case PIX_FMT_GRAY8:
57 58
        c  = '5';
        n  = avctx->width;
59 60
        break;
    case PIX_FMT_GRAY16BE:
61 62
        c  = '5';
        n  = avctx->width * 2;
63 64
        break;
    case PIX_FMT_RGB24:
65 66
        c  = '6';
        n  = avctx->width * 3;
67
        break;
68
    case PIX_FMT_RGB48BE:
69 70
        c  = '6';
        n  = avctx->width * 6;
71
        break;
72
    case PIX_FMT_YUV420P:
73 74
        c  = '5';
        n  = avctx->width;
75 76 77 78 79 80
        h1 = (h * 3) / 2;
        break;
    default:
        return -1;
    }
    snprintf(s->bytestream, s->bytestream_end - s->bytestream,
81
             "P%c\n%d %d\n", c, avctx->width, h1);
82 83 84
    s->bytestream += strlen(s->bytestream);
    if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
        snprintf(s->bytestream, s->bytestream_end - s->bytestream,
85
                 "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
86 87 88
        s->bytestream += strlen(s->bytestream);
    }

89
    ptr      = p->data[0];
90
    linesize = p->linesize[0];
91
    for (i = 0; i < h; i++) {
92 93
        memcpy(s->bytestream, ptr, n);
        s->bytestream += n;
94
        ptr           += linesize;
95 96 97 98 99 100 101
    }

    if (avctx->pix_fmt == PIX_FMT_YUV420P) {
        h >>= 1;
        n >>= 1;
        ptr1 = p->data[1];
        ptr2 = p->data[2];
102
        for (i = 0; i < h; i++) {
103 104 105 106 107 108 109 110 111 112 113 114
            memcpy(s->bytestream, ptr1, n);
            s->bytestream += n;
            memcpy(s->bytestream, ptr2, n);
            s->bytestream += n;
                ptr1 += p->linesize[1];
                ptr2 += p->linesize[2];
        }
    }
    return s->bytestream - s->bytestream_start;
}


115
#if CONFIG_PGM_ENCODER
116 117
AVCodec pgm_encoder = {
    "pgm",
118
    AVMEDIA_TYPE_VIDEO,
119 120
    CODEC_ID_PGM,
    sizeof(PNMContext),
121
    ff_pnm_init,
122
    pnm_encode_frame,
123 124
    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
    .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
125
};
126
#endif
127

128
#if CONFIG_PGMYUV_ENCODER
129 130
AVCodec pgmyuv_encoder = {
    "pgmyuv",
131
    AVMEDIA_TYPE_VIDEO,
132 133
    CODEC_ID_PGMYUV,
    sizeof(PNMContext),
134
    ff_pnm_init,
135
    pnm_encode_frame,
136 137
    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
    .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
138
};
139
#endif
140

141
#if CONFIG_PPM_ENCODER
142 143
AVCodec ppm_encoder = {
    "ppm",
144
    AVMEDIA_TYPE_VIDEO,
145 146
    CODEC_ID_PPM,
    sizeof(PNMContext),
147
    ff_pnm_init,
148
    pnm_encode_frame,
149 150
    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
    .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
151
};
152
#endif
153

154
#if CONFIG_PBM_ENCODER
155 156
AVCodec pbm_encoder = {
    "pbm",
157
    AVMEDIA_TYPE_VIDEO,
158 159
    CODEC_ID_PBM,
    sizeof(PNMContext),
160
    ff_pnm_init,
161
    pnm_encode_frame,
162 163
    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
    .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
164
};
165
#endif