]> git.sesse.net Git - ffmpeg/blob - libavcodec/aasc.c
g723.1dec: Make postfilter user switchable
[ffmpeg] / 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
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     GetByteContext gb;
38     AVFrame frame;
39 } AascContext;
40
41 static av_cold int aasc_decode_init(AVCodecContext *avctx)
42 {
43     AascContext *s = avctx->priv_data;
44
45     s->avctx = avctx;
46     switch (avctx->bits_per_coded_sample) {
47     case 16:
48         avctx->pix_fmt = PIX_FMT_RGB555;
49         break;
50     case 24:
51         avctx->pix_fmt = PIX_FMT_BGR24;
52         break;
53     default:
54         av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_coded_sample);
55         return -1;
56     }
57     avcodec_get_frame_defaults(&s->frame);
58
59     return 0;
60 }
61
62 static int aasc_decode_frame(AVCodecContext *avctx,
63                               void *data, int *data_size,
64                               AVPacket *avpkt)
65 {
66     const uint8_t *buf = avpkt->data;
67     int buf_size = avpkt->size;
68     AascContext *s = avctx->priv_data;
69     int compr, i, stride;
70
71     s->frame.reference = 3;
72     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
73     if (avctx->reget_buffer(avctx, &s->frame)) {
74         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
75         return -1;
76     }
77
78     compr = AV_RL32(buf);
79     buf += 4;
80     buf_size -= 4;
81     switch (avctx->codec_tag) {
82     case MKTAG('A', 'A', 'S', '4'):
83         bytestream2_init(&s->gb, buf - 4, buf_size + 4);
84         ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
85         break;
86     case MKTAG('A', 'A', 'S', 'C'):
87     switch(compr){
88     case 0:
89         stride = (avctx->width * 3 + 3) & ~3;
90         for(i = avctx->height - 1; i >= 0; i--){
91             if(avctx->width*3 > buf_size){
92                 av_log(avctx, AV_LOG_ERROR, "Next line is beyond buffer bounds\n");
93                 break;
94             }
95             memcpy(s->frame.data[0] + i*s->frame.linesize[0], buf, avctx->width*3);
96             buf += stride;
97             buf_size -= stride;
98         }
99         break;
100     case 1:
101         bytestream2_init(&s->gb, buf, buf_size);
102         ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
103         break;
104     default:
105         av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
106         return -1;
107     }
108         break;
109     default:
110         av_log(avctx, AV_LOG_ERROR, "Unknown FourCC: %X\n", avctx->codec_tag);
111         return -1;
112     }
113
114     *data_size = sizeof(AVFrame);
115     *(AVFrame*)data = s->frame;
116
117     /* report that the buffer was completely consumed */
118     return buf_size;
119 }
120
121 static av_cold int aasc_decode_end(AVCodecContext *avctx)
122 {
123     AascContext *s = avctx->priv_data;
124
125     /* release the last frame */
126     if (s->frame.data[0])
127         avctx->release_buffer(avctx, &s->frame);
128
129     return 0;
130 }
131
132 AVCodec ff_aasc_decoder = {
133     .name           = "aasc",
134     .type           = AVMEDIA_TYPE_VIDEO,
135     .id             = CODEC_ID_AASC,
136     .priv_data_size = sizeof(AascContext),
137     .init           = aasc_decode_init,
138     .close          = aasc_decode_end,
139     .decode         = aasc_decode_frame,
140     .capabilities   = CODEC_CAP_DR1,
141     .long_name      = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
142 };