]> git.sesse.net Git - ffmpeg/blob - libavcodec/aasc.c
5fcdfc01949545754a014363559eeef7c852a068
[ffmpeg] / libavcodec / aasc.c
1 /*
2  * Autodesk RLE Decoder
3  * Copyright (C) 2005 the ffmpeg project
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 /**
23  * @file
24  * Autodesk RLE Video Decoder by Konstantin Shishkov
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "avcodec.h"
32 #include "msrledec.h"
33
34 typedef struct AascContext {
35     AVCodecContext *avctx;
36     GetByteContext gb;
37     AVFrame frame;
38 } AascContext;
39
40 static av_cold int aasc_decode_init(AVCodecContext *avctx)
41 {
42     AascContext *s = avctx->priv_data;
43
44     s->avctx = avctx;
45
46     avctx->pix_fmt = AV_PIX_FMT_BGR24;
47
48     return 0;
49 }
50
51 static int aasc_decode_frame(AVCodecContext *avctx,
52                               void *data, int *got_frame,
53                               AVPacket *avpkt)
54 {
55     const uint8_t *buf = avpkt->data;
56     int buf_size       = avpkt->size;
57     AascContext *s     = avctx->priv_data;
58     int compr, i, stride, ret;
59
60     s->frame.reference = 1;
61     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
62     if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
63         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
64         return ret;
65     }
66
67     compr     = AV_RL32(buf);
68     buf      += 4;
69     buf_size -= 4;
70     switch (compr) {
71     case 0:
72         stride = (avctx->width * 3 + 3) & ~3;
73         for (i = avctx->height - 1; i >= 0; i--) {
74             memcpy(s->frame.data[0] + i * s->frame.linesize[0], buf, avctx->width * 3);
75             buf += stride;
76         }
77         break;
78     case 1:
79         bytestream2_init(&s->gb, buf, buf_size);
80         ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
81         break;
82     default:
83         av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
84         return AVERROR_INVALIDDATA;
85     }
86
87     *got_frame = 1;
88     *(AVFrame*)data = s->frame;
89
90     /* report that the buffer was completely consumed */
91     return buf_size;
92 }
93
94 static av_cold int aasc_decode_end(AVCodecContext *avctx)
95 {
96     AascContext *s = avctx->priv_data;
97
98     /* release the last frame */
99     if (s->frame.data[0])
100         avctx->release_buffer(avctx, &s->frame);
101
102     return 0;
103 }
104
105 AVCodec ff_aasc_decoder = {
106     .name           = "aasc",
107     .type           = AVMEDIA_TYPE_VIDEO,
108     .id             = AV_CODEC_ID_AASC,
109     .priv_data_size = sizeof(AascContext),
110     .init           = aasc_decode_init,
111     .close          = aasc_decode_end,
112     .decode         = aasc_decode_frame,
113     .capabilities   = CODEC_CAP_DR1,
114     .long_name      = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
115 };