p*menc: use the AVFrame API properly.
[libav.git] / libavcodec / pamenc.c
CommitLineData
638783bf
DB
1/*
2 * PAM image format
3 * Copyright (c) 2002, 2003 Fabrice Bellard
4 *
2912e87a 5 * This file is part of Libav.
638783bf 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
638783bf
DB
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
638783bf
DB
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
638783bf
DB
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avcodec.h"
23#include "bytestream.h"
e9b380a7 24#include "internal.h"
638783bf 25
e9b380a7
AK
26static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
27 const AVFrame *pict, int *got_packet)
638783bf 28{
afa21a12
AK
29 uint8_t *bytestream_start, *bytestream, *bytestream_end;
30 const AVFrame * const p = pict;
e9b380a7 31 int i, h, w, n, linesize, depth, maxval, ret;
638783bf
DB
32 const char *tuple_type;
33 uint8_t *ptr;
34
e9b380a7
AK
35 if ((ret = ff_alloc_packet(pkt, avpicture_get_size(avctx->pix_fmt,
36 avctx->width,
37 avctx->height) + 200)) < 0) {
638783bf 38 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
e9b380a7 39 return ret;
638783bf
DB
40 }
41
afa21a12
AK
42 bytestream_start =
43 bytestream = pkt->data;
44 bytestream_end = pkt->data + pkt->size;
638783bf
DB
45
46 h = avctx->height;
47 w = avctx->width;
48 switch (avctx->pix_fmt) {
716d413c 49 case AV_PIX_FMT_MONOWHITE:
638783bf
DB
50 n = (w + 7) >> 3;
51 depth = 1;
52 maxval = 1;
53 tuple_type = "BLACKANDWHITE";
54 break;
716d413c 55 case AV_PIX_FMT_GRAY8:
638783bf
DB
56 n = w;
57 depth = 1;
58 maxval = 255;
59 tuple_type = "GRAYSCALE";
60 break;
716d413c 61 case AV_PIX_FMT_RGB24:
638783bf
DB
62 n = w * 3;
63 depth = 3;
64 maxval = 255;
65 tuple_type = "RGB";
66 break;
716d413c 67 case AV_PIX_FMT_RGB32:
638783bf
DB
68 n = w * 4;
69 depth = 4;
70 maxval = 255;
71 tuple_type = "RGB_ALPHA";
72 break;
73 default:
74 return -1;
75 }
afa21a12 76 snprintf(bytestream, bytestream_end - bytestream,
66b9d706 77 "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
638783bf 78 w, h, depth, maxval, tuple_type);
afa21a12 79 bytestream += strlen(bytestream);
638783bf
DB
80
81 ptr = p->data[0];
82 linesize = p->linesize[0];
83
716d413c 84 if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
638783bf
DB
85 int j;
86 unsigned int v;
87
88 for (i = 0; i < h; i++) {
89 for (j = 0; j < w; j++) {
90 v = ((uint32_t *)ptr)[j];
afa21a12
AK
91 bytestream_put_be24(&bytestream, v);
92 *bytestream++ = v >> 24;
638783bf
DB
93 }
94 ptr += linesize;
95 }
96 } else {
97 for (i = 0; i < h; i++) {
afa21a12
AK
98 memcpy(bytestream, ptr, n);
99 bytestream += n;
100 ptr += linesize;
638783bf
DB
101 }
102 }
e9b380a7 103
afa21a12 104 pkt->size = bytestream - bytestream_start;
e9b380a7
AK
105 pkt->flags |= AV_PKT_FLAG_KEY;
106 *got_packet = 1;
107 return 0;
638783bf
DB
108}
109
afa21a12
AK
110static av_cold int pam_encode_init(AVCodecContext *avctx)
111{
112 avctx->coded_frame = av_frame_alloc();
113 if (!avctx->coded_frame)
114 return AVERROR(ENOMEM);
115
116 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
117 avctx->coded_frame->key_frame = 1;
118
119 return 0;
120}
121
122static av_cold int pam_encode_close(AVCodecContext *avctx)
123{
124 av_frame_free(&avctx->coded_frame);
125 return 0;
126}
638783bf 127
d36beb3f 128AVCodec ff_pam_encoder = {
ec6402b7 129 .name = "pam",
b2bed932 130 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
ec6402b7 131 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 132 .id = AV_CODEC_ID_PAM,
afa21a12
AK
133 .init = pam_encode_init,
134 .close = pam_encode_close,
e9b380a7 135 .encode2 = pam_encode_frame,
716d413c
AK
136 .pix_fmts = (const enum AVPixelFormat[]){
137 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_GRAY8, AV_PIX_FMT_MONOWHITE,
138 AV_PIX_FMT_NONE
00c3b67b 139 },
638783bf 140};