4 * Copyright (C) Georg Martius - Feb - 2013
5 * georg dot martius at web dot de
7 * This file is part of vid.stab video stabilization library
9 * vid.stab is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License,
11 * as published by the Free Software Foundation; either version 2, or
12 * (at your option) any later version.
14 * vid.stab is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Make; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "frameinfo.h"
26 #include "vidstabdefines.h"
30 int vsFrameInfoInit(VSFrameInfo
* fi
, int width
, int height
, VSPixelFormat pFormat
){
38 assert(width
%2==0 && height
%2==0);
86 int vsGetPlaneWidthSubS(const VSFrameInfo
* fi
, int plane
){
87 return plane
== 1 || plane
== 2 ? fi
->log2ChromaW
: 0;
90 int vsGetPlaneHeightSubS(const VSFrameInfo
* fi
, int plane
){
91 return plane
== 1 || plane
== 2 ? fi
->log2ChromaH
: 0;
94 int vsFrameIsNull(const VSFrame
* frame
) {
95 return frame
==0 || frame
->data
[0]==0;
99 int vsFramesEqual(const VSFrame
* frame1
,const VSFrame
* frame2
){
100 return frame1
&& frame2
&& (frame1
==frame2
|| frame1
->data
[0] == frame2
->data
[0]);
103 void vsFrameNull(VSFrame
* frame
){
104 memset(frame
->data
,0,sizeof(uint8_t*)*4);
105 memset(frame
->linesize
,0,sizeof(int)*4);
108 void vsFrameAllocate(VSFrame
* frame
, const VSFrameInfo
* fi
){
110 if(fi
->pFormat
<PF_PACKED
){
112 assert(fi
->planes
> 0 && fi
->planes
<= 4);
113 for (i
=0; i
< fi
->planes
; i
++){
114 int w
= fi
->width
>> vsGetPlaneWidthSubS(fi
, i
);
115 int h
= fi
->height
>> vsGetPlaneHeightSubS(fi
, i
);
116 frame
->data
[i
] = vs_zalloc(w
* h
* sizeof(uint8_t));
117 frame
->linesize
[i
] = w
;
118 if(frame
->data
[i
]==0)
119 vs_log_error("vid.stab","out of memory: cannot allocated buffer");
122 assert(fi
->planes
==1);
125 frame
->data
[0] = vs_zalloc(w
* h
* sizeof(uint8_t)*fi
->bytesPerPixel
);
126 frame
->linesize
[0] = w
* fi
->bytesPerPixel
;
127 if(frame
->data
[0]==0)
128 vs_log_error("vid.stab","out of memory: cannot allocated buffer");
132 void vsFrameCopyPlane(VSFrame
* dest
, const VSFrame
* src
,
133 const VSFrameInfo
* fi
, int plane
){
134 assert(src
->data
[plane
]);
135 int h
= fi
->height
>> vsGetPlaneHeightSubS(fi
, plane
);
136 if(src
->linesize
[plane
] == dest
->linesize
[plane
])
137 memcpy(dest
->data
[plane
], src
->data
[plane
], src
->linesize
[plane
] * h
* sizeof(uint8_t));
139 uint8_t* d
= dest
->data
[plane
];
140 const uint8_t* s
= src
->data
[plane
];
141 int w
= fi
->width
>> vsGetPlaneWidthSubS(fi
, plane
);
143 memcpy(d
,s
,sizeof(uint8_t) * w
);
144 d
+= dest
->linesize
[plane
];
145 s
+= src
->linesize
[plane
];
150 void vsFrameCopy(VSFrame
* dest
, const VSFrame
* src
, const VSFrameInfo
* fi
){
152 assert(fi
->planes
> 0 && fi
->planes
<= 4);
153 for (plane
=0; plane
< fi
->planes
; plane
++){
154 vsFrameCopyPlane(dest
,src
,fi
,plane
);
158 void vsFrameFillFromBuffer(VSFrame
* frame
, uint8_t* img
, const VSFrameInfo
* fi
){
159 assert(fi
->planes
> 0 && fi
->planes
<= 4);
163 for (i
=0; i
< fi
->planes
; i
++){
164 int w
= fi
->width
>> vsGetPlaneWidthSubS(fi
, i
);
165 int h
= fi
->height
>> vsGetPlaneHeightSubS(fi
, i
);
166 frame
->data
[i
] = img
+ offset
;
167 frame
->linesize
[i
] = w
*fi
->bytesPerPixel
;
168 offset
+= h
* w
*fi
->bytesPerPixel
;
172 void vsFrameFree(VSFrame
* frame
){
174 for (plane
=0; plane
< 4; plane
++){
175 if(frame
->data
[plane
]) vs_free(frame
->data
[plane
]);
176 frame
->data
[plane
]=0;
177 frame
->linesize
[plane
]=0;
184 * c-file-style: "stroustrup"
185 * c-file-offsets: ((case-label . *) (statement-case-intro . *))
186 * indent-tabs-mode: nil
187 * c-basic-offset: 2 t
190 * vim: expandtab shiftwidth=2: