Statistics
| Branch: | Revision:

ffmpeg / libavfilter / libmpcodecs / vf_palette.c @ e4852fb3

History | View | Annotate | Download (7.71 KB)

1
/*
2
 * This file is part of MPlayer.
3
 *
4
 * MPlayer is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * MPlayer is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License along
15
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 */
18

    
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
#include <inttypes.h>
23

    
24
#include "config.h"
25
#include "mp_msg.h"
26
#include "help_mp.h"
27

    
28
#include "img_format.h"
29
#include "mp_image.h"
30
#include "vf.h"
31
#include "mpbswap.h"
32

    
33
#include "libswscale/swscale.h"
34

    
35
//===========================================================================//
36

    
37
// commented out 16 and 15 bit output support, because the conversion
38
// routines are incorrrect.  they assume the palette to be of the same
39
// depth as the output, which is incorrect. --Joey
40

    
41
static const unsigned int bgr_list[]={
42
    IMGFMT_BGR32,
43
    IMGFMT_BGR24,
44
//    IMGFMT_BGR16,
45
//    IMGFMT_BGR15,
46
    0
47
};
48
static const unsigned int rgb_list[]={
49
    IMGFMT_RGB32,
50
    IMGFMT_RGB24,
51
//    IMGFMT_RGB16,
52
//    IMGFMT_RGB15,
53
    0
54
};
55

    
56
/**
57
 * Palette is assumed to contain BGR16, see rgb32to16 to convert the palette.
58
 */
59
static void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
60
{
61
    long i;
62
    for (i=0; i<num_pixels; i++)
63
        ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
64
}
65

    
66
static void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
67
{
68
    long i;
69
    for (i=0; i<num_pixels; i++)
70
        ((uint16_t *)dst)[i] = bswap_16(((const uint16_t *)palette)[src[i]]);
71
}
72

    
73
static unsigned int gray_pal[256];
74

    
75
static unsigned int find_best(struct vf_instance *vf, unsigned int fmt){
76
    unsigned int best=0;
77
    int ret;
78
    const unsigned int* p;
79
    if(fmt==IMGFMT_BGR8) p=bgr_list;
80
    else if(fmt==IMGFMT_RGB8) p=rgb_list;
81
    else return 0;
82
    while(*p){
83
        ret=vf->next->query_format(vf->next,*p);
84
        mp_msg(MSGT_VFILTER,MSGL_DBG2,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
85
        if(ret&VFCAP_CSP_SUPPORTED_BY_HW){ best=*p; break;} // no conversion -> bingo!
86
        if(ret&VFCAP_CSP_SUPPORTED && !best) best=*p; // best with conversion
87
        ++p;
88
    }
89
    return best;
90
}
91

    
92
//===========================================================================//
93

    
94
struct vf_priv_s {
95
    unsigned int fmt;
96
    int pal_msg;
97
};
98

    
99
static int config(struct vf_instance *vf,
100
        int width, int height, int d_width, int d_height,
101
        unsigned int flags, unsigned int outfmt){
102
    if (!vf->priv->fmt)
103
        vf->priv->fmt=find_best(vf,outfmt);
104
    if(!vf->priv->fmt){
105
        // no matching fmt, so force one...
106
        if(outfmt==IMGFMT_RGB8) vf->priv->fmt=IMGFMT_RGB32;
107
        else if(outfmt==IMGFMT_BGR8) vf->priv->fmt=IMGFMT_BGR32;
108
        else return 0;
109
    }
110
    return vf_next_config(vf,width,height,d_width,d_height,flags,vf->priv->fmt);
111
}
112

    
113
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
114
    mp_image_t *dmpi;
115
    uint8_t *old_palette = mpi->planes[1];
116

    
117
    // hope we'll get DR buffer:
118
    dmpi=vf_get_image(vf->next,vf->priv->fmt,
119
        MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
120
        mpi->w, mpi->h);
121

    
122
    if (!mpi->planes[1])
123
    {
124
        if(!vf->priv->pal_msg){
125
            mp_msg(MSGT_VFILTER,MSGL_V,"[%s] no palette given, assuming builtin grayscale one\n",vf->info->name);
126
            vf->priv->pal_msg=1;
127
        }
128
        mpi->planes[1] = (unsigned char*)gray_pal;
129
    }
130

    
131
    if(mpi->w==mpi->stride[0] && dmpi->w*(dmpi->bpp>>3)==dmpi->stride[0]){
132
        // no stride conversion needed
133
        switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
134
        case 15:
135
        case 16:
136
            if (IMGFMT_IS_BGR(dmpi->imgfmt))
137
                palette8tobgr16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
138
            else
139
                palette8torgb16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
140
            break;
141
        case 24:
142
            if (IMGFMT_IS_BGR(dmpi->imgfmt))
143
                sws_convertPalette8ToPacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
144
            else
145
                sws_convertPalette8ToPacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
146
            break;
147
        case 32:
148
            if (IMGFMT_IS_BGR(dmpi->imgfmt))
149
                sws_convertPalette8ToPacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
150
            else
151
                sws_convertPalette8ToPacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
152
            break;
153
        }
154
    } else {
155
        int y;
156
        for(y=0;y<mpi->h;y++){
157
            unsigned char* src=mpi->planes[0]+y*mpi->stride[0];
158
            unsigned char* dst=dmpi->planes[0]+y*dmpi->stride[0];
159
            switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
160
            case 15:
161
            case 16:
162
                if (IMGFMT_IS_BGR(dmpi->imgfmt))
163
                    palette8tobgr16(src,dst,mpi->w,mpi->planes[1]);
164
                else
165
                    palette8torgb16(src,dst,mpi->w,mpi->planes[1]);
166
                break;
167
            case 24:
168
                if (IMGFMT_IS_BGR(dmpi->imgfmt))
169
                    sws_convertPalette8ToPacked24(src,dst,mpi->w,mpi->planes[1]);
170
                else
171
                    sws_convertPalette8ToPacked24(src,dst,mpi->w,mpi->planes[1]);
172
                break;
173
            case 32:
174
                if (IMGFMT_IS_BGR(dmpi->imgfmt))
175
                    sws_convertPalette8ToPacked32(src,dst,mpi->w,mpi->planes[1]);
176
                else
177
                    sws_convertPalette8ToPacked32(src,dst,mpi->w,mpi->planes[1]);
178
                break;
179
            }
180
        }
181
    }
182
    mpi->planes[1] = old_palette;
183

    
184
    return vf_next_put_image(vf,dmpi, pts);
185
}
186

    
187
//===========================================================================//
188

    
189
static int query_format(struct vf_instance *vf, unsigned int fmt){
190
    int best=find_best(vf,fmt);
191
    if(!best) return 0; // no match
192
    return vf->next->query_format(vf->next,best);
193
}
194

    
195
static void uninit(vf_instance_t *vf) {
196
  free(vf->priv);
197
}
198

    
199
static int vf_open(vf_instance_t *vf, char *args){
200
    unsigned int i;
201
    vf->config=config;
202
    vf->uninit=uninit;
203
    vf->put_image=put_image;
204
    vf->query_format=query_format;
205
    vf->priv=malloc(sizeof(struct vf_priv_s));
206
    memset(vf->priv, 0, sizeof(struct vf_priv_s));
207
    for(i=0;i<256;i++) gray_pal[i]=0x01010101*i;
208
    if (args)
209
    {
210
        if (!strcasecmp(args,"rgb15")) vf->priv->fmt=IMGFMT_RGB15; else
211
        if (!strcasecmp(args,"rgb16")) vf->priv->fmt=IMGFMT_RGB16; else
212
        if (!strcasecmp(args,"rgb24")) vf->priv->fmt=IMGFMT_RGB24; else
213
        if (!strcasecmp(args,"rgb32")) vf->priv->fmt=IMGFMT_RGB32; else
214
        if (!strcasecmp(args,"bgr15")) vf->priv->fmt=IMGFMT_BGR15; else
215
        if (!strcasecmp(args,"bgr16")) vf->priv->fmt=IMGFMT_BGR16; else
216
        if (!strcasecmp(args,"bgr24")) vf->priv->fmt=IMGFMT_BGR24; else
217
        if (!strcasecmp(args,"bgr32")) vf->priv->fmt=IMGFMT_BGR32; else
218
        {
219
            mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_UnknownFormatName, args);
220
            return 0;
221
        }
222
    }
223
    return 1;
224
}
225

    
226
const vf_info_t vf_info_palette = {
227
    "8bpp indexed (using palette) -> BGR 15/16/24/32 conversion",
228
    "palette",
229
    "A'rpi & Alex",
230
    "",
231
    vf_open,
232
    NULL
233
};
234

    
235
//===========================================================================//