stream.h: Add 2 prototypes instead of declaring them in cache2.c
[mplayer.git] / libmpcodecs / vd_libmpeg2.c
blob3bd17a4e0e5310aad663c5c73861415a4d5e323a
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
5 #include "options.h"
6 #include "mp_msg.h"
8 #include "vd_internal.h"
10 //#undef MPEG12_POSTPROC
12 static const vd_info_t info =
14 "MPEG 1/2 Video decoder libmpeg2-v0.4.0b",
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 #ifdef ARCH_ALPHA
82 accel |= MPEG2_ACCEL_ALPHA;
83 #elif ARCH_ARM
84 accel |= MPEG2_ACCEL_ARM;
85 #endif
86 #ifdef HAVE_IWMMXT
87 accel |= MPEG2_ACCEL_ARM_IWMMXT;
88 #elif HAVE_MVI
89 accel |= MPEG2_ACCEL_ALPHA_MVI;
90 #elif HAVE_VIS
91 accel |= MPEG2_ACCEL_SPARC_VIS;
92 #endif
93 mpeg2_accel(accel);
95 mpeg2dec = mpeg2_init ();
97 if(!mpeg2dec) return 0;
99 mpeg2_custom_fbuf(mpeg2dec,1); // enable DR1
101 context = calloc(1, sizeof(vd_libmpeg2_ctx_t));
102 context->mpeg2dec = mpeg2dec;
103 sh->context = context;
105 mpeg2dec->pending_buffer = 0;
106 mpeg2dec->pending_length = 0;
108 return 1;
111 // uninit driver
112 static void uninit(sh_video_t *sh){
113 int i;
114 vd_libmpeg2_ctx_t *context = sh->context;
115 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
116 if (mpeg2dec->pending_buffer) free(mpeg2dec->pending_buffer);
117 mpeg2dec->decoder.convert=NULL;
118 mpeg2dec->decoder.convert_id=NULL;
119 mpeg2_close (mpeg2dec);
120 for (i=0; i < 3; i++)
121 free(context->quant_store[i]);
122 free(sh->context);
125 static void draw_slice (void * _sh, uint8_t * const * src, unsigned int y){
126 sh_video_t* sh = (sh_video_t*) _sh;
127 vd_libmpeg2_ctx_t *context = sh->context;
128 mpeg2dec_t* mpeg2dec = context->mpeg2dec;
129 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
130 int stride[3];
132 // printf("draw_slice() y=%d \n",y);
134 stride[0]=mpeg2dec->decoder.stride;
135 stride[1]=stride[2]=mpeg2dec->decoder.uv_stride;
137 mpcodecs_draw_slice(sh, (uint8_t **)src,
138 stride, info->sequence->picture_width,
139 (y+16<=info->sequence->picture_height) ? 16 :
140 info->sequence->picture_height-y,
141 0, y);
144 // decode a frame
145 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
146 vd_libmpeg2_ctx_t *context = sh->context;
147 mpeg2dec_t * mpeg2dec = context->mpeg2dec;
148 const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
149 int drop_frame, framedrop=flags&3;
151 // MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context
152 mpeg2dec->decoder.convert=NULL;
153 mpeg2dec->decoder.convert_id=NULL;
155 if(len<=0) return NULL; // skipped null frame
157 // append extra 'end of frame' code:
158 ((char*)data+len)[0]=0;
159 ((char*)data+len)[1]=0;
160 ((char*)data+len)[2]=1;
161 ((char*)data+len)[3]=0xff;
162 len+=4;
164 if (mpeg2dec->pending_length) {
165 mpeg2_buffer (mpeg2dec, mpeg2dec->pending_buffer, mpeg2dec->pending_buffer + mpeg2dec->pending_length);
166 } else {
167 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len);
170 while(1){
171 int state=mpeg2_parse (mpeg2dec);
172 int type, use_callback;
173 mp_image_t* mpi_new;
174 unsigned long pw, ph;
175 int imgfmt;
177 switch(state){
178 case STATE_BUFFER:
179 if (mpeg2dec->pending_length) {
180 // just finished the pending data, continue with processing of the passed buffer
181 mpeg2dec->pending_length = 0;
182 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len);
183 } else {
184 // parsing of the passed buffer finished, return.
185 return 0;
187 break;
188 case STATE_SEQUENCE:
189 pw = info->sequence->display_width * info->sequence->pixel_width;
190 ph = info->sequence->display_height * info->sequence->pixel_height;
191 if(ph) sh->aspect = (float) pw / (float) ph;
192 // video parameters initialized/changed, (re)init libvo:
193 if (info->sequence->width >> 1 == info->sequence->chroma_width &&
194 info->sequence->height >> 1 == info->sequence->chroma_height) {
195 imgfmt = IMGFMT_YV12;
196 } else if (info->sequence->width >> 1 == info->sequence->chroma_width &&
197 info->sequence->height == info->sequence->chroma_height) {
198 imgfmt = IMGFMT_422P;
199 } else return 0;
200 if (imgfmt == context->imgfmt &&
201 info->sequence->picture_width == context->width &&
202 info->sequence->picture_height == context->height &&
203 sh->aspect == context->aspect)
204 break;
205 if(!mpcodecs_config_vo(sh,
206 info->sequence->picture_width,
207 info->sequence->picture_height, imgfmt))
208 return 0;
209 context->imgfmt = imgfmt;
210 context->width = info->sequence->picture_width;
211 context->height = info->sequence->picture_height;
212 context->aspect = sh->aspect;
213 break;
214 case STATE_PICTURE:
215 type=info->current_picture->flags&PIC_MASK_CODING_TYPE;
217 drop_frame = framedrop && (mpeg2dec->decoder.coding_type == B_TYPE);
218 drop_frame |= framedrop>=2; // hard drop
219 if (drop_frame) {
220 mpeg2_skip(mpeg2dec, 1);
221 //printf("Dropping Frame ...\n");
222 break;
224 mpeg2_skip(mpeg2dec, 0); //mpeg2skip skips frames until set again to 0
226 use_callback = (!framedrop && sh->opts->vd_use_slices &&
227 (info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) ?
228 MP_IMGFLAG_DRAW_CALLBACK:0;
230 // get_buffer "callback":
231 mpi_new=mpcodecs_get_image(sh,MP_IMGTYPE_IPB,
232 (type==PIC_FLAG_CODING_TYPE_B) ?
233 use_callback : (MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE),
234 info->sequence->width,
235 info->sequence->height);
237 if(!mpi_new) return 0; // VO ERROR!!!!!!!!
238 mpeg2_set_buf(mpeg2dec, mpi_new->planes, mpi_new);
239 mpi_new->stride[0] = info->sequence->width;
240 mpi_new->stride[1] = info->sequence->chroma_width;
241 mpi_new->stride[2] = info->sequence->chroma_width;
242 if (info->current_picture->flags&PIC_FLAG_TOP_FIELD_FIRST)
243 mpi_new->fields |= MP_IMGFIELD_TOP_FIRST;
244 else mpi_new->fields &= ~MP_IMGFIELD_TOP_FIRST;
245 if (info->current_picture->flags&PIC_FLAG_REPEAT_FIRST_FIELD)
246 mpi_new->fields |= MP_IMGFIELD_REPEAT_FIRST;
247 else mpi_new->fields &= ~MP_IMGFIELD_REPEAT_FIRST;
248 mpi_new->fields |= MP_IMGFIELD_ORDERED;
249 if (!(info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME))
250 mpi_new->fields |= MP_IMGFIELD_INTERLACED;
252 #ifdef MPEG12_POSTPROC
253 mpi_new->qstride=info->sequence->width>>4;
255 char **p = &context->quant_store[type==PIC_FLAG_CODING_TYPE_B ?
256 2 : (context->quant_store_idx ^= 1)];
257 *p = realloc(*p, mpi_new->qstride*(info->sequence->height>>4));
258 mpi_new->qscale = *p;
260 mpeg2dec->decoder.quant_store=mpi_new->qscale;
261 mpeg2dec->decoder.quant_stride=mpi_new->qstride;
262 mpi_new->pict_type=type; // 1->I, 2->P, 3->B
263 mpi_new->qscale_type= 1;
264 #endif
266 if (mpi_new->flags&MP_IMGFLAG_DRAW_CALLBACK
267 && !(mpi_new->flags&MP_IMGFLAG_DIRECT)) {
268 // nice, filter/vo likes draw_callback :)
269 mpeg2dec->decoder.convert=draw_slice;
270 mpeg2dec->decoder.convert_id=sh;
271 } else {
272 mpeg2dec->decoder.convert=NULL;
273 mpeg2dec->decoder.convert_id=NULL;
276 break;
277 case STATE_SLICE:
278 case STATE_END:
279 case STATE_INVALID_END:
280 // decoding done:
281 if(info->display_fbuf) {
282 mp_image_t* mpi = info->display_fbuf->id;
283 if (mpeg2dec->pending_length == 0) {
284 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
285 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length);
286 memcpy(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
287 } else {
288 // still some data in the pending buffer, shouldn't happen
289 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
290 memmove(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
291 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length + len);
292 memcpy(mpeg2dec->pending_buffer+mpeg2dec->pending_length, data, len);
293 mpeg2dec->pending_length += len;
295 // fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length);
296 return mpi;