8 #include "vd_internal.h"
10 //#undef MPEG12_POSTPROC
12 static 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"
37 // to set/get/query special features/parameters
38 static int control(sh_video_t
*sh
,int cmd
,void* arg
,...){
39 vd_libmpeg2_ctx_t
*context
= sh
->context
;
40 mpeg2dec_t
* mpeg2dec
= context
->mpeg2dec
;
41 const mpeg2_info_t
* info
= mpeg2_info (mpeg2dec
);
44 case VDCTRL_QUERY_FORMAT
:
45 if (info
->sequence
->width
>> 1 == info
->sequence
->chroma_width
&&
46 info
->sequence
->height
>> 1 == info
->sequence
->chroma_height
&&
47 (*((int*)arg
)) == IMGFMT_YV12
)
49 if (info
->sequence
->width
>> 1 == info
->sequence
->chroma_width
&&
50 info
->sequence
->height
== info
->sequence
->chroma_height
&&
51 (*((int*)arg
)) == IMGFMT_422P
)
56 return CONTROL_UNKNOWN
;
60 static int init(sh_video_t
*sh
){
61 vd_libmpeg2_ctx_t
*context
;
62 mpeg2dec_t
* mpeg2dec
;
63 // const mpeg2_info_t * info;
68 accel
|= MPEG2_ACCEL_X86_MMX
;
70 accel
|= MPEG2_ACCEL_X86_MMXEXT
;
72 accel
|= MPEG2_ACCEL_X86_3DNOW
;
73 if(gCpuCaps
.hasAltiVec
)
74 accel
|= MPEG2_ACCEL_PPC_ALTIVEC
;
76 accel
|= MPEG2_ACCEL_SPARC_VIS
;
80 mpeg2dec
= mpeg2_init ();
82 if(!mpeg2dec
) return 0;
84 mpeg2_custom_fbuf(mpeg2dec
,1); // enable DR1
86 context
= calloc(1, sizeof(vd_libmpeg2_ctx_t
));
87 context
->mpeg2dec
= mpeg2dec
;
88 sh
->context
= context
;
90 mpeg2dec
->pending_buffer
= 0;
91 mpeg2dec
->pending_length
= 0;
97 static void uninit(sh_video_t
*sh
){
99 vd_libmpeg2_ctx_t
*context
= sh
->context
;
100 mpeg2dec_t
* mpeg2dec
= context
->mpeg2dec
;
101 if (mpeg2dec
->pending_buffer
) free(mpeg2dec
->pending_buffer
);
102 mpeg2dec
->decoder
.convert
=NULL
;
103 mpeg2dec
->decoder
.convert_id
=NULL
;
104 mpeg2_close (mpeg2dec
);
105 for (i
=0; i
< 3; i
++)
106 free(context
->quant_store
[i
]);
110 static void draw_slice (void * _sh
, uint8_t * const * src
, unsigned int y
){
111 sh_video_t
* sh
= (sh_video_t
*) _sh
;
112 vd_libmpeg2_ctx_t
*context
= sh
->context
;
113 mpeg2dec_t
* mpeg2dec
= context
->mpeg2dec
;
114 const mpeg2_info_t
* info
= mpeg2_info (mpeg2dec
);
117 // printf("draw_slice() y=%d \n",y);
119 stride
[0]=mpeg2dec
->decoder
.stride
;
120 stride
[1]=stride
[2]=mpeg2dec
->decoder
.uv_stride
;
122 mpcodecs_draw_slice(sh
, (uint8_t **)src
,
123 stride
, info
->sequence
->picture_width
,
124 (y
+16<=info
->sequence
->picture_height
) ? 16 :
125 info
->sequence
->picture_height
-y
,
130 static mp_image_t
* decode(sh_video_t
*sh
,void* data
,int len
,int flags
){
131 vd_libmpeg2_ctx_t
*context
= sh
->context
;
132 mpeg2dec_t
* mpeg2dec
= context
->mpeg2dec
;
133 const mpeg2_info_t
* info
= mpeg2_info (mpeg2dec
);
134 int drop_frame
, framedrop
=flags
&3;
136 // MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context
137 mpeg2dec
->decoder
.convert
=NULL
;
138 mpeg2dec
->decoder
.convert_id
=NULL
;
140 if(len
<=0) return NULL
; // skipped null frame
142 // append extra 'end of frame' code:
143 ((char*)data
+len
)[0]=0;
144 ((char*)data
+len
)[1]=0;
145 ((char*)data
+len
)[2]=1;
146 ((char*)data
+len
)[3]=0xff;
149 if (mpeg2dec
->pending_length
) {
150 mpeg2_buffer (mpeg2dec
, mpeg2dec
->pending_buffer
, mpeg2dec
->pending_buffer
+ mpeg2dec
->pending_length
);
152 mpeg2_buffer (mpeg2dec
, data
, (uint8_t *)data
+len
);
156 int state
=mpeg2_parse (mpeg2dec
);
157 int type
, use_callback
;
159 unsigned long pw
, ph
;
163 if (mpeg2dec
->pending_length
) {
164 // just finished the pending data, continue with processing of the passed buffer
165 mpeg2dec
->pending_length
= 0;
166 mpeg2_buffer (mpeg2dec
, data
, (uint8_t *)data
+len
);
168 // parsing of the passed buffer finished, return.
173 pw
= info
->sequence
->display_width
* info
->sequence
->pixel_width
;
174 ph
= info
->sequence
->display_height
* info
->sequence
->pixel_height
;
175 if(ph
) sh
->aspect
= (float) pw
/ (float) ph
;
176 // video parameters inited/changed, (re)init libvo:
177 if (info
->sequence
->width
>> 1 == info
->sequence
->chroma_width
&&
178 info
->sequence
->height
>> 1 == info
->sequence
->chroma_height
) {
179 if(!mpcodecs_config_vo(sh
,
180 info
->sequence
->picture_width
,
181 info
->sequence
->picture_height
, IMGFMT_YV12
)) return 0;
182 } else if (info
->sequence
->width
>> 1 == info
->sequence
->chroma_width
&&
183 info
->sequence
->height
== info
->sequence
->chroma_height
) {
184 if(!mpcodecs_config_vo(sh
,
185 info
->sequence
->picture_width
,
186 info
->sequence
->picture_height
, IMGFMT_422P
)) return 0;
190 type
=info
->current_picture
->flags
&PIC_MASK_CODING_TYPE
;
192 drop_frame
= framedrop
&& (mpeg2dec
->decoder
.coding_type
== B_TYPE
);
193 drop_frame
|= framedrop
>=2; // hard drop
195 mpeg2_skip(mpeg2dec
, 1);
196 //printf("Dropping Frame ...\n");
199 mpeg2_skip(mpeg2dec
, 0); //mpeg2skip skips frames until set again to 0
201 use_callback
= (!framedrop
&& vd_use_slices
&&
202 (info
->current_picture
->flags
&PIC_FLAG_PROGRESSIVE_FRAME
)) ?
203 MP_IMGFLAG_DRAW_CALLBACK
:0;
205 // get_buffer "callback":
206 mpi_new
=mpcodecs_get_image(sh
,MP_IMGTYPE_IPB
,
207 (type
==PIC_FLAG_CODING_TYPE_B
) ?
208 use_callback
: (MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
),
209 info
->sequence
->width
,
210 info
->sequence
->height
);
212 if(!mpi_new
) return 0; // VO ERROR!!!!!!!!
213 mpeg2_set_buf(mpeg2dec
, mpi_new
->planes
, mpi_new
);
214 mpi_new
->stride
[0] = info
->sequence
->width
;
215 mpi_new
->stride
[1] = info
->sequence
->chroma_width
;
216 mpi_new
->stride
[2] = info
->sequence
->chroma_width
;
217 if (info
->current_picture
->flags
&PIC_FLAG_TOP_FIELD_FIRST
)
218 mpi_new
->fields
|= MP_IMGFIELD_TOP_FIRST
;
219 else mpi_new
->fields
&= ~MP_IMGFIELD_TOP_FIRST
;
220 if (info
->current_picture
->flags
&PIC_FLAG_REPEAT_FIRST_FIELD
)
221 mpi_new
->fields
|= MP_IMGFIELD_REPEAT_FIRST
;
222 else mpi_new
->fields
&= ~MP_IMGFIELD_REPEAT_FIRST
;
223 mpi_new
->fields
|= MP_IMGFIELD_ORDERED
;
224 if (!(info
->current_picture
->flags
&PIC_FLAG_PROGRESSIVE_FRAME
))
225 mpi_new
->fields
|= MP_IMGFIELD_INTERLACED
;
227 #ifdef MPEG12_POSTPROC
228 mpi_new
->qstride
=info
->sequence
->width
>>4;
230 char **p
= &context
->quant_store
[type
==PIC_FLAG_CODING_TYPE_B
?
231 2 : (context
->quant_store_idx
^= 1)];
232 *p
= realloc(*p
, mpi_new
->qstride
*(info
->sequence
->height
>>4));
233 mpi_new
->qscale
= *p
;
235 mpeg2dec
->decoder
.quant_store
=mpi_new
->qscale
;
236 mpeg2dec
->decoder
.quant_stride
=mpi_new
->qstride
;
237 mpi_new
->pict_type
=type
; // 1->I, 2->P, 3->B
238 mpi_new
->qscale_type
= 1;
241 if (mpi_new
->flags
&MP_IMGFLAG_DRAW_CALLBACK
242 && !(mpi_new
->flags
&MP_IMGFLAG_DIRECT
)) {
243 // nice, filter/vo likes draw_callback :)
244 mpeg2dec
->decoder
.convert
=draw_slice
;
245 mpeg2dec
->decoder
.convert_id
=sh
;
247 mpeg2dec
->decoder
.convert
=NULL
;
248 mpeg2dec
->decoder
.convert_id
=NULL
;
254 case STATE_INVALID_END
:
256 if(info
->display_fbuf
) {
257 mp_image_t
* mpi
= info
->display_fbuf
->id
;
258 if (mpeg2dec
->pending_length
== 0) {
259 mpeg2dec
->pending_length
= mpeg2dec
->buf_end
- mpeg2dec
->buf_start
;
260 mpeg2dec
->pending_buffer
= realloc(mpeg2dec
->pending_buffer
, mpeg2dec
->pending_length
);
261 memcpy(mpeg2dec
->pending_buffer
, mpeg2dec
->buf_start
, mpeg2dec
->pending_length
);
263 // still some data in the pending buffer, shouldn't happen
264 mpeg2dec
->pending_length
= mpeg2dec
->buf_end
- mpeg2dec
->buf_start
;
265 memmove(mpeg2dec
->pending_buffer
, mpeg2dec
->buf_start
, mpeg2dec
->pending_length
);
266 mpeg2dec
->pending_buffer
= realloc(mpeg2dec
->pending_buffer
, mpeg2dec
->pending_length
+ len
);
267 memcpy(mpeg2dec
->pending_buffer
+mpeg2dec
->pending_length
, data
, len
);
268 mpeg2dec
->pending_length
+= len
;
270 // fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length);