200分求助gif编码的问题,使用libungif库或ffmpeg库

qiujian5628 2011-11-03 09:23:31
使用动态库的方式编码gif动画图片,发现如下几个问题:
1、用libungif,编码的图片颜色不对,主要代码如下,不知道是哪的问题?(libungif不支持压缩,还得自己添加压缩代码,哪位达人有经验的,帮忙指点指点,谢谢了。)
colormap = MakeMapObject(colormap_size, NULL);

for (i = 0; i < width*height; i++)
{
red[i] = (GifByteType) rgb[3*i ];
green[i] = (GifByteType) rgb[3*i+1];
blue[i] = (GifByteType) rgb[3*i+2];
}

if (QuantizeBuffer(width, height, &colormap_size, red, green, blue,
buffer, colormap->Colors) == GIF_ERROR)
{
PrintGifError();
return(-1);
}

outfile = EGifOpenFileName((char *) filename, FALSE);
if (outfile == NULL)
{
PrintGifError();
return(-1);
}

EGifSetGifVersion("87a"); //两种版本的GIF规格,GIF87a和GIF89a。主要的区别在于GIF89a允许动画
if (EGifPutScreenDesc(outfile, width, height, colormap_size, 0, colormap)
== GIF_ERROR)
{
PrintGifError();
return(-1);
}

if (EGifPutImageDesc(outfile, 0, 0, width, height, 0, NULL)
== GIF_ERROR)
{
PrintGifError();
return(-1);
}

ptr = buffer;
for (i = 0; i < height; i++)
{
if (EGifPutLine(outfile, ptr, width) == GIF_ERROR)
{
PrintGifError();
return(-1);
}
ptr += width;
}

EGifSpew(outfile);

if (EGifCloseFile(outfile) == GIF_ERROR)
PrintGifError();

2、使用ffmpeg库编码gif图片,也有问题,流程如下,不知道是哪个地方出错了。
AVFormatContext *o_fctx;
AVOutputFormat *o_fmt;
AVStream *o_st;
AVCodecContext *o_cctx;

AVCodec *codec;
int i, out_size, outbuf_size;
AVFrame *picture,*tmp_picture;
uint8_t *outbuf, *picture_buf,*tmp_picture_buf;

av_register_all();
//format
o_fmt = av_guess_format(NULL, filename, NULL);
if (!o_fmt)
{
o_fmt = av_guess_format("gif", NULL, NULL);
}

o_fctx = avformat_alloc_context();
if (!o_fctx)
{
fprintf(stderr, "avformat_alloc_context error\n");
return;
}
o_fctx->oformat = o_fmt;
av_strlcpy(o_fctx->filename, filename, sizeof(o_fctx->filename));


o_st = av_new_stream(o_fctx, 0);
if (!o_st)
{
fprintf(stderr, "av_new_stream error\n");
return;
}
o_cctx = o_st->codec;
o_cctx->codec_id = o_fmt->video_codec; //; CODEC_ID_GIF;/
o_cctx->codec_type = AVMEDIA_TYPE_VIDEO;
o_cctx->bit_rate = 400000;
/* resolution must be a multiple of two */
o_cctx->width = 352;
o_cctx->height = 288;
/* frames per second */
//c->time_base = (AVRational){1,25};
o_cctx->time_base.num=1;
o_cctx->time_base.den=25;

o_cctx->gop_size = 10; /* emit one intra frame every ten frames */
o_cctx->max_b_frames=1;
o_cctx->pix_fmt=PIX_FMT_RGB24;

if (av_set_parameters(o_fctx, NULL) < 0) {
fprintf(stderr, "Invalid output format parameters\n");
exit(1);
}

dump_format(o_fctx, 0, filename, 1);


printf("Video encoding\n");

/* find the mpeg1 video encoder */
codec = avcodec_find_encoder( o_fmt->video_codec );//CODEC_ID_GIF);//
if (!codec) {
fprintf(stderr, "codec not found\n");
return;
}

/* open it */
if (avcodec_open(o_cctx, codec) < 0) {
fprintf(stderr, "could not open codec\n");
return;
}

if (!(o_fctx->oformat->flags & AVFMT_RAWPICTURE)) {
/* allocate output buffer */
/* XXX: API change will be done */
outbuf_size = 200000;
outbuf = (uint8_t *)malloc(outbuf_size);
}

picture= avcodec_alloc_frame();
int numBytes = avpicture_get_size( o_cctx->pix_fmt, o_cctx->width, o_cctx->height);
picture_buf = (uint8_t *)malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)(picture), picture_buf, o_cctx->pix_fmt,o_cctx->width, o_cctx->height);

tmp_picture=NULL;
tmp_picture_buf=NULL;
if ( o_cctx->pix_fmt != PIX_FMT_YUV420P )
{
tmp_picture= avcodec_alloc_frame();
numBytes = avpicture_get_size(PIX_FMT_YUV420P, o_cctx->width, o_cctx->height);
tmp_picture_buf = (uint8_t *)malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)(tmp_picture), tmp_picture_buf, PIX_FMT_YUV420P,o_cctx->width, o_cctx->height);
}

/* open the file */
if ( url_fopen(&o_fctx->pb, filename, URL_WRONLY) < 0) {
fprintf(stderr, "url_fopen error '%s'\n",filename);
return;
}
av_write_header(o_fctx);

/* encode 1 second of video */
for(i=0;i<25;i++)
{
if (o_cctx->pix_fmt != PIX_FMT_YUV420P) {
/* as we only generate a YUV420P picture, we must convert it
to the codec pixel format if needed */
fill_yuv_image(tmp_picture, i, o_cctx->width, o_cctx->height);
static struct SwsContext *img_convert_ctx;
// Convert the image into YUV format that SDL uses
if(img_convert_ctx == NULL)
{
img_convert_ctx = sws_getContext(o_cctx->width,o_cctx->height,
PIX_FMT_YUV420P,
o_cctx->width, o_cctx->height, o_cctx->pix_fmt, SWS_BICUBIC,
NULL, NULL, NULL);
}

int ret = sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize, 0, o_cctx->height, picture->data, picture->linesize);
if ( ret < 0 )
{
fprintf(stderr, "sws_scale error\n");
return;
}
}
else
{
fill_yuv_image(picture, i, o_cctx->width, o_cctx->height);
}

if ( o_fctx->oformat->flags & AVFMT_RAWPICTURE)
{
/* raw video case. The API will change slightly in the near
futur for that */
AVPacket pkt;
av_init_packet(&pkt);

//pkt.flags |= PKT_FLAG_KEY;
pkt.stream_index= o_st->index;
pkt.data= (uint8_t *)picture;
pkt.size= sizeof(AVPicture);

av_write_frame(o_fctx, &pkt);
}
else
{

/* encode the image */
out_size = avcodec_encode_video(o_cctx, outbuf, outbuf_size, picture);

AVPacket pkt;
av_init_packet(&pkt);
pkt.data= (uint8_t *)outbuf;
pkt.size= out_size;
pkt.stream_index= o_st->index;
pkt.pts= o_cctx->coded_frame->pts;

av_write_frame(o_fctx,&pkt);
}
}

av_write_trailer(o_fctx);
...全文
264 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Huer 2012-10-10
  • 打赏
  • 举报
回复
您好!为什么我用libungif在windows系统下不能正确运行呢。。。。
qiujian5628 2011-11-09
  • 打赏
  • 举报
回复
最后确定libunfig,及ffmpeg编码的gif,都严重颜色失真。因此没有再走这条路,而是采用convert编码gif图片。
CyberLogix 2011-11-03
  • 打赏
  • 举报
回复
libgif有例子程序你可一参考,是你调用有问题
qiujian5628 2011-11-03
  • 打赏
  • 举报
回复
我是参考了libgif的rgb2gif程序写的,你可以看看我那段代码,我的rgb数据是从ffmpeg的AVPicture,RGB24格式数据,此数据用libjpeg转jpeg没有问题。但是就是用这个libungif转出来的图片有问题,颜色质量不高,我怀疑是gif的全局调色板没设好,但是libungif库没找到这个接口,不知该如何改动?

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧