之前在從事FFmpeg相關工作的時候,其實早就想寫這篇文章,但是由于一些雜事就給擱置了,最近因為逛技術部落格看到“Floyd Steinberg Dither”算法,才想起來之前有篇關于“提高GIF壓縮品質”的文章,一直還沒有總結,怕再次耽擱,是以趕緊提筆記錄之。
1.解碼GIF
FFmpeg 解碼 GIF 其實和解碼普通的視訊沒太大差別,廢話不多說,請看代碼:
// gif_decode_encode_test.cpp : 定義控制台應用程式的入口點。
//
#include "stdafx.h"
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
};
#endif
#endif
int main(int argc, char* argv[])
{
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameYUV;
uint8_t *out_buffer;
AVPacket *packet;
int y_size;
int ret, got_picture;
struct SwsContext *img_convert_ctx;
char filepath[] = "..//deer.gif";
FILE *fp_yuv = fopen("..//output.yuv", "wb+");
av_register_all();
//avformat_network_init();
pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0)
{
printf("Couldn't open input stream.\n");
return -1;
}
if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
printf("Couldn't find stream information.\n");
return -1;
}
videoindex = -1;
for(i = 0; i < pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoindex = i;
break;
}
}
if(videoindex == -1)
{
printf("Didn't find a video stream.\n");
return -1;
}
pCodecCtx = pFormatCtx->streams[videoindex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id); // AV_CODEC_ID_GIF
if(pCodec == NULL)
{
printf("Codec not found.\n");
return -1;
}
if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("Could not open codec.\n");
return -1;
}
pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
out_buffer = (uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
packet = (AVPacket *)av_malloc(sizeof(AVPacket));
//Output Info-----------------------------
printf("--------------- File Information ----------------\n");
av_dump_format(pFormatCtx, 0, filepath, 0);
printf("-------------------------------------------------\n");
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P,
SWS_BICUBIC, NULL, NULL, NULL);
int count = 0;
while(count <= 10)
{
ret = av_read_frame(pFormatCtx, packet);
if (ret < 0)
{
count++;
av_seek_frame(pFormatCtx, -1, 0 * AV_TIME_BASE, AVSEEK_FLAG_ANY);
}
if(packet->stream_index == videoindex)
{
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if(ret < 0)
{
printf("Decode Error.\n");
return -1;
}
if(got_picture)
{
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
pFrameYUV->data, pFrameYUV->linesize);
y_size = pCodecCtx->width * pCodecCtx->height;
fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Y
fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //U
fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //V
printf("Succeed to decode 1 frame!\n");
}
}
av_free_packet(packet);
}
//flush decoder
//FIX: Flush Frames remained in Codec
while(1)
{
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if(ret < 0)
break;
if(!got_picture)
break;
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
pFrameYUV->data, pFrameYUV->linesize);
int y_size = pCodecCtx->width * pCodecCtx->height;
fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Y
fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //U
fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //V
printf("Flush Decoder: Succeed to decode 1 frame!\n");
}
sws_freeContext(img_convert_ctx);
fclose(fp_yuv);
av_frame_free(&pFrameYUV);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
以上代碼很簡單,就是将GIF逐幀解碼出來,然後再重新編碼為YUV格式。
2.編碼GIF:
網上有相當豐富的編碼GIF的方法:Awsome GIF —— 真心是好東西,收集得灰常全面!
但是這裡我隻介紹FFmpeg的,其他大家感興趣可以去深挖。
其實FFmpeg的源碼(libavcodec/gif.c) 是有一個叫GIF encoder的東東,但是發現國内國外網上幾乎沒人用!?大多數人都是用指令行來編碼GIF的(真的簡單友善多了),那麼我也就随大流咯~
既然網上方法很多,那我也不贅述,就推薦幾篇文章給大家把:
1. 《Quick Tip: create GIFs of your apps》
2. 《使用 FFmpeg 處理高品質 GIF 圖檔》(這篇是本文開頭那篇的中文版,但是國人的翻譯,你懂得,建議還是直接看原版)
在Android平台也是可以使用shell指令的,因為我主要是做底層開發,是以我偏愛用NDK來調用shell,當然Java層也是可以調用的。【NDK執行shell指令的例子】和【還有一篇英文】【Java層執行shell指令】【還有大神寫好的Bash】(真慶幸當初沒有傻傻死磕ffmpeg的GIF encoder,不然怎麼死都不知道,做技術也要選對路,選對了,就會感覺到全世界都在幫你)
另外除了用FFmpeg來編碼GIF,Android NDK 還可以用gifflen,調用接口也是相當的簡單!
綜上,就是我對編解碼GIF的一點小總結,希望能夠幫助到和我一樣的同學。