]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/ptx.c
Fix segault
[frescor/ffmpeg.git] / libavcodec / ptx.c
1 /*
2  * V.Flash PTX (.ptx) image decoder
3  * Copyright (c) 2007 Ivo van Poorten
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 #include "libavutil/intreadwrite.h"
23 #include "avcodec.h"
24
25 typedef struct PTXContext {
26     AVFrame picture;
27 } PTXContext;
28
29 static av_cold int ptx_init(AVCodecContext *avctx) {
30     PTXContext *s = avctx->priv_data;
31
32     avcodec_get_frame_defaults(&s->picture);
33     avctx->coded_frame= &s->picture;
34
35     return 0;
36 }
37
38 static int ptx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
39                             AVPacket *avpkt) {
40     const uint8_t *buf = avpkt->data;
41     PTXContext * const s = avctx->priv_data;
42     AVFrame *picture = data;
43     AVFrame * const p = &s->picture;
44     unsigned int offset, w, h, y, stride, bytes_per_pixel;
45     uint8_t *ptr;
46
47     offset          = AV_RL16(buf);
48     w               = AV_RL16(buf+8);
49     h               = AV_RL16(buf+10);
50     bytes_per_pixel = AV_RL16(buf+12) >> 3;
51
52     if (bytes_per_pixel != 2) {
53         av_log(avctx, AV_LOG_ERROR, "image format is not rgb15, please report on ffmpeg-users mailing list\n");
54         return -1;
55     }
56
57     avctx->pix_fmt = PIX_FMT_RGB555;
58
59     if (offset != 0x2c)
60         av_log(avctx, AV_LOG_WARNING, "offset != 0x2c, untested due to lack of sample files\n");
61
62     buf += offset;
63
64     if (p->data[0])
65         avctx->release_buffer(avctx, p);
66
67     if (avcodec_check_dimensions(avctx, w, h))
68         return -1;
69     if (w != avctx->width || h != avctx->height)
70         avcodec_set_dimensions(avctx, w, h);
71     if (avctx->get_buffer(avctx, p) < 0) {
72         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
73         return -1;
74     }
75
76     p->pict_type = FF_I_TYPE;
77
78     ptr    = p->data[0];
79     stride = p->linesize[0];
80
81     for (y=0; y<h; y++) {
82 #ifdef WORDS_BIGENDIAN
83         unsigned int x;
84         for (x=0; x<w*bytes_per_pixel; x+=bytes_per_pixel)
85             AV_WN16(ptr+x, AV_RL16(buf+x));
86 #else
87         memcpy(ptr, buf, w*bytes_per_pixel);
88 #endif
89         ptr += stride;
90         buf += w*bytes_per_pixel;
91     }
92
93     *picture = s->picture;
94     *data_size = sizeof(AVPicture);
95
96     return offset + w*h*bytes_per_pixel;
97 }
98
99 static av_cold int ptx_end(AVCodecContext *avctx) {
100     PTXContext *s = avctx->priv_data;
101
102     if(s->picture.data[0])
103         avctx->release_buffer(avctx, &s->picture);
104
105     return 0;
106 }
107
108 AVCodec ptx_decoder = {
109     "ptx",
110     CODEC_TYPE_VIDEO,
111     CODEC_ID_PTX,
112     sizeof(PTXContext),
113     ptx_init,
114     NULL,
115     ptx_end,
116     ptx_decode_frame,
117     0,
118     NULL,
119     .long_name = NULL_IF_CONFIG_SMALL("V.Flash PTX image"),
120 };