20% faster hqdn3d on x86_64
[mplayer/glamo.git] / libmpcodecs / vd_libmpeg2.c
blob626aca09db569dbe278fda6985fd64f43ee22693
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[MP_MAX_PLANES] = {mpeg2dec->decoder.stride, mpeg2dec->decoder.uv_stride, mpeg2dec->decoder.uv_stride};
129 uint8_t *srcs[MP_MAX_PLANES] = {src[0], src[1], src[2]};
131 // printf("draw_slice() y=%d \n",y);
133 mpcodecs_draw_slice(sh, srcs,
134 stride, info->sequence->picture_width,
135 (y+16<=info->sequence->picture_height) ? 16 :
136 info->sequence->picture_height-y,
137 0, y);
140 // decode a frame
141 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
142 vd_libmpeg2_ctx_t *context = sh->context;
143 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
144 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
145 int drop_frame, framedrop=flags&3;
147 // MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context
148 mpeg2dec->decoder.convert=NULL;
149 mpeg2dec->decoder.convert_id=NULL;
151 if(len<=0) return NULL; // skipped null frame
153 // append extra 'end of frame' code:
154 ((char*)data+len)[0]=0;
155 ((char*)data+len)[1]=0;
156 ((char*)data+len)[2]=1;
157 ((char*)data+len)[3]=0xff;
158 len+=4;
160 if (mpeg2dec->pending_length) {
161 mpeg2_buffer (mpeg2dec, mpeg2dec->pending_buffer, mpeg2dec->pending_buffer + mpeg2dec->pending_length);
162 } else {
163 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len);
166 while(1){
167 int state=mpeg2_parse (mpeg2dec);
168 int type, use_callback;
169 mp_image_t* mpi_new;
170 unsigned long pw, ph;
171 int imgfmt;
173 switch(state){
174 case STATE_BUFFER:
175 if (mpeg2dec->pending_length) {
176 // just finished the pending data, continue with processing of the passed buffer
177 mpeg2dec->pending_length = 0;
178 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len);
179 } else {
180 // parsing of the passed buffer finished, return.
181 return 0;
183 break;
184 case STATE_SEQUENCE:
185 pw = info->sequence->display_width * info->sequence->pixel_width;
186 ph = info->sequence->display_height * info->sequence->pixel_height;
187 if(ph) sh->aspect = (float) pw / (float) ph;
188 // video parameters initialized/changed, (re)init libvo:
189 if (info->sequence->width >> 1 == info->sequence->chroma_width &&
190 info->sequence->height >> 1 == info->sequence->chroma_height) {
191 imgfmt = IMGFMT_YV12;
192 } else if (info->sequence->width >> 1 == info->sequence->chroma_width &&
193 info->sequence->height == info->sequence->chroma_height) {
194 imgfmt = IMGFMT_422P;
195 } else return 0;
196 if (imgfmt == context->imgfmt &&
197 info->sequence->picture_width == context->width &&
198 info->sequence->picture_height == context->height &&
199 sh->aspect == context->aspect)
200 break;
201 if(!mpcodecs_config_vo(sh,
202 info->sequence->picture_width,
203 info->sequence->picture_height, imgfmt))
204 return 0;
205 context->imgfmt = imgfmt;
206 context->width = info->sequence->picture_width;
207 context->height = info->sequence->picture_height;
208 context->aspect = sh->aspect;
209 break;
210 case STATE_PICTURE:
211 type=info->current_picture->flags&PIC_MASK_CODING_TYPE;
213 drop_frame = framedrop && (mpeg2dec->decoder.coding_type == B_TYPE);
214 drop_frame |= framedrop>=2; // hard drop
215 if (drop_frame) {
216 mpeg2_skip(mpeg2dec, 1);
217 //printf("Dropping Frame ...\n");
218 break;
220 mpeg2_skip(mpeg2dec, 0); //mpeg2skip skips frames until set again to 0
222 use_callback = (!framedrop && vd_use_slices &&
223 (info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) ?
224 MP_IMGFLAG_DRAW_CALLBACK:0;
226 // get_buffer "callback":
227 mpi_new=mpcodecs_get_image(sh,MP_IMGTYPE_IPB,
228 (type==PIC_FLAG_CODING_TYPE_B) ?
229 use_callback : (MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE),
230 info->sequence->width,
231 info->sequence->height);
233 if(!mpi_new) return 0; // VO ERROR!!!!!!!!
234 mpeg2_set_buf(mpeg2dec, mpi_new->planes, mpi_new);
235 mpi_new->stride[0] = info->sequence->width;
236 mpi_new->stride[1] = info->sequence->chroma_width;
237 mpi_new->stride[2] = info->sequence->chroma_width;
238 if (info->current_picture->flags&PIC_FLAG_TOP_FIELD_FIRST)
239 mpi_new->fields |= MP_IMGFIELD_TOP_FIRST;
240 else mpi_new->fields &= ~MP_IMGFIELD_TOP_FIRST;
241 if (info->current_picture->flags&PIC_FLAG_REPEAT_FIRST_FIELD)
242 mpi_new->fields |= MP_IMGFIELD_REPEAT_FIRST;
243 else mpi_new->fields &= ~MP_IMGFIELD_REPEAT_FIRST;
244 mpi_new->fields |= MP_IMGFIELD_ORDERED;
245 if (!(info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME))
246 mpi_new->fields |= MP_IMGFIELD_INTERLACED;
248 #ifdef MPEG12_POSTPROC
249 mpi_new->qstride=info->sequence->width>>4;
251 char **p = &context->quant_store[type==PIC_FLAG_CODING_TYPE_B ?
252 2 : (context->quant_store_idx ^= 1)];
253 *p = realloc(*p, mpi_new->qstride*(info->sequence->height>>4));
254 mpi_new->qscale = *p;
256 mpeg2dec->decoder.quant_store=mpi_new->qscale;
257 mpeg2dec->decoder.quant_stride=mpi_new->qstride;
258 mpi_new->pict_type=type; // 1->I, 2->P, 3->B
259 mpi_new->qscale_type= 1;
260 #endif
262 if (mpi_new->flags&MP_IMGFLAG_DRAW_CALLBACK
263 && !(mpi_new->flags&MP_IMGFLAG_DIRECT)) {
264 // nice, filter/vo likes draw_callback :)
265 mpeg2dec->decoder.convert=draw_slice;
266 mpeg2dec->decoder.convert_id=sh;
267 } else {
268 mpeg2dec->decoder.convert=NULL;
269 mpeg2dec->decoder.convert_id=NULL;
272 break;
273 case STATE_SLICE:
274 case STATE_END:
275 case STATE_INVALID_END:
276 // decoding done:
277 if(info->display_fbuf) {
278 mp_image_t* mpi = info->display_fbuf->id;
279 if (mpeg2dec->pending_length == 0) {
280 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
281 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length);
282 memcpy(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
283 } else {
284 // still some data in the pending buffer, shouldn't happen
285 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
286 memmove(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
287 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length + len);
288 memcpy(mpeg2dec->pending_buffer+mpeg2dec->pending_length, data, len);
289 mpeg2dec->pending_length += len;
291 // fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length);
292 return mpi;