Commit 729eb86d authored by François Grisez's avatar François Grisez
Browse files

Make the ffmpeg's h264 decoder to allocate a buffer for each decoded frame

parent 03d4063a
......@@ -30,11 +30,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
typedef struct _DecData{
mblk_t *yuv_msg;
mblk_t *sps,*pps;
AVFrame* orig;
Rfc3984Context unpacker;
MSPicture outbuf;
MSVideoSize vsize;
struct SwsContext *sws_ctx;
MSAverageFPS fps;
AVCodecContext av_context;
......@@ -71,15 +70,14 @@ static void dec_open(DecData *d){
static void dec_init(MSFilter *f){
DecData *d=(DecData*)ms_new(DecData,1);
ffmpeg_init();
d->yuv_msg=NULL;
d->sps=NULL;
d->pps=NULL;
d->sws_ctx=NULL;
rfc3984_init(&d->unpacker);
d->packet_num=0;
dec_open(d);
d->outbuf.w=0;
d->outbuf.h=0;
d->vsize.width=0;
d->vsize.height=0;
d->bitstream_size=65536;
d->bitstream=ms_malloc0(d->bitstream_size);
d->orig = av_frame_alloc();
......@@ -111,7 +109,6 @@ static void dec_uninit(MSFilter *f){
DecData *d=(DecData*)f->data;
rfc3984_uninit(&d->unpacker);
avcodec_close(&d->av_context);
if (d->yuv_msg) freemsg(d->yuv_msg);
if (d->sps) freemsg(d->sps);
if (d->pps) freemsg(d->pps);
if (d->orig) av_frame_free(&d->orig);
......@@ -121,36 +118,36 @@ static void dec_uninit(MSFilter *f){
static mblk_t *get_as_yuvmsg(MSFilter *f, DecData *s, AVFrame *orig){
AVCodecContext *ctx=&s->av_context;
MSPicture pic = {0};
mblk_t *yuv_msg;
if (s->outbuf.w!=ctx->width || s->outbuf.h!=ctx->height){
if (s->vsize.width!=ctx->width || s->vsize.height!=ctx->height){
if (s->sws_ctx!=NULL){
sws_freeContext(s->sws_ctx);
s->sws_ctx=NULL;
freemsg(s->yuv_msg);
s->yuv_msg=NULL;
}
ms_message("Getting yuv picture of %ix%i",ctx->width,ctx->height);
s->yuv_msg=ms_yuv_buf_alloc(&s->outbuf,ctx->width,ctx->height);
s->outbuf.w=ctx->width;
s->outbuf.h=ctx->height;
s->vsize.width=ctx->width;
s->vsize.height=ctx->height;
s->sws_ctx=sws_getContext(ctx->width,ctx->height,ctx->pix_fmt,
ctx->width,ctx->height,PIX_FMT_YUV420P,SWS_FAST_BILINEAR,
NULL, NULL, NULL);
ms_filter_notify_no_arg(f,MS_FILTER_OUTPUT_FMT_CHANGED);
}
yuv_msg=ms_yuv_buf_alloc(&pic,ctx->width,ctx->height);
#if LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(0,9,0)
if (sws_scale(s->sws_ctx,(const uint8_t * const *)orig->data,orig->linesize, 0,
ctx->height, s->outbuf.planes, s->outbuf.strides)<0){
ctx->height, pic.planes, pic.strides)<0){
#else
if (sws_scale(s->sws_ctx,(uint8_t **)orig->data,orig->linesize, 0,
ctx->height, s->outbuf.planes, s->outbuf.strides)<0){
ctx->height, pic.planes, pic.strides)<0){
#endif
ms_error("%s: error in sws_scale().",f->desc->name);
}
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(50,43,0) // backward compatibility with Debian Squeeze (6.0)
mblk_set_timestamp_info(s->yuv_msg, orig->pkt_pts);
mblk_set_timestamp_info(yuv_msg, orig->pkt_pts);
#endif
return dupmsg(s->yuv_msg);
return yuv_msg;
}
static void update_sps(DecData *d, mblk_t *sps){
......@@ -319,7 +316,7 @@ static void dec_process(MSFilter *f){
ms_filter_notify_no_arg(f,MS_VIDEO_DECODER_FIRST_IMAGE_DECODED);
}
if (ms_average_fps_update(&d->fps, f->ticker->time)) {
ms_message("ffmpeg H264 decoder: Frame size: %dx%d", d->outbuf.w, d->outbuf.h);
ms_message("ffmpeg H264 decoder: Frame size: %dx%d", d->vsize.width, d->vsize.height);
}
}
}
......@@ -354,8 +351,8 @@ static int dec_get_vsize(MSFilter *f, void *data) {
DecData *d = (DecData *)f->data;
MSVideoSize *vsize = (MSVideoSize *)data;
if (d->first_image_decoded == TRUE) {
vsize->width = d->outbuf.w;
vsize->height = d->outbuf.h;
vsize->width = d->vsize.width;
vsize->height = d->vsize.height;
} else {
vsize->width = MS_VIDEO_SIZE_UNKNOWN_W;
vsize->height = MS_VIDEO_SIZE_UNKNOWN_H;
......@@ -371,7 +368,7 @@ static int dec_get_fps(MSFilter *f, void *data){
static int dec_get_outfmt(MSFilter *f, void *data){
DecData *s = (DecData *)f->data;
((MSPinFormat*)data)->fmt=ms_factory_get_video_format(f->factory,"YUV420P",ms_video_size_make(s->outbuf.w,s->outbuf.h),0,NULL);
((MSPinFormat*)data)->fmt=ms_factory_get_video_format(f->factory,"YUV420P",ms_video_size_make(s->vsize.width,s->vsize.height),0,NULL);
return 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment