]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/aasc.c
daabb2cd306f9a618398ad2478d6d5a7a7bfecfd
[frescor/ffmpeg.git] / libavcodec / aasc.c
1 /*
2  * Autodesk RLE Decoder
3  * Copyright (C) 2005 the ffmpeg project
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 /**
23  * @file libavcodec/aasc.c
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 "dsputil.h"
33 #include "msrledec.h"
34
35 typedef struct AascContext {
36     AVCodecContext *avctx;
37     AVFrame frame;
38 } AascContext;
39
40 #define FETCH_NEXT_STREAM_BYTE() \
41     if (stream_ptr >= buf_size) \
42     { \
43       av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
44       break; \
45     } \
46     stream_byte = buf[stream_ptr++];
47
48 static av_cold int aasc_decode_init(AVCodecContext *avctx)
49 {
50     AascContext *s = avctx->priv_data;
51
52     s->avctx = avctx;
53
54     avctx->pix_fmt = PIX_FMT_BGR24;
55     s->frame.data[0] = NULL;
56
57     return 0;
58 }
59
60 static int aasc_decode_frame(AVCodecContext *avctx,
61                               void *data, int *data_size,
62                               AVPacket *avpkt)
63 {
64     const uint8_t *buf = avpkt->data;
65     int buf_size = avpkt->size;
66     AascContext *s = avctx->priv_data;
67     int compr, i, stride;
68
69     s->frame.reference = 1;
70     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
71     if (avctx->reget_buffer(avctx, &s->frame)) {
72         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
73         return -1;
74     }
75
76     compr = AV_RL32(buf);
77     buf += 4;
78     buf_size -= 4;
79     switch(compr){
80     case 0:
81         stride = (avctx->width * 3 + 3) & ~3;
82         for(i = avctx->height - 1; i >= 0; i--){
83             memcpy(s->frame.data[0] + i*s->frame.linesize[0], buf, avctx->width*3);
84             buf += stride;
85         }
86         break;
87     case 1:
88         ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, buf - 4, buf_size + 4);
89         break;
90     default:
91         av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
92         return -1;
93     }
94
95     *data_size = sizeof(AVFrame);
96     *(AVFrame*)data = s->frame;
97
98     /* report that the buffer was completely consumed */
99     return buf_size;
100 }
101
102 static av_cold int aasc_decode_end(AVCodecContext *avctx)
103 {
104     AascContext *s = avctx->priv_data;
105
106     /* release the last frame */
107     if (s->frame.data[0])
108         avctx->release_buffer(avctx, &s->frame);
109
110     return 0;
111 }
112
113 AVCodec aasc_decoder = {
114     "aasc",
115     CODEC_TYPE_VIDEO,
116     CODEC_ID_AASC,
117     sizeof(AascContext),
118     aasc_decode_init,
119     NULL,
120     aasc_decode_end,
121     aasc_decode_frame,
122     CODEC_CAP_DR1,
123     .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
124 };