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