2002-03-09 02:18:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2002-11-01 16:40:15 +00:00
|
|
|
|
2002-03-09 02:18:33 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
|
|
|
|
#include "vd_internal.h"
|
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
//#undef MPEG12_POSTPROC
|
|
|
|
|
2002-03-09 02:18:33 +00:00
|
|
|
static vd_info_t info =
|
|
|
|
{
|
2004-08-02 11:26:43 +00:00
|
|
|
"MPEG 1/2 Video decoder libmpeg2-v0.4.0b",
|
2002-03-09 02:18:33 +00:00
|
|
|
"libmpeg2",
|
2003-04-06 16:43:46 +00:00
|
|
|
"A'rpi & Fabian Franz",
|
2002-03-09 02:18:33 +00:00
|
|
|
"Aaron & Walken",
|
|
|
|
"native"
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBVD_EXTERN(libmpeg2)
|
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
//#include "libvo/video_out.h" // FIXME!!!
|
2002-03-09 02:18:33 +00:00
|
|
|
|
|
|
|
#include "libmpeg2/mpeg2.h"
|
2004-08-02 11:26:43 +00:00
|
|
|
#include "libmpeg2/attributes.h"
|
2002-03-09 02:18:33 +00:00
|
|
|
#include "libmpeg2/mpeg2_internal.h"
|
2002-04-03 02:39:02 +00:00
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "cpudetect.h"
|
2002-04-03 02:39:02 +00:00
|
|
|
|
2006-04-27 02:46:33 +00:00
|
|
|
typedef struct {
|
|
|
|
mpeg2dec_t *mpeg2dec;
|
|
|
|
int quant_store_idx;
|
|
|
|
char *quant_store[3];
|
|
|
|
} vd_libmpeg2_ctx_t;
|
|
|
|
|
2002-03-09 02:18:33 +00:00
|
|
|
// to set/get/query special features/parameters
|
|
|
|
static int control(sh_video_t *sh,int cmd,void* arg,...){
|
2006-04-27 02:46:33 +00:00
|
|
|
vd_libmpeg2_ctx_t *context = sh->context;
|
|
|
|
mpeg2dec_t * mpeg2dec = context->mpeg2dec;
|
2004-11-22 10:20:47 +00:00
|
|
|
const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
|
2004-11-20 14:37:38 +00:00
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case VDCTRL_QUERY_FORMAT:
|
2004-11-22 10:20:47 +00:00
|
|
|
if (info->sequence->width >> 1 == info->sequence->chroma_width &&
|
|
|
|
info->sequence->height >> 1 == info->sequence->chroma_height &&
|
|
|
|
(*((int*)arg)) == IMGFMT_YV12)
|
2004-11-20 14:37:38 +00:00
|
|
|
return CONTROL_TRUE;
|
2004-11-22 10:20:47 +00:00
|
|
|
if (info->sequence->width >> 1 == info->sequence->chroma_width &&
|
|
|
|
info->sequence->height == info->sequence->chroma_height &&
|
|
|
|
(*((int*)arg)) == IMGFMT_422P)
|
2004-11-20 14:37:38 +00:00
|
|
|
return CONTROL_TRUE;
|
|
|
|
return CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
|
2002-03-09 02:18:33 +00:00
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// init driver
|
|
|
|
static int init(sh_video_t *sh){
|
2006-04-27 02:46:33 +00:00
|
|
|
vd_libmpeg2_ctx_t *context;
|
2003-04-06 16:43:46 +00:00
|
|
|
mpeg2dec_t * mpeg2dec;
|
2004-11-20 14:37:38 +00:00
|
|
|
// const mpeg2_info_t * info;
|
2003-04-06 16:43:46 +00:00
|
|
|
int accel;
|
|
|
|
|
|
|
|
accel = 0;
|
|
|
|
if(gCpuCaps.hasMMX)
|
|
|
|
accel |= MPEG2_ACCEL_X86_MMX;
|
|
|
|
if(gCpuCaps.hasMMX2)
|
|
|
|
accel |= MPEG2_ACCEL_X86_MMXEXT;
|
|
|
|
if(gCpuCaps.has3DNow)
|
|
|
|
accel |= MPEG2_ACCEL_X86_3DNOW;
|
2003-06-09 12:10:42 +00:00
|
|
|
if(gCpuCaps.hasAltiVec)
|
|
|
|
accel |= MPEG2_ACCEL_PPC_ALTIVEC;
|
2004-08-24 17:11:02 +00:00
|
|
|
#ifdef HAVE_VIS
|
|
|
|
accel |= MPEG2_ACCEL_SPARC_VIS;
|
2003-04-06 16:43:46 +00:00
|
|
|
#endif
|
|
|
|
mpeg2_accel(accel);
|
|
|
|
|
|
|
|
mpeg2dec = mpeg2_init ();
|
|
|
|
|
|
|
|
if(!mpeg2dec) return 0;
|
|
|
|
|
|
|
|
mpeg2_custom_fbuf(mpeg2dec,1); // enable DR1
|
2006-04-27 02:46:33 +00:00
|
|
|
|
|
|
|
context = calloc(1, sizeof(vd_libmpeg2_ctx_t));
|
|
|
|
context->mpeg2dec = mpeg2dec;
|
|
|
|
sh->context = context;
|
2003-04-06 16:43:46 +00:00
|
|
|
|
2004-08-24 16:15:56 +00:00
|
|
|
mpeg2dec->pending_buffer = 0;
|
|
|
|
mpeg2dec->pending_length = 0;
|
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
return 1;
|
2002-03-09 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// uninit driver
|
|
|
|
static void uninit(sh_video_t *sh){
|
2006-04-27 02:46:33 +00:00
|
|
|
int i;
|
|
|
|
vd_libmpeg2_ctx_t *context = sh->context;
|
|
|
|
mpeg2dec_t * mpeg2dec = context->mpeg2dec;
|
2004-08-24 16:15:56 +00:00
|
|
|
if (mpeg2dec->pending_buffer) free(mpeg2dec->pending_buffer);
|
2004-11-22 10:20:47 +00:00
|
|
|
mpeg2dec->decoder.convert=NULL;
|
|
|
|
mpeg2dec->decoder.convert_id=NULL;
|
2003-04-06 16:43:46 +00:00
|
|
|
mpeg2_close (mpeg2dec);
|
2006-04-27 02:46:33 +00:00
|
|
|
for (i=0; i < 3; i++)
|
|
|
|
free(context->quant_store[i]);
|
|
|
|
free(sh->context);
|
2002-03-09 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 16:15:56 +00:00
|
|
|
static void draw_slice (void * _sh, uint8_t * const * src, unsigned int y){
|
2003-04-06 16:43:46 +00:00
|
|
|
sh_video_t* sh = (sh_video_t*) _sh;
|
2006-04-27 02:46:33 +00:00
|
|
|
vd_libmpeg2_ctx_t *context = sh->context;
|
|
|
|
mpeg2dec_t* mpeg2dec = context->mpeg2dec;
|
2003-04-06 16:43:46 +00:00
|
|
|
const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
|
2002-04-03 02:39:02 +00:00
|
|
|
int stride[3];
|
|
|
|
|
2004-08-24 16:15:56 +00:00
|
|
|
// printf("draw_slice() y=%d \n",y);
|
2002-04-03 02:39:02 +00:00
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
stride[0]=mpeg2dec->decoder.stride;
|
|
|
|
stride[1]=stride[2]=mpeg2dec->decoder.uv_stride;
|
|
|
|
|
|
|
|
mpcodecs_draw_slice(sh, (uint8_t **)src,
|
2004-06-12 16:35:52 +00:00
|
|
|
stride, info->sequence->picture_width,
|
|
|
|
(y+16<=info->sequence->picture_height) ? 16 :
|
|
|
|
info->sequence->picture_height-y,
|
2002-04-03 02:39:02 +00:00
|
|
|
0, y);
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
// decode a frame
|
|
|
|
static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
|
2006-04-27 02:46:33 +00:00
|
|
|
vd_libmpeg2_ctx_t *context = sh->context;
|
|
|
|
mpeg2dec_t * mpeg2dec = context->mpeg2dec;
|
2003-04-06 16:43:46 +00:00
|
|
|
const mpeg2_info_t * info = mpeg2_info (mpeg2dec);
|
|
|
|
int drop_frame, framedrop=flags&3;
|
2002-04-03 02:39:02 +00:00
|
|
|
|
2004-11-20 14:37:38 +00:00
|
|
|
// MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context
|
2004-11-22 10:20:47 +00:00
|
|
|
mpeg2dec->decoder.convert=NULL;
|
|
|
|
mpeg2dec->decoder.convert_id=NULL;
|
2004-08-26 10:34:20 +00:00
|
|
|
|
2003-10-11 11:21:54 +00:00
|
|
|
if(len<=0) return NULL; // skipped null frame
|
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
// append extra 'end of frame' code:
|
|
|
|
((char*)data+len)[0]=0;
|
|
|
|
((char*)data+len)[1]=0;
|
|
|
|
((char*)data+len)[2]=1;
|
|
|
|
((char*)data+len)[3]=0xff;
|
|
|
|
len+=4;
|
2002-04-03 02:39:02 +00:00
|
|
|
|
2004-08-24 16:15:56 +00:00
|
|
|
if (mpeg2dec->pending_length) {
|
|
|
|
mpeg2_buffer (mpeg2dec, mpeg2dec->pending_buffer, mpeg2dec->pending_buffer + mpeg2dec->pending_length);
|
|
|
|
} else {
|
|
|
|
mpeg2_buffer (mpeg2dec, data, data+len);
|
|
|
|
}
|
2003-04-06 16:43:46 +00:00
|
|
|
|
|
|
|
while(1){
|
|
|
|
int state=mpeg2_parse (mpeg2dec);
|
2004-11-20 14:37:38 +00:00
|
|
|
int type, use_callback;
|
|
|
|
mp_image_t* mpi_new;
|
2006-05-10 22:03:18 +00:00
|
|
|
unsigned long pw, ph;
|
2004-11-20 14:37:38 +00:00
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
switch(state){
|
2004-08-02 11:26:43 +00:00
|
|
|
case STATE_BUFFER:
|
2004-08-24 16:15:56 +00:00
|
|
|
if (mpeg2dec->pending_length) {
|
2004-08-26 12:07:02 +00:00
|
|
|
// just finished the pending data, continue with processing of the passed buffer
|
2004-08-24 16:15:56 +00:00
|
|
|
mpeg2dec->pending_length = 0;
|
|
|
|
mpeg2_buffer (mpeg2dec, data, data+len);
|
|
|
|
} else {
|
|
|
|
// parsing of the passed buffer finished, return.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2003-04-06 16:43:46 +00:00
|
|
|
case STATE_SEQUENCE:
|
2006-05-10 22:03:18 +00:00
|
|
|
pw = info->sequence->display_width * info->sequence->pixel_width;
|
|
|
|
ph = info->sequence->display_height * info->sequence->pixel_height;
|
|
|
|
if(ph) sh->aspect = (float) pw / (float) ph;
|
2003-04-06 16:43:46 +00:00
|
|
|
// video parameters inited/changed, (re)init libvo:
|
2004-11-20 14:37:38 +00:00
|
|
|
if (info->sequence->width >> 1 == info->sequence->chroma_width &&
|
|
|
|
info->sequence->height >> 1 == info->sequence->chroma_height) {
|
|
|
|
if(!mpcodecs_config_vo(sh,
|
2004-11-22 15:32:08 +00:00
|
|
|
info->sequence->picture_width,
|
|
|
|
info->sequence->picture_height, IMGFMT_YV12)) return 0;
|
2004-11-20 14:37:38 +00:00
|
|
|
} else if (info->sequence->width >> 1 == info->sequence->chroma_width &&
|
|
|
|
info->sequence->height == info->sequence->chroma_height) {
|
|
|
|
if(!mpcodecs_config_vo(sh,
|
2004-11-22 15:32:08 +00:00
|
|
|
info->sequence->picture_width,
|
|
|
|
info->sequence->picture_height, IMGFMT_422P)) return 0;
|
2004-11-20 14:37:38 +00:00
|
|
|
} else return 0;
|
2003-04-06 16:43:46 +00:00
|
|
|
break;
|
2004-11-20 14:37:38 +00:00
|
|
|
case STATE_PICTURE:
|
|
|
|
type=info->current_picture->flags&PIC_MASK_CODING_TYPE;
|
2003-04-06 16:43:46 +00:00
|
|
|
|
|
|
|
drop_frame = framedrop && (mpeg2dec->decoder.coding_type == B_TYPE);
|
|
|
|
drop_frame |= framedrop>=2; // hard drop
|
|
|
|
if (drop_frame) {
|
|
|
|
mpeg2_skip(mpeg2dec, 1);
|
|
|
|
//printf("Dropping Frame ...\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mpeg2_skip(mpeg2dec, 0); //mpeg2skip skips frames until set again to 0
|
|
|
|
|
2004-11-22 10:20:47 +00:00
|
|
|
use_callback = (!framedrop && vd_use_slices &&
|
|
|
|
(info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) ?
|
|
|
|
MP_IMGFLAG_DRAW_CALLBACK:0;
|
2004-11-20 14:37:38 +00:00
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
// get_buffer "callback":
|
2004-11-20 14:37:38 +00:00
|
|
|
mpi_new=mpcodecs_get_image(sh,MP_IMGTYPE_IPB,
|
2004-11-22 10:20:47 +00:00
|
|
|
(type==PIC_FLAG_CODING_TYPE_B) ?
|
|
|
|
use_callback : (MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE),
|
2004-12-01 09:35:31 +00:00
|
|
|
info->sequence->width,
|
|
|
|
info->sequence->height);
|
2004-11-20 14:37:38 +00:00
|
|
|
|
2004-08-24 16:15:56 +00:00
|
|
|
if(!mpi_new) return 0; // VO ERROR!!!!!!!!
|
|
|
|
mpeg2_set_buf(mpeg2dec, mpi_new->planes, mpi_new);
|
2006-11-02 16:20:25 +00:00
|
|
|
mpi_new->stride[0] = info->sequence->width;
|
|
|
|
mpi_new->stride[1] = info->sequence->chroma_width;
|
|
|
|
mpi_new->stride[2] = info->sequence->chroma_width;
|
2003-08-03 12:09:58 +00:00
|
|
|
if (info->current_picture->flags&PIC_FLAG_TOP_FIELD_FIRST)
|
2004-08-24 16:15:56 +00:00
|
|
|
mpi_new->fields |= MP_IMGFIELD_TOP_FIRST;
|
|
|
|
else mpi_new->fields &= ~MP_IMGFIELD_TOP_FIRST;
|
2003-08-03 12:09:58 +00:00
|
|
|
if (info->current_picture->flags&PIC_FLAG_REPEAT_FIRST_FIELD)
|
2004-08-24 16:15:56 +00:00
|
|
|
mpi_new->fields |= MP_IMGFIELD_REPEAT_FIRST;
|
|
|
|
else mpi_new->fields &= ~MP_IMGFIELD_REPEAT_FIRST;
|
|
|
|
mpi_new->fields |= MP_IMGFIELD_ORDERED;
|
2006-11-04 07:08:54 +00:00
|
|
|
if (!(info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME))
|
|
|
|
mpi_new->fields |= MP_IMGFIELD_INTERLACED;
|
2002-04-03 02:39:02 +00:00
|
|
|
|
2004-08-02 13:09:17 +00:00
|
|
|
#ifdef MPEG12_POSTPROC
|
2006-04-27 02:46:33 +00:00
|
|
|
mpi_new->qstride=info->sequence->width>>4;
|
|
|
|
{
|
|
|
|
char **p = &context->quant_store[type==PIC_FLAG_CODING_TYPE_B ?
|
|
|
|
2 : (context->quant_store_idx ^= 1)];
|
|
|
|
*p = realloc(*p, mpi_new->qstride*(info->sequence->height>>4));
|
|
|
|
mpi_new->qscale = *p;
|
2004-08-02 13:09:17 +00:00
|
|
|
}
|
2004-08-24 16:15:56 +00:00
|
|
|
mpeg2dec->decoder.quant_store=mpi_new->qscale;
|
|
|
|
mpeg2dec->decoder.quant_stride=mpi_new->qstride;
|
|
|
|
mpi_new->pict_type=type; // 1->I, 2->P, 3->B
|
|
|
|
mpi_new->qscale_type= 1;
|
2004-08-02 13:09:17 +00:00
|
|
|
#endif
|
|
|
|
|
2004-11-22 10:20:47 +00:00
|
|
|
if (mpi_new->flags&MP_IMGFLAG_DRAW_CALLBACK
|
|
|
|
&& !(mpi_new->flags&MP_IMGFLAG_DIRECT)) {
|
|
|
|
// nice, filter/vo likes draw_callback :)
|
|
|
|
mpeg2dec->decoder.convert=draw_slice;
|
|
|
|
mpeg2dec->decoder.convert_id=sh;
|
|
|
|
} else {
|
|
|
|
mpeg2dec->decoder.convert=NULL;
|
|
|
|
mpeg2dec->decoder.convert_id=NULL;
|
2004-11-20 14:37:38 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:43:46 +00:00
|
|
|
break;
|
|
|
|
case STATE_SLICE:
|
|
|
|
case STATE_END:
|
2004-08-02 11:26:43 +00:00
|
|
|
case STATE_INVALID_END:
|
2003-04-06 16:43:46 +00:00
|
|
|
// decoding done:
|
2004-08-24 16:15:56 +00:00
|
|
|
if(info->display_fbuf) {
|
2004-08-26 12:07:02 +00:00
|
|
|
mp_image_t* mpi = info->display_fbuf->id;
|
|
|
|
if (mpeg2dec->pending_length == 0) {
|
2004-11-20 14:37:38 +00:00
|
|
|
mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
|
|
|
|
mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length);
|
|
|
|
memcpy(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
|
2004-08-26 12:07:02 +00:00
|
|
|
} else {
|
|
|
|
// still some data in the pending buffer, shouldn't happen
|
|
|
|
mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start;
|
|
|
|
memmove(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length);
|
|
|
|
mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length + len);
|
|
|
|
memcpy(mpeg2dec->pending_buffer+mpeg2dec->pending_length, data, len);
|
|
|
|
mpeg2dec->pending_length += len;
|
|
|
|
}
|
|
|
|
// fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length);
|
2004-08-24 16:15:56 +00:00
|
|
|
return mpi;
|
|
|
|
}
|
2002-10-29 11:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
2002-04-03 02:39:02 +00:00
|
|
|
}
|