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