8 #include "vd_internal.h"
10 //#undef MPEG12_POSTPROC
12 static vd_info_t info
=
14 "libmpeg2 MPEG 1/2 Video decoder",
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_ALPHA_MVI
;
89 accel
|= MPEG2_ACCEL_SPARC_VIS
;
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;
110 static void uninit(sh_video_t
*sh
){
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
]);
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
);
130 // printf("draw_slice() y=%d \n",y);
132 stride
[0]=mpeg2dec
->decoder
.stride
;
133 stride
[1]=stride
[2]=mpeg2dec
->decoder
.uv_stride
;
135 mpcodecs_draw_slice(sh
, (uint8_t **)src
,
136 stride
, info
->sequence
->picture_width
,
137 (y
+16<=info
->sequence
->picture_height
) ? 16 :
138 info
->sequence
->picture_height
-y
,
143 static mp_image_t
* decode(sh_video_t
*sh
,void* data
,int len
,int flags
){
144 vd_libmpeg2_ctx_t
*context
= sh
->context
;
145 mpeg2dec_t
* mpeg2dec
= context
->mpeg2dec
;
146 const mpeg2_info_t
* info
= mpeg2_info (mpeg2dec
);
147 int drop_frame
, framedrop
=flags
&3;
149 // MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context
150 mpeg2dec
->decoder
.convert
=NULL
;
151 mpeg2dec
->decoder
.convert_id
=NULL
;
153 if(len
<=0) return NULL
; // skipped null frame
155 // append extra 'end of frame' code:
156 ((char*)data
+len
)[0]=0;
157 ((char*)data
+len
)[1]=0;
158 ((char*)data
+len
)[2]=1;
159 ((char*)data
+len
)[3]=0xff;
162 if (mpeg2dec
->pending_length
) {
163 mpeg2_buffer (mpeg2dec
, mpeg2dec
->pending_buffer
, mpeg2dec
->pending_buffer
+ mpeg2dec
->pending_length
);
165 mpeg2_buffer (mpeg2dec
, data
, (uint8_t *)data
+len
);
169 int state
=mpeg2_parse (mpeg2dec
);
170 int type
, use_callback
;
172 unsigned long pw
, ph
;
177 if (mpeg2dec
->pending_length
) {
178 // just finished the pending data, continue with processing of the passed buffer
179 mpeg2dec
->pending_length
= 0;
180 mpeg2_buffer (mpeg2dec
, data
, (uint8_t *)data
+len
);
182 // parsing of the passed buffer finished, return.
187 pw
= info
->sequence
->display_width
* info
->sequence
->pixel_width
;
188 ph
= info
->sequence
->display_height
* info
->sequence
->pixel_height
;
189 if(ph
) sh
->aspect
= (float) pw
/ (float) ph
;
190 // video parameters initialized/changed, (re)init libvo:
191 if (info
->sequence
->width
>> 1 == info
->sequence
->chroma_width
&&
192 info
->sequence
->height
>> 1 == info
->sequence
->chroma_height
) {
193 imgfmt
= IMGFMT_YV12
;
194 } else if (info
->sequence
->width
>> 1 == info
->sequence
->chroma_width
&&
195 info
->sequence
->height
== info
->sequence
->chroma_height
) {
196 imgfmt
= IMGFMT_422P
;
198 if (imgfmt
== context
->imgfmt
&&
199 info
->sequence
->picture_width
== context
->width
&&
200 info
->sequence
->picture_height
== context
->height
&&
201 sh
->aspect
== context
->aspect
)
203 if(!mpcodecs_config_vo(sh
,
204 info
->sequence
->picture_width
,
205 info
->sequence
->picture_height
, imgfmt
))
207 context
->imgfmt
= imgfmt
;
208 context
->width
= info
->sequence
->picture_width
;
209 context
->height
= info
->sequence
->picture_height
;
210 context
->aspect
= sh
->aspect
;
213 type
=info
->current_picture
->flags
&PIC_MASK_CODING_TYPE
;
215 drop_frame
= framedrop
&& (mpeg2dec
->decoder
.coding_type
== B_TYPE
);
216 drop_frame
|= framedrop
>=2; // hard drop
218 mpeg2_skip(mpeg2dec
, 1);
219 //printf("Dropping Frame ...\n");
222 mpeg2_skip(mpeg2dec
, 0); //mpeg2skip skips frames until set again to 0
224 use_callback
= (!framedrop
&& vd_use_slices
&&
225 (info
->current_picture
->flags
&PIC_FLAG_PROGRESSIVE_FRAME
)) ?
226 MP_IMGFLAG_DRAW_CALLBACK
:0;
228 // get_buffer "callback":
229 mpi_new
=mpcodecs_get_image(sh
,MP_IMGTYPE_IPB
,
230 (type
==PIC_FLAG_CODING_TYPE_B
) ?
231 use_callback
: (MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
),
232 info
->sequence
->width
,
233 info
->sequence
->height
);
235 if(!mpi_new
) return 0; // VO ERROR!!!!!!!!
236 mpeg2_set_buf(mpeg2dec
, mpi_new
->planes
, mpi_new
);
237 mpi_new
->stride
[0] = info
->sequence
->width
;
238 mpi_new
->stride
[1] = info
->sequence
->chroma_width
;
239 mpi_new
->stride
[2] = info
->sequence
->chroma_width
;
240 if (info
->current_picture
->flags
&PIC_FLAG_TOP_FIELD_FIRST
)
241 mpi_new
->fields
|= MP_IMGFIELD_TOP_FIRST
;
242 else mpi_new
->fields
&= ~MP_IMGFIELD_TOP_FIRST
;
243 if (info
->current_picture
->flags
&PIC_FLAG_REPEAT_FIRST_FIELD
)
244 mpi_new
->fields
|= MP_IMGFIELD_REPEAT_FIRST
;
245 else mpi_new
->fields
&= ~MP_IMGFIELD_REPEAT_FIRST
;
246 mpi_new
->fields
|= MP_IMGFIELD_ORDERED
;
247 if (!(info
->current_picture
->flags
&PIC_FLAG_PROGRESSIVE_FRAME
))
248 mpi_new
->fields
|= MP_IMGFIELD_INTERLACED
;
250 #ifdef MPEG12_POSTPROC
251 mpi_new
->qstride
=info
->sequence
->width
>>4;
253 char **p
= &context
->quant_store
[type
==PIC_FLAG_CODING_TYPE_B
?
254 2 : (context
->quant_store_idx
^= 1)];
255 *p
= realloc(*p
, mpi_new
->qstride
*(info
->sequence
->height
>>4));
256 mpi_new
->qscale
= *p
;
258 mpeg2dec
->decoder
.quant_store
=mpi_new
->qscale
;
259 mpeg2dec
->decoder
.quant_stride
=mpi_new
->qstride
;
260 mpi_new
->pict_type
=type
; // 1->I, 2->P, 3->B
261 mpi_new
->qscale_type
= 1;
264 if (mpi_new
->flags
&MP_IMGFLAG_DRAW_CALLBACK
265 && !(mpi_new
->flags
&MP_IMGFLAG_DIRECT
)) {
266 // nice, filter/vo likes draw_callback :)
267 mpeg2dec
->decoder
.convert
=draw_slice
;
268 mpeg2dec
->decoder
.convert_id
=sh
;
270 mpeg2dec
->decoder
.convert
=NULL
;
271 mpeg2dec
->decoder
.convert_id
=NULL
;
277 case STATE_INVALID_END
:
279 if(info
->display_fbuf
) {
280 mp_image_t
* mpi
= info
->display_fbuf
->id
;
281 if (mpeg2dec
->pending_length
== 0) {
282 mpeg2dec
->pending_length
= mpeg2dec
->buf_end
- mpeg2dec
->buf_start
;
283 mpeg2dec
->pending_buffer
= realloc(mpeg2dec
->pending_buffer
, mpeg2dec
->pending_length
);
284 memcpy(mpeg2dec
->pending_buffer
, mpeg2dec
->buf_start
, mpeg2dec
->pending_length
);
286 // still some data in the pending buffer, shouldn't happen
287 mpeg2dec
->pending_length
= mpeg2dec
->buf_end
- mpeg2dec
->buf_start
;
288 memmove(mpeg2dec
->pending_buffer
, mpeg2dec
->buf_start
, mpeg2dec
->pending_length
);
289 mpeg2dec
->pending_buffer
= realloc(mpeg2dec
->pending_buffer
, mpeg2dec
->pending_length
+ len
);
290 memcpy(mpeg2dec
->pending_buffer
+mpeg2dec
->pending_length
, data
, len
);
291 mpeg2dec
->pending_length
+= len
;
293 // fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length);