File: image_format.c

package info (click to toggle)
zapping 0.10~cvs6-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 9,828 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,816; xml: 2,742; makefile: 1,282; perl: 488
file content (259 lines) | stat: -rw-r--r-- 6,117 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 *  Copyright (C) 2001-2004 Michael H. Schimek
 *  Copyright (C) 2000-2003 Iaki Garca Etxebarria
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: image_format.c,v 1.20 2006/05/17 18:02:21 mschimek Exp $ */

#include <string.h>		/* memset() */
#include <assert.h>
#include "misc.h"
#include "image_format.h"

void
_tv_image_format_dump		(const tv_image_format *format,
				 FILE *			fp)
{
	assert (NULL != format);

	fprintf (fp, "width=%u height=%u "
		 "offset=%lu,%lu,%lu,%lu bpl=%lu,%lu,%lu,%lu "
		 "size=%lu pixfmt=%s",
		 format->width,
		 format->height,
		 format->offset[0],
		 format->offset[1],
		 format->offset[2],
		 format->offset[3],
		 format->bytes_per_line[0],
		 format->bytes_per_line[1],
		 format->bytes_per_line[2],
		 format->bytes_per_line[3],
		 format->size,
		 format->pixel_format->name);
}

tv_bool
tv_image_format_init		(tv_image_format *	format,
				 unsigned int		width,
				 unsigned int		height,
				 unsigned long		bytes_per_line,
				 tv_pixfmt		pixfmt,
				 tv_colspc		colspc)
{
	const tv_pixel_format *pf;
	unsigned long min_bpl;

	assert (NULL != format);

	if (!(pf = tv_pixel_format_from_pixfmt (pixfmt)))
		return FALSE;

	if (0 == width || 0 == height)
		return FALSE;

	width = (width + pf->hmask) & ~pf->hmask;
	height = (height + pf->vmask) & ~pf->vmask;

	min_bpl = (width * pf->bits_per_pixel + 7) >> 3;

	if (0 == bytes_per_line) {
		bytes_per_line = min_bpl;
	} else if (bytes_per_line < min_bpl) {
		return FALSE;
	}

	format->width = width;
	format->height = height;

	format->bytes_per_line[0] = bytes_per_line;

	format->offset[0] = 0;

	if (pf->n_planes > 1) {
		unsigned long uv_bpl;
		unsigned long y_size;
		unsigned long uv_size;

		/* No padding. */
		uv_bpl = format->bytes_per_line[0] >> pf->uv_hshift;

		y_size = format->bytes_per_line[0] * height;
		uv_size = y_size >> (pf->uv_hshift + pf->uv_vshift);

		switch (pixfmt) {
		case TV_PIXFMT_NV12:
		case TV_PIXFMT_HM12:
			format->bytes_per_line[1] = uv_bpl;
			format->bytes_per_line[2] = 0;
			format->bytes_per_line[3] = 0;

			format->offset[1] = y_size;
			format->offset[2] = 0;
			format->offset[3] = 0;

			format->size = y_size + uv_size;

			break;

		default:
			format->bytes_per_line[1] = uv_bpl;
			format->bytes_per_line[2] = uv_bpl;
			format->bytes_per_line[3] = 0;

			if (pf->vu_order) {
				format->offset[1] = y_size + uv_size;
				format->offset[2] = y_size; 
			} else {
				format->offset[1] = y_size; 
				format->offset[2] = y_size + uv_size;
			}

			format->offset[3] = 0;

			format->size = y_size + uv_size * 2;

			break;
		}
	} else {
		format->bytes_per_line[1] = 0;
		format->bytes_per_line[2] = 0;
		format->bytes_per_line[3] = 0;

		format->offset[1] = 0;
		format->offset[2] = 0;
		format->offset[3] = 0;

		format->size = format->bytes_per_line[0] * height;
	}

	format->pixel_format = tv_pixel_format_from_pixfmt (pixfmt);
	format->colspc = colspc;

	return TRUE;
}

tv_bool
tv_image_format_is_valid	(const tv_image_format *format)
{
	const tv_pixel_format *pf;
	unsigned long min_size[3];
	unsigned long min_bpl;

	assert (NULL != format);

	pf = format->pixel_format;

	if (0 == format->width
	    || 0 == format->height)
		return FALSE;

	if (0 != ((format->width & pf->hmask)
		  | (format->height & pf->vmask)))
		return FALSE;

	min_bpl = (format->width * pf->bits_per_pixel + 7) >> 3;

	if (format->bytes_per_line[0] < min_bpl)
		return FALSE;

	/* We don't enforce bytes_per_line padding on the last
	   line, in case offset and bytes_per_line were adjusted
	   for cropping. */
	min_size[0] = format->bytes_per_line[0] * (format->height - 1) + min_bpl;

	if (pf->n_planes > 1) {
		unsigned int order[3];
		unsigned long min_uv_bpl;

		/* U and V bits_per_pixel assumed 8. */
		min_uv_bpl = format->width >> pf->uv_hshift;

		if (format->bytes_per_line[1] < min_uv_bpl)
			return FALSE;

		/* We don't enforce bytes_per_line padding
		   on the last line. */
		min_size[1] = format->bytes_per_line[1]
			* ((format->height >> pf->uv_vshift) - 1)
			+ min_uv_bpl;

		order[0] = (format->offset[1] < format->offset[0]);
		order[1] = order[0] ^ 1;
		order[2] = 2;

		if (pf->n_planes > 2) {
			if (format->bytes_per_line[2] < min_uv_bpl)
				return FALSE;

			min_size[2] = format->bytes_per_line[2]
				* ((format->height >> pf->uv_vshift) - 1)
				+ min_uv_bpl;

			if (format->offset[2] < format->offset[order[1]]) {
				order[2] = order[1];
				if (format->offset[2] < format->offset[order[0]]) {
					order[1] = order[0];
					order[0] = 2;
				} else {
					order[1] = 2;
				}
			}

			/* Enforce plane order, planes must not overlap and
			   must fit in buffer. */
			if (format->offset[order[2]] + min_size[order[2]]
			    > format->size)
				return FALSE;
		}

		if (format->offset[order[1]] + min_size[order[1]]
		    > format->offset[order[2]])
			return FALSE;

		if (format->offset[order[0]] + min_size[order[0]]
		    > format->offset[order[1]])
			return FALSE;
	} else {
		if (format->offset[0] + min_size[0] > format->size)
			return FALSE;
	}

	return TRUE;
}

void *
tv_new_image			(const void *		src_image,
				 const tv_image_format *src_format)
{
	void *dst_image;

	assert (NULL != src_format);
	assert (src_format->size > 0);

	dst_image = malloc (src_format->size);

	if (dst_image && src_image) {
		if (!tv_copy_image (dst_image, src_format,
				    src_image, src_format)) {
			free (dst_image);
			dst_image = NULL;
		}
	}

	return dst_image;
}