]> git.sesse.net Git - ffmpeg/blob - libavcodec/aasc.c
xsubdec: Convert to the new bitstream reader
[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 "internal.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
47     avctx->pix_fmt = AV_PIX_FMT_BGR24;
48
49     s->frame = av_frame_alloc();
50     if (!s->frame)
51         return AVERROR(ENOMEM);
52
53     return 0;
54 }
55
56 static int aasc_decode_frame(AVCodecContext *avctx,
57                               void *data, int *got_frame,
58                               AVPacket *avpkt)
59 {
60     const uint8_t *buf = avpkt->data;
61     int buf_size       = avpkt->size;
62     AascContext *s     = avctx->priv_data;
63     int compr, i, stride, ret;
64
65     if (buf_size < 4)
66         return AVERROR_INVALIDDATA;
67
68     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
69         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
70         return ret;
71     }
72
73     compr     = AV_RL32(buf);
74     buf      += 4;
75     buf_size -= 4;
76     switch (compr) {
77     case 0:
78         stride = (avctx->width * 3 + 3) & ~3;
79         if (buf_size < stride * avctx->height)
80             return AVERROR_INVALIDDATA;
81         for (i = avctx->height - 1; i >= 0; i--) {
82             memcpy(s->frame->data[0] + i * s->frame->linesize[0], buf, avctx->width * 3);
83             buf += stride;
84         }
85         break;
86     case 1:
87         bytestream2_init(&s->gb, buf, buf_size);
88         ff_msrle_decode(avctx, s->frame, 8, &s->gb);
89         break;
90     default:
91         av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
92         return AVERROR_INVALIDDATA;
93     }
94
95     *got_frame = 1;
96     if ((ret = av_frame_ref(data, s->frame)) < 0)
97         return ret;
98
99     /* report that the buffer was completely consumed */
100     return avpkt->size;
101 }
102
103 static av_cold int aasc_decode_end(AVCodecContext *avctx)
104 {
105     AascContext *s = avctx->priv_data;
106
107     av_frame_free(&s->frame);
108
109     return 0;
110 }
111
112 AVCodec ff_aasc_decoder = {
113     .name           = "aasc",
114     .long_name      = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
115     .type           = AVMEDIA_TYPE_VIDEO,
116     .id             = AV_CODEC_ID_AASC,
117     .priv_data_size = sizeof(AascContext),
118     .init           = aasc_decode_init,
119     .close          = aasc_decode_end,
120     .decode         = aasc_decode_frame,
121     .capabilities   = AV_CODEC_CAP_DR1,
122 };