summaryrefslogtreecommitdiff
path: root/src/libreal/xine_decoder.c
blob: 163927f0a99d0cdd06605ff0cdea69b0e4828094 (plain)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/* 
 * Copyright (C) 2000-2002 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
 *
 * $Id: xine_decoder.c,v 1.9 2002/11/26 02:25:26 guenter Exp $
 *
 * thin layer to use real binary-only codecs in xine
 *
 * code inspired by work from Florian Schneider for the MPlayer Project 
 */


#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dlfcn.h>

#include "bswap.h"
#include "xine_internal.h"
#include "video_out.h"
#include "buffer.h"

/*
#define LOG
*/


typedef struct {
  video_decoder_class_t   decoder_class;

  char                   *real_codec_path;

} real_class_t;

#define BUF_SIZE       32768
#define CHUNK_TAB_SIZE 64

typedef struct realdec_decoder_s {
  video_decoder_t  video_decoder;

  real_class_t    *cls;

  xine_stream_t   *stream;

  void            *rv_handle;

  unsigned long (*rvyuv_custom_message)(unsigned long*,void*);
  unsigned long (*rvyuv_free)(void*);
  unsigned long (*rvyuv_hive_message)(unsigned long,unsigned long);
  unsigned long (*rvyuv_init)(void*, void*); /* initdata,context */
  unsigned long (*rvyuv_transform)(char*, char*,unsigned long*,unsigned long*,void*);

  void            *context;

  int              width, height;

  uint8_t          chunk_buffer[BUF_SIZE];
  int              chunk_buffer_size;

  int              num_chunks;
  uint32_t         chunk_tab[CHUNK_TAB_SIZE];

  /* keep track of timestamps, estimate framerate */
  uint64_t         pts;
  int              num_frames;
  uint64_t         last_pts;
  uint64_t         duration;

  uint8_t         *frame_buffer;
  int              frame_size;

} realdec_decoder_t;

/* we need exact positions */
typedef struct {
        short unk1;
        short w;
        short h;
        short unk3;
        int unk2;
        int subformat;
        int unk5;
        int format;
} rv_init_t;

#ifdef LOG
static void hexdump (char *buf, int length) {

  int i;

  printf ("libreal: ascii contents>");
  for (i = 0; i < length; i++) {
    unsigned char c = buf[i];

    if ((c >= 32) && (c <= 128))
      printf ("%c", c);
    else
      printf (".");
  }
  printf ("\n");

  printf ("libreal: complete hexdump of package follows:\nlibreal 0x0000:  ");
  for (i = 0; i < length; i++) {
    unsigned char c = buf[i];

    printf ("%02x", c);

    if ((i % 16) == 15)
      printf ("\nlibreal 0x%04x: ", i+1);

    if ((i % 2) == 1)
      printf (" ");

  }
  printf ("\n");
}
#endif

/*
 * real codec loader
 */

static int load_syms_linux (realdec_decoder_t *this, char *codec_name) {

  char path[1024];

  sprintf (path, "%s/%s", this->cls->real_codec_path, codec_name);

#ifdef LOG
  printf ("libreal: opening shared obj '%s'\n", path);
#endif
  this->rv_handle = dlopen (path, RTLD_LAZY);

  if (!this->rv_handle) {
    printf ("libreal: error: %s\n", dlerror());
    return 0;
  }
  
  this->rvyuv_custom_message = dlsym (this->rv_handle, "RV20toYUV420CustomMessage");
  this->rvyuv_free           = dlsym (this->rv_handle, "RV20toYUV420Free");
  this->rvyuv_hive_message   = dlsym (this->rv_handle, "RV20toYUV420HiveMessage");
  this->rvyuv_init           = dlsym (this->rv_handle, "RV20toYUV420Init");
  this->rvyuv_transform      = dlsym (this->rv_handle, "RV20toYUV420Transform");
  
  if (this->rvyuv_custom_message &&
      this->rvyuv_free &&
      this->rvyuv_hive_message &&
      this->rvyuv_init &&
      this->rvyuv_transform) 
    return 1;

  printf ("libreal: Error resolving symbols! (version incompatibility?)\n");
  return 0;
}

static int init_codec (realdec_decoder_t *this, buf_element_t *buf) {

  unsigned int* extrahdr = (unsigned int*) (buf->content+28);
  int           result;
  rv_init_t     init_data = {11, 0, 0, 0, 0, 
			     0, 1, 0}; /* rv30 */


  switch (buf->type) {
  case BUF_VIDEO_RV20:
    if (!load_syms_linux (this, "drv2.so.6.0"))
      return 0;
    break;
  case BUF_VIDEO_RV30:
    if (!load_syms_linux (this, "drv3.so.6.0"))
      return 0;
    break;
  default:
    printf ("libreal: error, i don't handle buf type 0x%08x\n",
	    buf->type);
    abort();
  }

  init_data.w = BE_16(&buf->content[12]);
  init_data.h = BE_16(&buf->content[14]);
  
  this->width  = init_data.w;
  this->height = init_data.h;
  
  init_data.subformat = BE_32(&buf->content[26]);
  init_data.format    = BE_32(&buf->content[30]);
  
#ifdef LOG
  hexdump (&init_data, sizeof (init_data));
  
  hexdump (buf->content, 32);
  hexdump (extrahdr, 10);

  printf ("libreal: init codec %dx%d... %x %x\n", 
	  init_data.w, init_data.h,
	  init_data.subformat, init_data.format );
#endif  

  this->context = NULL;
  
  result = this->rvyuv_init (&init_data, &this->context); 
  
  /* setup rv30 codec (codec sub-type and image dimensions): */
  if (init_data.format>=0x20200002){
    unsigned long cmsg24[4]={this->width,this->height,
			     this->width,this->height};
    unsigned long cmsg_data[3]={0x24,1+((init_data.subformat>>16)&7),
				(unsigned long) &cmsg24};
    
#ifdef LOG
    printf ("libreal: cmsg24:\n");
    hexdump (cmsg24, sizeof (cmsg24));
    printf ("libreal: cmsg_data:\n");
    hexdump (cmsg_data, sizeof (cmsg_data));
#endif
    
    this->rvyuv_custom_message (cmsg_data, this->context);
  }
  
  this->stream->video_out->open(this->stream->video_out, this->stream);
    
  this->frame_size   = this->width*this->height;
  this->frame_buffer = xine_xmalloc (this->width*this->height*3/2);

  return 1;
}

static void realdec_decode_data (video_decoder_t *this_gen, buf_element_t *buf) {
  realdec_decoder_t *this = (realdec_decoder_t *) this_gen;

#ifdef LOG
  printf ("libreal: decode_data, flags=0x%08x, len=%d ...\n", 
	  buf->decoder_flags, buf->size);
#endif

  if (buf->decoder_flags & BUF_FLAG_PREVIEW) {
    /* real_find_sequence_header (&this->real, buf->content, buf->content + buf->size);*/
  } else if (buf->decoder_flags & BUF_FLAG_HEADER) {

    init_codec (this, buf);

  } else if (this->context) {

    if (buf->decoder_flags & BUF_FLAG_FRAME_START) {

      if (this->num_chunks>0) {

	int            result;
	vo_frame_t    *img;

	unsigned long  transform_out[5];
	unsigned long  transform_in[6]={
	  this->chunk_buffer_size,/* length of the packet (sub-packets appended) */
	  0,		          /* unknown, seems to be unused  */
	  this->num_chunks-1,	  /* number of sub-packets - 1    */
	  this->chunk_tab,        /* table of sub-packet offsets  */
	  0,	   	          /* unknown, seems to be unused  */
	  this->pts               /* timestamp (the integer value from the stream) */
	};

#ifdef LOG
	printf ("libreal: got %d chunks in buffer and new frame is starting\n",
		this->num_chunks);
#endif

	img = this->stream->video_out->get_frame (this->stream->video_out,
						  /* this->av_picture.linesize[0],  */
						  this->width,
						  this->height,
						  42, 
						  XINE_IMGFMT_YV12,
						  VO_BOTH_FIELDS);
	
	if (this->pts != this->last_pts) {
	  int64_t new_duration;

	  img->pts         = this->pts * 90;
	  new_duration     = (this->pts - this->last_pts) * 90 / (this->num_frames+1);
	  this->duration   = (this->duration * 9 + new_duration)/10;
	  this->num_frames = 0;
	  this->last_pts   = this->pts;
	} else {
	  img->pts       = 0;
	  this->num_frames++;
	}
	img->duration  = this->duration; 
	img->bad_frame = 0;
	
#ifdef LOG
	printf ("libreal: pts %lld %lld diff %lld # %d est. duration %lld\n", 
		this->pts*90, 
		buf->pts*90,
		(buf->pts - this->pts) * 90,
		this->num_frames,
		this->duration);

	printf ("libreal: decoding %d bytes:\n", this->chunk_buffer_size);
	hexdump (this->chunk_buffer, this->chunk_buffer_size);

	printf ("libreal: transform_in:\n");
	hexdump (transform_in, 6*4);
	
	printf ("libreal: chunk_table:\n");
	hexdump (this->chunk_tab, this->num_chunks*8+8);
#endif
	
	result = this->rvyuv_transform (this->chunk_buffer, 
					this->frame_buffer, 
					transform_in,
					transform_out, 
					this->context);

	xine_fast_memcpy (img->base[0], this->frame_buffer, this->frame_size);
	xine_fast_memcpy (img->base[1], this->frame_buffer+this->frame_size, 
			  this->frame_size/4);
	xine_fast_memcpy (img->base[2], 
			  this->frame_buffer+this->frame_size*5/4, 
			  this->frame_size/4);
	
	if (img->copy) {
	  int height = img->height;
	  uint8_t *src[3];
	  
	  src[0] = img->base[0];
	  src[1] = img->base[1];
	  src[2] = img->base[2];
	  
	  while ((height -= 16) >= 0) {
	    img->copy(img, src);
	    src[0] += 16 * img->pitches[0];
	    src[1] +=  8 * img->pitches[1];
	    src[2] +=  8 * img->pitches[2];
	  }
	}
	img->draw(img, this->stream);
	img->free(img);
	
      }

      /* new frame starting */

#ifdef LOG
      printf ("libreal: new frame starting (%d bytes)\n", buf->size);
#endif

      memcpy (this->chunk_buffer, buf->content, buf->size);

      this->chunk_buffer_size = buf->size;
      this->chunk_tab[0]      = 1;
      this->chunk_tab[1]      = 0;
      this->num_chunks        = 1;

      if (buf->pts)
	this->pts = buf->pts;
      else
	this->pts = 0;
    } else {

      /* buffer another fragment */

#ifdef LOG
      printf ("libreal: another fragment (%d chunks in buffer)\n", 
	      this->num_chunks);
#endif
      
      memcpy (this->chunk_buffer+this->chunk_buffer_size, buf->content, buf->size);

      this->chunk_tab[2*this->num_chunks]    = 1;
      this->chunk_tab[2*this->num_chunks+1]  = this->chunk_buffer_size; 
      this->num_chunks++;
      this->chunk_buffer_size               += buf->size;

      if (buf->pts)
	this->pts = buf->pts;

    }
  }

#ifdef LOG
  printf ("libreal: decode_data...done\n");
#endif
}

static void realdec_flush (video_decoder_t *this_gen) {
  /* realdec_decoder_t *this = (realdec_decoder_t *) this_gen; */

#ifdef LOG
  printf ("libreal: flush\n");
#endif

}

static void realdec_reset (video_decoder_t *this_gen) {
  /* realdec_decoder_t *this = (realdec_decoder_t *) this_gen; */

}

static void realdec_discontinuity (video_decoder_t *this_gen) {
  /* realdec_decoder_t *this = (realdec_decoder_t *) this_gen; */

}

static void realdec_dispose (video_decoder_t *this_gen) {

  realdec_decoder_t *this = (realdec_decoder_t *) this_gen;

#ifdef LOG
  printf ("libreal: close\n");
#endif

  free (this);
}

static video_decoder_t *open_plugin (video_decoder_class_t *class_gen, 
				     xine_stream_t *stream) {

  real_class_t      *cls = (real_class_t *) class_gen;
  realdec_decoder_t *this ;

  this = (realdec_decoder_t *) malloc (sizeof (realdec_decoder_t));
  memset(this, 0, sizeof (realdec_decoder_t));

  this->video_decoder.decode_data         = realdec_decode_data;
  this->video_decoder.flush               = realdec_flush;
  this->video_decoder.reset               = realdec_reset;
  this->video_decoder.discontinuity       = realdec_discontinuity;
  this->video_decoder.dispose             = realdec_dispose;
  this->stream                            = stream;
  this->cls                               = cls;

  this->context    = 0;
  this->num_chunks = 0;
  this->pts        = 0;
  this->last_pts   = 0;
  this->num_frames = 0;
  this->duration   = 3000;

  return &this->video_decoder;
}

/*
 * real plugin class
 */

static char *get_identifier (video_decoder_class_t *this) {
  return "realvdec";
}

static char *get_description (video_decoder_class_t *this) {
  return "real binary-only codec based video decoder plugin";
}

static void dispose_class (video_decoder_class_t *this) {
  free (this);
}

/*
 * some fake functions to make real codecs happy 
 */
void *__builtin_vec_new(unsigned long size) {
  return malloc(size);
}
void __builtin_vec_delete(void *mem) {
  free(mem);
}
void __pure_virtual(void) {
  printf("libreal: FATAL: __pure_virtual() called!\n");
  /*      exit(1); */
}

static void codec_path_cb (void *data, xine_cfg_entry_t *cfg) {
  real_class_t *this = (real_class_t *) data;
  
  this->real_codec_path = cfg->str_value;
}

static void *init_class (xine_t *xine, void *data) {

  real_class_t       *this;
  config_values_t    *config = xine->config;

  this = (real_class_t *) xine_xmalloc (sizeof (real_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;

  this->real_codec_path = config->register_string (config, "codec.real_codecs_path", 
						   "unknown",
						   _("path to real player codecs, if installed"),
						   NULL, 10, codec_path_cb, (void *)this);
  
  if (!strcmp (this->real_codec_path, "unknown")) {

    struct stat s;

    /* try some auto-detection */

    if (!stat ("/usr/local/RealPlayer8/Codecs/drv3.so.6.0", &s)) 
      config->update_string (config, "codec.real_codecs_path", 
			     "/usr/local/RealPlayer8/Codecs");
    if (!stat ("/usr/RealPlayer8/Codecs/drv3.so.6.0", &s)) 
      config->update_string (config, "codec.real_codecs_path", 
			     "/usr/RealPlayer8/Codecs");
    if (!stat ("/opt/RealPlayer8/Codecs/drv3.so.6.0", &s)) 
      config->update_string (config, "codec.real_codecs_path", 
			     "/opt/RealPlayer8/Codecs");
  }

#ifdef LOG
  printf ("libareal: real codec path : %s\n",  this->real_codec_path);
#endif

  return this;
}

/*
 * exported plugin catalog entry
 */

static uint32_t supported_types[] = { BUF_VIDEO_RV20, BUF_VIDEO_RV30, 0 };

static decoder_info_t dec_info_real = {
  supported_types,     /* supported types */
  6                    /* priority        */
};

plugin_info_t xine_plugin_info[] = {
  /* type, API, "name", version, special_info, init_function */  
  { PLUGIN_VIDEO_DECODER, 13, "real", XINE_VERSION_CODE, &dec_info_real, init_class },
  { PLUGIN_NONE, 0, "", 0, NULL, NULL }
};