cocoa_common: replace TickCount() with NSProcessInfo#systemUptime
[mplayer.git] / libmpcodecs / mp_image.c
blob2dd58a00863d84cadb12020ffcef7ea4432d3432
1 /*
2 * This file is part of MPlayer.
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.
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.
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.
19 #include "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "talloc.h"
27 #include "libmpcodecs/img_format.h"
28 #include "libmpcodecs/mp_image.h"
30 #include "libvo/fastmemcpy.h"
31 #include "libavutil/mem.h"
33 void mp_image_alloc_planes(mp_image_t *mpi) {
34 // IF09 - allocate space for 4. plane delta info - unused
35 if (mpi->imgfmt == IMGFMT_IF09) {
36 mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8+
37 mpi->chroma_width*mpi->chroma_height);
38 } else
39 mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8);
40 if (!mpi->planes[0])
41 abort(); //out of memory
42 if (mpi->flags&MP_IMGFLAG_PLANAR) {
43 int bpp = IMGFMT_IS_YUVP16(mpi->imgfmt)? 2 : 1;
44 // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
45 mpi->stride[0]=mpi->stride[3]=bpp*mpi->width;
46 if(mpi->num_planes > 2){
47 mpi->stride[1]=mpi->stride[2]=bpp*mpi->chroma_width;
48 if(mpi->flags&MP_IMGFLAG_SWAPPED){
49 // I420/IYUV (Y,U,V)
50 mpi->planes[1]=mpi->planes[0]+mpi->stride[0]*mpi->height;
51 mpi->planes[2]=mpi->planes[1]+mpi->stride[1]*mpi->chroma_height;
52 if (mpi->num_planes > 3)
53 mpi->planes[3]=mpi->planes[2]+mpi->stride[2]*mpi->chroma_height;
54 } else {
55 // YV12,YVU9,IF09 (Y,V,U)
56 mpi->planes[2]=mpi->planes[0]+mpi->stride[0]*mpi->height;
57 mpi->planes[1]=mpi->planes[2]+mpi->stride[1]*mpi->chroma_height;
58 if (mpi->num_planes > 3)
59 mpi->planes[3]=mpi->planes[1]+mpi->stride[1]*mpi->chroma_height;
61 } else {
62 // NV12/NV21
63 mpi->stride[1]=mpi->chroma_width;
64 mpi->planes[1]=mpi->planes[0]+mpi->stride[0]*mpi->height;
66 } else {
67 mpi->stride[0]=mpi->width*mpi->bpp/8;
68 if (mpi->flags & MP_IMGFLAG_RGB_PALETTE)
69 mpi->planes[1] = av_malloc(1024);
71 mpi->flags|=MP_IMGFLAG_ALLOCATED;
74 mp_image_t* alloc_mpi(int w, int h, unsigned long int fmt) {
75 mp_image_t* mpi = new_mp_image(w,h);
77 mp_image_setfmt(mpi,fmt);
78 mp_image_alloc_planes(mpi);
80 return mpi;
83 void copy_mpi(mp_image_t *dmpi, mp_image_t *mpi) {
84 if(mpi->flags&MP_IMGFLAG_PLANAR){
85 memcpy_pic(dmpi->planes[0],mpi->planes[0], mpi->w, mpi->h,
86 dmpi->stride[0],mpi->stride[0]);
87 memcpy_pic(dmpi->planes[1],mpi->planes[1], mpi->chroma_width, mpi->chroma_height,
88 dmpi->stride[1],mpi->stride[1]);
89 memcpy_pic(dmpi->planes[2], mpi->planes[2], mpi->chroma_width, mpi->chroma_height,
90 dmpi->stride[2],mpi->stride[2]);
91 } else {
92 memcpy_pic(dmpi->planes[0],mpi->planes[0],
93 mpi->w*(dmpi->bpp/8), mpi->h,
94 dmpi->stride[0],mpi->stride[0]);
98 void mp_image_setfmt(mp_image_t* mpi,unsigned int out_fmt){
99 mpi->flags&=~(MP_IMGFLAG_PLANAR|MP_IMGFLAG_YUV|MP_IMGFLAG_SWAPPED);
100 mpi->imgfmt=out_fmt;
101 // compressed formats
102 if(out_fmt == IMGFMT_MPEGPES || IMGFMT_IS_HWACCEL(out_fmt)){
103 mpi->bpp=0;
104 return;
106 mpi->num_planes=1;
107 if (IMGFMT_IS_RGB(out_fmt)) {
108 if (IMGFMT_RGB_DEPTH(out_fmt) < 8 && !(out_fmt&128))
109 mpi->bpp = IMGFMT_RGB_DEPTH(out_fmt);
110 else
111 mpi->bpp=(IMGFMT_RGB_DEPTH(out_fmt)+7)&(~7);
112 return;
114 if (IMGFMT_IS_BGR(out_fmt)) {
115 if (IMGFMT_BGR_DEPTH(out_fmt) < 8 && !(out_fmt&128))
116 mpi->bpp = IMGFMT_BGR_DEPTH(out_fmt);
117 else
118 mpi->bpp=(IMGFMT_BGR_DEPTH(out_fmt)+7)&(~7);
119 mpi->flags|=MP_IMGFLAG_SWAPPED;
120 return;
122 mpi->num_planes=3;
123 if (IMGFMT_IS_GBRP(out_fmt)) {
124 mpi->bpp = IMGFMT_RGB_DEPTH(out_fmt);
125 mpi->flags |= MP_IMGFLAG_PLANAR;
126 return;
128 mpi->flags|=MP_IMGFLAG_YUV;
129 if (mp_get_chroma_shift(out_fmt, NULL, NULL, NULL)) {
130 mpi->flags|=MP_IMGFLAG_PLANAR;
131 mpi->bpp = mp_get_chroma_shift(out_fmt, &mpi->chroma_x_shift, &mpi->chroma_y_shift, NULL);
132 mpi->chroma_width = mpi->width >> mpi->chroma_x_shift;
133 mpi->chroma_height = mpi->height >> mpi->chroma_y_shift;
135 switch(out_fmt){
136 case IMGFMT_I420:
137 case IMGFMT_IYUV:
138 mpi->flags|=MP_IMGFLAG_SWAPPED;
139 case IMGFMT_YV12:
140 return;
141 case IMGFMT_420A:
142 case IMGFMT_IF09:
143 mpi->num_planes=4;
144 case IMGFMT_YVU9:
145 case IMGFMT_444P:
146 case IMGFMT_422P:
147 case IMGFMT_411P:
148 case IMGFMT_440P:
149 case IMGFMT_444P16_LE:
150 case IMGFMT_444P16_BE:
151 case IMGFMT_444P10_LE:
152 case IMGFMT_444P10_BE:
153 case IMGFMT_444P9_LE:
154 case IMGFMT_444P9_BE:
155 case IMGFMT_422P16_LE:
156 case IMGFMT_422P16_BE:
157 case IMGFMT_422P10_LE:
158 case IMGFMT_422P10_BE:
159 case IMGFMT_422P9_LE:
160 case IMGFMT_422P9_BE:
161 case IMGFMT_420P16_LE:
162 case IMGFMT_420P16_BE:
163 case IMGFMT_420P10_LE:
164 case IMGFMT_420P10_BE:
165 case IMGFMT_420P9_LE:
166 case IMGFMT_420P9_BE:
167 return;
168 case IMGFMT_Y800:
169 case IMGFMT_Y8:
170 /* they're planar ones, but for easier handling use them as packed */
171 mpi->flags&=~MP_IMGFLAG_PLANAR;
172 mpi->num_planes=1;
173 return;
174 case IMGFMT_UYVY:
175 mpi->flags|=MP_IMGFLAG_SWAPPED;
176 case IMGFMT_YUY2:
177 mpi->bpp=16;
178 mpi->num_planes=1;
179 return;
180 case IMGFMT_NV12:
181 mpi->flags|=MP_IMGFLAG_SWAPPED;
182 case IMGFMT_NV21:
183 mpi->flags|=MP_IMGFLAG_PLANAR;
184 mpi->bpp=12;
185 mpi->num_planes=2;
186 mpi->chroma_width=(mpi->width>>0);
187 mpi->chroma_height=(mpi->height>>1);
188 mpi->chroma_x_shift=0;
189 mpi->chroma_y_shift=1;
190 return;
192 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"mp_image: unknown out_fmt: 0x%X\n",out_fmt);
193 mpi->bpp=0;
196 static int mp_image_destructor(void *ptr)
198 mp_image_t *mpi = ptr;
200 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
201 /* because we allocate the whole image at once */
202 av_free(mpi->planes[0]);
203 if (mpi->flags & MP_IMGFLAG_RGB_PALETTE)
204 av_free(mpi->planes[1]);
207 return 0;
210 mp_image_t* new_mp_image(int w,int h){
211 mp_image_t* mpi = talloc_zero(NULL, mp_image_t);
212 talloc_set_destructor(mpi, mp_image_destructor);
213 mpi->width=mpi->w=w;
214 mpi->height=mpi->h=h;
215 return mpi;
218 void free_mp_image(mp_image_t* mpi){
219 talloc_free(mpi);