]> git.sesse.net Git - ffmpeg/blob - libavcodec/pamenc.c
pthread_frame: ensure the threads don't run simultaneously with hwaccel
[ffmpeg] / libavcodec / pamenc.c
1 /*
2  * PAM image format
3  * Copyright (c) 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/imgutils.h"
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27
28 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29                             const AVFrame *pict, int *got_packet)
30 {
31     uint8_t *bytestream_start, *bytestream, *bytestream_end;
32     const AVFrame * const p = pict;
33     int i, h, w, n, linesize, depth, maxval, ret;
34     const char *tuple_type;
35     uint8_t *ptr;
36     int size = av_image_get_buffer_size(avctx->pix_fmt,
37                                         avctx->width, avctx->height, 1);
38
39     if ((ret = ff_alloc_packet(pkt, size + 200)) < 0) {
40         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
41         return ret;
42     }
43
44     bytestream_start =
45     bytestream       = pkt->data;
46     bytestream_end   = pkt->data + pkt->size;
47
48     h = avctx->height;
49     w = avctx->width;
50     switch (avctx->pix_fmt) {
51     case AV_PIX_FMT_MONOWHITE:
52         n          = (w + 7) >> 3;
53         depth      = 1;
54         maxval     = 1;
55         tuple_type = "BLACKANDWHITE";
56         break;
57     case AV_PIX_FMT_GRAY8:
58         n          = w;
59         depth      = 1;
60         maxval     = 255;
61         tuple_type = "GRAYSCALE";
62         break;
63     case AV_PIX_FMT_RGB24:
64         n          = w * 3;
65         depth      = 3;
66         maxval     = 255;
67         tuple_type = "RGB";
68         break;
69     case AV_PIX_FMT_RGB32:
70         n          = w * 4;
71         depth      = 4;
72         maxval     = 255;
73         tuple_type = "RGB_ALPHA";
74         break;
75     default:
76         return -1;
77     }
78     snprintf(bytestream, bytestream_end - bytestream,
79              "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
80              w, h, depth, maxval, tuple_type);
81     bytestream += strlen(bytestream);
82
83     ptr      = p->data[0];
84     linesize = p->linesize[0];
85
86     if (avctx->pix_fmt == AV_PIX_FMT_RGB32) {
87         int j;
88         unsigned int v;
89
90         for (i = 0; i < h; i++) {
91             for (j = 0; j < w; j++) {
92                 v = ((uint32_t *)ptr)[j];
93                 bytestream_put_be24(&bytestream, v);
94                 *bytestream++ = v >> 24;
95             }
96             ptr += linesize;
97         }
98     } else {
99         for (i = 0; i < h; i++) {
100             memcpy(bytestream, ptr, n);
101             bytestream += n;
102             ptr        += linesize;
103         }
104     }
105
106     pkt->size   = bytestream - bytestream_start;
107     pkt->flags |= AV_PKT_FLAG_KEY;
108     *got_packet = 1;
109     return 0;
110 }
111
112 static av_cold int pam_encode_init(AVCodecContext *avctx)
113 {
114 #if FF_API_CODED_FRAME
115 FF_DISABLE_DEPRECATION_WARNINGS
116     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
117     avctx->coded_frame->key_frame = 1;
118 FF_ENABLE_DEPRECATION_WARNINGS
119 #endif
120
121     return 0;
122 }
123
124 AVCodec ff_pam_encoder = {
125     .name           = "pam",
126     .long_name      = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
127     .type           = AVMEDIA_TYPE_VIDEO,
128     .id             = AV_CODEC_ID_PAM,
129     .init           = pam_encode_init,
130     .encode2        = pam_encode_frame,
131     .pix_fmts       = (const enum AVPixelFormat[]){
132         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_GRAY8, AV_PIX_FMT_MONOWHITE,
133         AV_PIX_FMT_NONE
134     },
135 };