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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*
* Copyright (C) 2000-2004 the xine project
*
* This file is part of xine, a free video player.
*
* xine 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.
*
* xine 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* YUV "Decoder" by Mike Melanson (melanson@pcisys.net)
* Actually, this decoder just reorganizes chunks of raw YUV data in such
* a way that xine can display them.
*
* $Id: yuv.c,v 1.37 2006/07/10 22:08:43 dgp85 Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "xine_internal.h"
#include "video_out.h"
#include "buffer.h"
#include "xineutils.h"
#include "bswap.h"
#define VIDEOBUFSIZE 128*1024
typedef struct {
video_decoder_class_t decoder_class;
} yuv_class_t;
typedef struct yuv_decoder_s {
video_decoder_t video_decoder; /* parent video decoder structure */
yuv_class_t *class;
xine_stream_t *stream;
/* these are traditional variables in a video decoder object */
uint64_t video_step; /* frame duration in pts units */
int decoder_ok; /* current decoder status */
int skipframes;
unsigned char *buf; /* the accumulated buffer data */
int bufsize; /* the maximum size of buf */
int size; /* the current size of buf */
int width; /* the width of a video frame */
int height; /* the height of a video frame */
double ratio; /* the width to height ratio */
int progressive;
int top_field_first;
} yuv_decoder_t;
/**************************************************************************
* xine video plugin functions
*************************************************************************/
/*
* This function receives a buffer of data from the demuxer layer and
* figures out how to handle it based on its header flags.
*/
static void yuv_decode_data (video_decoder_t *this_gen,
buf_element_t *buf) {
yuv_decoder_t *this = (yuv_decoder_t *) this_gen;
xine_bmiheader *bih;
vo_frame_t *img; /* video out frame */
/* a video decoder does not care about this flag (?) */
if (buf->decoder_flags & BUF_FLAG_PREVIEW)
return;
if (buf->decoder_flags & BUF_FLAG_FRAMERATE) {
this->video_step = buf->decoder_info[0];
_x_stream_info_set(this->stream, XINE_STREAM_INFO_FRAME_DURATION, this->video_step);
}
if (buf->decoder_flags & BUF_FLAG_STDHEADER) { /* need to initialize */
this->stream->video_out->open (this->stream->video_out, this->stream);
bih = (xine_bmiheader *) buf->content;
this->width = (bih->biWidth + 3) & ~0x03;
this->height = (bih->biHeight + 3) & ~0x03;
if (buf->decoder_flags & BUF_FLAG_ASPECT)
this->ratio = (double)buf->decoder_info[1] / (double)buf->decoder_info[2];
else
this->ratio = (double)this->width / (double)this->height;
this->progressive = buf->decoder_info[3];
this->top_field_first = buf->decoder_info[4];
if (this->buf) {
free (this->buf);
this->buf = NULL;
}
this->bufsize = VIDEOBUFSIZE;
this->buf = malloc(this->bufsize);
this->size = 0;
this->decoder_ok = 1;
/* load the stream/meta info */
switch (buf->type) {
case BUF_VIDEO_YUY2:
_x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, "Raw YUY2");
break;
case BUF_VIDEO_YV12:
_x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, "Raw YV12");
break;
case BUF_VIDEO_YVU9:
_x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, "Raw YVU9");
break;
case BUF_VIDEO_GREY:
_x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, "Greyscale YUV");
break;
case BUF_VIDEO_I420:
_x_meta_info_set_utf8(this->stream, XINE_META_INFO_VIDEOCODEC, "Raw I420");
break;
}
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_WIDTH, this->width);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HEIGHT, this->height);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_RATIO, this->ratio*10000);
return;
} else if (this->decoder_ok && !(buf->decoder_flags & BUF_FLAG_SPECIAL)) {
uint8_t *src;
/* if buffer contains an entire frame then there's no need to copy it
* into our internal buffer */
if ((buf->decoder_flags & BUF_FLAG_FRAME_START) &&
(buf->decoder_flags & BUF_FLAG_FRAME_END))
src = buf->content;
else {
if (this->size + buf->size > this->bufsize) {
this->bufsize = this->size + 2 * buf->size;
this->buf = realloc (this->buf, this->bufsize);
}
xine_fast_memcpy (&this->buf[this->size], buf->content, buf->size);
this->size += buf->size;
src = this->buf;
}
if (buf->decoder_flags & BUF_FLAG_FRAME_END) {
if (buf->type == BUF_VIDEO_YUY2) {
img = this->stream->video_out->get_frame (this->stream->video_out,
this->width, this->height,
this->ratio, XINE_IMGFMT_YUY2, VO_BOTH_FIELDS);
yuy2_to_yuy2(
/* src */
src, this->width*2,
/* dst */
img->base[0], img->pitches[0],
/* width x height */
this->width, this->height);
} else if (buf->type == BUF_VIDEO_YV12) {
img = this->stream->video_out->get_frame (this->stream->video_out,
this->width, this->height,
this->ratio, XINE_IMGFMT_YV12, VO_BOTH_FIELDS);
yv12_to_yv12(
/* Y */
src, this->width,
img->base[0], img->pitches[0],
/* U */
src + (this->width * this->height * 5/4), this->width/2,
img->base[1], img->pitches[1],
/* V */
src + (this->width * this->height), this->width/2,
img->base[2], img->pitches[2],
/* width x height */
this->width, this->height);
} else if (buf->type == BUF_VIDEO_I420) {
img = this->stream->video_out->get_frame (this->stream->video_out,
this->width, this->height,
this->ratio, XINE_IMGFMT_YV12, VO_BOTH_FIELDS);
yv12_to_yv12(
/* Y */
src, this->width,
img->base[0], img->pitches[0],
/* U */
src + (this->width * this->height), this->width/2,
img->base[1], img->pitches[1],
/* V */
src + (this->width * this->height * 5/4), this->width/2,
img->base[2], img->pitches[2],
/* width x height */
this->width, this->height);
} else if (buf->type == BUF_VIDEO_YVU9) {
img = this->stream->video_out->get_frame (this->stream->video_out,
this->width, this->height,
this->ratio, XINE_IMGFMT_YV12, VO_BOTH_FIELDS);
yuv9_to_yv12(
/* Y */
src,
this->width,
img->base[0],
img->pitches[0],
/* U */
src + (this->width * this->height),
this->width / 4,
img->base[1],
img->pitches[1],
/* V */
src + (this->width * this->height) +
(this->width * this->height / 16),
this->width / 4,
img->base[2],
img->pitches[2],
/* width x height */
this->width,
this->height);
} else if (buf->type == BUF_VIDEO_GREY) {
img = this->stream->video_out->get_frame (this->stream->video_out,
this->width, this->height,
this->ratio, XINE_IMGFMT_YV12, VO_BOTH_FIELDS);
xine_fast_memcpy(img->base[0], src, this->width * this->height);
memset( img->base[1], 0x80, this->width * this->height / 4 );
memset( img->base[2], 0x80, this->width * this->height / 4 );
} else {
/* just allocate something to avoid compiler warnings */
img = this->stream->video_out->get_frame (this->stream->video_out,
this->width, this->height,
this->ratio, XINE_IMGFMT_YV12, VO_BOTH_FIELDS);
}
img->duration = this->video_step;
img->pts = buf->pts;
img->bad_frame = 0;
img->draw(img, this->stream);
img->free(img);
this->size = 0;
}
}
}
/*
* This function is called when xine needs to flush the system. Not
* sure when or if this is used or even if it needs to do anything.
*/
static void yuv_flush (video_decoder_t *this_gen) {
}
/*
* This function resets the video decoder.
*/
static void yuv_reset (video_decoder_t *this_gen) {
yuv_decoder_t *this = (yuv_decoder_t *) this_gen;
this->size = 0;
}
static void yuv_discontinuity (video_decoder_t *this_gen) {
}
/*
* This function frees the video decoder instance allocated to the decoder.
*/
static void yuv_dispose (video_decoder_t *this_gen) {
yuv_decoder_t *this = (yuv_decoder_t *) this_gen;
if (this->buf) {
free (this->buf);
this->buf = NULL;
}
if (this->decoder_ok) {
this->decoder_ok = 0;
this->stream->video_out->close(this->stream->video_out, this->stream);
}
free (this_gen);
}
static video_decoder_t *open_plugin (video_decoder_class_t *class_gen, xine_stream_t *stream) {
yuv_decoder_t *this ;
this = (yuv_decoder_t *) xine_xmalloc (sizeof (yuv_decoder_t));
this->video_decoder.decode_data = yuv_decode_data;
this->video_decoder.flush = yuv_flush;
this->video_decoder.reset = yuv_reset;
this->video_decoder.discontinuity = yuv_discontinuity;
this->video_decoder.dispose = yuv_dispose;
this->size = 0;
this->stream = stream;
this->class = (yuv_class_t *) class_gen;
this->decoder_ok = 0;
this->buf = NULL;
return &this->video_decoder;
}
static char *get_identifier (video_decoder_class_t *this) {
return "YUV";
}
static char *get_description (video_decoder_class_t *this) {
return "Raw YUV video decoder plugin";
}
static void dispose_class (video_decoder_class_t *this) {
free (this);
}
static void *init_plugin (xine_t *xine, void *data) {
yuv_class_t *this;
this = (yuv_class_t *) xine_xmalloc (sizeof (yuv_class_t));
this->decoder_class.open_plugin = open_plugin;
this->decoder_class.get_identifier = get_identifier;
this->decoder_class.get_description = get_description;
this->decoder_class.dispose = dispose_class;
return this;
}
/*
* exported plugin catalog entry
*/
static const uint32_t video_types[] = {
BUF_VIDEO_YUY2,
BUF_VIDEO_YV12,
BUF_VIDEO_YVU9,
BUF_VIDEO_GREY,
BUF_VIDEO_I420,
0
};
static const decoder_info_t dec_info_video = {
video_types, /* supported types */
1 /* priority */
};
const plugin_info_t xine_plugin_info[] EXPORTED = {
/* type, API, "name", version, special_info, init_function */
{ PLUGIN_VIDEO_DECODER, 18, "yuv", XINE_VERSION_CODE, &dec_info_video, init_plugin },
{ PLUGIN_NONE, 0, "", 0, NULL, NULL }
};
|