typo fixes
[mplayer/greg.git] / libmpcodecs / vd_libmpeg2.c
blob3db1ca85f05bee088b4068d05611f6f9812a2b29
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
5 #ifdef USE_LIBMPEG2
7 #include "mp_msg.h"
9 #include "vd_internal.h"
11 //#undef MPEG12_POSTPROC
13 static vd_info_t info =
15 "MPEG 1/2 Video decoder libmpeg2-v0.4.0b",
16 "libmpeg2",
17 "A'rpi & Fabian Franz",
18 "Aaron & Walken",
19 "native"
22 LIBVD_EXTERN(libmpeg2)
24 //#include "libvo/video_out.h" // FIXME!!!
26 #include "libmpeg2/mpeg2.h"
27 #include "libmpeg2/attributes.h"
28 #include "libmpeg2/mpeg2_internal.h"
30 #include "cpudetect.h"
32 typedef struct {
33 mpeg2dec_t *mpeg2dec;
34 int quant_store_idx;
35 char *quant_store[3];
36 } vd_libmpeg2_ctx_t;
38 // to set/get/query special features/parameters
39 static int control(sh_video_t *sh,int cmd,void* arg,...){
40 vd_libmpeg2_ctx_t *context = sh->context;
41 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
42 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
44 switch(cmd) {
45 case VDCTRL_QUERY_FORMAT:
46 if (info->sequence->width >> 1 == info->sequence->chroma_width &&
47 info->sequence->height >> 1 == info->sequence->chroma_height &&
48 (*((int*)arg)) == IMGFMT_YV12)
49 return CONTROL_TRUE;
50 if (info->sequence->width >> 1 == info->sequence->chroma_width &&
51 info->sequence->height == info->sequence->chroma_height &&
52 (*((int*)arg)) == IMGFMT_422P)
53 return CONTROL_TRUE;
54 return CONTROL_FALSE;
57 return CONTROL_UNKNOWN;
60 // init driver
61 static int init(sh_video_t *sh){
62 vd_libmpeg2_ctx_t *context;
63 mpeg2dec_t * mpeg2dec;
64 // const mpeg2_info_t * info;
65 int accel;
67 accel = 0;
68 if(gCpuCaps.hasMMX)
69 accel |= MPEG2_ACCEL_X86_MMX;
70 if(gCpuCaps.hasMMX2)
71 accel |= MPEG2_ACCEL_X86_MMXEXT;
72 if(gCpuCaps.has3DNow)
73 accel |= MPEG2_ACCEL_X86_3DNOW;
74 if(gCpuCaps.hasAltiVec)
75 accel |= MPEG2_ACCEL_PPC_ALTIVEC;
76 #ifdef HAVE_VIS
77 accel |= MPEG2_ACCEL_SPARC_VIS;
78 #endif
79 mpeg2_accel(accel);
81 mpeg2dec = mpeg2_init ();
83 if(!mpeg2dec) return 0;
85 mpeg2_custom_fbuf(mpeg2dec,1); // enable DR1
87 context = calloc(1, sizeof(vd_libmpeg2_ctx_t));
88 context->mpeg2dec = mpeg2dec;
89 sh->context = context;
91 mpeg2dec->pending_buffer = 0;
92 mpeg2dec->pending_length = 0;
94 return 1;
97 // uninit driver
98 static void uninit(sh_video_t *sh){
99 int i;
100 vd_libmpeg2_ctx_t *context = sh->context;
101 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
102 if (mpeg2dec->pending_buffer) free(mpeg2dec->pending_buffer);
103 mpeg2dec->decoder.convert=NULL;
104 mpeg2dec->decoder.convert_id=NULL;
105 mpeg2_close (mpeg2dec);
106 for (i=0; i < 3; i++)
107 free(context->quant_store[i]);
108 free(sh->context);
111 static void draw_slice (void * _sh, uint8_t * const * src, unsigned int y){
112 sh_video_t* sh = (sh_video_t*) _sh;
113 vd_libmpeg2_ctx_t *context = sh->context;
114 mpeg2dec_t* mpeg2dec = context->mpeg2dec;
115 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
116 int stride[3];
118 // printf("draw_slice() y=%d \n",y);
120 stride[0]=mpeg2dec->decoder.stride;
121 stride[1]=stride[2]=mpeg2dec->decoder.uv_stride;
123 mpcodecs_draw_slice(sh, (uint8_t **)src,
124 stride, info->sequence->picture_width,
125 (y+16<=info->sequence->picture_height) ? 16 :
126 info->sequence->picture_height-y,
127 0, y);
130 // decode a frame
131 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
132 vd_libmpeg2_ctx_t *context = sh->context;
133 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
134 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
135 int drop_frame, framedrop=flags&3;
137 // MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context
138 mpeg2dec->decoder.convert=NULL;
139 mpeg2dec->decoder.convert_id=NULL;
141 if(len<=0) return NULL; // skipped null frame
143 // append extra 'end of frame' code:
144 ((char*)data+len)[0]=0;
145 ((char*)data+len)[1]=0;
146 ((char*)data+len)[2]=1;
147 ((char*)data+len)[3]=0xff;
148 len+=4;
150 if (mpeg2dec->pending_length) {
151 mpeg2_buffer (mpeg2dec, mpeg2dec->pending_buffer, mpeg2dec->pending_buffer + mpeg2dec->pending_length);
152 } else {
153 mpeg2_buffer (mpeg2dec, data, data+len);
156 while(1){
157 int state=mpeg2_parse (mpeg2dec);
158 int type, use_callback;
159 mp_image_t* mpi_new;
160 unsigned long pw, ph;
162 switch(state){
163 case STATE_BUFFER:
164 if (mpeg2dec->pending_length) {
165 // just finished the pending data, continue with processing of the passed buffer
166 mpeg2dec->pending_length = 0;
167 mpeg2_buffer (mpeg2dec, data, data+len);
168 } else {
169 // parsing of the passed buffer finished, return.
170 return 0;
172 break;
173 case STATE_SEQUENCE:
174 pw = info->sequence->display_width * info->sequence->pixel_width;
175 ph = info->sequence->display_height * info->sequence->pixel_height;
176 if(ph) sh->aspect = (float) pw / (float) ph;
177 // video parameters inited/changed, (re)init libvo:
178 if (info->sequence->width >> 1 == info->sequence->chroma_width &&
179 info->sequence->height >> 1 == info->sequence->chroma_height) {
180 if(!mpcodecs_config_vo(sh,
181 info->sequence->picture_width,
182 info->sequence->picture_height, IMGFMT_YV12)) return 0;
183 } else if (info->sequence->width >> 1 == info->sequence->chroma_width &&
184 info->sequence->height == info->sequence->chroma_height) {
185 if(!mpcodecs_config_vo(sh,
186 info->sequence->picture_width,
187 info->sequence->picture_height, IMGFMT_422P)) return 0;
188 } else return 0;
189 break;
190 case STATE_PICTURE:
191 type=info->current_picture->flags&PIC_MASK_CODING_TYPE;
193 drop_frame = framedrop && (mpeg2dec->decoder.coding_type == B_TYPE);
194 drop_frame |= framedrop>=2; // hard drop
195 if (drop_frame) {
196 mpeg2_skip(mpeg2dec, 1);
197 //printf("Dropping Frame ...\n");
198 break;
200 mpeg2_skip(mpeg2dec, 0); //mpeg2skip skips frames until set again to 0
202 use_callback = (!framedrop && vd_use_slices &&
203 (info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) ?
204 MP_IMGFLAG_DRAW_CALLBACK:0;
206 // get_buffer "callback":
207 mpi_new=mpcodecs_get_image(sh,MP_IMGTYPE_IPB,
208 (type==PIC_FLAG_CODING_TYPE_B) ?
209 use_callback : (MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE),
210 info->sequence->width,
211 info->sequence->height);
213 if(!mpi_new) return 0; // VO ERROR!!!!!!!!
214 mpeg2_set_buf(mpeg2dec, mpi_new->planes, mpi_new);
215 if (info->current_picture->flags&PIC_FLAG_TOP_FIELD_FIRST)
216 mpi_new->fields |= MP_IMGFIELD_TOP_FIRST;
217 else mpi_new->fields &= ~MP_IMGFIELD_TOP_FIRST;
218 if (info->current_picture->flags&PIC_FLAG_REPEAT_FIRST_FIELD)
219 mpi_new->fields |= MP_IMGFIELD_REPEAT_FIRST;
220 else mpi_new->fields &= ~MP_IMGFIELD_REPEAT_FIRST;
221 mpi_new->fields |= MP_IMGFIELD_ORDERED;
223 #ifdef MPEG12_POSTPROC
224 mpi_new->qstride=info->sequence->width>>4;
226 char **p = &context->quant_store[type==PIC_FLAG_CODING_TYPE_B ?
227 2 : (context->quant_store_idx ^= 1)];
228 *p = realloc(*p, mpi_new->qstride*(info->sequence->height>>4));
229 mpi_new->qscale = *p;
231 mpeg2dec->decoder.quant_store=mpi_new->qscale;
232 mpeg2dec->decoder.quant_stride=mpi_new->qstride;
233 mpi_new->pict_type=type; // 1->I, 2->P, 3->B
234 mpi_new->qscale_type= 1;
235 #endif
237 if (mpi_new->flags&MP_IMGFLAG_DRAW_CALLBACK
238 && !(mpi_new->flags&MP_IMGFLAG_DIRECT)) {
239 // nice, filter/vo likes draw_callback :)
240 mpeg2dec->decoder.convert=draw_slice;
241 mpeg2dec->decoder.convert_id=sh;
242 } else {
243 mpeg2dec->decoder.convert=NULL;
244 mpeg2dec->decoder.convert_id=NULL;
247 break;
248 case STATE_SLICE:
249 case STATE_END:
250 case STATE_INVALID_END:
251 // decoding done:
252 if(info->display_fbuf) {
253 mp_image_t* mpi = info->display_fbuf->id;
254 if (mpeg2dec->pending_length == 0) {
255 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
256 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length);
257 memcpy(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
258 } else {
259 // still some data in the pending buffer, shouldn't happen
260 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
261 memmove(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
262 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length + len);
263 memcpy(mpeg2dec->pending_buffer+mpeg2dec->pending_length, data, len);
264 mpeg2dec->pending_length += len;
266 // fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length);
267 return mpi;
272 #endif