基于ffmpeg 编解码 GIF 【PC】【Android】
之前在从事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的一点小总结,希望能够帮助到和我一样的同学。
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/52253131
- 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)