]> git.sesse.net Git - ffmpeg/blob - libavcodec/pnmenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / pnmenc.c
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 "pnm.h"
24
25
26 static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
27                             int buf_size, void *data)
28 {
29     PNMContext *s     = avctx->priv_data;
30     AVFrame *pict     = data;
31     AVFrame * const p = (AVFrame*)&s->picture;
32     int i, h, h1, c, n, linesize;
33     uint8_t *ptr, *ptr1, *ptr2;
34
35     if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
36         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
37         return -1;
38     }
39
40     *p           = *pict;
41     p->pict_type = AV_PICTURE_TYPE_I;
42     p->key_frame = 1;
43
44     s->bytestream_start =
45     s->bytestream       = outbuf;
46     s->bytestream_end   = outbuf + buf_size;
47
48     h  = avctx->height;
49     h1 = h;
50     switch (avctx->pix_fmt) {
51     case PIX_FMT_MONOWHITE:
52         c  = '4';
53         n  = (avctx->width + 7) >> 3;
54         break;
55     case PIX_FMT_GRAY8:
56         c  = '5';
57         n  = avctx->width;
58         break;
59     case PIX_FMT_GRAY16BE:
60         c  = '5';
61         n  = avctx->width * 2;
62         break;
63     case PIX_FMT_RGB24:
64         c  = '6';
65         n  = avctx->width * 3;
66         break;
67     case PIX_FMT_RGB48BE:
68         c  = '6';
69         n  = avctx->width * 6;
70         break;
71     case PIX_FMT_YUV420P:
72         c  = '5';
73         n  = avctx->width;
74         h1 = (h * 3) / 2;
75         break;
76     default:
77         return -1;
78     }
79     snprintf(s->bytestream, s->bytestream_end - s->bytestream,
80              "P%c\n%d %d\n", c, avctx->width, h1);
81     s->bytestream += strlen(s->bytestream);
82     if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
83         snprintf(s->bytestream, s->bytestream_end - s->bytestream,
84                  "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
85         s->bytestream += strlen(s->bytestream);
86     }
87
88     ptr      = p->data[0];
89     linesize = p->linesize[0];
90     for (i = 0; i < h; i++) {
91         memcpy(s->bytestream, ptr, n);
92         s->bytestream += n;
93         ptr           += linesize;
94     }
95
96     if (avctx->pix_fmt == PIX_FMT_YUV420P) {
97         h >>= 1;
98         n >>= 1;
99         ptr1 = p->data[1];
100         ptr2 = p->data[2];
101         for (i = 0; i < h; i++) {
102             memcpy(s->bytestream, ptr1, n);
103             s->bytestream += n;
104             memcpy(s->bytestream, ptr2, n);
105             s->bytestream += n;
106                 ptr1 += p->linesize[1];
107                 ptr2 += p->linesize[2];
108         }
109     }
110     return s->bytestream - s->bytestream_start;
111 }
112
113
114 #if CONFIG_PGM_ENCODER
115 AVCodec ff_pgm_encoder = {
116     .name           = "pgm",
117     .type           = AVMEDIA_TYPE_VIDEO,
118     .id             = CODEC_ID_PGM,
119     .priv_data_size = sizeof(PNMContext),
120     .init           = ff_pnm_init,
121     .encode         = pnm_encode_frame,
122     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
123     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
124 };
125 #endif
126
127 #if CONFIG_PGMYUV_ENCODER
128 AVCodec ff_pgmyuv_encoder = {
129     .name           = "pgmyuv",
130     .type           = AVMEDIA_TYPE_VIDEO,
131     .id             = CODEC_ID_PGMYUV,
132     .priv_data_size = sizeof(PNMContext),
133     .init           = ff_pnm_init,
134     .encode         = pnm_encode_frame,
135     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
136     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
137 };
138 #endif
139
140 #if CONFIG_PPM_ENCODER
141 AVCodec ff_ppm_encoder = {
142     .name           = "ppm",
143     .type           = AVMEDIA_TYPE_VIDEO,
144     .id             = CODEC_ID_PPM,
145     .priv_data_size = sizeof(PNMContext),
146     .init           = ff_pnm_init,
147     .encode         = pnm_encode_frame,
148     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
149     .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
150 };
151 #endif
152
153 #if CONFIG_PBM_ENCODER
154 AVCodec ff_pbm_encoder = {
155     .name           = "pbm",
156     .type           = AVMEDIA_TYPE_VIDEO,
157     .id             = CODEC_ID_PBM,
158     .priv_data_size = sizeof(PNMContext),
159     .init           = ff_pnm_init,
160     .encode         = pnm_encode_frame,
161     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
162     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
163 };
164 #endif