]> git.sesse.net Git - ffmpeg/blob - libavcodec/aliaspixenc.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / aliaspixenc.c
1 /*
2  * Alias PIX image encoder
3  * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@gmail.com>
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 "libavutil/intreadwrite.h"
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27
28 #define ALIAS_HEADER_SIZE 10
29
30 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
31                         const AVFrame *frame, int *got_packet)
32 {
33     int width, height, bits_pixel, i, j, length, ret;
34     uint8_t *in_buf, *buf;
35
36     width  = avctx->width;
37     height = avctx->height;
38
39     if (width > 65535 || height > 65535 ||
40         width * height >= INT_MAX / 4 - ALIAS_HEADER_SIZE) {
41         av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", width, height);
42         return AVERROR_INVALIDDATA;
43     }
44
45     switch (avctx->pix_fmt) {
46     case AV_PIX_FMT_GRAY8:
47         bits_pixel = 8;
48         break;
49     case AV_PIX_FMT_BGR24:
50         bits_pixel = 24;
51         break;
52     default:
53         return AVERROR(EINVAL);
54     }
55
56     length = ALIAS_HEADER_SIZE + 4 * width * height; // max possible
57     if ((ret = ff_alloc_packet2(avctx, pkt, length, ALIAS_HEADER_SIZE + height*2)) < 0) {
58         av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
59         return ret;
60     }
61
62     buf = pkt->data;
63
64     /* Encode header. */
65     bytestream_put_be16(&buf, width);
66     bytestream_put_be16(&buf, height);
67     bytestream_put_be32(&buf, 0); /* X, Y offset */
68     bytestream_put_be16(&buf, bits_pixel);
69
70     for (j = 0; j < height; j++) {
71         in_buf = frame->data[0] + frame->linesize[0] * j;
72         for (i = 0; i < width; ) {
73             int count = 0;
74             int pixel;
75
76             if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
77                 pixel = *in_buf;
78                 while (count < 255 && count + i < width && pixel == *in_buf) {
79                     count++;
80                     in_buf++;
81                 }
82                 bytestream_put_byte(&buf, count);
83                 bytestream_put_byte(&buf, pixel);
84             } else { /* AV_PIX_FMT_BGR24 */
85                 pixel = AV_RB24(in_buf);
86                 while (count < 255 && count + i < width &&
87                        pixel == AV_RB24(in_buf)) {
88                     count++;
89                     in_buf += 3;
90                 }
91                 bytestream_put_byte(&buf, count);
92                 bytestream_put_be24(&buf, pixel);
93             }
94             i += count;
95         }
96     }
97
98     /* Total length */
99     av_shrink_packet(pkt, buf - pkt->data);
100     pkt->flags |= AV_PKT_FLAG_KEY;
101     *got_packet = 1;
102
103     return 0;
104 }
105
106 const AVCodec ff_alias_pix_encoder = {
107     .name      = "alias_pix",
108     .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
109     .type      = AVMEDIA_TYPE_VIDEO,
110     .id        = AV_CODEC_ID_ALIAS_PIX,
111     .encode2   = encode_frame,
112     .pix_fmts  = (const enum AVPixelFormat[]) {
113         AV_PIX_FMT_BGR24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
114     },
115 };