summaryrefslogtreecommitdiffstats
path: root/libmpcodecs/vd_xvid.c
blob: f2ca6a152b18f49a4943af19d63447bb837c355e (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
#include <stdio.h>
#include <stdlib.h>

#include "config.h"
#include "mp_msg.h"

#ifdef HAVE_XVID3

#include "vd_internal.h"
#include "m_option.h"

#include <xvid.h>

#ifdef XVID_API_UNSTABLE
#warning *******************************************************************
#warning **                                                               **
#warning **  Y O U '' R E   U S I N G   U N S T A B L E   S O F T W A R E  **
#warning **                                                               **
#warning ** There are bugs, this code could crash, could blow up your PC  **
#warning ** or the whole building and do many other nasty things !        **
#warning **                                                               **
#warning ** If you want stable code use stable XViD releases (0.9.x).     **
#warning **                                                               **
#warning *******************************************************************
#endif

typedef struct
{
	void *y;
	void *u;
	void *v;
	int stride_y;
	int stride_uv;
}
DIVX4_DEC_PICTURE;



static vd_info_t info = 
{
	"xvid decoder",
	"xvid",
	"Albeu",
	"Albeu",
	""
};

LIBVD_EXTERN(xvid)

typedef struct {
  int cs;
  unsigned char img_type;
  void* hdl;
  mp_image_t* mpi;
} priv_t;

static int do_dr2 = 0;

m_option_t xvid_dec_opts[] = {
  { "dr2", &do_dr2, CONF_TYPE_FLAG, 0, 0, 1, NULL},
  { "nodr2", &do_dr2, CONF_TYPE_FLAG, 0, 1, 0, NULL},
  {NULL, NULL, 0, 0, 0, 0, NULL}
};

// to set/get/query special features/parameters
static int control(sh_video_t *sh,int cmd,void* arg,...){
  return CONTROL_UNKNOWN;
}

// init driver
static int init(sh_video_t *sh){
  XVID_INIT_PARAM ini;
  XVID_DEC_PARAM dec_p;
  priv_t* p;
  int cs;

#ifdef XVID_API_UNSTABLE
    mp_msg (MSGT_DECVIDEO, MSGL_WARN,
	    "\n"
	    "*******************************************************************\n"
	    "**                                                               **\n"
	    "**  Y O U ' R E   U S I N G   U N S T A B L E   S O F T W A R E  **\n"
	    "**                                                               **\n"
            "** There are bugs, this code could crash, could blow up your PC  **\n"
	    "** or the whole building and do many other nasty things !        **\n"
	    "**                                                               **\n"
	    "** If you want stable code use stable XViD releases (0.9.x).     **\n"
	    "**                                                               **\n"
	    "*******************************************************************\n"
	    "\n");
#endif

  memset(&ini,0,sizeof(XVID_INIT_PARAM));
  memset(&dec_p,0,sizeof(XVID_DEC_PARAM));

  if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12))
    return 0;

  switch(sh->codec->outfmt[sh->outfmtidx]){
  case IMGFMT_YV12:
#ifdef XVID_CSP_EXTERN
    cs= do_dr2 ? XVID_CSP_EXTERN : XVID_CSP_USER;
#else
    cs= XVID_CSP_USER;
#endif
    break;
  case IMGFMT_YUY2:
    cs=XVID_CSP_YUY2;
    break;
  case IMGFMT_UYVY:
    cs=XVID_CSP_UYVY;
    break;
  case IMGFMT_I420: 
  case IMGFMT_IYUV:
    cs=XVID_CSP_I420;
    break;
  case IMGFMT_BGR15: 
    cs=XVID_CSP_RGB555;
    break;
  case IMGFMT_BGR16: 
    cs=XVID_CSP_RGB565;
    break;
  case IMGFMT_BGR24:
    cs=XVID_CSP_RGB24;
    break;
  case IMGFMT_BGR32:
    cs=XVID_CSP_RGB32;
    break;
  case IMGFMT_YVYU:
    cs=XVID_CSP_YVYU;
    break;
  default:
    mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Unsupported out_fmt: 0x%X\n",sh->codec->outfmt[sh->outfmtidx]);
    return 0;
  }
  
  if(xvid_init(NULL, 0, &ini, NULL))
    return 0;

  if(ini.api_version != API_VERSION) {
    if(ini.api_version < API_VERSION) {
      mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Too old version of xvid (min. %d)\n",API_VERSION);
      return 0;
    }
    mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Bad xvid version %d was compiled with %d\n",
	   ini.api_version,API_VERSION);
  }

  dec_p.width = sh->disp_w;
  dec_p.height =  sh->disp_h;

  if(xvid_decore(NULL, XVID_DEC_CREATE, &dec_p, NULL)) {
    mp_msg(MSGT_DECVIDEO,MSGL_ERR,"xvid init failed\n");
    return 0;
  }

  p = (priv_t*)malloc(sizeof(priv_t));
  p->cs = cs;
  p->hdl = dec_p.handle;
  sh->context = p;

  switch(cs) {
#ifdef XVID_CSP_EXTERN
  case XVID_CSP_EXTERN:
    p->img_type = MP_IMGTYPE_STATIC;
    break;
#endif
  case XVID_CSP_USER:
    p->img_type = MP_IMGTYPE_EXPORT;
    break;
  default:
    p->img_type = MP_IMGTYPE_TEMP;
    break;
  }

  return 1;
}

// uninit driver
static void uninit(sh_video_t *sh){
  priv_t* p = sh->context;
  if(!p)
    return;
  xvid_decore(p->hdl,XVID_DEC_DESTROY, NULL, NULL);
  free(p);
}

// decode a frame
static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
  XVID_DEC_FRAME dec;
  DIVX4_DEC_PICTURE d4_pic;
#ifdef XVID_CSP_EXTERN
  XVID_DEC_PICTURE pic;
#endif
  priv_t* p = sh->context;

  mp_image_t* mpi = mpcodecs_get_image(sh,  p->img_type,
				       MP_IMGFLAG_ACCEPT_STRIDE,
				       sh->disp_w,sh->disp_h);

  if(!data || !mpi || len <= 0)
    return NULL;

  memset(&dec,0,sizeof(XVID_DEC_FRAME));

  dec.bitstream = data;
  dec.length = len;
#ifdef XVID_API_UNSTABLE
  dec.general |= XVID_DEC_LOWDELAY;
#endif
  switch(p->cs) {
  case XVID_CSP_USER:
    dec.image = &d4_pic;
    break;
#ifdef XVID_CSP_EXTERN
  case XVID_CSP_EXTERN:
    pic.y = mpi->planes[0];
    pic.u = mpi->planes[1];
    pic.v = mpi->planes[2];
    pic.stride_y = mpi->stride[0];
    pic.stride_u = mpi->stride[1];
    pic.stride_v = mpi->stride[2];
    dec.image = &pic;
    break;
#endif
  default:
    dec.image = mpi->planes[0];
    if(IMGFMT_IS_BGR(mpi->imgfmt) || IMGFMT_IS_RGB(mpi->imgfmt))
      dec.stride = mpi->width;
    else
      dec.stride = mpi->stride[0];
  }
  dec.colorspace = p->cs;

  if(xvid_decore(p->hdl,XVID_DEC_DECODE,&dec,NULL)) {
    mp_msg(MSGT_DECVIDEO,MSGL_ERR,"decoding error\n");
    return NULL;
  }

  if(p->cs == XVID_CSP_USER) {
    mpi->planes[0] = d4_pic.y;
    mpi->planes[1] = d4_pic.u;
    mpi->planes[2] = d4_pic.v;
    mpi->stride[0] = d4_pic.stride_y;
    mpi->stride[1] = mpi->stride[2] = d4_pic.stride_uv;
  }

  return mpi;
}
#endif  //have_xvid