Move setting of O_NONBLOCK before lirc_readconfig, this avoids a memleak
[mplayer/glamo.git] / libmpcodecs / vd_libmpeg2.c
blobfdcb5cd2e377bd0df35578df05a6584ba4b91133
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
6 #include "mp_msg.h"
8 #include "vd_internal.h"
10 //#undef MPEG12_POSTPROC
12 static vd_info_t info =
14 "libmpeg2 MPEG 1/2 Video decoder",
15 "libmpeg2",
16 "A'rpi & Fabian Franz",
17 "Aaron & Walken",
18 "native"
21 LIBVD_EXTERN(libmpeg2)
23 //#include "libvo/video_out.h" // FIXME!!!
25 #include "libmpeg2/mpeg2.h"
26 #include "libmpeg2/attributes.h"
27 #include "libmpeg2/mpeg2_internal.h"
29 #include "cpudetect.h"
31 typedef struct {
32 mpeg2dec_t *mpeg2dec;
33 int quant_store_idx;
34 char *quant_store[3];
35 int imgfmt;
36 int width;
37 int height;
38 double aspect;
39 } vd_libmpeg2_ctx_t;
41 // to set/get/query special features/parameters
42 static int control(sh_video_t *sh,int cmd,void* arg,...){
43 vd_libmpeg2_ctx_t *context = sh->context;
44 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
45 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
47 switch(cmd) {
48 case VDCTRL_QUERY_FORMAT:
49 if (info->sequence->width >> 1 == info->sequence->chroma_width &&
50 info->sequence->height >> 1 == info->sequence->chroma_height &&
51 (*((int*)arg)) == IMGFMT_YV12)
52 return CONTROL_TRUE;
53 if (info->sequence->width >> 1 == info->sequence->chroma_width &&
54 info->sequence->height == info->sequence->chroma_height &&
55 (*((int*)arg)) == IMGFMT_422P)
56 return CONTROL_TRUE;
57 return CONTROL_FALSE;
60 return CONTROL_UNKNOWN;
63 // init driver
64 static int init(sh_video_t *sh){
65 vd_libmpeg2_ctx_t *context;
66 mpeg2dec_t * mpeg2dec;
67 // const mpeg2_info_t * info;
68 int accel;
70 accel = 0;
71 if(gCpuCaps.hasMMX)
72 accel |= MPEG2_ACCEL_X86_MMX;
73 if(gCpuCaps.hasMMX2)
74 accel |= MPEG2_ACCEL_X86_MMXEXT;
75 if(gCpuCaps.has3DNow)
76 accel |= MPEG2_ACCEL_X86_3DNOW;
77 if(gCpuCaps.hasSSE2)
78 accel |= MPEG2_ACCEL_X86_SSE2;
79 if(gCpuCaps.hasAltiVec)
80 accel |= MPEG2_ACCEL_PPC_ALTIVEC;
81 #if ARCH_ALPHA
82 accel |= MPEG2_ACCEL_ALPHA;
83 #elif ARCH_ARM
84 accel |= MPEG2_ACCEL_ARM;
85 #endif
86 #if HAVE_MVI
87 accel |= MPEG2_ACCEL_ALPHA_MVI;
88 #elif HAVE_VIS
89 accel |= MPEG2_ACCEL_SPARC_VIS;
90 #endif
91 mpeg2_accel(accel);
93 mpeg2dec = mpeg2_init ();
95 if(!mpeg2dec) return 0;
97 mpeg2_custom_fbuf(mpeg2dec,1); // enable DR1
99 context = calloc(1, sizeof(vd_libmpeg2_ctx_t));
100 context->mpeg2dec = mpeg2dec;
101 sh->context = context;
103 mpeg2dec->pending_buffer = 0;
104 mpeg2dec->pending_length = 0;
106 return 1;
109 // uninit driver
110 static void uninit(sh_video_t *sh){
111 int i;
112 vd_libmpeg2_ctx_t *context = sh->context;
113 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
114 if (mpeg2dec->pending_buffer) free(mpeg2dec->pending_buffer);
115 mpeg2dec->decoder.convert=NULL;
116 mpeg2dec->decoder.convert_id=NULL;
117 mpeg2_close (mpeg2dec);
118 for (i=0; i < 3; i++)
119 free(context->quant_store[i]);
120 free(sh->context);
123 static void draw_slice (void * _sh, uint8_t * const * src, unsigned int y){
124 sh_video_t* sh = (sh_video_t*) _sh;
125 vd_libmpeg2_ctx_t *context = sh->context;
126 mpeg2dec_t* mpeg2dec = context->mpeg2dec;
127 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
128 int stride[3];
130 // printf("draw_slice() y=%d \n",y);
132 stride[0]=mpeg2dec->decoder.stride;
133 stride[1]=stride[2]=mpeg2dec->decoder.uv_stride;
135 mpcodecs_draw_slice(sh, (uint8_t **)src,
136 stride, info->sequence->picture_width,
137 (y+16<=info->sequence->picture_height) ? 16 :
138 info->sequence->picture_height-y,
139 0, y);
142 // decode a frame
143 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
144 vd_libmpeg2_ctx_t *context = sh->context;
145 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
146 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
147 int drop_frame, framedrop=flags&3;
149 // MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context
150 mpeg2dec->decoder.convert=NULL;
151 mpeg2dec->decoder.convert_id=NULL;
153 if(len<=0) return NULL; // skipped null frame
155 // append extra 'end of frame' code:
156 ((char*)data+len)[0]=0;
157 ((char*)data+len)[1]=0;
158 ((char*)data+len)[2]=1;
159 ((char*)data+len)[3]=0xff;
160 len+=4;
162 if (mpeg2dec->pending_length) {
163 mpeg2_buffer (mpeg2dec, mpeg2dec->pending_buffer, mpeg2dec->pending_buffer + mpeg2dec->pending_length);
164 } else {
165 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len);
168 while(1){
169 int state=mpeg2_parse (mpeg2dec);
170 int type, use_callback;
171 mp_image_t* mpi_new;
172 unsigned long pw, ph;
173 int imgfmt;
175 switch(state){
176 case STATE_BUFFER:
177 if (mpeg2dec->pending_length) {
178 // just finished the pending data, continue with processing of the passed buffer
179 mpeg2dec->pending_length = 0;
180 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len);
181 } else {
182 // parsing of the passed buffer finished, return.
183 return 0;
185 break;
186 case STATE_SEQUENCE:
187 pw = info->sequence->display_width * info->sequence->pixel_width;
188 ph = info->sequence->display_height * info->sequence->pixel_height;
189 if(ph) sh->aspect = (float) pw / (float) ph;
190 // video parameters initialized/changed, (re)init libvo:
191 if (info->sequence->width >> 1 == info->sequence->chroma_width &&
192 info->sequence->height >> 1 == info->sequence->chroma_height) {
193 imgfmt = IMGFMT_YV12;
194 } else if (info->sequence->width >> 1 == info->sequence->chroma_width &&
195 info->sequence->height == info->sequence->chroma_height) {
196 imgfmt = IMGFMT_422P;
197 } else return 0;
198 if (imgfmt == context->imgfmt &&
199 info->sequence->picture_width == context->width &&
200 info->sequence->picture_height == context->height &&
201 sh->aspect == context->aspect)
202 break;
203 if(!mpcodecs_config_vo(sh,
204 info->sequence->picture_width,
205 info->sequence->picture_height, imgfmt))
206 return 0;
207 context->imgfmt = imgfmt;
208 context->width = info->sequence->picture_width;
209 context->height = info->sequence->picture_height;
210 context->aspect = sh->aspect;
211 break;
212 case STATE_PICTURE:
213 type=info->current_picture->flags&PIC_MASK_CODING_TYPE;
215 drop_frame = framedrop && (mpeg2dec->decoder.coding_type == B_TYPE);
216 drop_frame |= framedrop>=2; // hard drop
217 if (drop_frame) {
218 mpeg2_skip(mpeg2dec, 1);
219 //printf("Dropping Frame ...\n");
220 break;
222 mpeg2_skip(mpeg2dec, 0); //mpeg2skip skips frames until set again to 0
224 use_callback = (!framedrop && vd_use_slices &&
225 (info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) ?
226 MP_IMGFLAG_DRAW_CALLBACK:0;
228 // get_buffer "callback":
229 mpi_new=mpcodecs_get_image(sh,MP_IMGTYPE_IPB,
230 (type==PIC_FLAG_CODING_TYPE_B) ?
231 use_callback : (MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE),
232 info->sequence->width,
233 info->sequence->height);
235 if(!mpi_new) return 0; // VO ERROR!!!!!!!!
236 mpeg2_set_buf(mpeg2dec, mpi_new->planes, mpi_new);
237 mpi_new->stride[0] = info->sequence->width;
238 mpi_new->stride[1] = info->sequence->chroma_width;
239 mpi_new->stride[2] = info->sequence->chroma_width;
240 if (info->current_picture->flags&PIC_FLAG_TOP_FIELD_FIRST)
241 mpi_new->fields |= MP_IMGFIELD_TOP_FIRST;
242 else mpi_new->fields &= ~MP_IMGFIELD_TOP_FIRST;
243 if (info->current_picture->flags&PIC_FLAG_REPEAT_FIRST_FIELD)
244 mpi_new->fields |= MP_IMGFIELD_REPEAT_FIRST;
245 else mpi_new->fields &= ~MP_IMGFIELD_REPEAT_FIRST;
246 mpi_new->fields |= MP_IMGFIELD_ORDERED;
247 if (!(info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME))
248 mpi_new->fields |= MP_IMGFIELD_INTERLACED;
250 #ifdef MPEG12_POSTPROC
251 mpi_new->qstride=info->sequence->width>>4;
253 char **p = &context->quant_store[type==PIC_FLAG_CODING_TYPE_B ?
254 2 : (context->quant_store_idx ^= 1)];
255 *p = realloc(*p, mpi_new->qstride*(info->sequence->height>>4));
256 mpi_new->qscale = *p;
258 mpeg2dec->decoder.quant_store=mpi_new->qscale;
259 mpeg2dec->decoder.quant_stride=mpi_new->qstride;
260 mpi_new->pict_type=type; // 1->I, 2->P, 3->B
261 mpi_new->qscale_type= 1;
262 #endif
264 if (mpi_new->flags&MP_IMGFLAG_DRAW_CALLBACK
265 && !(mpi_new->flags&MP_IMGFLAG_DIRECT)) {
266 // nice, filter/vo likes draw_callback :)
267 mpeg2dec->decoder.convert=draw_slice;
268 mpeg2dec->decoder.convert_id=sh;
269 } else {
270 mpeg2dec->decoder.convert=NULL;
271 mpeg2dec->decoder.convert_id=NULL;
274 break;
275 case STATE_SLICE:
276 case STATE_END:
277 case STATE_INVALID_END:
278 // decoding done:
279 if(info->display_fbuf) {
280 mp_image_t* mpi = info->display_fbuf->id;
281 if (mpeg2dec->pending_length == 0) {
282 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
283 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length);
284 memcpy(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
285 } else {
286 // still some data in the pending buffer, shouldn't happen
287 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
288 memmove(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
289 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length + len);
290 memcpy(mpeg2dec->pending_buffer+mpeg2dec->pending_length, data, len);
291 mpeg2dec->pending_length += len;
293 // fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length);
294 return mpi;