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",
16 "A'rpi & Fabian Franz",
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"
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
);
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
)
53 if (info
->sequence
->width
>> 1 == info
->sequence
->chroma_width
&&
54 info
->sequence
->height
== info
->sequence
->chroma_height
&&
55 (*((int*)arg
)) == IMGFMT_422P
)
60 return CONTROL_UNKNOWN
;
64 static int init(sh_video_t
*sh
){
65 vd_libmpeg2_ctx_t
*context
;
66 mpeg2dec_t
* mpeg2dec
;
67 // const mpeg2_info_t * info;
72 accel
|= MPEG2_ACCEL_X86_MMX
;
74 accel
|= MPEG2_ACCEL_X86_MMXEXT
;
76 accel
|= MPEG2_ACCEL_X86_3DNOW
;
78 accel
|= MPEG2_ACCEL_X86_SSE2
;
79 if(gCpuCaps
.hasAltiVec
)
80 accel
|= MPEG2_ACCEL_PPC_ALTIVEC
;
82 accel
|= MPEG2_ACCEL_ALPHA
;
84 accel
|= MPEG2_ACCEL_ARM
;
87 accel
|= MPEG2_ACCEL_ARM_IWMMXT
;
89 accel
|= MPEG2_ACCEL_ALPHA_MVI
;
91 accel
|= MPEG2_ACCEL_SPARC_VIS
;
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;
112 static void uninit(sh_video_t
*sh
){
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
]);
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
);
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
,
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;
164 if (mpeg2dec
->pending_length
) {
165 mpeg2_buffer (mpeg2dec
, mpeg2dec
->pending_buffer
, mpeg2dec
->pending_buffer
+ mpeg2dec
->pending_length
);
167 mpeg2_buffer (mpeg2dec
, data
, (uint8_t *)data
+len
);
171 int state
=mpeg2_parse (mpeg2dec
);
172 int type
, use_callback
;
174 unsigned long pw
, ph
;
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
);
184 // parsing of the passed buffer finished, return.
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
;
200 if (imgfmt
== context
->imgfmt
&&
201 info
->sequence
->picture_width
== context
->width
&&
202 info
->sequence
->picture_height
== context
->height
&&
203 sh
->aspect
== context
->aspect
)
205 if(!mpcodecs_config_vo(sh
,
206 info
->sequence
->picture_width
,
207 info
->sequence
->picture_height
, imgfmt
))
209 context
->imgfmt
= imgfmt
;
210 context
->width
= info
->sequence
->picture_width
;
211 context
->height
= info
->sequence
->picture_height
;
212 context
->aspect
= sh
->aspect
;
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
220 mpeg2_skip(mpeg2dec
, 1);
221 //printf("Dropping Frame ...\n");
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;
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
;
272 mpeg2dec
->decoder
.convert
=NULL
;
273 mpeg2dec
->decoder
.convert_id
=NULL
;
279 case STATE_INVALID_END
:
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
);
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);