OSDN Git Service

cmdutils: add support for programs in check_stream_specifier()
[coroid/libav_saccubus.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 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 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25
26 typedef struct PTXContext {
27     AVFrame picture;
28 } PTXContext;
29
30 static av_cold int ptx_init(AVCodecContext *avctx) {
31     PTXContext *s = avctx->priv_data;
32
33     avcodec_get_frame_defaults(&s->picture);
34     avctx->coded_frame= &s->picture;
35
36     return 0;
37 }
38
39 static int ptx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
40                             AVPacket *avpkt) {
41     const uint8_t *buf = avpkt->data;
42     PTXContext * const s = avctx->priv_data;
43     AVFrame *picture = data;
44     AVFrame * const p = &s->picture;
45     unsigned int offset, w, h, y, stride, bytes_per_pixel;
46     uint8_t *ptr;
47
48     offset          = AV_RL16(buf);
49     w               = AV_RL16(buf+8);
50     h               = AV_RL16(buf+10);
51     bytes_per_pixel = AV_RL16(buf+12) >> 3;
52
53     if (bytes_per_pixel != 2) {
54         av_log_ask_for_sample(avctx, "Image format is not RGB15.\n");
55         return -1;
56     }
57
58     avctx->pix_fmt = PIX_FMT_RGB555;
59
60     if (offset != 0x2c)
61         av_log_ask_for_sample(avctx, "offset != 0x2c\n");
62
63     buf += offset;
64
65     if (p->data[0])
66         avctx->release_buffer(avctx, p);
67
68     if (av_image_check_size(w, h, 0, avctx))
69         return -1;
70     if (w != avctx->width || h != avctx->height)
71         avcodec_set_dimensions(avctx, w, h);
72     if (avctx->get_buffer(avctx, p) < 0) {
73         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
74         return -1;
75     }
76
77     p->pict_type = AV_PICTURE_TYPE_I;
78
79     ptr    = p->data[0];
80     stride = p->linesize[0];
81
82     for (y=0; y<h; y++) {
83 #if HAVE_BIGENDIAN
84         unsigned int x;
85         for (x=0; x<w*bytes_per_pixel; x+=bytes_per_pixel)
86             AV_WN16(ptr+x, AV_RL16(buf+x));
87 #else
88         memcpy(ptr, buf, w*bytes_per_pixel);
89 #endif
90         ptr += stride;
91         buf += w*bytes_per_pixel;
92     }
93
94     *picture = s->picture;
95     *data_size = sizeof(AVPicture);
96
97     return offset + w*h*bytes_per_pixel;
98 }
99
100 static av_cold int ptx_end(AVCodecContext *avctx) {
101     PTXContext *s = avctx->priv_data;
102
103     if(s->picture.data[0])
104         avctx->release_buffer(avctx, &s->picture);
105
106     return 0;
107 }
108
109 AVCodec ff_ptx_decoder = {
110     .name           = "ptx",
111     .type           = AVMEDIA_TYPE_VIDEO,
112     .id             = CODEC_ID_PTX,
113     .priv_data_size = sizeof(PTXContext),
114     .init           = ptx_init,
115     .close          = ptx_end,
116     .decode         = ptx_decode_frame,
117     .capabilities   = CODEC_CAP_DR1,
118     .long_name = NULL_IF_CONFIG_SMALL("V.Flash PTX image"),
119 };