comparison cyuv.c @ 1054:f874e2122d45 libavcodec

Creative YUV (CYUV) decoder by (Mike Melanson <melanson at pcisys dot net>)
author michaelni
date Sun, 09 Feb 2003 00:37:50 +0000
parents
children b32afefe7d33
comparison
equal deleted inserted replaced
1053:f07fd48c23d4 1054:f874e2122d45
1 /*
2 *
3 * Copyright (C) 2003 the ffmpeg project
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Creative YUV (CYUV) Video Decoder
20 * by Mike Melanson (melanson@pcisys.net)
21 * based on "Creative YUV (CYUV) stream format for AVI":
22 * http://www.csse.monash.edu.au/~timf/videocodec/cyuv.txt
23 *
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "common.h"
32 #include "avcodec.h"
33 #include "dsputil.h"
34 #include "mpegvideo.h"
35 #include "bswap.h"
36
37
38 typedef struct CyuvDecodeContext {
39 AVCodecContext *avctx;
40 int width, height;
41 AVFrame frame;
42 } CyuvDecodeContext;
43
44 static int cyuv_decode_init(AVCodecContext *avctx)
45 {
46 CyuvDecodeContext *s = avctx->priv_data;
47
48 s->avctx = avctx;
49 s->width = avctx->width;
50 s->height = avctx->height;
51 avctx->pix_fmt = PIX_FMT_YUV411P;
52 avctx->has_b_frames = 0;
53
54 return 0;
55 }
56
57 static int cyuv_decode_frame(AVCodecContext *avctx,
58 void *data, int *data_size,
59 UINT8 *buf, int buf_size)
60 {
61 CyuvDecodeContext *s=avctx->priv_data;
62
63 unsigned char *y_plane;
64 unsigned char *u_plane;
65 unsigned char *v_plane;
66 int y_ptr;
67 int u_ptr;
68 int v_ptr;
69
70 /* prediction error tables (make it clear that they are signed values) */
71 signed char *y_table = buf + 0;
72 signed char *u_table = buf + 16;
73 signed char *v_table = buf + 32;
74
75 unsigned char y_pred, u_pred, v_pred;
76 int stream_ptr;
77 unsigned char cur_byte;
78 int pixel_groups;
79
80 *data_size = 0;
81
82 /* sanity check the buffer size: A buffer has 3x16-bytes tables
83 * followed by (height) lines each with 3 bytes to represent groups
84 * of 4 pixels. Thus, the total size of the buffer ought to be:
85 * (3 * 16) + height * (width * 3 / 4) */
86 if (buf_size != 48 + s->height * (s->width * 3 / 4)) {
87 printf ("ffmpeg: cyuv: got a buffer with %d bytes when %d were expected\n",
88 buf_size,
89 48 + s->height * (s->width * 3 / 4));
90 return -1;
91 }
92
93 /* pixel data starts 48 bytes in, after 3x16-byte tables */
94 stream_ptr = 48;
95
96 s->frame.reference = 0;
97 if(avctx->get_buffer(avctx, &s->frame) < 0) {
98 fprintf(stderr, "get_buffer() failed\n");
99 return -1;
100 }
101
102 y_plane = s->frame.data[0];
103 u_plane = s->frame.data[1];
104 v_plane = s->frame.data[2];
105
106 /* iterate through each line in the height */
107 for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
108 y_ptr < (s->height * s->frame.linesize[0]);
109 y_ptr += s->frame.linesize[0] - s->width,
110 u_ptr += s->frame.linesize[1] - s->width / 4,
111 v_ptr += s->frame.linesize[2] - s->width / 4) {
112
113 /* reset predictors */
114 cur_byte = buf[stream_ptr++];
115 u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
116 y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
117
118 cur_byte = buf[stream_ptr++];
119 v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
120 y_pred += y_table[cur_byte & 0x0F];
121 y_plane[y_ptr++] = y_pred;
122
123 cur_byte = buf[stream_ptr++];
124 y_pred += y_table[cur_byte & 0x0F];
125 y_plane[y_ptr++] = y_pred;
126 y_pred += y_table[(cur_byte & 0xF0) >> 4];
127 y_plane[y_ptr++] = y_pred;
128
129 /* iterate through the remaining pixel groups (4 pixels/group) */
130 pixel_groups = s->width / 4 - 1;
131 while (pixel_groups--) {
132
133 cur_byte = buf[stream_ptr++];
134 u_pred += u_table[(cur_byte & 0xF0) >> 4];
135 u_plane[u_ptr++] = u_pred;
136 y_pred += y_table[cur_byte & 0x0F];
137 y_plane[y_ptr++] = y_pred;
138
139 cur_byte = buf[stream_ptr++];
140 v_pred += v_table[(cur_byte & 0xF0) >> 4];
141 v_plane[v_ptr++] = v_pred;
142 y_pred += y_table[cur_byte & 0x0F];
143 y_plane[y_ptr++] = y_pred;
144
145 cur_byte = buf[stream_ptr++];
146 y_pred += y_table[cur_byte & 0x0F];
147 y_plane[y_ptr++] = y_pred;
148 y_pred += y_table[(cur_byte & 0xF0) >> 4];
149 y_plane[y_ptr++] = y_pred;
150
151 }
152 }
153
154 *data_size=sizeof(AVFrame);
155 *(AVFrame*)data= s->frame;
156
157 avctx->release_buffer(avctx, &s->frame);
158
159 return buf_size;
160 }
161
162 static int cyuv_decode_end(AVCodecContext *avctx)
163 {
164 /* CyuvDecodeContext *s = avctx->priv_data;*/
165
166 return 0;
167 }
168
169 AVCodec cyuv_decoder = {
170 "cyuv",
171 CODEC_TYPE_VIDEO,
172 CODEC_ID_CYUV,
173 sizeof(CyuvDecodeContext),
174 cyuv_decode_init,
175 NULL,
176 cyuv_decode_end,
177 cyuv_decode_frame,
178 0,
179 NULL
180 };
181