3 * Copyright (c) 2002, 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bytestream.h"
28 static int pam_encode_frame(AVCodecContext
*avctx
, AVPacket
*pkt
,
29 const AVFrame
*pict
, int *got_packet
)
31 PNMContext
*s
= avctx
->priv_data
;
32 AVFrame
* const p
= &s
->picture
;
33 int i
, h
, w
, n
, linesize
, depth
, maxval
, ret
;
34 const char *tuple_type
;
37 if ((ret
= ff_alloc_packet(pkt
, avpicture_get_size(avctx
->pix_fmt
,
39 avctx
->height
) + 200)) < 0) {
40 av_log(avctx
, AV_LOG_ERROR
, "encoded frame too large\n");
45 p
->pict_type
= AV_PICTURE_TYPE_I
;
49 s
->bytestream
= pkt
->data
;
50 s
->bytestream_end
= pkt
->data
+ pkt
->size
;
54 switch (avctx
->pix_fmt
) {
55 case AV_PIX_FMT_MONOWHITE
:
59 tuple_type
= "BLACKANDWHITE";
61 case AV_PIX_FMT_GRAY8
:
65 tuple_type
= "GRAYSCALE";
67 case AV_PIX_FMT_RGB24
:
73 case AV_PIX_FMT_RGB32
:
77 tuple_type
= "RGB_ALPHA";
82 snprintf(s
->bytestream
, s
->bytestream_end
- s
->bytestream
,
83 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
84 w
, h
, depth
, maxval
, tuple_type
);
85 s
->bytestream
+= strlen(s
->bytestream
);
88 linesize
= p
->linesize
[0];
90 if (avctx
->pix_fmt
== AV_PIX_FMT_RGB32
) {
94 for (i
= 0; i
< h
; i
++) {
95 for (j
= 0; j
< w
; j
++) {
96 v
= ((uint32_t *)ptr
)[j
];
97 bytestream_put_be24(&s
->bytestream
, v
);
98 *s
->bytestream
++ = v
>> 24;
103 for (i
= 0; i
< h
; i
++) {
104 memcpy(s
->bytestream
, ptr
, n
);
110 pkt
->size
= s
->bytestream
- s
->bytestream_start
;
111 pkt
->flags
|= AV_PKT_FLAG_KEY
;
117 AVCodec ff_pam_encoder
= {
119 .type
= AVMEDIA_TYPE_VIDEO
,
120 .id
= AV_CODEC_ID_PAM
,
121 .priv_data_size
= sizeof(PNMContext
),
123 .encode2
= pam_encode_frame
,
124 .pix_fmts
= (const enum AVPixelFormat
[]){
125 AV_PIX_FMT_RGB24
, AV_PIX_FMT_RGB32
, AV_PIX_FMT_GRAY8
, AV_PIX_FMT_MONOWHITE
,
128 .long_name
= NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),