3 * Copyright (C) 2003 the ffmpeg project
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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
28 * Creative YUV (CYUV) Video Decoder.
39 #include "mpegvideo.h"
42 typedef struct CyuvDecodeContext {
43 AVCodecContext *avctx;
48 static int cyuv_decode_init(AVCodecContext *avctx)
50 CyuvDecodeContext *s = avctx->priv_data;
53 s->width = avctx->width;
54 /* width needs to be divisible by 4 for this codec to work */
57 s->height = avctx->height;
58 avctx->pix_fmt = PIX_FMT_YUV411P;
59 avctx->has_b_frames = 0;
64 static int cyuv_decode_frame(AVCodecContext *avctx,
65 void *data, int *data_size,
66 uint8_t *buf, int buf_size)
68 CyuvDecodeContext *s=avctx->priv_data;
70 unsigned char *y_plane;
71 unsigned char *u_plane;
72 unsigned char *v_plane;
77 /* prediction error tables (make it clear that they are signed values) */
78 signed char *y_table = buf + 0;
79 signed char *u_table = buf + 16;
80 signed char *v_table = buf + 32;
82 unsigned char y_pred, u_pred, v_pred;
84 unsigned char cur_byte;
87 /* sanity check the buffer size: A buffer has 3x16-bytes tables
88 * followed by (height) lines each with 3 bytes to represent groups
89 * of 4 pixels. Thus, the total size of the buffer ought to be:
90 * (3 * 16) + height * (width * 3 / 4) */
91 if (buf_size != 48 + s->height * (s->width * 3 / 4)) {
92 av_log(avctx, AV_LOG_ERROR, "ffmpeg: cyuv: got a buffer with %d bytes when %d were expected\n",
94 48 + s->height * (s->width * 3 / 4));
98 /* pixel data starts 48 bytes in, after 3x16-byte tables */
102 avctx->release_buffer(avctx, &s->frame);
104 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
105 s->frame.reference = 0;
106 if(avctx->get_buffer(avctx, &s->frame) < 0) {
107 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
111 y_plane = s->frame.data[0];
112 u_plane = s->frame.data[1];
113 v_plane = s->frame.data[2];
115 /* iterate through each line in the height */
116 for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
117 y_ptr < (s->height * s->frame.linesize[0]);
118 y_ptr += s->frame.linesize[0] - s->width,
119 u_ptr += s->frame.linesize[1] - s->width / 4,
120 v_ptr += s->frame.linesize[2] - s->width / 4) {
122 /* reset predictors */
123 cur_byte = buf[stream_ptr++];
124 u_plane[u_ptr++] = u_pred = cur_byte & 0xF0;
125 y_plane[y_ptr++] = y_pred = (cur_byte & 0x0F) << 4;
127 cur_byte = buf[stream_ptr++];
128 v_plane[v_ptr++] = v_pred = cur_byte & 0xF0;
129 y_pred += y_table[cur_byte & 0x0F];
130 y_plane[y_ptr++] = y_pred;
132 cur_byte = buf[stream_ptr++];
133 y_pred += y_table[cur_byte & 0x0F];
134 y_plane[y_ptr++] = y_pred;
135 y_pred += y_table[(cur_byte & 0xF0) >> 4];
136 y_plane[y_ptr++] = y_pred;
138 /* iterate through the remaining pixel groups (4 pixels/group) */
139 pixel_groups = s->width / 4 - 1;
140 while (pixel_groups--) {
142 cur_byte = buf[stream_ptr++];
143 u_pred += u_table[(cur_byte & 0xF0) >> 4];
144 u_plane[u_ptr++] = u_pred;
145 y_pred += y_table[cur_byte & 0x0F];
146 y_plane[y_ptr++] = y_pred;
148 cur_byte = buf[stream_ptr++];
149 v_pred += v_table[(cur_byte & 0xF0) >> 4];
150 v_plane[v_ptr++] = v_pred;
151 y_pred += y_table[cur_byte & 0x0F];
152 y_plane[y_ptr++] = y_pred;
154 cur_byte = buf[stream_ptr++];
155 y_pred += y_table[cur_byte & 0x0F];
156 y_plane[y_ptr++] = y_pred;
157 y_pred += y_table[(cur_byte & 0xF0) >> 4];
158 y_plane[y_ptr++] = y_pred;
163 *data_size=sizeof(AVFrame);
164 *(AVFrame*)data= s->frame;
169 static int cyuv_decode_end(AVCodecContext *avctx)
171 /* CyuvDecodeContext *s = avctx->priv_data;*/
176 AVCodec cyuv_decoder = {
180 sizeof(CyuvDecodeContext),