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);
...全文
358 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库没找到这个接口,不知该如何改动?
内容概要:本文深入探讨了DMA高效数据传输实现方案在高性能计算芯片领域的应用与架构创新,重点析了缓存一致性DMA、多通道DMA架构及其在数据中心SmartNIC、存算一体芯片和Chiplet互连等场景中的实践。文章结合RISC-V架构,通过Chisel硬件描述语言和C语言驱动代码,展示了多通道DMA控制器的设计与实现,涵盖仲裁机制、AXI总线适配、散-聚集传输模式及中断处理等核心技术,并强调了性能优化与验证方法。最后展望了AI调度、光互连、近存计算与安全DMA等未来发展方向。; 适合人群:具备数字电路与计算机体系结构基础,从事芯片设计、嵌入式开发或高性能计算相关工作的研发人员,尤其是有1-5年经验的工程师与研究人员。; 使用场景及目标:①理解DMA在突破“内存墙”和降低系统能耗中的关键技术路径;②掌握多通道DMA控制器的硬件设计与驱动开发方法;③应用于SmartNIC、AI加速器、Chiplet等高性能芯片系统的数据传输架构设计;④为构建高带宽、低延迟、高能效的异构计算平台提供参考。; 阅读建议:此资源融合硬件设计与软件驱动,建议结合Chisel仿真与RISC-V平台实操,重点关注DMA与缓存一致性、异构计算单元的协同机制,并通过性能计数器与错误注入手段进行系统级验证。
内容概要:本文围绕基于共享储能服务的智能楼宇双层优化配置展开研究,通过Matlab代码实现相应的数学建模与仿真析,提出一种结合上层规划与下层运行的协同优化框架,旨在提升智能楼宇能源系统的经济性、能效水平与电网互动能力。研究充考虑光伏发电、负荷需求、储能充放电等多元因素,采用先进的优化算法(如智能优化算法)对共享储能资源的容量配置与运行调度进行精细化决策,有效降低用能成本,提高可再生能源消纳率,并增强系统运行的稳定性与灵活性。全文涵盖模型构建、算法设计、求解流程及结果验证,具备较高的理论深度与工程应用价值; 适合人群:具备电力系统、能源管理、优化算法等相关背景的科研人员、研究生,以及从事智能电网、综合能源系统、建筑节能等领域的工程技术人员; 使用场景及目标:①用于智能楼宇及园区级能源系统的规划与运行优化研究;②支撑共享储能机制下的资源配置、经济调度与商业模式设计;③作为Matlab仿真教学与高水平论文复现的典型案例,帮助深入理解双层优化模型、主从博弈结构及智能算法在能源系统中的应用; 阅读建议:建议结合提供的Matlab代码进行同步学习与调试,重点关注上下层模型的耦合关系与迭代求解过程,可进一步拓展至多主体协同、不确定性建模(如风光出力波动)及鲁棒优化等前沿方向开展深化研究。
内容概要:本文《【故障检测】基于 KPCA 的故障检测【T2 和 Q 统计指数的可视化】(Matlab代码实现)》系统阐述了基于核主成析(KPCA)的非线性故障检测方法,重点实现了T²和Q统计量的构建与可视化过程。通过Matlab编程,将高维非线性数据映射至特征空间,提取主成并建立监控模型,利用T²和Q指数对工业过程中的异常状态进行联合监测与诊断,有效提升了复杂系统中早期故障的识别能力,具有较强的工程实用性与理论参考价值。; 适合人群:适用于具备信号处理、控制工程或工业过程监测背景,熟悉Matlab编程语言,并从事故障诊断、智能运维、自动化系统研发等相关工作的研究生、科研人员及工程技术开发者。; 使用场景及目标:①应用于化工、电力、制造等流程工业中的关键设备状态监控与早期故障预警;②作为学术研究中KPCA算法的仿真验证平台,用于对比析不同非线性降维方法的检测性能;③深化对非线性过程监控中统计指标设计与阈值判定机制的理解与实践应用。; 阅读建议:建议读者结合所提供的Matlab代码逐模块运行与调试,深入掌握KPCA建模流程、主成子空间划及T²、Q统计量的计算逻辑,鼓励在标准数据集(如TE过程)上复现实验结果,并尝试扩展至其他非线性场景以提升模型泛化能力。

2,554

社区成员

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

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