Mercurial > mplayer.hg
comparison libmpdemux/yuv4mpeg.h @ 3786:7ebf504c92d6
yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
author | arpi |
---|---|
date | Thu, 27 Dec 2001 02:08:31 +0000 |
parents | |
children | 9cdbcd86c176 |
comparison
equal
deleted
inserted
replaced
3785:44c74b600573 | 3786:7ebf504c92d6 |
---|---|
1 /* | |
2 * yuv4mpeg.h: Functions for reading and writing "new" YUV4MPEG2 streams. | |
3 * | |
4 * Stream format is described at the end of this file. | |
5 * | |
6 * | |
7 * Copyright (C) 2001 Matthew J. Marjanovic <maddog@mir.com> | |
8 * | |
9 * This file is ripped from the lavtools package (mjpeg.sourceforge.net) | |
10 * Ported to mplayer by Rik Snel <snel@phys.uu.nl> | |
11 * | |
12 * This program is free software; you can redistribute it and/or | |
13 * modify it under the terms of the GNU General Public License | |
14 * as published by the Free Software Foundation; either version 2 | |
15 * of the License, or (at your option) any later version. | |
16 * | |
17 * This program is distributed in the hope that it will be useful, | |
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 * GNU General Public License for more details. | |
21 * | |
22 * You should have received a copy of the GNU General Public License | |
23 * along with this program; if not, write to the Free Software | |
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 */ | |
26 | |
27 #ifndef __YUV4MPEG_H__ | |
28 #define __YUV4MPEG_H__ | |
29 | |
30 #include <stdlib.h> | |
31 //#include "mp_msg.h" | |
32 #include "stream.h" | |
33 | |
34 | |
35 /************************************************************************ | |
36 * error codes returned by y4m_* functions | |
37 ************************************************************************/ | |
38 #define Y4M_OK 0 | |
39 #define Y4M_ERR_RANGE 1 | |
40 #define Y4M_ERR_SYSTEM 2 | |
41 #define Y4M_ERR_HEADER 3 | |
42 #define Y4M_ERR_BADTAG 4 | |
43 #define Y4M_ERR_MAGIC 5 | |
44 #define Y4M_ERR_EOF 6 | |
45 #define Y4M_ERR_XXTAGS 7 | |
46 | |
47 | |
48 /* generic 'unknown' value for integer parameters (e.g. interlace, height) */ | |
49 #define Y4M_UNKNOWN -1 | |
50 | |
51 | |
52 | |
53 /************************************************************************ | |
54 * 'ratio' datatype, for rational numbers | |
55 * (see 'ratio' functions down below) | |
56 ************************************************************************/ | |
57 typedef struct _y4m_ratio { | |
58 int n; /* numerator */ | |
59 int d; /* denominator */ | |
60 } y4m_ratio_t; | |
61 | |
62 | |
63 /************************************************************************ | |
64 * useful standard framerates (as ratios) | |
65 ************************************************************************/ | |
66 extern const y4m_ratio_t y4m_fps_UNKNOWN; | |
67 extern const y4m_ratio_t y4m_fps_NTSC_FILM; /* 24000/1001 film (in NTSC) */ | |
68 extern const y4m_ratio_t y4m_fps_FILM; /* 24fps film */ | |
69 extern const y4m_ratio_t y4m_fps_PAL; /* 25fps PAL */ | |
70 extern const y4m_ratio_t y4m_fps_NTSC; /* 30000/1001 NTSC */ | |
71 extern const y4m_ratio_t y4m_fps_30; /* 30fps */ | |
72 extern const y4m_ratio_t y4m_fps_PAL_FIELD; /* 50fps PAL field rate */ | |
73 extern const y4m_ratio_t y4m_fps_NTSC_FIELD; /* 60000/1001 NTSC field rate */ | |
74 extern const y4m_ratio_t y4m_fps_60; /* 60fps */ | |
75 | |
76 /************************************************************************ | |
77 * useful standard sample (pixel) aspect ratios | |
78 ************************************************************************/ | |
79 extern const y4m_ratio_t y4m_sar_UNKNOWN; | |
80 extern const y4m_ratio_t y4m_sar_SQUARE; /* square pixels */ | |
81 extern const y4m_ratio_t y4m_sar_NTSC_CCIR601; /* 525-line (NTSC) Rec.601 */ | |
82 extern const y4m_ratio_t y4m_sar_NTSC_16_9; /* 16:9 NTSC/Rec.601 */ | |
83 extern const y4m_ratio_t y4m_sar_NTSC_SVCD_4_3; /* NTSC SVCD 4:3 */ | |
84 extern const y4m_ratio_t y4m_sar_NTSC_SVCD_16_9;/* NTSC SVCD 16:9 */ | |
85 extern const y4m_ratio_t y4m_sar_PAL_CCIR601; /* 625-line (PAL) Rec.601 */ | |
86 extern const y4m_ratio_t y4m_sar_PAL_16_9; /* 16:9 PAL/Rec.601 */ | |
87 extern const y4m_ratio_t y4m_sar_PAL_SVCD_4_3; /* PAL SVCD 4:3 */ | |
88 extern const y4m_ratio_t y4m_sar_PAL_SVCD_16_9; /* PAL SVCD 16:9 */ | |
89 | |
90 | |
91 /************************************************************************ | |
92 * 'xtag_list' --- list of unparsed and/or meta/X header tags | |
93 * | |
94 * Do not touch this structure directly! | |
95 * | |
96 * Use the y4m_xtag_*() functions (see below). | |
97 * You must initialize/finalize this structure before/after use. | |
98 ************************************************************************/ | |
99 #define Y4M_MAX_XTAGS 32 /* maximum number of xtags in list */ | |
100 #define Y4M_MAX_XTAG_SIZE 32 /* max length of an xtag (including 'X') */ | |
101 typedef struct _y4m_xtag_list { | |
102 int count; | |
103 char *tags[Y4M_MAX_XTAGS]; | |
104 } y4m_xtag_list_t; | |
105 | |
106 | |
107 | |
108 /************************************************************************ | |
109 * 'stream_info' --- stream header information | |
110 * | |
111 * Do not touch this structure directly! | |
112 * | |
113 * Use the y4m_si_*() functions (see below). | |
114 * You must initialize/finalize this structure before/after use. | |
115 ************************************************************************/ | |
116 typedef struct _y4m_stream_info { | |
117 /* values from header */ | |
118 int width; | |
119 int height; | |
120 int interlace; /* see Y4M_ILACE_* definitions below */ | |
121 y4m_ratio_t framerate; /* frames-per-second; 0:0 == unknown */ | |
122 y4m_ratio_t sampleaspect; /* pixel width/height; 0:0 == unknown */ | |
123 /* computed/derivative values */ | |
124 int framelength; /* bytes of data per frame (not including header) */ | |
125 /* mystical X tags */ | |
126 y4m_xtag_list_t x_tags; | |
127 } y4m_stream_info_t; | |
128 | |
129 /* possible options for the interlace parameter */ | |
130 #define Y4M_ILACE_NONE 0 /* non-interlaced, progressive frame */ | |
131 #define Y4M_ILACE_TOP_FIRST 1 /* interlaced, top-field first */ | |
132 #define Y4M_ILACE_BOTTOM_FIRST 2 /* interlaced, bottom-field first */ | |
133 | |
134 | |
135 /************************************************************************ | |
136 * 'frame_info' --- frame header information | |
137 * | |
138 * Do not touch this structure directly! | |
139 * | |
140 * Use the y4m_fi_*() functions (see below). | |
141 * You must initialize/finalize this structure before/after use. | |
142 ************************************************************************/ | |
143 typedef struct _y4m_frame_info { | |
144 /* mystical X tags */ | |
145 y4m_xtag_list_t x_tags; | |
146 } y4m_frame_info_t; | |
147 | |
148 | |
149 | |
150 #ifdef __cplusplus | |
151 extern "C" { | |
152 #else | |
153 #endif | |
154 | |
155 | |
156 /************************************************************************ | |
157 * 'ratio' functions | |
158 ************************************************************************/ | |
159 | |
160 /* 'normalize' a ratio (remove common factors) */ | |
161 void y4m_ratio_reduce(y4m_ratio_t *r); | |
162 | |
163 /* parse "nnn:ddd" into a ratio (returns Y4M_OK or Y4M_ERR_RANGE) */ | |
164 int y4m_parse_ratio(y4m_ratio_t *r, const char *s); | |
165 | |
166 /* quick test of two ratios for equality (i.e. identical components) */ | |
167 #define Y4M_RATIO_EQL(a,b) ( ((a).n == (b).n) && ((a).d == (b).d) ) | |
168 | |
169 /* quick conversion of a ratio to a double (no divide-by-zero check!) */ | |
170 #define Y4M_RATIO_DBL(r) ((double)(r).n / (double)(r).d) | |
171 | |
172 | |
173 | |
174 /************************************************************************ | |
175 * 'xtag' functions | |
176 * | |
177 * o Before using an xtag_list (but after the structure/memory has been | |
178 * allocated), you must initialize it via y4m_init_xtag_list(). | |
179 * o After using an xtag_list (but before the structure is released), | |
180 * call y4m_fini_xtag_list() to free internal memory. | |
181 * | |
182 ************************************************************************/ | |
183 | |
184 /* initialize an xtag_list structure */ | |
185 void y4m_init_xtag_list(y4m_xtag_list_t *xtags); | |
186 | |
187 /* finalize an xtag_list structure */ | |
188 void y4m_fini_xtag_list(y4m_xtag_list_t *xtags); | |
189 | |
190 /* make one xtag_list into a copy of another */ | |
191 void y4m_copy_xtag_list(y4m_xtag_list_t *dest, const y4m_xtag_list_t *src); | |
192 | |
193 /* return number of tags in an xtag_list */ | |
194 int y4m_xtag_count(const y4m_xtag_list_t *xtags); | |
195 | |
196 /* access n'th tag in an xtag_list */ | |
197 const char *y4m_xtag_get(const y4m_xtag_list_t *xtags, int n); | |
198 | |
199 /* append a new tag to an xtag_list | |
200 returns: Y4M_OK - success | |
201 Y4M_ERR_XXTAGS - list is already full */ | |
202 int y4m_xtag_add(y4m_xtag_list_t *xtags, const char *tag); | |
203 | |
204 /* remove a tag from an xtag_list | |
205 returns: Y4M_OK - success | |
206 Y4M_ERR_RANGE - n is out of range */ | |
207 int y4m_xtag_remove(y4m_xtag_list_t *xtags, int n); | |
208 | |
209 /* remove all tags from an xtag_list | |
210 returns: Y4M_OK - success */ | |
211 int y4m_xtag_clearlist(y4m_xtag_list_t *xtags); | |
212 | |
213 /* append copies of tags from src list to dest list | |
214 returns: Y4M_OK - success | |
215 Y4M_ERR_XXTAGS - operation would overfill dest list */ | |
216 int y4m_xtag_addlist(y4m_xtag_list_t *dest, const y4m_xtag_list_t *src); | |
217 | |
218 | |
219 | |
220 /************************************************************************ | |
221 * '*_info' functions | |
222 * | |
223 * o Before using a *_info structure (but after the structure/memory has | |
224 * been allocated), you must initialize it via y4m_init_*_info(). | |
225 * o After using a *_info structure (but before the structure is released), | |
226 * call y4m_fini_*_info() to free internal memory. | |
227 * o Use the 'set' and 'get' accessors to modify or access the fields in | |
228 * the structures; don't touch the structure directly. (Ok, so there | |
229 * is no really convenient C syntax to prevent you from doing this, | |
230 * but we are all responsible programmers here, so just don't do it!) | |
231 * | |
232 ************************************************************************/ | |
233 | |
234 /* initialize a stream_info structure */ | |
235 void y4m_init_stream_info(y4m_stream_info_t *i); | |
236 | |
237 /* finalize a stream_info structure */ | |
238 void y4m_fini_stream_info(y4m_stream_info_t *i); | |
239 | |
240 /* make one stream_info into a copy of another */ | |
241 void y4m_copy_stream_info(y4m_stream_info_t *dest, y4m_stream_info_t *src); | |
242 | |
243 /* access or set stream_info fields */ | |
244 void y4m_si_set_width(y4m_stream_info_t *si, int width); | |
245 int y4m_si_get_width(y4m_stream_info_t *si); | |
246 void y4m_si_set_height(y4m_stream_info_t *si, int height); | |
247 int y4m_si_get_height(y4m_stream_info_t *si); | |
248 void y4m_si_set_interlace(y4m_stream_info_t *si, int interlace); | |
249 int y4m_si_get_interlace(y4m_stream_info_t *si); | |
250 void y4m_si_set_framerate(y4m_stream_info_t *si, y4m_ratio_t framerate); | |
251 y4m_ratio_t y4m_si_get_framerate(y4m_stream_info_t *si); | |
252 void y4m_si_set_sampleaspect(y4m_stream_info_t *si, y4m_ratio_t sar); | |
253 y4m_ratio_t y4m_si_get_sampleaspect(y4m_stream_info_t *si); | |
254 int y4m_si_get_framelength(y4m_stream_info_t *si); | |
255 | |
256 /* access stream_info xtag_list */ | |
257 y4m_xtag_list_t *y4m_si_xtags(y4m_stream_info_t *si); | |
258 | |
259 | |
260 /* initialize a frame_info structure */ | |
261 void y4m_init_frame_info(y4m_frame_info_t *i); | |
262 | |
263 /* finalize a frame_info structure */ | |
264 void y4m_fini_frame_info(y4m_frame_info_t *i); | |
265 | |
266 /* make one frame_info into a copy of another */ | |
267 void y4m_copy_frame_info(y4m_frame_info_t *dest, y4m_frame_info_t *src); | |
268 | |
269 /* access frame_info xtag_list */ | |
270 y4m_xtag_list_t *y4m_fi_xtags(y4m_frame_info_t *fi); | |
271 | |
272 | |
273 | |
274 /************************************************************************ | |
275 * blocking read and write functions | |
276 * | |
277 * o guaranteed to transfer entire payload (or fail) | |
278 * o return values: | |
279 * 0 (zero) complete success | |
280 * -(# of remaining bytes) error (and errno left set) | |
281 * +(# of remaining bytes) EOF (for y4m_read only) | |
282 * | |
283 ************************************************************************/ | |
284 | |
285 /* read len bytes from fd into buf */ | |
286 ssize_t y4m_read(stream_t *s, char *buf, size_t len); | |
287 | |
288 #if 0 | |
289 /* write len bytes from fd into buf */ | |
290 ssize_t y4m_write(int fd, char *buf, size_t len); | |
291 #endif | |
292 | |
293 | |
294 /************************************************************************ | |
295 * stream header processing functions | |
296 * | |
297 * o return values: | |
298 * Y4M_OK - success | |
299 * Y4M_ERR_* - error (see y4m_strerr() for descriptions) | |
300 * | |
301 ************************************************************************/ | |
302 | |
303 /* parse a string of stream header tags */ | |
304 int y4m_parse_stream_tags(char *s, y4m_stream_info_t *i); | |
305 | |
306 /* read a stream header from file descriptor fd */ | |
307 int y4m_read_stream_header(stream_t *s, y4m_stream_info_t *i); | |
308 | |
309 #if 0 | |
310 /* write a stream header to file descriptor fd */ | |
311 int y4m_write_stream_header(int fd, y4m_stream_info_t *i); | |
312 #endif | |
313 | |
314 | |
315 /************************************************************************ | |
316 * frame processing functions | |
317 * | |
318 * o return values: | |
319 * Y4M_OK - success | |
320 * Y4M_ERR_* - error (see y4m_strerr() for descriptions) | |
321 * | |
322 ************************************************************************/ | |
323 | |
324 /* read a frame header from file descriptor fd */ | |
325 int y4m_read_frame_header(stream_t *s, y4m_frame_info_t *i); | |
326 | |
327 #if 0 | |
328 /* write a frame header to file descriptor fd */ | |
329 int y4m_write_frame_header(int fd, y4m_frame_info_t *i); | |
330 #endif | |
331 | |
332 /* read a complete frame (header + data) | |
333 o yuv[3] points to three buffers, one each for Y, U, V planes */ | |
334 int y4m_read_frame(stream_t *s, y4m_stream_info_t *si, | |
335 y4m_frame_info_t *fi, unsigned char *yuv[3]); | |
336 | |
337 #if 0 | |
338 /* write a complete frame (header + data) | |
339 o yuv[3] points to three buffers, one each for Y, U, V planes */ | |
340 int y4m_write_frame(int fd, y4m_stream_info_t *si, | |
341 y4m_frame_info_t *fi, unsigned char *yuv[3]); | |
342 #endif | |
343 | |
344 #if 0 | |
345 /* read a complete frame (header + data), but de-interleave fields | |
346 into two separate buffers | |
347 o upper_field[3] same as yuv[3] above, but for upper field | |
348 o lower_field[3] same as yuv[3] above, but for lower field | |
349 */ | |
350 int y4m_read_fields(int fd, y4m_stream_info_t *si, y4m_frame_info_t *fi, | |
351 unsigned char *upper_field[3], | |
352 unsigned char *lower_field[3]); | |
353 | |
354 /* write a complete frame (header + data), but interleave fields | |
355 from two separate buffers | |
356 o upper_field[3] same as yuv[3] above, but for upper field | |
357 o lower_field[3] same as yuv[3] above, but for lower field | |
358 */ | |
359 int y4m_write_fields(int fd, y4m_stream_info_t *si, y4m_frame_info_t *fi, | |
360 unsigned char *upper_field[3], | |
361 unsigned char *lower_field[3]); | |
362 | |
363 #endif | |
364 | |
365 /************************************************************************ | |
366 * miscellaneous functions | |
367 ************************************************************************/ | |
368 | |
369 /* convenient dump of stream header info via mjpeg_log facility | |
370 * - each logged/printed line is prefixed by 'prefix' | |
371 */ | |
372 void y4m_log_stream_info(const char *prefix, y4m_stream_info_t *i); | |
373 | |
374 /* convert a Y4M_ERR_* error code into mildly explanatory string */ | |
375 const char *y4m_strerr(int err); | |
376 | |
377 /* set 'allow_unknown_tag' flag for library... | |
378 o yn = 0 : unknown header tags will produce a parsing error | |
379 o yn = 1 : unknown header tags/values will produce a warning, but | |
380 are otherwise passed along via the xtags list | |
381 o yn = -1: don't change, just return current setting | |
382 | |
383 return value: previous setting of flag | |
384 */ | |
385 int y4m_allow_unknown_tags(int yn); | |
386 | |
387 | |
388 #ifdef __cplusplus | |
389 } | |
390 #endif | |
391 | |
392 /************************************************************************ | |
393 ************************************************************************ | |
394 | |
395 Description of the (new!, forever?) YUV4MPEG2 stream format: | |
396 | |
397 STREAM consists of | |
398 o one '\n' terminated STREAM-HEADER | |
399 o unlimited number of FRAMEs | |
400 | |
401 FRAME consists of | |
402 o one '\n' terminated FRAME-HEADER | |
403 o "length" octets of planar YCrCb 4:2:0 image data | |
404 (if frame is interlaced, then the two fields are interleaved) | |
405 | |
406 | |
407 STREAM-HEADER consists of | |
408 o string "YUV4MPEG2 " (note the space after the '2') | |
409 o unlimited number of ' ' separated TAGGED-FIELDs | |
410 o '\n' line terminator | |
411 | |
412 FRAME-HEADER consists of | |
413 o string "FRAME " (note the space after the 'E') | |
414 o unlimited number of ' ' separated TAGGED-FIELDs | |
415 o '\n' line terminator | |
416 | |
417 | |
418 TAGGED-FIELD consists of | |
419 o single ascii character tag | |
420 o VALUE (which does not contain whitespace) | |
421 | |
422 VALUE consists of | |
423 o integer (base 10 ascii representation) | |
424 or o RATIO | |
425 or o single ascii character | |
426 or o generic ascii string | |
427 | |
428 RATIO consists of | |
429 o numerator (integer) | |
430 o ':' (a colon) | |
431 o denominator (integer) | |
432 | |
433 | |
434 The currently supported tags for the STREAM-HEADER: | |
435 W - [integer] frame width, pixels, should be > 0 | |
436 H - [integer] frame height, pixels, should be > 0 | |
437 I - [char] interlacing: p - progressive (none) | |
438 t - top-field-first | |
439 b - bottom-field-first | |
440 ? - unknown | |
441 F - [ratio] frame-rate, 0:0 == unknown | |
442 A - [ratio] sample (pixel) aspect ratio, 0:0 == unknown | |
443 X - [character string] 'metadata' (unparsed, but passed around) | |
444 | |
445 The currently supported tags for the FRAME-HEADER: | |
446 X - character string 'metadata' (unparsed, but passed around) | |
447 | |
448 ************************************************************************ | |
449 ************************************************************************/ | |
450 | |
451 #endif /* __YUV4MPEG_H__ */ | |
452 | |
453 |