yading@10
|
1 /*
|
yading@10
|
2 * Autodesk RLE Decoder
|
yading@10
|
3 * Copyright (C) 2005 the ffmpeg project
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /**
|
yading@10
|
23 * @file
|
yading@10
|
24 * Autodesk RLE Video Decoder by Konstantin Shishkov
|
yading@10
|
25 */
|
yading@10
|
26
|
yading@10
|
27 #include <stdio.h>
|
yading@10
|
28 #include <stdlib.h>
|
yading@10
|
29 #include <string.h>
|
yading@10
|
30
|
yading@10
|
31 #include "avcodec.h"
|
yading@10
|
32 #include "internal.h"
|
yading@10
|
33 #include "msrledec.h"
|
yading@10
|
34
|
yading@10
|
35 typedef struct AascContext {
|
yading@10
|
36 AVCodecContext *avctx;
|
yading@10
|
37 GetByteContext gb;
|
yading@10
|
38 AVFrame *frame;
|
yading@10
|
39
|
yading@10
|
40 uint32_t palette[AVPALETTE_COUNT];
|
yading@10
|
41 int palette_size;
|
yading@10
|
42 } AascContext;
|
yading@10
|
43
|
yading@10
|
44 static av_cold int aasc_decode_init(AVCodecContext *avctx)
|
yading@10
|
45 {
|
yading@10
|
46 AascContext *s = avctx->priv_data;
|
yading@10
|
47 uint8_t *ptr;
|
yading@10
|
48 int i;
|
yading@10
|
49
|
yading@10
|
50 s->avctx = avctx;
|
yading@10
|
51 switch (avctx->bits_per_coded_sample) {
|
yading@10
|
52 case 8:
|
yading@10
|
53 avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
yading@10
|
54
|
yading@10
|
55 ptr = avctx->extradata;
|
yading@10
|
56 s->palette_size = FFMIN(avctx->extradata_size, AVPALETTE_SIZE);
|
yading@10
|
57 for (i = 0; i < s->palette_size / 4; i++) {
|
yading@10
|
58 s->palette[i] = 0xFFU << 24 | AV_RL32(ptr);
|
yading@10
|
59 ptr += 4;
|
yading@10
|
60 }
|
yading@10
|
61 break;
|
yading@10
|
62 case 16:
|
yading@10
|
63 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
|
yading@10
|
64 break;
|
yading@10
|
65 case 24:
|
yading@10
|
66 avctx->pix_fmt = AV_PIX_FMT_BGR24;
|
yading@10
|
67 break;
|
yading@10
|
68 default:
|
yading@10
|
69 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_coded_sample);
|
yading@10
|
70 return -1;
|
yading@10
|
71 }
|
yading@10
|
72
|
yading@10
|
73 s->frame = av_frame_alloc();
|
yading@10
|
74 if (!s->frame)
|
yading@10
|
75 return AVERROR(ENOMEM);
|
yading@10
|
76
|
yading@10
|
77 return 0;
|
yading@10
|
78 }
|
yading@10
|
79
|
yading@10
|
80 static int aasc_decode_frame(AVCodecContext *avctx,
|
yading@10
|
81 void *data, int *got_frame,
|
yading@10
|
82 AVPacket *avpkt)
|
yading@10
|
83 {
|
yading@10
|
84 const uint8_t *buf = avpkt->data;
|
yading@10
|
85 int buf_size = avpkt->size;
|
yading@10
|
86 AascContext *s = avctx->priv_data;
|
yading@10
|
87 int compr, i, stride, psize, ret;
|
yading@10
|
88
|
yading@10
|
89 if (buf_size < 4) {
|
yading@10
|
90 av_log(avctx, AV_LOG_ERROR, "frame too short\n");
|
yading@10
|
91 return AVERROR_INVALIDDATA;
|
yading@10
|
92 }
|
yading@10
|
93
|
yading@10
|
94 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
|
yading@10
|
95 return ret;
|
yading@10
|
96
|
yading@10
|
97 compr = AV_RL32(buf);
|
yading@10
|
98 buf += 4;
|
yading@10
|
99 buf_size -= 4;
|
yading@10
|
100 psize = avctx->bits_per_coded_sample / 8;
|
yading@10
|
101 switch (avctx->codec_tag) {
|
yading@10
|
102 case MKTAG('A', 'A', 'S', '4'):
|
yading@10
|
103 bytestream2_init(&s->gb, buf - 4, buf_size + 4);
|
yading@10
|
104 ff_msrle_decode(avctx, (AVPicture*)s->frame, 8, &s->gb);
|
yading@10
|
105 break;
|
yading@10
|
106 case MKTAG('A', 'A', 'S', 'C'):
|
yading@10
|
107 switch (compr) {
|
yading@10
|
108 case 0:
|
yading@10
|
109 stride = (avctx->width * psize + psize) & ~psize;
|
yading@10
|
110 for (i = avctx->height - 1; i >= 0; i--) {
|
yading@10
|
111 if (avctx->width * psize > buf_size) {
|
yading@10
|
112 av_log(avctx, AV_LOG_ERROR, "Next line is beyond buffer bounds\n");
|
yading@10
|
113 break;
|
yading@10
|
114 }
|
yading@10
|
115 memcpy(s->frame->data[0] + i * s->frame->linesize[0], buf, avctx->width * psize);
|
yading@10
|
116 buf += stride;
|
yading@10
|
117 buf_size -= stride;
|
yading@10
|
118 }
|
yading@10
|
119 break;
|
yading@10
|
120 case 1:
|
yading@10
|
121 bytestream2_init(&s->gb, buf, buf_size);
|
yading@10
|
122 ff_msrle_decode(avctx, (AVPicture*)s->frame, 8, &s->gb);
|
yading@10
|
123 break;
|
yading@10
|
124 default:
|
yading@10
|
125 av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
|
yading@10
|
126 return AVERROR_INVALIDDATA;
|
yading@10
|
127 }
|
yading@10
|
128 break;
|
yading@10
|
129 default:
|
yading@10
|
130 av_log(avctx, AV_LOG_ERROR, "Unknown FourCC: %X\n", avctx->codec_tag);
|
yading@10
|
131 return -1;
|
yading@10
|
132 }
|
yading@10
|
133
|
yading@10
|
134 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
|
yading@10
|
135 memcpy(s->frame->data[1], s->palette, s->palette_size);
|
yading@10
|
136
|
yading@10
|
137 *got_frame = 1;
|
yading@10
|
138 if ((ret = av_frame_ref(data, s->frame)) < 0)
|
yading@10
|
139 return ret;
|
yading@10
|
140
|
yading@10
|
141 /* report that the buffer was completely consumed */
|
yading@10
|
142 return buf_size;
|
yading@10
|
143 }
|
yading@10
|
144
|
yading@10
|
145 static av_cold int aasc_decode_end(AVCodecContext *avctx)
|
yading@10
|
146 {
|
yading@10
|
147 AascContext *s = avctx->priv_data;
|
yading@10
|
148
|
yading@10
|
149 av_frame_free(&s->frame);
|
yading@10
|
150
|
yading@10
|
151 return 0;
|
yading@10
|
152 }
|
yading@10
|
153
|
yading@10
|
154 AVCodec ff_aasc_decoder = {
|
yading@10
|
155 .name = "aasc",
|
yading@10
|
156 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
157 .id = AV_CODEC_ID_AASC,
|
yading@10
|
158 .priv_data_size = sizeof(AascContext),
|
yading@10
|
159 .init = aasc_decode_init,
|
yading@10
|
160 .close = aasc_decode_end,
|
yading@10
|
161 .decode = aasc_decode_frame,
|
yading@10
|
162 .capabilities = CODEC_CAP_DR1,
|
yading@10
|
163 .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
|
yading@10
|
164 };
|