]> git.sesse.net Git - ffmpeg/blob - libavcodec/cyuv.c
Slightly reworked MC code, it gives better picture.
[ffmpeg] / libavcodec / cyuv.c
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 /**
27  * @file cyuv.c
28  * Creative YUV (CYUV) Video Decoder.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include "common.h"
37 #include "avcodec.h"
38 #include "dsputil.h"
39 #include "mpegvideo.h"
40
41
42 typedef struct CyuvDecodeContext {
43     AVCodecContext *avctx;
44     int width, height;
45     AVFrame frame;
46 } CyuvDecodeContext;
47
48 static int cyuv_decode_init(AVCodecContext *avctx)
49 {
50     CyuvDecodeContext *s = avctx->priv_data;
51
52     s->avctx = avctx;
53     s->width = avctx->width;
54     /* width needs to be divisible by 4 for this codec to work */
55     if (s->width & 0x3)
56         return -1;
57     s->height = avctx->height;
58     avctx->pix_fmt = PIX_FMT_YUV411P;
59     avctx->has_b_frames = 0;
60
61     return 0;
62 }
63
64 static int cyuv_decode_frame(AVCodecContext *avctx,
65                              void *data, int *data_size,
66                              uint8_t *buf, int buf_size)
67 {
68     CyuvDecodeContext *s=avctx->priv_data;
69
70     unsigned char *y_plane;
71     unsigned char *u_plane;
72     unsigned char *v_plane;
73     int y_ptr;
74     int u_ptr;
75     int v_ptr;
76
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;
81
82     unsigned char y_pred, u_pred, v_pred;
83     int stream_ptr;
84     unsigned char cur_byte;
85     int pixel_groups;
86
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",
93         buf_size,
94         48 + s->height * (s->width * 3 / 4));
95       return -1;
96     }
97
98     /* pixel data starts 48 bytes in, after 3x16-byte tables */
99     stream_ptr = 48;
100
101     if(s->frame.data[0])
102         avctx->release_buffer(avctx, &s->frame);
103
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");
108         return -1;
109     }
110
111     y_plane = s->frame.data[0];
112     u_plane = s->frame.data[1];
113     v_plane = s->frame.data[2];
114
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) {
121
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;
126
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;
131
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;
137
138         /* iterate through the remaining pixel groups (4 pixels/group) */
139         pixel_groups = s->width / 4 - 1;
140         while (pixel_groups--) {
141
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;
147
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;
153
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;
159
160         }
161     }
162
163     *data_size=sizeof(AVFrame);
164     *(AVFrame*)data= s->frame;
165
166     return buf_size;
167 }
168
169 static int cyuv_decode_end(AVCodecContext *avctx)
170 {
171 /*    CyuvDecodeContext *s = avctx->priv_data;*/
172
173     return 0;
174 }
175
176 AVCodec cyuv_decoder = {
177     "cyuv",
178     CODEC_TYPE_VIDEO,
179     CODEC_ID_CYUV,
180     sizeof(CyuvDecodeContext),
181     cyuv_decode_init,
182     NULL,
183     cyuv_decode_end,
184     cyuv_decode_frame,
185     CODEC_CAP_DR1,
186     NULL
187 };
188