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 "config.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #if HAVE_MALLOC_H
26 #include <malloc.h>
27 #endif
28
29 #include "libmpcodecs/img_format.h"
30 #include "libmpcodecs/mp_image.h"
31 #include "libvo/fastmemcpy.h"
32 #include "libavutil/mem.h"
33 #include "mp_msg.h"
34
mp_image_alloc_planes(mp_image_t * mpi)35 void mp_image_alloc_planes(mp_image_t *mpi) {
36 /* This condition is stricter than needed, but I want to be sure that every
37 * calculation step can fit in int32_t. This assumption is true over most of
38 * the code, so this acts as a safeguard for other image size calulations. */
39 if ((unsigned int)mpi->height + 2 > INT_MAX ||
40 (int64_t)mpi->width*(mpi->height+2) > INT_MAX ||
41 (int64_t)mpi->bpp*mpi->width*(mpi->height+2) > INT_MAX) {
42 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"mp_image: Unreasonable image parameters\n");
43 return;
44 }
45 // IF09 - allocate space for 4. plane delta info - unused
46 if (mpi->imgfmt == IMGFMT_IF09) {
47 if ((int64_t)mpi->chroma_width*mpi->chroma_height > INT_MAX ||
48 mpi->bpp*mpi->width*(mpi->height+2)/8 > INT_MAX - mpi->chroma_width*mpi->chroma_height) {
49 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"mp_image: Unreasonable image parameters\n");
50 return;
51 }
52 mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8+
53 mpi->chroma_width*mpi->chroma_height);
54 } else
55 mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8);
56 if (mpi->flags&MP_IMGFLAG_PLANAR) {
57 int bpp = IMGFMT_IS_YUVP16(mpi->imgfmt)? 2 : 1;
58 // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
59 mpi->stride[0]=mpi->stride[3]=bpp*mpi->width;
60 if(mpi->num_planes > 2){
61 mpi->stride[1]=mpi->stride[2]=bpp*mpi->chroma_width;
62 if(mpi->flags&MP_IMGFLAG_SWAPPED){
63 // I420/IYUV (Y,U,V)
64 mpi->planes[1]=mpi->planes[0]+mpi->stride[0]*mpi->height;
65 mpi->planes[2]=mpi->planes[1]+mpi->stride[1]*mpi->chroma_height;
66 if (mpi->num_planes > 3)
67 mpi->planes[3]=mpi->planes[2]+mpi->stride[2]*mpi->chroma_height;
68 } else {
69 // YV12,YVU9,IF09 (Y,V,U)
70 mpi->planes[2]=mpi->planes[0]+mpi->stride[0]*mpi->height;
71 mpi->planes[1]=mpi->planes[2]+mpi->stride[1]*mpi->chroma_height;
72 if (mpi->num_planes > 3)
73 mpi->planes[3]=mpi->planes[1]+mpi->stride[1]*mpi->chroma_height;
74 }
75 } else {
76 // NV12/NV21
77 mpi->stride[1]=mpi->chroma_width;
78 mpi->planes[1]=mpi->planes[0]+mpi->stride[0]*mpi->height;
79 }
80 } else {
81 mpi->stride[0]=mpi->width*mpi->bpp/8;
82 if (mpi->flags & MP_IMGFLAG_RGB_PALETTE)
83 mpi->planes[1] = av_malloc(1024);
84 }
85 mpi->flags|=MP_IMGFLAG_ALLOCATED;
86 }
87
alloc_mpi(int w,int h,unsigned long int fmt)88 mp_image_t* alloc_mpi(int w, int h, unsigned long int fmt) {
89 mp_image_t* mpi = new_mp_image(w,h);
90
91 mp_image_setfmt(mpi,fmt);
92 mp_image_alloc_planes(mpi);
93
94 return mpi;
95 }
96
copy_mpi(mp_image_t * dmpi,mp_image_t * mpi)97 void copy_mpi(mp_image_t *dmpi, mp_image_t *mpi) {
98 if(mpi->flags&MP_IMGFLAG_PLANAR){
99 memcpy_pic(dmpi->planes[0],mpi->planes[0], mpi->w, mpi->h,
100 dmpi->stride[0],mpi->stride[0]);
101 memcpy_pic(dmpi->planes[1],mpi->planes[1], mpi->chroma_width, mpi->chroma_height,
102 dmpi->stride[1],mpi->stride[1]);
103 memcpy_pic(dmpi->planes[2], mpi->planes[2], mpi->chroma_width, mpi->chroma_height,
104 dmpi->stride[2],mpi->stride[2]);
105 } else {
106 memcpy_pic(dmpi->planes[0],mpi->planes[0],
107 mpi->w*(dmpi->bpp/8), mpi->h,
108 dmpi->stride[0],mpi->stride[0]);
109 }
110 }
111
mp_image_setfmt(mp_image_t * mpi,unsigned int out_fmt)112 void mp_image_setfmt(mp_image_t* mpi,unsigned int out_fmt){
113 mpi->flags&=~(MP_IMGFLAG_PLANAR|MP_IMGFLAG_YUV|MP_IMGFLAG_SWAPPED);
114 mpi->imgfmt=out_fmt;
115 // compressed formats
116 if(out_fmt == IMGFMT_MPEGPES ||
117 out_fmt == IMGFMT_ZRMJPEGNI || out_fmt == IMGFMT_ZRMJPEGIT || out_fmt == IMGFMT_ZRMJPEGIB ||
118 IMGFMT_IS_HWACCEL(out_fmt)){
119 mpi->bpp=0;
120 return;
121 }
122 mpi->num_planes=1;
123 if (IMGFMT_IS_RGB(out_fmt)) {
124 if (IMGFMT_RGB_DEPTH(out_fmt) < 8 && !(out_fmt&128))
125 mpi->bpp = IMGFMT_RGB_DEPTH(out_fmt);
126 else
127 mpi->bpp=(IMGFMT_RGB_DEPTH(out_fmt)+7)&(~7);
128 return;
129 }
130 if (IMGFMT_IS_BGR(out_fmt)) {
131 if (IMGFMT_BGR_DEPTH(out_fmt) < 8 && !(out_fmt&128))
132 mpi->bpp = IMGFMT_BGR_DEPTH(out_fmt);
133 else
134 mpi->bpp=(IMGFMT_BGR_DEPTH(out_fmt)+7)&(~7);
135 mpi->flags|=MP_IMGFLAG_SWAPPED;
136 return;
137 }
138 if (IMGFMT_IS_XYZ(out_fmt)) {
139 mpi->bpp=3*((IMGFMT_XYZ_DEPTH(out_fmt) + 7) & ~7);
140 return;
141 }
142 mpi->num_planes=3;
143 if (out_fmt == IMGFMT_GBR24P) {
144 mpi->bpp=24;
145 mpi->flags|=MP_IMGFLAG_PLANAR;
146 return;
147 } else if (out_fmt == IMGFMT_GBR10P) {
148 mpi->bpp=30;
149 mpi->flags|=MP_IMGFLAG_PLANAR;
150 return;
151 } else if (out_fmt == IMGFMT_GBR12P) {
152 mpi->bpp=36;
153 mpi->flags|=MP_IMGFLAG_PLANAR;
154 return;
155 } else if (out_fmt == IMGFMT_GBR14P) {
156 mpi->bpp=42;
157 mpi->flags|=MP_IMGFLAG_PLANAR;
158 return;
159 }
160 mpi->flags|=MP_IMGFLAG_YUV;
161 if (mp_get_chroma_shift(out_fmt, NULL, NULL, NULL)) {
162 mpi->flags|=MP_IMGFLAG_PLANAR;
163 mpi->bpp = mp_get_chroma_shift(out_fmt, &mpi->chroma_x_shift, &mpi->chroma_y_shift, NULL);
164 mpi->chroma_width = mpi->width >> mpi->chroma_x_shift;
165 mpi->chroma_height = mpi->height >> mpi->chroma_y_shift;
166 }
167 switch(out_fmt){
168 case IMGFMT_I420:
169 case IMGFMT_IYUV:
170 mpi->flags|=MP_IMGFLAG_SWAPPED;
171 case IMGFMT_YV12:
172 return;
173 case IMGFMT_420A:
174 case IMGFMT_422A:
175 case IMGFMT_444A:
176 case IMGFMT_IF09:
177 mpi->num_planes=4;
178 case IMGFMT_YVU9:
179 case IMGFMT_444P:
180 case IMGFMT_422P:
181 case IMGFMT_411P:
182 case IMGFMT_440P:
183 case IMGFMT_444P16_LE:
184 case IMGFMT_444P16_BE:
185 case IMGFMT_444P14_LE:
186 case IMGFMT_444P14_BE:
187 case IMGFMT_444P12_LE:
188 case IMGFMT_444P12_BE:
189 case IMGFMT_444P10_LE:
190 case IMGFMT_444P10_BE:
191 case IMGFMT_444P9_LE:
192 case IMGFMT_444P9_BE:
193 case IMGFMT_422P16_LE:
194 case IMGFMT_422P16_BE:
195 case IMGFMT_422P14_LE:
196 case IMGFMT_422P14_BE:
197 case IMGFMT_422P12_LE:
198 case IMGFMT_422P12_BE:
199 case IMGFMT_422P10_LE:
200 case IMGFMT_422P10_BE:
201 case IMGFMT_422P9_LE:
202 case IMGFMT_422P9_BE:
203 case IMGFMT_420P16_LE:
204 case IMGFMT_420P16_BE:
205 case IMGFMT_420P14_LE:
206 case IMGFMT_420P14_BE:
207 case IMGFMT_420P12_LE:
208 case IMGFMT_420P12_BE:
209 case IMGFMT_420P10_LE:
210 case IMGFMT_420P10_BE:
211 case IMGFMT_420P9_LE:
212 case IMGFMT_420P9_BE:
213 case IMGFMT_440P12_LE:
214 case IMGFMT_440P12_BE:
215 case IMGFMT_440P10_LE:
216 case IMGFMT_440P10_BE:
217 return;
218 case IMGFMT_Y16_LE:
219 case IMGFMT_Y16_BE:
220 mpi->bpp=16;
221 case IMGFMT_Y800:
222 case IMGFMT_Y8:
223 /* they're planar ones, but for easier handling use them as packed */
224 mpi->flags&=~MP_IMGFLAG_PLANAR;
225 mpi->num_planes=1;
226 return;
227 case IMGFMT_Y8A:
228 mpi->num_planes=2;
229 return;
230 case IMGFMT_UYVY:
231 mpi->flags|=MP_IMGFLAG_SWAPPED;
232 case IMGFMT_YUY2:
233 mpi->chroma_x_shift = 1;
234 mpi->bpp=16;
235 mpi->num_planes=1;
236 return;
237 case IMGFMT_NV12:
238 mpi->flags|=MP_IMGFLAG_SWAPPED;
239 case IMGFMT_NV21:
240 mpi->flags|=MP_IMGFLAG_PLANAR;
241 mpi->bpp=12;
242 mpi->num_planes=2;
243 mpi->chroma_width=(mpi->width>>0);
244 mpi->chroma_height=(mpi->height>>1);
245 mpi->chroma_x_shift=0;
246 mpi->chroma_y_shift=1;
247 return;
248 }
249 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"mp_image: unknown out_fmt: 0x%X\n",out_fmt);
250 mpi->bpp=0;
251 }
252
new_mp_image(int w,int h)253 mp_image_t* new_mp_image(int w,int h){
254 mp_image_t* mpi = malloc(sizeof(mp_image_t));
255 if(!mpi) return NULL; // error!
256 memset(mpi,0,sizeof(mp_image_t));
257 mpi->width=mpi->w=w;
258 mpi->height=mpi->h=h;
259 return mpi;
260 }
261
free_mp_image(mp_image_t * mpi)262 void free_mp_image(mp_image_t* mpi){
263 if(!mpi) return;
264 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
265 /* because we allocate the whole image at once */
266 av_free(mpi->planes[0]);
267 if (mpi->flags & MP_IMGFLAG_RGB_PALETTE)
268 av_free(mpi->planes[1]);
269 }
270 free(mpi);
271 }
272
273